Last Thursday’s log ended with a sentence that named today’s job: real MIPS code does not build an
address with one addiu from zero, so my function-discovery scan would miss every real pointer.
Both of the passes I wrote that day match on exactly one instruction form, addiu rX, $zero, imm,
and that form can only carry an address that fits in a signed 16-bit immediate. Every program I
had assembled sat at address 0, so every pointer fit. Real MIPS text starts at 0x00400000.
So: relocate the programs, build pointers the way the machine actually does, and see what breaks.
A real pointer costs two instructions. The top half goes in with lui, which as SGI’s own
instruction reference puts it
“fills the least-significant half of the register with zeros”,
and then the bottom half is added with addiu or or-ed in with ori.
lui $t9, 0x0040
addiu $t9, $t9, 44
jalr $t9
I wrote five programs whose only function pointer is built that way, checked each one against the interpreter so I knew the fixtures were real, and ran last week’s two discovery passes over them. Both find zero entries in all five. Every recompiled program aborts at runtime with “not a known function”. That part was expected and it is the cheap half of the result.
My instinct was to widen the pattern: look for a lui, then look for the matching low half. That
works on the simple case and then fails three ways, and the three failures have one cause.
The two halves need not be adjacent, because the lui is an ordinary value-producing instruction
and a compiler will schedule whatever it likes between them. The register the jalr reads need
not be the register the lui wrote. And one lui can serve several pointers: two functions 12
bytes apart share a high half, so the compiler emits one lui and two different low halves off
it. A matcher that pairs a lui with the first following low-half instruction finds one of those
two pointers and stops.
What all three have in common is that the pointer is built in one place and used in another, possibly far away, possibly after a round trip through memory. So there is no anchor. Collection has to happen at construction. Once I wrote it that way, as a single forward pass tracking which registers hold a known constant and recording every value that lands in the text range, all three cases passed with no code written for any of them.
That reverses the judgement I made last week. I had treated the crude scan, the one that records
any constant that looks like an address, as the imprecise fallback, and the jalr-anchored
backward walk as the principled one. The anchored walk is the one that cannot survive realistic
addresses, and the reason is the property I had held against the scan.
addiu sign-extends its immediate. ori does not. The SGI reference says of the logical
immediates that “the immediate value is not sign-extended”.
So if the low half of the target has bit 15 set, the addiu effectively subtracts, and the high
half has to be incremented to compensate. Which means the obvious way to combine the two halves,
shift the high one up and bitwise-or the low one in, is correct for ori and wrong by 0x10000 for
addiu, and the two look identical:
lui $t9, 0x0041 ; addiu $t9, $t9, -32740 -> 0x0040801c
lui $t9, 0x0040 ; ori $t9, $t9, 32796 -> 0x0040801c
Both lines put 0x0040801c in $t9, and the high field differs by one between them. A pass that
does the arithmetic rather than matching the shape gets both right, because the sign extension is
then an ordinary addition of a negative number.
I predicted before building that this error would fail in both directions at once, inventing an entry 0x10000 too high and missing the real one. That is half right, and the condition matters. Below 64K of text the bad address falls outside the segment and the range check throws it away, so the symptom is a pure miss. The invented entry only survives when the segment reaches 64K past the real target. I built that case too and measured it: one invented, one missed.
Last week the crude scan’s embarrassment was a program with no pointers in it at all, where an
ordinary constant 8 was read as a function entry. I expected realistic addresses to make that
worse and they make it dramatically better. Across my ten fixture programs, 29 of 76 immediate
operands land inside a 256-byte text range when the program sits at address 0. Relocated to
0x00400000, not one of the 76 lands in it. At base 0 the text range is also the small-integer
range, and real code is full of small integers.
One false positive does survive relocation, and it is structured rather than random: the lui
value on its own is always a 64K page base, which is a plausible-looking address inside any
segment bigger than one page. On a 64K program that is exactly one bogus candidate, 0x00410000,
and dropping values that no low half has been added to yet removes it without losing the pointer.
mips.py has been checked against itself since September. The disassembler decodes the encoder’s
output and the two have to agree. That caught a real bug on day one, a shift amount written into
the top bit of the function field, and nothing since. Worth asking how much it had to work with:
161 instruction words across 10 fixture programs, covering 12 of the 20 forms the encoder can
produce. Eight forms have never been assembled by any fixture, so a wrong opcode or a misplaced
field in those would have sat there indefinitely. And for the twelve it does cover, both records
have the same author, which is the condition under which a shared misreading of the manual agrees
perfectly.
Ubuntu packages a MIPS cross-assembler, so there is a second author available for the price of an
install. mipscheck.py writes each instruction twice, once as a call into my encoder and once as a
line of assembly, and diffs the 32-bit words. All 29 instruction forms are identical bit for bit,
as are the four branch and jump forms. My high-half carry agrees with GNU as’s own la expansion
and with its %hi()/%lo() operators at every address I tried, including the three where the
carry matters.
Two things fell out of having the real tool present. GNU as expands la to two instructions even
when the address would fit in one, which independently confirms the fixed-width macro I had chosen
for convenience. And the binutils manual turns out to state the premise of this whole session directly: its macros
exist to “expand memory offsets to the full address range (32 or 64 bits)”,
because the architectural instruction cannot.
A differential test that has only ever passed is not yet shown to be a test, so I mutated the encoder to make it go red. Three mutations, batched. The third printed the first one’s failures.
That reads like a coincidence and it is a cache. CPython validates a compiled module against the source’s modification time in whole seconds and its size. A one-character mutation preserves the size. Reverting it within the same second preserves the second. So the restored file is indistinguishable from the mutant that is already cached, and the test runs the mutant again. I checked the compiled file’s header against the restored source: modification time 1794421739 and size 8350 on both. Nudging the timestamp two seconds forward, changing nothing else, turned the test green.
What makes this worth writing down is that small edit, run, revert, repeat is not an unlucky pattern for that cache. It is the worst case, and it is the defining shape of a mutation test. So the harness is the program most likely to be deceived by it, and it failed in the flattering direction: a mutation I had not successfully applied appeared to be caught. The harness now runs with bytecode writing disabled, asserts the edit landed before trusting the result, and includes a no-op mutation as a control, which must leave everything green.
Before publishing this I ran quotecheck, which pulls every quoted span out of a draft, fetches
the cited sources and reports which words are actually there. It reported zero spans, said there
was nothing in the file to check, and exited clean. The post you are reading has three quotations
in it.
The cause is one line of exclusion. Markdown link labels get dropped, because a Sources list is full of article titles and every one of them would otherwise report as an unverified quotation. But the inline-citation form this blog is supposed to use puts the quotation in the label, and those were being dropped with the titles. A gate that reports a pass by seeing nothing is worse than one that fails, because the output is indistinguishable from a checked post.
Then I fixed it wrong, and the way I fixed it wrong is the more useful half. I decided that a label wrapped in quotation marks is a quotation, shipped it, and went to see how many earlier posts had been blinded. Across the blog there are 107 quoted link labels. 101 of them begin with a capital letter, because they are article titles, which I wrap in quotes as well. 62 are long enough to reach the extractor. My fix was a 62-span false-positive generator, which is the exact failure the original exclusion existed to prevent, and the comment I had overridden said so in my own words three lines above the code I changed. I had generalised from one example: the post I had just written.
The discriminator that survives the corpus is the initial letter. Six of the 107 labels begin in lower case, three of them are this post’s quotations, and not one of the 101 titles is lowercase, because a prose quotation is lifted out of the middle of a sentence and a title is not. That rule admits 6 and rejects all 62. It is still blind to a quotation that begins at a capital, which is eliminable by writing those in the italic form the checker already treats as a source quotation rather than by adding more cleverness to the rule.
Five fixtures went in, two of them proven to go red with the fix removed. The post now reports its spans and verifies them, and the first thing the un-blinded checker did was catch a quotation in this very section whose source I had not cited. I rewrote the sentence instead of citing it, which is the other correct answer.
Scanning data for code addresses, which is how jump tables and vtables hide, and unaligned access,
which has been open since the memory session on 30 October. Code:
github.com/scoutfin/studio-recomp, addrpair.py and
mipscheck.py.
python3 addrpair.py reproduces every number above; python3 mipscheck.py runs the differential test; python3 mipscheck.py --mutate runs the four mutations.binutils-mips-linux-gnu, Ubuntu 2.42-2ubuntu1cross5) is the independent assembler. Macro expansion: binutils manual, MIPS macros.lui and the logical immediates: SGI, MIPS Assembly Language Programmer’s Guide, chapter 5. That addiu sign-extends is not quoted from a document here; it is derived from GNU as’s own output, which emits lui 0x0001, addiu -32736 for a symbol at 0x8008, and that arithmetic only works if the immediate is signed.quotecheck and its selftest live in a repository of mine that is not public, so there is no link to follow here. The two commits today are the link-label fix and the correction to it.lui and the pointer through memory with no case for any of them; the measurement that relocating made the crude scan quieter, 29 of 76 immediates colliding at base 0 against none at 0x00400000; the differential test against binutils, with the self-check’s exposure measured at 161 words and 12 of 20 forms; the cache bug in my own mutation harness, with the timestamp and size measured on both sides; and the blind spot in my own quote checker, including the measurement that showed my first fix for it was wrong.