Debian Codegraph @

src/core/fuzz-unit-file.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "conf-parser.h"
4 #include "fd-util.h"
5 #include "fuzz.h"
6 #include "load-fragment.h"
7 #include "manager.h"
8 #include "manager-dump.h"
9 #include "memstream-util.h"
10 #include "string-util.h"
11 #include "unit-serialize.h"
12 #include "utf8.h"
13
14 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
15 _cleanup_fclose_ FILE *f = NULL;
16 _cleanup_free_ char *p = NULL;
17 UnitType t;
18 _cleanup_(manager_freep) Manager *m = NULL;
19 Unit *u;
20 const char *name;
21 long offset;
22
23 if (outside_size_range(size, 0, 65536))
24 return 0;
25
26 f = data_to_file(data, size);
27
28 assert_se(f);
29
30 if (read_line(f, LINE_MAX, &p) < 0)
31 return 0;
32
33 t = unit_type_from_string(p);
34 if (t < 0)
35 return 0;
36
37 if (!unit_vtable[t]->load)
38 return 0;
39
40 offset = ftell(f);
41 assert_se(offset >= 0);
42
43 for (;;) {
44 _cleanup_free_ char *l = NULL;
45 const char *ll;
46
47 if (read_line(f, LONG_LINE_MAX, &l) <= 0)
48 break;
49
50 ll = startswith(l, UTF8_BYTE_ORDER_MARK) ?: l;
51 ll = ll + strspn(ll, WHITESPACE);
52
53 if (HAS_FEATURE_MEMORY_SANITIZER && startswith(ll, "ListenNetlink")) {
54 /* ListenNetlink causes a false positive in msan,
55 * let's skip this for now. */
56 log_notice("Skipping test because ListenNetlink= is present");
57 return 0;
58 }
59 }
60
61 assert_se(fseek(f, offset, SEEK_SET) == 0);
62
63 fuzz_setup_logging();
64
65 assert_se(manager_new(RUNTIME_SCOPE_SYSTEM, MANAGER_TEST_RUN_MINIMAL|MANAGER_TEST_DONT_OPEN_EXECUTOR, &m) >= 0);
66
67 name = strjoina("a.", unit_type_to_string(t));
68 assert_se(unit_new_for_name(m, unit_vtable[t]->object_size, name, &u) >= 0);
69
70 (void) config_parse(
71 name, name, f,
72 UNIT_VTABLE(u)->sections,
73 config_item_perf_lookup, load_fragment_gperf_lookup,
74 0,
75 u,
76 NULL);
77
78 _cleanup_(memstream_done) MemStream ms = {};
79 FILE *g;
80
81 assert_se(g = memstream_init(&ms));
82 unit_dump(u, g, "");
83 manager_dump(m, g, /* patterns= */ NULL, ">>>");
84
85 return 0;
86 }