Skip to content

[AUTOMATED] Keep SPARC64 locals in an unused hidden-return slot - #1588

Open
thotypous wants to merge 2 commits into
Noelo-Lab:mainfrom
thotypous:fix/sparc64-hidden-return-slot-locals
Open

thotypous wants to merge 2 commits into
Noelo-Lab:mainfrom
thotypous:fix/sparc64-hidden-return-slot-locals

Conversation

@thotypous

Copy link
Copy Markdown
Contributor

Problem

SparcV9_64.cspec declares the hidden struct-return pointer as a stack input entry at stack 0x7ef..0x7f6, inside every SPARC V9 function's own frame. restructure_varnode cleared the prototype's whole parameter range, that entry included, out of local-variable analysis, so in a function that returns no struct, locals stored there were never mapped. GCC places ordinary locals there (char delimiter[2] at %fp+0x7f5, a va_list at %fp+0x7ef).

This independent example stores its argument into a one-byte buffer at %fp+0x7f5, lets a callee update the buffer, and returns the updated byte:

python3 -c "from pathlib import Path; Path('/tmp/sparc64-hiddenret.bin').write_bytes(bytes.fromhex('9de3bf50f02fa7f59007a7f54000000d01000000f00fa7f581c7e00881e80000010000000100000001000000010000000100000001000000010000000100000081c3e00801000000'))"
kuna decompile /tmp/sparc64-hiddenret.bin 0x1000 --raw-image \
  --target sparc:BE:64:default --base 0x1000 --assert-strict \
  --assert 'prototype 0x1000 unsigned char sub_1000(unsigned char c)' \
  --assert 'prototype 0x1040 void sub_1040(unsigned char *buf)'

Before the fix, the buffer is never declared and the result is the stale argument:

  v1 = c;
  sub_1040(&Stack00000000000007f5);
  return v1;

That C does not compile (Stack00000000000007f5 is undeclared), and replacing the symbol with any separate object still returns the value from before the call.

Fix

  • funcdata_spacebase.rs (local_param_range) builds the parameter range restructure_varnode clears. A storage="hiddenret" stack entry is left out of it only when nothing says the function takes a hidden return pointer: no recorded hidden-return input, no output in indirect storage, no input Varnode read from the slot, and no ordinary stack entry sharing its storage. An unrecovered prototype has no store yet, so the read of the slot on entry is the evidence that keeps a real hidden pointer a parameter.
  • No option: the previous output is false about the program. Only the SPARC V9 32- and 64-bit cspecs have a stack hiddenret entry.

After the fix:

  v1[0] = c;
  sub_1040(v1);
  return v1[0];

(The [686] extent printed for v1 is the separate 32-bit local-range spelling in SparcV9_64.cspec, fixed in #1587.)

Tests

  • Stage test kuna-sparc64-hiddenret-slot-locals.xml: the buffer case, plus a prototype-less function that reads its hidden return pointer from [%fp+0x7ef] and stores through it. With the entry-read evidence removed, that control writes through an uninitialized local instead (ablation run on the cluster).
  • Emitted C for the example, compiled with clang for sparcv9-linux-gnu and run under qemu-sparc64 against a callee that xors the byte with 7, over all 256 inputs: before, the C does not compile; after, every input matches the source at both -O0 and -O2.
  • Debian corpus (decompile-all, 113 executables, 23,142 functions across amd64, arm64, armhf, i386, ppc32, ppc64, riscv32, riscv64, sparc32 and sparc64; a second base run showed no nondeterministic function): only 13 functions change, all SPARC, all reviewed.
    • 12 × sparc64 coreutils error/error_at_line (6 binaries): the va_list at 0x7ef is now a local; the store of &ap into it, and the two variadic spills that fed only that store, are gone because nothing in the C reads them. Both versions already omit ap from the call to verror; that missing argument is unchanged.
    • 1 × sparc32 sort: the outgoing struct-return word written through Stack00000040 (undeclared before) is now written through a declared local array at the same address.
  • CI command set on the rebased tree: datatests 675/675, stages 4346/4346 (baseline re-recorded: adds this test's six 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 6 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.36 s +0.40%
sparc32 (11) 8.99 s 8.99 s +0.03%
amd64 (12) 27.40 s 27.41 s +0.05%

SparcV9_64.cspec declares the hidden struct-return pointer as a stack entry at 0x7ef..0x7f6,
inside every function's own frame. restructure_varnode cleared the prototype's parameter range,
that entry included, out of local analysis, so a buffer there (gcc's delimiter[2] at fp+0x7f5)
never became a symbol: a call that may write it printed as mutate(&Stack00000000000007f5) and the
read after the call returned the value from before it.

local_param_range releases the slot only when nothing says the function takes a hidden return:
no recorded hidden-return input, no output in indirect storage, no input Varnode read from the
slot (an unrecovered prototype has no store yet, so the entry read is the only evidence), and no
ordinary stack entry sharing it. Without the entry-read test a prototype-less function that
writes through its hidden pointer stored through an uninitialized local instead (ablation).

Stage test kuna-sparc64-hiddenret-slot-locals: the buffer case and that control. Stage baseline
re-recorded (+6 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