Skip to content

[AUTOMATED] Keep a libc record a call fills whole: 17 more glibc layouts, on by default - #2145

Open
mahaloz wants to merge 17 commits into
mainfrom
feat/libclayout
Open

mahaloz wants to merge 17 commits into
mainfrom
feat/libclayout

Conversation

@mahaloz

@mahaloz mahaloz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

On an x86-64 glibc binary, a struct sigaction, pollfd, addrinfo or utsname local comes out split into unrelated locals, so the printed C writes a member into a variable the libc call never sees:

$ cat > t.c <<'EOF'
#include <signal.h>
int install(int sig, void (*h)(int)) {
  struct sigaction act;
  act.sa_handler = h;
  act.sa_flags = SA_RESTART;
  sigemptyset(&act.sa_mask);
  return sigaction(sig, &act, 0);
}
int main(void) { return install(SIGINT, SIG_IGN); }
EOF
$ gcc -O1 -o t t.c && kuna decompile t install
void install(int a0,unsigned long a1)
{
  char act [136]; // rsi | stack - 0xb8
  undefined4 v2; // stack - 0x30

  v2 = 0x10000000;
  *(unsigned long *)act = a1;
  sigemptyset((sigset_t *)&act[8]);
  sigaction(a0,(sigaction *)act,NULL);
}

With this change:

void install(int a0,void *a1)
{
  sigaction act; // rsi | stack - 0xb8

  act.sa_flags = 0x10000000;
  act.sa_handler = a1;
  sigemptyset(&act.sa_mask);
  sigaction(a0,&act,NULL);
}

The change

  • libctypes glibc lays out 17 more aggregates glibc publishes: addrinfo (whose ai_next points at its own type, so p = p->ai_next needs no cast), hostent, servent, pollfd, fd_set, msghdr, iovec, utsname, rlimit, rusage, itimerval, utimbuf, statvfs/statvfs64, stat64, and sigaction with its sigset_t mask. docs/features/libctypes/crosscheck.py checks every offset and size against the installed headers (173 members in 26 records, 0 mismatches).
  • The calls that fill them get prototypes (getaddrinfo, poll, select, uname, getrlimit, getrusage, recvmsg, ...), time_t * is long *, and __ctype_b_loc/__ctype_tolower_loc/__ctype_toupper_loc return unsigned short **/int **, so isalpha(c) reads (*v1)[c] >> 10 & 1 and not *(uint2 *)(*v1 + c * 2UL).
  • A laid-out record goes only where the accesses fit it. A frame slot the compiler reuses keeps the record only if what else it holds starts on a member of the same kind and width. A record held by value never lands on a value the program compares, adds or converts. A pointer stepping through the record must meet a member at each step. A frame address too near the frame's top is not typed as a record that would run past it, so select(n, (fd_set *)&mask, ...) on a one-word mask prints the local. The ctype table type goes only to values joined to the accessor's result.
  • A read or write through a libc record kuna names but does not lay out (sockaddr, obstack, termios, utmp, ..., and every record under opaque) is an access to an incomplete type, so it prints at its byte offset, never as p->field_0xN. ntohs(((struct sockaddr_in *)res->ai_addr)->sin_port) prints ((unsigned short *)pai->ai_addr)[1] (it printed pai->ai_addr->field_0x2), and the getc refill under opaque reads ((unsigned char **)f)[1] (it printed *(unsigned char **)&f->field_0x8).
  • glibc becomes the default. It applies only to an x86-64 ELF whose .dynstr names glibc. A static x86-64 image keeps the old named shells, and any other target gets no slots at all: 564 repo fixtures (32-bit, ARM, MIPS, PE, Mach-O, ...) decompile byte for byte as before. --option libctypes opaque keeps the named records without members, and off the void * tables.

On the decbench corpus (87,514 functions), functions with a perfect type score go 12,801 → 12,951 (+150 / −0), 803 score better and 28 worse, and casts go 463,202 → 453,421. field_0x accesses through libc types drop from 3,439 to 42, none of them through a record without members. With --option libctypes off every function prints as before. Instructions executed on eight whole-binary decompiles: +0.05% in total (each binary between −0.20% and +0.22%).

Tests

tests/stages/kuna-libctypes.xml passes 15–19 run two new fixtures. In passes 15/16 (libctypes_layouts_x86_64.c), off prints the bug in count_ai, install, readable, machine_len and alpha_count, and the default prints the member. In passes 17/18 (libctypes_slots_x86_64.c), three functions share frame slots: a one-word select mask, a stat frame reused for char *s, and a pollfd sharing a word with a double. Each keeps its own object in both passes. Passes 15, 16 and 19 (opaque) read a sockaddr_in and a sockaddr_in6 through ai_addr at byte offsets, the second 24 bytes in, past a struct sockaddr; a kuna-cli round trip compiles those two functions against the records kuna declares (and under opaque against <netdb.h>) with gcc and clang at -O0 and -O2, and runs them against the source. Unit tests cover the layouts, the self-referencing addrinfo, the sigset_t member, the ctype rows and the slot rules. Three CLI probes and two round trips pinned the old spellings. The library_names round trip now compiles the printed tp->tm_year against <time.h> and runs it.

Limitations

  • Where a slot or register holds a record and another variable whose every access starts on a member or falls inside an integer one, the two are still one variable of the record's type. dash's redirect reads its pipe descriptors as (int)buf.st_dev and ((int *)&buf.st_dev)[1]. In bzip2's compress the body is right (file_times.actime = ...; utime(..., &file_times)), but retVal has no name of its own.
  • The ctype table type still reaches the spill-slot locals that tar's from_header and dash's readtoken1 share with other values (4 functions; no parameter or return type).
  • A slot shared by a pointer and an integer member can still take the member's type: gcc -O0 keeps an iovec's iov_len and a char * in one word, which prints as unsigned long v7; ... v1 = *v7;. The fit check looks at the record's own accesses, not at the types of its members. No corpus function shows it.
  • Seven parameters that print char * without this change print unsigned long with it (27 move the other way). The char * came from a vote by a caller decompiled later, and the new layouts change which callers have voted by then. One is test_ciphersuite in gnutls-cli at -O2, whose calls with string literals gcc 14 rejects under -Wint-conversion.
  • 42 accesses in 9 functions read a gap of a laid-out record as field_0xN: the reserved words at the end of stat (0x78–0x88) and the padding at 0x24 in tm, read by whole-record copies. The printed definition declares those bytes as a _pad array, so these accesses do not compile against it.
  • No rows yet for getrlimit64, setrlimit64, fstatvfs64 or sigismember.
  • The fieldtype round trip pins libctypes opaque, because it compiles beside <stdio.h>.
  • In project exports a record tag can share its name with a function (sigaction, stat).

Model: anthropic/claude-opus-5-5

🤖 Generated with Claude Code

https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT

mahaloz and others added 6 commits December 4, 2026 17:08
…, default glibc (WIP)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
… rest, docs (WIP)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…outs in the spec and feature notes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
Three probes spelled the opaque FILE/option shells and the stat clamp
through a cast; under the default they are the published layouts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…glibc default

The catalog fixture is recaptured for the libctypes row. library_names'
stamp round trip compiles the printed tp->tm_year against <time.h> rather
than a field_0x stand-in, and the fieldtype round trip pins libctypes
opaque, since it compiles beside <stdio.h> and drops the opaque shells.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Dec 4, 2026
mahaloz and others added 11 commits December 4, 2026 20:17
…s and loop pointers

A frame slot takes a laid-out libc record only where what else the slot holds
sits on the record's members (p6 kuna_libcslot, called from
RangeHint::reconcile_with): a pointer on a pointer member, a float on a float,
a record on a member of its name, an integer starting on a member. A stat
frame reused for an argv array no longer prints the pointers as st_ino
subscripts.

A libc record vote or propagation onto a constant frame address declines when
the record would wrap past the top of the frame or leave the local range
(kuna_libcfit::outgrows_frame); the wrapped open hint used to swallow the
terminating hint and leave the slot without a symbol (&Stackffff...e8).

Propagation carries a record pointer onto a pointer that steps through it only
when every step meets a member of the access's width (cronie child_process),
and carries the ctype table pointer only onto a value loaded as one (tar
from_header's parameters and return).

utsname's domainname row is laid out under its GNU spelling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…ad rule

The table-read rule refused unsigned short ** and int ** on any value not
loaded as a table pointer, which also stripped a program's own int ** (the
globalpointee g_* functions printed void *a2). It now applies only to values
joined to a __ctype_*_loc result through copies, casts, phis and indirect
effects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…-slot assertions

Adds LIBCTYPES #52-#55 (passes 17 and 18) on top of the merged main baseline;
6290/6290 stage assertions pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…r member

A four-byte read of st_ino's high half (st.st_ino >> 32 at -O0) starts inside
a member, so the frame-slot rule made the merge concede and the 144-byte stat
collapsed to a 24-byte char array that stat() overflowed. An integer read
wholly inside an integer member is a read of the record's own bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
… its frame word

At -O2 coreutils tail passes one frame word to poll as an inlined struct
pollfd and to xstrtod/xstrtoumax as sleep_interval and n_units.
propagate_ref gave every value in the word the pollfd piece of the typed
address, and the record rode the phis into fourteen locals, one tested as a
condition. A laid-out record held by value now goes, by propagation or the
spacebase reference, only onto a value whose copy/phi web never reaches
arithmetic, a comparison, a float operation or an address. In the restructure,
a frame-slot concession on a libc misfit now holds for the rest of the merged
range, so the record's own open hint no longer takes the word back. The
pointer checks in kuna_libcfit::refuses also share one access walk.

Stage passes 17/18 gain `pace` (fixture rebuilt, same gcc), LIBCTYPES #56/#57.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…, within its extent

The persistent concession also fired after a pointer-vs-stat misfit that
the stat won, then forced the later 152-byte sigaction hint into
confusion; a confused range that is not a power of two collapses to one
byte, so a sigaction and a stat sharing a frame at -O0 printed as
`char buf[8]` and the round trip smashed the stack (battery t26). The flag
is now set only when the merge actually left an unknown, and a later record
concedes only inside that unknown's extent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
…as a member

A read through a pointer to a libc record the table names but does not lay
out (an incomplete type: struct sockaddr, obstack, termios, ... and every
record under libctypes opaque) printed as `p->field_0xN`, which compiles
against neither the real headers nor kuna's own. AddTreeState no longer
pretends a field sits inside such a record, so the access prints as a word at
its byte offset. Stage passes 15/16/19 and a kuna-cli round trip cover a
sockaddr_in read and a sockaddr_in6 read past byte 16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
Brings in main eb91b37. The stages baseline is re-recorded on the merged
tree (6388/6388) and the phase catalog fixture is recaptured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT
SplitDatatype rooted a word copy at a `utmp *` the table names but does not
lay out, so every piece was a hole and printed as `v10->field_0x2` (rsyslog
wallmsg). A split no longer roots at, or backs up into, such a record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0179iMKNAQaUNYkq7CbbU1yT

This branch has not been deployed

No deployments
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