Skip to content

[AUTOMATED] fix(p3,p6): a load of a global after a pointer store prints the value stored before it - #832

Merged
mahaloz merged 2 commits into
mainfrom
fix/storealias
Oct 3, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fix/storealias

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The problem

A global read back after a store through a pointer that may point at it prints the value stored before that store, a value read before such a store prints as a read of the global after it, and a load through the pointer prints after a later store to the global.

$ cat pa.c
#include <stdio.h>
int gi;
__attribute__((noinline)) int p1(int a, int *p, int k) { gi = a; *p = k; return gi / 16; }
__attribute__((noinline)) int keep(int *p, int k) { int a = gi; *p = k; return a; }
__attribute__((noinline)) int ord(int *p, int b) { int x = *p; gi = b; return x; }
int main(void) { int r = p1(1, &gi, -77); gi = 4; int s = keep(&gi, 8); gi = 3; printf("%d %d %d\n", r, s, ord(&gi, 9)); return 0; }
$ gcc -O2 -o pa pa.c && ./pa
-4 4 3
$ kuna decompile ./pa p1
  gi = a0;
  *a1 = a2;
  v1 = a0 + 0xf;
  if (0 <= a0)
    v1 = a0;
  return v1 >> 4;
$ kuna decompile ./pa keep
  *a0 = a1;
  return gi;
$ kuna decompile ./pa ord
  gi = a1;
  return *a0;

With p == &gi the printed functions return 0, 8 and 9. The same happens when the store is on a branch or in a loop, and a store to the global can print behind a pointer store or load the binary makes after it (gi = a; *p = k; touch(); printed *a1 = a2; gi = a0; touch();).

The fix

  • option indexaliasguard gets a level global, now the default (off|load|global|full; load was the default). Heritage puts an INDIRECT on a global at every STORE into its space, the global half of upstream's Heritage::guardStores. Read-only ranges and stores through the stack pointer are skipped, and a function stops guarding new ranges at 1024 INDIRECTs.
  • RulePropagateCopy does not move a stored value into one of these INDIRECTs, nor past a pointer load that may read the global (into a MULTIEQUAL, a call's INDIRECT, or the COPY that splitting a shared return block makes), so the store stays where the binary makes it.
  • A pointer load keeps its own variable while its value is live across a store to a global.
  • Heritage records which global ranges it guarded. The checks above act only on those, so a range past the 1024 budget prints exactly as at load.
  • elemptr's index walk follows an INDIRECT's first input, so an element pointer stored to a global keeps its type.

p1 now prints v1 = gi + 0xf; if (0 <= gi) v1 = gi;, keep prints v1 = gi; *a0 = a1; return v1; and ord prints v1 = *a0; gi = a1; return v1;.

Not fixed: a value computed before a pointer load and stored to the global after it still prints as stored ahead of the load (#871; it needs #825 first), a store before a pointer load is still dropped when the global is stored again (#825), and a function that returns the value it stores to a global prints void (#843).

The tests

  • kuna-cli/tests/store_alias.rs compiles the printed functions of gcc -O0, gcc -O2 and clang -O2 builds of storealias_x86_64.c (30 functions; 28 in the -O0 build, where [AUTOMATED] a store to a global before a pointer load is dropped when the global is stored again #825 drops the first store of w_dead and w_dead2) with gcc and clang at -O0 and -O2, runs them with every pointer aimed at the global and compares with the binary. Main fails the issue's shapes in every build. w_epi (store, then load, before a shared -O0 return), w_big/w_big2 (past the guard budget) and w_dead/w_dead2 (store, branch load, store again) guard against moving output that main prints right.
  • tests/stages/kuna-storealias.xml: 30 assertions, load against the default. No datatest assertion moved (675/675). PEMAIN and globalpointee now expect the read kept ahead of the store, simdlane and retsplitglobal pin load, and globalvalue Render unknown types as real C types (realtypes, DIV-6) #6 also accepts the widening printed with its product.
  • Against main ec8d0d1, 794 of 15,541 functions change on 22 stripped binaries and the goto count goes from 14,274 to 14,282. The classes, with functions checked against the disassembly, are in docs/features/storealias/footprint.md.
  • Whole-binary CPU time against main ec8d0d1, paired concurrent runs, median / geomean (N=11): cf2.elf +2.0% / +1.8%, gzip +3.6% / +5.3%, dash +3.1% / +2.5%, sort +2.1% / +2.0%. gzip again at N=15: +3.8% / +3.2%.

Fixes #792

🤖 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
mahaloz force-pushed the fix/storealias branch 6 times, most recently from f2e9945 to 0fb6364 Compare October 3, 2026 00:59
mahaloz and others added 2 commits October 3, 2026 01:50
…ts the value stored before it

At the new default `indexaliasguard global`, heritage puts an indirect_store
INDIRECT on a global range at every STORE into its space (up to 1024 per
function), so a load of the global after a pointer store reads the global, a
value read before the store keeps its own variable, and the store to the
global stays where the binary makes it. RulePropagateCopy keeps a global
store's COPY when a LOAD that may read the global follows it (also for the COPY
that splitting a shared return block makes of the global's MULTIEQUAL), and
kuna_loadorder keeps a pointer load ahead of a later store to a global. These
checks act only on a range the STORE guard covered; a range past the budget
prints as at `load`.

Not fixed: a value computed before a pointer load and stored to the global
after it (#871, which needs #825).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
794 of 15,541 functions change on 22 stripped binaries (goto 14,274 to
14,282). The sections on the computed-value checks are replaced by the
shared-return-block and guard-budget changes, with the functions each one
moves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz merged commit 511bd08 into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/storealias branch October 3, 2026 02:11
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 3, 2026
Brings in Noelo-Lab#872, Noelo-Lab#832 (indexaliasguard global, now the default), Noelo-Lab#875 (varargforward) and Noelo-Lab#877.
Heritage::guard keeps the stackstoreguard branch first and main's global STORE guard as the else
branch; spec 03 keeps main's global/full paragraphs and notes the stack guard also runs at global.
Counts re-derived (254 settables, corpus 392), catalog fixture recaptured, options.md regenerated,
stages baseline re-recorded (1795 = main 1792 + the 3 STACK STORE keys).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: 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 load of a global after a pointer store prints the parameter or constant stored before it

1 participant