Skip to content

[AUTOMATED] Size an MSVC __cdecl import's by-value class at the call site (msvcbyval) - #1072

Merged
mahaloz merged 6 commits into
mainfrom
fix/fix990-byval
Oct 14, 2026
Merged

mahaloz merged 6 commits into
mainfrom
fix/fix990-byval

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

A call to an MSVC __cdecl import that takes a class by value keeps only the arguments before that class. With protos.exe from #990 (in the tree as decompiler/crates/kuna-analysis/tests/fixtures/msvcsig_protos_i386.exe), Arr Build(Arr &x, Arr &y) { return MakeStack(x, x, y); } calls ?MakeStack@@YA?AVArr@@V1@AAV1@1@Z:

kuna decompile-all decompiler/crates/kuna-analysis/tests/fixtures/msvcsig_protos_i386.exe --addr 0x401020
Arr * sub_401020(unsigned int a0,Arr *a1,Arr *a2,unsigned int a3)
{
  ...
  v3 = a3;
  v2 = a2;
  v1 = a0;
  Arr::Arr((Arr *)&v1,a2);
  MakeStack(a1);
  return a1;
}

After:

Arr * sub_401020(Arr *a0,Arr *a1,Arr *a2)
{
  unsigned int v1; // stack - 0x10

  Arr::Arr((Arr *)&v1,a1);
  MakeStack(a0,v1,a1,a2);
  return a0;
}

Fix

  • New --option msvcbyval on|off (default on). The name gives no size for a class passed by value, so msvcsig leaves the rest of the list open. The caller pops a __cdecl callee's arguments, so an add esp,k right after the call gives the argument bytes: the class takes k less the other parameters' slots, and the call gets the full declaration.
  • The cleanup is used only when a walk back over the call's block shows it is this call's alone: every other argument pushed, no earlier call's pop deferred into it, no local released with it. Otherwise the call keeps the open list, as before.
  • A slot whose address the caller takes stays a local. When that address is the this of an MSVC constructor or the hidden return pointer of an MSVC function returning a class by value, that call creates the slot's value, so the push ecx that only reserves the slot no longer makes ecx a parameter. MakeStack(GetArr(), x, y) prints GetArr((Arr *)&v1); MakeStack(a0,v1,a1,a2);.

Tests

tests/stages/kuna-msvcbyval.xml decompiles the #990 fixture and two nasm fixtures (msvcbyval_i386.asm sizes an 8-byte class, a GetArr() argument, and declines a deferred pop and a folded local; msvcbyval_ce_i386.asm declines a pop;pop or split cleanup, padding above a class-last argument, dead reserving pushes, an addressed local the cleanup releases, and an 8-byte class after a 4-byte slot). With the option off the calls keep only the parameters before the class; on, the full calls, with every unsound case declined in both passes. No datatest assertion moves (675/675). The 175 32-bit MSVC PE images used for msvcsig are byte-identical (none imports a __cdecl function taking a class by value); the sizing runs only on that path, so it is inert on the ELF speed corpus.

Known limits: one sub esp larger than the class, or a never-read pushed local above a plain struct, still reads as a wider class; the stack holds nothing that tells them apart.

Still open: a class passed by value to a callee-pops convention (Arr::operator=(Arr), __thiscall/__stdcall) is still refused by msvcsig, since its stack adjustment depends on the size the name does not give.

Fixes #990

🤖 Generated with Claude Code

https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz

mahaloz added a commit that referenced this pull request Oct 14, 2026
…eanup alone (#1072 review)

The cleanup `add esp,k` over-counts when padding, a local, or an earlier call's
deferred pop is folded into it, and a `pop` run was read as k=4. The class size
is now taken from the stack and cross-checked against the cleanup:

- Only a single `add esp,k` is a cleanup; a pop (run) is declined (a `pop ebp`
  epilogue cannot be told apart locally).
- A class with a copy constructor or a by-value return is sized by the one
  reservation the builder captured (`push ecx` = 4, `sub esp,n` = n), accepted
  only when known params + reservation == k. A trivially copyable aggregate is
  sized k - known only when no byte of the argument area is an unwritten gap.
- An 8-byte class after an odd number of 4-byte slots is declined (the cdecl
  model misaligns it); documented.

New fixture msvcbyval_ce_i386.exe (the review's ce4.asm) and stage assertions
cover pop-run, class-last padding, dead push-ecx locals, and the misaligned
8-byte class -- all declined -- plus the sized 6-byte-padded class.

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 14, 2026
mahaloz added a commit that referenced this pull request Oct 14, 2026
…s, and a split cleanup (#1072 delta review)

- Restore the guard the round-2 rewrite dropped: once the slot is placed, an
  address formed into a live byte of the argument area outside it is a local
  the same `add esp` releases, and the sizing is declined. A frameless
  `push ecx` local passed as `Fill(&r)` before `Take(*p, r)` printed
  `Take(v1._0_12_,a0)`; it now prints `Take(*a1,a1[1],v1)` as with the option off.
- Decline a cleanup followed by another bare `add esp` before the next call,
  branch or return (`add esp,8; add esp,4`).
- Document the two shapes the stack cannot tell apart: one reservation larger
  than the class, and a dead pushed local above a trivially copyable aggregate.

Fixture msvcbyval_ce_i386 gains podfold, split and a plain-struct control,
with stage assertions #19-#21.

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
mahaloz and others added 6 commits October 14, 2026 02:45
…site (msvcbyval)

An MSVC name does not state the size of a class passed by value, so msvcsig
left every parameter after it open, and the copy constructor that builds the
class in its outgoing slot made the call-site recovery refuse the rest:
`MakeStack(a1)` for `Arr MakeStack(Arr, Arr &, Arr &)`.

The new `msvcbyval` option (default on) reads the class's slot off the
caller's `add esp,k` right after the call, once a walk back over the block's
raw p-code shows that cleanup is this call's alone (no deferred pop, no folded
frame release), and gives the call the full declaration. A constructor whose
`this` is the slot creates the slot's value in heritage, and the slot stays a
mapped local, so Build prints `Arr::Arr((Arr *)&v1,a1); MakeStack(a0,v1,a1,a2);`
with three parameters.

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
… keep any address-taken slot mapped

`MakeStack(GetArr(), x, y)` has GetArr return straight into the by-value slot
through its hidden pointer; with only constructors recognised, the argument
printed as the `ecx` value pushed to reserve the slot. The walk now traces the
object pointer a builder receives (a constructor's `ecx`, or the first stack
argument of an MSVC declaration returning a class by value) back to the stack
pointer, and any by-value slot whose address the caller forms stays a mapped
local. The constructor call must be __thiscall.

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
…eanup alone (#1072 review)

The cleanup `add esp,k` over-counts when padding, a local, or an earlier call's
deferred pop is folded into it, and a `pop` run was read as k=4. The class size
is now taken from the stack and cross-checked against the cleanup:

- Only a single `add esp,k` is a cleanup; a pop (run) is declined (a `pop ebp`
  epilogue cannot be told apart locally).
- A class with a copy constructor or a by-value return is sized by the one
  reservation the builder captured (`push ecx` = 4, `sub esp,n` = n), accepted
  only when known params + reservation == k. A trivially copyable aggregate is
  sized k - known only when no byte of the argument area is an unwritten gap.
- An 8-byte class after an odd number of 4-byte slots is declined (the cdecl
  model misaligns it); documented.

New fixture msvcbyval_ce_i386.exe (the review's ce4.asm) and stage assertions
cover pop-run, class-last padding, dead push-ecx locals, and the misaligned
8-byte class -- all declined -- plus the sized 6-byte-padded class.

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
…o origin/main

Rebase onto b2ac4b5: re-derive the hard-coded settable counts (295), the
live-value count (130), recapture the catalog fixture, regenerate options.md,
and re-record the stages baseline (only the new MSVCBYVAL keys and the catalog
row-count rename change versus main).

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
…s, and a split cleanup (#1072 delta review)

- Restore the guard the round-2 rewrite dropped: once the slot is placed, an
  address formed into a live byte of the argument area outside it is a local
  the same `add esp` releases, and the sizing is declined. A frameless
  `push ecx` local passed as `Fill(&r)` before `Take(*p, r)` printed
  `Take(v1._0_12_,a0)`; it now prints `Take(*a1,a1[1],v1)` as with the option off.
- Decline a cleanup followed by another bare `add esp` before the next call,
  branch or return (`add esp,8; add esp,4`).
- Document the two shapes the stack cannot tell apart: one reservation larger
  than the class, and a dead pushed local above a trivially copyable aggregate.

Fixture msvcbyval_ce_i386 gains podfold, split and a plain-struct control,
with stage assertions #19-#21.

[AUTOMATED] change made by Claude Code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015sdZpNU5odw5dVyaxeSvvz
@mahaloz
mahaloz merged commit 6ba0e7d into main Oct 14, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/fix990-byval branch October 14, 2026 03:08
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.

[AUTOMATED] x86 PE: MSVC-mangled imports get a name but no prototype

1 participant