Skip to content

[AUTOMATED] Name locals after the library calls they meet (libcnames) - #1102

Merged
mahaloz merged 6 commits into
mainfrom
fix/libcnames-names
Oct 14, 2026
Merged

mahaloz merged 6 commits into
mainfrom
fix/libcnames-names

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Part 1 of the #1098 proposal (#1076): a local that holds a library call's result, or is handed to one whole, prints the library's name for it, as a second name for the number it keeps.

What this makes possible

// two.c
#include <fcntl.h>
#include <unistd.h>
long copy_file(const char *from, const char *to) {
    int in = open(from, O_RDONLY), out = open(to, O_WRONLY | O_CREAT, 0644);
    char block[512];
    long total = 0;
    ssize_t got;
    while ((got = read(in, block, sizeof block)) > 0)
        total += write(out, block, got);
    close(in);
    close(out);
    return total;
}
int main(int c, char **v) { return (int)copy_file(v[1], v[2]); }

gcc -O2 -fno-inline two.c -o two && kuna decompile ./two copy_file on main:

  v1 = open(a0,0);
  v5 = 0;
  v2 = open(a1,0x41,0x1a4);
  while (v3 = read(v1,v4,0x200), 1 <= v3) {
    v5 += write(v2,v4,v3);

With this PR (--mode auto picks aggressive here, which turns the option on):

  fd = open(a0,0);
  v5 = 0;
  fd_2 = open(a1,0x41,0x1a4);
  while (n = read(fd,v4,0x200), 1 <= n) {
    v5 += write(fd_2,v4,n);

v4 and v5 keep the numbers they have with the option off, so a directive reaches the same local in every mode, by either name: name fd src and name v1 src both rename the first descriptor, under --mode reliable too.

The change

  • The libc tables carry glibc's declared parameter names (leading __ stripped, read from the headers by docs/features/libcnames/gen_names.py; 554 of 567 entries) and a result name where the result is one kind of thing (fd, n, len, p, fp, pid, tp, ...; 91 entries). They ride on the parked prototype but outside innames, so callee signatures and name locks are unchanged.
  • New P6 option libcnames. The evidence is a return value first (only when every library result the local holds agrees), then a whole argument whose name says what the value is, then a role name (dest/src, s1/s2), but only when every role name agrees. A name equal to a type the code prints (tm, FILE) or a C or Rust keyword is skipped. Clashes take _2, _3 in call order, past every printed name, globals included.
  • The naming walk numbers every local as with the option off. Only then does a candidate print its library name, so the replay record and stable numbering only ever see numbers. A local whose library name a rename took prints its number and stays addressable (name v5 n leaves read's result v3, and name v3 got reaches it). A name a rename vacates goes to no one.
  • Default off: forced on, 0/675 datatest assertions move, but six stage assertions of other features do, each spelling a vN this names (strlen(v7); becomes strlen(s);). They are KUNA-CALLPLACEHOLDER Port to Rust #2, elemptr Recover PLT/GOT import names in ELF decompilation #4, fieldtype Recover PLT/GOT import names in ELF decompilation #4, Stage reorg #5 and infra: dev container + cross-arch toolchains #12, and FMTSTATIC Render unknown types as real C types (realtypes, DIV-6) #6. --mode aggressive turns it on, so the web page and --mode auto under 500 KiB get it.

Tests

  • tests/stages/kuna-libcnames.xml: the shipped default, the option, and three console renames (fd, onto read's n, then that local by its number).
  • kuna-cli library_names, on both output surfaces:
    • renames and retypes move no other local, a retype is applied (and reported applied) by the library name or the number (type fd unsigned int, type v1 unsigned int), and a rename to a number-shaped name (name fd v9) is kept;
    • a directive reaches the same local in auto, aggressive and reliable;
    • renaming onto a library name and then renaming the displaced local;
    • a global named like a library name (pid_2, addressable);
    • access's type in C and Rust;
    • tp beside the typedef tm, compiled and run against the source with gcc and clang.
  • The wasm and browser suites rename onto len on the page, rename the displaced local by its number, and switch to Reliable and back with every edit still applied.
  • Round-trip tests and seven CLI probes of other options that pinned such a vN read the new name. The round trips still compile and run the printed C. The ghidra-sim differential runs its CLI side with the option off, as it does formatstring.
  • Over 14 stripped decbench binaries (--mode aggressive), 968 of 10,106 numbered locals print a library name, in 546 of 3,297 functions. Every changed function differs only in those names, and every other local keeps its number. Of 415 named locals located in the source, 91 carry the source's name and 324 another spelling of the same meaning (fp for inStr, tp for tm); none is misleading.
  • Speed, decompile-all min of 11 interleaved against main 151c35d: +0.00% under --mode aggressive, -0.42% under --mode reliable, and +0.59% under --mode reliable with directives (noise).

🤖 Generated with Claude Code

https://claude.ai/code/session_011ib7dvKSnoqFMNwr6Wu6dm

@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 14, 2026
@mahaloz
mahaloz force-pushed the fix/libcnames-names branch 3 times, most recently from 28e2eac to 12c94c3 Compare October 14, 2026 22:20
mahaloz and others added 5 commits October 14, 2026 22:35
A local that holds a library call's result, or that is handed to one
whole, kept a vN although kuna's own libc tables type the call:
`v1 = open(a0,0); v3 = read(v1,v4,0x200);`.

The libc tables now carry glibc's declared parameter names (leading
underscores stripped, read from the headers by
docs/features/libcnames/gen_names.py) and a conventional result name
where the result is one kind of thing (fd, n, len, p, fp, pid, tp, ...),
on the prototype they park but outside innames, so the callee's
signature and name locks do not change. The new P6 option libcnames
(default off, on under --mode aggressive) prints a numbered local under
the name of its strongest evidence: a call's return value (when every
library result it holds agrees), then a whole argument whose name says
what the value is, then a role name (dest/src, s1/s2) only when every
such name agrees. Names equal to a printed type or a C or Rust keyword
are skipped; clashes take _2, _3 in call order past every printed name.

The library name is a second name for the local's number: the walk
numbers every local as with the option off, records that numbering,
and only then applies the names, so vN identity is the same in every
mode. A directive reaches the local by either name and a replay
records the number; a local whose library name a rename took prints
its number, and a vacated name goes to no one.

Tests: tests/stages/kuna-libcnames.xml, kuna-cli library_names (renames,
retypes, mode switches, displaced names, globals, keywords, and a
compile-and-run of tp beside the typedef tm), the wasm and browser rename
and mode-switch flows, and unit tests. Round-trip tests and seven CLI
probes of other options read the new names; the ghidra-sim differential
runs its CLI side with the option off. Over 14 stripped decbench
binaries under --mode aggressive, 968 of 10,106 numbered locals print a
library name in 546 of 3,297 functions, only names changing; 0/675
datatest assertions move. Default off because six stage assertions of
other features spell a vN it names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011ib7dvKSnoqFMNwr6Wu6dm
`name fd v9` printed `fd_3`: the naming pass took any number-shaped name
for a number to print a library name over, the caller's included. A name
a rename gave, or a name-locked Symbol holds, now stays, and a local's
prior library name is looked up by the number its instances had, so a
rename onto that number leaves it its library name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011ib7dvKSnoqFMNwr6Wu6dm
…ed under its number too

The check that an applied rename or retype is visible looked for a
declaration under the number a replay records, and a local printed
under its library name never matched: `type fd unsigned int` was
applied and reported refused. A declaration under a local's library
name now counts for its number as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011ib7dvKSnoqFMNwr6Wu6dm
@mahaloz
mahaloz force-pushed the fix/libcnames-names branch from 12c94c3 to 6b3c9ca Compare October 14, 2026 22:38
…does not read

A global the function never reads still owns its name in the printed
program, so the printer suffixed the local as fd_1, which no directive
can address. Every global the program names is now reserved, and the
local takes the next spelling (fd_2) instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011ib7dvKSnoqFMNwr6Wu6dm
@mahaloz
mahaloz merged commit 7df1645 into main Oct 14, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/libcnames-names branch October 14, 2026 23:53
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