Skip to content

[AUTOMATED] fix: keep a narrowed argument's truncation at a call - #678

Merged
mahaloz merged 21 commits into
mainfrom
fix/truncarg
Sep 18, 2026
Merged

mahaloz merged 21 commits into
mainfrom
fix/truncarg

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

A call argument narrower than int that the caller zero-extends loses its truncation in the printed C. kuna prints the bare expression, or a byte it spells char, and C then promotes it to int, so the callee (which reads the whole register) gets a different value than the binary passes.

$ cat t.c
unsigned acc;
__attribute__((noinline)) void sink(unsigned x) { acc = x; }
__attribute__((noinline)) void f(unsigned v) { sink((unsigned short)(v * 3u)); }
__attribute__((noinline)) void g(unsigned char *p) { sink(p[3]); }
int main(void) { unsigned char b[4] = {0, 0, 0, 0x80}; f(0x7777); g(b); return acc; }
$ gcc -O2 -o t t.c && kuna decompile t f && kuna decompile t g
void f(short a0)
{
  sink(a0 * 3); // tail-call
}
void g(long a0)
{
  sink(*(char *)(a0 + 3)); // tail-call
}

The binary passes 0x6665 from f (movzx edi,di) and 0x80 from g (movzbl 0x3(%rdi),%edi). The printed calls pass 0x16665 and 0xffffff80. Upstream Ghidra prints the same promoted f.

Fix

  • When subvariable flow trims a call argument and every bit it drops is known zero, the trim is recorded on the call. That record is the proof that the binary zero-extended the value. At emission such an argument is cast to the unsigned type of its width, unless C's promotion already gives the zero-extension: sink((unsigned short)(a0 * 3)), sink((unsigned char)*(char *)(a0 + 3)). The code is in p9_emit/kuna_truncarg.rs.
  • A narrow argument without that record keeps its current form. clang passes a narrow parameter on untouched (mov %dil,acc8; jmp sinki), so the value there is whatever the caller extended, and sinki(a0) stays.
  • The check treats a one-byte value of unknown type as signed when realtypes prints it as char. No cast is added when the value provably fits the unsigned range ((a != 2) + 6, or a variable set to (x == 0x55) + 2). An argument that already prints as a truncating cast is retyped instead of cast twice.
  • There is no option, because the change only restores a conversion the binary performed. It applies to C output only, since Rust has no integer promotion. The spec prose is in docs/spec/09-emission.md and 03-ssa-and-simplification.md.

Tests

🤖 Generated with Claude Code

https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG

@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Sep 18, 2026
mahaloz and others added 21 commits September 18, 2026 22:03
…ion (WIP)

A one-byte value of unknown type prints as char under realtypes, which C
sign-extends, so a trimmed movzbl argument still printed a sign-extending
value. Promotion now sees that spelling. Also skip the cast when the
expression provably fits its unsigned range, and retype a truncation read
only by call arguments.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
… known-zero bits (WIP)

A narrow call input with no zero-extension evidence (a parameter clang
forwards untouched, relying on the caller's extension) keeps its promoted
form; the Rust output, which has no integer promotion, is left alone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
…e final build

A narrowed call argument is cast only where its trim dropped known-zero bits,
so a narrow parameter clang forwards untouched keeps its promoted form; Rust
output is left alone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
@mahaloz
mahaloz merged commit d03560b into main Sep 18, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/truncarg branch September 18, 2026 22:38
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.

1 participant