From cb5f8b3c56d88c4a8deb23f712eeb9207dc21652 Mon Sep 17 00:00:00 2001 From: Paulo Matias Date: Sat, 14 Nov 2026 21:32:52 -0300 Subject: [PATCH] [AUTOMATED] Keep x86 stack arguments a later direct stack store sits below checkInputTrialUse drops a stack trial when the frame's alias boundary reaches it: some pointer formed from the stack pointer sits at or below the slot. A LOAD/STORE through such a pointer counts until heritage turns it into a stack variable, and a pointer whose offset only becomes known late (a stack pointer restored from the frame slot it was saved in after an alloca) is still one when call inputs are scored. The call after the restore stores its own argument through it, below the earlier calls' arguments, and those were dropped (reg(1) printed for reg(1, n)). kuna_directalias: a pointer that is the stack-pointer input plus constants, through copies and frame slots only, and is used solely as the address of LOADs and STOREs, aliases a trial only when the bytes it touches overlap it (AliasChecker::has_local_alias_among draws the boundary from the other pointers). Indexed pointers keep the upstream boundary. Stage test kuna-x86-directalias; Debian corpus (23,142 functions, 10 architectures) and the public decbench corpus (95,698 functions) are unchanged. --- decompiler/crates/kuna-base/src/xml.rs | 3 +- .../src/p4_calls/funcdata_callsite.rs | 2 +- .../src/p4_calls/kuna_directalias.rs | 84 +++++++++++++++++++ .../crates/kuna-decomp/src/p4_calls/mod.rs | 1 + .../kuna-decomp/src/p6_variables/varmap.rs | 20 +++++ docs/baseline-stages.json | 7 +- docs/spec/04-calls-and-prototypes.md | 11 +++ tests/stages/kuna-x86-directalias.xml | 40 +++++++++ 8 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 decompiler/crates/kuna-decomp/src/p4_calls/kuna_directalias.rs create mode 100644 tests/stages/kuna-x86-directalias.xml diff --git a/decompiler/crates/kuna-base/src/xml.rs b/decompiler/crates/kuna-base/src/xml.rs index 5ef7f42d0f..1e835146df 100644 --- a/decompiler/crates/kuna-base/src/xml.rs +++ b/decompiler/crates/kuna-base/src/xml.rs @@ -2205,7 +2205,8 @@ mod tests { // and kuna-repframecopy / a by-value argument copied with rep movs reaches its call // and kuna-libcnames-win32 / a local takes the name of the Win32 call it meets // and kuna-libcnames-addr / a stack object takes the name of the library parameter its address meets - assert_eq!(count, 756, "corpus file count drifted"); + // and kuna-x86-directalias / a call keeps an argument a later direct stack store sits below + assert_eq!(count, 757, "corpus file count drifted"); } /// ~20 representative SLEIGH spec files across varied processors diff --git a/decompiler/crates/kuna-decomp/src/p4_calls/funcdata_callsite.rs b/decompiler/crates/kuna-decomp/src/p4_calls/funcdata_callsite.rs index cc2588ca2f..7dfce2f943 100644 --- a/decompiler/crates/kuna-decomp/src/p4_calls/funcdata_callsite.rs +++ b/decompiler/crates/kuna-decomp/src/p4_calls/funcdata_callsite.rs @@ -157,7 +157,7 @@ pub fn check_input_trial_use( let has_alias = !read_end.is_some_and(|end| trial_end <= end) && { let mut access = data.alias_gather_access(); aliascheck.has_local_alias(Some((vn_space.clone(), vn_offset)), &mut access) - }; + } && crate::p4_calls::kuna_directalias::aliases(data, aliascheck, vn_offset, vn_size); // C++ keeps these two no-use conditions as separate branches // (fspec.cc:5616-5619): a stack alias, OR a stack location outside the // CALLER's local range, both mean "not a parameter". The range test is diff --git a/decompiler/crates/kuna-decomp/src/p4_calls/kuna_directalias.rs b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_directalias.rs new file mode 100644 index 0000000000..426cc0c94d --- /dev/null +++ b/decompiler/crates/kuna-decomp/src/p4_calls/kuna_directalias.rs @@ -0,0 +1,84 @@ +//! (kuna) A direct access at a known stack offset aliases only the bytes it +//! touches. +//! +//! `checkInputTrialUse` drops a stack trial when the frame's alias boundary +//! (`AliasChecker::hasLocalAlias`) reaches it: some pointer formed from the +//! stack pointer sits at or below the slot, so anything above it may be +//! reached. A `LOAD`/`STORE` through such a pointer counts until heritage turns +//! it into a stack variable, and a pointer whose offset only becomes known late +//! — a stack pointer restored from the frame slot it was saved in, after an +//! `alloca` — is still one when call inputs are scored. The call after the +//! restore stores its own argument through it, below the earlier calls' +//! arguments, and those were dropped (`reg(1)` printed for `reg(1, n)`). +//! +//! A pointer that is the stack-pointer input plus constants, through copies +//! and frame slots only, and that is used solely as the address of `LOAD`s and +//! `STORE`s, touches exactly `[offset, offset + size)`. Such a pointer aliases a +//! trial only when those bytes overlap it; every other pointer keeps the +//! upstream boundary. Always on. + +use crate::context::VarnodeId; +use crate::funcdata::Funcdata; +use crate::varmap::{AddBase, AliasChecker}; +use kuna_base::types::{int4, uintb}; +use kuna_num::opcodes::OpCode; + +/// How many definitions [`fixed_offset`] follows back to the stack pointer. +const MAX_STEPS: usize = 16; + +/// Does a gathered pointer reach the trial at `[offset, offset + size)`? +/// Call after `has_local_alias` answered `true` for it. +pub fn aliases(data: &Funcdata, aliascheck: &AliasChecker, offset: uintb, size: int4) -> bool { + let direct = |base: &AddBase| direct_bytes(data, base.base).is_some(); + if aliascheck.has_local_alias_among(offset, |base| !direct(base)) { + return true; + } + let end = offset.wrapping_add(size.max(1) as uintb); + aliascheck.get_alias().iter().zip(aliascheck.get_add_base()).any(|(&start, base)| { + direct_bytes(data, base.base).is_some_and(|bytes| start < end && offset < start.wrapping_add(bytes)) + }) +} + +/// The widest access through `vn` when it is only ever the address of a +/// `LOAD`/`STORE` and a fixed offset from the stack pointer. +fn direct_bytes(data: &Funcdata, vn: VarnodeId) -> Option { + if !fixed_offset(data, vn) { + return None; + } + let mut widest = 0; + for id in data.vbank().get(vn)?.descend_iter() { + let op = data.obank().get(id)?; + let accessed = match op.code() { + OpCode::CPUI_LOAD if op.get_in(1) == Some(vn) => op.get_out()?, + OpCode::CPUI_STORE if op.get_in(1) == Some(vn) && op.get_in(2) != Some(vn) => op.get_in(2)?, + _ => return None, + }; + widest = widest.max(data.vbank().get(accessed)?.get_size().max(0) as uintb); + } + (widest > 0).then_some(widest) +} + +/// Is `vn` the stack-pointer input plus constants, through copies only? +fn fixed_offset(data: &Funcdata, mut vn: VarnodeId) -> bool { + for _ in 0..MAX_STEPS { + let Some(node) = data.vbank().get(vn) else { return false }; + if node.is_input() { + return node.get_space().get_type() == kuna_base::space::spacetype::IPTR_PROCESSOR + && data.get_arch().manage().get_stack_space().is_some_and(|stack| { + stack.get_spacebase(0).is_ok_and(|sp| sp.offset == node.get_offset() && sp.size as int4 == node.get_size()) + }); + } + let Some(op) = node.get_def().and_then(|id| data.obank().get(id)) else { return false }; + let constant = |slot| op.get_in(slot).and_then(|v| data.vbank().get(v)).is_some_and(|v| v.is_constant()); + let next = match op.code() { + OpCode::CPUI_COPY => op.get_in(0), + OpCode::CPUI_INT_ADD if constant(1) => op.get_in(0), + OpCode::CPUI_INT_ADD if constant(0) => op.get_in(1), + OpCode::CPUI_INT_SUB if constant(1) => op.get_in(0), + _ => None, + }; + let Some(next) = next else { return false }; + vn = next; + } + false +} diff --git a/decompiler/crates/kuna-decomp/src/p4_calls/mod.rs b/decompiler/crates/kuna-decomp/src/p4_calls/mod.rs index 3520152833..3b14825abe 100644 --- a/decompiler/crates/kuna-decomp/src/p4_calls/mod.rs +++ b/decompiler/crates/kuna-decomp/src/p4_calls/mod.rs @@ -37,6 +37,7 @@ pub mod kuna_retcallhalf; // (kuna) a call's result handed back beside a compute pub mod kuna_retsysreg; // (kuna) a register set for a system register is not the high word of the return pub mod kuna_retpushedhalf; pub mod kuna_reloadarg; // (kuna) a register reloaded from the caller's frame is re-scored once the frame is heritaged +pub mod kuna_directalias; // (kuna) a direct access at a known stack offset aliases only the bytes it touches pub mod kuna_returnuncomputed; pub mod kuna_spillargtrial; pub mod kuna_stackaddrargtrial; diff --git a/decompiler/crates/kuna-decomp/src/p6_variables/varmap.rs b/decompiler/crates/kuna-decomp/src/p6_variables/varmap.rs index f4b2e22f49..6b6a57d8b0 100644 --- a/decompiler/crates/kuna-decomp/src/p6_variables/varmap.rs +++ b/decompiler/crates/kuna-decomp/src/p6_variables/varmap.rs @@ -812,6 +812,26 @@ impl AliasChecker { vn_offset >= self.alias_boundary } + /// (kuna) [`Self::has_local_alias`]'s boundary drawn from only the gathered + /// pointers `keep` accepts. Call after `has_local_alias` gathered them. + pub fn has_local_alias_among(&self, offset: uintb, keep: impl Fn(&AddBase) -> bool) -> bool { + if self.direction == -1 { + return false; + } + if self.alias.len() != self.add_base.len() { + return offset >= self.alias_boundary; + } + let mut boundary = self.local_extreme; + for (&alias, base) in self.alias.iter().zip(&self.add_base) { + let parameter = + if self.direction == 1 { alias < self.local_boundary } else { alias > self.local_boundary }; + if !parameter && keep(base) { + boundary = boundary.min(alias); + } + } + offset >= boundary + } + /// (kuna) Whether a pointer formed into the parameter area could reach the /// slot at `offset`: an address taken there at or below it. `hasLocalAlias` /// answers only for locals, so a caller slot above the return address whose diff --git a/docs/baseline-stages.json b/docs/baseline-stages.json index d2edbac94a..b5dae70f24 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4343, + 4343 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -4309,6 +4309,9 @@ "data:x86 callee stack: all second words survive", "data:x86 callee stack: first writes survive off and real observers", "data:x86 callee stack: stack-passed const writer does not fold stale bytes", + "data:x86-directalias #1: both functions pass n to the first call", + "data:x86-directalias #2: no first call loses its argument", + "data:x86-directalias #3: the call after the restore keeps its argument", "data:x86_64 callee stack: all later overwrites survive", "data:x86_64 callee stack: all second words survive", "data:x86_64 callee stack: first writes survive off and real observers", diff --git a/docs/spec/04-calls-and-prototypes.md b/docs/spec/04-calls-and-prototypes.md index 526bb647dd..aa1d2663b2 100644 --- a/docs/spec/04-calls-and-prototypes.md +++ b/docs/spec/04-calls-and-prototypes.md @@ -1850,6 +1850,17 @@ classified: argument. That is visible as deleted basic blocks, not merely as a shorter argument list. (kuna) The caller-relative Varnode address is the one probed (`decompiler/crates/kuna-decomp/src/p4_calls/kuna_callsitestackargs.rs`, always on). + + **Direct accesses alias only their bytes** (always on, kuna): a gathered + pointer that is the stack-pointer input plus constants, through copies and + frame slots only, and is used solely as the address of `LOAD`s and `STORE`s + (`kuna_directalias.rs (aliases)`) aliases a trial only when the bytes it + touches overlap the trial; every other pointer keeps the boundary above + (`varmap.rs (AliasChecker::has_local_alias_among)`). Such a pointer is an + access heritage has not converted yet: a stack pointer restored from the + frame slot it was saved in after an `alloca` is still one when call inputs + are scored, and the call after the restore storing its argument at -0x2c + dropped an earlier call's argument at -0x28 (`reg(1)` for `reg(1, n)`). - (kuna `calleeargs`, default on) **Callee stack-argument extent.** On x86-32 under a model that leaves extrapop unknown (`x86win.cspec`), a direct call whose callee's own code can be read takes its stack arguments from what diff --git a/tests/stages/kuna-x86-directalias.xml b/tests/stages/kuna-x86-directalias.xml new file mode 100644 index 0000000000..3c230aa539 --- /dev/null +++ b/tests/stages/kuna-x86-directalias.xml @@ -0,0 +1,40 @@ + + + + +5589e583ec188b4d0883ec1089e0894804c70001000000e8e400000083c4108b4d0883ec3089e0894804c7402008000000c70000000000e8c400000083c4308b4d0883ec10890c24e8b300000083c41089ec5dc390909090909090909090909090909090909090909090909090909090909090909090909090909090909090905589e583ec188b4d0883ec1089e0894804c70001000000e86400000083c4108b4d0883ec3089e0894804c7402008000000c70000000000e84400000083c43089e1894df48b550829d489e0c600078b45f489c48b4d0883ec10890c24e81f00000083c41089ec5dc3909090909090909090909090909090909090909090909090c3909090909090909090909090909090 + + + + + + +reg\(1,n\); +reg\(1\); +reg\(n\); +