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
3 changes: 2 additions & 1 deletion decompiler/crates/kuna-base/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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-sparc64-or-masked-spill / a spill read through an OR-masked frame address is kept
assert_eq!(count, 757, "corpus file count drifted");
}

/// ~20 representative SLEIGH spec files across varied processors
Expand Down
20 changes: 20 additions & 0 deletions decompiler/crates/kuna-decomp/src/p3_dataflow/heritage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5543,6 +5543,26 @@ impl Heritage {
);
}
}
OpCode::CPUI_INT_OR => {
// (kuna) `p | c` with constant `c` lies in `[p, p+c]` (absent
// wraparound): GCC reads a spilled int's low byte through
// `(fp+off)|3` on SPARC64. Which byte depends on the runtime low
// bits of `p`, so it is an indexed access at `p`'s offset, and the
// LOAD/STORE it reaches needs a guard. An unrefined guard covers
// `[p, p+0x1000)` (`LoadGuard::establish_range`), so a larger `c`
// could only guard slots it cannot reach.
let small_const = fd
.obank()
.get(op)
.and_then(|o| o.get_in(1 - o.get_slot(cur_vn)))
.and_then(|v| fd.vbank().get(v))
.is_some_and(|v| v.is_constant() && v.get_offset() < 0x1000);
let ov = match out_vn {
Some(v) if small_const => v,
_ => continue,
};
descend_into!(ov, cur_offset, cur_traversals | stacknode_flags::nonconstant_index);
}
OpCode::CPUI_SEGMENTOP => {
// Check that the stackpointer comes in as the inner pointer.
let in2 = fd.obank().get(op).and_then(|o| o.get_in(2));
Expand Down
6 changes: 4 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
4342,
4342
],
"passing": [
"data:64-bit logical shift retains unsigned destination",
Expand Down Expand Up @@ -3952,6 +3952,8 @@
"data:sparc64-double-abs #2 has no numeric high-word conversion",
"data:sparc64-double-sign #1 restores unary negation",
"data:sparc64-double-sign #2 has no numeric high-word conversion",
"data:sparc64-or-masked-spill #1: the argument spill is kept",
"data:sparc64-or-masked-spill #2: the byte is read back through the spilled local",
"data:spill and CTR effects acquire no terminal contract",
"data:split caller stays untyped with protoorder off",
"data:split caller takes the callee type by default",
Expand Down
9 changes: 8 additions & 1 deletion docs/spec/03-ssa-and-simplification.md
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,14 @@ the stack-pointer input's descendant tree — accumulating constant `INT_ADD`
offsets, passing through COPY/INDIRECT/SEGMENTOP, and flagging any traversal of
a *non-constant* add or a MULTIEQUAL — and records a guard
(`heritage.rs (LoadGuard)`) for every LOAD/STORE reached on a flagged path,
marking the op `spacebase_ptr`. A guard is born covering the **entire space**
marking the op `spacebase_ptr`. **OR-masked pointers** (always on, kuna): an
`INT_OR` of the path with a constant below `0x1000` (the window an unrefined
guard covers) is flagged the same way. GCC reads a spilled `int`'s low byte
through `(fp+0x7fb)|3` on SPARC64; which byte depends on the pointer's
runtime low bits, so it is an indexed access at the pointer's offset.
Upstream stops at the OR: the LOAD gets no guard, the spill
looks dead and is deleted, and the result reads a slot nothing wrote. A guard
is born covering the **entire space**
(`LoadGuard::set`: minimum 0, maximum the space's highest offset). A STORE
whose pointer is still a free varnode cannot be classified yet: it is
conservatively marked and queued (`heritage.rs (Heritage::protect_free_stores)`),
Expand Down
5 changes: 5 additions & 0 deletions docs/spec/04-calls-and-prototypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ printed as a store to one variable and a call on `&Stack0000000000000018`;
with the spec's ranges it is the local array the stock decompiler prints. MIPS
o32 says the same of its 0..15 register backup area, so a spill there that
nothing reads is dead and no longer makes an unused argument a parameter.
SparcV9_64.cspec spelled its below-the-frame local range in 32 bits
(`0xfff0bdc1..0xffffffff`), which in a 64-bit stack space is a range near
4 GiB, so the frame's only local range ended at the window save area and an
open array there ran on through the parameter range. kuna's copy sign-extends
it, as x86-64-gcc.cspec does; an array at `fp+0x7fb` now ends at the frame.

The spec's **named** models are registered alongside it, in document order
(`decompiler/crates/kuna-decomp/src/infra/architecture.rs (decode_named_protos,
Expand Down
4 changes: 2 additions & 2 deletions specs/Ghidra/Processors/Sparc/data/languages/SparcV9_64.cspec
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<register name="fd2"/>
</killedbycall>
<localrange>
<range space="stack" first="0xfff0bdc1" last="0xffffffff"/>
<range space="stack" first="0xfffffffffff0bdc1" last="0xffffffffffffffff"/> <!-- (kuna) 64-bit stack: sign-extend, as x86-64 does -->
<range space="stack" first="0x0" last="0x8ae"/> <!-- Stack bias of 7FF + 0xb0 window size -->
</localrange>
</prototype>
Expand Down Expand Up @@ -219,7 +219,7 @@
<register name="didrestore"/>
</unaffected>
<localrange>
<range space="stack" first="0xfff0bdc1" last="0xffffffff"/>
<range space="stack" first="0xfffffffffff0bdc1" last="0xffffffffffffffff"/> <!-- (kuna) 64-bit stack: sign-extend, as x86-64 does -->
<range space="stack" first="0x0" last="0x8ae"/> <!-- Stack bias of 7FF + 0xb0 window size -->
</localrange>
</prototype>
Expand Down
39 changes: 39 additions & 0 deletions tests/stages/kuna-sparc64-or-masked-spill.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<decompilertest>
<binaryimage arch="sparc:BE:64:default:default">
<!--
GCC -O0 on SPARC V9 64-bit spills an int argument and reads its low byte
back through an OR-masked frame address (sample_branch's `n` spill):

int flag(int n) { return n & 1; }

save %sp, -0xb0, %sp
stw %i0, [%fp+0x7fb]
add %fp, 0x7fb, %i0
or %i0, 3, %i0 ! low byte of the big-endian int
ldub [%i0], %i0
and %i0, 1, %i0
ret
restore

Two defects removed the spill, so the result read a slot nothing wrote:
the indexed-stack-pointer walk did not follow INT_OR, so the LOAD got no
guard; and SparcV9_64.cspec spelled its negative local range in 32 bits,
so the open array at 0x7fb ran past the frame into the parameter range,
markUnaliased called it unaliased, and the guard's address force on the
spill was cleared. Hand-assembled.
-->
<bytechunk space="ram" offset="0x100000" readonly="true">
9de3bf50f027a7fbb007a7fbb0162003f00e0000b00e200181c7e00881e80000
</bytechunk>
<symbol space="ram" offset="0x100000" name="flag"/>
</binaryimage>
<script>
<com>map prototype flag int flag(int n);</com>
<com>lo fu flag</com>
<com>decompile</com>
<com>print C</com>
<com>quit</com>
</script>
<stringmatch name="sparc64-or-masked-spill #1: the argument spill is kept" min="1" max="1">v1\[0\] = n;</stringmatch>
<stringmatch name="sparc64-or-masked-spill #2: the byte is read back through the spilled local" min="1" max="1">return \*\(uint1 \*\)\(\(uint8\)v1 \| 3\) &amp; 1;</stringmatch>
</decompilertest>
Loading