Skip to content

[AUTOMATED] fix(p3): a 64-bit return whose high register is zero keeps its zero-extension - #794

Merged
mahaloz merged 2 commits into
mainfrom
fix/zext-return-sign
Oct 2, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fix/zext-return-sign

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

The problem

On a 32-bit target, a function that returns a 64-bit integer with its high word set to zero is printed as returning only its low word, typed as a signed int. A caller that widens the result then sign-extends it.

printf 'typedef unsigned long long u64;\nu64 ins16(unsigned a, unsigned short b) { return ((a + 1) << 16) | b; }\n' > ins16.c
clang --target=arm-linux-gnueabi -O2 -c ins16.c -o ins16.o   # orr r0,r1,r0,lsl #16; mov r1,#0; add r0,r0,#65536
kuna decompile-all ins16.o
int ins16(int a0,unsigned int a1)
{
  return (a1 | a0 << 0x10) + 0x10000;
}

ins16(0x7fff, 0) is 0x80000000, but through the printed prototype (unsigned long long)ins16(0x7fff, 0) is 0xffffffff80000000. MIPS and PowerPC little-endian, RISC-V 32 and i386 (xor %edx,%edx; ret) print the same int, and u64 f(int a, int b) { return (unsigned)(a + b); } prints int f(...) { return a1 + a0; }.

The fix

  • Sub-variable flow no longer trims a RETURN that reads a 64-bit integer held in two output registers: a value of at most eight bytes, wider than any single integer output register of the prototype model, with its high word where the register file's byte order puts it. That covers the joins r1:r0, $3:$2 and EDX:EAX, and SPARC's o0_1, a register that names both halves. With the high register zero the RETURN reads ZEXT(lo), and RuleSubvarZext used to cut it back to the low register.
  • The function now prints unsigned long long ins16(int a0,unsigned int a1). A word whose C type is signed gets the cast that makes C zero-extend it: return (unsigned int)(a1 + a0);.
  • 16-byte pairs (x86-64 RDX:RAX) keep the trim, because a zeroed RDX there is almost always -fzero-call-used-regs after an int function. Where a 32-bit function's bytes are exactly those of a zero-extended return, it now reads unsigned long long though the source returns a word: an i386 int function scrubbed that way, gcc -O0's int f(u64 a) { return a >> 32; }, or ChibiOS's chEvtGetAndClearEvents, which zeroes r1 for msr basepri before returning. The value is unchanged, and --option returnpair single gives the word back.

What this does not fix:

Tests

  • tests/stages/kuna-zextpair.xml (ARM): 5 of 6 assertions fail on main. The control passes on both.
  • kuna-cli/tests/zext_pair_returns.rs builds ARM, MIPS and PowerPC little-endian and i386 objects. It compiles the printed C, callers included, with gcc and clang at -O0/-O2 and compares it with the source over 289 input pairs (544 wrong values on main for ARM). It also checks under -m32 that every return is eight bytes on ARM, i386 and SPARC.
  • In passthrough_own_return.rs, the forwarder bl provider; mov r1,#0 now returns unsigned long long.
  • decompile-all changes no function over Lua 5.3 built for nine 32-bit targets (5,015 functions, including SPARC, ARM hard-float, MIPS and PowerPC big-endian). Over 15 decbench ARM firmware and i386 PE binaries (14,181 functions) it changes 7: one 64-bit return now right (betaflight, lsrs r0,r0,#8; movs r1,#0), the ChibiOS function above and four of its callers (variable numbering only), and one stretch of data decoded as code. Datatests are unchanged at 675/675. CPU time (min of 21 to 31 interleaved runs, five witnesses) moves between −4.9% and +1.4%.

Fixes #769

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU

@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 1, 2026
@mahaloz
mahaloz force-pushed the fix/zext-return-sign branch from 83404e7 to f97f46d Compare October 1, 2026 19:03
mahaloz and others added 2 commits October 2, 2026 00:12
…s its zero-extension

On a 32-bit target a function returning a 64-bit integer whose high word it
sets to zero (ARM `mov r1,#0`, MIPS `$3 = 0`, i386 `xor %edx,%edx`) was
printed returning its low word, typed by that word's own operations:
`u64 ins16(unsigned a, unsigned short b) { return ((a + 1) << 16) | b; }`
printed as `int ins16(int a0,unsigned int a1)`, so a caller widening the
result sign-extended what the binary zero-extends (0x80000000 became
0xffffffff80000000), and `(unsigned)(a + b)` lost its zero-extension.

Return recovery joins the pair and the RETURN reads ZEXT(lo); dead-code
analysis counts only its possibly-nonzero bits as consumed, so RuleSubvarZext
trimmed the RETURN to the low register. SubvariableFlow::try_return_pull now
refuses to trim a value of at most eight bytes that is wider than every single
integer output register of the prototype model and laid out as one integer in
the register file's byte order: a join such as ARM r1:r0, or a register naming
both halves such as SPARC o0_1. The function returns `unsigned long long` and a
signed word is cast to `unsigned int` before the widening. A pair joined
against the register order (big-endian pairs on main, which #763 reorders) and
a 16-byte pair (x86-64 RDX:RAX, where a zeroed RDX is usually
-fzero-call-used-regs after an int function) keep the trim. Functions whose
bytes are a zero-extended return but that mean a word (i386
-fzero-call-used-regs, gcc -O0 `int f(u64 a){return a>>32;}`) now print as
`unsigned long long`; `--option returnpair single` restores the word.

Tests: stage test kuna-zextpair (5 of 6 assertions fail before the fix, the
control passes on both); kuna-cli round trip zext_pair_returns compiling the
printed C for ARM, MIPS, PowerPC little-endian and i386 objects, callers
included, against the source with gcc and clang at -O0/-O2 (544 wrong values
on the ARM build before the fix), plus a return-width check under -m32 for
ARM, i386 and SPARC (MIPS and PowerPC name an eight-byte integer `long`, so
each function must be declared like the 64-bit control); passthrough_own_return's
`bl provider; mov r1,#0` forwarder now returns `unsigned long long`.
decompile-all over 99 ARM firmware and i386 binaries (45,490 functions), 38
x86-64 binaries (14,836) and zlib for seven 32-bit targets (1,493) changes no
function; datatests 675/675 unchanged.

Fixes #769

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…d base

Re-recorded on origin/main 5eb9733 plus this branch: the 6 ZEXTPAIR keys are the only additions, none removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz
mahaloz force-pushed the fix/zext-return-sign branch from f97f46d to 0792c7b Compare October 2, 2026 00:21
@mahaloz
mahaloz merged commit 363123e into main Oct 2, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/zext-return-sign branch October 2, 2026 00:36
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
…ifted, not its return form

With the zero-extension of a 64-bit return kept (#794), sel_const prints as a
64-bit return of its zero-extended 10 or 20 instead of two 32-bit returns, so
the assertion now rejects the old join's shift rather than requiring one
spelling. The header describes main's output as it is now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
sjkim1127 added a commit to sjkim1127/kuna that referenced this pull request Nov 19, 2026
… honest bounds

- Return values: compare only the bytes the physical ABI defines for the
  claimed type; a width or signedness verdict needs a typed contract (fixture
  source, DWARF). Noelo-Lab#794 is a DIFFERS against its source's u64 contract.
- Inputs: both sides start from the same raw state; an undeclared live-in is
  PROTOTYPE_SUSPECT, not a poison-induced DIFFERS.
- Calls: stubs write reachable memory and the decompiled side replays it;
  Tier S models calls as (regs, memory) -> (regs, memory) and never encodes a
  function Tier C could not fully execute.
- The Funcdata interpreter is new work; EmulateFunction's stores are no-ops.
  Unsupported ops give UNKNOWN(unsupported-op).
- EQUIVALENT(<=k) covers inputs on which both sides finish within the bound;
  one-sided overruns are replayed, not dropped. Unreplayable sat models are
  separated from encoder bugs.
- Localization reports a 1-minimal candidate set; log2 k only for a single
  culprit.
- Declared divergences are predicates (option, arch, observable, pattern).
- Slices: C1 pure integer, C2 memory and calls, C3 localization, C4 corpus.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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] A zero-extended 64-bit return narrowed to its low word is typed signed int

1 participant