Repository navigation
[AUTOMATED] feat(p4): cspecprotos - register the compiler spec's named prototype models (registry 1 -> 5 models, __thiscall resolves) - #265
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The gap
kuna's
__thiscall/this-pointer machinery is fully ported, wired and consumed - and was permanently dead, because nothing could ever sethasThis.ProtoModel::hasThisPointerp4_calls/fspec.rsFuncProto::hasThisPointer/updateThisPointer/getThisPointerStoragep4_calls/fspec.rsParameterPieces::isThisPointerp4_calls/fspec.rsupdate_this_pointer, end of input recovery)p4_calls/coreaction_protos.rsthisparam pointing at aTYPE_STRUCTis effectively type-locked)p5_types/coreaction_infertypes.rsArchitecture::build_default_protoread 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, sogetModel("__thiscall")could never resolve andProtoModelMergedhad 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 thehasthis/constructorattributes, with upstream'sif (name == "__thiscall") hasThis = trueoverride applied last (fspec.cc:2595), so an explicithasthis="false"loses exactly as it does upstream.<resolveprototype>- folds each<model name=...>constituent into aProtoModelMergedand 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.__thiscallmodel, 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.
make testmake test-stagesmake rust-testmake check-speckuna catalog --checkByte-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:/bin/ls/usr/bin/grep/usr/bin/gzip/usr/bin/du/usr/bin/xxdcpp_demo(g++ -O1, vtables + libstdc++)cppproto_x86_64(the #264 C++ DWARF fixture)msvc_rtti_x86.exepe_imports.exepe_dwarf.exepdb_prog.exemsvc_rtti_x64.exestderr 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:compilerpair in the vendored.ldefs, countingArchitecture::proto_modelsafterinit_post_engine:getModel("__thiscall")resolvesZero 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:__stdcall__cdecl,__fastcall,__thiscall, merged__fastcall/__thiscall/__stdcall(3 constituents)__fastcall__thiscall, aliases__cdecl+__stdcall__cdecl__thiscall,__regparm1/2/3,syscall,processEntry, merged__cdecl/__regparm__stdcallMSABI,syscall,processEntry, cloned__thiscallplus:
__thiscall.hasThisPointer()is true and__stdcall's is false;__thiscallaccepts ECX as an input parameter and__stdcalldoes not;defaultfpis still the sameRcasgetModel("__stdcall")with the same extrapop and effect list.defaultprototype __thiscall: before / afterBefore - on every target, including the ones whose cspec has defined
__thiscallsince it was vendored:After, on the real MSVC PE32 fixture
msvc_rtti_x86.exe:It also resolves on ELF x86-64, where the cspec declares no
__thiscalland the cloned-off-the-default invariant supplies it.tests/stages/ghdec-cspecprotos.xmlis the two-pass e2e case (6 assertions) on two MSVC-shaped member bodies withthisin ECX. Pass 1 (default__stdcall) pins the bug -unsigned int getfield(void)withint4 v1; // ecx. Pass 2 (option defaultprototype __thiscall) proves the fix -unsigned int getfield(int4 a0)andint4 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 --jsonis a full load + analysis with no decompilation; interleaved before/after on the same tree, n=31, minimum of the run:msvc_rtti_x86.exemsvc_rtti_x64.exepe_imports.exe/usr/bin/gzipWhole-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_protosparsed the cspec document a third time. Folding the return-address, default-proto and named-model readers onto one parse inbuild_default_protois 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.__thiscallassignment to member functions, from the demangler or from DWARFDW_AT_object_pointer.FuncProto::decode's Java-tierconstructor/destructorimport,prepareThisPointer,hide_thisparam.What this unblocks
DW_AT_object_pointerto__thiscall- [AUTOMATED] feat(p1): cppproto - ingest C++ function prototypes from DWARF #264'sdocs/features/cppproto/analysis.mdparks exactly on this: "kuna cannot register one yet (ProtoModel::decodeis a stub)". That seam is now open, and this PR lines up with it rather than duplicating it.__thiscallfor MSVC/Itanium member functions.FuncProto::decode's constructor/destructor import;prepareThisPointer;hide_thisparam.ProtoModelMerged::selectModelscoring, which had no live producer before.<eval_*_prototype>as a future option.Conventions
phases.tomlrow, no catalog-count bump, nodocs/options.mdregeneration.docs/history.md's Convergences table, per its own rule ("a port defect whose fix moves kuna back onto upstream's default earns no DIV row"). DIV-59/60/61 are claimed by [AUTOMATED] fix(p9): cppcallnames - render a C++ callee's qualified name at the call site (DIV-59) #262/[AUTOMATED] feat(p1): cppproto - ingest C++ function prototypes from DWARF #264/[AUTOMATED] feat(p1): fdeinterior - a C++ landing pad is not a function, it is a point inside one (-1030 spurious entries, 0 real lost) #263; this PR claims none.docs/spec/04-calls-and-prototypes.md4.1, which previously admitted this gap ("secondary named models and merged models have no producer yet").docs/features/cspecprotos/{analysis,plan}.md+record.json.f2c15037; all gates re-run after the rebase.🤖 Generated with Claude Code
https://claude.ai/code/session_01McQC7imaNfzj5k5AVumSYN