Skip to content

[AUTOMATED] Narrow a SPARC int's return 0 folded into the return pair - #1000

Merged
mahaloz merged 2 commits into
mainfrom
fix/issue-796-sparc-const-pair-return
Oct 10, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fix/issue-796-sparc-const-pair-return

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

On SPARC, a function that returns an int printed as a long long that shifts its second argument into the high word whenever it returns 0.

$ printf 'int keep_zero(int *p, int b) { *p = b; return 0; }\nunsigned same(unsigned a, unsigned b) { return a == b; }\n' > k.c
$ clang --target=sparc-linux-gnu -fintegrated-as -O2 -c k.c && kuna decompile-all k.o
long long keep_zero(unsigned int *a0,unsigned int a1)
{
  *a0 = a1;
  return (unsigned long long)a1 << 0x20;
}
long long same(unsigned int a0,unsigned int a1)
{
  if (a0 == a1)
    return 1;
  return (unsigned long long)a1 << 0x20;
}

With this change both are unsigned int, and the return 0 paths print return 0;.

Fix

  • restore copies %i1 back into %o1, so return recovery joins %o0:%o1 for these functions. The uncomputed-half repair already drops a leftover %o1 beside any other value (return 5 was fine), but the rules folded PIECE(a1, 0) into ZEXT(a1) << 32 before it ran. The repair now reads that form as the pair a1 : 0.
  • It does so only for a pair joined first register low on an ABI that returns the first register high (SPARC, PowerPC, MIPS and ARM big-endian). There the folded value has its words swapped for any real long long, so narrowing it never replaces a correct value, and no option is added.
  • unsigned long long f(unsigned a, unsigned b) { return b; } is the same SPARC code as int f(int a, int b) { return 0; } and now prints as that int, as ((u64)5 << 32) | b already printed return 5;.

Tests

  • tests/stages/kuna-sparc-zeropair.xml: SPARC -O2 and -O0 keep_zero, same and flag_zero return an int 0; keep_five and three long long returns keep their output. 5 of its 7 asserts fail without the fix.
  • The SPARC round-trip tests in decompile_all_cli.rs now include keep_zero, zero_after and one_after (both fail without the fix), plus unit tests for the folded form.
  • 25 userland projects built with clang for SPARC (-O0/-O2/-Os) and PowerPC, MIPS and ARM big-endian (-O0/-O2): 1,272 of 90,049 functions change, all on SPARC, none with an 8-byte DWARF return type. x86-64 output is unchanged and speed is neutral.

Fixes #796

🤖 Generated with Claude Code

https://claude.ai/code/session_01JiSdQ5U9toD6x81TksF4u9

On SPARC, restore copies %i1 back into %o1 on every return, so return
recovery joins %o0:%o1 (first register low) for a function that returns
one int. The uncomputed-half repair drops the %o1 half when it is the
untouched argument, but by then the rule pool has folded PIECE(a1, 0)
into ZEXT(a1) << 32, which the repair did not read: `int f(int *p, int b)
{ *p = b; return 0; }` printed as a long long returning
`(unsigned long long)a1 << 0x20`.

The repair now reads ZEXT(hi) << 8k as the pair hi:0 on a pair joined
first register low on an ABI that returns the first register high
(Funcdata::kuna_pairs_first_low). There the folded value has its words
swapped for any genuine 64-bit return, so narrowing it replaces only a
wrong value; no option. Over 25 userland projects built with clang for
SPARC (-O0, -O2, -Os) and PowerPC, MIPS and ARM big-endian (-O0, -O2),
1,272 of 90,049 functions change, all on SPARC, none with an eight-byte
DWARF return type.

Adds tests/stages/kuna-sparc-zeropair.xml (re-records
docs/baseline-stages.json, corpus count 483), unit tests for the folded
form, and extends the SPARC big-endian round-trip CLI tests with
keep_zero, zero_after and one_after.

Fixes #796

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JiSdQ5U9toD6x81TksF4u9
@mahaloz
mahaloz force-pushed the fix/issue-796-sparc-const-pair-return branch from f0097e1 to b517894 Compare October 10, 2026 02:06
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 10, 2026
# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
@mahaloz
mahaloz merged commit 755dd0b into main Oct 10, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/issue-796-sparc-const-pair-return branch October 10, 2026 02:44
mahaloz pushed a commit that referenced this pull request Nov 13, 2026
…ktrap) (#1519)

AArch64 SLEIGH lifts `brk #imm` to a jump through a SoftwareBreakpoint user-op. Flow truncated that jump to a call followed by a return, so a function whose overflow or bounds check ends in a compiler trap printed as void, passed its return value to the trap, or returned a value made up from a call through it. Flow classification now replaces that jump with a no-return halt when the immediate is one a compiler emits for a trap that does not return: #1 (llvm.trap, Swift, and Darwin/Windows unreachable), #1000 (GCC), #0x5500-#0x55ff (llvm.ubsantrap), #0xc470-#0xc473 (arm64e pointer-auth failure) and #0xf003 (MSVC __fastfail).
The trap still prints as a SoftwareBreakpoint(..) statement. For __fastfail it also reads the failure code from w0, so `mov w0,#8; brk #0xf003` prints `SoftwareBreakpoint(0xf003,..,8);`. No other trap reads a register. Always on, with no option: a brk never returns to its caller. brk #0xf000, #0 and the Linux kernel's resumable immediates keep the old rendering. ptrauthtrap still removes the pointer-auth checks it matches, and a check it keeps now ends the flow. The fix lives in p2_lift/kuna_brktrap.rs and is covered by tests/stages/kuna-brktrap.xml.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] SPARC: an int function returning a constant prints its second argument shifted into the high word

1 participant