diff --git a/decompiler/crates/kuna-analysis/tests/fixtures/README.md b/decompiler/crates/kuna-analysis/tests/fixtures/README.md index 45400cbb52..8cb3ca9bd0 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 +`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 @@ -1999,10 +1999,18 @@ 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`, `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-#30) 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 9783856b72..0321df6f3b 100755 Binary files a/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe and b/decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe differ 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..0560222a7d 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,16 @@ # 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. +# 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 @@ -207,6 +217,88 @@ _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 +_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] + 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..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,15 +158,31 @@ 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) +} + +/// [`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 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); @@ -174,6 +190,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 { @@ -197,6 +216,21 @@ pub(crate) fn caller_cleanup(arch: &Architecture, call_at: &Address) -> u32 { 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/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/baseline-stages.json b/docs/baseline-stages.json index d2edbac94a..098bfde6c6 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4349, + 4349 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -305,6 +305,15 @@ "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 #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", diff --git a/docs/spec/04-calls-and-prototypes.md b/docs/spec/04-calls-and-prototypes.md index 526bb647dd..d6e36dc5c9 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 @@ -1900,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 063631d990..845693b87c 100644 --- a/tests/stages/kuna-calleereginputs.xml +++ b/tests/stages/kuna-calleereginputs.xml @@ -41,12 +41,24 @@ 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; 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`, `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), and so does vseh: the SEH unlink + ends the cleanup scan (#38, #39). --> c3 @@ -164,6 +176,26 @@ 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 + 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 @@ -203,6 +235,26 @@ 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 + function bounds 0x401270 as vseh + load function vseh + decompile + print C option ctypes on option calltargettype on function bounds 0x401000 as vcall_c @@ -243,4 +295,13 @@ 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, +\[7\] \+ 0x3c\)\)\(7 +\[7\] \+ 0x3c\)\)\(\(\(int4 \*\*\)a0\)\[7\],7