Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2704,7 +2704,7 @@ GenTree* Compiler::impImportLdvirtftn(GenTree* thisPtr,
call = gtNewVirtualFunctionLookupHelperCallNode(CORINFO_HELP_GVMLOOKUP_FOR_SLOT, TYP_I_IMPL, thisPtr,
dispatchCell);
}
#ifdef TARGET_WASMARCH
#if defined(TARGET_WASMARCH) || defined(TARGET_X86)
else if (IsNativeAot() && isInterface)
{
GenTree* dispatchCell = impLookupToTree(&pCallInfo->codePointerLookup, GTF_ICON_GLOBAL_PTR, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static Utf8String GetName(ReadyToRunHelper function, TargetDetails target
ReadyToRunHelper.GVMLookupForSlot => "RhpDispatchResolve",
ReadyToRunHelper.InterfaceDispatch => "RhpInterfaceDispatch",
ReadyToRunHelper.InterfaceDispatchGuarded => "RhpInterfaceDispatchGuarded",
ReadyToRunHelper.ResolveInterfaceMethodFast => "RhpResolveInterfaceMethodFast",
ReadyToRunHelper.ResolveInterfaceMethodFast => "RhpDispatchResolve",
ReadyToRunHelper.ResolveInterfaceMethod => "RhpResolveInterfaceMethod",

ReadyToRunHelper.StackProbe => "RhpStackProbe",
Expand Down Expand Up @@ -189,8 +189,7 @@ ReadyToRunHelper.CheckedWriteBarrier_ESI or

ReadyToRunHelper.GVMLookupForSlot => MakeSignature(nativeIntType, objectType, nativeIntType),
ReadyToRunHelper.ResolveInterfaceMethod => MakeSignature(nativeIntType, objectType, nativeIntType),
ReadyToRunHelper.ResolveInterfaceMethodFast => throw new NotImplementedException(
$"{nameof(ReadyToRunHelper.ResolveInterfaceMethodFast)} is not implemented by the runtime."),
ReadyToRunHelper.ResolveInterfaceMethodFast => MakeSignature(nativeIntType, objectType, nativeIntType),

ReadyToRunHelper.InterfaceDispatch or
ReadyToRunHelper.InterfaceDispatchGuarded => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,8 +1612,10 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
&& ((flags & CORINFO_CALLINFO_FLAGS.CORINFO_CALLINFO_LDFTN) == 0
|| _compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.Wasm32))
{
bool isWasm = _compilation.TypeSystemContext.Target.Architecture == TargetArchitecture.Wasm32;
pResult->kind = isWasm
// On x86, the interface dispatch slow path cannot unwind to the caller since it does not know the size of
// the callee-popped arguments, so the target is resolved with a helper call instead.
bool resolveWithHelper = _compilation.TypeSystemContext.Target.Architecture is TargetArchitecture.Wasm32 or TargetArchitecture.X86;
pResult->kind = resolveWithHelper
? CORINFO_CALL_KIND.CORINFO_VIRTUALCALL_LDVIRTFTN
: CORINFO_CALL_KIND.CORINFO_VIRTUALCALL_STUB;

Expand All @@ -1630,7 +1632,7 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
{
pResult->codePointerOrStubLookup.lookupKind.needsRuntimeLookup = false;
pResult->codePointerOrStubLookup.constLookup.accessType =
isWasm ? InfoAccessType.IAT_VALUE : InfoAccessType.IAT_PVALUE;
resolveWithHelper ? InfoAccessType.IAT_VALUE : InfoAccessType.IAT_PVALUE;
#pragma warning disable SA1001, SA1113, SA1115 // Commas should be spaced correctly
pResult->codePointerOrStubLookup.constLookup.addr = (void*)ObjectToHandle(
_compilation.NodeFactory.DispatchCell(targetMethod
Expand All @@ -1641,7 +1643,7 @@ private void getCallInfo(ref CORINFO_RESOLVED_TOKEN pResolvedToken, CORINFO_RESO
#pragma warning restore SA1001, SA1113, SA1115 // Commas should be spaced correctly
}

pResult->nullInstanceCheck = isWasm;
pResult->nullInstanceCheck = resolveWithHelper;
}
else if ((flags & CORINFO_CALLINFO_FLAGS.CORINFO_CALLINFO_LDFTN) == 0)
{
Expand Down
106 changes: 106 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_138481/test138481.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

// On x86, the NativeAOT interface dispatch slow path unwinds to the caller with an SP that does not account for the
// callee-popped arguments of the interface method. A GC while resolving the interface call then corrupted the stack
// walk when the caller was a funclet, or misreported pending pushed GC ref arguments in partially interruptible code.
public class Runtime_138481
{
private interface IManyArgs
{
int Sum(int a, int b, int c, int d, int e, int f);
}

[DynamicInterfaceCastableImplementation]
private interface IManyArgsImpl : IManyArgs
{
int IManyArgs.Sum(int a, int b, int c, int d, int e, int f) => a + b + c + d + e + f;
}

private sealed class Castable : IDynamicInterfaceCastable
{
public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
{
GC.Collect(2, GCCollectionMode.Forced, blocking: true, compacting: true);

// Reuse the memory of anything that was not reported so that a stale reference is visible.
for (int i = 0; i < 1000; i++)
s_junk[i % s_junk.Length] = new long[] { -1, -1, -1, -1 };

return typeof(IManyArgsImpl).TypeHandle;
}

public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) => true;
}

private static object[] s_junk = new object[64];

private sealed class Payload
{
public int Magic;
public Payload(int magic) => Magic = magic;
}

private const int ExpectedMagic = 0x12345678;

[MethodImpl(MethodImplOptions.NoInlining)]
private static IManyArgs Get() => (IManyArgs)(object)new Castable();

[MethodImpl(MethodImplOptions.NoInlining)]
private static void Throw() => throw new InvalidOperationException();

[MethodImpl(MethodImplOptions.NoInlining)]
private static Payload MakePayload() => new Payload(ExpectedMagic);

[MethodImpl(MethodImplOptions.NoInlining)]
private static int Outer(int a, int b, Payload obj, int sum)
{
if (obj.GetType() != typeof(Payload) || obj.Magic != ExpectedMagic)
{
Console.WriteLine($"Bad payload: magic=0x{obj.Magic:X}");
return -1;
}
return a + b + sum;
}

// Partially interruptible, no EH, no loops. `obj` is pushed on the stack for Outer
// before the nested interface call is made.
[MethodImpl(MethodImplOptions.NoInlining)]
private static int Caller()
{
return Outer(1, 2, MakePayload(), Get().Sum(1, 2, 3, 4, 5, 6));
}

[Fact]
public static void InterfaceCallInCatch()
{
int sum = 0;
for (int i = 0; i < 8; i++)
{
try
{
Throw();
}
catch (InvalidOperationException)
{
sum += Get().Sum(1, 2, 3, 4, 5, 6);
}
}

Assert.Equal(8 * 21, sum);
}

[Fact]
public static void InterfaceCallWithPendingGcArg()
{
for (int i = 0; i < 8; i++)
{
Assert.Equal(3 + 21, Caller());
}
}
}
8 changes: 8 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_138481/test138481.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Include="test138481.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestLibraryProjectPath)" />
</ItemGroup>
</Project>
Loading