Coverage Report

Created: 2026-11-22 08:06

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