Repository navigation
[AUTOMATED] fix(p3): reconcile a renaming input with guardInput's leftover pieces (DIV-50) - #238
Merged
Merged
Conversation
…tover pieces (DIV-50)
gnulib's `rpl_fcntl` compiled at -O2 produced no body at all in 48 of 267
optimized x86-64 binaries (gzip, gnutls-serv, grep, tar, findutils, diffutils,
39 coreutils), exactly one function per binary:
rename_recurse: set_input_varnode (empty stack):
Lowlevel { explain: "Overlapping input varnodes" }
Classification: this is NOT a port defect and NOT a precondition kuna alone
violates. Ghidra 12.1 headless raises the identical
`Low-level Error: Overlapping input varnodes` on the same address of the same
binary, so it is an upstream bug kuna inherited faithfully; the fix is a kuna
divergence (DIV-50), not a convergence.
Root cause, P3 dataflow. gcc -O2 spills the register arguments of a varargs
function into a va_list register-save area and reads them back through an
indexed load. The spill has no direct reader on the first stack heritage pass,
so ActionDeadCode removes it; the indexed load only folds to the constant stack
offset on a later pass. From there heritage invents an input for the
now-undefined slot, SubvariableFlow narrows it to 4 bytes, and
Heritage::guardInput tiles the 8-byte range with two write-masked 4-byte input
pieces plus a concatenating PIECE. The simplification pools then fold that PIECE
away. On the next pass a fresh free 8-byte read of the same range finds an empty
variable stack, asks setInputVarnode for a full-size input, and collides with the
leftover pieces.
The leftover pieces are exactly the value the read wants, so the collision is
reconciled instead of raised: p3_dataflow/kuna_inputtile.rs completes the request
into an end-to-end tiling (a fresh input for any hole, as guardInput does) and
folds it into one full-size input with the upstream
Funcdata::combineInputVarnodes primitive, which destroys the pieces, rewrites
concatenating PIECEs into COPYs and repoints every other reader at a SUBPIECE of
the whole. Only write-masked pieces fully contained in the request are folded -- a
write-masked Varnode is never pushed onto a VariableStack, so the rewrite cannot
strand a renaming-stack entry -- and every other overlap still raises, exactly as
upstream.
No option: the affected functions produced no output at all before, and the new
path runs only where setInputVarnode was about to raise.
Evidence
- 267-binary O2 decbench corpus, 76,527 functions: overlapping-input class
48 -> 0 functions in 48 -> 0 binaries; total error records 56 -> 8 (the 8
residuals are an unrelated "Unable to load 512 bytes" class, unchanged).
- 0 functions stop being produced, 48 start.
- Whole-binary text diffs on gzip, coreutils sort, diffutils diff, findutils find
and coreutils nohup change exactly 1 function each: the one that used to fail.
- Speed, interleaved medians of 5 (`decompile-all --mode reliable`): gzip
4,796 -> 4,792 ms (-0.08%), coreutils sort 13,477 -> 13,114 ms (-2.69%),
findutils find 8,362 -> 8,279 ms (-0.99%).
- Gates: make test PARITY OK (675/675), make test-stages PARITY OK (346/346, the
delta is purely the 8 new keys, no re-pin and no per-test opt-out), make
rust-test green, make check-spec green, kuna catalog --check green.
- tests/stages/ghdec-inputtile-overlap.xml: 1 of 8 assertions passes pre-fix,
8/8 post-fix. The bytes are a reduced standalone gcc -O2 build of the gnulib
wrapper; the vendored gzip bytes did not reproduce outside the ELF loader path.
Known residual (out of scope, noted in the DIV row): the recovered body still
reads the spill slot as an undefined `stack - 0x38` local. Keeping the va_list
spill alive across the dead-code round needs Heritage::guardLoads /
handleNewLoadCopies (kuna hard-wires `highPtrPossible = false`), a separate and
much larger port.
Provenance: inspiration = "decbench:cases-missing pool; gnulib rpl_fcntl at -O2,
48 of 213 optimized x86-64 binaries".
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011RXVU58TKJxb518oViChwo
…rlapping-inputs # Conflicts: # docs/history.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
gnulib's
rpl_fcntlcompiled at-O2produced no body at all — one functionin each of 48 of the 267 optimized x86-64 decbench binaries (gzip,
gnutls-serv, grep, tar, findutils, diffutils, 39 coreutils):
0xe0f0isrpl_fcntlin the unstripped build. It is-O2-only:O0andO2-noinlineare clean.Root-cause classification
Neither an unported guard nor a kuna-only precondition violation: an upstream
Ghidra bug kuna inherited faithfully.
I ran the pinned upstream sources side by side with the Rust port —
Heritage::renameRecurse,Funcdata::setInputVarnode,Heritage::guardInput,Heritage::collect,SubvariableFlow::getReplaceVarnode/replaceInputare allfaithful transcriptions. So I ran the real thing:
Ghidra 12.1 fails on the same address of the same binary with the same message.
The fix is therefore a kuna divergence (DIV-50), not a Convergence.
Why
-O2rpl_fcntlis a varargs wrapper. At-O2gcc spills the register arguments intoa SysV va_list register-save area and reads the third argument back through an
indexed load:
Traced with instrumentation on the failing function, the sequence is:
stack -0x38W[COPY@0xe0fd/b0],R[]— the spill has no direct reader yet, soActionDeadCoderemoves it (deadremoved=1)R[-0x38:4, -0x38:4], and no write in the entry block →renameRecurseinvents an 8-byte inputSubvariableFlow::getReplaceVarnode→replaceInputnarrows that input to 4 bytesI[-0x38:4]≠ range size →guardInputfills the hole (-0x34:4), write-masks both pieces, and concatenates them with aPIECESUBPIECE(PIECE(hi,lo),0)folds away; thePIECEdies, the two write-masked input pieces stay in the def tree and are invisible tocollectVariableStack, askssetInputVarnodefor a full-size input → collides with the leftover pieces →LowlevelErrorThe fix
The leftover pieces are the value the read wants, so the collision is
reconcilable. New module
decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_inputtile.rs:set_input_varnodeexactly as before (the fast path, unchanged);any of them straddles the request boundary, or is not write-masked, give up
and let the upstream error fire;
input for any hole, exactly as
guardInputdoes — and fold it into onefull-size input with the upstream
Funcdata::combineInputVarnodesprimitive(
funcdata_varnode.cc:383), which destroys the pieces, rewrites concatenatingPIECEs intoCOPYs, and repoints every other reader at aSUBPIECEof thenew whole.
Restricting the fold to write-masked pieces is what makes the rewrite safe
mid-rename: a write-masked Varnode is never pushed onto a
VariableStack(renaming pushes only
activeHeritageop outputs and the inputs it createsitself, neither of which is write-masked) and each
Heritage::renamebuilds afresh stack, so the rewrite cannot strand a renaming-stack entry.
heritage.rschanges only three call sites (the two empty-stack paths and theINDIRECT same-time path) to route through the new helper.
No option. These functions produced no output at all, and the new code runs
only where
setInputVarnodewas about to raise. PerCLAUDE.mda strict bug fixthat only corrects wrong output needs no flag. It is recorded as DIV-50 in
docs/history.mdbecause it diverges from upstream.Judged output
gziprpl_fcntl(--mode reliable), after:Matching the gnulib source (
lib/fcntl.c): theaction == 0/action == F_DUPFD_CLOEXEC (1030)arms, theEINVAL (22)retry probe, thehave_dupfd_cloexeccache (dat_dfeb0), thefcntl(result,F_GETFD)/fcntl(result,F_SETFD,flags|1)fix-up and itserrno-preservingclose, andthe bitmask-dispatched default
switch. IDA's stored pane for the same addresshas the same control flow.
Residual, reported honestly: the recovered body still reads the spill slot as
an undefined
stack - 0x38local (return fcntl(a0,a1,v6)), where IDA printsfcntl(a1, a2, v13)withv13 = a3. Keeping the va_list spill alive across thedead-code round is the other half of the story — upstream does it with
Heritage::guardLoads+handleNewLoadCopies(addrForceon boundary Varnodesinside a LOAD-guard range), which kuna does not port (
Heritage::guardhard-wires
high_ptr_possible = falseand both guards areunimplemented_stubs).That is a much larger, separately-measurable port and is deliberately out of
scope here; Ghidra cannot even be used as a reference for it, because it fails on
this function too.
Before/after over the O2 corpus
Same 267 stripped
-O2binaries,decompile-all --json --mode reliable,76,527 functions:
Overlapping input varnodesfunctionsThe 8 residual errors are an unrelated
Unable to load 512 bytes at r0x…class,byte-identical before and after.
Blast radius, whole-binary text diff of every emitted function:
O2/gzip/gzipsub_e0f0)O2/coreutils/sortsub_13850)O2/diffutils/diffsub_1b520)O2/findutils/findsub_17dd0)O2/coreutils/nohupsub_3160)Exactly the function that used to fail, in each case. Nothing else moves.
Speed
Interleaved medians of 5,
decompile-all --mode reliable:O2/gzip/gzipO2/coreutils/sortO2/findutils/findWithin the +5% budget (the new code runs only on the error path).
Regression test
tests/stages/ghdec-inputtile-overlap.xml— 1 of 8 assertions passes pre-fix(the bare-shell signature line), 8/8 post-fix.
The vendored gzip bytes do not reproduce outside the ELF loader path (the
bytechunk console decompiles them fine even pre-fix), so the testcase carries a
reduced standalone
gcc -O2 -fstack-protector-strongbuild of the gnulibwrapper instead — verified to raise the identical error on the pre-fix build via
both the real-binary path (
kuna decompile <bin> rpl_fcntl) and the stagesharness. The reduced C source is quoted in the testcase comment.
Gates
make testmake test-stagesmake rust-testdocs/options.mdfresh)make check-speckuna catalog --checkDocs
docs/spec/03-ssa-and-simplification.md§3.1 — new prose paragraph"Materializing an input over existing pieces".
docs/history.md— DIV-50 row with the corpus measurements above.decompiler/crates/kuna-base/src/xml.rs— stages corpus file count 181 → 182.Provenance:
inspiration = "decbench:cases-missing pool; gnulib rpl_fcntl at -O2, 48 of 213 optimized x86-64 binaries".🤖 Generated with Claude Code
https://claude.ai/code/session_011RXVU58TKJxb518oViChwo