Skip to content

[AUTOMATED] feat(analysis): libctypes names 7 more libc structs - #692

Merged
mahaloz merged 37 commits into
mainfrom
feat/libcstructs
Sep 21, 2026
Merged

mahaloz merged 37 commits into
mainfrom
feat/libcstructs

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

The problem

libctypes names a libc struct only where the image IMPORTS the function that
declares it. gnulib's obstack is linked into the program instead, so the widest
aggregate in the corpus's ground truth after FILE — 431 pointer variables — is
never named, and the declaration that would have named it is in the binary's own
symbol table. Sixty-odd other libc slots are simply missing from the table.

/usr/bin/grep on any glibc box, on main:

$ kuna decompile-all /usr/bin/grep | grep -E '_obstack_newchunk'
void _obstack_newchunk(struct_14 *a0,unsigned long a1) // ternary
      _obstack_newchunk(a0,0x20);
      _obstack_newchunk(a0);          <- one argument; the second was never recovered

After: void _obstack_newchunk(obstack *a0,unsigned long a1), with both arguments
at every call (_obstack_newchunk(a0,v63)).

Two defects of this PR were found after it merged, and #706 fixes both. It
typed grep's kwsprep/kwsincr (sub_f210/sub_ed20) as taking obstack *,
but they take kwset_t, a struct whose first member is the obstack. And on
32-bit images the x86-64 widths grow the caller's frame object over the locals
beside it.

The fix

  • Seven named aggregates, sized from sizeof/_Alignof and cross-checked
    against the corpus's own DWARF: obstack 88, spwd 72, utmpx 384,
    utmp 384, re_pattern_buffer 64, lconv 96, statfs 120.
  • 61 new slots: the rest of the stream surface, the three account databases,
    more tm/timespec/termios, the regex entry points. Signatures come from
    gcc -aux-info over the installed headers; libctypes off stays
    byte-identical to main.
  • The five _obstack_* entry points are the one thing matched against a name the
    image DEFINES (the reserved half of obstack.h). Two tables, because gnulib's
    size type is size_t and the installed glibc header says int.
  • Left out on purpose: utimensat/futimens/utimes/futimesat (the slot is a
    two-element array, and naming one element split gzip lutimens's stat), and
    sigfillset plus five other sigset_t slots (each splits a struct sigaction
    handed &sa.sa_mask).

The tests

Six assertions on two new fixtures in tests/stages/kuna-libctypes.xml, plus unit
tests over the table invariants. 444-slice typesweep: 1,349 → 1,353 perfect, mean
.3405 → .3415, 71 improved / 1 worse, exported variable counts identical. Counts
of the two renderings main already prints that reach a few more calls are in
docs/features/libcstructs/analysis.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_017ei9zqc7XHgZdMAzrk8jAL

@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 20, 2026
@mahaloz
mahaloz force-pushed the feat/libcstructs branch 3 times, most recently from 147e93f to 8bad3c1 Compare September 20, 2026 17:52
@mahaloz
mahaloz force-pushed the feat/libcstructs branch 3 times, most recently from 7b9e785 to bc2653d Compare September 21, 2026 17:09
mahaloz and others added 22 commits September 21, 2026 18:22
Mined the libc/POSIX-named ptr_struct ground-truth pool from the DWARF twins
of the 444-slice corpus and added the slots that pay.

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

gnulib's copy of obstack.c declares its size type size_t; the installed glibc
header declares int. The table asserted size_t for both, so an image that
IMPORTS _obstack_begin/_obstack_newchunk from glibc (the five dpkg programs at
each optimization level, 15 slices of the same results tree) got a false
declaration, and the caller of a two-line glibc-obstack program went from
`void f(int n)` to `void f(unsigned int n)` with two inserted casts.

Split the channel: LIBC_DEFINED_NAMED (size_t) is seeded only from the names
the image defines and does not also import, LIBC_IMPORTED_OBSTACK (int) from
the import channel by name and by resolver address. Corrects the module and
spec prose that claimed obstack is never an import.

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

The single worse typesweep row said "a by-value struct stat local that becomes a
byte blob". What it actually is: char v5 [80] handed to an lstat that writes 144
bytes, with st_atim/st_mtim detached into locals the body reads and nothing
writes. State that, say what causes it (the timespec * slot on
utimensat/futimens -- reverting those two slots brings stat v5 back), and record
the measurement it is kept on: over the 30 corpus slices that import either
name, 122 functions gain a timespec rendering and this is the only new
under-declared stat, against 7 that main already prints in those same slices.

Also: rebased onto argclobber (#689), stages baseline re-recorded, PR body cut
under a screen, and the statfs/export redeclaration noted beside the option's
documented export cost.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
libctypes_obstack_import_x86_64 is the twin of the existing obstack fixture on
the other channel: _obstack_newchunk imported from glibc rather than linked in
from gnulib. Two assertions, off and on, pin `void grow_import(obstack *a0,int4
a1)` against #28's `uint8` -- a table that spelled both channels the same fails
one of them.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
The 444-slice typesweep was run again in full after the rebase onto argclobber
(#689), base arm built from origin/main's kuna_libctypes.rs + protos/mod.rs in
this tree. Identical to the first run down to the per-variable tables: 1,349 ->
1,353 perfect, mean .3403 -> .3417, 147 newly correct / 6 newly wrong, 91
improved / 1 worse. Also completes the newly-correct breakdown, which was
listing 139 of the 147.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
min-of-9 interleaved `kuna functions` over -O2 tar: 0.144s before the extra
defined-name walk, 0.142s after.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
decompile-all over five whole binaries, the two builds differing only by the
split: grep/tar/ls/gzip byte-identical, dpkg-query moves 7 lines.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
Nothing outside protos/mod.rs reads it now that the obstack seam takes the
defined-name set.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
The `sigfillset(sigset_t *)` slot gives the `sa_mask` sub-object its own
identity, so a 152-byte `sigaction` frame slot splits at 136 and the
`sa_flags` store lands outside the object handed to `sigaction()`.

Measured by ablating that one table row: no `sigaction` declaration moves
anywhere in the 444 scored slices (6 of them import `sigfillset`), and in a
disjoint openssh -O2 sweep exactly one function per binary changes, all of
them `ssh_signal`. The shape already ships through `sigemptyset`, which
prints the identical split on the ablated build.

Also records the `statfs` name collision as it appears in plain
`decompile-all`, not only in a project export.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
mahaloz and others added 15 commits September 21, 2026 18:22
The mitigation claim is only checkable with the program it was measured on.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
Third full run of both arms, this time at 69d252d. The perfect counts, the
improved/worse split and every per-variable table are unchanged; only the
pooled mean moves, 0.3403 -> 0.3405 off and 0.3417 -> 0.3419 on, which is #694
lifting both arms alike.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
…e limit, the re-derived fixture swap

Adds three sections to the feature analysis: the utmp/utmpx rows reaching a
caller's own local on who/logoutd/runlevel (they ship on a pool count, not a
sweep delta, and this is what they buy); the table's sizes being glibc x86-64
with nothing scaling them by word size, and what that is confined to; and the
export fixture swap re-derived under #693's callee-first schedule.

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

The gate block now names the base it was run on, and a new section records how
the two repointed fixtures were re-derived under that schedule, the nine-binary
`libctypes off` identity against a build of the new base, and why the type sweep
did not need re-running (`decompile-all --json` is byte-identical between the
two bases). The timespec rows are recorded as a decision rather than a
disclosure.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
It splits a struct sigaction frame slot in openssh's ssh_signal and buys
nothing in the mined pool.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_017ei9zqc7XHgZdMAzrk8jAL
…igset_t sub-object rows

utimensat/futimens/utimes/futimesat take a two-element array; naming one
element shrinks the caller's frame object (gzip lutimens reads a stat tail
nothing writes; scp/sftp hand utimes 16 of 32 bytes). sigsuspend,
pthread_sigmask, sigdelset, sigismember and sigwait split a struct sigaction
local when handed &sa.sa_mask.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_017ei9zqc7XHgZdMAzrk8jAL
@mahaloz
mahaloz merged commit eaa19eb into main Sep 21, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/libcstructs branch September 21, 2026 18:55
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