Skip to content

[AUTOMATED] feat(analysis): resolve printf/scanf format strings at load time so formatstring can be on by default - #673

Merged
mahaloz merged 23 commits into
mainfrom
feat/formatstring
Sep 19, 2026
Merged

mahaloz merged 23 commits into
mainfrom
feat/formatstring

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

The problem

A printf/scanf call states the types of its variadic arguments in its
format string, and the default output ignores it. Reading the string was
already possible (--option formatstring on), but it cost a second decompile of
every caller, +43% to +77% on a whole binary, so it stayed off.

$ kuna decompile decompiler/crates/kuna-analysis/tests/fixtures/fmt_x86_64 main
unsigned long main(unsigned int a0,unsigned long *a1)
{
  printf("%d %s\n",(unsigned long)a0,*a1);

With this change the default prints main(int a0,unsigned long *a1) and
printf("%d %s\n",a0,(char *)*a1);.

The fix

  • formatstring becomes off|static|full, default static (on still means
    full). static reads the format constant out of the image at load, from the
    Listing's call and tail-jump edges and a short constant fold before each call,
    and hands the per-call prototype to the first decompile. full keeps the old
    second-decompile loop for the sites static cannot answer.
  • A callee takes a format when its built-in signature has a char * just
    before the ..., so err/warn/error/syslog count too. A format that
    comes out of dcgettext is read through its msgid.
  • The resolved prototype is installed with an open tail: the call is scored
    exactly like the untyped call and drops what it took past its declared
    arguments only once every call in the function has its arguments, so the
    calls around it keep what off gives them. Installed closed, it freed the
    stack slots it used to claim, and calls whose format is not resolved took
    them (three __fprintf_chk calls in Ubuntu m4's version_etc went from 9
    to 12 arguments); it also let a clang sscanf("%d", &a) destination in the
    first outgoing stack slot fold to the value stored before the call.
  • The first decompile has the last word. An override is dropped, and the
    function decompiled once more, when the call's format is not the resolved
    string (a join reached from a jump table) or its argument count disagrees.
  • The default types x86 only. A declared argument, now certain, can veto the
    same value at a neighbouring call. On x86-64 the 8 calls this moved all lost
    a phantom; on AArch64 and ARM32 firmware (measured with the prototype
    closed) it gave calls an x8 of 0 or leftover registers and cost iproute2
    ip's parse_rtattr its len. full still types those targets.
  • An override is built only where the target passes a vararg exactly like a
    named argument: every conversion on x86 and standard AArch64, integer and
    pointer conversions on ARM32, RISC-V, MIPS, PowerPC and Windows AArch64,
    nothing on Apple AArch64 or an unchecked processor.
  • The format must sit in a read-only section, and a site whose call
    instruction writes the format register itself (a delay slot) is declined.
  • Fixes in the existing typing: override parameter names no longer leak into
    the caller; %c is a char and %lc a wint_t; %lf is a double (a
    double * for scanf); %zu/%td are pointer-width, so 8 bytes on Win64
    where long is 4; %Lf declines the site.

The tests

  • tests/stages/kuna-formatstring-static.xml, 32 passes and 38 assertions:
    off, default and full on fmt_x86_64, the jump-table/alloca sites, %lf
    on x86-64 and ARM hard-float, a Win64 PE with %zu/%td, fmt_aarch64,
    gnulib's version_etc shape, clang sscanf destinations in the first
    outgoing stack slot and a ninth double on the stack, and default == off on
    Apple arm64, AArch64, ARM and a format in .data. A build that installs the
    prototype closed fails 6 of the last 10.
  • decbench type_match over 444 slices (x86-64): 988 → 1026 perfect, 176
    functions better, none worse.
  • Every call in every function the default changes, off vs default, over 325
    decbench and 25 Ubuntu /usr/bin x86-64 binaries (81,955 functions): no
    format call newly disagrees with its format (428 stop disagreeing), no call
    whose format is not resolved gains an argument, and the 8 other calls whose
    argument count moved all lost a phantom. With formatstring off, and on
    every non-x86 target by default, the output is byte-identical to main.
    decompile-all min-of-15 against main: fmt -4.6%, ls +0.8%, sort
    -0.4%, m4 +0.7% (against off: +1.2%, +1.2%, -0.7%, +1.1%).

🤖 Generated with Claude Code

https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG

@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/formatstring branch 4 times, most recently from ba4f924 to 4130b00 Compare September 19, 2026 13:11
mahaloz and others added 23 commits September 19, 2026 17:20
…ad time (formatstring static)

Squash of the formatstring branch (rounds 1-3) before the round-4 rebase.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
C11 7.21.6.1p7: l has no effect on a floating printf conversion; for scanf it
selects a double destination. Ghidra's longLengthModification fell through to
unsigned long, which moved the double out of its register and added phantom
parameters once the typing was on by default. %lp is a void pointer.
(docs/baseline-stages.json is main's until it is re-recorded.)

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

Passes 15-20 load fmtlf_x86_64 and fmtlf_armhf (one source, gcc -O2 / -marm).
The round-3 build fails 8 of the 9 new assertions.

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

A MIPS or SPARC call runs its delay slot before it transfers, so a format
register set there is invisible to the backward window; the site (and a
gettext hop) is declined. Also moves the ghidra_sim test's inline note into
its header.

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

Win64 has a 4-byte long and 8-byte pointers, so %zu/%zd/%td/%tu sized as a
long kept only the low half of the argument: printf("%zu\n", (n << 32) | 5)
decompiled to printf("%zu\n", 5) and the caller lost its parameter. The
conversion is now the int, long or long long as wide as a pointer. Adds an
LLP64 unit test and a Win64 PE32+ stage pass (fmtzu_pe_x86_64, passes 21-23).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
…ument count; Thumb sites are declined

argcount.py now compares the argument count of every non-format call in a
changed function (paired per callee in order, a reordered body is not a
change) against the callee's own decompiled arity. The spec and analysis say
that a Thumb call site's window does not fold, so Thumb code renders as off.

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

A closed format prototype also changes how P4 scores the argument trials of
the calls around it. On AArch64 and ARM32 firmware that added more wrong
arguments to neighbouring calls than it removed (an x8 of 0 ahead of the real
arguments; iproute2 ip's parse_rtattr losing its len), while on x86 every such
change removed a phantom. static now resolves at load on x86 only; full still
resolves on every target VarargAbi admits. Stage passes 24-26 pin AArch64
default == off and full typed; #13/#19/#23/#28 pin ARM the same way.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
…e round-6 neighbouring-call counts

Spec and analysis explain how a closed format prototype moves the argument
trials of neighbouring calls, with the per-corpus counts (x86-64: 8 moved, all
phantoms removed; AArch64/ARM32 firmware: 24 moved, 19 wrong). argcount.txt is
the round-6 sweep with a verdict on every moved call. PR body draft updated.

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

A closed format prototype stopped claiming the stack slots it used to take as
phantoms, so later calls whose format is not resolved took them instead
(gnulib version_etc in /usr/bin/m4: three __fprintf_chk calls 9 -> 12
arguments). The override is now installed as a locked prefix plus ..., scored
like the open call, and the tail is shed once no call has trials left.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
…arguments when the version line is typed

fmtvalist_x86_64 (gcc -O2 -flto) passes 27-29: off keeps the version line's
three va_list phantoms, the default and full drop them, and no switch call
takes the va_list's slots. The round-6 binary fails #31 and #32.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
… arguments through fillinMap; pin the outgoing-slot cases

A ninth double goes on the stack behind the four integer registers a
"%f ... %d" call leaves unused, and fillinMap's positional rules ended the
open-tail call's list there, so the audit withdrew the override. The declared
(fixed-position) trials are now kept active.

fmtslots_x86_64 (clang -O2) passes 30-32 pin show9's nine doubles and two
sscanf destinations in the first outgoing stack slot, which the closed
prototype folded to the value stored before the call (return 6; return 0;).
The round-6 binary fails #31-#36.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
… and the catalog; scope the neighbouring-call claims

Spec 04 describes kuna_formattail (open tail, declared trials kept, shed after
the last call finalizes). Spec 01, the reached_by_default doc and the
phases.toml row no longer say x86 only ever removed phantoms: closed from the
start, x86 calls whose format is not resolved gained phantom stack arguments
(m4 version_etc 9 -> 12); with the open tail the 8 neighbouring calls that
move over 350 x86-64 binaries all lose one. analysis.md adds the Ubuntu
/usr/bin corpus (25 binaries: 115 fixed / 0 new) beside decbench (313 / 0),
and the audit's alloca withdrawals (130 -> 0). options.md and
phase_catalog.json regenerated.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
…r/bin corpus, typesweep and speed on d6c5862), PR body, alloca notes

record.json round7: m4 root cause and fix, the clang push-rax fold the closed
install hid, stage passes 27-32, decbench 313/0 and Ubuntu 115/0, audit
withdrawals 130 -> 0, typesweep 988 -> 1026 (176 better / 0 worse), speed
min-of-15 vs main fmt -4.6 ls +0.8 sort -0.4 m4 +0.7. argcount-ubuntu.txt
holds the Ubuntu argcount rows. The fmtjoin alloca notes say the site is
kept now.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
@mahaloz
mahaloz merged commit e76fd04 into main Sep 19, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/formatstring branch September 19, 2026 17:58
mahaloz added a commit that referenced this pull request Sep 19, 2026
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