Skip to content

[AUTOMATED] Keep a stack array read after an indexed store from seeing the value written before it - #952

Merged
mahaloz merged 10 commits into
mainfrom
fix/fixidxalias
Oct 7, 2026
Merged

mahaloz merged 10 commits into
mainfrom
fix/fixidxalias

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A stack array element read after an indexed store into the same array prints the value written before that store. x86-64, gcc -O2:

printf 'int ext(int x);\nint t3(int i, int j) { unsigned char b[12]; b[0] = i; b[(i >> 4) %% 12] = j; if (b[0]) return ext(b[0]) + 1; return 5; }\n' > ia.c
gcc -O2 -c ia.c -o ia.o && kuna decompile ia.o t3
  v1[(a0 >> 4) % 0xc] = a1;
  if (!(char)a0)
    return 5;
  v1[0] = (char)a0;
  return ext() + 1;

The binary re-reads b[0] after the indexed store, so for i = 5, j = 0 it returns 5, while the printed C calls ext. The same happens for int, long, short and struct arrays, with clang, at -O0 and on AArch64. (ext() losing its argument is a separate issue.)

Fix

  • stackstoreguard (on by default) also guards a stack slot the function writes with any value, at each indexed stack store of any width that may reach it. Before, it only guarded constant-initialized slots against byte stores. Stores through alloca'd stack are skipped.
  • A store's reach is its value-set window cut to its pointer's own bound (known-bits masks, unsigned remainders by a constant, constant scales), or four elements with neither.
  • The function is decompiled again without the guard, as on main, when the final layout could make the C index past a local: a wider store's guarded slots outside one array of its width at any stack address its pointer may index from; a slot read only through the guard (so it has no local on main) ending an array an access or walk goes through; or a guarded pointer the layout checks cannot follow from the stack base (p = n >= 16 ? alloca(n * 8) : buf). To avoid the fallback, the layout folds such guard-only integer slots at an element boundary into the array below them.
  • The merge's call-crossing test, as upstream, skips the next block after every block a register's live range shares with it. Under stackstoreguard it also walks the guarded stores without the skip, so a register is not merged into a struct field across the loop that zeroes the struct.
  • option stackstoreguard off prints what main prints, including the array sizes arrayextent picks.

Tests

  • New tests/stages/kuna-indexedstoreguard.xml and kuna-indexedstoreguard-a64.xml (byte, int, long, short and struct arrays; slots past four elements; unbounded indexes with narrower reads; pointer walks over arrays and struct arrays; a pointer that is either alloca'd stack or a frame array; gcc -O2, clang -O2, gcc -O0, AArch64 clang -O2): 3/16 and 5/9 on main. New kuna-zeroloopfield.xml: two netlink-style requests zeroed by rep stos, whose field stores main prints before the loops. Unit test for the walk.
  • kuna-stackguard-tailcall.xml cleanup: Rust-first restructure + terminology refresh #3 had encoded the stale read; it now asserts the re-read.
  • make test 675/675 unchanged. decompile-all on 38 binaries (x86-64 gcc -O0/-O2, ARM firmware, PE i386; 32,718 functions): 66 change. Fixed: 6 stale reads (bzip2 BZ2_hbMakeCodeLengths -O0/-O2, OpenSSH b64_ntop, sftp_server_main, u-boot ubi_read_volume_table, betaflight quickMedianFilter5) and 3 struct fields stored after a zeroing loop that main printed before it (iproute2 ip -O2 0x2a3b0, 0x73c10, -O0 0x33e99). The other 57 print the same values (a field stored directly instead of through a temporary, copy placement, declaration comments, small arrays printed as one array instead of pointer arithmetic over separate locals).
  • Speed (decompile-all, min of 11, interleaved): total +0.84% over gzip -O2, kmod, grep -O0 and ls, +1.92% with iproute2 ip -O2 (gzip -O2 +3.67%, ip -O2 +3.38%, the others within ±1.4%). The cost is functions that fall back to main and so are decompiled twice: gzip huft_build and sub_ea40, ip sub_150d0, sub_4fb10 and sub_72110, whose guarded pointers the layout checks cannot follow.

Still wrong, as on main: a slot past four elements that an unlocked, unbounded index can reach (a remainder in multiply-and-shift form, AArch64 udiv/msub); the functions that fall back to main's output (gzip -O2 huft_build, grep -O2 build_state, dash -O2 command, p = n >= 16 ? alloca(n * 8) : buf); field stores both arms of a branch make after zeroing their struct, which the dominant-copy merge hoists above the branch (iproute2 ip -O2 0x44bb0); pointer walks that step down from the top of their array, or struct-array walks main already cuts into two arrays.

Found while working on #890.
Part of #890

🤖 Generated with Claude Code

https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39

@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 6, 2026
@mahaloz
mahaloz force-pushed the fix/fixidxalias branch 5 times, most recently from 92877cf to ece375c Compare October 7, 2026 11:56
mahaloz and others added 8 commits October 7, 2026 14:50
…g the value written before it

stackstoreguard also guards a stack slot the function writes, at each
indexed stack STORE of any width that may reach it (window cut to the
pointer's own bound, four elements without a bound or a locked window;
alloca stores skipped). The layout makes a wider guarded array as long as
its bound and checks it; the call-crossing merge test also tests the
guarded STOREs in the block its upstream walk skips, so a register is not
merged into a struct field across the loop that zeroes the struct. Stage
tests kuna-indexedstoreguard(-a64), kuna-zeroloopfield.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
… wider-element local

A wider store the C prints through the local at its base (one stack
address plus indices, no walk) must find an array there of its width or a
part of it covering its guarded slots, bounded index or not; otherwise the
guard is withdrawn and the function decompiled as with stackstoreguard
off. Inside the four elements of such a store with no bound, the narrower
unlocked integer hints are dropped (as in a widened array), so `b[1] = i;
b[j] = 3; return (short)b[1];` keeps `int v1[]` instead of a scalar and
`(&v1)[a1] = 3`. Stage asserts for u2/u5 (x86 gcc/clang -O2, AArch64
clang -O2); the VM-selector stage test is back to main's (its guard is
now withdrawn).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
…ray invariant

A byte store prints through a byte pointer into any integer array
(((char *)v1)[i]); withdrawing the guard for it dropped the byte family's
own fix (indexed_stack_stores k11u).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
…ss or walk goes through

Slots the guard alone keeps (read only through its INDIRECTs) get no hint
that ends an open range: element-aligned integer ones are dropped into the
range below them, and a final layout check withdraws the guard when the
local at an indexed access's or walk's base is a guard-only non-array or is
followed by a guard-only local. A walk whose base local is an array must
also hold every slot its guard keeps.

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

A guard-only hint is dropped into the open range below it only when that
range's element is wider than a byte: the byte reach checks take the
element type from the byte hints, so dropping one lost the sign of
`unsigned char b[12]`. New stage asserts: the arrays the stride-2 walks of
w5 and ws (gcc -O2, clang -O2, AArch64 clang -O2) go through keep their
length with the option on and off. The spec chapter and the option summary
describe the guard-only drop and the final layout check. Stages baseline
re-recorded for the new asserts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
…t slots

The walk clause of the wide-array invariant demanded that every slot a
walk's guard keeps lie inside the array at the walk's base. That withdrew
the guard from functions whose layout it never changed (iproute2 ip -O2
print_linkinfo: two separate 16-byte buffers, as on main), decompiling
them twice: ip -O2 took 7% longer.

The final layout check now covers walks alone: the local at the lowest
base of an indexed access or walk must not be followed by guard-only
storage up to and including the start of the next local. That also
catches a guard-only slot the merge mapped to a register (a walk over
`struct {long a; int b; int c;} s[4]` left `long v4[3]` below an
`int v5; // edi | stack - 0x30`) and one past a gap (`unsigned int
v4[11]` two bytes below a guarded `short`). Stage assert #15 now also
covers those two walks (gcc -O2 wp, clang -O2 wq).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
…as main

A slot a layout pass finds kept by the written-slot guard alone stays
guard-only for the rest of the analysis. Later passes may no longer see the
guard's INDIRECTs live while the COPY and MULTIEQUAL they left keep the slot
a local of its own, so the final check missed it: an AArch64 clang -O2 walk
over the .a fields of `struct { long a; char pad[6]; short z; } s[8]` left
`unsigned long v3[5]` below the guarded s[2].z, and the walk wrote past it.
New AArch64 stage assert #8 (gap2).

pointer_pieces bounds an index by an unsigned remainder only under
stackstoreguard; with the option off it uses the known-bits mask, as main,
so arrayextent sizes arrays as on main.

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

A guarded wider store whose pointer may also be alloca'd stack
(`p = n >= 16 ? alloca(n * 8) : buf; p[j % 4] = 3`) was checked by no
layout check, so the guard left buf as scalars and the C indexed past one
(`v1 = &v3; v1[a1 & 3] = 3`), wrong where main is right. A pointer that
comes from the stack base along a path pointer_pieces cannot resolve now
withdraws the guard, and a choice of stack addresses (two frame arrays, a
frame array or a pointer from a call or a global) is checked at every
stack address it may index from, as a single base is. Loop walks are left
to the final check.

New stage asserts: m8 with clang -O2 on x86-64 (#16) and AArch64 (#9)
store through the frame array, not a scalar, in both passes. Spec
chapters 03 and 06 and the option summary describe the check; stages
baseline re-recorded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
mahaloz and others added 2 commits October 7, 2026 15:54
…lowable walks

A guarded store through a choice of stack addresses with no index of its
own (`*p = x` for `p = c ? &a : &b`) needs one local holding the stored
bytes at each address, not an array there. A walk whose pointer the checks
cannot follow from the stack base (a walk over alloca'd stack or a frame
array) withdraws the guard like any other such pointer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
A walk that advances a pointer into a frame buffer by a variable amount
(`p = buf; ... p += len;`) is not a pointer pointer_pieces resolves, so
the previous commit withdrew the guard from every function with one:
iproute2 ip -O2 `print_linkinfo` and six others were decompiled twice
(ip -O2 +9%). variable_walk follows such a walk from its start; the final
layout check covers its array as it does a constant-step walk's, and a
walk that also starts from alloca'd stack still withdraws the guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
@mahaloz
mahaloz merged commit bb53264 into main Oct 7, 2026
2 of 8 checks passed
@mahaloz
mahaloz deleted the fix/fixidxalias branch October 7, 2026 18:17
mahaloz added a commit that referenced this pull request Oct 7, 2026
[AUTOMATED] 19 SYSCALLREGS-MIPS-A3 / SYSCALLREGS-PPC-CR0 keys added and
SYSCALLREGS-MIPS #1 renamed; no other key of main's baseline is missing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
mahaloz added a commit that referenced this pull request Oct 8, 2026
…d; let the watchdog stop it

Since #952, decompile-all on MIPS ld.so.1 never finished, with or without
--max-fn-seconds: the walk back along a guarded STORE pointer's definitions
(pointer_pieces, and the loop and choice tests beside it) visited every
path through a chain of nested MULTIEQUALs and additions, inside one
heritage pass, where the watchdog was never polled.

Each walk now remembers what every (Varnode, depth) gave, so it visits each
value once and answers exactly as before. The drive also publishes its
per-function deadline (decompile_drive::deadline_passed): the walks give up
once it has passed, the written-slot guard checks it before each address,
and the drive checks it again after the final layout checks, so such a
function becomes an error record.

New stages kuna-pointerwalk-budget-mips.xml (sub_d2f0 of the MIPS ld.so.1)
and kuna-pointerwalk-memo.xml (a pointer that resolves only past 4,096
values prints as main prints).

Part of #890

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
mahaloz added a commit that referenced this pull request Oct 8, 2026
…d so one function cannot hang a run (#973)

## Problem

Since #952, `kuna decompile-all` never finishes on angr-dev's MIPS `ld.so.1` (big- and little-endian). It runs at 100% CPU with no output, and `--max-fn-seconds` does not stop it:

```sh
timeout 120 kuna decompile-all binaries/tests/mips/ld.so.1 --max-fn-seconds 60   # angr-dev binaries
# exit 124, no output; 2ab3b82 (before #952) finishes in 23 s
```

`sub_d2f0` (and `sub_d23c` in the mipsel build) never completes.

## Fix

- The `stackstoreguard` walk back along a guarded STORE pointer's definitions (`pointer_pieces`, and the loop and choice tests beside it) remembers what each Varnode at each depth gave. These STORE pointers are built through a chain of nested MULTIEQUALs and additions; the walk visited every path, inside one heritage pass. It now visits each value once. The answer at a Varnode depends only on it and its depth, so the output is unchanged.
- `--max-fn-seconds` was polled only at action and heritage-loop boundaries, which that walk never reached. The drive now also publishes its deadline (`decompile_drive.rs (deadline_passed)`). The walks give up once it has passed, the written-slot guard checks it before each address, and the drive checks it again after the final layout checks, so a function that runs past it becomes an error record. `--max-fn-seconds 2` errors out `sub_d2f0` after 2.5 s.

## Tests

- New `tests/stages/kuna-pointerwalk-budget-mips.xml`: `sub_d2f0`'s bytes. Never finishes on main; about 7 s now.
- New `tests/stages/kuna-pointerwalk-memo.xml`: a pointer through five 8-way switches (clang -O2) that resolves only after more than 4,096 values prints `char v3[16]`, as on main.
- `decompile-all` on 61 binaries (the 47 binaries #952 was measured on, and angr-dev's MIPS, PowerPC and ARM `ld.so*` / `libc*`; 58,467 functions) prints what main prints, except the two hung functions, which now print what 2ab3b82 printed. The mips64 builds do not load, and the PowerPC and ARM libcs run past 360 s on 2ab3b82 too.
- Speed against main (`decompile-all`, min of 11, interleaved): total -0.35% (72.23 s -> 71.97 s) over gzip -O2 +3.60% (min of 5 re-run: +1.7%), kmod +1.30%, grep -O0 +0.69%, ls +0.01% and cp -O2 -1.82%. MIPS `ld.so.1`, which main never finishes, takes 24.2 s against 22.4 s on 2ab3b82 (min of 3).

Part of #890

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019c7W9nMmArWipVaBuMCy39
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