diff --git a/src/coreclr/jit/importer.cpp b/src/coreclr/jit/importer.cpp index 925a3d7b77570a..4507bd9e432155 100644 --- a/src/coreclr/jit/importer.cpp +++ b/src/coreclr/jit/importer.cpp @@ -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); diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/KnownExternFunctions.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/KnownExternFunctions.cs index 2f8d2970ae266a..fa9a7a60949d29 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/KnownExternFunctions.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/KnownExternFunctions.cs @@ -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", @@ -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, diff --git a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs index d720daaaf2a8b9..973649ddfd9e0a 100644 --- a/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs +++ b/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs @@ -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; @@ -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 @@ -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) { diff --git a/src/tests/Regressions/coreclr/GitHub_138481/test138481.cs b/src/tests/Regressions/coreclr/GitHub_138481/test138481.cs new file mode 100644 index 00000000000000..c0f93c8eca4b62 --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_138481/test138481.cs @@ -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()); + } + } +} diff --git a/src/tests/Regressions/coreclr/GitHub_138481/test138481.csproj b/src/tests/Regressions/coreclr/GitHub_138481/test138481.csproj new file mode 100644 index 00000000000000..5ad9a2d72a5f3a --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_138481/test138481.csproj @@ -0,0 +1,8 @@ + + + + + + + +