Skip to content

[AUTOMATED] feat(p4): cspecprotos - register the compiler spec's named prototype models (registry 1 -> 5 models, __thiscall resolves) - #265

Merged
mahaloz merged 1 commit into
mainfrom
feat/cspecprotos
Aug 5, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/cspecprotos

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

The gap

kuna's __thiscall / this-pointer machinery is fully ported, wired and consumed - and was permanently dead, because nothing could ever set hasThis.

Piece Where
ProtoModel::hasThisPointer p4_calls/fspec.rs
FuncProto::hasThisPointer / updateThisPointer / getThisPointerStorage p4_calls/fspec.rs
ParameterPieces::isThisPointer p4_calls/fspec.rs
driver (update_this_pointer, end of input recovery) p4_calls/coreaction_protos.rs
consumer (a this param pointing at a TYPE_STRUCT is effectively type-locked) p5_types/coreaction_infertypes.rs

Architecture::build_default_proto read exactly one cspec element, <default_proto><prototype>, where the C++ parseCompilerConfig (architecture.cc:1253/1280-1284/1297-1303) also dispatches <prototype>, <resolveprototype> and <modelalias>. The per-program registry held exactly one model, so getModel("__thiscall") could never resolve and ProtoModelMerged had no live producer at all.

In spec text: 110 cspecs declare 186 <prototype> elements, and only the 110 inside <default_proto> were ever decoded - 76 named models, 6 <resolveprototype> and 3 <modelalias> were dead.

What this PR does

Registers what the spec declares. Nothing more.

  • <prototype> - decoded through the same body as the default one (decode_proto_model), so a named model carries identical <pentry> storage and <unaffected>/<killedbycall>/<returnaddress>/<internal_storage> fidelity. Plus the hasthis/constructor attributes, with upstream's if (name == "__thiscall") hasThis = true override applied last (fspec.cc:2595), so an explicit hasthis="false" loses exactly as it does upstream.
  • <resolveprototype> - folds each <model name=...> constituent into a ProtoModelMerged and finalizes the merged input list (ProtoModelMerged::decode, fspec.cc:2904). The merge primitives were already ported; this is their first live producer.
  • <modelalias> - createModelAlias (architecture.cc:1137), refusing a merged parent and an alias-of-an-alias exactly as upstream.
  • the post-parse invariant that every language has a __thiscall model, cloned off the default when the spec declares none (architecture.cc:1342).

Deliberate deviation from upstream: a named model that fails to decode is skipped, not fatal. Upstream throws and loses the architecture; kuna's cspec corpus spans every processor it ships, and one undecodable named model must not cost the program its default model. The census below shows this masks nothing today.

Proof that registration is behavior-preserving

Nothing selects a model differently. This is the load-bearing claim, so it is measured three ways.

gate result
make test PARITY OK 675/675, no re-pin
make test-stages PARITY OK 400/400 (394 + the 6 new keys)
make rust-test green - 300 test binaries, 4,463 tests, 0 failed
make check-spec OK (lenient and strict)
kuna catalog --check OK (no option added)

Byte-identical sweep - kuna decompile-all, both arms on the same rebased tree (f2c15037, i.e. after #262/#263/#264, which themselves move C++ output). 12 binaries, 4 compiler specs, 73,173 lines:

binary spec lines result
/bin/ls x86-64-gcc 14,762 IDENTICAL
/usr/bin/grep x86-64-gcc 22,054 IDENTICAL
/usr/bin/gzip x86-64-gcc 9,157 IDENTICAL
/usr/bin/du x86-64-gcc 18,823 IDENTICAL
/usr/bin/xxd x86-64-gcc 1,033 IDENTICAL
cpp_demo (g++ -O1, vtables + libstdc++) x86-64-gcc 436 IDENTICAL
cppproto_x86_64 (the #264 C++ DWARF fixture) x86-64-gcc 216 IDENTICAL
msvc_rtti_x86.exe x86win 16 IDENTICAL
pe_imports.exe x86win 5,217 IDENTICAL
pe_dwarf.exe x86win 1,425 IDENTICAL
pdb_prog.exe x86win 13 IDENTICAL
msvc_rtti_x64.exe x86-64-win 21 IDENTICAL

stderr is byte-identical too. Byte-identical is the expected result and the sweep is the proof, not a sanity check - any diff would mean registration is selecting something.

Proof the registry now holds more than one model

Census over every language:compiler pair in the vendored .ldefs, counting Architecture::proto_models after init_post_engine:

before after
pairs bootstrapped 294 / 294 294 / 294
pairs with more than one model 0 294
pairs where getModel("__thiscall") resolves 0 294
models registered in total 294 790

Zero bootstrap failures, so the tolerant skip is not hiding a decode failure anywhere in the corpus.

decompiler/crates/kuna-decomp/tests/verify_cspecprotos.rs (6 tests) pins the shape against the real vendored specs:

spec default additionally registered
x86win __stdcall __cdecl, __fastcall, __thiscall, merged __fastcall/__thiscall/__stdcall (3 constituents)
x86-64-win __fastcall __thiscall, aliases __cdecl + __stdcall
x86gcc __cdecl __thiscall, __regparm1/2/3, syscall, processEntry, merged __cdecl/__regparm
x86-64-gcc __stdcall MSABI, syscall, processEntry, cloned __thiscall

plus: __thiscall.hasThisPointer() is true and __stdcall's is false; __thiscall accepts ECX as an input parameter and __stdcall does not; defaultfp is still the same Rc as getModel("__stdcall") with the same extrapop and effect list.

defaultprototype __thiscall: before / after

Before - on every target, including the ones whose cspec has defined __thiscall since it was vendored:

Execution error: Unknown prototype model :__thiscall
Aborting process

After, on the real MSVC PE32 fixture msvc_rtti_x86.exe:

// before: ECX read before it is ever assigned - not valid C
int4 Box::vftable_0(void)
{
  int4 v1; // ecx
  return *(int4 *)(v1 + 4) * *(int4 *)(v1 + 4);
}

// after, with --option defaultprototype __thiscall
int4 Box::vftable_0(int4 a0)
{
  return *(int4 *)(a0 + 4) * *(int4 *)(a0 + 4);
}

It also resolves on ELF x86-64, where the cspec declares no __thiscall and the cloned-off-the-default invariant supplies it.

tests/stages/ghdec-cspecprotos.xml is the two-pass e2e case (6 assertions) on two MSVC-shaped member bodies with this in ECX. Pass 1 (default __stdcall) pins the bug - unsigned int getfield(void) with int4 v1; // ecx. Pass 2 (option defaultprototype __thiscall) proves the fix - unsigned int getfield(int4 a0) and int4 addfield(int4 *a0,int4 a1). Before this PR pass 2 could not run at all: the option aborted the script.

Speed

The extra elements are parsed once per program load, so load is the metric. kuna functions --json is a full load + analysis with no decompilation; interleaved before/after on the same tree, n=31, minimum of the run:

target spec before after delta
msvc_rtti_x86.exe x86win (5 models registered) 115.1 ms 115.6 ms +0.42%
msvc_rtti_x64.exe x86-64-win 134.6 ms 135.3 ms +0.55%
pe_imports.exe x86win 236.8 ms 237.2 ms +0.15%
/usr/bin/gzip x86-64-gcc 317.3 ms 317.2 ms -0.05%

Whole-binary decompile-all: gzip -1.89% (n=5), pe_imports.exe +0.63% (n=9), cpp_demo +0.89% (n=5). Worst case +0.89%, inside the 5% budget.

The first cut cost +2.8% / +4.3% on the two smallest PE loads because decode_named_protos parsed the cspec document a third time. Folding the return-address, default-proto and named-model readers onto one parse in build_default_proto is a net removal of one parse versus the pre-PR tree, and is what brought it to +0.5% worst case.

Scope: what this PR deliberately does NOT do

Every one of these changes which model a function gets, which registration does not:

  • <eval_current_prototype> / <eval_called_prototype> are still ignored. Six cspecs declare one (x86win, x86gcc, x86borland, HCS12, HCS12X, CR16); honoring it would hand every function on those targets the merged model. That is a default change with its own evidence burden, and belongs behind its own option.
  • Automatic __thiscall assignment to member functions, from the demangler or from DWARF DW_AT_object_pointer.
  • FuncProto::decode's Java-tier constructor/destructor import, prepareThisPointer, hide_thisparam.

What this unblocks

  • DWARF DW_AT_object_pointer to __thiscall - [AUTOMATED] feat(p1): cppproto - ingest C++ function prototypes from DWARF #264's docs/features/cppproto/analysis.md parks exactly on this: "kuna cannot register one yet (ProtoModel::decode is a stub)". That seam is now open, and this PR lines up with it rather than duplicating it.
  • Demangler-driven __thiscall for MSVC/Itanium member functions.
  • FuncProto::decode's constructor/destructor import; prepareThisPointer; hide_thisparam.
  • ProtoModelMerged::selectModel scoring, which had no live producer before.
  • <eval_*_prototype> as a future option.

Conventions

🤖 Generated with Claude Code

https://claude.ai/code/session_01McQC7imaNfzj5k5AVumSYN

…d prototype models (registry 1 -> 5 models, __thiscall resolves)

kuna's `__thiscall` machinery is fully ported, wired and consumed - and was
permanently dead, because nothing could ever set `hasThis`.

`Architecture::build_default_proto` read exactly one cspec element,
`<default_proto><prototype>`, where the C++ `parseCompilerConfig`
(architecture.cc:1253/1280-1284/1297-1303) also dispatches `<prototype>`,
`<resolveprototype>` and `<modelalias>`.  The per-program model registry
therefore held exactly ONE model: `getModel("__thiscall")` could never resolve,
`option defaultprototype __thiscall` failed with "Unknown prototype model" on
every target, and `ProtoModelMerged` had no live producer at all.  Across the
vendored corpus, 110 cspecs declare 186 `<prototype>` elements and only the 110
inside `<default_proto>` were ever decoded.

The named models now register alongside the default one, decoded through the
SAME body (`decode_proto_model`), so they carry identical `<pentry>` storage and
`<unaffected>`/`<killedbycall>`/`<returnaddress>`/`<internal_storage>` fidelity.
Added on top: the `hasthis`/`constructor` attributes with upstream's
`if (name == "__thiscall") hasThis = true` override applied last (fspec.cc:2595),
`ProtoModelMerged::decode`'s fold-in + finalize for `<resolveprototype>`,
`createModelAlias` for `<modelalias>`, and the post-parse invariant that every
language has a `__thiscall` model, cloned off the default when the spec declares
none (architecture.cc:1342).

Registration selects nothing.  No function's model changes, so emitted C is
byte-identical: 675/675 datatests unmoved, and 12 whole-binary `decompile-all`
panes (73,173 lines, four compiler specs, both arms on the same rebased tree)
identical on stdout and stderr.  What changed is what is REACHABLE - on
`x86:LE:32:default:windows` the registry now holds `__stdcall` + `__cdecl` +
`__fastcall` + `__thiscall` + the merged `__fastcall/__thiscall/__stdcall`, and
`option defaultprototype __thiscall` turns the MSVC member function
`int4 Box::vftable_0(void) { int4 v1; // ecx ... }` (a read-before-write of ECX,
not valid C) into `int4 Box::vftable_0(int4 a0)` on the real `msvc_rtti_x86.exe`
fixture.

Registry census over every language:compiler pair kuna ships: 294/294 bootstrap,
pairs holding more than one model 0 -> 294, pairs where `getModel("__thiscall")`
resolves 0 -> 294, models registered 294 -> 790.

No option: registration cannot change emitted C, proven by the ablation and the
sweep rather than assumed.  No DIV: this is a Convergence back onto upstream's
own cspec dispatch with no corpus output shift (docs/history.md).

Deliberately NOT in this PR, each noted as a follow-up: honoring
`<eval_current_prototype>`/`<eval_called_prototype>` (that changes which model
every function on six specs is evaluated with), automatic `__thiscall` assignment
from the demangler or DWARF `DW_AT_object_pointer` - the seam #264 explicitly
parked on "kuna cannot register one yet" - `FuncProto::decode`'s Java-tier
constructor/destructor import, `prepareThisPointer`, and `hide_thisparam`.

Speed: the extra elements are parsed once per program load, so load is the
metric.  Interleaved `kuna functions --json` minima, n=31: +0.42% / +0.55% /
+0.15% / -0.05%.  `decompile-all` worst case +0.89%.  Getting there required
folding the return-address, default-proto and named-model readers onto ONE cspec
parse - a net removal of one parse versus the pre-PR tree.

Gates: make test PARITY OK 675/675 | make test-stages PARITY OK 400/400 |
make rust-test green (4,463 tests, 0 failed) | make check-spec OK (strict) |
kuna catalog --check OK.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01McQC7imaNfzj5k5AVumSYN
@mahaloz
mahaloz merged commit 9f7fa6d into main Aug 5, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/cspecprotos branch August 5, 2026 06:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant