Skip to content

Commit be631a8

Browse files
lewingCopilot
andcommitted
Test that the Wasm generic context encodes like a leading pointer argument
InitHelpers.CallClassConstructor calls a shared generic class constructor as delegate*<void*, void>, so without an async continuation the context must lower to the same thunk key as a leading pointer argument. Document the invariant in readytorun-format.md. Co-authored-by: Copilot App <[email protected]>
1 parent 4b5c46e commit be631a8

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎docs/design/coreclr/botr/readytorun-format.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,9 @@ for parameters because their placement in the transition block depends on it.
11871187
**Hidden parameters** (inserted between `this` and explicit parameters, in order):
11881188

11891189
1. **Generic context** (`i`): present when the method requires an inst method desc or
1190-
method table argument.
1190+
method table argument. It is encoded like a pointer argument: without an async
1191+
continuation it occupies the same slot as a leading pointer argument, and callers such as
1192+
`InitHelpers.CallClassConstructor` pass it as one.
11911193
2. **Async continuation** (`a`): present for async calls.
11921194

11931195
Note: the hidden return buffer pointer is **not** encoded in the signature string. Its

‎src/coreclr/tools/aot/ILCompiler.ReadyToRun.Tests/WasmArgumentLayoutTests.cs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ public void ClosedStaticDelegateSignaturePreservesHiddenArguments(WasmLowering.L
108108
Assert.True(lowered.FuncType.Returns.Types.IsEmpty);
109109
}
110110

111+
/// <summary>
112+
/// Without an async continuation the generic context occupies the slot of a leading pointer argument,
113+
/// and callers rely on that: InitHelpers.CallClassConstructor calls a shared generic class constructor
114+
/// as delegate*&lt;void*, void&gt;. Both shapes must produce the same thunk key.
115+
/// </summary>
116+
[Theory]
117+
[InlineData(false, false, false, "vip")]
118+
[InlineData(false, false, true, "viip")]
119+
[InlineData(true, false, true, "vTiip")]
120+
[InlineData(false, true, true, "S16iip")]
121+
[InlineData(true, true, true, "S16Tiip")]
122+
public void GenericContextEncodesAsLeadingPointerArgument(bool hasThis, bool returnsStruct, bool hasArgument, string expectedSignature)
123+
{
124+
ReadyToRunCompilerContext context = CreateWasmContext();
125+
TypeDesc returnType = returnsStruct ? MakeAlignedEightBlob(context, 16) : context.GetWellKnownType(WellKnownType.Void);
126+
MethodSignatureFlags flags = hasThis ? MethodSignatureFlags.None : MethodSignatureFlags.Static;
127+
TypeDesc[] arguments = hasArgument ? [context.GetWellKnownType(WellKnownType.Int32)] : [];
128+
MethodSignature withContext = new MethodSignature(flags, 0, returnType, arguments);
129+
MethodSignature withPointer = new MethodSignature(flags, 0, returnType, [context.GetWellKnownType(WellKnownType.IntPtr), .. arguments]);
130+
131+
Assert.Equal(expectedSignature, WasmLowering.GetSignature(withContext, WasmLowering.LoweringFlags.HasGenericContextArg).SignatureString);
132+
Assert.Equal(expectedSignature, WasmLowering.GetSignature(withPointer, WasmLowering.LoweringFlags.None).SignatureString);
133+
}
134+
111135
[Theory]
112136
[InlineData(false, false, WasmLowering.LoweringFlags.HasGenericContextArg | WasmLowering.LoweringFlags.IsAsyncCall, "iiaip",
113137
"GenericContext@8:1 AsyncContinuation@16:2 Argument@24:3")]

0 commit comments

Comments
 (0)