From 35ffe012796097e8aaa44d214c13d9609230e08b Mon Sep 17 00:00:00 2001 From: Paulo Matias Date: Sat, 14 Nov 2026 18:16:54 -0300 Subject: [PATCH 1/2] [AUTOMATED] Keep SPARC64 spills read through an OR-masked frame address GCC -O0 on SPARC V9 64-bit reads a spilled int's low byte through (fp+0x7fb)|3. The spill was deleted and the result read a slot nothing wrote, from two defects that each suffice: - Heritage's indexed-stack-pointer walk did not follow INT_OR, so the LOAD got no guard. An INT_OR with a constant below 0x1000 (the window an unrefined guard covers) is now an indexed access at the pointer's offset, like a non-constant INT_ADD. - SparcV9_64.cspec spelled its below-the-frame local range in 32 bits (0xfff0bdc1..0xffffffff), a range near 4 GiB in the 64-bit stack space. The open array at 0x7fb then ran on into the parameter range, markUnaliased reset it to unaliased at the range boundary, and symbol sync cleared the guard's address force from the spill. The range is sign-extended, as x86-64-gcc.cspec spells it. Measured: either change alone leaves the spill dead; both keep v1[0] = n. Stage test kuna-sparc64-or-masked-spill; stage baseline re-recorded (+2 keys, none removed); make test, test-stages, check-spec, lint, test-cli, rust-test and the kuna-ghidra suite pass. --- decompiler/crates/kuna-base/src/xml.rs | 3 +- .../kuna-decomp/src/p3_dataflow/heritage.rs | 20 ++++++++++ docs/spec/03-ssa-and-simplification.md | 9 ++++- docs/spec/04-calls-and-prototypes.md | 5 +++ .../Sparc/data/languages/SparcV9_64.cspec | 4 +- tests/stages/kuna-sparc64-or-masked-spill.xml | 39 +++++++++++++++++++ 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tests/stages/kuna-sparc64-or-masked-spill.xml diff --git a/decompiler/crates/kuna-base/src/xml.rs b/decompiler/crates/kuna-base/src/xml.rs index 5ef7f42d0f..7736f54af4 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-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 diff --git a/decompiler/crates/kuna-decomp/src/p3_dataflow/heritage.rs b/decompiler/crates/kuna-decomp/src/p3_dataflow/heritage.rs index f0d4915367..66078e3890 100644 --- a/decompiler/crates/kuna-decomp/src/p3_dataflow/heritage.rs +++ b/decompiler/crates/kuna-decomp/src/p3_dataflow/heritage.rs @@ -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)); diff --git a/docs/spec/03-ssa-and-simplification.md b/docs/spec/03-ssa-and-simplification.md index fd9d3c1f45..022cbb02d5 100644 --- a/docs/spec/03-ssa-and-simplification.md +++ b/docs/spec/03-ssa-and-simplification.md @@ -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)`), diff --git a/docs/spec/04-calls-and-prototypes.md b/docs/spec/04-calls-and-prototypes.md index 526bb647dd..2150250af5 100644 --- a/docs/spec/04-calls-and-prototypes.md +++ b/docs/spec/04-calls-and-prototypes.md @@ -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, diff --git a/specs/Ghidra/Processors/Sparc/data/languages/SparcV9_64.cspec b/specs/Ghidra/Processors/Sparc/data/languages/SparcV9_64.cspec index 25e6dec75a..0c9400a76d 100644 --- a/specs/Ghidra/Processors/Sparc/data/languages/SparcV9_64.cspec +++ b/specs/Ghidra/Processors/Sparc/data/languages/SparcV9_64.cspec @@ -146,7 +146,7 @@ - + @@ -219,7 +219,7 @@ - + diff --git a/tests/stages/kuna-sparc64-or-masked-spill.xml b/tests/stages/kuna-sparc64-or-masked-spill.xml new file mode 100644 index 0000000000..4f30f55205 --- /dev/null +++ b/tests/stages/kuna-sparc64-or-masked-spill.xml @@ -0,0 +1,39 @@ + + + + +9de3bf50f027a7fbb007a7fbb0162003f00e0000b00e200181c7e00881e80000 + + + + +v1\[0\] = n; +return \*\(uint1 \*\)\(\(uint8\)v1 \| 3\) & 1; + From 24da2026be8eb978528d3506a1abc3616d052149 Mon Sep 17 00:00:00 2001 From: Paulo Matias Date: Sat, 14 Nov 2026 18:57:27 -0300 Subject: [PATCH 2/2] [AUTOMATED] Re-record the stage baseline on current main Regenerated with `kuna test --datatests --datatests-dir tests/stages --save-baseline`; adds only this branch's new stage assertions. --- 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 d2edbac94a..97fb2df79e 100644 --- a/docs/baseline-stages.json +++ b/docs/baseline-stages.json @@ -1,7 +1,7 @@ { "data_footer": [ - 4340, - 4340 + 4342, + 4342 ], "passing": [ "data:64-bit logical shift retains unsigned destination", @@ -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",