Skip to content

[AUTOMATED] fix(p5): a byte or halfword a function returns zero-extended was typed signed on ARM, PowerPC and RISC-V - #864

Merged
mahaloz merged 2 commits into
mainfrom
fix/arm-ldrh-return-signed
Oct 3, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fix/arm-ldrh-return-signed

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The problem

On ARM, PowerPC and RISC-V a function extends a narrow return value by the sign of its type. When it zero-extends one, kuna still types the value by its own arithmetic, often as short or char, and C then sign-extends it in every caller.

cat > next.c <<'EOF2'
unsigned short state16 = 0x7fff;
unsigned short Next(void) { return ++state16; }
int Scale(int bound) { return (bound * Next()) >> 16; }
EOF2
clang --target=arm-linux-gnueabi -O2 -fno-inline -c next.c -o next.o   # ldrh; add; strh; and r0,r1,#0xffff
kuna decompile-all next.o
short Next(void)
{
  short v1;

  v1 = dat_400054;
  dat_400054 += 1;
  return v1 + 1;
}

int Scale(int a0)
{
  return Next() * a0 >> 0x10;
}

Scale(100) returns 50 in the binary and -50 through the printed C. #833 shows the same thing for an ldrh reload.

The fix

  • When sub-variable flow narrows a RETURN to the value it zero-extends, it records this on the RETURN if the convention extends a value of that width by its type's sign. That means the compiler spec's inttype (ARM, PowerPC, SPARC) or the narrowext rule (RISC-V and LoongArch below 32 bits; MIPS and Apple arm64 under narrowext compiler).
  • Type inference then gives the returned value the unsigned type of its width. This happens only when the value's own type is signed or unknown, its sign bit can be set and every RETURN carries the record. Next now returns unsigned short, so Next() * a0 zero-extends.
  • A RETURN that kuna copies into another exit (block duplication, // return-dupe) keeps the record, so a PowerPC loop like unsigned short h_loop(int n, short *a) { short s = 0; for (...) s += a[i]; return s; } also prints unsigned short.
  • On x86 and AArch64 the bits above a narrow return are unspecified, so a zero-extension says nothing about its sign. Output there does not change.

What this does not fix: by default, MIPS keeps its old types, as narrowext already does for declared MIPS returns, because no ABI document states the extension. --option narrowext compiler applies the fix there. SPARC returns are still mangled by #796.

Tests

  • kuna-cli/tests/narrow_zext_returns.rs decompiles clang objects for ARM (A32 at -O0 and -O2, ARMv7, Thumb-2), PowerPC, RV32, RV64 and MIPS (narrowext compiler). It builds the printed callees, plus the ARM callers, with gcc and clang at -O0 and -O2, and compares them with the source over 28 inputs. On main it fails: u16_mul(-1) gives 0xfffffffffffffed4 where the source gives 0xfed4. If a duplicated RETURN drops the record, PowerPC h_loop gives 0xfffffffffffffffe for 0xfffe.
  • decompile-all over 15 stripped ARM firmware binaries (15,770 functions) changes 319 functions:
    • 274 return types become unsigned: 263 bytes and 11 signed halfwords.
    • 34 already-unsigned halfwords change spelling from unsigned short to uint2 in reliable mode.
    • 11 functions follow those types: store and local types, one shared struct field, and one global now printed by name.
    • With the return type, byte constants print as numbers ('\0' becomes 0, '0' becomes 0x30), and a few byte locals split in two. The values do not change.
  • Lua objects for PowerPC, RV32 and RV64, plus x86-64, i386 and other fixtures (2,669 functions), change 3 functions, each one lua_status now returning unsigned char.
  • Datatests stay at 675/675. CPU time (min of 11 interleaved runs, 6 binaries) moves between -1.9% and +0.3%, and the duplicated-RETURN change between -1.2% and +1.2% (3 binaries).

Fixes #833

🤖 Generated with Claude Code

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt

@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 2, 2026
mahaloz and others added 2 commits October 3, 2026 00:35
…ded was typed signed on ARM, PowerPC and RISC-V

Sub-variable flow trims a RETURN that reads ZEXT(x) to the narrow x, and the
type fold then types x by its own ops: an add or multiply reads as short, an
unknown byte as char. Where the calling convention extends a narrow return by
the sign of its type (cspec inttype, or the narrowext rule), that zero-extension
means the value is unsigned. The RETURN pull now records it and type inference
offers the unsigned type of that width for the returned value.

Fixes #833

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…rn record

CloneBlockOps copied only the upstream addl-flag subset onto a cloned op, so a
RETURN that block duplication copies into another exit lost kuna_zextreturn and
the function kept the signed type: a PowerPC -O2 loop summing shorts into an
unsigned short return still printed `short h_loop(...)`. The clone now carries
the record. The round trip gains that loop (h_loop) on every fixture.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/arm-ldrh-return-signed branch from f506962 to 0a2cd4a Compare October 3, 2026 00:39
@mahaloz
mahaloz merged commit 03fddeb into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/arm-ldrh-return-signed branch October 3, 2026 00:56
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 3, 2026
Brings in Noelo-Lab#795 (flowmode) and Noelo-Lab#864. Counts re-derived on a fresh build:
253 settables, tiers 81/105/67, 252 catalog rows, corpus 389; the stages
baseline is main's plus the three STACK STORE keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
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] ARM LDRH return inferred as signed short changes caller arithmetic

1 participant