From 0cf5db82f97fba8fe9fb85beb064f86762ddc77a Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sat, 14 Nov 2026 21:00:47 +0100 Subject: [PATCH 1/4] [AUTOMATED] Give a virtual call its receiver when MSVC sets ECX after the vtable load calleereginputs' virtual-call rule now accepts an ECX write between the vtable load and the call when it copies the object back (mov eax,[ebx]; push 6; mov ecx,ebx; call eax), and judges a caller cleanup only up to the next call: past a later call the calleeargs scan reads frame teardown, or a later callee's pops of earlier pushes, as cleanup of the virtual call. Co-Authored-By: Claude Opus 5.5 --- .../kuna-analysis/tests/fixtures/README.md | 16 +++-- .../tests/fixtures/vcall_pe_i386.exe | Bin 2560 -> 3072 bytes .../tests/fixtures/vcall_pe_i386.s | 67 ++++++++++++++++++ .../src/p4_calls/kuna_calleepurge.rs | 13 ++++ .../src/p4_calls/kuna_calleereginputs.rs | 18 +++-- docs/spec/04-calls-and-prototypes.md | 28 +++++--- tests/stages/kuna-calleereginputs.xml | 52 +++++++++++++- 7 files changed, 170 insertions(+), 24 deletions(-) diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/README.md b/decompiler/crates/kuna-analysis/tests/fixtures/README.md index 45400cbb52..eef5de7b86 100644 --- a/decompiler/crates/kuna-analysis/tests/fixtures/README.md +++ b/decompiler/crates/kuna-analysis/tests/fixtures/README.md @@ -1988,8 +1988,8 @@ add esp,0x10; ret 0xc`. `consts`@`0x401060` defers a `__cdecl` cleanup past ### Calls through the vtable of the object in ECX -`vcall_pe_i386.exe` (2,560 bytes, PE32/i386, SHA-256 -`fca8c39d626479635bb785c0cec4678258899d7d135043fcb0d785dee8509354`) is clean-room: +`vcall_pe_i386.exe` (3,072 bytes, PE32/i386, SHA-256 +`f61bc379aec82a17e92e719a55e1493a2a8b09af31987de7786a8dacb1027e60`) is clean-room: `vcall`@`0x401000` calls a virtual member (`mov eax,[ecx]; push 5; call [eax+8]`), `comcall`@`0x401010` a COM method that pushes the interface last, `reserve`@`0x401020` a virtual member whose `push ecx` only reserves the slot @@ -1999,10 +1999,16 @@ pushed from the register ECX was copied from, `comesi`@`0x401060` five COM calls through ESI copied from ECX before the first of them, `crosscall`@`0x4010a0` a C table call through ESI copied from ECX before a call that clobbers ECX, `ecxcopy`@`0x4010c0` a virtual member whose ECX is copied from ESI before the vtable -is loaded from ESI, and `gccsret`@`0x4010d0` a GCC (before 4.7) `__cdecl` member -returning a class, `this` stored at `[esp+4]` behind the hidden return pointer. +is loaded from ESI, `gccsret`@`0x4010d0` a GCC (before 4.7) `__cdecl` member +returning a class, `this` stored at `[esp+4]` behind the hidden return pointer, +`latecopy`@`0x4011f0` a virtual member whose ECX is copied from EBX after the +vtable is loaded from EBX and the argument pushed, `lateother`@`0x401210` a call +through EBX's vtable with a second object loaded into ECX, `vepilog`@`0x401230` a +virtual member followed by another call and an epilogue that frees a frame slot +with `pop ecx`, and `vouter`@`0x401250` `o->child()->take(1, 2)`, whose pushes +for `take` precede the virtual `child()` call. Source and recipe (clang + lld-link) in `vcall_pe_i386.s`. Used by -`tests/stages/kuna-calleereginputs.xml` (#18-#30) and +`tests/stages/kuna-calleereginputs.xml` (#18-#37) and `decompiler/crates/kuna-cli/tests/calleereginputs_custom.rs`. ### AIF fragment of a known function diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe index 9783856b720d8b1fff9ddd09a19cb3eb6f5089be..df29bbb03680b776fc80a6b70950204b76d59d18 100755 GIT binary patch literal 3072 zcmeHJ&1(};5TBRbY*q~+fgExO_zHXQARz}2-drWUrMR{2f?vgGgY80-5?2U#QF2-y z^?wm6NKb9zMQFuqZoLRO^pd;yoVUAgKLSGVASgTV_RVkRH}huZ!S1d-8Ug_T6<1Qs zB>0U!M0@G_aS4vDoUeQ$?emq*-Mv@zV9Mgi(e5tk!1-0sjt>Kp9AuS@i~FnkK|tTEW>xSjB9G=SjixDsLln2mr2RM?xi z1n;T^=_RTKt+r!7NgWPMsU95kywPjH3A z@#WEHbg`D9dZ(=76fFLrIytkEpnG3!k?#-SU|} z4g10RmAh)h6L_r0L5TGvKFrseCRnqhpPb_WB>Wh_U4XZDr0s)m(&naGnmx@#Kkz`MO`nkAM`lCF+j=Hod`~;AJYzY&x1Z<=-ABow481@ZQSYy1D4L z#UTA_a|+%Z?{s#ICq9hsMEXS^OcRXT-sw!dr2~#X--Q{b>9aWf0oA4ULMsucqO diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s index b21448c350..a9ce46dac7 100644 --- a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s +++ b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s @@ -16,6 +16,14 @@ # vtable is loaded from ESI. # gccsret (0x4010d0): GCC before 4.7: a __cdecl member returning a class, # this stored at [esp+4] behind the hidden pointer. +# latecopy : a virtual member, the vtable loaded from EBX and ECX copied +# from EBX after the pushes. +# lateother: the vtable loaded from EBX, ECX then loaded with a second +# object: not a call on the object the table belongs to. +# vepilog : a virtual member followed by another call; the epilogue frees +# a frame slot with `pop ecx`, which is no cleanup of either. +# vouter : o->child()->take(1, 2): MSVC pushes take's arguments before +# the virtual child() that yields its object; take pops them. # # Build: # clang --target=i686-pc-windows-msvc -c vcall_pe_i386.s -o v.obj @@ -207,6 +215,65 @@ _cfn: mov eax, dword ptr [esp + 4] ret + .p2align 4, 0xcc +_latecopy: # int latecopy(Obj *o) { return o->put(6); } + push ebx + mov ebx, dword ptr [esp + 8] + mov eax, dword ptr [ebx] + mov eax, dword ptr [eax + 0x30] + push 6 + mov ecx, ebx + call eax + pop ebx + ret + + .p2align 4, 0xcc +_lateother: # int lateother(Obj *o, Obj *p) + push ebx + mov ebx, dword ptr [esp + 8] + mov eax, dword ptr [ebx] + push 6 + mov ecx, dword ptr [esp + 0x10] + call dword ptr [eax + 0x2c] + pop ebx + ret + + .p2align 4, 0xcc +_vepilog: # int vepilog(Obj *o) { o->put(7); return leaf(); } + push esi + sub esp, 4 + mov esi, dword ptr [esp + 0xc] + mov eax, dword ptr [esi] + push 7 + mov ecx, esi + call dword ptr [eax + 0x34] + call _leaf + pop ecx + pop esi + ret + + .p2align 4, 0xcc +_vouter: # void vouter(Obj *o) { o->child()->take(1, 2); } + push esi + sub esp, 4 + mov esi, dword ptr [esp + 0xc] + push 2 + push 1 + mov eax, dword ptr [esi] + mov ecx, esi + call dword ptr [eax + 0x38] + mov ecx, eax + call _take + pop ecx + pop esi + ret + + .p2align 4, 0xcc +_take: + mov eax, dword ptr [esp + 4] + add eax, dword ptr [esp + 8] + ret 8 + .data _vtable: .long 0, _scale, _get, _setvolume, _cfn, _setvolume diff --git a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs index 3f78f15ba0..cdd0985935 100644 --- a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs +++ b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs @@ -160,6 +160,16 @@ pub struct StackArgs { /// stack pointer from the real one. One right after a push is a move /// (`push 3; pop ebx`), which the scan passes over. pub(crate) fn caller_cleanup(arch: &Architecture, call_at: &Address) -> u32 { + cleanup_scan(arch, call_at, false) +} + +/// [`caller_cleanup`], counting only what the caller removes before the next +/// call. +pub(crate) fn immediate_cleanup(arch: &Architecture, call_at: &Address) -> u32 { + cleanup_scan(arch, call_at, true) +} + +fn cleanup_scan(arch: &Architecture, call_at: &Address, before_call: bool) -> u32 { let (Some(space), Some(regs)) = (call_at.get_space().cloned(), registers(arch)) else { return 0 }; let Some(call) = decode(arch, &space, call_at.get_offset()) else { return 0 }; let mut at = call_at.get_offset().wrapping_add(call.1); @@ -174,6 +184,9 @@ pub(crate) fn caller_cleanup(arch: &Architecture, call_at: &Address) -> u32 { break; } let Some(after) = s.state.0[SP].and_then(Val::as_exact) else { break }; + if before_call && s.call.is_some() { + break; + } let pops = match s.call { None => 0, Some(Call::Direct(t)) => match summary(arch, &Address::new(space.clone(), t), DEPTH).effect { diff --git a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleereginputs.rs b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleereginputs.rs index 541dcd3c1b..414b774eb3 100644 --- a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleereginputs.rs +++ b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleereginputs.rs @@ -111,7 +111,9 @@ pub fn virtual_this(arch: &Architecture) -> Option<(Rc, Address, int /// The `__thiscall` model for an indirect call through the vtable of the object /// in ECX: the target is loaded from a slot of a table that is itself loaded -/// from where ECX points, and ECX is not written between that load and the call. +/// from where ECX points, and ECX holds that object from the load to the call: a +/// write to ECX in between must copy the object back (`mov eax,[ebx]; push 6; +/// mov ecx,ebx`). /// A COM method is `__stdcall` with the interface pushed as its first argument, /// so a call whose last push before it stores the object (ECX, or a register /// holding the same value) keeps the default model (a `push ecx` that only @@ -119,8 +121,9 @@ pub fn virtual_this(arch: &Architecture) -> Option<(Rc, Address, int /// stores the object at `[esp+4]`: GCC before 4.7 passes a member function's /// `this` on the stack, behind the hidden pointer of a returned class /// (`mov [esp+4],ecx; mov [esp],edx`). A `__thiscall` callee -/// removes its own stack arguments, so a call the caller cleans up after -/// (`add esp,4`, a C table of `__cdecl` function pointers) keeps it too, as does +/// removes its own stack arguments, so a call the caller cleans up after before +/// its next call (`add esp,4`, a C table of `__cdecl` function pointers) keeps it +/// too, as does /// one that also holds the object in EAX (Delphi's `register` convention passes /// it there). Values are compared over the ops since the previous call only /// ([`Values`]): a call clobbers ECX, so a register copied from ECX before it @@ -181,11 +184,12 @@ pub fn virtual_call(data: &Funcdata, call: OpId) -> Option> { } } let out = o.get_out().and_then(|v| data.vbank().get(v)); - if after_table && out.is_some_and(|v| overlaps(v.get_addr(), v.get_size(), ecx, *ecx_size)) { - return None; - } + let sets_ecx = after_table && out.is_some_and(|v| overlaps(v.get_addr(), v.get_size(), ecx, *ecx_size)); after_table |= id == table_load; values.write(data, id); + if sets_ecx && values.at(ecx, *ecx_size) != object { + return None; + } } let receiver = values.at(ecx, *ecx_size)?; let call_esp = call_esp.unwrap_or_else(|| values.at(&sp_addr, sp.size as int4)); @@ -261,7 +265,7 @@ pub fn callind_cleanup(arch: &Architecture, call_at: &Address) -> u32 { if virtual_this(arch).is_none() { return 0; } - crate::p4_calls::kuna_calleepurge::caller_cleanup(arch, call_at) + crate::p4_calls::kuna_calleepurge::immediate_cleanup(arch, call_at) } /// Ops [`virtual_call`] reads back from the call. diff --git a/docs/spec/04-calls-and-prototypes.md b/docs/spec/04-calls-and-prototypes.md index 526bb647dd..f22b329bcd 100644 --- a/docs/spec/04-calls-and-prototypes.md +++ b/docs/spec/04-calls-and-prototypes.md @@ -1644,9 +1644,11 @@ that machinery. virtual dispatch (`kuna_calleereginputs.rs (virtual_call)`, chosen in `ActionDefaultParams` beside the Swift indirect-call hook): the target is loaded from a slot of a table that is itself loaded from where ECX points - (`mov eax,[ecx]; call [eax+k]`, through copies and a zero offset), and - neither a write to ECX nor another call comes between that load and the - call. The object, the pushes and EAX are compared as values over the ops + (`mov eax,[ecx]; call [eax+k]`, through copies and a zero offset), no other + call comes between that load and the call, and ECX holds the object from the + load to the call: a write to ECX in between must copy the object back, as + MSVC sets ECX last (`mov eax,[ebx]; mov eax,[eax+k]; push 6; mov ecx,ebx; + call eax`), and one that loads anything else keeps the default. The object, the pushes and EAX are compared as values over the ops since the previous call in the block (at most 128), carried forward through copies (`Values`): a call clobbers ECX, so a register copied from ECX before it (`mov esi,ecx; call f; mov eax,[esi]; push esi; call [eax+k]`) @@ -1663,12 +1665,18 @@ that machinery. `__cdecl` argument, second behind the hidden pointer of a returned class (`mov ecx,[ebp+0xc]; mov eax,[ecx]; mov [esp+4],ecx; mov [esp],edx; call [eax+k]`), and ECX only held it on the way. A `__thiscall` callee removes its - own stack arguments, so a call the caller cleans up after (the - `kuna_calleepurge.rs (caller_cleanup)` scan, read at flow time: + own stack arguments, so a call the caller cleans up after before its next + call (the `kuna_calleepurge.rs (immediate_cleanup)` scan, read at flow time: `add esp,4`, a C table of `__cdecl` function pointers reached through ECX) keeps the default too, and so does one that also holds the object in EAX (Delphi's `register` convention, also read under `x86win.cspec`, passes - it there). The chosen model is recorded as the call's read-register model, + it there). A cleanup `calleeargs` credits past a later call is not + evidence here: that scan takes the virtual call to pop only its return + address, so after a later call the frame teardown (`pop ecx` freeing a slot + before `pop edi`) can read as a cleanup, and a later callee that pops + pushes made before the virtual call (`o->child()->take(1, 2)`: MSVC pushes + `take`'s arguments, then calls `child()` for its object) lifts the scan + above the call. The chosen model is recorded as the call's read-register model, so ECX is an argument with the same standing as a direct callee's read register: a receiver whose definition the caller's data flow cannot see (ECX set in an earlier block) no longer ends the argument list and costs @@ -1676,10 +1684,10 @@ that machinery. Windows, under `on` and `custom` alike: `custom` decides its registers only for direct callees, and a function reached through a vtable has its address stored in the image, so it states none. Not - recognized: the common MSVC shape that loads the vtable from the register - ECX is then copied from (`mov eax,[esi]; mov ecx,esi; call [eax+k]`), and a - COM interface pushed by reloading it from memory (`push dword ptr [esp+8]`), - which still prints it twice. + recognized: a COM interface pushed by reloading it from memory + (`push dword ptr [esp+8]`), which still prints it twice. The stack arguments + of `o->child()->take(1, 2)` are still offered to `child()`, which has no way + to say it takes none. - **`ActionExtraPopSetup`** (`coreaction_protos.rs (ActionExtraPopSetup)`) models the stack pointer across each call: a known extrapop becomes an explicit `INT_ADD sp, #extrapop` after the call; an unknown one becomes an diff --git a/tests/stages/kuna-calleereginputs.xml b/tests/stages/kuna-calleereginputs.xml index 063631d990..ce6f68f96c 100644 --- a/tests/stages/kuna-calleereginputs.xml +++ b/tests/stages/kuna-calleereginputs.xml @@ -41,12 +41,21 @@ that clobbers ECX; ecxcopy (0x4010c0) a virtual member whose ECX is copied from ESI before the vtable is loaded from ESI; gccsret (0x4010d0) a GCC (before 4.7) __cdecl member returning a class, whose this is stored at - [esp+4] behind the hidden return pointer while ECX holds it too. By default + [esp+4] behind the hidden return pointer while ECX holds it too; latecopy + (0x4011f0) a virtual member whose vtable is loaded from EBX before ECX is + copied from EBX after the pushes; lateother (0x401210) loads the vtable + from EBX and then a second object into ECX; vepilog (0x401230) a virtual + member followed by another call, after which the epilogue frees a frame + slot with `pop ecx`; vouter (0x401250) o->child()->take(1, 2), whose + pushes for take precede the virtual child() call. By default the virtual members get their receiver (#29); the COM methods keep their pushed interface, once; the C calls and the GCC member get no receiver (#26, #27, #30). A last pass with C types (`ctypes`, `calltargettype`) types the virtual call's target as a function pointer - whose first parameter is `this` (#25). + whose first parameter is `this` (#25). latecopy gets its receiver by + default (#31, #32); lateother gets none in either pass (#33). A pop + after a later call is no cleanup of the virtual call: vepilog and vouter + get their receiver by default (#34-#37). --> c3 @@ -164,6 +173,22 @@ c3 load function gccsret decompile print C + function bounds 0x4011f0 as latecopy + load function latecopy + decompile + print C + function bounds 0x401210 as lateother + load function lateother + decompile + print C + function bounds 0x401230 as vepilog + load function vepilog + decompile + print C + function bounds 0x401250 as vouter + load function vouter + decompile + print C clear architecture load file decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe read symbols @@ -203,6 +228,22 @@ c3 load function gccsret decompile print C + function bounds 0x4011f0 as latecopy + load function latecopy + decompile + print C + function bounds 0x401210 as lateother + load function lateother + decompile + print C + function bounds 0x401230 as vepilog + load function vepilog + decompile + print C + function bounds 0x401250 as vouter + load function vouter + decompile + print C option ctypes on option calltargettype on function bounds 0x401000 as vcall_c @@ -243,4 +284,11 @@ c3 \(\*a0 \+ 0x24\)\)\(6\); \(\*a0 \+ 0x24\)\)\(a0,6\); \(\*a1 \+ 0x28\)\)\(a0,a1\); +\(\*a0 \+ 0x30\)\)\(6\); +\(\*a0 \+ 0x30\)\)\(a0,6\); +\(\*a0 \+ 0x2c\)\)\(6\); +\(\*a0 \+ 0x34\)\)\(7\); +\(\*a0 \+ 0x34\)\)\(a0,7\); +\(\*a0 \+ 0x38\)\)\(1 +\(\*a0 \+ 0x38\)\)\(a0, From 748bb96a023400c12e7fdbcb228bc8f3bdac3554 Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sat, 14 Nov 2026 21:07:02 +0100 Subject: [PATCH 2/4] [AUTOMATED] Record the new calleereginputs stage assertions Co-Authored-By: Claude Opus 5.5 --- docs/baseline-stages.json | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index d2edbac94a..76dfcaf22e 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4347, + 4347 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -305,6 +305,13 @@ "data:CALLEEREGINPUTS #29: default, ECX copied from the register the vtable is then loaded from passes this", "data:CALLEEREGINPUTS #2: off, the method reached through a stub gets nothing", "data:CALLEEREGINPUTS #30: both, a GCC __cdecl member whose this is stored at [esp+4] behind the hidden return pointer gets no receiver", + "data:CALLEEREGINPUTS #31: off, ECX copied from the vtable's register after the pushes gets no receiver", + "data:CALLEEREGINPUTS #32: default, ECX copied from the vtable's register after the pushes passes this", + "data:CALLEEREGINPUTS #33: both, ECX loaded with another object after the vtable gets no receiver", + "data:CALLEEREGINPUTS #34: off, the virtual call before another call gets no receiver", + "data:CALLEEREGINPUTS #35: default, a frame pop after a later call does not hide this", + "data:CALLEEREGINPUTS #36: off, the virtual call yielding the next call's object gets no receiver", + "data:CALLEEREGINPUTS #37: default, a later callee popping the earlier pushes does not hide this", "data:CALLEEREGINPUTS #3: default, the constructor gets this and nothing else", "data:CALLEEREGINPUTS #4: default, the method reached through a stub gets this", "data:CALLEEREGINPUTS #5: default, the constructed field is read back", From 9ff06c2bfb269e3e95e7a2263bda41d8f37a68d2 Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sun, 15 Nov 2026 10:00:13 +0100 Subject: [PATCH 3/4] [AUTOMATED] End the caller-cleanup scan at the SEH unlink A store through FS (mov fs:[0],ecx) unlinks an MSVC SEH frame; the pop ecx after it frees the frame's own slot and is no cleanup of an earlier call. An MSVC virtual call right before such an epilogue kept the default model and lost its receiver. Co-Authored-By: Claude Opus 5.5 --- .../kuna-analysis/tests/fixtures/README.md | 10 ++++--- .../tests/fixtures/vcall_pe_i386.exe | Bin 3072 -> 3072 bytes .../tests/fixtures/vcall_pe_i386.s | 25 ++++++++++++++++++ .../src/p4_calls/kuna_calleepurge.rs | 23 +++++++++++++++- docs/spec/04-calls-and-prototypes.md | 7 ++++- tests/stages/kuna-calleereginputs.xml | 17 ++++++++++-- 6 files changed, 74 insertions(+), 8 deletions(-) diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/README.md b/decompiler/crates/kuna-analysis/tests/fixtures/README.md index eef5de7b86..8cb3ca9bd0 100644 --- a/decompiler/crates/kuna-analysis/tests/fixtures/README.md +++ b/decompiler/crates/kuna-analysis/tests/fixtures/README.md @@ -1989,7 +1989,7 @@ add esp,0x10; ret 0xc`. `consts`@`0x401060` defers a `__cdecl` cleanup past ### Calls through the vtable of the object in ECX `vcall_pe_i386.exe` (3,072 bytes, PE32/i386, SHA-256 -`f61bc379aec82a17e92e719a55e1493a2a8b09af31987de7786a8dacb1027e60`) is clean-room: +`6af1e3e45f66b435e8f13473d5261efa9b19f63238f3da93179d63ef38b01300`) is clean-room: `vcall`@`0x401000` calls a virtual member (`mov eax,[ecx]; push 5; call [eax+8]`), `comcall`@`0x401010` a COM method that pushes the interface last, `reserve`@`0x401020` a virtual member whose `push ecx` only reserves the slot @@ -2005,10 +2005,12 @@ returning a class, `this` stored at `[esp+4]` behind the hidden return pointer, vtable is loaded from EBX and the argument pushed, `lateother`@`0x401210` a call through EBX's vtable with a second object loaded into ECX, `vepilog`@`0x401230` a virtual member followed by another call and an epilogue that frees a frame slot -with `pop ecx`, and `vouter`@`0x401250` `o->child()->take(1, 2)`, whose pushes -for `take` precede the virtual `child()` call. +with `pop ecx`, `vouter`@`0x401250` `o->child()->take(1, 2)`, whose pushes +for `take` precede the virtual `child()` call, and `vseh`@`0x401270` a virtual +member under an SEH frame whose epilogue unlinks it (`mov fs:[0],ecx`) and pops +a slot into ECX right after the call. Source and recipe (clang + lld-link) in `vcall_pe_i386.s`. Used by -`tests/stages/kuna-calleereginputs.xml` (#18-#37) and +`tests/stages/kuna-calleereginputs.xml` (#18-#39) and `decompiler/crates/kuna-cli/tests/calleereginputs_custom.rs`. ### AIF fragment of a known function diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe index df29bbb03680b776fc80a6b70950204b76d59d18..0321df6f3b55d08c200dddb8f56cbe154e44eb6c 100755 GIT binary patch delta 121 zcmZpWXporDz|zZedDX<0FO0i4S2Dh3ad%^2V2F%6eCEuVtp8aIDGPyIhJdgvhF%vH zj+Dh9UU!L#M7N(zw_taWO&0tAOWi&|nND7iRAgN95gw4b>9aWf0oA4ULXqAFMb delta 61 zcmZpWXporDz|!Z`96Pb)3uE=>O2)S=>}(7S43Tk%H?L(2W}JM1&0!J;3nxekP?$kr P^GC+-jGI}w{xAXnkXsZ) diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s index a9ce46dac7..0560222a7d 100644 --- a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s +++ b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s @@ -24,6 +24,8 @@ # a frame slot with `pop ecx`, which is no cleanup of either. # vouter : o->child()->take(1, 2): MSVC pushes take's arguments before # the virtual child() that yields its object; take pops them. +# vseh : a virtual member under an SEH frame, whose epilogue unlinks it +# (mov fs:[0],ecx) and frees a slot with `pop ecx` right after. # # Build: # clang --target=i686-pc-windows-msvc -c vcall_pe_i386.s -o v.obj @@ -268,6 +270,29 @@ _vouter: # void vouter(Obj *o) { o->child()->take(1, 2); pop esi ret + .p2align 4, 0xcc +_vseh: # void vseh(Holder *h) { try { h->obj->set(7); } ... } + push -1 + push 0 + mov eax, dword ptr fs:[0] + push eax + push esi + push 0 + lea eax, [esp + 8] + mov dword ptr fs:[0], eax + mov esi, dword ptr [esp + 0x18] + mov ecx, dword ptr [esi + 0x1c] + mov edx, dword ptr [ecx] + mov edx, dword ptr [edx + 0x3c] + push 7 + call edx + mov ecx, dword ptr [esp + 8] + mov dword ptr fs:[0], ecx + pop ecx + pop esi + add esp, 0xc + ret + .p2align 4, 0xcc _take: mov eax, dword ptr [esp + 4] diff --git a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs index cdd0985935..7ca48323c1 100644 --- a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs +++ b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs @@ -158,7 +158,9 @@ pub struct StackArgs { /// epilogue restoring it and ends the scan: the frame teardown after it is not /// a cleanup, however far a later call's unproven pop has left the scan's /// stack pointer from the real one. One right after a push is a move -/// (`push 3; pop ebx`), which the scan passes over. +/// (`push 3; pop ebx`), which the scan passes over. A store through FS ends the +/// scan too: it unlinks the SEH frame (`mov fs:[0],ecx`), and the `pop ecx` +/// after it frees the frame's own slot. pub(crate) fn caller_cleanup(arch: &Architecture, call_at: &Address) -> u32 { cleanup_scan(arch, call_at, false) } @@ -172,11 +174,15 @@ pub(crate) fn immediate_cleanup(arch: &Architecture, call_at: &Address) -> u32 { fn cleanup_scan(arch: &Architecture, call_at: &Address, before_call: bool) -> u32 { let (Some(space), Some(regs)) = (call_at.get_space().cloned(), registers(arch)) else { return 0 }; let Some(call) = decode(arch, &space, call_at.get_offset()) else { return 0 }; + let fs = arch.translate().get_register_data("FS_OFFSET").ok(); let mut at = call_at.get_offset().wrapping_add(call.1); let (mut sp, mut high, mut pushed) = (0i64, 0i64, false); for _ in 0..CLEANUP_SCAN { let Some(insn) = decode(arch, &space, at) else { break }; let (ops, len) = (&insn.0, insn.1); + if fs.as_ref().is_some_and(|fs| stores_through(ops, fs)) { + break; + } let mut state = State::at_entry(); state.0[SP] = Some(Val::exact(sp)); let s = step(ops, ®s, state); @@ -210,6 +216,21 @@ fn cleanup_scan(arch: &Architecture, call_at: &Address, before_call: bool) -> u3 high.min(MAX_EXTRAPOP as i64) as u32 } +/// Does an instruction store through an address formed from `base`? +fn stores_through(ops: &[(OpCode, Option, Vec)], base: &VarnodeData) -> bool { + let mut derived: Vec<&VarnodeData> = vec![base]; + for (code, out, ins) in ops { + if *code == OpCode::CPUI_STORE { + if ins.get(1).is_some_and(|p| derived.contains(&p)) { + return true; + } + } else if let Some(out) = out.as_ref().filter(|_| ins.iter().any(|i| derived.contains(&i))) { + derived.push(out); + } + } + false +} + /// x86-32 under a default model whose extrapop is unknown. pub(crate) fn applies(arch: &Architecture) -> bool { arch.archid.starts_with("x86:LE:32:") diff --git a/docs/spec/04-calls-and-prototypes.md b/docs/spec/04-calls-and-prototypes.md index f22b329bcd..d6e36dc5c9 100644 --- a/docs/spec/04-calls-and-prototypes.md +++ b/docs/spec/04-calls-and-prototypes.md @@ -1908,7 +1908,12 @@ classified: taken to pop only its return address), since the scan's stack pointer then sits low by those bytes and the teardown seems to lift it above the call. A pop right after a push is a move (`push 3; pop ebx`, MSVC `/O1`'s - `mov ebx,3`), not the epilogue, and the run goes on past it. A frame torn + `mov ebx,3`), not the epilogue, and the run goes on past it. A store + through FS ends the run as well + (`decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs + (stores_through)`): `mov fs:[0],ecx` unlinks an MSVC SEH frame, and the + `pop ecx` that follows it frees the frame's own slot (the `/GS` cookie or + a local) before the callee-saved registers are restored. A frame torn down with no callee-saved register restored first (`sub esp,0x10` and no pushes) still reads as a cleanup. Where the extent is stated, a stack trial starting at or past the bound (or past what the callee diff --git a/tests/stages/kuna-calleereginputs.xml b/tests/stages/kuna-calleereginputs.xml index ce6f68f96c..845693b87c 100644 --- a/tests/stages/kuna-calleereginputs.xml +++ b/tests/stages/kuna-calleereginputs.xml @@ -47,7 +47,9 @@ from EBX and then a second object into ECX; vepilog (0x401230) a virtual member followed by another call, after which the epilogue frees a frame slot with `pop ecx`; vouter (0x401250) o->child()->take(1, 2), whose - pushes for take precede the virtual child() call. By default + pushes for take precede the virtual child() call; vseh (0x401270) a + virtual member under an SEH frame, whose epilogue unlinks it and frees a + slot with `pop ecx` right after the call. By default the virtual members get their receiver (#29); the COM methods keep their pushed interface, once; the C calls and the GCC member get no receiver (#26, #27, #30). A last pass with C types (`ctypes`, @@ -55,7 +57,8 @@ whose first parameter is `this` (#25). latecopy gets its receiver by default (#31, #32); lateother gets none in either pass (#33). A pop after a later call is no cleanup of the virtual call: vepilog and vouter - get their receiver by default (#34-#37). + get their receiver by default (#34-#37), and so does vseh: the SEH unlink + ends the cleanup scan (#38, #39). --> c3 @@ -189,6 +192,10 @@ c3 load function vouter decompile print C + function bounds 0x401270 as vseh + load function vseh + decompile + print C clear architecture load file decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe read symbols @@ -244,6 +251,10 @@ c3 load function vouter decompile print C + function bounds 0x401270 as vseh + load function vseh + decompile + print C option ctypes on option calltargettype on function bounds 0x401000 as vcall_c @@ -291,4 +302,6 @@ c3 \(\*a0 \+ 0x34\)\)\(a0,7\); \(\*a0 \+ 0x38\)\)\(1 \(\*a0 \+ 0x38\)\)\(a0, +\[7\] \+ 0x3c\)\)\(7 +\[7\] \+ 0x3c\)\)\(\(\(int4 \*\*\)a0\)\[7\],7 From 3e65540c5305c5221a52d9fc5bdebd3e3a816153 Mon Sep 17 00:00:00 2001 From: Boran Car Date: Sun, 15 Nov 2026 10:03:50 +0100 Subject: [PATCH 4/4] [AUTOMATED] Record the SEH-unlink calleereginputs assertions Co-Authored-By: Claude Opus 5.5 --- docs/baseline-stages.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index 76dfcaf22e..098bfde6c6 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4347, - 4347 + 4349, + 4349 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -312,6 +312,8 @@ "data:CALLEEREGINPUTS #35: default, a frame pop after a later call does not hide this", "data:CALLEEREGINPUTS #36: off, the virtual call yielding the next call's object gets no receiver", "data:CALLEEREGINPUTS #37: default, a later callee popping the earlier pushes does not hide this", + "data:CALLEEREGINPUTS #38: off, the virtual call under an SEH frame gets no receiver", + "data:CALLEEREGINPUTS #39: default, the pop after the SEH unlink does not hide this", "data:CALLEEREGINPUTS #3: default, the constructor gets this and nothing else", "data:CALLEEREGINPUTS #4: default, the method reached through a stub gets this", "data:CALLEEREGINPUTS #5: default, the constructed field is read back",