Skip to content

[Wasm] Add missing return buffer argument in wasm import thunks#128104

Draft
kg wants to merge 3 commits into
dotnet:mainfrom
kg:wasm-import-thunk-retbuff-arg
Draft

[Wasm] Add missing return buffer argument in wasm import thunks#128104
kg wants to merge 3 commits into
dotnet:mainfrom
kg:wasm-import-thunk-retbuff-arg

Conversation

@kg
Copy link
Copy Markdown
Member

@kg kg commented May 12, 2026

Fixes errors like

Failed to construct WebAssembly module for Webcil image: {
  wasmPath: '/Users/kg/AppData/Local/Temp/wasm-ryujit-runner/sandbox/IL/test-module.wasm',
  errorMessage: 'WebAssembly.Module(): Compiling function #39 failed: not enough arguments on the stack for call_indirect (need 3, got 2) @+8418'
}
Unhandled exception. System.BadImageFormatException: Bad IL format. The format of the file '/Users/kg/AppData/Local/Temp/wasm-ryujit-runner/sandbox/IL/test-module.wasm' is invalid.

#39 WasmDelayLoadHelper->EagerImports->SignaturePointer_ReadyToRunHelper_DelayLoad_MethodCall(ImportSection:MethodImports,Kind:DelayLoadHelper,Sig:S16p) ( i32, i32, i32 ) -> none (75 byte(s))

cc @davidwrighton @AndyAyersMS

Copilot AI review requested due to automatic review settings May 12, 2026 22:58
@kg kg added the arch-wasm WebAssembly architecture label May 12, 2026
@github-actions github-actions Bot added the area-crossgen2-coreclr only use for closed issues label May 12, 2026
// Pass return buffer argument if needed
if (hasRetBuffArg)
{
expressions.Add(Local.Get(1));
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always going to be local 1 or will there be cases where it's 0? Statics?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order is:

  • $sp if managed
  • this if instance method
  • retBuff if returning struct by-ref

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the WebAssembly import thunk emitter in the ReadyToRun AOT toolchain to account for signatures that require a hidden return-buffer (struct return) argument, preventing invalid wasm codegen at indirect call sites.

Changes:

  • Detect struct-return signatures that require a hidden return buffer argument.
  • Attempt to push the return buffer argument before the final call_indirect dispatch in the import thunk.

// Pass return buffer argument if needed
if (hasRetBuffArg)
{
expressions.Add(Local.Get(hasThis ? 1 : 0));
Comment on lines 129 to 132
int[] offsets = new int[methodSignature.Length];
bool[] isIndirectStructArg = new bool[methodSignature.Length];
bool hasRetBuffArg = _wasmSignature.SignatureString[0] == 'S';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-crossgen2-coreclr only use for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants