Skip to content

[AUTOMATED] fix(libctypes): name libc structs only where their widths hold, and not for a struct that only starts with one - #706

Merged
mahaloz merged 14 commits into
mainfrom
feat/libcstructs-followup
Sep 21, 2026
Merged

mahaloz merged 14 commits into
mainfrom
feat/libcstructs-followup

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

The problem

libctypes puts a libc struct where the program has a different one, in two ways.

A struct whose first member is a libc aggregate gets typed as that aggregate, because both have the same address. grep's kwset starts with its obstack:

$ cat kw.c
#include <obstack.h>
struct kwset { struct obstack obstack; long words; long pad[290]; long trans; };
__attribute__((noinline)) void kws_grow(struct kwset *k, int n)
{ _obstack_newchunk(&k->obstack, n); k->trans = n; k->words++; }
int main(void) { static struct kwset ks; kws_grow(&ks, 1); return 0; }
$ gcc -O1 -o kw kw.c && kuna decompile ./kw kws_grow
void kws_grow(obstack *a0,int a1)
  _obstack_newchunk(a0,a1);
  *(long *)&a0[0x1b].field_0x28 = (long)a1;
  *(long *)&a0[1] = *(long *)&a0[1] + 1;

On a 32-bit image the struct widths, which are glibc's x86-64 ones, are too large, and the caller's frame object grows over its neighbours. i386 struct timespec is 8 bytes, so y and z become fields of it:

$ cat ts.c
struct timespec { long s, ns; };
int clock_getres(int, struct timespec *);
int res_sum(int a0) { int x = a0 + 1, y = a0 + 2, z = a0 + 3; struct timespec ts;
                      clock_getres(a0, &ts); return (int)ts.ns + x + y + z; }
void _start(void) { res_sum(1); for (;;); }
$ echo 'int clock_getres(int c, void *t) { return 0; }' > s.c
$ gcc -m32 -shared -nostdlib -Wl,-soname,libc.so.6 -o libc.so.6 s.c
$ gcc -m32 -O0 -fno-pie -no-pie -nostdlib -o ts ts.c ./libc.so.6 && kuna decompile ./ts res_sum
  timespec v1; // stack - 0x20
  v1._12_4_ = a0 + 2;
  v1._8_4_ = a0 + 3;
  clock_getres(a0,&v1);

The fix

  • Name nothing unless the image is an x86-64 ELF that is not linked against another C library. i386, ARM, AArch64, MIPS, PE and Mach-O get the plain void * tables, the same as libctypes off, and so does a name declared there with --define-function.
  • At each call, decline the table's struct type for the argument when the caller reaches it outside the struct: past its end, before its start, or stepped by more than a struct but not exactly one. The argument falls back to void *, and the call keeps a cast where one is needed (stat(v10,(stat *)v15) in wc, which walks &fstatus[i].st). Type propagation refuses the same type on the same evidence. This reuses the access walk protoorder already runs for its own votes.
  • Drop pthread_mutex_destroy: it scored nothing and its one argument is recovered anyway. pthread_mutex_init stays, because without it 12 calls lose their NULL argument.

After: void kws_grow(void *a0,int a1) with *(long *)((long)a0 + 0x970) = (long)a1;, and res_sum keeps x, y, z as its own locals.

The tests

tests/stages/kuna-libctypes.xml gains the kwset and mutex shapes, a control that keeps obstack *, and an i386 witness. Seven assertions fail without the fix. Unit tests cover the target gate, the fit rule and the name list.

444-slice typesweep: 1,353 → 1,353 perfect, mean .34149 → .34145, 0 improved / 1 worse (grep -O0 kwsprep, whose obstack macro locals only ever hold a copy of the kwset pointer), exported variable counts identical. Over 663 x86-64 binaries, functions that index a named libc pointer past element 0 go from 71 to 8 (the 8 are real struct sigaction arrays), and none of the 136 changed functions gains or loses a call. Classification: docs/features/libcstructs/followup-hunk-classification.txt.

🤖 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 21, 2026
@mahaloz
mahaloz force-pushed the feat/libcstructs-followup branch from 9c9f62d to 80beaf2 Compare September 21, 2026 21:29
mahaloz and others added 12 commits September 21, 2026 21:29
@mahaloz
mahaloz force-pushed the feat/libcstructs-followup branch from 80beaf2 to acc5bd5 Compare September 21, 2026 21:31
@mahaloz
mahaloz marked this pull request as ready for review September 21, 2026 22:00
@mahaloz
mahaloz merged commit 2da6198 into main Sep 21, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/libcstructs-followup branch September 21, 2026 23:33
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