|
| 1 | +#include <limits.h> |
| 2 | +#include <signal.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +// AFL++ fuzzer harness for Zig fuzz targets. |
| 10 | +// |
| 11 | +// This file is the C "glue" that connects AFL++'s runtime to Zig-defined |
| 12 | +// fuzz test functions. We can't use AFL++'s compiler wrappers (afl-clang, |
| 13 | +// afl-gcc) because the code under test is compiled with Zig, so we manually |
| 14 | +// expand the AFL macros (__AFL_INIT, __AFL_LOOP, __AFL_FUZZ_INIT, etc.) and |
| 15 | +// wire up the sanitizer coverage symbols ourselves. |
| 16 | + |
| 17 | +// To ensure checks are not optimized out it is recommended to disable |
| 18 | +// code optimization for the fuzzer harness main() |
| 19 | +#pragma clang optimize off |
| 20 | +#pragma GCC optimize("O0") |
| 21 | + |
| 22 | +// Zig-exported entry points. zig_fuzz_init() performs one-time setup and |
| 23 | +// zig_fuzz_test() runs one fuzz iteration on the given input buffer. |
| 24 | +// The Zig object should export these. |
| 25 | +void zig_fuzz_init(); |
| 26 | +void zig_fuzz_test(unsigned char*, size_t); |
| 27 | + |
| 28 | +// Linker-provided symbols marking the boundaries of the __sancov_guards |
| 29 | +// section. These must be declared extern so the linker provides the actual |
| 30 | +// section boundaries from the instrumented code, rather than creating new |
| 31 | +// variables that shadow them. On macOS (Mach-O), the linker uses a different |
| 32 | +// naming convention for section boundaries than Linux (ELF), so we use asm |
| 33 | +// labels to reference them. |
| 34 | +#ifdef __APPLE__ |
| 35 | +extern uint32_t __start___sancov_guards __asm( |
| 36 | + "section$start$__DATA$__sancov_guards"); |
| 37 | +extern uint32_t __stop___sancov_guards __asm( |
| 38 | + "section$end$__DATA$__sancov_guards"); |
| 39 | +#else |
| 40 | +extern uint32_t __start___sancov_guards; |
| 41 | +extern uint32_t __stop___sancov_guards; |
| 42 | +#endif |
| 43 | + |
| 44 | +// Provided by afl-compiler-rt; initializes the guard array used by |
| 45 | +// SanitizerCoverage's trace-pc-guard instrumentation mode. |
| 46 | +void __sanitizer_cov_trace_pc_guard_init(uint32_t*, uint32_t*); |
| 47 | + |
| 48 | +// Stubs for sanitizer coverage callbacks that the Zig-compiled code references |
| 49 | +// but AFL's runtime (afl-compiler-rt) does not provide. Without these, linking |
| 50 | +// would fail with undefined symbol errors. |
| 51 | +__attribute__((visibility("default"))) __attribute__(( |
| 52 | + tls_model("initial-exec"))) _Thread_local uintptr_t __sancov_lowest_stack; |
| 53 | +void __sanitizer_cov_trace_pc_indir() {} |
| 54 | +void __sanitizer_cov_8bit_counters_init() {} |
| 55 | +void __sanitizer_cov_pcs_init() {} |
| 56 | + |
| 57 | +// Manual expansion of __AFL_FUZZ_INIT(). |
| 58 | +// |
| 59 | +// Enables shared-memory fuzzing: AFL++ writes test cases directly into |
| 60 | +// shared memory (__afl_fuzz_ptr) instead of passing them via stdin, which |
| 61 | +// is much faster. When not running under AFL++ (e.g. standalone execution), |
| 62 | +// __afl_fuzz_ptr will be NULL and we fall back to reading from stdin into |
| 63 | +// __afl_fuzz_alt (a 1 MB static buffer). |
| 64 | +int __afl_sharedmem_fuzzing = 1; |
| 65 | +extern __attribute__((visibility("default"))) unsigned int* __afl_fuzz_len; |
| 66 | +extern __attribute__((visibility("default"))) unsigned char* __afl_fuzz_ptr; |
| 67 | +unsigned char __afl_fuzz_alt[1048576]; |
| 68 | +unsigned char* __afl_fuzz_alt_ptr = __afl_fuzz_alt; |
| 69 | + |
| 70 | +int main(int argc, char** argv) { |
| 71 | + // Tell AFL's coverage runtime about our guard section so it can track |
| 72 | + // which edges in the instrumented Zig code have been hit. |
| 73 | + __sanitizer_cov_trace_pc_guard_init(&__start___sancov_guards, |
| 74 | + &__stop___sancov_guards); |
| 75 | + |
| 76 | + // Manual expansion of __AFL_INIT() — deferred fork server mode. |
| 77 | + // |
| 78 | + // The magic string "##SIG_AFL_DEFER_FORKSRV##" is embedded in the binary |
| 79 | + // so AFL++'s tooling can detect that this harness uses deferred fork |
| 80 | + // server initialization. The `volatile` + `used` attributes prevent the |
| 81 | + // compiler/linker from stripping it. We then call __afl_manual_init() to |
| 82 | + // start the fork server at this point (after our setup) rather than at |
| 83 | + // the very beginning of main(). |
| 84 | + static volatile const char* _A __attribute__((used, unused)); |
| 85 | + _A = (const char*)"##SIG_AFL_DEFER_FORKSRV##"; |
| 86 | +#ifdef __APPLE__ |
| 87 | + __attribute__((visibility("default"))) void _I(void) __asm__( |
| 88 | + "___afl_manual_init"); |
| 89 | +#else |
| 90 | + __attribute__((visibility("default"))) void _I(void) __asm__( |
| 91 | + "__afl_manual_init"); |
| 92 | +#endif |
| 93 | + _I(); |
| 94 | + |
| 95 | + zig_fuzz_init(); |
| 96 | + |
| 97 | + // Manual expansion of __AFL_FUZZ_TESTCASE_BUF. |
| 98 | + // Use shared memory buffer if available, otherwise fall back to the |
| 99 | + // static buffer (for standalone/non-AFL execution). |
| 100 | + unsigned char* buf = __afl_fuzz_ptr ? __afl_fuzz_ptr : __afl_fuzz_alt_ptr; |
| 101 | + |
| 102 | + // Manual expansion of __AFL_LOOP(UINT_MAX) — persistent mode loop. |
| 103 | + // |
| 104 | + // Persistent mode keeps the process alive across many test cases instead |
| 105 | + // of fork()'ing for each one, dramatically improving throughput. The magic |
| 106 | + // string "##SIG_AFL_PERSISTENT##" signals to AFL++ that this binary |
| 107 | + // supports persistent mode. __afl_persistent_loop() returns non-zero |
| 108 | + // while there are more inputs to process. |
| 109 | + // |
| 110 | + // When connected to AFL++, we loop UINT_MAX times (essentially forever, |
| 111 | + // AFL will restart us periodically). When running standalone, we loop |
| 112 | + // once so the harness can be used for manual testing/reproduction. |
| 113 | + while (({ |
| 114 | + static volatile const char* _B __attribute__((used, unused)); |
| 115 | + _B = (const char*)"##SIG_AFL_PERSISTENT##"; |
| 116 | + extern __attribute__((visibility("default"))) int __afl_connected; |
| 117 | +#ifdef __APPLE__ |
| 118 | + __attribute__((visibility("default"))) int _L(unsigned int) __asm__( |
| 119 | + "___afl_persistent_loop"); |
| 120 | +#else |
| 121 | + __attribute__((visibility("default"))) int _L(unsigned int) __asm__( |
| 122 | + "__afl_persistent_loop"); |
| 123 | +#endif |
| 124 | + _L(__afl_connected ? UINT_MAX : 1); |
| 125 | + })) { |
| 126 | + // Manual expansion of __AFL_FUZZ_TESTCASE_LEN. |
| 127 | + // In shared-memory mode, the length is provided directly by AFL++. |
| 128 | + // In standalone mode, we read from stdin into the fallback buffer. |
| 129 | + int len = |
| 130 | + __afl_fuzz_ptr ? *__afl_fuzz_len |
| 131 | + : (*__afl_fuzz_len = read(0, __afl_fuzz_alt_ptr, 1048576)) == 0xffffffff |
| 132 | + ? 0 |
| 133 | + : *__afl_fuzz_len; |
| 134 | + |
| 135 | + if (len >= 0) { |
| 136 | + zig_fuzz_test(buf, len); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
0 commit comments