Skip to content

[AUTOMATED] Keep calls after a declared __stdcall import from losing their arguments - #1663

Merged
mahaloz merged 91 commits into
mainfrom
fix/declared-pop-equation-form
Nov 23, 2026
Merged

mahaloz merged 91 commits into
mainfrom
fix/declared-pop-equation-form

Conversation

@mahaloz

@mahaloz mahaloz commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

On x86-32 Windows, a call made after a declared __stdcall import can lose its stack arguments. This happens in functions where the stack solver still has to guess other calls' pops. setuptools 59.6.0 ships setuptools/cli-32.exe (sha256 75f12ea2…c346):

$ kuna decompile cli-32.exe 0x4014e0 | grep -E 'sub_40c991|sub_4018e1|sub_40c175'
  v4 = sub_40c991();
    sub_4017c5(sub_4018e1() + 0x40);
  v5 = sub_40c175(v4,v11);
  sub_4017c5(sub_4018e1(v6) + 0x40);

The function calls GetModuleFileNameA(NULL, path, 0x100), measures path, then does push 0; push edx; call sub_40c991. With this change:

  v5 = sub_40c991(Filename,0);
    sub_4017c5(sub_4018e1("Cannot open %s\n",Filename) + 0x40);
  v6 = sub_40c175(v5,v13,0x100);
  sub_4017c5(sub_4018e1(v7,v5) + 0x40);

Fix

  • The import's declared pop was written as INT_ADD(sp, #pop) before the stack solve. That joined the stack pointer after the call to the one before it, and the solved pops came out the same either way. But the simplifier then rewrote end - (path + 1) in terms of the stack pointer at the import's last push. The local-alias boundary took that slot as an escaped address and refused every later outgoing argument. The next call's push-run guess also read the import's popped slot as its own push (push edi; call [VariantClear]; push esi; call [lstrlenA] gave lstrlenA no argument).
  • A call through a pointer whose declaration states the pop (kuna_declaredpop) now takes an INDIRECT like the other calls and keeps the pop on its prototype. StackSolver::build reads it as an exact equation, and the solve writes it into the IR. If the solve cannot place the stack pointer (a runtime-sized frame, or a stack pointer an SEH handler loads), the pop is written as the INT_ADD afterwards.
  • The push-run guess's cleanup and fold checks read past a later declared call by its pop, as they did when the INT_ADD had already folded it in. Direct calls (DWARF or demangled declarations) and functions with nothing to guess keep the INT_ADD.

Tests

  • tests/stages/kuna-declaredpop.xml decompiles a new nasm fixture, declaredpop_pe_i386.exe (recipe in the fixtures README). It pins sub_401080(Filename,0), xlen(a1) and xfill(v1,&Stackfffffff0). Without the change these print sub_401080() and xfill(xlen(),&Stackffffffec).
  • On 258 i386 PEs, 15 call sites regain arguments and none lose any. The import calls the declarations trimmed to their declared arity keep that arity. Off-target binaries (x86-64, ARM, AArch64, MIPS, PPC, SPARC, i386 ELF) and the repo's PE fixtures produce byte-identical output.

🤖 Generated with Claude Code

https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h

mahaloz and others added 11 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 (#7-#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
@mahaloz

mahaloz commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

[AUTOMATED] Pushed 767f3ce (+ a merge of main and a stage-baseline commit).

With the declared pop deferred, nine functions on the 258-PE i386 set printed worse than main, e.g. Spongebob.exe sub_404b60 (cookie check v2 = 0x404bf5), 736cb272 sub_4090e0 (wrong cookie address) and 064e1d62 sub_4737b5/sub_472fd8/sub_46dae5 (SetFileTime/FormatMessageW guessed short). Main was right there only because its push-run walk took whatever stack reference sat at a slot's offset (a popped argument slot or a return-address slot of the declared call). The commit adds three readings to that walk, each only inside the frame a call with a stated pop left:

  • a join of stack pointers whose inputs agree once read through stated pops is that reference (call [GetLastError]; push eax / push edx before FormatMessageW);
  • a pushed EAX/ECX/EDX entry value there is an argument, not a saved register (mov edi,edx; call [CreateFileW]; ... push edi);
  • a mov [esp],x local at that stack pointer, written before the pushes below it, ends the run (call [GetModuleFileNameA]; mov [esp],0x44; push x10).

All nine print main's text again; on the same set call sites stay +15/-0, and 24 functions now guess the callee's real pop. The fixture gains frame_local, keep_edx and join_push, pinned by asserts #7-#11.

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 #12 and #13
fail without this change.

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

mahaloz commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

[AUTOMATED] Pushed 15b916e: two of the push-run readings for a declared call's frame misread GCC/MinGW code.

  • The mov [esp],x local test also fired on a push that lands on the stack pointer the declared call left after a raise (GCC -O2 push-args: call [Sleep]; add esp,12; push; push; push; push; call [f]), so f was guessed to pop 12 bytes too few and a stack argument read after it became a saved register (return v1 for return a1). By the solve the simplifier has removed that push's stack-pointer write, so the store's instruction bytes now decide push vs. mov.
  • The entry-value test counted ECX/EDX entry values written with MinGW's fixed-frame mov [esp],reg as arguments, so a __cdecl callee was guessed to pop them: wine's atl.dll _string_register (mov esi,edx … call [HeapAlloc]; sub esp,0xc; mov [esp],esi; call eax) lost an argument and shuffled its buffer fields. Only pushed entry values count now.

The fixture gains land_push (0x4011a0) and acc_entry (0x4011d0); stage asserts #12/#13 fail on 451d238 and pass here. Output on the 258 i386 PEs is byte-identical to 451d238.

@mahaloz
mahaloz marked this pull request as draft November 18, 2026 22:24
morluto and others added 6 commits November 18, 2026 18:58
[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
@mahaloz

mahaloz commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

[AUTOMATED] Pushed 941dd6f (narrowing commit d6269db plus merges of main). On 1,274 i386 PEs outside the original corpus the deferral cost about 52 real arguments, from two mechanisms: the push-run guess of an import reads different slots in the two forms (each reading right where the other is wrong), and wine's DWARF-declared direct calls carry false stated pops whose error the solve places by an order the form changes. The deferral now happens only where neither applies: every pop the function guesses is a direct call into its own code, and every stated direct-call pop matches the callee's RET. The push-run readings added for the declared call's frame are unreachable there and are removed. Against main: 258 PEs +10/−0 call sites (cli-32/gui), 1,274 other PEs no call site changes (one function moves an assignment past an early return), 200 PEs built from C no guessed pop changes (957 scored), off-target byte-identical. lstrlenA ×3 and StrStrA ×2 are no longer recovered. The description above predates this narrowing.

sjkim1127 and others added 8 commits November 21, 2026 08:11
…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 26 commits November 23, 2026 00:41
@mahaloz
mahaloz marked this pull request as ready for review November 23, 2026 08:21
@mahaloz
mahaloz merged commit 4eacdc2 into main Nov 23, 2026
6 checks passed
@mahaloz
mahaloz deleted the fix/declared-pop-equation-form branch November 23, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants