]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/fuzz-unit-file.c
core: split out core/manager-dump.[ch]
[thirdparty/systemd.git] / src / core / fuzz-unit-file.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
ec7a02ea
ZJS
2
3#include "conf-parser.h"
4#include "fd-util.h"
5#include "fileio.h"
6#include "fuzz.h"
7#include "install.h"
8#include "load-fragment.h"
2a341bb9 9#include "manager-dump.h"
ec7a02ea 10#include "string-util.h"
2d3b784d 11#include "unit-serialize.h"
d6ea3b78 12#include "utf8.h"
ec7a02ea
ZJS
13
14int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
15 _cleanup_free_ char *out = NULL; /* out should be freed after g */
16 size_t out_size;
17 _cleanup_fclose_ FILE *f = NULL, *g = NULL;
18 _cleanup_free_ char *p = NULL;
19 UnitType t;
20 _cleanup_(manager_freep) Manager *m = NULL;
21 Unit *u;
22 const char *name;
bd0763b6 23 long offset;
ec7a02ea
ZJS
24
25 if (size == 0)
26 return 0;
27
673a1e6f 28 f = fmemopen_unlocked((char*) data, size, "re");
ec7a02ea
ZJS
29 assert_se(f);
30
31 if (read_line(f, LINE_MAX, &p) < 0)
32 return 0;
33
34 t = unit_type_from_string(p);
35 if (t < 0)
36 return 0;
37
38 if (!unit_vtable[t]->load)
39 return 0;
40
bd0763b6
ZJS
41 offset = ftell(f);
42 assert_se(offset >= 0);
43
44 for (;;) {
45 _cleanup_free_ char *l = NULL;
af3865ab 46 const char *ll;
bd0763b6 47
483ed8a6 48 if (read_line(f, LONG_LINE_MAX, &l) <= 0)
bd0763b6
ZJS
49 break;
50
d6ea3b78
ZJS
51 ll = startswith(l, UTF8_BYTE_ORDER_MARK) ?: l;
52 ll = ll + strspn(ll, WHITESPACE);
af3865ab 53
b834c6ce 54 if (HAS_FEATURE_MEMORY_SANITIZER && startswith(ll, "ListenNetlink")) {
bd0763b6
ZJS
55 /* ListenNetlink causes a false positive in msan,
56 * let's skip this for now. */
483ed8a6 57 log_notice("Skipping test because ListenNetlink= is present");
bd0763b6 58 return 0;
483ed8a6 59 }
bd0763b6
ZJS
60 }
61
62 assert_se(fseek(f, offset, SEEK_SET) == 0);
63
b872843c
ZJS
64 /* We don't want to fill the logs with messages about parse errors.
65 * Disable most logging if not running standalone */
66 if (!getenv("SYSTEMD_LOG_LEVEL"))
67 log_set_max_level(LOG_CRIT);
68
ec7a02ea
ZJS
69 assert_se(manager_new(UNIT_FILE_SYSTEM, MANAGER_TEST_RUN_MINIMAL, &m) >= 0);
70
71 name = strjoina("a.", unit_type_to_string(t));
72 assert_se(unit_new_for_name(m, unit_vtable[t]->object_size, name, &u) >= 0);
73
4f9ff96a
LP
74 (void) config_parse(
75 name, name, f,
76 UNIT_VTABLE(u)->sections,
77 config_item_perf_lookup, load_fragment_gperf_lookup,
7ade8982 78 0,
4f9ff96a
LP
79 u,
80 NULL);
ec7a02ea 81
673a1e6f 82 g = open_memstream_unlocked(&out, &out_size);
ec7a02ea
ZJS
83 assert_se(g);
84
85 unit_dump(u, g, "");
4832ce7e 86 manager_dump(m, g, ">>>");
ec7a02ea
ZJS
87
88 return 0;
89}