/src/qemu/tests/qtest/fuzz/fuzz.c
Line | Count | Source |
1 | | /* |
2 | | * fuzzing driver |
3 | | * |
4 | | * Copyright Red Hat Inc., 2019 |
5 | | * |
6 | | * Authors: |
7 | | * Alexander Bulekov <alxndr@bu.edu> |
8 | | * |
9 | | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
10 | | * See the COPYING file in the top-level directory. |
11 | | * |
12 | | */ |
13 | | |
14 | | #include "qemu/osdep.h" |
15 | | |
16 | | #include <wordexp.h> |
17 | | |
18 | | #include "qemu/cutils.h" |
19 | | #include "qemu/datadir.h" |
20 | | #include "system/system.h" |
21 | | #include "system/qtest.h" |
22 | | #include "system/runstate.h" |
23 | | #include "qemu/main-loop.h" |
24 | | #include "qemu/rcu.h" |
25 | | #include "qemu/target-info-qom.h" |
26 | | #include "tests/qtest/libqtest.h" |
27 | | #include "tests/qtest/libqos/qgraph.h" |
28 | | #include "fuzz.h" |
29 | | |
30 | 47.5M | #define MAX_EVENT_LOOPS 10 |
31 | | |
32 | | typedef struct FuzzTargetState { |
33 | | FuzzTarget *target; |
34 | | QSLIST_ENTRY(FuzzTargetState) target_list; |
35 | | } FuzzTargetState; |
36 | | |
37 | | typedef QSLIST_HEAD(, FuzzTargetState) FuzzTargetList; |
38 | | |
39 | | static const char *fuzz_arch = TARGET_NAME; |
40 | | |
41 | | static FuzzTargetList *fuzz_target_list; |
42 | | static FuzzTarget *fuzz_target; |
43 | | static QTestState *fuzz_qts; |
44 | | |
45 | | int (*qemu_main)(void); |
46 | | |
47 | | |
48 | | void flush_events(QTestState *s) |
49 | 47.5M | { |
50 | 47.5M | int i = MAX_EVENT_LOOPS; |
51 | 106M | while (g_main_context_pending(NULL) && i-- > 0) { |
52 | 58.5M | main_loop_wait(false); |
53 | 58.5M | } |
54 | 47.5M | } |
55 | | |
56 | | void fuzz_reset(QTestState *s) |
57 | 81.9k | { |
58 | 81.9k | qemu_system_reset(SHUTDOWN_CAUSE_GUEST_RESET); |
59 | 81.9k | main_loop_wait(true); |
60 | 81.9k | } |
61 | | |
62 | | static QTestState *qtest_setup(void) |
63 | 61 | { |
64 | 61 | qtest_server_set_send_handler(&qtest_client_inproc_recv, &fuzz_qts); |
65 | 61 | return qtest_inproc_init(&fuzz_qts, false, fuzz_arch, |
66 | 61 | &qtest_server_inproc_recv); |
67 | 61 | } |
68 | | |
69 | | void fuzz_add_target(const FuzzTarget *target) |
70 | 2.80k | { |
71 | 2.80k | FuzzTargetState *tmp; |
72 | 2.80k | FuzzTargetState *target_state; |
73 | 2.80k | if (!fuzz_target_list) { |
74 | 61 | fuzz_target_list = g_new0(FuzzTargetList, 1); |
75 | 61 | } |
76 | | |
77 | 63.1k | QSLIST_FOREACH(tmp, fuzz_target_list, target_list) { |
78 | 63.1k | if (g_strcmp0(tmp->target->name, target->name) == 0) { |
79 | 0 | fprintf(stderr, "Error: Fuzz target name %s already in use\n", |
80 | 0 | target->name); |
81 | 0 | abort(); |
82 | 0 | } |
83 | 63.1k | } |
84 | 2.80k | target_state = g_new0(FuzzTargetState, 1); |
85 | 2.80k | target_state->target = g_new0(FuzzTarget, 1); |
86 | 2.80k | *(target_state->target) = *target; |
87 | 2.80k | QSLIST_INSERT_HEAD(fuzz_target_list, target_state, target_list); |
88 | 2.80k | } |
89 | | |
90 | | |
91 | | |
92 | | static void usage(char *path) |
93 | 0 | { |
94 | 0 | printf("Usage: %s --fuzz-target=FUZZ_TARGET [LIBFUZZER ARGUMENTS]\n", path); |
95 | 0 | printf("where FUZZ_TARGET is one of:\n"); |
96 | 0 | FuzzTargetState *tmp; |
97 | 0 | if (!fuzz_target_list) { |
98 | 0 | fprintf(stderr, "Fuzz target list not initialized\n"); |
99 | 0 | abort(); |
100 | 0 | } |
101 | 0 | QSLIST_FOREACH(tmp, fuzz_target_list, target_list) { |
102 | 0 | printf(" * %s : %s\n", tmp->target->name, |
103 | 0 | tmp->target->description); |
104 | 0 | } |
105 | 0 | printf("Alternatively, add -target-FUZZ_TARGET to the executable name\n\n" |
106 | 0 | "Set the environment variable FUZZ_SERIALIZE_QTEST=1 to serialize\n" |
107 | 0 | "QTest commands into an ASCII protocol. Useful for building crash\n" |
108 | 0 | "reproducers, but slows down execution.\n\n" |
109 | 0 | "Set the environment variable QTEST_LOG=fuzz to log all qtest commands" |
110 | 0 | "\n"); |
111 | 0 | exit(0); |
112 | 0 | } |
113 | | |
114 | | static FuzzTarget *fuzz_get_target(char* name) |
115 | 61 | { |
116 | 61 | FuzzTargetState *tmp; |
117 | 61 | if (!fuzz_target_list) { |
118 | 0 | fprintf(stderr, "Fuzz target list not initialized\n"); |
119 | 0 | abort(); |
120 | 0 | } |
121 | | |
122 | 1.31k | QSLIST_FOREACH(tmp, fuzz_target_list, target_list) { |
123 | 1.31k | if (strcmp(tmp->target->name, name) == 0) { |
124 | 61 | return tmp->target; |
125 | 61 | } |
126 | 1.31k | } |
127 | 0 | return NULL; |
128 | 61 | } |
129 | | |
130 | | |
131 | | /* Sometimes called by libfuzzer to mutate two inputs into one */ |
132 | | size_t LLVMFuzzerCustomCrossOver(const uint8_t *data1, size_t size1, |
133 | | const uint8_t *data2, size_t size2, |
134 | | uint8_t *out, size_t max_out_size, |
135 | | unsigned int seed) |
136 | 0 | { |
137 | 0 | if (fuzz_target->crossover) { |
138 | 0 | return fuzz_target->crossover(data1, size1, data2, size2, out, |
139 | 0 | max_out_size, seed); |
140 | 0 | } |
141 | 0 | return 0; |
142 | 0 | } |
143 | | |
144 | | /* Executed for each fuzzing-input */ |
145 | | int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size) |
146 | 81.9k | { |
147 | | /* |
148 | | * Do the pre-fuzz-initialization before the first fuzzing iteration, |
149 | | * instead of before the actual fuzz loop. This is needed since libfuzzer |
150 | | * may fork off additional workers, prior to the fuzzing loop, and if |
151 | | * pre_fuzz() sets up e.g. shared memory, this should be done for the |
152 | | * individual worker processes |
153 | | */ |
154 | 81.9k | static int pre_fuzz_done; |
155 | 81.9k | if (!pre_fuzz_done && fuzz_target->pre_fuzz) { |
156 | 23 | fuzz_target->pre_fuzz(fuzz_qts); |
157 | 23 | pre_fuzz_done = true; |
158 | 23 | } |
159 | | |
160 | 81.9k | fuzz_target->fuzz(fuzz_qts, Data, Size); |
161 | 81.9k | return 0; |
162 | 81.9k | } |
163 | | |
164 | | /* Executed once, prior to fuzzing */ |
165 | | int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp) |
166 | 61 | { |
167 | | |
168 | 61 | char *target_name; |
169 | 61 | GString *cmd_line; |
170 | 61 | gchar *pretty_cmd_line; |
171 | 61 | bool serialize = false; |
172 | 61 | bool verbose = qtest_verbose("fuzz"); |
173 | | |
174 | | /* Initialize qgraph and modules */ |
175 | 61 | qos_graph_init(); |
176 | 61 | module_call_init(MODULE_INIT_TARGET_INFO); |
177 | 61 | target_info_qom_set_target(); |
178 | 61 | module_call_init(MODULE_INIT_FUZZ_TARGET); |
179 | 61 | module_call_init(MODULE_INIT_QOM); |
180 | 61 | module_call_init(MODULE_INIT_LIBQOS); |
181 | | |
182 | 61 | qemu_init_exec_dir(**argv); |
183 | 61 | target_name = strstr(**argv, "-target-"); |
184 | 61 | if (target_name) { /* The binary name specifies the target */ |
185 | 61 | target_name += strlen("-target-"); |
186 | 61 | } else if (*argc > 1) { /* The target is specified as an argument */ |
187 | 0 | target_name = (*argv)[1]; |
188 | 0 | if (!strstr(target_name, "--fuzz-target=")) { |
189 | 0 | usage(**argv); |
190 | 0 | } |
191 | 0 | target_name += strlen("--fuzz-target="); |
192 | 0 | } else { |
193 | 0 | usage(**argv); |
194 | 0 | } |
195 | | |
196 | | /* Should we always serialize qtest commands? */ |
197 | 61 | if (getenv("FUZZ_SERIALIZE_QTEST")) { |
198 | 0 | serialize = true; |
199 | 0 | } |
200 | | |
201 | 61 | fuzz_qtest_set_serialize(serialize); |
202 | | |
203 | | /* Identify the fuzz target */ |
204 | 61 | fuzz_target = fuzz_get_target(target_name); |
205 | 61 | if (!fuzz_target) { |
206 | 0 | usage(**argv); |
207 | 0 | } |
208 | | |
209 | 61 | fuzz_qts = qtest_setup(); |
210 | | |
211 | 61 | if (fuzz_target->pre_vm_init) { |
212 | 0 | fuzz_target->pre_vm_init(); |
213 | 0 | } |
214 | | |
215 | | /* Run QEMU's system main with the fuzz-target dependent arguments */ |
216 | 61 | cmd_line = fuzz_target->get_init_cmdline(fuzz_target); |
217 | 61 | g_string_append_printf(cmd_line, " %s -qtest /dev/null ", |
218 | 61 | verbose ? "" : "-qtest-log none"); |
219 | | |
220 | | /* Split the runcmd into an argv and argc */ |
221 | 61 | wordexp_t result; |
222 | 61 | wordexp(cmd_line->str, &result, 0); |
223 | 61 | g_string_free(cmd_line, true); |
224 | | |
225 | 61 | if (verbose) { |
226 | 0 | pretty_cmd_line = g_strjoinv(" ", result.we_wordv + 1); |
227 | 0 | printf("Starting %s with Arguments: %s\n", |
228 | 0 | result.we_wordv[0], pretty_cmd_line); |
229 | 0 | g_free(pretty_cmd_line); |
230 | 0 | } |
231 | | |
232 | 61 | qemu_init(result.we_wordc, result.we_wordv); |
233 | | |
234 | | /* re-enable the rcu atfork, which was previously disabled in qemu_init */ |
235 | 61 | rcu_enable_atfork(); |
236 | | |
237 | | /* |
238 | | * Disable QEMU's signal handlers, since we manually control the main_loop, |
239 | | * and don't check for main_loop_should_exit |
240 | | */ |
241 | 61 | signal(SIGINT, SIG_DFL); |
242 | 61 | signal(SIGHUP, SIG_DFL); |
243 | 61 | signal(SIGTERM, SIG_DFL); |
244 | | |
245 | 61 | return 0; |
246 | 61 | } |