wrfuzz Internals (Part 1): Guard-Page Heap & Fault Recovery Without ASan
Source repository by Mr.Cupcake (mscupcakegkill): https://gitlab.com/mscupcakegkill/wrfuzz
This is Part 1 of the wrfuzz engineering notes, covering how the in-process runner recovers from faults without fork(), avoids per-iteration timer syscalls under PRoot, and catches 1-byte heap overflows in ~256 KB of memory without AddressSanitizer.
Execution harness (run.cpp)
run_ex(buf, deadline) performs one execution.
- Deadline check. If the deadline has already passed, it returns at once.
RunResultdefaults toTimeoutstatus. - Reset. Clear the hit counters of the previous run through the
touchedlist, so the cost is proportional to coverage, not map size. Reset the I2S ring and the recent-PC ring. Reset the guard-heap bump pointer. - Call the target.
-
sigsetjmp(jmp_env, 1), then setin_target = 1. - Call
fuzz_target. - Run
gpool_verify(), then clearin_target.
-
- Fault path. The handler is installed with
SA_SIGINFO | SA_NODEFER | SA_RESTARTforSEGV,ABRT,FPE,ILL,BUSandTRAP. It records the signal andsi_addr, then jumps back with value 1, which becomesCrash. - Timeout path. The same handler serves
SIGALRM. Whilein_targetis set, each tick incrementshang_ticks. When it reachestimeout_ticks, the handler jumps back with value 2, which becomesTimeout. Because the timer runs freely, the real limit falls between(ticks-1)*25andticks*25ms. - Late check. If the outer deadline has passed by the time the target returns, the result is discarded and keeps the default
Timeoutstatus. With the main loop's 1 s deadline, the effective hang limit is therefore aboutmin(TIMEOUT_MS, 1000 ms). - Success. Compute the coverage signature and return
Okwith the target's return code.
run(buf, deadline) is the thin adapter used by colorize(). It maps any non-Ok result to nullopt.
On Windows, the call is wrapped in SEH instead. There is no timeout, guard heap or libc comparison hook on that path.
Guard heap (GUARD_HEAP=1)
Layout. One mmap reserves 2*64+1 pages with no access. The odd-numbered pages are then made read-write, giving 64 slots, each followed by a guard page.
Allocation. While in_target is set, any allocation of up to one page is bump-allocated into the next free slot.
- The block is placed at the end of its page, aligned to 16 bytes. An overflow past the aligned end hits the guard page.
- The slack between the requested size and the aligned end is filled with the canary
0xA5.
Free.
-
freechecks the canary and fills the block with0xFE. - Double frees, frees of interior pointers and frees of stale slots call
__builtin_trap. -
reallocon a pool pointer allocates a new block, copies and frees the old one. -
callocchecks for multiplication overflow.
End-of-run check. gpool_verify checks every canary and every freed block. This catches small overflows and use-after-free writes. Reads after free, and underflows within the page, are not detected.
Limits. Slots are not reused within a run. Once 64 are used, further allocations go to the system allocator.
Crash triage
A run counts as a bug if it crashed, timed out, or returned 2.
Signature.
- The code is the signal number. Timeouts use
SIGALRM, andrc == 2uses the code2. - The address is normally replaced by an FNV-1a hash of the recent-PC ring. So in practice, two bugs count as distinct when they have the same signal but different last-eight-block path suffixes.
- Up to 64 signatures are kept.
Saving. The first 8 unique bugs are saved as crash_<id>_<code>.bin. With STOP_ON_CRASH=1, the fuzzer stops at the first new one.
Full source code (fuzz.cpp, run.cpp, Code.cpp) and architecture docs by Mr.Cupcake (mscupcakegkill): https://gitlab.com/mscupcakegkill/wrfuzz