Skip to content

[AUTOMATED] Keep SPARC64 spills read through an OR-masked frame address - #1587

Open
thotypous wants to merge 2 commits into
Noelo-Lab:mainfrom
thotypous:fix/sparc64-or-masked-stack-loads
Open

thotypous wants to merge 2 commits into
Noelo-Lab:mainfrom
thotypous:fix/sparc64-or-masked-stack-loads

Conversation

@thotypous

Copy link
Copy Markdown
Contributor

Problem

GCC -O0 on SPARC V9 64-bit spills an int argument and reads its low byte back through an OR-masked frame address. The spill was deleted, so the result read a stack slot nothing wrote. This independent example is int f(int n) { return n & 1; }:

python3 -c "from pathlib import Path; Path('/tmp/sparc64-ormask.bin').write_bytes(bytes.fromhex('9de3bf50f027a7fbb007a7fbb0162003f00e0000b00e200181c7e00881e80000'))"
kuna decompile /tmp/sparc64-ormask.bin 0x1000 --raw-image \
  --target sparc:BE:64:default --base 0x1000 --assert-strict \
  --assert 'prototype 0x1000 int sub_1000(int n)'
save %sp,-0xb0,%sp ; stw %i0,[%fp+0x7fb] ; add %fp,0x7fb,%i0 ; or %i0,3,%i0 ; ldub [%i0],%i0 ; and %i0,1,%i0 ; ret ; restore

Before the fix the spill is gone:

  char v1 [680]; // stack + 0x7fb

  return *(unsigned char *)((unsigned long long)v1 | 3) & 1;

Compiled and run, that returns whatever the uninitialized array holds.

Fix

Two independent defects each delete the spill:

  • heritage.rs (Heritage::discover_indexed_stack_pointers) followed INT_ADD, SEGMENTOP, COPY, INDIRECT and MULTIEQUAL from the stack pointer, but not 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; the constant is not added, since p | c lies anywhere in [p, p+c].
  • 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 frame's only real local range then ended at the window save area, so the open array at 0x7fb ran on through the parameter range, markUnaliased reset it to unaliased at the range boundary, and symbol sync cleared the guard's address force from the spill. Both localrange entries are sign-extended, as x86-64-gcc.cspec spells the same window.

Either change alone leaves the spill dead (measured). With both:

  int v1 [45]; // i0 | o0 | stack + 0x7fb

  v1[0] = n;
  return *(unsigned char *)((unsigned long long)v1 | 3) & 1;

No option: the previous output is false about the program.

Tests

  • Stage test kuna-sparc64-or-masked-spill.xml with the example (0x7fb | 3 == 0x7fb, so a fix that added the constant would miss the slot).
  • Emitted C compiled with clang for sparcv9-linux-gnu and run under qemu-sparc64 for n in [-300, 300): before, wrong results at -O0 and -O2; after, every value matches n & 1 at both.
  • Debian corpus (decompile-all, 113 executables, 23,142 functions across 10 architectures; a second base run showed no nondeterministic function): 46 functions change, all sparc64, all reviewed.
    • 18: an array at 0x887 now ends at the frame ([540] → [40]) instead of running into the parameter range.
    • 6: a stack-protected 1 KiB buffer is char buf[1024] with the canary load beside it, instead of a 1708-byte array that swallowed the canary slot.
    • 2 (ls): locals below the stack pointer are declared. sub_9140 was sub_8bf0(&Stack…e7e7, …) with undeclared symbols; it is now ptr = buf; sub_8bf0(&ptr, …); if (ptr != a0 && ptr != buf) free(ptr);.
    • 8 (ls): callers of sub_9140 pick up its char * parameter.
    • 12 (error/error_at_line): the third spilled variadic register (%i5) is recovered with the other two.
  • CI command set on the rebased tree: datatests 675/675, stages 4342/4342 (baseline re-recorded: adds this test's two assertions, removes none), tooling 115/115, CLI probes 324/324, workspace 10,908 passed / 0 failed, kuna-ghidra 62/62 including ignored, lint, spec, catalog, options and counters. Web: WASI build and worker/replay tests pass; dev-viz needs full git history and could not run in the cluster's export.
  • Full public decbench corpus: 770 binaries / 96,103 requested functions across 40 projects (663 x86-64 ELF, 95 ARM ELF, 12 x86 PE; no SPARC, so this is regression evidence only). Base and this PR recover the same 95,698 functions with the same 405 gaps; decompiled C, JSON payloads and coverage are identical, as are GED (90,305 scored, 38,454 perfect) and type-match (87,892 scored, 9,050 perfect).
  • Byte-match over all 770 binaries (native GCC, arm-none-eabi-gcc, i686-w64-mingw32-gcc; every recompiler available): no change from this PR. Each arm scores 95,698 functions, 80,402 recompilable, 15,296 not, 5,836 perfect, 0 metric errors. The default run reported 7 per-function score differences, but the decompiled C is byte-identical between arms for every binary, so they cannot come from this change. They come from the scorer's wall-clock diff timeout on large functions (inflate, template_parse, ssh_tty_parse_modes, ...). Re-scored with that timeout disabled, every pair has byte-identical original and recompiled machine code and equal scores.

Timing: kuna decompile-all --json over each Debian set, from node-local copies, one pinned CPU on an exclusive node, 11 rounds (2 warmups discarded), arm order rotated. Process start and spec loading are included.

Median wall time Base PR Delta
sparc64 (12 executables) 15.30 s 15.27 s −0.22%
sparc32 (11) 8.99 s 8.95 s −0.43%
amd64 (12) 27.40 s 27.28 s −0.45%

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.
Regenerated with `kuna test --datatests --datatests-dir tests/stages --save-baseline`;
adds only this branch's new stage assertions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant