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