Skip to content

[AUTOMATED] Keep the sign-flag polarity when folding SBORROW compares - #1833

Merged
mahaloz merged 93 commits into
Noelo-Lab:mainfrom
sjkim1127:fix/sborrowge-polarity
Nov 23, 2026
Merged

mahaloz merged 93 commits into
Noelo-Lab:mainfrom
sjkim1127:fix/sborrowge-polarity

Conversation

@sjkim1127

Copy link
Copy Markdown
Contributor

kuna inverts a PIC-24 signed loop test. The loop runs while W2 < 7, so seven times, but kuna prints a condition that is false on entry:

echo 02002000730020000306510004003d00000020000004880061014100faff370000000600 | xxd -r -p > gh8777.bin
kuna decompile gh8777.bin 0x0 --addr --raw-image --target PIC-24F:LE:24:default --base 0
  for (v1 = 0; 7 <= v1; v1 = v1 + 1) {
    dat_80 = 0;
  }

The machine code is W2=0; loop: W12 = W2 - 7; bra ge exit; [0x80] = 0; W2++; bra loop.

Fix

  • RuleSborrowGe folds XNOR(F, SBORROW(V,K)) to one signed compare, where F is a sign test of V-K. It treated every sign test as N. 0 s<= (V-K) and -1 s< (V-K) are !N, so their tree is N != V, which means V < K.
  • The rule now records which test it matched. (V-K) s< 0 still folds to K <= V. The two non-negative spellings fold to V < K. Both the AND-of-ORs and OR-of-ANDs forms take the same path.
  • This is a correctness fix, so there is no option.

Tests

  • tests/stages/gh8777-flagcompare-sborrow.xml used to pin the wrong 7 <= v1. It now pins for (v1 = 0; v1 < 7; v1 = v1 + 1), and its header explains the flag semantics.
  • New unit tests cover all three sign-test spellings in both De Morgan forms.
  • No datatest expectation moved (675/675).

🤖 Generated with Claude Code

mahaloz and others added 30 commits November 18, 2026 17:28
…tion, not an early INT_ADD

[AUTOMATED] An x86-32 `call [slot]` that kuna_slotargs declares states its
__stdcall pop. ActionExtraPopSetup wrote that pop into the IR as
`INT_ADD(sp_call, #pop)` before the stack solve. In a function whose other
pops the solver guesses, that joined the stack pointer after the call to the
one before it, and two passes read the joined form wrongly: the next call's
push-run guess walked the slots the declared call had popped as its own
pushes (VariantClear's argument credited to lstrlenA), and term collection
rewrote a pointer difference after GetModuleFileNameA as minus the stack
pointer at its last push, which the local-alias boundary took as an escaped
address, refusing every later outgoing argument.

Such a pop now takes the INDIRECT the other calls take while the prototype
keeps it, so StackSolver::build gives the call its exact equation and the
solve writes it into the IR. A pop the solve cannot place is written as the
INT_ADD afterwards. The cleanup and fold checks of the push-run guess read
past a later call by its stated pop. Direct calls and functions with nothing
to guess keep the INT_ADD.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
…tion-form

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
…tion-form

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
…tion-form

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
…rough its popped slots

With a declared pop as an exact INT_ADD, the push-run guess of the next call
read whichever stack reference sat at a slot's offset, often the declared
call's own popped argument slots. With the pop deferred to the solver, three
readings take the place of that coincidence, all inside the frame a call with
a stated pop left:
- a join of stack pointers whose inputs read back (through constant
  adjustments and stated pops) to one place is that place, and a joined slot
  stored on every path with one argument push is a push (FormatMessageW after
  `call [GetLastError]; push eax` / `push edx`); the folded test reads through
  such joins too (two `__cdecl` frees cleaned by one `pop ecx; pop ecx`);
- a pushed entry value of EAX/ECX/EDX in that frame is an argument, not a
  saved register (`mov edi,edx; call [CreateFileW]; ... push edi`);
- the slot at the stack pointer the declared call left, written before
  anything is pushed below it, is a local and ends the run
  (`call [GetModuleFileNameA]; mov [esp],0x44; push` x10).
The fixture gains frame_local, keep_edx and join_push; the stage test checks
each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
Records the five declaredpop stage assertions added for frame_local,
keep_edx and join_push (Noelo-Lab#7-Noelo-Lab#11) in docs/baseline-stages.json; every other
entry is the merge of main's and this branch's baselines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
Two of the push-run readings for the frame a declared call leaves misread
GCC/MinGW code, found on held-out builds:
- The `mov [esp],x` local test took a push for a mov when the push landed
  on the stack pointer the declared call left after a raise (GCC's
  `call [Sleep]; add esp,12; push; push; push; push; call [f]` around a
  padded call): the run stopped at the third push and f was guessed to pop
  12 bytes too few. The simplifier has already removed such a push's
  stack-pointer write, so the store's instruction bytes say whether it is a
  push; only a mov ends the run.
- The entry-value test counted ECX's entry value written with MinGW's fixed
  frame `mov [esp],ebx` as an argument push, so a __cdecl callee was
  guessed to pop it and the function gained a phantom parameter. Only a
  pushed entry value counts now.
The fixture gains land_push and acc_entry; stage asserts Noelo-Lab#12 and Noelo-Lab#13
fail without this change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
[AUTOMATED] Preserve the exact decoded call site and executable-section exclusion without adding a discovered procedure.
…ead the form

A declared import pop goes to the stack solver as an equation only in a
function whose other guessed pops are all direct calls into the image's own
code, and whose direct calls' stated pops are the ones their callees' RETs
prove. Elsewhere it stays the INT_ADD it was before.

The push-run guess of an import or of a call through a pointer reads the
slots in front of the call. With the INT_ADD folded in it read whichever
reference sat at a slot's offset; with the INDIRECT it reads the call's own
slots. On code nobody tuned this on, each reading is right where the other is
wrong (GCC alignment pushes, a __cdecl import's cleanup merged into the next
call's reservation, arguments stored in the block before the call, a pushed
copy of EDX's entry value). A direct call's stated pop under the unknown-pop
model is computed as if the callee popped its declared arguments, which a GCC
__cdecl or regparm callee does not; that false equation leaves a merge whose
paths disagree, and the form changes which path the solve takes. Neither is a
change of form only, so both keep the INT_ADD.

With no guessed import left in a deferring function, the push-run readings
added for the declared call's frame have nothing to read and are removed
(kuna_calleepop.rs and kuna_calleepopslot.rs are main's again). The fixed-frame
guess measures a call's slots from the stack pointer a stated call left when
nothing else is in between, as the INT_ADD form measured them from the one
before that call.

The fixture keeps only the launcher shape; the stage test keeps its three
assertions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
RuleSborrowGe folds XNOR(F, SBORROW(V,K)) to a signed compare, but it
read every sign test of V-K as N. `0 s<= (V-K)` and `-1 s< (V-K)` are
!N, so XNOR(!N, V) is N != V, i.e. `V < K`, not `K <= V`. On the PIC-24
GH-8777 loop this turned a seven-iteration loop into
`for (v1 = 0; 7 <= v1; ...)`, which never runs.

The rule now tracks which spelling it matched: `(V-K) s< 0` still folds
to INT_SLESSEQUAL(K, V); the non-negative spellings fold to
INT_SLESS(V, K). The stage test pins `v1 < 7`.

[AUTOMATED] Strict correctness fix, no option.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…traps

The datatest harness built every test file's architecture from scratch:
it re-decoded the same .sla and re-parsed every .ldefs for each file,
which was ~60% of `make test` / `make test-stages` CPU.

engine::reuse_specs() (opt-in, called by decomp_test_dbg) makes a process
scan each set of on-disk spec roots once, and keep a decoded template of
each .sla a thread decodes twice (kuna_slacache, keyed by file contents).
Later translators are SleighBase::clone_decoded copies: a fresh space set
rebuilt from the .sla space records, every space reference remapped by
index, xrefs and context variables rebuilt as a decode does. Single-binary
CLI runs never enable it and take the unchanged decode path.

Harness output is byte-identical on both corpora. decomp_test_dbg, A/B:
datatests 3.3 s -> 1.25 s CPU, stages ~50-58 s -> ~23-28 s CPU; peak RSS
96 -> 232 MB and ~400 -> ~620 MB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mahaloz and others added 29 commits November 23, 2026 00:41
@mahaloz
mahaloz merged commit a17a36e into Noelo-Lab:main Nov 23, 2026
2 checks passed
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.

4 participants