Skip to content

[AUTOMATED] fix(p6): close GH-180 - overflow guards read the pre-add operands (verified fixed by DIV-47; regression pinned) - #304

Merged
mahaloz merged 1 commit into
mainfrom
fix/i180-implied-cover-dirty
Aug 16, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/i180-implied-cover-dirty

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #180

Summary

GH-180 reported that kuna printed an overflow guard after the add it guards, on the
same variable — v3 = v3 + v1; if (SCARRY8(v3,v1)) — a predicate the binary does not
compute (the hardware OF at grep's 0x7ce0 comes from the pre-add operands). The IR
was correct; the pre-add Varnode (the SCARRY input) and the ADD output were illegally
merged into one HighVariable.

Investigating on current main: the code fix already landed in #228 (DIV-47, merged
2026-08-01), which restored exactly the two dropped halves this issue diagnosed —
ActionMarkImplied now goes through Merge::mark_implied (upstream merge.cc:1595,
operand cover-dirtying), and the MergeContext::vn_set_cover_dirty bridge forwards a
Varnode coverdirty to the owning HighVariable the way upstream Varnode::setFlags
does (varnode.cc:377-378). #228 was driven by two different decbench witnesses
(ocsptool, ptx) and never referenced this issue, so GH-180 stayed open with its
witnesses unverified and unpinned.

This PR completes GH-180 end-to-end: it re-verifies every witness and the issue's whole
scope table against current main, audits the shipped fix against the pinned upstream
rev, and adds the missing stage regression testcase pinning the corrected contract. No
engine code changes; no option and no DIV row (the shipped fix is a strict correctness
fix restoring upstream behavior, recorded as DIV-47 by #228).

Root cause (recap, re-verified against the pinned GHIDRA_REV)

Cover::rebuild (upstream cover.cc:477) walks forward through implied consumers,
so an operand of an inlined expression stays live until the point where that expression
is printed — here the implied SCARRY8 prints at the CBRANCH, so the pre-add value is
live across the ADD output's definition and the merge is illegal. That protection only
fires if implied-marking invalidates the operands' Covers: upstream
Merge::markImplied marks every operand of the defining op coverdirty, and
Varnode::setFlags forwards that to high->coverDirty(). The kuna port had
Merge::mark_implied sitting with zero callers and no high-forward, so
HighIntersectTest::update_high short-circuited on covers computed before
implied-marking, and ActionMergeAdjacent/ActionMergeType saw boundary-only
intersection where a real conflict exists. #228 wired both halves; I verified each
against coreaction.cc:3524, merge.cc:1595-1605, and varnode.cc:371-380 at the
pinned rev (upstream has exactly one markImplied call site, and so does kuna).

Design note on the second half: upstream fires the forward inside Varnode::setFlags;
kuna's Varnode::set_flags is a W7 stub because a Varnode cannot reach
Funcdata::high_bank. The #228 bridge forward at vn_set_cover_dirty is the faithful
equivalent: Merge::markImplied is the only post-heritage site that sets coverdirty
on another Varnode, and vn_set_cover_dirty is its only entry point in kuna, so the
forward is observationally identical to completing the stub — without inventing a new
Varnode-to-HighBank back-reference the W7 architecture deliberately avoids.

Load-bearing check (why the new test pins the outcome, not the seam)

Reverting the ActionMarkImplied wiring on current HEAD (back to bare set_implied()):

The new stage testcase therefore pins the user-visible contract (guards read the
pre-add operands; the collapsed self-update-then-test rendering must not appear), which
catches a regression of this bug through any future seam.

Witnesses (before = issue, after = current main + this branch)

grep nlscan @ 0x7c90 (in-repo tests/bug-repro/grep):

// before (GH-180)                          // after
v3 = v3 + v1;                               if (!SCARRY8(v3,v1)) {
if (SCARRY8(v3,v1)) {                         dat_2b7c0 = a0;
                                              dat_2b818 = v3 + v1;

grep @ 0x1f0d0: v2 = v3 + v1; if (!CARRY8(v3,v1)) — pre-add. grep @ 0x1f160:
v3 = v4 + v2; if (!CARRY8(v4,v2)) — pre-add. gzip @ 0xf320 (decbench):
v2 = v3 + v1; if (!CARRY8(v3,v1)) — pre-add. All match Ghidra's rendering.

Scope scan (the issue's regex, X = X ± Y; then SCARRY|SBORROW|CARRY(X,Y) within 3 lines)

Over kuna decompile-all --json on current code:

binary functions affected
tests/bug-repro/grep 449 0 (was 3)
tests/bug-repro/sort 490 0
tests/bug-repro/faillog 69 0
tests/bug-repro/libselinux.so.1 621 0
decbench O2/gzip (stripped) 204 0 (was 4)
decbench O2/diffutils/diff (stripped) 398 0 (was 2)
decbench O2/tar (stripped) 1125 0 (was 1)
decbench O2/bzip2 (stripped) 114 0

gzip/diff/tar are not vendored in tests/bug-repro/; they were verified from the local
decbench checkout (results/full_run/O2/<pkg>/stripped/), byte-for-byte the binaries
the issue measured.

Regression test

tests/stages/gh180-implied-cover-scarry.xml (single pass — no option exists to flip a
strict correctness fix): the verbatim nlscan witness bytes (0x7c90..0x7d05, padding
replaced by RET; memchr and the no-return stub as single-RET stand-ins at the real
call targets), plus a minimal synthetic of the unsigned add/jb xalloc-growth form.
Eight asserts: both guards keep their PRE-add operands with the sum a separate later
expression (3-line order-pinning matches), exactly one SCARRY8/standalone CARRY8
each, and four negative pins that the collapsed v += w; if (CARRY(v,w)) rendering
(compound and plain spelling) does not appear.

Bookkeeping: corpus file count 204 -> 205 in kuna-base/src/xml.rs,
docs/baseline-stages.json re-recorded (460 -> 468 keys, only the 8 new GH180 keys
added), row added to tests/stages/README.md. The spec prose for the implied-marking
cover-dirty semantics already landed with #228 in docs/spec/06-variables-and-merge.md
(the implied-consumer forward walk and the coverdirty forward), so no spec change is
needed; make check-spec is green.

Collateral audit

decompile-all --json on grep/sort/faillog/libselinux.so.1 with the base (main)
binary vs this branch's binary: decompiled C positionally identical on all four
binaries, zero changed functions
— as expected for a test-only change.

Gates

Gate Result
make test PARITY OK (675/675)
make test-stages PARITY OK (468/468)
make rust-test green
make check-spec green

No docs/baseline.json re-pin; the stages baseline re-record is the sanctioned
add-a-testcase procedure.

Speed

No engine code changes, so the two binaries are built from identical decompiler
sources. Measured anyway (interleaved, decompile-all tests/bug-repro/grep --json,
under concurrent sibling-agent build load): min-of-11 base 13.63s vs branch 14.10s
(+3.4%), order-swapped min-of-7 base 16.05s vs branch 15.51s (-3.4%) — i.e. no
delta, within measurement noise
, as expected for a test-only change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq

[AUTOMATED] GH-180's illegal pre-add/post-add merge (the SCARRY input and
the ADD output printing as one variable, so the emitted guard tested the
post-add value) was fixed by the DIV-47 cover restorations in #228, which
never referenced the issue. This closes the loop: every witness and the
issue's whole scope table re-verified at zero on current main (including
the non-vendored decbench gzip/diff/tar binaries), the shipped fix audited
against the pinned upstream rev, and the corrected contract pinned by a
stage testcase: grep nlscan's verbatim bytes plus the minimal unsigned
add/jb growth-guard form; the SCARRY8/CARRY8 guards must keep their
PRE-add operands and the collapsed self-update-then-test rendering must
not appear.

Bookkeeping: corpus file count 204 -> 205 (kuna-base xml.rs), stages
baseline re-recorded 460 -> 468 keys, README row added. No engine code
change, no option, no DIV row.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq
@mahaloz
mahaloz merged commit ee7363f into main Aug 16, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/i180-implied-cover-dirty branch August 16, 2026 19:51
mahaloz added a commit that referenced this pull request Aug 16, 2026
…T effect (GH-181)

[AUTOMATED] Two stacked correctness fixes, no new option:

1. Port the upstream Merge::snipReads carve-out (merge.cc:461, "snip must
   come after OP CAUSING EFFECT") that kuna's do_snip_reads_insert_point
   dropped: a cover-trim COPY of an INDIRECT-defined Varnode is inserted
   after the op iop-encoded in the INDIRECT's input 1 (the call), not at
   the INDIRECT marker, which precedes the call in block order.  Without
   it, libselinux lsetfilecon_raw's out-parameter copy printed above the
   lgetfilecon_raw call that fills the slot, so freecon() consumed the
   pre-call NULL.  Audited the other upstream sites: trimOpOutput already
   had the arm; eliminateIntersect/snipOutputInterference already used
   indirect_effect_op; mergeIndirect/trimOpInput match upstream.

2. Close the matching reordering hazard in kuna's own foldcallret
   (default-on, DIV-14): an op strictly between a call and its lone use
   that reads a value the call indirectly writes (an input defined by a
   CPUI_INDIRECT whose iop input names the call) is now a fold barrier,
   so the call expression is not sunk past the very copy fix 1 places
   after it.  Marker ops are skipped (a later call's INDIRECTs chain the
   earlier call's versions without a textual evaluation point).

New two-pass stage test gh181-snipreads-indirect.xml (default proves both
fixes; option foldcallret off isolates fix 1) plus a README row; corpus
count recounted to 206 on the rebased tree; baseline-stages re-recorded
(473/473 on post-#306 main, including #304's eight GH-180 assertions;
this branch's stage patterns are number-agnostic, so #306's canary
renumbering required no re-pinning).  Three stage tests pinned
renderings the fixes legitimately changed and were updated with comments:
ghangr-x8664-cvs-863633 (slot 0x58 now merge-coalesced even with
dedupvardecls off), ghangr-morton-my-message-callback (renumbering; the
bogus self-assign artifact is gone), ghdec-symbol-keyed-local-decls (the
checksum call is now sequenced before the CONCAT13 reading the slot it
may write).  Spec prose added to docs/spec/06-variables-and-merge.md.

Gates: make test PARITY OK (675/675), make test-stages PARITY OK,
make rust-test green, make check-spec green.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq
mahaloz added a commit that referenced this pull request Aug 16, 2026
…T effect (GH-181) (#307)

[AUTOMATED] Two stacked correctness fixes, no new option:

1. Port the upstream Merge::snipReads carve-out (merge.cc:461, "snip must
   come after OP CAUSING EFFECT") that kuna's do_snip_reads_insert_point
   dropped: a cover-trim COPY of an INDIRECT-defined Varnode is inserted
   after the op iop-encoded in the INDIRECT's input 1 (the call), not at
   the INDIRECT marker, which precedes the call in block order.  Without
   it, libselinux lsetfilecon_raw's out-parameter copy printed above the
   lgetfilecon_raw call that fills the slot, so freecon() consumed the
   pre-call NULL.  Audited the other upstream sites: trimOpOutput already
   had the arm; eliminateIntersect/snipOutputInterference already used
   indirect_effect_op; mergeIndirect/trimOpInput match upstream.

2. Close the matching reordering hazard in kuna's own foldcallret
   (default-on, DIV-14): an op strictly between a call and its lone use
   that reads a value the call indirectly writes (an input defined by a
   CPUI_INDIRECT whose iop input names the call) is now a fold barrier,
   so the call expression is not sunk past the very copy fix 1 places
   after it.  Marker ops are skipped (a later call's INDIRECTs chain the
   earlier call's versions without a textual evaluation point).

New two-pass stage test gh181-snipreads-indirect.xml (default proves both
fixes; option foldcallret off isolates fix 1) plus a README row; corpus
count recounted to 206 on the rebased tree; baseline-stages re-recorded
(473/473 on post-#306 main, including #304's eight GH-180 assertions;
this branch's stage patterns are number-agnostic, so #306's canary
renumbering required no re-pinning).  Three stage tests pinned
renderings the fixes legitimately changed and were updated with comments:
ghangr-x8664-cvs-863633 (slot 0x58 now merge-coalesced even with
dedupvardecls off), ghangr-morton-my-message-callback (renumbering; the
bogus self-assign artifact is gone), ghdec-symbol-keyed-local-decls (the
checksum call is now sequenced before the CONCAT13 reading the slot it
may write).  Spec prose added to docs/spec/06-variables-and-merge.md.

Gates: make test PARITY OK (675/675), make test-stages PARITY OK,
make rust-test green, make check-spec green.


Claude-Session: https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overflow guards read the post-add value: v3 = v3 + v1; if (SCARRY8(v3,v1))

1 participant