Mahdi Heydari · Ordering Before Execution

Hunting Protocol Bugs by Reading the Fixes First

A method for finding consensus and networking bugs in large codebases you don't write in — and a note on what stays valuable when the machine reads faster than you.


The thing that changed

For most of my career, finding a bug meant reading code until something looked wrong. The scarce resource was attention: there is always more code than one person can read carefully, so you pick a corner and hope you picked the right one.

That resource stopped being scarce. A model can now read a function and tell you what it does faster than I can, in a language I don't write. My first instinct was that this made my skill worthless. The truth is the opposite. When mechanical reading becomes free, the value moves to the two things the machine is still bad at: knowing where to point it, and refusing to believe it when it says "this is safe."

This is the method I built around that. It has found real, remotely-triggerable denial-of-service, resource-exhaustion, and state-divergence bugs in several large Rust blockchain codebases. The findings aren't the point — most are universal anyway. The method is.

The insight: the fix history is a map

I don't start from my own intuition about where bugs live. I start from the project's own commit history. Every serious codebase is constantly fixing bugs, and each fix is a confession: here is a place we got it wrong. Read enough confessions and you stop seeing individual bugs and start seeing shapes — the same mistake, made independently, in unrelated files, over years.

So the first phase is archaeology. Take a window of history — the last few months, or a year for a first pass — and read every non-merge commit's full diff. Not the messages; the diffs. Classify each: feature, refactor, docs, or a fix for something that could crash, hang, exhaust a resource, or make two honest nodes disagree. In one large codebase that meant reading over a thousand commits, roughly a sixth of them security-relevant. This is exactly the work that was impossible by hand and is now cheap: split the commits into batches, have a fleet of readers classify every diff against a written rubric, collect the relevant ones.

Reading every diff beats a clever keyword search, for two reasons. The obvious one: a keyword filter throws away every case that doesn't use the obvious word. The important one: maintainers often fix a security bug quietly, worded as a refactor or a "small fix," with nothing in the message a search could catch — sometimes to avoid advertising the hole before the fix rolls out, sometimes just habit. The only place the truth is written is the diff. Both reasons are only affordable now because reading is cheap.

Distilling shapes, ranked by recurrence and severity

A list of five hundred fixes is not useful. Collapse them into abstract shapes, described without the codebase's specific types, ranked by how many independent times each recurred and how bad it is when it bites.

Recurrence is the thesis. A mistake fixed once is a bug. A mistake made ten times independently, across unrelated subsystems, is a pattern — and a pattern that recurred ten times in one codebase is almost certainly sitting unfixed in the next codebase with the same architecture. Severity is the second axis, because a shape that recurs constantly but only logs a warning is not worth a shape that halts the network.

It helps to keep three failure kinds separate rather than lumping them as "crashes": a crash (a node panics and dies), resource exhaustion (memory, disk, or a queue grows unbounded until the node falls over later), and divergence (no crash at all, but two honest nodes compute different bytes from the same input — the quiet, dangerous one, because it attacks consensus itself).

The shapes that recur most, everywhere, are mundane: unbounded channels, queues, and caches fed by the network and never revisited; blocking or panicking sends inside a consensus-critical loop; assertions on attacker-controllable conditions; expensive work done before the cheap rejection check; resource limits never reconciled with the host's real provisioning; aggregate-signature gaps; metering charged by a cheap-to-measure proxy instead of the thing that actually costs. Underneath most of them is one meta-shape: trust delegated across a boundary by a comment. One layer assumes another already validated something — "the caller checks this" — and the assumption lives in a comment, not in code. Sometimes it holds. Sometimes it doesn't, and that is the bug. A comment is a claim, never evidence.

Hunting a new codebase

With a ranked catalog, hunting a new system is translation. Map the target's real structure first, and treat every guess about "the equivalent of X is probably Y" as a hint to verify — in practice every one of my first guesses needed correcting once I opened the source. Then hunt one shape at a time, highest-recurrence first, and stop after each. I tried running patterns in parallel once; it was faster and worse, because nobody was watching any single thread closely enough to catch a soft verdict. One at a time, deliberately slow. Depth over throughput.

The part only I can do: distrusting "safe"

This is where the method lives or dies, and it does not get cheaper as models improve. When a model reads a suspicious function and concludes "safe because X," it is right often enough to be dangerous — wrong in a confident, well-written sentence that is easy to accept. Nearly every "safe" verdict I went back and challenged needed correcting. Not some. Nearly every one.

They failed the same two ways. Generalizing from one example: the model checked one caller and the sentence quietly became "every caller." Once, a verdict rested on "around fourteen setters, every one validates." Forced to actually enumerate them, it was nineteen, and five didn't validate — that gap was the bug. Trusting a comment instead of traced code: a closing argument sourced from a comment, a prior write-up, or the model's own earlier summary, never from code read this session.

The best trick I have is almost absurdly simple: tell the model to challenge its own reasoning. The same model that just wrote a confident "safe" will, asked to attack its own verdict, often find the hole itself within minutes. It won't do this alone — left to itself it defends its first answer — but directed to be its own adversary it is astonishingly effective. And one round often isn't enough: a verdict would survive the first challenge and the second and only crack open on the fourth or fifth, each pass peeling off one more reassuring layer. I'll admit to a trick that shouldn't work as well as it does — now and then I'd say "I'm sure there's a problem here" when I had no idea whether there was. More than once, after several rounds of being pushed as if a bug were definitely there, one that was definitely there surfaced. My job is mostly to keep asking, and to know which verdicts are worth asking about.

The rest is mechanical: before any "safe" verdict, name the one claim it depends on and test it against those two red flags plus a third — is this just plausibility with no attempted counterexample? Two rules capture the whole discipline:

A completeness claim is never a sentence. It's a search, and the search's output is the evidence.
Before you write "safe," try to break it.

And the belief underneath all of it:

Most missed bugs aren't missed because nobody looked. They're missed because someone was sure they'd looked everywhere, and hadn't.

Reading proves a mechanism exists. It can't prove it's reachable.

A bug on paper is not a bug. Reading shows a dangerous mechanism exists — an unbounded queue, an unchecked cast. It usually can't show an attacker can reach it at meaningful scale. Those are different claims, and only the second is a finding.

So every verdict that looks real gets promoted out of the tracking file into its own followup document, and I open it in a fresh session with no memory of how I convinced myself the first time. This is where most of the challenge rounds actually happen. Finding the candidate is the easy half; the deep read of all the surrounding code, and the four or five rounds of trying to break the reachability claim, belong here, in the followup, not back at the first verdict.

Then you build the proof of reachability, and building it is itself the hardest challenge round. Things appear when you write a PoC that no amount of reading surfaced. And the most humbling lesson is how often "not safe" collapses back to safe only at this step: a small unit test makes the bug look real, and then you run it against a real multi-node swarm and the system absorbs it — backpressure, a timeout, a check on another node — in a way the unit test never modeled. A mechanism that is reachable in isolation is not always reachable in a running network. You don't learn which until you build the swarm and try.

If you want to try this

The method compresses to a few habits:

Use the model for all of it: the reading, the classification, the tracing, the first verdict — it will often find the flaw in its own reasoning faster than you could, once you make it look. The one step you can't hand off is the decision to stop. Left alone, the model settles on its first comfortable answer; the whole method is a structured refusal to let it settle too early. That refusal is the part that stays yours, and it is enough.


Mahdi Heydari · protocol engineer & security researcher · Ordering Before Execution · Resume · github.com/abramsymons · x.com/0xmahdi_ · linkedin.com/in/abramsymons