Skip to content

[AUTOMATED] feat(p5): synthesize struct types from constant-offset dereferences of a pointer parameter (structsynth) - #655

Merged
mahaloz merged 12 commits into
mainfrom
feat/structsynth
Sep 17, 2026
Merged

mahaloz merged 12 commits into
mainfrom
feat/structsynth

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

What this makes possible

A stripped binary keeps no record of the aggregate a pointer points at, so kuna
proves the pointee it can and stops there. Build a two-field record and read it:

struct word { const char *text; int length; };
__attribute__((noinline)) void put_word(struct word *w) {
    for (int i = 0; i < w->length; i++) putchar(w->text[i]);
}
int main(int argc, char **argv) { struct word w = { argv[0], argc }; put_word(&w); return 0; }
$ gcc -O2 -s -o /tmp/w /tmp/w.c
$ kuna decompile-all /tmp/w --addr 0x11c0
void sub_11c0(long *a0)
{
  if (1 <= (int)a0[1]) {
    do {
      v1 = (char *)(*a0 + v2);
      ...
    } while ((int)v2 < (int)a0[1]);

The layout is only in the reader's head, across every function that touches the
same object. With --option structsynth param:

void sub_11c0(struct_0 *a0)
{
  if (1 <= a0->field_0x8) {
    do {
      v1 = (char *)(a0->field_0x0 + v2);
      ...
    } while ((int)v2 < a0->field_0x8);

On ls -O2, hash_rehash recovers nine of the ten members of gnulib's
struct hash_table, each at its real offset.

The change

  • A P5 action (p5_types/kuna_structsynth.rs) walks the live LOAD/STORE ops
    once, peels each address to (base, constant offset) through COPY/CAST and
    constant pointer arithmetic, and type-locks the parameter to a completed
    struct_N *. Widest access wins a conflicting offset.
  • It fires only once the type lattice has stopped moving. ActionInferTypes
    returns 0 unconditionally, so nothing downstream can see the fixpoint; a
    plateau flag on the Funcdata is the signal.
  • Pointer-ness is never invented — the base must already be TYPE_PTR — and a
    named pointee (DWARF, a parsed declaration, a libc shell from libctypes, an
    earlier synthesized layout) always wins. Phis, non-constant offset terms,
    integer uses, stack and spacebase bases, and uniform-stride runs all decline.
  • The layout is pruned to one a C compiler reproduces byte for byte, then made
    dense and C-sized, because kuna decompile-project exports the declaration as
    source and the body names members the header has to declare.
  • It pairs with structdefs: --option structdefs on --option structsynth param
    prints struct struct_0 { long field_0x0; int field_0x8; char field_0xc[4]; };
    above the function, and carries it in decompile-all --json.

The default is off, on measured evidence

Speed passes (worst whole-binary delta +1.34% over six binaries, against an
inert control binary that moved +2.60% on the same run) and so do the corpora
under a temporary flip (675/675 datatest assertions unmoved; four stage
assertions move and all four are the intended field rendering). Two things say
no. Over the 444-slice type_match sweep the flip is worth one perfect function
(959 → 958, aggregate −0.049%), because the metric compares pointee spellings by
name and struct_0 * cannot intersect a ground-truth WORD *. And over a
15-binary, 5,431-function decompile-all sweep the type lock changes which
blocks the structurer duplicates, landing one more function on an emitter defect
that is already live in nine others: find -O2 sub_f620 emits
goto label_f752; with no such label. It is out of aggressive for the same
reason. docs/features/structsynth/ carries every number.

Tests

Three two-pass stage tests (structsynth-param-struct, -overlap-layout,
-hole-member): pass one is option structsynth off and the raw offsets, pass
two the fields. -overlap-layout #5 and -hole-member #4/#5 fail with the
layout prune and the hole filler ablated. 16 unit tests in
p5_types/kuna_structsynth/tests.rs cover each decline clause, the rounded
size, and the plateau latch.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29

@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 17, 2026
@mahaloz
mahaloz force-pushed the feat/structsynth branch 2 times, most recently from c88849b to b096d9e Compare September 17, 2026 03:42

@github-advanced-security github-advanced-security AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@mahaloz
mahaloz force-pushed the feat/structsynth branch 2 times, most recently from 40dcaff to 715a2c4 Compare September 17, 2026 07:20
@mahaloz mahaloz added full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) and removed full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) labels Sep 17, 2026
@mahaloz mahaloz changed the title [PROPOSAL][AUTOMATED] structsynth: synthesize struct types from constant-offset dereferences of a pointer parameter [AUTOMATED] feat(p5): synthesize struct types from constant-offset dereferences of a pointer parameter (structsynth) Sep 17, 2026
@mahaloz
mahaloz marked this pull request as ready for review September 17, 2026 15:39
@mahaloz
mahaloz force-pushed the feat/structsynth branch 2 times, most recently from d7e8596 to 6f04718 Compare September 17, 2026 17:24
mahaloz and others added 12 commits September 17, 2026 18:34
…references of a pointer parameter (structsynth)

A new P5 action (`p5_types/kuna_structsynth.rs`) collects the constant-offset
loads and stores made through a pointer parameter, prunes them to a layout a C
compiler reproduces byte for byte, and type-locks the parameter to the
synthesized `struct_N *`.  Behind `--option structsynth off|param`.

Rebased onto origin/main (foldcallretphi #654, ptrfromuse #659, libctypes
default-on #658); counters re-derived on the rebased tree.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
…rebase

Speed (worst +1.34% over six binaries against a +2.60% inert control) and the
corpora under a temporary default flip (675/675 datatests unmoved, four stage
assertions that are each the intended field rendering) both pass; the 444-slice
type_match perfect count misses by four-hundredths of a function (959 -> 958),
and the 15-binary decompile-all sweep turns up a bug hunk -- findutils find O2
sub_f620 emits `goto label_f752;` with no such label, one more function landing
on an emitter defect already live in nine others.  Default stays `off`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
…the others

The interleaved min-of-N `decompile-all` A/B that produced the speed block lived
in a scratch directory while the five other instruments in this bundle are
checked in; the numbers are not reproducible without it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
Rebase onto origin/main (structdefs, indirectonly). Every hard-coded count
re-derived from the freshly built tree with scripts.repipe.counters --rederive
--fix, phase_catalog.json re-captured, and docs/baseline-stages.json re-recorded
with SLEIGHHOME set -- no arithmetic anywhere.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
…ol and the structdefs pairing

The base now carries structdefs and indirectonly. Re-proves the default-off arm
byte-identical against a build of it, records the structdefs x structsynth
combination (the definition of the synthesized layout printed above the function
it was synthesized for), and states both readings of the accuracy criterion.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
…ew base

The census, the libctypes precedence witness, the aggressive-preset exclusion
and the dangling-goto delta all reproduce unchanged after the rebase.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
…s-glibc base

Real glibc layouts take no parameter this pass was taking -- the census is
byte-identical -- the off-arm stays byte-identical against a build of the new
base, and the hunk that decides the default reproduces unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
…the rebases

Both arms are byte-identical across the three bases on the identity-control
binaries, so the speed and typesweep blocks stand as measured; the census and
the deciding hunk were re-run on the current base anyway.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
Rebased onto origin/main (the foldcallret barrier, GH-657). Counters re-derived
on the freshly built tree, phase_catalog.json recaptured, docs/options.md
regenerated and docs/baseline-stages.json re-recorded so both sides' stage
assertions are present.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MAeimyWm5oSewpeCgAwY29
@mahaloz
mahaloz merged commit 2272403 into main Sep 17, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/structsynth branch September 17, 2026 19:15
mahaloz pushed a commit that referenced this pull request Sep 17, 2026
…on the structsynth base

Rebased onto origin/main 2272403 (structsynth #655). #655 adds a settable and
three stage files, so the counters move: 217 settables, tiers 68/86/63, 311
corpus files, fixture 217 and the comma-terminated catalog-row count 216, all
re-derived with `counters --fix` on the freshly built tree. The stages baseline
is re-recorded, not hand-merged: 1110 = main's 1100 + this branch's 10, with 0
assertions removed and every neighbour's keys intact (18 structsynth, 15
LIBCTYPES, 13 indirectonly, 8 foldcallret, 5 structdefs).

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

2 participants