## Q5 Coverage-Guided Fuzzing In lectures we saw that **blind** mutation-based fuzzing wastes almost all of its effort when a program hides its bug behind exact checks (magic bytes, length fields, checksums): the chance of randomly satisfying every check at once is tiny. **Coverage-guided** fuzzing fixes this by *listening to the program*. The target is **instrumented** so that every time execution reaches a new place in the code, the fuzzer is told. The fuzzer then keeps any input that reached somewhere new and mutates it further, so progress accumulates one check at a time instead of all at once. In this question you implement the two mechanisms at the heart of that idea — **a mutation operator** and **the coverage-guided loop** — and use them to crash a small program that blind fuzzing cannot realistically crack. There are **two independent bugs** to find, and they reward two different mutation strategies. Everything runs **in your web browser**; there is nothing to install and no C compiler to run. Open the provided page (see below) and you will find: * the **target** already compiled to WebAssembly and instrumented with a coverage probe at every edge (its source is not provided — this is a black-box target, so `run`, `coverage`, and `crashed` below are all the information you get); * a Python editor with a starter file `fuzz.py`; * a live dashboard showing executions, coverage, corpus size, and any crashes. ### The target A tiny binary-message parser. It reads a byte buffer and walks a chain of checks behind a 4-byte magic prefix. Two independent memory-safety bugs sit behind that same prefix: * **Bug 1 — out-of-bounds read.** One field indexes a small internal table with no range check. * **Bug 2 — integer overflow.** A separate, wider (multi-byte) numeric field feeds an arithmetic size check; the check trusts a computation that can silently wrap around, while the real access does not. (This assignment does not use AddressSanitizer; the harness reports which bug(s) fired via `crashed`, which you should treat exactly like a sanitizer abort for that bug.) ### The API available in `fuzz.py` ```text run(data) -> (coverage, crashed) Execute the target on `data` (bytes / bytearray / list of ints). coverage : list[int] of length NUM_EDGES; coverage[k] > 0 means edge k was reached on this run. crashed : int BITMASK. bit0 (value 1) = Bug 1 fired on this run; bit1 (value 2) = Bug 2 fired. Either, both, or neither bit may be set. 0 means nothing crashed on this run. NUM_EDGES : int number of instrumented edges SEED : bytes a starting input (valid length, does NOT crash) report(execs, corpus_size, covered_edges) update the dashboard save_crash(data, which) record a crashing input; `which` is "bug1" or "bug2" log(*args) print to the console ``` ### What you must do The fuzzing loop is already written for you and keeps going after a crash — finding one bug does not end the search for the other. You only complete **two small functions** it calls: 1. **`mutate(data) -> bytes`** — a mutation operator that returns a slightly changed copy of the input. A single operator does not have to be your only strategy: the two bugs above are different *shapes* of bug and reward different mutations. *Hints:* * For an **exact-byte check** (like a magic prefix), think about what a single bit-flip can and cannot accomplish, versus substituting a brand-new random byte. * For a **wide numeric field feeding arithmetic** (like an overflow), random byte substitution is a poor fit — the values that actually matter cluster at specific numbers (0, 1, the field's maximum, half its maximum, powers of two). Once you are near a promising value, small `+`/`-` adjustments let you creep onto the exact point where the arithmetic wraps around. 2. **`is_interesting(cov, covered) -> bool`** — the coverage decision. `cov` is the coverage of the current run (`cov[k] > 0` means edge `k` was reached); `covered` is the set of edges reached on all previous runs. Return `True` exactly when the run reached an edge that is **not** already in `covered` (i.e. the input discovered new code and should be kept) — regardless of whether that same run also happened to crash. Run it in the page: when `is_interesting` is right, the loop keeps each input that unlocks new coverage, the corpus grows, and the fuzzer climbs the checks until it hits each bug and saves its crash file. Your fuzzer must find **both** crashes **in the browser within 5 minutes** (a correct solution finds Bug 1 in seconds and Bug 2 shortly after). You may **not** hard-code either crashing input. ### Deliverables * `fuzz.py` — your `mutate` and `is_interesting` implementations. * `crash_bug1.bin` and `crash_bug2.bin` — the crashing inputs your fuzzer discovered for each bug (downloaded from the page). Finding only Bug 1 earns partial credit; full credit requires both. * A short answer (4–6 sentences) covering **both** bugs: for each one, which mutation operator did you use and why is it suitable for that bug's shape? Contrast an operator that would work for one bug but fail (or be unreliable) for the other, and explain in one sentence why making `is_interesting` always return `True` (keeping everything) or always `False` (keeping nothing) would break the search.