Debian Codegraph @

src/fuzz/fuzz-main.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "alloc-util.h"
6 #include "fileio.h"
7 #include "fuzz.h"
8 #include "log.h"
9 #include "parse-util.h"
10 #include "string-util.h"
11 #include "tests.h"
12
13 /* This is a test driver for the systemd fuzzers that provides main function
14 * for regression testing outside of oss-fuzz (https://github.com/google/oss-fuzz)
15 *
16 * It reads files named on the command line and passes them one by one into the
17 * fuzzer that it is compiled into. */
18
19 /* This one was borrowed from
20 * https://github.com/google/oss-fuzz/blob/646fca1b506b056db3a60d32c4a1a7398f171c94/infra/base-images/base-runner/bad_build_check#L19
21 */
22 #define NUMBER_OF_RUNS 4
23
24 int main(int argc, char **argv) {
25 int r;
26
27 test_setup_logging(LOG_DEBUG);
28
29 unsigned number_of_runs = NUMBER_OF_RUNS;
30
31 const char *v = getenv("SYSTEMD_FUZZ_RUNS");
32 if (!isempty(v)) {
33 r = safe_atou(v, &number_of_runs);
34 if (r < 0)
35 return log_error_errno(r, "Failed to parse SYSTEMD_FUZZ_RUNS=%s: %m", v);
36 }
37
38 for (int i = 1; i < argc; i++) {
39 _cleanup_free_ char *buf = NULL;
40 size_t size;
41 char *name;
42
43 name = argv[i];
44 r = read_full_file(name, &buf, &size);
45 if (r < 0) {
46 log_error_errno(r, "Failed to open '%s': %m", name);
47 return EXIT_FAILURE;
48 }
49 printf("%s... ", name);
50 fflush(stdout);
51 for (unsigned j = 0; j < number_of_runs; j++)
52 if (LLVMFuzzerTestOneInput((uint8_t*)buf, size) == EXIT_TEST_SKIP)
53 return EXIT_TEST_SKIP;
54 printf("ok\n");
55 }
56
57 return EXIT_SUCCESS;
58 }