Skip to content

[AUTOMATED] feat(analysis): resource-loader-c-retains — give a PE's Win32 imports their signatures (win32sigs, DIV-141) - #520

Merged
mahaloz merged 3 commits into
mainfrom
feat/re-resource-loader-c-retains
Sep 8, 2026
Merged

mahaloz merged 3 commits into
mainfrom
feat/re-resource-loader-c-retains

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

The problem

kuna carries libc prototypes and not one Windows API signature, so on a PE every
LoadLibraryExW / WriteFile / VirtualQuery call has to have its arguments
guessed from the call site — and when that guess loses, the values are left
behind as stack locals.

$ kuna decompile ./win32sigs_pe_i386.exe 0x401020
void sub_401020(void)
{
  int v1; // edx
  wchar_t *v2; // stack - 0x10

  v2 = L"kernel32.dll";
  sub_401080(L"kernel32.dll",0,8);
  LoadLibraryExW((int)v2 + v1);
}

LoadLibraryExW takes three arguments. Two of them have been handed to the
unrelated sub_401080 above it. On the reported binary the same shape costs 47
declarations where 8 would do, and the calls print LoadLibraryExW(),
EnumResourceTypesW() and FreeLibrary() with empty argument lists. The
fixture is generated by win32sigs_pe_i386.py beside it.

The fix

  • A built-in Win32 prototype table (win32sigs, default on), the Windows half of
    the .gdt stand-in libproto already is. ~130 WINAPI exports, ranked by an
    import histogram over 54 PE images rather than picked by hand, plus the
    resource/loader family the report was about.
  • Parked by entry address, not by name. A PE import is two FunctionSymbols —
    the size-0 IAT slot and the FF 25 thunk a direct call targets — the global
    by-name query answers with the slot, and ActionDefaultParams asks about the
    thunk. --assert 'prototype LoadLibraryExW …' is byte-identical to no assert
    at all on the fixture; the same prototype at the thunk address repairs the
    call. A new AnalysisOutput::prototypes_at stream carries the address.
  • Only callee-cleans exports, no CRT spellings: the x86 PE default model is
    __stdcall with an unknown extrapop, so a locked arity is also the callee's
    stack pop, and a __cdecl name here would get the wrong cleanup.
  • A parameter with no width-stable spelling is rejected rather than
    approximated, which is why SetFilePointerEx and RtlVirtualUnwind are absent.

The tests

tests/cli/resource-loader-c-retains.json runs the command above against the
vendored fixture and fails without the change. Unit tests pin the resource-family
arities, the CRT exclusion, and that the resolver names both the slot and the
thunk. Sweep over 22 PE images / 3,029 functions: 168 changed, 0 added, 0 removed,
and every changed function contains a call to a table name — 35 call sites gain
arguments to the documented arity, 16 shed invented ones
(GetCurrentProcess(CONCAT44(dat_c,argc)) → GetCurrentProcess()). Parity is
675/675 and 704/704, and sub_401ba0 decompiles 16.9% faster.

One kuna-owned stage assertion moved with the change. ghdec-peimportcall pinned
ExitProcess(); // no-return, an empty-parens spelling from before kuna had any
Win32 prototype. ExitProcess takes one UINT, and that fixture's
bail(int code) passes it, so ExitProcess(a0) is the correct rendering and the
old regex was pinning the argument being lost. What the test exists to protect,
the IAT-slot call binding to its import and keeping its no-return flow effect, is
unchanged; the other four assertions in the file are untouched.

🤖 Generated with Claude Code

mahaloz and others added 2 commits September 8, 2026 16:23
…in32 imports their signatures (win32sigs, DIV-141)

kuna's whole prototype knowledge base was libc, so on a PE every LoadLibraryExW /
WriteFile / VirtualQuery call reached ActionDefaultParams with an empty prototype
and call-site trial scoring was the only thing between the pushes and the call.
Where that scoring loses, the arguments are dropped and the outgoing slots
`callsitestackargs` already mapped survive as stack-local debris.

`win32sigs` supplies the Windows half of the `.gdt` stand-in `libproto` already
is: ~130 WINAPI exports ranked by an import histogram over the 54 PE images of
the RE arena corpus, plus the resource/loader family the need was filed against.
Signatures are reduced to the same width-stable vocabulary the libc tables use,
and a slot with no honest spelling is rejected rather than approximated.

The prototypes are parked by ENTRY ADDRESS. A PE import is two FunctionSymbols --
the size-0 IAT slot and the `FF 25` thunk veneer a direct `call` targets -- the
global by-name query answers with the slot, and ActionDefaultParams asks about
the thunk, so the by-name park is a silent no-op on exactly the calls that need
it. A new AnalysisOutput::prototypes_at stream carries the address.

Sweep over 22 PE images / 3,029 functions: 168 changed, 0 added, 0 removed, every
changed function containing a call to a table name; 35 call sites gain arguments
to the documented arity and 16 shed invented ones. Speed -16.9%.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…g the analysis-tier gates with no codegen live reader

`option_values_live_value_present_for_58_suppressed_for_101` enumerates the
options whose live state is read console-side rather than through the codegen
`live_value`. Like `libproto`/`libcsigs`/`declaredlibcproto`, `win32sigs` is an
analysis-pass gate and belongs in that list.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@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 8, 2026
…es a UINT, so its peimportcall assertion does too

`ghdec-peimportcall` PEIMPORTCALL #5 pinned `ExitProcess\(\); // no-return` on
the `bail` function of `pe_noreturn_import.exe`.  That empty-parens spelling was
frozen in by DIV-57 (#254), when kuna carried no Windows API prototype at all
and the call's one argument was simply lost.

`win32sigs` (DIV-141) supplies the import's `void __stdcall ExitProcess(UINT)`,
and the fixture's source is `void bail(int code) { ExitProcess(code); }`, so the
site now renders `ExitProcess(a0)` and `bail` recovers its parameter.  The
assertion is the stale side of that; what the test exists to protect -- the
`FF 25`/IAT-slot call binding to its import name and both `// no-return` flow
effects surviving -- is asserted by the other four stringmatches and unchanged.

The assertion name is untouched, so `docs/baseline-stages.json` needs no
re-record; `docs/baseline.json` is byte-identical to origin/main.

make test 675/675 PARITY OK, make test-stages 704/704 PARITY OK,
make test-cli 99/99, make check-spec OK, kuna catalog --check OK.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@mahaloz
mahaloz merged commit b23e01e into main Sep 8, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/re-resource-loader-c-retains branch September 8, 2026 17:15
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