udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / fuzz-unit-file.c
bloba11d6b53b5e4181e37f5d75792e9243d713b28da
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "conf-parser.h"
4 #include "fd-util.h"
5 #include "fuzz.h"
6 #include "install.h"
7 #include "load-fragment.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"
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;
23 if (outside_size_range(size, 0, 65536))
24 return 0;
26 f = data_to_file(data, size);
28 assert_se(f);
30 if (read_line(f, LINE_MAX, &p) < 0)
31 return 0;
33 t = unit_type_from_string(p);
34 if (t < 0)
35 return 0;
37 if (!unit_vtable[t]->load)
38 return 0;
40 offset = ftell(f);
41 assert_se(offset >= 0);
43 for (;;) {
44 _cleanup_free_ char *l = NULL;
45 const char *ll;
47 if (read_line(f, LONG_LINE_MAX, &l) <= 0)
48 break;
50 ll = startswith(l, UTF8_BYTE_ORDER_MARK) ?: l;
51 ll = ll + strspn(ll, WHITESPACE);
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;
61 assert_se(fseek(f, offset, SEEK_SET) == 0);
63 /* We don't want to fill the logs with messages about parse errors.
64 * Disable most logging if not running standalone */
65 if (!getenv("SYSTEMD_LOG_LEVEL"))
66 log_set_max_level(LOG_CRIT);
68 assert_se(manager_new(RUNTIME_SCOPE_SYSTEM, MANAGER_TEST_RUN_MINIMAL, &m) >= 0);
70 name = strjoina("a.", unit_type_to_string(t));
71 assert_se(unit_new_for_name(m, unit_vtable[t]->object_size, name, &u) >= 0);
73 (void) config_parse(
74 name, name, f,
75 UNIT_VTABLE(u)->sections,
76 config_item_perf_lookup, load_fragment_gperf_lookup,
79 NULL);
81 _cleanup_(memstream_done) MemStream ms = {};
82 FILE *g;
84 assert_se(g = memstream_init(&ms));
85 unit_dump(u, g, "");
86 manager_dump(m, g, /* patterns= */ NULL, ">>>");
88 return 0;