One of my PoC checker's rules was wrong more than two times in three

Per-rule precision for realism-gate 0.2 on three pinned corpora, with intervals, a fix graded honestly, and a one-line CI step.

When I read every flag one rule of realism-gate raised, in context, it was right 4 times out of 13. That is the no-baseline rule, RG10, and it is the number I would least like to publish. The rule had shipped in 0.1.0, it had a documentation page explaining how it could be wrong, and a review I did at the time had called most of its flags correct. That review was wrong in a specific, fixable way, and the fix is in 0.2.0. Along the way one other rule lost its right to fail a build, and the tool learned to run in CI in one line. This is what was measured, what changed, and what the numbers do not tell you.

What realism-gate flags, and why a flag is not a bug

realism-gate reads a Foundry proof of concept and reports what makes a passing test prove less than it claims. It never runs the test and never touches the network. It parses the Solidity, rebuilds the order in which the test's code runs (initializers, setUp(), the test wrapped in its modifiers, inlined helpers, callbacks), works out who sends every call, and looks for a fixed list of patterns: the exploit step runs as the owner, a balance is written with deal(), a storage slot is written with vm.store, the assertion compares a value with itself, the profit is measured without a starting value.

A flag is a claim about the test, not about the protocol. "This step runs as a keeper" is true or false by reading the test. Whether the keeper really runs that step on a schedule, so the attack still works, is a question for the report, and the tool does not try to answer it. So the question I can measure is narrow: when a rule fires, is the thing it says about the test true, and is it something a reviewer of that PoC would need explained?

For 0.1.0 I ran it over DeFiHackLabs, a public collection of Foundry tests that reproduce real exploits. Of its 886 tests, 611 (69.0%) assert nothing at all; they log balances and leave the reader to compare them. In 0.2.0, 20 tests have a finding other than that one. Those figures did not change much. What I had never done properly was count how often each flag was right.

How the sample was drawn and labelled

Three corpora, each pinned by commit and by the sha256 of its file listing:

  • in-sample: DeFiHackLabs src/test at the commit 0.1.0 was built against, 891 files. The rules were written while reading this corpus, so any number on it is optimistic.

  • later: the 70 .sol files added to DeFiHackLabs after that commit. The rules had never seen them. I labelled them before writing any fix, but two of the patterns I then fixed were first visible here, so this is a second tuning set, not a held-out one. I had intended it as held-out and it stopped being one the moment I looked at it.

  • fresh: DeFiVulnLabs src/test, 57 files, a different style of test (local deployments of deliberately vulnerable contracts, not fork reproductions). I first opened it after freezing the rule logic, and nothing in it was used to change a rule.

A flag is an active finding. Its id is a hash of the rule, file, line, column and the flagged source, so the same flag in 0.1.0 and 0.2.0 has the same id and one label covers both. Per corpus and per rule, flags were ordered by sha256 of a committed seed and the id, and the first 30 were taken, or all of them if the rule raised 30 or fewer. Only the no-assertion rule ever raised more than 30: 611 in-sample, of which 30 were drawn, and 55 in the fresh corpus, of which 30 were drawn. Every other rule's flags were labelled in full.

Each sampled flag got one of three labels and a one-line reason:

  • true positive: the pattern is there and the rule's stated consequence holds in context;

  • false positive: the consequence does not hold (the "replaced" code is the attacker's own contract; the "unknown" starting balance was set by the test);

  • unclear: the source alone does not settle it.

Precision is true positives over true plus false positives, with a Wilson 95% interval. None of the 106 labels ended up unclear, which says as much about the labeller's confidence as about the flags.

I reviewed all 106 labels against the source around each flag. They are published, one line each with its reason, so anyone can check them and disagree.

Precision per rule

In-sample, 0.1.0 against 0.2.0. Brackets are the 95% interval in percent.

Rule

0.1.0

0.2.0

RG01 attacker holds a role

1/1 [21-100]

1/1 [21-100]

RG02 privileged step in the exploit

15/15 [80-100]

15/15 [80-100]

RG04 storage written with vm.store

7/7 [65-100]

7/7 [65-100]

RG05 deployed code replaced

2/4 [15-85]

2/4 [15-85]

RG08 no assertion (30 of 611 drawn)

30/30 [89-100]

30/30 [89-100]

RG10 gain without a baseline

2/5 [12-77]

2/2 [34-100]

RG11 fork not pinned to a block

1/1 [21-100]

1/1 [21-100]

Read the intervals before the fractions. "1/1" means the rule fired once and was right once; the interval says the true rate could be anywhere from about a fifth to all of the time. Only two rows have enough flags to say much: the no-assertion rule (the lower bound is 89% on 30 draws) and the privileged-step rule (80% on 15).

Pooled over all three corpora, the no-assertion rule was right on 61 of 61 labelled flags, an interval of 94-100%. The privileged-step rule was right on 15 of 16 [72-99]; the miss is in the later set, a fresh key whose name contains a role word, registering itself. RG10 in 0.1.0 was right on 4 of 13 [13-58]. RG05 was right on 2 of 5 [12-77].

Six rules never fired on any of the three corpora: the two about harnesses that deploy the protocol themselves, the tautological-assertion rule, and the three that read an optional attacker-model file. Their precision is unknown, not 100%.

The false-positive patterns, and what changed

RG10 checks that a gain is measured against a starting value. It fires on assertGt(token.balanceOf(attacker), X) when nothing reads that balance before the attack, because an account on a fork can already hold tokens. Every one of its nine false positives had the same cause: the test had fixed the starting balance itself, so the absolute read afterwards was already a difference. It showed up in four forms:

  1. deal() or vm.deal of the measured account before the attack, such as dealing a taker four units of a stablecoin and then asserting it ends with over a million.

  2. A shared logging modifier, inherited from a base contract, that zeroes the attacker's ether with vm.deal(_attacker(), 0), where _attacker() is an internal getter returning attacker, or the test contract if that is unset. The tool inlined the modifier but could not see through the getter.

  3. A contract the test deploys and funds with one token, measured in a different one. 0.1.0 treated "funded" as a property of the address, not of the address and the asset.

  4. The test contract's own token balances. Foundry deploys the test contract fresh; it holds no tokens before the attack.

0.2.0 handles all four: it resolves zero-argument getters and the "default to the test contract" ternary, tracks funding per asset, and treats the test contract as fresh for tokens. It still flags three things that look similar and are not: > 0 over a balance the test funded (the funding alone satisfies it), a literal bound below a literal funded amount, and the test contract's ether, because Foundry gives the test contract a large ether balance by default. The first and the last have test fixtures that must keep firing; the middle one is covered only by the code, which is a gap.

RG05 checks for code replaced with vm.etch. Of its five flags, two replace the vulnerable contract with a hand-written stand-in, which is exactly what it is for. The other three recreate the attacker's own historical contract at its old address, or install a chain-wide registry contract that a local test chain does not have. From the test source, those three look like the two good ones: a constant address, code from a contract written next to the test. I could not find a rule that separated them without also hiding the true positives, so I did not write one. Instead RG05 is now a warning, never an error. The release had a written floor: a rule under 80% on its labelled sample does not get to fail a build. RG05 is at 40%. CI enforces the floor against the published labels on every change.

Before and after on data the fix had not been written against

Here the honest answer is thinner than I wanted.

On the later files, RG10 went from 2 of 8 right [7-59] to 1 of 1 right. Six false positives went away and one true positive went with them: a test that asserts WETH > 0 on an account it had dealt WETH, next to other assertions that do have a baseline. RG10 stays silent on a test once any of its assertions has a baseline, and before the fix those other assertions did not count. But as I said, I had looked at these files before writing the fix, so this is not an out-of-sample result.

On the fresh corpus, which I had not looked at, 0.1.0 and 0.2.0 raise exactly the same 57 flags. RG10 does not fire there at all. So the RG10 fix has no out-of-sample measurement. The only grade it has is on data that was in front of me while I wrote it. What the fresh corpus does show is that nothing else moved: the no-assertion rule is 30 of 30 there too, and the one RG05 flag there is the registry case above, a false positive in 0.1.0 and 0.2.0 alike.

What the tool still cannot tell you

  • Recall. I can say how often a flag is right. I cannot say how many real problems it misses: that needs PoCs labelled as defective whether or not a rule fired, and I do not have that set. I have not given a recall number anywhere and I will not invent one.

  • Anything a static read cannot see. It reads the test, not the deployed protocol, and not foundry.toml. It flattens control flow, so a vm.deal inside an if counts as if it always ran.

  • Bounty PoCs. All three corpora are public reproductions or teaching demos. A post-incident reproduction is allowed to impersonate a compromised key; a bounty PoC that does the same is proving something else. The precision of "this step runs as the owner" does not depend on that. How much the flag matters does.

  • A second opinion. One reviewer, one pass. The labels are published so that can change.

A clean run means no known defect pattern was found. It does not mean the PoC is valid, the bug is real, or the program pays for it.

Putting it in CI in one line

- uses: KhomDev/realism-gate@v0
  with:
    path: test/

The action downloads the release binary for the runner, checks it against the release's SHA256SUMS, and needs no Rust toolchain. It writes the results to the job summary, keeps one pull-request comment edited in place instead of adding one per push, and by default reports only the files the pull request changes while still reading everything, so inherited setup code is understood. A path with no Foundry tests fails the job instead of passing quietly. On a pull request from a fork, whose token cannot comment, it falls back to the job summary and says so. The documentation shows how to comment on fork pull requests from a second workflow that never checks out the fork's code.

Outside GitHub, cargo binstall realism-gate installs a prebuilt binary, and realism-gate check test --changed-since origin/main does the same changed-files filtering locally.

Reproduce it yourself

git clone https://github.com/KhomDev/realism-gate && cd realism-gate
git checkout v0.2.0
cargo build --release
gh release download v0.1.0 --pattern 'realism-gate-0.1.0-x86_64-unknown-linux-gnu.tar.gz' --pattern SHA256SUMS
sha256sum -c SHA256SUMS --ignore-missing
tar xzf realism-gate-0.1.0-x86_64-unknown-linux-gnu.tar.gz
bash eval/run.sh realism-gate-0.1.0-x86_64-unknown-linux-gnu/realism-gate target/release/realism-gate --check

eval/run.sh fetches the three corpora at their pinned commits, checks each listing hash, regenerates every flag file with both binaries, recomputes the precision tables from the labels, and fails if anything differs from what is committed, or if a rule under the 80% floor ships as an error. To read the source behind any label: python eval/show.py in_sample eval/samples/in_sample.jsonl --id <id>.

Data

Repository KhomDev/realism-gate, tag v0.2.0. sha256 of each file as committed (LF line endings):

9586b509014800a1ea6a4e34a1f12d86e04bff6fcfe7693b18abff1f55be857c  eval/corpus.json
e1b2b9b8f86d956f7d8dde06c02be3d815dfdb05ff7e9fcf786b279e4d772903  eval/seed.txt
8fec11416fd44854eff158ba39505fc59a9bc49728bb7249f1ddc7ddba4c7abd  eval/labels/v0.2.0.jsonl
aa48aef35b6fbf76fc959af7ce38e08fbb5197b9fb54c8a332231cad6324b88e  eval/samples/in_sample.jsonl
79c715bc52af69e3cd5fb36ec921edcfc5438ada65b9910461a8182ef880eda1  eval/samples/later.jsonl
2939b5485da4e05f6aa91f2aaad329f19589a73a2acf0a46d2ee1d32847432d9  eval/samples/fresh.jsonl
3a6a7b7578657689a7a7c8d1b9c5f6ae4c6384a4df66864d39d0f0affab17626  eval/results/precision.json
8cc27cc67564e3c770b967372ff094696eda9c52031e5c1d22d18c8ccd05b518  bench/defihacklabs.json

Corpora, pinned in eval/corpus.json by commit and listing sha256:

DeFiHackLabs  2c99b565ae24ea2006adf181da20c4419b3edc30  listing a7e8949a4b0613670edff919820aa793d0eca255e0b3f2f4fd6b243f2f803bc7  (in-sample, 891 files)
DeFiHackLabs  d4bd931e758a375ed7fcecf52322dfeab54f8c0d  new files 318c8027cfa386766a32d71bb29f7b166bcc7d6e3a6dba17e15f09c8504eea24  (later, 70 files)
DeFiVulnLabs  f61f6ee5c3f89eb7685030647f8df19997596f8b  listing 219497dfbf4b7d0fb34ea2f96d78a142b6bc12c39a17cd35f3b1db1f94d1c8b2  (fresh, 57 files)

Rule logic frozen at commit e9bea03 before the fresh corpus was opened.