A null that wasn’t null
The setting
We’re reverse engineering a large 32-bit x86 Linux program that ships without symbols, by rewriting its functions as C++ one small batch at a time.
The rewriting is done by a heterogeneous fleet of language-model agents. Models from three providers work side by side, each in its own vendor’s command-line agent:
- OpenAI’s GPT-5.6 Sol, GPT-5.6 Luna and GPT-6 Luna, through Codex;
- Google’s Gemini 3.8 Flash and Gemini 4 Argon;
- Anthropic’s Claude Haiku, Sonnet and Opus 5.5, through Claude Code.
A coordinating Claude Opus session mines batches of functions from the decompiled executable, hands them out, sends batches one model has failed to the strongest workers, and paces each subscription against its weekly quota.
A fleet like that only works if no worker’s word is taken for anything. Every rewrite has to earn its place: its test runs it next to the original machine code on 10,000 inputs and requires identical results every time. The tests are written by the workers too, and this post is about a test that was wrong.
That comparison happens inside one process. The test loads the program’s executable at its fixed load address, so the original machine code for a function sits in memory next to our C++ rewrite of it. The test then calls both with the same inputs, the original straight at its address in the program, and checks that they agree. Every round, the whole collection (now about 18,000 functions) is re-run in 20 parallel processes. A function that fails a later run is quarantined automatically, so one bad test can’t stop the others.
On 29 October one was quarantined: a 60-byte function at 0x0a98b1a0. It had been in the collection since the 27th, from the fleet’s first days, before the pipeline recorded which model wrote each batch. It had passed about 230 runs since we started logging them, and then it failed one.
The function
Here is the rewrite. The names are the worker’s guesses; the arithmetic is the machine code’s:
inline void* channel_slot_alloc(void* base, int32_t index) noexcept {
if (index == -1) return nullptr;
auto* b = reinterpret_cast<char*>(base);
auto* count_p = reinterpret_cast<uint32_t*>(b + index * 8 + 0x114);
uint32_t count = *count_p;
if (count > 0xffff) return nullptr;
*count_p = count + 1;
return b + 0x80114 + count * 0x2c;
}
It hands out slots from a pool. Near the start of some large object is a table of counters, one per index. From 512 KB in comes an array of 44-byte records, room for 65,536 of them. The function bumps a counter and returns the address of the next record. That makes the object about 3.4 MB.
It never touches the record it returns; its caller does. So the test only had to back the counter table, and it used a 2 KB buffer on the stack:
alignas(16) uint8_t buf_r[0x800]{0}; // for the original
alignas(16) uint8_t buf_c[0x800]{0}; // for the rewrite
...
void* ret_r = retail(buf_r, index);
void* ret_c = channel_slot_alloc(buf_c, index);
uintptr_t diff_r = ret_r ? (uintptr_t)ret_r - (uintptr_t)buf_r : 0;
uintptr_t diff_c = ret_c ? (uintptr_t)ret_c - (uintptr_t)buf_c : 0;
assert(diff_r == diff_c);
Two buffers at two addresses, so the test compares offsets rather than pointers, and treats null as offset 0. That looks reasonable.
The red herring
The function reads nothing but its arguments, and the test prepares both buffers identically. Its inputs come from a fixed-seed generator, so every run sees the same 10,000 of them. Run on its own, the test passed every time we tried it. So for a while the leading theory was that the original code had been changed: some earlier test in the same process writing through a stray pointer into the program’s mapped code, which the harness had mapped writable.
We tested that by mapping the program’s code read-only after loading it, so a stray write would crash at the culprit. All 17,830 functions passed. No test was writing into the code. (The change then broke the next round itself. One test patches two bytes of the program on purpose, and our edit giving it permission was overwritten when its harness was regenerated from source. Read-only code is still a good idea: it is now in the harness, and the deliberate patch goes through a helper that unlocks just those bytes.)
Where the address goes
The returned address is base + 0x80114 + 44 × count, up to 3.4 MB past a 2 KB buffer. Where is the buffer?
A 32-bit process on a 64-bit Linux kernel gets nearly the whole 4 GB address space, and its stack starts just below the top, near 0xffffffff. Address-space randomisation moves it a little each run. So a buffer on the stack can sit within 3.4 MB of the top of memory, and adding 3.4 MB to its address goes past the top and wraps around to small numbers.
For one stack position and one counter value, the sum wraps to exactly zero. To check, we mapped a page at 0xfff7f000 and put a buffer at 0xfff7feec, which is 2³² − 0x80114, with its counter at 0:
base 0xfff7feec count 0: retail (nil), reconstruction (nil); test offsets 0 vs 0x80114
Both versions return the same bits, zero, which is the right answer. The original code and the rewrite agree. But the test computed an offset of 0 for the original and 0x80114 for the rewrite, and failed.
Null, and also not null
The rewrite’s return value printed as (nil), yet the test’s ret_c ? … : 0 took the non-null branch.
The compiler inlined the rewrite into the test, so it could see exactly where ret_c came from: b + 0x80114 + count * 0x2c, a valid pointer plus an offset. In C and C++, arithmetic that leaves its object is undefined behaviour, so the compiler is entitled to assume it never happens, and in particular that a valid pointer plus an offset is never null. GCC uses assumptions like that to delete checks it considers always true. (This is the modern GCC 13.3 we build the tests with, not the GCC 6.3 that built the program; the program’s code is only ever run, never recompiled.) Evidently it decided the test’s null check was one of them, and computed the offset as plain 0x80114 + count * 0x2c, without looking at the pointer at all. (We saw the result, not which optimisation pass produced it.)
The original code got no such treatment. The test calls it through a function pointer into the program’s machine code, which the compiler can’t see into, so ret_r really was compared with zero.
So the same value, zero, was null on one side of the comparison and an offset of 0x80114 on the other. The machine code was never wrong: x86 address arithmetic simply wraps. Only the test, which modelled that arithmetic as C++ pointers, had promised the compiler something that wasn’t true.
An old bug in a new place
This is a well-worn pair of hazards: address wraparound, and compilers optimising on the assumption that it can’t happen. In 2008 CERT published advisory VU#162289 after GCC began silently deleting overflow checks like if (buf + len < buf), which programmers had written precisely to catch wraparound. The standard fix then is the standard fix now: do the comparison on integers or lengths, not on pointers.
It mostly belongs to the 32-bit era. A 64-bit process’s addresses top out around 2⁴⁷, so a few megabytes never come near wrapping. You need a 32-bit process, a buffer near the top of its address space, and a pointer formed well outside the buffer. In 32-bit memory only the stack lives that high: heap blocks, including large ones from mmap, sit lower.
In the program itself, the object is presumably a real 3.4 MB allocation, not a stack buffer, so the original code almost certainly never meets this. The bug is the test’s. A 2 KB stand-in for a 3.4 MB object made every returned pointer point outside its object, and the stack was the one place where that could wrap.
The fix
The function is back in the queue with a corrected test, which runs both versions on one buffer at one address:
- save the buffer;
- run the original, and keep its return value and the buffer’s contents;
- restore the buffer and run the rewrite;
- compare the raw return values and the whole buffer.
Same address, so no offset is needed and no pointer outside the object is ever used for anything but its bits. Whichever worker picks it up gets the story above with its assignment.
For a fleet, the lesson is about where trust sits. Differential testing lets us combine the work of models we can’t individually vouch for, but the tests are agent-written code too. Here a correct rewrite was quarantined by its own test, and a flawed test can just as easily wave a wrong rewrite through. Checking the checkers is part of the job.
It isn’t always the test’s fault, either. The same harness has caught a real bug in the program itself. A routine that sorts matrices by size before packing them into one large block compares them by their two dimensions, but its comparison isn’t consistent: an 82×402 matrix and a 400×100 one each compare as smaller than the other. The C++ standard library’s sort assumes a consistent ordering, and for lists longer than 28 its last pass trusts that the smallest element is already at the front, so it never checks where the array begins. With this comparison that trust is misplaced, and the pass walks off the front of the array and crashes. The matrices the program ships with seem never to have the shape that triggers it. Custom matrices we introduced did, and the in-process harness reproduces the crash register for register. (This is the test that patches two bytes of the program on purpose: the patch sends every list through the sort’s guarded pass instead.)
The inputs were the same in all of those runs. What changed was where address-space randomisation put the stack, and once in about 230 runs it put it in just the wrong place.