Skip to content

[AUTOMATED] fix(p3/p6): dereference a pointer stored to a global through its own variable - #798

Merged
mahaloz merged 6 commits into
mainfrom
fix/global-pointee
Oct 2, 2026
Merged

mahaloz merged 6 commits into
mainfrom
fix/global-pointee

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

A pointer the binary stores to a global and keeps using from its register is printed as reads through the global, so the global's declared pointee type decides what is read.

cat > gptr.c <<'SRC'
#include <stdio.h>
char *gc;
__attribute__((noinline)) int p_int(int *p, int k) { int *q = p + k; gc = (char *)q; return q[1] + q[2]; }
int main(void) { int a[8] = {1, 2, 3, 4, 5, 6, 7, 8}; printf("%d\n", p_int(a, 2)); return 0; }
SRC
gcc -O2 -o gptr gptr.c && ./gptr        # 9
kuna decompile gptr p_int
int p_int(int *a0,int a1)
{
  gc = &a0[a1];
  return gc[2] + gc[1];
}

Compiled with char *gc as declared, that body reads two bytes and returns 0. It now prints a0 = &a0[a1]; gc = a0; return a0[2] + a0[1];.

Fix

  • Merge no longer joins a global with a register value that is used as an address (a load or store address, or a PTRADD/PTRSUB base, directly or through the adds that offset it) in the optional COPY and adjacent merges (p6_variables/kuna_pointeevalue.rs). This holds whatever the global's type: kuna never prints the global's declaration, and the type it infers for it comes from these same uses.
  • RulePropagateCopy keeps such a store's COPY out of the global's markers (p3_dataflow/kuna_pointeestorekeep.rs), so the forced merge at a call's INDIRECT or a join's MULTIEQUAL never takes the value.
  • A load of the global the binary makes after a STORE, or after a call with no INDIRECT on the global, keeps reading the global, whether or not the load is used as an address: gi = q; x = q[2]; *pp = p; return gi[1] + x; prints *a2 = a0; return v1 + gi[1];. A register copy of such a load is not moved past a later store and prints as its own variable (v2 = gi; *a3 = &a0[1]; return v1 + v2[1];), and RulePushMulti no longer replaces a global's join of two such stores with the stored values' join, which at -O0 made the load read a frame variable.
  • Not covered: a stored parameter, frame variable or constant, or a stored value never used as an address, keeps main's handling, where a load after a pointer store can still print the stored value ([AUTOMATED] a load of a global after a pointer store prints the parameter or constant stored before it #792).

Tests

  • tests/stages/kuna-globalpointee.xml: 15 assertions over gcc -O2 and -O0 fixtures, all failing on main; Fix test stages #9 to Analysis-tier: ARM Thumb decode end-to-end (linked fixture) #15 cover the loads after a pointer store.
  • kuna-cli/tests/global_pointee.rs: the printed functions of gcc -O0, gcc -O2 and clang -O2 builds are compiled by gcc and clang at -O0 and -O2 with every global declared as in the source, and must print what the binary prints. The g_ functions are called with the stored-through pointer aimed at the global itself. On main the gcc builds do not compile (return gr[3] + *gr;) and the clang build returns wrong values for g_alias, g_reload, g_write, g_before, g_twice, g_old and g_branch.
  • Datatests 675/675 unchanged. Over 66 O0/O2/O2-noinline binaries (37,064 functions) 188 functions change and none gains or loses an error: values used as addresses keep their own variables, stores print where the binary makes them, and loads after a pointer store read the global (tar, rsyslogd yylex). Speed (decompile-all, interleaved, min of 11): diff -1.5%, grep -3.8%, tar -O2-noinline +0.3%, dash -O0 -2.4%; a synthetic 1,000-store function that reloads the global around pointer stores, +2.5%.

Fixes #767

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU

@mahaloz
mahaloz force-pushed the fix/global-pointee branch from b7b267f to a8f9557 Compare October 1, 2026 20:42
@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 1, 2026
@mahaloz
mahaloz force-pushed the fix/global-pointee branch from 0624297 to e630053 Compare October 1, 2026 21:51
mahaloz and others added 6 commits October 1, 2026 23:46
…ugh its own variable

A register value stored to a global and also used as an address no longer
merges into the global, so a dereference through it never takes the global's
pointee type (`gc = &a0[a1]; return gc[2] + gc[1];` read bytes once `gc` is
declared `char *`). RulePropagateCopy keeps the store's COPY out of the
global's markers, and keeps a load of the global the binary makes reading the
global when it is used as an address; Merge refuses the optional copy-shadow
and adjacent joins. Stage test kuna-globalpointee.xml, round trip
kuna-cli/tests/global_pointee.rs over gcc -O0/-O2 and clang -O2 fixtures.

Fixes #767

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
…ading the global

A pointer stored to a global and kept in its own variable made every load of
the global after the store read the register: `gi = q; x = q[2]; *pp = p;
return gi[1] + x;` printed `return v2 + v1[1];`, which is wrong when `pp`
points at `gi`. RulePropagateCopy now leaves such a load reading the global
when a STORE, or a call with no INDIRECT on the global, lies between the store
and the load, and does not move a register copy of that load past a later
store; Merge and checkImpliedCover keep that copy as its own variable. A join
that still reads the global's value from before a store that became a COPY
after heritage counts as an earlier value read after the store, so yylex's
-O2 store keeps upstream's handling.

The fixture gains g_reload, g_write, g_before, g_diff and g_twice, called
with the stored-through pointer aimed at the global; the round trip and stage
assertions #9-#13 cover them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…n earlier value

At -O0 a pointer stored to a global in both arms of a branch let
RulePushMulti replace the global's MULTIEQUAL with the frame variable's join,
so the load after `*pp = p` read the frame variable; RulePushMulti now leaves
a global's join whose input is a kept store. A load after a pointer store also
stays on the global when an earlier value of the global is read after the
store through a register; only a heritage gap still keeps upstream's handling.
The fixture gains g_old and g_branch; stage assertions #14 and #15.

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

Re-recorded on origin/main 48f49b3 plus this branch: the 15 globalpointee keys are the only additions, none removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz
mahaloz force-pushed the fix/global-pointee branch from e630053 to ed93e4d Compare October 1, 2026 23:52
@mahaloz
mahaloz merged commit 5eb9733 into main Oct 2, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/global-pointee branch October 2, 2026 00:10
mahaloz added a commit that referenced this pull request Oct 2, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
The fixture is now the early-return shape whose decompile time #798
raised from 1.0 s to about 5 s, rather than one #798 never slowed, so
the probe's wall bound separates main from this branch by a wide margin
on both sides. It also pins the load through the global after the
pointer store, which the decompile printed as the register before #798.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 3, 2026
…mpile time grow cubically (#820)

## The problem

Since #798, a function that stores a pointer to a global many times, with other pointer stores in between, decompiles much more slowly, and the gap grows faster than the function. On main:

```sh
cat > gen.py <<'PY'
import sys
n = int(sys.argv[1])
print("int *gi, *gi2;\nint big(int *p, int k, int **pp, int **pp2) {\n  int x = 0; int *q;")
for i in range(n):
    print(f"  q = p + (k + {i % 7}); gi = q; x += q[{i % 5}]; *pp = p + {i % 3}; x += gi[{i % 4}]; if (x & {1 << (i % 8)}) {{ gi2 = gi; *pp2 = q; }}")
print("  return x;\n}")
PY
python3 gen.py 600 > s600.c && gcc -O2 -c s600.c -o s600.o
/usr/bin/time -f "user %U s" kuna decompile s600.o big > /dev/null
```

```
user 37.01 s
```

The build before #798 takes 15.48 s, and this branch 13.37 s.

## The fix

- The store walk #798 added (`old_value_read_after`) asked `dominates()` once per value of the global, each a walk up the dominator tree, and scanned every input of a join once per value the join reads. It now sets aside the values written at or after the store first, reads only the earlier values' readers, and reads each join among them once. A dominance question walks up the tree plainly for eight blocks, and only a longer walk goes through a memo for the store's block, so a long chain of blocks below the store is climbed once. One walk now costs time linear in the global's values, the earlier values' readers and the blocks of the dominator tree, but the rule pool still runs it once per reader, so the total is not linear. The order of the checks in `RulePropagateCopy` is main's.
- The stores #798 keeps are many COPYs of one register into one variable, and `markRedundantCopies` built the whole cover between every pair of them, with each step of a cover walk rescanning the block list to find a block by its index. Cover walks now find blocks through a table built once per walk. The pairs of one group share that table, the answers of long dominator walks and the variable's writes listed by block, and each pair's walk stops at the first write it meets. A cover block only grows during a walk, so the answer is the full range's.
- Nothing printed changes. Debug builds check the dominator memo against `dominates()`, the block table against a scan of the block list, and each pair's shortened walk against `check_copy_pair`. A build that ran the old store walk beside the new one on every call agreed on all of at least 1.3 million calls, over 13 stripped binaries and 234 small objects. `decompile-all` of 31 stripped binaries (18,953 functions: x86-64 at O0, O2 and O2-noinline, including bash, dpkg, dash and tar; ARM firmware) is byte-identical to main (b3d0e99), as are 233 small objects built by gcc and clang at -O0 and -O2 for x86-64, aarch64, arm, i386, mips, mipsel, ppc, ppc64le and riscv64.

## The tests

- Unit tests check `dominates_memo` and `dominates_within` against `dominates`, including chains longer than the plain walk, `add_ref_point_until` against the full cover, and the new pair loop against the old one. #798's and #758's stage tests and round trips pass unchanged.
- `tests/cli/repeated-global-pointer-stores-decompile-fast.json` decompiles a fixture with 200 stores, each followed by an early return, in under 3.5 s (min of 3). It also checks that the global is read after the pointer store, which the build before #798 does not print.
- CPU seconds, min / median of N runs, interleaved, on a shared machine. Before #798 is 954a5ef; main is 64d21a5. The early-return rows replace the generator's `if` with `if (x == 3*i+1) return x;`; the call-store rows replace `*pp = ...` with a call `ext(x)`. The switch rows store the pointer once and read through it, then switch over n cases that each store the global when `c == i` (gcc -O0); there the store walk itself took 0.41 s on this branch and 0.51 s on main when both were on 72d2e9b (timed inside the process, min of 10).

| case | N | before #798 | main | this branch |
|---|---:|---:|---:|---:|
| switch, n=300 | 15 | 3.76 / 4.14 | 4.76 / 5.37 | 4.52 / 5.19 |
| switch, n=600 | 7 | 14.99 / 15.53 | 25.82 / 28.06 | 24.93 / 28.88 |
| generator, n=300 | 11 | 2.52 / 2.96 | 3.94 / 4.22 | 2.46 / 2.76 |
| generator, n=600 | 5 | 14.63 / 15.03 | 37.72 / 38.83 | 12.79 / 13.53 |
| early returns, n=300 | 5 | 2.35 / 2.60 | 16.75 / 18.13 | 4.53 / 4.96 |
| call stores, n=300 | 21 | 0.47 / 0.48 | 0.60 / 0.62 | 0.54 / 0.57 |
| call stores, n=600 | 5 | 1.39 / 1.44 | 2.17 / 2.27 | 1.76 / 1.79 |
| probe fixture | 11 | 0.95 / 1.32 | 4.25 / 4.93 | 1.72 / 1.86 |
| bash, 11 largest functions | 11 | – | 48.50 / 52.52 | 46.75 / 54.42 |
| e2fsck, 11 largest | 11 | – | 15.34 / 16.93 | 14.10 / 17.10 |
| tar, 11 largest | 11 | – | 31.26 / 33.71 | 31.18 / 34.58 |
| dash, whole binary | 15 | – | 6.30 / 7.46 | 6.13 / 7.04 |
| rsyslogd, 10 largest | 15 | – | 13.63 / 14.31 | 13.52 / 14.66 |

- Not fixed: a function with hundreds of early returns runs the rule pool again for each return, and every pass still runs the store walk once per reader. The early-return row is still 1.9x the build before #798.

Fixes #818

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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] A pointer stored to a global is dereferenced through the global, in the global's pointee type

1 participant