Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions decompiler/crates/kuna-analysis/tests/fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Binary file modified decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.exe
Binary file not shown.
92 changes: 92 additions & 0 deletions decompiler/crates/kuna-analysis/tests/fixtures/vcall_pe_i386.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleepurge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,41 @@ 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, &regs, state);
if s.ret.is_some() || !s.targets.is_empty() || !s.falls_through {
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 {
Expand All @@ -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<VarnodeData>, Vec<VarnodeData>)], 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:")
Expand Down
18 changes: 11 additions & 7 deletions decompiler/crates/kuna-decomp/src/p4_calls/kuna_calleereginputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@ pub fn virtual_this(arch: &Architecture) -> Option<(Rc<ProtoModel>, 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
/// reserves a slot a later store fills is not that push). So does one that
/// 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
Expand Down Expand Up @@ -181,11 +184,12 @@ pub fn virtual_call(data: &Funcdata, call: OpId) -> Option<Rc<ProtoModel>> {
}
}
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));
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions docs/baseline-stages.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"data_footer": [
4340,
4340
4349,
4349
],
"passing": [
"data:64-bit logical shift retains unsigned destination",
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 24 additions & 11 deletions docs/spec/04-calls-and-prototypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`)
Expand All @@ -1663,23 +1665,29 @@ 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
the call its stack arguments. Gated like the direct case to x86-32
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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading