/src/racket-fuzz/driver.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: (Apache-2.0 OR MIT) */ |
2 | | |
3 | | /* libFuzzer driver for Racket CS. |
4 | | |
5 | | Boots Racket CS once, loads "safe-eval.rkt", and calls one of its |
6 | | `fuzz-*` entry points on each input. An entry point returns 0 for |
7 | | every expected outcome, and for a bug either 1 or a description, |
8 | | which becomes a failed assertion here. |
9 | | |
10 | | Compile-time settings (all strings): |
11 | | FUZZ_ENTRY entry point name, such as "fuzz-eval" |
12 | | and either |
13 | | FUZZ_EMBEDDED defined: boot files and modules come from |
14 | | "embed-data.S", and no files are needed at run time |
15 | | or |
16 | | FUZZ_MODULE absolute path of "safe-eval.rkt" |
17 | | BOOT_DIR directory holding petite.boot, scheme.boot, racket.boot |
18 | | COLLECTS_DIR the "collects" directory |
19 | | CONFIG_DIR the "etc" directory |
20 | | and optionally |
21 | | FUZZ_ADDON_DIR the addon directory, so that Racket does not look |
22 | | for the home directory |
23 | | |
24 | | With RACKET_COVERAGE_COUNTERS defined (and a Racket CS built with it |
25 | | and with PLT_CS_COVERAGE_COUNTERS set), Scheme code also reports |
26 | | coverage: the Chez Scheme counters are registered with libFuzzer |
27 | | like clang's inline 8-bit counters. Code that a fuzzed program |
28 | | compiles gets no counters, since counters placed by hashing the |
29 | | program's own code would reward new programs, not new behavior of |
30 | | Racket. */ |
31 | | |
32 | | #ifndef _GNU_SOURCE |
33 | | # define _GNU_SOURCE /* for REG_RIP and dladdr */ |
34 | | #endif |
35 | | #include <assert.h> |
36 | | #include <signal.h> |
37 | | #include <stdint.h> |
38 | | #include <stdio.h> |
39 | | #include <stdlib.h> |
40 | | #include <string.h> |
41 | | |
42 | | #include "chezscheme.h" |
43 | | #include "racketcs.h" |
44 | | |
45 | | #ifndef __has_feature |
46 | | # define __has_feature(x) 0 |
47 | | #endif |
48 | | #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) |
49 | | # include <sanitizer/lsan_interface.h> |
50 | | # define FUZZ_LSAN_DISABLE() __lsan_disable() |
51 | | # define FUZZ_LSAN_ENABLE() __lsan_enable() |
52 | | #else |
53 | 2 | # define FUZZ_LSAN_DISABLE() (void)0 |
54 | 2 | # define FUZZ_LSAN_ENABLE() (void)0 |
55 | | #endif |
56 | | |
57 | | #ifndef FUZZ_ENTRY |
58 | | # define FUZZ_ENTRY "fuzz-eval" |
59 | | #endif |
60 | | |
61 | | static ptr entry_proc; |
62 | | |
63 | | #ifdef FUZZ_EMBEDDED |
64 | | extern char fuzz_petite_boot[], fuzz_petite_boot_end[]; |
65 | | extern char fuzz_scheme_boot[], fuzz_scheme_boot_end[]; |
66 | | extern char fuzz_racket_boot[], fuzz_racket_boot_end[]; |
67 | | extern char fuzz_modules[], fuzz_modules_end[]; |
68 | | #endif |
69 | | |
70 | | /* Chez Scheme turns every memory fault into an "invalid memory |
71 | | reference" exception, and every exception that the harness counts |
72 | | as a bug ends in `report_bug` below, with no C stack trace of its |
73 | | own. For a fault in C code, |
74 | | the sanitizer's report and stack trace say much more, so send those |
75 | | faults to the handler that was in place before booting. A fault in |
76 | | Scheme code, which is outside any shared object or the executable, |
77 | | still goes to Chez Scheme, since the sanitizer cannot walk Scheme |
78 | | frames; "safe-eval.rkt" then prints the Racket context and describes |
79 | | the bug by where in Racket it happened. Racket does |
80 | | not rely on these signals in normal operation. */ |
81 | | static struct sigaction sanitizer_segv, sanitizer_bus, chez_segv, chez_bus; |
82 | | |
83 | | #if defined(__linux__) && (defined(__x86_64__) || defined(__aarch64__)) |
84 | | # include <dlfcn.h> |
85 | | # include <ucontext.h> |
86 | | |
87 | | static int fault_in_c_code(void *data) |
88 | 0 | { |
89 | 0 | ucontext_t *uc = (ucontext_t *)data; |
90 | 0 | Dl_info info; |
91 | 0 | # if defined(__x86_64__) |
92 | 0 | void *pc = (void *)uc->uc_mcontext.gregs[REG_RIP]; |
93 | | # else |
94 | | void *pc = (void *)uc->uc_mcontext.pc; |
95 | | # endif |
96 | 0 | return dladdr(pc, &info) != 0; |
97 | 0 | } |
98 | | |
99 | | static void call_handler(struct sigaction *sa, int sig, siginfo_t *si, void *data) |
100 | 0 | { |
101 | 0 | if (sa->sa_flags & SA_SIGINFO) |
102 | 0 | sa->sa_sigaction(sig, si, data); |
103 | 0 | else if ((sa->sa_handler != SIG_DFL) && (sa->sa_handler != SIG_IGN)) |
104 | 0 | sa->sa_handler(sig); |
105 | 0 | else { |
106 | | /* returning re-runs the faulting instruction, now with the |
107 | | default action */ |
108 | 0 | signal(sig, SIG_DFL); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | static void handle_fault(int sig, siginfo_t *si, void *data) |
113 | 0 | { |
114 | 0 | int is_segv = (sig == SIGSEGV); |
115 | 0 | if (fault_in_c_code(data)) |
116 | 0 | call_handler(is_segv ? &sanitizer_segv : &sanitizer_bus, sig, si, data); |
117 | 0 | else |
118 | 0 | call_handler(is_segv ? &chez_segv : &chez_bus, sig, si, data); |
119 | 0 | } |
120 | | |
121 | | static void split_fault_handlers(void) |
122 | 2 | { |
123 | 2 | struct sigaction act; |
124 | | |
125 | 2 | sigaction(SIGSEGV, NULL, &chez_segv); |
126 | 2 | sigaction(SIGBUS, NULL, &chez_bus); |
127 | | |
128 | 2 | act = chez_segv; |
129 | 2 | act.sa_flags |= SA_SIGINFO | (sanitizer_segv.sa_flags & SA_ONSTACK); |
130 | 2 | act.sa_sigaction = handle_fault; |
131 | 2 | sigaction(SIGSEGV, &act, NULL); |
132 | 2 | sigaction(SIGBUS, &act, NULL); |
133 | 2 | } |
134 | | #else |
135 | | static void split_fault_handlers(void) { } |
136 | | #endif |
137 | | |
138 | | |
139 | | #ifdef RACKET_COVERAGE_COUNTERS |
140 | | void __sanitizer_cov_8bit_counters_init(uint8_t *start, uint8_t *stop); |
141 | | void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, const uintptr_t *pcs_end); |
142 | | |
143 | | /* libFuzzer wants a PC table entry, a (PC, flags) pair, for each |
144 | | counter; the PCs only need to be distinct. */ |
145 | | static void register_scheme_counters(void) |
146 | 2 | { |
147 | 2 | iptr len; |
148 | 2 | uint8_t *counters = Scoverage_counters(&len); |
149 | 2 | uintptr_t *pcs; |
150 | 2 | iptr i; |
151 | | |
152 | 2 | if (!counters) return; |
153 | | |
154 | 2 | pcs = malloc(2 * len * sizeof(uintptr_t)); |
155 | 2.09M | for (i = 0; i < len; i++) { |
156 | 2.09M | pcs[2 * i] = (uintptr_t)(counters + i); |
157 | 2.09M | pcs[2 * i + 1] = 0; |
158 | 2.09M | } |
159 | | |
160 | 2 | __sanitizer_cov_8bit_counters_init(counters, counters + len); |
161 | 2 | __sanitizer_cov_pcs_init(pcs, pcs + 2 * len); |
162 | 2 | } |
163 | | #endif |
164 | | |
165 | | static int initialized = 0; |
166 | | |
167 | | /* An error while booting or loading the harness ends the process with |
168 | | `exit`, often with status 0, which would look like a passing run. */ |
169 | | static void check_initialized(void) |
170 | 2 | { |
171 | | /* not a property: Racket failed to boot or to load the harness */ |
172 | 2 | if (!initialized) { |
173 | 0 | fprintf(stderr, "racket-fuzz: initialization failed\n"); |
174 | 0 | abort(); |
175 | 0 | } |
176 | 2 | } |
177 | | |
178 | | int LLVMFuzzerInitialize(int *argc, char ***argv) |
179 | 2 | { |
180 | 2 | racket_boot_arguments_t ba; |
181 | | |
182 | 2 | atexit(check_initialized); |
183 | | |
184 | 2 | memset(&ba, 0, sizeof(ba)); |
185 | 2 | #ifdef FUZZ_EMBEDDED |
186 | 2 | ba.boot1_data = fuzz_petite_boot; |
187 | 2 | ba.boot1_len = fuzz_petite_boot_end - fuzz_petite_boot; |
188 | 2 | ba.boot2_data = fuzz_scheme_boot; |
189 | 2 | ba.boot2_len = fuzz_scheme_boot_end - fuzz_scheme_boot; |
190 | 2 | ba.boot3_data = fuzz_racket_boot; |
191 | 2 | ba.boot3_len = fuzz_racket_boot_end - fuzz_racket_boot; |
192 | 2 | ba.collects_dir = ""; |
193 | 2 | ba.config_dir = NULL; |
194 | | #else |
195 | | ba.boot1_path = BOOT_DIR "/petite.boot"; |
196 | | ba.boot2_path = BOOT_DIR "/scheme.boot"; |
197 | | ba.boot3_path = BOOT_DIR "/racket.boot"; |
198 | | /* A list of paths, each ending in NUL, with an extra NUL at the end; |
199 | | `parse_coldirs` in "boot.c" reads past a single NUL. */ |
200 | | ba.collects_dir = COLLECTS_DIR "\0"; |
201 | | ba.config_dir = CONFIG_DIR; |
202 | | #endif |
203 | 2 | ba.exec_file = "racket-fuzz"; |
204 | 2 | ba.exit_after = 0; |
205 | | #ifdef FUZZ_ADDON_DIR |
206 | | /* Naming the addon directory keeps Racket from finding it through |
207 | | the home directory, which looks in "/etc/passwd" when HOME is not |
208 | | set; "-n" is what a NULL `argv` would mean */ |
209 | | { |
210 | | static char *args[] = { "-A", FUZZ_ADDON_DIR, "-n", NULL }; |
211 | | ba.argc = 3; |
212 | | ba.argv = args; |
213 | | } |
214 | | #endif |
215 | | |
216 | | /* Racket keeps what it allocates while booting until the process |
217 | | exits, often referenced only from the Scheme heap, which |
218 | | LeakSanitizer does not scan; so don't report those as leaks. |
219 | | Leaks from running an input are still reported. */ |
220 | 2 | FUZZ_LSAN_DISABLE(); |
221 | | |
222 | 2 | sigaction(SIGSEGV, NULL, &sanitizer_segv); |
223 | 2 | sigaction(SIGBUS, NULL, &sanitizer_bus); |
224 | | |
225 | 2 | racket_boot(&ba); |
226 | | |
227 | 2 | split_fault_handlers(); |
228 | | |
229 | 2 | #ifdef RACKET_COVERAGE_COUNTERS |
230 | 2 | register_scheme_counters(); |
231 | 2 | #endif |
232 | | |
233 | 2 | #ifdef FUZZ_EMBEDDED |
234 | 2 | racket_embedded_load_bytes(fuzz_modules, fuzz_modules_end - fuzz_modules, 0); |
235 | 2 | #endif |
236 | | |
237 | 2 | { |
238 | 2 | #ifdef FUZZ_EMBEDDED |
239 | | /* "embed-modules.rkt" declares the module as 'fuzz:safe-eval */ |
240 | 2 | ptr mod_path = Scons(Sstring_to_symbol("quote"), |
241 | 2 | Scons(Sstring_to_symbol("fuzz:safe-eval"), Snil)); |
242 | | #else |
243 | | ptr mod_path = Scons(Sstring_to_symbol("file"), |
244 | | Scons(Sstring(FUZZ_MODULE), Snil)); |
245 | | #endif |
246 | 2 | ptr vals = racket_dynamic_require(mod_path, Sstring_to_symbol(FUZZ_ENTRY)); |
247 | 2 | entry_proc = Scar(vals); |
248 | 2 | Slock_object(entry_proc); |
249 | 2 | } |
250 | | |
251 | 2 | FUZZ_LSAN_ENABLE(); |
252 | | |
253 | 2 | initialized = 1; |
254 | | |
255 | 2 | return 0; |
256 | 2 | } |
257 | | |
258 | | /* Reports a bug found by "safe-eval.rkt" as a failed assertion, which |
259 | | prints the description and aborts. A fuzzing service groups crashes |
260 | | by an assertion's message, so bugs with different descriptions are |
261 | | reported separately, although they all end here. */ |
262 | | static void report_bug(const char *what) |
263 | 0 | { |
264 | 0 | __assert_fail(what, "safe-eval.rkt", 0, FUZZ_ENTRY); |
265 | 0 | } |
266 | | |
267 | | static void check_result(ptr vals) |
268 | 10.2k | { |
269 | 10.2k | ptr r; |
270 | | |
271 | 10.2k | if (!Spairp(vals)) |
272 | 0 | report_bug("no result"); |
273 | 10.2k | r = Scar(vals); |
274 | 10.2k | if (Sstringp(r)) { |
275 | 0 | char buf[256]; |
276 | 0 | iptr i, len = Sstring_length(r); |
277 | 0 | if (len > (iptr)sizeof(buf) - 1) |
278 | 0 | len = sizeof(buf) - 1; |
279 | 0 | for (i = 0; i < len; i++) { |
280 | 0 | string_char c = Sstring_ref(r, i); |
281 | 0 | buf[i] = ((c >= 32) && (c < 127)) ? (char)c : '?'; |
282 | 0 | } |
283 | 0 | buf[len] = 0; |
284 | 0 | report_bug(buf); |
285 | 10.2k | } else if (!Sfixnump(r) || (Sfixnum_value(r) != 0)) { |
286 | 0 | static const char *properties[] = { |
287 | 0 | NULL, |
288 | 0 | "print round trip failed", |
289 | 0 | "exact number round trip failed", |
290 | 0 | "inexact number round trip failed", |
291 | 0 | "regexp-match and regexp-match-positions disagree", |
292 | 0 | "fasl round trip failed", |
293 | 0 | "fasl->s-exp raised a value that is not exn:fail", |
294 | 0 | "heap check failed", |
295 | 0 | "compiled and interpreted evaluation disagree", |
296 | 0 | "compiled evaluation under a compiler variant disagrees" |
297 | 0 | }; |
298 | 0 | iptr n = Sfixnump(r) ? Sfixnum_value(r) : 0; |
299 | 0 | if ((n > 0) && (n < (iptr)(sizeof(properties) / sizeof(properties[0])))) |
300 | 0 | report_bug(properties[n]); |
301 | 0 | else |
302 | 0 | report_bug("wrong result"); |
303 | 0 | } |
304 | 10.2k | } |
305 | | |
306 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
307 | 10.2k | { |
308 | 10.2k | ptr bstr = Smake_bytevector(size, 0); |
309 | | |
310 | 10.2k | memcpy(Sbytevector_data(bstr), data, size); |
311 | 10.2k | check_result(racket_apply(entry_proc, Scons(bstr, Snil))); |
312 | | |
313 | 10.2k | return 0; |
314 | 10.2k | } |