Coverage-guided fuzzing
A property passed a million random tries. Is there a bug behind a check those tries never got past?
A pass can’t say. How sure is a pass? works out what a pass claims: how rare a failure could still be among the inputs drawn. It says nothing of code the inputs never reached. And some code is behind a check that random inputs almost never pass: a file’s magic number, a message’s checksum, a command’s exact name. A bug there is safe from random tries however many you make.
proptastic fuzz searches for inputs that get further into the code. This page follows one header
reader with a bug only one shape of input reaches. It runs the same property at random and guided,
ten times each from ten seeds, then looks at what a guided search keeps, how it changes what it
keeps, what it claims when it’s done, and when it’s the wrong tool. Every number below is from those
searches: the reader, its properties and the searches that measure them are in proptastic’s
agent/src/test/java/codec
and fuzzing,
and the docs build runs them again on every change and fails if a number here moves. A search from
the same seed does the same thing every time, on every machine, so they don’t.
The reader
Section titled “The reader”A file in this format starts with the four bytes CA FE BA BE, then how many blocks it holds and
their total size. The reader checks the magic bytes one at a time, and works out each block’s size:
/** Reads a file's header: CA FE BA BE, a count of blocks, and their size. */public final class HeaderReader { /** The bytes every file starts with. */ private static final int[] MAGIC = {0xCA, 0xFE, 0xBA, 0xBE};
private HeaderReader() {}
/** The header {@code bytes} start with, or none when they don't. */ public static Optional<Header> read(byte[] bytes) { if (bytes.length < 6) { return Optional.empty(); } for (int at = 0; at < MAGIC.length; at++) { if (Byte.toUnsignedInt(bytes[at]) != MAGIC[at]) { return Optional.empty(); } } int blocks = Byte.toUnsignedInt(bytes[4]); int size = Byte.toUnsignedInt(bytes[5]); return Optional.of(new Header(blocks, size / blocks)); }}A file that says it has no blocks divides by zero. Only an input that starts CA FE BA BE 00
reaches that line. The property says any bytes read as a header or as none, and never throw:
@Testpublic void anyBytesReadAsAHeaderOrNone() { forAll(bytes().ofSize(atLeast(6), atMost(12))).check(HeaderReader::read);}A million random tries
Section titled “A million random tries”Run at random, ten times, from the seeds 0x1 to 0xa, a million tries each:
The grey lines are the random searches. Each line steps up when a try first gets one byte further
into the header. Every one of them found an input starting CA within the first 1,276 tries, and
one starting CA FE by try 373,385. None found CA FE BA in a million tries, and so none found the
bug: ten million random tries, and the property passed every one.
That’s what random tries should do. Each byte is one of 256, so an input that starts with four given bytes comes up about once in 4 billion tries, and it must have a count of 0 after them too. The pass is true as far as it goes: under 3 in a million of the inputs drawn this way fail, by the rule of three on a million tries, and here far fewer than that do. It’s the bound How sure works out, and like any bound it’s silent on code the tries never reached.
A search that keeps what gets further
Section titled “A search that keeps what gets further”The accent lines are guided searches of the same property, from the same ten seeds. Each found the bug, between try 1,167 and try 7,166, and the shrunk counterexample is the same every time:
shrunk counterexample: bytes = HexFormat.of().parseHex("cafebabe0000")On a laptop each search took under half a second. The tries are what the build checks, as they’re the same on every machine.
Each guided line climbs the header a step at a time. A guided search watches which parts of the
reader each input runs, and keeps an input that runs something no input before it did. It makes
its next inputs by changing those it kept, so once it has an input that starts CA, it’s changing
that input, and needs only the next byte, not all four at once: four bytes found one after the
other, rather than one chance in 4 billion.
From the command line, proptastic fuzz runs the same search, from a seed of its own, and says
what it found:
proptastic fuzz --for=20s --only=anyBytesReadAsAHeaderOrNone Found ─────────────────────────────────────────────────────────────────
✗ HeaderTest#anyBytesReadAsAHeaderOrNone counterexample c657fb13a anyBytesReadAsAHeaderOrNone failed after 3,112 tries of a search (seed 0xcd66f10181a3a6e4) shrunk counterexample: bytes = HexFormat.of().parseHex("cafebabe0000") pin it: .example(HexFormat.of().parseHex("cafebabe0000")) rerun: proptastic run --only='codec.HeaderTest#anyBytesReadAsAHeaderOrNone()' --seed=0xcd66f10181a3a6e4
Next: proptastic example c657fb13a · or propose: proptastic example c657fb13a --propose --reason="<why>"
Reports build/proptastic/report/report.json · build/proptastic/report/index.html
searched 1.0s · 3,112 tries · found c657fb13aThat search took 3,112 tries, from a seed none of the ten above had.
What it keeps
Section titled “What it keeps”To see which parts of the code an input runs, proptastic’s agent
adds a probe at every branch of the code under test as it loads: at each side of each if, each
case of a switch, each exception handler. The search counts which branches each input passes,
and how often.
Here are the inputs the first search, from seed 0x1, kept, in the order it kept them:
| Kept | Its bytes | How far it got |
|---|---|---|
| 1 | C7 A0 89 C6 01 11 31 | not CA: the loop stops at the first byte |
| 2 | CA A0 89 C6 C6 01 01 00 | CA, then a byte that isn't the next |
| 3 | CA FE 89 FF C6 01 01 | CA FE, then a byte that isn't the next |
| 4 | CA FE BA A0 89 00 | CA FE BA, then a byte that isn't the next |
| 5 | CA FE BA BE 89 00 | CA FE BA BE, then the division |
| found | CA FE BA BE 00 00 | CA FE BA BE, a count of 0: / by zero, after 4,549 tries |
The first input was kept because anything is new to a search that has seen nothing. The second
starts CA, so it takes the other side of the if for the first time, and goes round the loop
again. The third, CA FE, reaches no branch the second didn’t: the same loop, the same if, only
one more time round. That’s why a search counts how often, not only whether.
It keeps an input that passes a branch more times than any before it, counted in steps: 1, 2, 3, 4
to 7, 8 to 15, 16 to 31, 32 to 127, and 128 or more. Each byte of the magic that matches is one
more time round the loop, a new count, so each is kept. The fifth input reaches the division, a
branch no input before it had, and the search went on changing what it had kept until one had a
count of 0.
Count branches alone and random tries look as good. Every random search above reached 7 of the reader’s branches, as many as some of the guided searches that found the bug: a loop’s branches are the same whichever byte it stops at. What random tries never did was go round it a third time.
How a kept input is changed
Section titled “How a kept input is changed”A search doesn’t change an input’s bytes. It changes the choices the domain made to draw them, as shrinking does, and draws the input again from the changed choices. Each new input is one kept input with one to four changes:
- one choice moved: one up or down, up to 16 either way, doubled, halved, to its largest, to 0, or anywhere in its range;
- a span of up to 8 choices deleted, or repeated;
- spliced with another kept input: this one’s choices up to a point, then the other’s;
- a span copied over from another kept input, at the same place.
It picks the input to change at random, the newer kept inputs likelier, since what it kept last is where it last got further. One input in 16 is drawn afresh at random instead, so the search keeps the reach random tries have.
A choice that’s moved past its range is drawn at the end of it, and choices that run out are read as 0, so every input a search tries is one the domain could have drawn: here, 6 to 12 bytes, never 13. The bug it finds is shrunk, saved and pinned as any failure is. Searching the choices a generator makes, rather than the bytes of its output, is from Padhye et al., 2019.
Values the code compares with
Section titled “Values the code compares with”Moving a byte one or 16 at a time, or doubling it, would take a long walk to land on CA. Yet the
first search had an input starting CA by its 13th try.
The agent rewrites each comparison of two ints or two longs, and each String.equals, so that
while a search runs it notes both sides. Byte.toUnsignedInt(bytes[0]) != MAGIC[0] compares the
input’s first byte with 202, which is CA. The search counts the values the code compares with,
and in one change in three puts one of the 16 most compared into a choice that can hold it, or a
compared string where a string’s characters start. A constant comes up input after input, as the
same check runs again and again, while the input’s own side keeps changing, so the constants rise
to the top. That’s how the search guesses CA, then FE, BA and BE: it reads them off the
comparisons that stopped it.
It puts a compared value into one choice: as the number it is, or a negative one as the bits it has
in a byte, short, int or long, where a choice is that wide. A choice of integers() or
longs() takes it as the choice that draws it, as theirs go 0, 1, -1, 2 and on: x == 1000 puts
in choice 1999. Two other ways to write the same check show what that reaches and what it doesn’t.
Compared as Java compares a byte, signed, (byte) 0xCA is -54:
/** The magic compared byte by byte, as Java compares a byte: signed. */public static int blockSizeSigned(byte[] bytes) { if (bytes.length < 6) { return -1; } if (bytes[0] != (byte) 0xCA || bytes[1] != (byte) 0xFE || bytes[2] != (byte) 0xBA || bytes[3] != (byte) 0xBE) { return -1; } return Byte.toUnsignedInt(bytes[5]) / Byte.toUnsignedInt(bytes[4]);}A byte’s choice runs from 0 to 255, so -54 goes in as CA, the byte Java reads as -54. Five guided
searches of 50,000 tries each, from the seeds 0x1 to 0x5, found the bug in every one, in 2,346
to 8,080 tries.
Read as one int, the magic is a single comparison with 0xCAFEBABE:
/** The magic read as one int, and compared at once. */public static int blockSizeWhole(byte[] bytes) { if (bytes.length < 6 || ByteBuffer.wrap(bytes).getInt() != 0xCAFEBABE) { return -1; } return Byte.toUnsignedInt(bytes[5]) / Byte.toUnsignedInt(bytes[4]);}That’s four bytes in four choices, which no one choice holds, and one branch: an input with three of the four bytes right runs exactly what an input with none does, so there’s nothing to keep and nowhere to climb. Five searches of 50,000 tries each found it in none.
So a magic number compared whole keeps a search out as surely as random tries. Comparing it a byte at a time, signed or unsigned, lets it in.
What a search claims
Section titled “What a search claims”A search stops at the first counterexample, or when its time is up. If it found none, it says what it searched, and never that the property passed:
proptastic fuzz --for=4ssearched 1.0s · 83,645 tries · 11 of 43 branches in codec · nothing foundA pass is what a run of random tries claims, with how sure it is, a bound on how rare a failure could be among inputs drawn the same way. A guided search draws no two inputs the same way: each is a change of what it kept, so no such bound follows from it. What it can say is what it did: how many tries, and how many branches it reached of those loaded. Loaded means the classes the search’s inputs made the JVM load in the packages it counts, not every class in them, so the share isn’t coverage of your code, and nothing a search keeps is called coverage.
Nor does finding nothing say much about the next search. The ten guided searches above needed anywhere from 1,167 to 7,166 tries for the same bug, from the same code, by the same rules: one search took six times what another did. Give a search a different seed, or a little less time, and a bug one finds the next may miss.
When to reach for it
Section titled “When to reach for it”Reach for a better domain first. The bug above needs an input that starts with the magic bytes, so draw inputs that do. A domain can build headers whole:
/** Headers built whole: the magic bytes, then any count and size. */static Domain<byte[]> headers() { return bytes().ofSize(2).map(HeaderTest::afterTheMagic);}
private static byte[] afterTheMagic(byte[] rest) { var header = HexFormat.of().parseHex("cafebabe0000"); header[4] = rest[0]; header[5] = rest[1]; return header;}
@Testpublic void anyHeaderReadsOrNone() { forAll(headers()).check(HeaderReader::read);}Ten plain runs of it, 100 random tries each, from the same ten seeds, found the bug in all ten, each within 15 tries: a count of 0 is a byte at its smallest, which a domain draws often on purpose. No agent, no JDK 24, no search.
The research agrees. Etna, which ran property-testing tools over the same set of bugs many times each, found that a generator written for the job, one that builds only valid inputs, solved every task, while coverage-guided fuzzers “exhibit more variance” from run to run: on one set of tasks, about 30 were solved at least once in ten runs, but fewer than 10 every time (Keles et al., 2026). The ten guided searches above vary the same way, if less: each found the bug, but one took six times as long as another.
So reach for proptastic fuzz when valid inputs are hard to build: when what reaches the code is
bytes or text from outside, a file, a message, a request, and the checks that guard it are what
you’re testing. And reach for it when there’s time to spare: a property that a run tries a hundred
times can be searched for half an hour every night, starting where last night’s search stopped.
It’s the tool for after the domain is as good as you can make it, not before.
Where it can’t
Section titled “Where it can’t”A guided search needs proptastic’s agent to count branches, and the agent rewrites classes with
the JDK’s own class-file API, final in JDK 24. So it needs a test JVM of 24 or later, whatever the
code is compiled for, and proptastic-fuzz and proptastic-agent at the project’s proptastic
version. Where it can’t start them, fuzz runs the build in rounds, each a run of random tries from
a fresh seed, for as long as it’s given, and its first line says it isn’t coverage-guided, and why:
proptastic fuzz --for=20s --only=aShortHeaderIsNone ! host rounds, not coverage-guided: the test JDK is 17, and the agent needs 24 Reports build/proptastic/report/report.json · build/proptastic/report/index.html
searched 1.0s · 40.8M tries · nothing foundRounds are random tries, with no guidance: on the reader above, they’d do what the million random tries did.
It counts branches in the packages it’s told to: by default the properties’ own, or the ones
--cover names. Code outside them, a library the reader calls, runs uncounted, so a search can’t
climb through it. And as above, it climbs only where the code checks a little at a time, and
guesses only the values a single choice can hold.
What to do with what it finds
Section titled “What to do with what it finds”A counterexample a search finds is a failure like any other. It’s shrunk, here to
cafebabe0000, saved, and replayed first by every run after, so the next proptastic run fails on
it until the bug is fixed, with no search. From there:
proptastic explainputs it on one card: the input, where it failed, and how to pin it.proptastic examplepins it in the test as an.example(…), so it’s tried on every run even after the saved file is gone.
The inputs a search kept go into .proptastic/fuzz/, beside the corpus, and the next search of the
property starts from them. They aren’t added to the corpus: a search keeps far more inputs than a
run should replay. Cache the folder between nightly searches, as the CI examples do,
and each night’s search starts where the last one stopped. Searching longer
shows the command’s outputs, and proptastic fuzz has every option.