Skip to content

[AUTOMATED] fix(p3,p6): a struct parameter split between registers and the stack is printed as overwritten - #863

Merged
mahaloz merged 2 commits into
mainfrom
fix/split-struct-param
Oct 3, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fix/split-struct-param

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

The problem

On ARM, MIPS and RISC-V, a by-value struct parameter whose fields span registers and the stack is printed as assigned to when the function reuses one of its registers, and copying its register fields to memory is dropped.

cat > d.c <<'EOF2'
struct s3 { int x, y, z; };
extern int take3(struct s3, int);
extern int use3(struct s3 *);
int d1(int *k, int a, struct s3 s) { return take3(s, *k) + a; }
int d4(int a, int b, struct s3 s) { return use3(&s) + a; }
EOF2
clang --target=armv7-linux-gnueabihf -O2 -c d.c
kuna decompile-all d.o --assert-strict \
  --assert 'typedef struct s3 { int x; int y; int z; };' \
  --assert 'prototype take3 int take3(struct s3 s, int k)' \
  --assert 'prototype use3 int use3(struct s3 *p)' \
  --assert 'prototype d1 int d1(int *k, int a, struct s3 s)' \
  --assert 'prototype d4 int d4(int a, int b, struct s3 s)'
  s.z = s.z;
  s.y = *k;
  v2 = s.y;
  v1 = s.x;
  v3 = CONCAT44(v2,v1);
  s.x = s.z;
  return take3((s3)CONCAT48(s.x,v3),s.y) + a;
...
  s.z = s.z;
  v1.z = s.z;
  return use3(&v1) + a;

d1 passes s unchanged in r0-r2 and *k in r3. d4 stores s.x and s.y next to s.z before passing &v1. With this change they print k_00 = *k; return take3(s,k_00) + a; and v1.z = s.z; v1.y = s.y; v1.x = s.x;.

The fix

  • A struct parameter split between registers and the stack binds to its symbol only at the function input, as a register pair has since [AUTOMATED] Preserve declared indirect-call prototypes and ABI #804 and as upstream does for every join. A later write of one of its registers is a new value.
  • Heritage splits a join into its pieces on the first pass that heritages any of them. On little-endian targets the first piece is the stack word, so the split waited for the stack pass; by then every read of the register fields was a bare input that nothing kept alive, so the stores of them were removed as dead.

Not fixed: when the function stores the register fields just below the stack part and passes pointers into the result to a call (gcc and clang do this when the parameter's address is taken), the stack part still prints as a separate local, and a value the call writes through a pointer is read back from that local (#861). AArch64 should never split such a struct (#859).

Tests

split_struct_params.rs decompiles the two functions above for ARM and MIPS o32, and a RISC-V lp64d struct held in a7 and the stack whose a7 is reloaded for a call. All five fail on main. decompile-all over 14 stripped binaries (4,315 functions) is byte-identical. Over 10 unstripped ARM firmware images, where DWARF supplies the prototypes, 3 functions change: fopencookie and _fopencookie_r lose functions.seek = functions.seek; lines and a field-by-field copy, and gyroReadSensorTemperature is the case above. Decompile-all time, interleaved min of 15: +0.9% (chibios with DWARF), -2.7% (nuttx), +0.7% (cat).

Fixes #814

🤖 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 2, 2026 23:48
…rs and the stack whole

WIP: heritage splits a mixed-space join at the first pass that heritages
any piece; every join parameter's symbol is restricted to the function input.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/split-struct-param branch from 5ebd1fd to c797915 Compare October 2, 2026 23:52
@mahaloz
mahaloz merged commit 6161580 into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/split-struct-param branch October 3, 2026 00:08
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 3, 2026
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 struct parameter split between registers and the stack is printed as overwritten

1 participant