Skip to content

[AUTOMATED] fix(p4): evalcurrentproto - honor the compiler spec's <eval_current_prototype> so register arguments are recovered (DIV-71) - #290

Merged
mahaloz merged 1 commit into
mainfrom
feat/evalcurrentproto
Aug 12, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/evalcurrentproto

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

The defect

Most functions in an x86 Windows binary rendered as (void) though they take arguments:

uint4 sub_6ae390(void)              |   uint4 sub_6ae390(int4 a0,int4 a1)
{                                   |   {
  int4 v4; // ecx                   |
  ...                               |     ...
  v3 = *(uint1 *)(v4 + 0xf5);       |     v3 = *(uint1 *)(a0 + 0xf5);

The left column is not merely less informative — v4 is read before it is ever written, which is not something a C function can do. (IDA Pro reports this one as long __fastcall Load_CBPZ(struct _VQAHandleP *, unsigned long) — two arguments.)

Root cause

A compiler spec can nominate one of its registered models for evaluating a function's own unlocked prototype. x86win.cspec does exactly that:

<resolveprototype name="__fastcall/__thiscall/__stdcall">
  <model name="__stdcall"/>    <!-- The default case -->
  <model name="__fastcall"/>
  <model name="__thiscall"/>
</resolveprototype>
<eval_current_prototype name="__fastcall/__thiscall/__stdcall"/>

Every consumer of that nomination was already ported and wired: ActionPrototypeTypes installs glb->evalfp_current on any unlocked prototype (coreaction.cc:4843), ActionInputPrototype calls FuncProto::resolveModel on it (fspec.cc:3772), and ProtoModelMerged::selectModel scores the active trials against each constituent. Only the <eval_current_prototype> element itself was never read — the pass that registered named models deliberately stopped short of it, because honoring it changes which model every function is evaluated with.

So evalfp_current stayed null, every function fell back to <default_proto> = __stdcall, whose <input> list is stack-only — and a __fastcall/__thiscall function's ECX/EDX arguments were not possible parameters at all, so they never became trials. The merged-model machinery, fully ported and tested, had no live producer.

The fix

Read the element (p0_knowledge/kuna_evalcurrentproto.rs), resolve the nominated name against the model registry at <default_proto> time, and hand it to each function through the arch handle. The union then resolves per function against the trials that are actually active, so a function that touches neither register still comes out __stdcall.

Behind option evalcurrentproto (default-on, DIV-71). Precedence is explicit: option protoeval <model> outranks the nomination (same slot); the nomination outranks option defaultprototype for an unlocked prototype, which is upstream's own ordering. <eval_called_prototype> stays unread — no vendored spec declares one.

Six vendored specs nominate a model — x86win, x86borland, x86gcc (__cdecl/__regparm), CR16, HCS12, HCS12X. Every other language has no such element and is untouched whichever way the option is set.

Measured

3,153-function i386 PE game binary:

off on
functions rendering as (void) 1,553 602
functions with an ECX/EDX local read before written 2,128 636
bodies changed — 2,126

Ground truth. The COFF object from the same project carries MSVC-mangled names, which encode the declared arity independently of the decompiler. Across its 13 functions the recovered arity matches the mangled signature in 11 (counting a by-value Coord as the slots it occupies); the 2 that do not are under-recovery of a parameter the body never reads (Get_Explosion_Z, whose whole body is return 0) — never over-recovery.

?Modify_Damage@@YIHHPBVWarheadTypeClass@@W4ArmorType@@H@Z   declared 4   off 2   on 4
?Chain_Reaction_Damage@@YIXABVCell@@@Z                      declared 1   off 0   on 1
?Explosion_Damage@@YIXABVCoord@@HPAVTechnoClass@@…@Z        declared 5   off 3   on 5
?Calculate_Projectile_Speed@@YI?AW4MPHType@@HN@Z            declared 2   off 1   on 2
?Get_Floater_Gravity@@YINXZ                                 declared 0   off 0   on 0

Wrongness audit (standing requirement 8) over all 3,153 PE functions: 2,126 gained arguments, 0 lost any, and the set of called functions is identical in 3,152 of 3,153 — the exception is an unaligned pushf/popf flags-probe stub whose return widens to int8.

Selective, not blanket. A second i386 PE of comparable size (3,015 functions) moves only 36, because the union resolves most of it to __stdcall.

Off-Windows, since x86gcc nominates __cdecl/__regparm: i386 ls 1 of 537 (it is _start, whose EAX/EDX really are live at process entry), i386 all 0 of 23, and libpthread.so.0 34 of 467 — every one a genuine glibc regparm internal recovered with the argument the source declares (__pthread_mutex_lock_full(uint4 *a0), change_stack_perm(int4 a0)).

Speed (whole-binary decompile-all, arms interleaved, minimum of 3): the 3,153-function PE +0.27% (159.98 → 160.42 s), the second PE +1.91% (53.54 → 54.56 s), x86-64 ls +1.31% (31.27 → 31.68 s). ls is a language with no <eval_current_prototype>, so both arms run identical code — its +1.31% is this instrument's noise floor, and the two real arms sit at or below it — against the 5% budget.

Tests & gates

New two-pass stage test tests/stages/kuna-evalcurrentproto.xml (a clang -target i386-pc-windows-msvc __fastcall body: off = one stack argument + an // ecx local, default = all three arguments).

One per-test opt-out: tests/stages/ghdec-cspecprotos.xml exists to pin this very bug (getfield(void) with an // ecx local) and reach the fix by naming __thiscall by hand, so it now sets option evalcurrentproto off to keep testing what it was written for.

make test 675/675 PARITY OK with the raw flip — no re-pin, no datatest opt-out (only 3 of the 83 files are on an affected language and all 11 of their assertions are unmoved) · make test-stages PARITY OK (436/436) · make rust-test green · make check-spec OK lenient and strict · kuna catalog --check OK.

Counters re-derived from the built artifact: live catalog 102, catalog_bytecompat.rs 102, kuna_phases/tests.rs 102 / tiers (22, 45, 35) / },\n 101 / live readers 31, phase_catalog.json 102, docs/options.md 102, xml.rs corpus 200.

Web UI

integrations/web rebuilt: parity 19/19 native==wasm, glue/worker/fnfilter/auto-mode/zip green. Under WASI the same PE function comes out sub_6ae390(int4 a0,int4 a1), and the real browser UI (headless Chrome over CDP, uploading through the actual file input) renders Chain_Reaction_Damage(uint4 a0) where it used to render (void).

Docs: docs/spec/04-calls-and-prototypes.md (the section that previously recorded this element as deliberately ignored), phases.toml row + regenerated docs/options.md and catalog fixture, DIV-71 in docs/history.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DQNaY1pwg8rskYRksC9qbh

…al_current_prototype> so register arguments are recovered (DIV-71)

Most functions in an x86 Windows binary rendered as `(void)` though they take
arguments, with their ECX/EDX arguments surviving as locals READ BEFORE THEY ARE
EVER WRITTEN -- not merely less informative, but not something a C function can
do.

A compiler spec can nominate one of its registered models for evaluating a
function's OWN unlocked prototype, and x86win.cspec does exactly that: a
`<resolveprototype name="__fastcall/__thiscall/__stdcall">` union followed by
`<eval_current_prototype name=...>`. Every consumer of that nomination was
already ported and wired -- ActionPrototypeTypes installs `glb->evalfp_current`
on any unlocked prototype (coreaction.cc:4843), ActionInputPrototype calls
FuncProto::resolveModel on it (fspec.cc:3772), and ProtoModelMerged::selectModel
scores the active trials against each constituent. Only the
`<eval_current_prototype>` element ITSELF was never read: the pass that
registered named models deliberately stopped short of it, since honoring it
changes which model every function is evaluated with.

So evalfp_current stayed null, every function fell back to `<default_proto>` =
__stdcall, whose <input> list is stack-only, and a register-passed argument was
not a *possible* parameter at all -- it never became a trial. The merged-model
machinery, fully ported and tested, had no live producer.

Reading the element is the whole fix. The nominated name resolves against the
model registry at `<default_proto>` time and is handed to each function through
the arch handle; the union then resolves PER FUNCTION against the trials that
are actually active, so a function that touches neither register still comes out
__stdcall. Precedence is explicit: `option protoeval <model>` outranks the
nomination (same slot), and the nomination outranks `option defaultprototype`
for an unlocked prototype, which is upstream's own ordering.
`<eval_called_prototype>` stays unread -- no vendored spec declares one.

Six vendored specs nominate a model (x86win, x86borland, x86gcc
__cdecl/__regparm, CR16, HCS12, HCS12X); every other language is untouched
whichever way the option is set.

Measured on a 3,153-function i386 PE: functions rendering as (void) 1,553 ->
602, functions carrying an ECX/EDX local read before written 2,128 -> 636.
Ground truth from a COFF object of the same project, whose MSVC-mangled names
encode the declared arity: 11 of 13 recovered arities match exactly, and the two
that do not are UNDER-recovery of a parameter the body never reads. Wrongness
audit over all 3,153 functions: 2,126 gained arguments, 0 lost any, and the set
of called functions is identical in 3,152 of 3,153. Selective rather than
blanket: a second i386 PE moves only 36 functions. Off-Windows, i386 gcc `ls`
moves 1 of 537 and libpthread.so.0 34 of 467 -- every one a genuine glibc
regparm internal recovered with the argument the source declares.

Speed (decompile-all, interleaved, min of 3): +0.27% / +1.91%, with an
unaffected-language control at +1.31% as the instrument's noise floor.

Gates: make test 675/675 PARITY OK with the raw flip (no re-pin, no datatest
opt-out), make test-stages PARITY OK (436/436) with one per-test opt-out on
ghdec-cspecprotos.xml (it exists to pin this very bug), make rust-test green,
make check-spec OK lenient and strict, kuna catalog --check OK. Web:
integrations/web parity 19/19 and the real browser UI renders the recovered
arguments.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01DQNaY1pwg8rskYRksC9qbh
@mahaloz
mahaloz merged commit 9a069a6 into main Aug 12, 2026
9 checks passed
@mahaloz
mahaloz deleted the feat/evalcurrentproto branch August 12, 2026 14:09
mahaloz added a commit that referenced this pull request Aug 12, 2026
…hile the catalog is 103

`main` is red on the cargo workspace suite at 80f8f29:

    thread 'fixture_has_all_102_settables' panicked at
    crates/kuna-decomp/tests/catalog_bytecompat.rs:82:5:
    assertion `left == right` failed
      left: 103
     right: 102

Nothing is wrong with the catalog. `phases.toml` has 103 settable rows, the fixture
`tests/fixtures/phase_catalog.json` carries 103, and `kuna_phases/tests.rs` correctly
asserts 103. Only this one test's three hard-coded copies of the same number were left
behind: #287 regenerated the fixture and bumped `kuna_phases/tests.rs`, but did not touch
`catalog_bytecompat.rs`, which #290 had last set to 102.

This is the fourth time this class of counter has broken in two days -- three of the four
during the round-4 merge train, where `main` and a branch had made the identical
`N -> N+1` edit and git auto-merged them silently at the wrong value with no conflict
marker. It reaches `main` because the workspace suite is skipped for internal PRs
(#274), so nothing runs it until after the merge.

Exactly one test failed in the whole CI run and the spec-tree guards never fired, so this
assert is the entire breakage.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01C8UQbPqALzdUQ3cLLjUeKH
mahaloz added a commit that referenced this pull request Aug 12, 2026
…hile the catalog is 103 (#292)

`main` is red on the cargo workspace suite at 80f8f29:

    thread 'fixture_has_all_102_settables' panicked at
    crates/kuna-decomp/tests/catalog_bytecompat.rs:82:5:
    assertion `left == right` failed
      left: 103
     right: 102

Nothing is wrong with the catalog. `phases.toml` has 103 settable rows, the fixture
`tests/fixtures/phase_catalog.json` carries 103, and `kuna_phases/tests.rs` correctly
asserts 103. Only this one test's three hard-coded copies of the same number were left
behind: #287 regenerated the fixture and bumped `kuna_phases/tests.rs`, but did not touch
`catalog_bytecompat.rs`, which #290 had last set to 102.

This is the fourth time this class of counter has broken in two days -- three of the four
during the round-4 merge train, where `main` and a branch had made the identical
`N -> N+1` edit and git auto-merged them silently at the wrong value with no conflict
marker. It reaches `main` because the workspace suite is skipped for internal PRs
(#274), so nothing runs it until after the merge.

Exactly one test failed in the whole CI run and the spec-tree guards never fired, so this
assert is the entire breakage.


Claude-Session: https://claude.ai/code/session_01C8UQbPqALzdUQ3cLLjUeKH

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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