Skip to content

[AUTOMATED] fix(p3): keep a narrow load's width when it is read through a wider-typed pointer - #677

Merged
mahaloz merged 3 commits into
mainfrom
fix/expandload
Sep 18, 2026
Merged

mahaloz merged 3 commits into
mainfrom
fix/expandload

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

The problem

A narrow field that the binary zero-extends into a wider argument is printed as a signed cast of the wider element when the pointer it is read through is typed wider. Compiled, the printed C passes a different value than the binary does.

cat > t.c <<'X'
struct S { unsigned int w[26]; unsigned short h; };
unsigned acc;
__attribute__((noinline)) void sink(unsigned int x) { acc = acc * 31u + x / 3u; }
__attribute__((noinline)) unsigned f(struct S *s) { unsigned r = s->w[0] / 5u + (s->w[1] >> 3); sink(s->h); return r; }
int main(void) { static struct S st; st.h = 0x9abc; return f(&st); }
X
gcc -O2 -o t t.c && kuna decompile-all ./t --functions f
unsigned long f(unsigned int *a0)
{
  unsigned long v1; // r8

  v1 = (unsigned long)(*a0 / 5 + (a0[1] >> 3));
  sink((short)a0[0x1a]);
  return v1 & 0xffffffff;
}

f does movzwl 0x68(%rdi),%edi, so sink gets 39612. (short)a0[0x1a] sign-extends: compile this f and sink gets 4294941372.

The fix

The tests

  • a_zero_extended_narrow_load_round_trips_through_the_printed_c (decompile_all_cli.rs) decompiles the committed gcc -O2 fixture expandload_zext_x86_64, compiles the printed f and checks that sink receives 0x9abc. It fails on main and passes here.
  • Across 46 binaries (25,038 functions), 163 functions change. Every one is this load: (int)p[k] becomes *(unsigned int *)&p[k], sometimes with the temporary that holds it narrowed too. docs/features/expandload/corpus-diff.txt sorts them by class. Call arguments, variables[] rows and type_match (10,748 scored functions) are unchanged. Datatests 675/675 and stages match their baselines.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG

mahaloz and others added 2 commits September 18, 2026 17:07
…gh a wider-typed pointer

RuleExpandLoad's truncation form accepted a loaded value whose type was still
undefined; the SUBPIECE it left printed as a cast to int of the loaded width,
so a zero-extended call argument read back sign-extended ((short)a0[0x1a] for
movzwl 0x68(%rdi)). The form now declines an undefined value, which keeps the
narrow load: *(unsigned short *)&a0[0x1a]. A value whose type is known signed
or unsigned still takes the truncation form (declining those too would move
kuna-tiedphitrim #13/#14, which are value-correct). Strict fix, no option.

Round-trip regression test on a committed gcc -O2 fixture: the printed f is
compiled and must hand sink 0x9abc. Spec 03 describes the decline.

Extracted from the protoorder branch (PR #669), without any protoorder code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
… over 12 and 46 binaries

classify.py sorts each function whose decompile-all code differs between a
main build and this branch; corpus-diff.txt holds the per-binary tables, the
hand labels for the 8 rows the script leaves, and the arity invariants.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: 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
…ch and speed measurements

Speed, interleaved min-of-15 whole-binary decompile-all, main vs branch:
fmt +2.5%, ls +0.4%, kmod +0.1%, bzip2 O0 +1.5%, find O2-noinline +0.8%.
type_match over 444 slices (10,748 functions): 959 -> 959 perfect, 0 moved.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
@mahaloz
mahaloz merged commit e1139df into main Sep 18, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/expandload branch September 18, 2026 18:42
mahaloz added a commit that referenced this pull request Sep 18, 2026
…fix now comes from main; fixture README rows; a lost-truncation main defect handed off

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
mahaloz added a commit that referenced this pull request Sep 18, 2026
…ly when every access agrees (+ #677 round trip under the flip, signfield fixture, probe)

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
mahaloz added a commit that referenced this pull request Sep 18, 2026
…ly when every access agrees (+ #677 round trip under the flip, signfield fixture, probe)

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
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