From 325b242fabd57b3e4f9b62fc6254d1ad12504f5d Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sun, 15 Nov 2026 11:56:14 +0100 Subject: [PATCH 1/3] [AUTOMATED] Expand a by-value block for a callee whose stack effect the walk cannot settle repframecopy's argument_block takes the call's calleeargs window (what the callee's reached returns pop, or the caller removes after the call) as the pop releasing the block when neither a stated extrapop nor the purge walk gives one. A __thiscall method ending in ret 0x20 that calls a helper the walk cannot read kept the copy as a loop and printed with no arguments. Co-Authored-By: Claude Opus 5.5 --- .../kuna-analysis/tests/fixtures/README.md | 11 +++ .../tests/fixtures/byvalreuse_pe_i386.exe | Bin 0 -> 2560 bytes .../tests/fixtures/byvalreuse_pe_i386.s | 84 ++++++++++++++++++ .../src/p3_dataflow/kuna_repframecopy.rs | 7 +- docs/spec/03-ssa-and-simplification.md | 9 +- tests/stages/kuna-repframecopy.xml | 13 +++ 6 files changed, 121 insertions(+), 3 deletions(-) create mode 100755 decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.exe create mode 100644 decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.s diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/README.md b/decompiler/crates/kuna-analysis/tests/fixtures/README.md index 45400cbb52..effbf075f2 100644 --- a/decompiler/crates/kuna-analysis/tests/fixtures/README.md +++ b/decompiler/crates/kuna-analysis/tests/fixtures/README.md @@ -2458,3 +2458,14 @@ llvm-objcopy --strip-all hookfix swift_call_hook_elf whose `movq (%rdi),%rax; movq %rdi,%r13; callq *0x40(%rax)` dispatches `area()`. With `swiftmetadata on` the first call keeps the C model and the second takes `__swiftcall`. + +### `byvalreuse_pe_i386.exe` — a by-value block reusing an earlier call's pushes + +`byvalreuse_pe_i386.exe` (2,560 bytes, PE32/i386, SHA-256 +`288102072c3ba68bcbf0d6f6ef9368e1d76e6b541bd4c4214f8f28d10968e76b`) is clean-room: +`byval`@`0x401000` copies a 32-byte struct with `rep movsd` into an outgoing block that +reuses the 12 bytes an earlier `__cdecl` call's pushes left on the stack (`sub esp,0x14`), +then calls the `__thiscall` method `take`@`0x401040`, which ends in `ret 0x20` and hands +its argument's address to a helper called through a pointer, so the purge walk cannot +settle its stack effect. Source and recipe (clang + lld-link) in `byvalreuse_pe_i386.s`. +Used by `tests/stages/kuna-repframecopy.xml` (#14). diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.exe b/decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.exe new file mode 100755 index 0000000000000000000000000000000000000000..f96835099d51a584f966a9c1c6b0c60603be3907 GIT binary patch literal 2560 zcmeZ`s$gJbU|?VYVr1Ze%)!B~0E+X;@8V)}GVK8ujuz~VyP!Sl-0F;91VP;@x1SqK-10V*O z#{uFXfCB@=0R(T9H5vkl5C{uzexs13@V`XGq}N4-Bj80eP_ElW#iIF*NaypNpp?{G zqN4D5Y3IQgpyb+oL?J%z@EJG&YGMiaFUn%z0F+|sW?*(vQ8=W)0G9_E#e$^#Ojvk# zjS5e9iHbnCkBZ2_mrS3RqMFCb0W>P0mz4oTy@>t)|9|rl4rCMHMvpS65CWi-04syZ sDjz`xjDjH@0to^R;5GoL>HyZA;A)f=NQ0^yb|6NpPJkk#V7P<;09NXVTL1t6 literal 0 HcmV?d00001 diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.s b/decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.s new file mode 100644 index 0000000000..4a0c5ec964 --- /dev/null +++ b/decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.s @@ -0,0 +1,84 @@ +# kuna fixture: a 32-byte struct passed by value to a __thiscall method, copied +# with `rep movsd` into an outgoing block that reuses the 12 bytes an earlier +# __cdecl call's pushes left on the stack (MSVC defers that cleanup), as VC8 +# /O2 builds it. +# byval (0x401000): Rec t; copy(&t, r, 32); return o->take(t); +# take (0x401040): __thiscall, ret 0x20; hands its argument's address to +# a helper called through a pointer, so the purge walk +# cannot settle its stack effect. +# +# Build: +# clang --target=i686-pc-windows-msvc -c byvalreuse_pe_i386.s -o b.obj +# lld-link /nologo /entry:start /subsystem:console /nodefaultlib /base:0x400000 \ +# /fixed /safeseh:no /opt:noref /Brepro /out:byvalreuse_pe_i386.exe b.obj + .intel_syntax noprefix + .text + .globl _byval +_byval: # int byval(Obj *o, const Rec *r) + push esi + push edi + sub esp, 0x20 + push 0x20 + push dword ptr [esp + 0x34] + lea eax, [esp + 8] + push eax + call _copy + mov eax, dword ptr [esp + 0x38] + sub esp, 0x14 + mov edi, esp + mov ecx, 8 + lea esi, [esp + 0x20] + rep movsd + mov ecx, eax + call _take + add esp, 0x20 + pop edi + pop esi + ret + + .p2align 4, 0xcc +_take: # int Obj::take(Rec t) { return *sink(&t) + t.x[7]; } + lea eax, [esp + 4] + push eax + call dword ptr [_sink_ptr] + add esp, 4 + mov eax, dword ptr [eax] + add eax, dword ptr [esp + 0x20] + ret 0x20 + + .p2align 4, 0xcc +_sink: + mov eax, dword ptr [esp + 4] + ret + + .p2align 4, 0xcc +_copy: + push esi + push edi + mov edi, dword ptr [esp + 0xc] + mov esi, dword ptr [esp + 0x10] + mov ecx, dword ptr [esp + 0x14] + shr ecx, 2 + rep movsd + pop edi + pop esi + ret + + .p2align 4, 0xcc + .globl _start +_start: + lea eax, [_rec] + push eax + lea eax, [_obj] + push eax + call _byval + add esp, 8 + ret + + .data +_obj: + .long 0 +_sink_ptr: + .long _sink +_rec: + .long 1, 2, 3, 4, 5, 6, 7, 8 diff --git a/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs b/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs index 92cc09daaa..7a8a0af893 100644 --- a/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs +++ b/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs @@ -19,7 +19,10 @@ //! [`MAX_BYTES`] bytes upward, its destination is the stack pointer itself, and //! the block is the outgoing arguments of the first call after it //! ([`argument_block`]): nothing else takes the block's address, and that -//! callee reads into it or its pop releases it. A record copied into a +//! callee reads into it or its pop releases it. A callee whose stack effect the +//! walk cannot settle still releases it when its reached returns pop the block +//! (`ret 0x20`) or the caller removes it right after the call (the call's +//! `calleeargs` window). A record copied into a //! bottom-of-frame local before a register-argument or virtual call (Delphi's //! `lea edi,[esp]; rep movsd`) stays a loop; expanded, its slots would read as //! that call's stack arguments. When such a move reads the frame, the moves @@ -391,7 +394,7 @@ fn argument_block(fd: &Funcdata, sp: VarnodeId, rep: &RepMove, moves: &[RepMove] } let stated = fc.get_extra_pop(); let pop = if stated != crate::fspec::EXTRAPOP_UNKNOWN { Some(stated) } else { fc.callee_purge() }; - let Some(pop) = pop.map(|p| i64::from(p) - 4) else { return false }; + let Some(pop) = pop.map(|p| i64::from(p) - 4).or_else(|| fc.callee_stack_window().map(i64::from)) else { return false }; if pop >= pushed + bytes { return true; } diff --git a/docs/spec/03-ssa-and-simplification.md b/docs/spec/03-ssa-and-simplification.md index fd9d3c1f45..8ee0db4cc6 100644 --- a/docs/spec/03-ssa-and-simplification.md +++ b/docs/spec/03-ssa-and-simplification.md @@ -1295,7 +1295,14 @@ reaches nothing but frame loads and stores, the stack pointer and the move's own pointers (`block_escapes`, so a local handed to a call in `ECX` or `EDX` is not one), and that call's direct callee reads into the block as its own code shows (chapter 04's `calleeargs` measure), or the pop its prototype -states or its returns prove releases all of it. Under a model whose pop is +states or its returns prove releases all of it. When the walk cannot settle +the callee's stack effect (a call it cannot read, such as a string helper +handed the argument's address), the call's `calleeargs` window counts as that +pop: what the callee's reached returns pop (`ret 0x20`) or what the caller +removes right after the call. MSVC builds such a block partly from an earlier +`__cdecl` call's pushes whose cleanup it deferred (`push 0x20; push eax; push +ecx; call memcpy; sub esp,0x14; mov edi,esp; rep movsd`), so the 32-byte +struct sits 12 bytes above where the `sub` leaves the stack. Under a model whose pop is stated (gcc's cdecl), a release of the whole block by the caller before the next call or branch counts too, when nothing reads the block between the copy and that release. A record copied into a local at the bottom of the frame diff --git a/tests/stages/kuna-repframecopy.xml b/tests/stages/kuna-repframecopy.xml index c81db9a4c8..63874b9659 100644 --- a/tests/stages/kuna-repframecopy.xml +++ b/tests/stages/kuna-repframecopy.xml @@ -34,6 +34,12 @@ d_ptr (0x401340) and d_direct (0x401370) copy a record into a local at the bottom of the frame: no call takes it, so each move stays a loop and no call gains a stack argument. + byvalreuse_pe_i386.exe (clean-room, source and recipe in + byvalreuse_pe_i386.s): byval (0x401000) copies a 32-byte struct into an + outgoing block that reuses an earlier __cdecl call's 12 bytes of pushes + (`sub esp,0x14`) and calls a __thiscall method ending in `ret 0x20` whose + stack effect the purge walk cannot settle: the method takes the eight + dwords (#14). --> c3 @@ -83,6 +89,12 @@ c3 load addr 0x401370 decompile print C + clear architecture + load file decompiler/crates/kuna-analysis/tests/fixtures/byvalreuse_pe_i386.exe + read symbols + load addr 0x401000 + decompile + print C quit sub_401070\(v[0-9]+ \+ v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+\) @@ -98,4 +110,5 @@ c3 0x58\)\)\([^)] \(code \*\*\)\(\*v[0-9]+ \+ 0x58\) sub_4012e0\(\); +sub_401040\(v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+,v[0-9]+\); From dee62eae92a4796c7d7f41d61e36e985bf029cf8 Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sun, 15 Nov 2026 11:59:50 +0100 Subject: [PATCH 2/3] [AUTOMATED] Record the byvalreuse repframecopy assertion Co-Authored-By: Claude Opus 5.5 --- docs/baseline-stages.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index d2edbac94a..d9b0841378 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4341, + 4341 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -1914,6 +1914,7 @@ "data:REPFRAMECOPY #11: the virtual calls after a bottom-of-frame local take no stack argument", "data:REPFRAMECOPY #12: both virtual calls are kept", "data:REPFRAMECOPY #13: the register-argument call takes no stack argument after either local", + "data:REPFRAMECOPY #14: a block reusing an earlier call's pushes reaches a method whose pop the walk cannot settle", "data:REPFRAMECOPY #1: fill takes the copied struct and b", "data:REPFRAMECOPY #2: paint keeps its three arguments", "data:REPFRAMECOPY #3: no pushed return address is printed as a value", From 2466bd636baff8818451ae4eef1afd08c295e5a4 Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sun, 15 Nov 2026 12:29:58 +0100 Subject: [PATCH 3/3] [AUTOMATED] State the pops an expanded by-value block depends on When repframecopy expands a block on the call's calleeargs window alone, it states that pop on the call, and states that the earlier call whose pushes the block reuses pops only its return address. The solver otherwise guessed that call popped its pushes and placed the block's reads 12 bytes too high, in the caller's parameters. Co-Authored-By: Claude Opus 5.5 --- .../src/p3_dataflow/kuna_repframecopy.rs | 82 ++++++++++++++++++- docs/spec/03-ssa-and-simplification.md | 9 +- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs b/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs index 7a8a0af893..8282bdd891 100644 --- a/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs +++ b/decompiler/crates/kuna-decomp/src/p3_dataflow/kuna_repframecopy.rs @@ -22,7 +22,10 @@ //! callee reads into it or its pop releases it. A callee whose stack effect the //! walk cannot settle still releases it when its reached returns pop the block //! (`ret 0x20`) or the caller removes it right after the call (the call's -//! `calleeargs` window). A record copied into a +//! `calleeargs` window). That pop is then stated on the call, and an earlier +//! call whose pushes the block reuses (MSVC builds it over a deferred +//! `__cdecl` cleanup) is stated to pop only its return address, so the stack +//! pointer solver places the block where the copy put it. A record copied into a //! bottom-of-frame local before a register-argument or virtual call (Delphi's //! `lea edi,[esp]; rep movsd`) stays a loop; expanded, its slots would read as //! that call's stack arguments. When such a move reads the frame, the moves @@ -394,7 +397,8 @@ fn argument_block(fd: &Funcdata, sp: VarnodeId, rep: &RepMove, moves: &[RepMove] } let stated = fc.get_extra_pop(); let pop = if stated != crate::fspec::EXTRAPOP_UNKNOWN { Some(stated) } else { fc.callee_purge() }; - let Some(pop) = pop.map(|p| i64::from(p) - 4).or_else(|| fc.callee_stack_window().map(i64::from)) else { return false }; + let window = pop.is_none().then(|| fc.callee_stack_window()).flatten(); + let Some(pop) = pop.map(|p| i64::from(p) - 4).or_else(|| window.map(i64::from)) else { return false }; if pop >= pushed + bytes { return true; } @@ -419,6 +423,69 @@ fn argument_block(fd: &Funcdata, sp: VarnodeId, rep: &RepMove, moves: &[RepMove] !window.into_iter().any(|id| reads_block(fd, sp, id, start, bytes)) } +/// The pop to state on the call an argument block feeds, when only its +/// `calleeargs` window releases the block: the solver would otherwise guess +/// it, and an earlier call whose pushes the block reuses with it. +fn window_pop(fd: &Funcdata, rep: &RepMove, moves: &[RepMove]) -> Option<(kuna_base::types::int4, i32)> { + let (_, call, _) = next_call(fd, rep, moves)?; + let i = (0..fd.num_calls()).find(|&i| fd.get_call_specs(i).get_op() == call)?; + let fc = fd.get_call_specs(i); + if fc.get_extra_pop() != crate::fspec::EXTRAPOP_UNKNOWN || fc.callee_purge().is_some() { + return None; + } + if fc.callee_stack_arg_bytes().is_some() { + return None; + } + let window = fc.callee_stack_window()?; + (u64::from(window) >= rep.bytes).then(|| (i, window as i32 + 4)) +} + +/// The earlier call whose pushes the argument block of `rep` reuses (MSVC +/// defers a `__cdecl` cleanup and builds the block over it: `call memcpy; +/// sub esp,0x14; rep movsd` for 32 bytes), when its prototype states no pop: +/// it pops only its return address. A purge its returns prove is applied only +/// once every call in the function has one, so it is stated here too, when +/// it agrees. +fn reused_call(fd: &Funcdata, rep: &RepMove) -> Option { + let (base, _) = rep.base.as_ref()?; + let (i, offset) = after_call(fd, *base)?; + let fc = fd.get_call_specs(i); + let unstated = fc.get_extra_pop() == crate::fspec::EXTRAPOP_UNKNOWN && fc.callee_purge().is_none_or(|p| p == 4); + (unstated && offset < 0 && -offset < rep.bytes as i64).then_some(i) +} + +/// The call whose unsettled pop the stack pointer `vn` was last adjusted by, +/// and how far below that call's stack pointer `vn` lies. +fn after_call(fd: &Funcdata, mut vn: VarnodeId) -> Option<(kuna_base::types::int4, i64)> { + let mut offset = 0i64; + for _ in 0..16 { + let op = defining(fd, vn)?; + let constant = |k| op.get_in(k).and_then(|c| fd.vbank().get(c)).filter(|c| c.is_constant()).map(|c| c.get_offset() as u32 as i32 as i64); + match op.code() { + OpCode::CPUI_COPY => vn = op.get_in(0)?, + OpCode::CPUI_INT_ADD => { + offset += constant(1)?; + vn = op.get_in(0)?; + } + OpCode::CPUI_INT_SUB => { + offset -= constant(1)?; + vn = op.get_in(0)?; + } + OpCode::CPUI_INDIRECT => { + let iop = fd.vbank().get(op.get_in(1)?)?; + if iop.get_space().get_type() != kuna_base::space::spacetype::IPTR_IOP { + return None; + } + let call = crate::funcdata_varnode::op_iop_decode(iop.get_offset()); + let i = (0..fd.num_calls()).find(|&i| fd.get_call_specs(i).get_op() == call)?; + return Some((i, offset)); + } + _ => return None, + } + } + None +} + /// Is `id` a LOAD the frame places at a byte of the `bytes` at frame offset /// `start`? The caller reads a local back; it never reads an argument block. fn reads_block(fd: &Funcdata, sp: VarnodeId, id: OpId, start: i64, bytes: i64) -> bool { @@ -775,6 +842,12 @@ pub(crate) fn expand_all(fd: &mut Funcdata) -> i32 { moves.retain(|m| m.to_arguments); } } + let stated: Vec<(kuna_base::types::int4, i32)> = moves + .iter() + .filter(|m| m.to_arguments) + .filter_map(|m| window_pop(fd, m, &moves).map(|pop| (m, pop))) + .flat_map(|(m, pop)| std::iter::once(pop).chain(reused_call(fd, m).map(|i| (i, 4)))) + .collect(); let mut expanded = 0; for rep in &moves { if expand(fd, rep).is_err() { @@ -782,6 +855,11 @@ pub(crate) fn expand_all(fd: &mut Funcdata) -> i32 { } expanded += 1; } + if expanded as usize == moves.len() { + for (i, pop) in stated { + fd.get_call_specs_mut(i).proto_mut().set_extra_pop(pop); + } + } if expanded > 0 { fd.structure_reset(); } diff --git a/docs/spec/03-ssa-and-simplification.md b/docs/spec/03-ssa-and-simplification.md index 8ee0db4cc6..9ebefb887d 100644 --- a/docs/spec/03-ssa-and-simplification.md +++ b/docs/spec/03-ssa-and-simplification.md @@ -1302,7 +1302,14 @@ pop: what the callee's reached returns pop (`ret 0x20`) or what the caller removes right after the call. MSVC builds such a block partly from an earlier `__cdecl` call's pushes whose cleanup it deferred (`push 0x20; push eax; push ecx; call memcpy; sub esp,0x14; mov edi,esp; rep movsd`), so the 32-byte -struct sits 12 bytes above where the `sub` leaves the stack. Under a model whose pop is +struct sits 12 bytes above where the `sub` leaves the stack. An expansion that +relies on the window states that pop on the call, and states that the earlier +call whose pushes the block reuses (found by walking the copy's stack pointer +back to that call's INDIRECT) pops only its return address, when its +prototype states no pop and any purge its returns prove agrees. The solver +applies proven purges only when every call in the function has one, and +otherwise guesses that such a call pops its pushes, which places the whole +block, and the copy's reads, 12 bytes too high. Under a model whose pop is stated (gcc's cdecl), a release of the whole block by the caller before the next call or branch counts too, when nothing reads the block between the copy and that release. A record copied into a local at the bottom of the frame