From e83991cf626b81e478cce014d96276ec2993a684 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 19 Jan 2018 22:47:47 +0100 Subject: [PATCH] Stop treating all calls to instance interface methods as callvirt (#15925) Fixes #15827. --- src/vm/jitinterface.cpp | 9 ++- .../Regressions/coreclr/15827/nonvirtualcall.il | 72 ++++++++++++++++++++++ .../coreclr/15827/nonvirtualcall.ilproj | 35 +++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 tests/src/Regressions/coreclr/15827/nonvirtualcall.il create mode 100644 tests/src/Regressions/coreclr/15827/nonvirtualcall.ilproj diff --git a/src/vm/jitinterface.cpp b/src/vm/jitinterface.cpp index af7e903..5340e2d 100644 --- a/src/vm/jitinterface.cpp +++ b/src/vm/jitinterface.cpp @@ -5397,7 +5397,8 @@ void CEEInfo::getCallInfo( directCall = true; } else - if (pTargetMD->GetMethodTable()->IsInterface() && pTargetMD->IsVirtual()) + // Backwards compat: calls to abstract interface methods are treated as callvirt + if (pTargetMD->GetMethodTable()->IsInterface() && pTargetMD->IsAbstract()) { directCall = false; } @@ -5439,6 +5440,12 @@ void CEEInfo::getCallInfo( } else #endif + if (pTargetMD->GetMethodTable()->IsInterface()) + { + // Handle interface methods specially because the Sealed bit has no meaning on interfaces. + devirt = !IsMdVirtual(pTargetMD->GetAttrs()); + } + else { DWORD dwMethodAttrs = pTargetMD->GetAttrs(); devirt = !IsMdVirtual(dwMethodAttrs) || IsMdFinal(dwMethodAttrs) || pTargetMD->GetMethodTable()->IsSealed(); diff --git a/tests/src/Regressions/coreclr/15827/nonvirtualcall.il b/tests/src/Regressions/coreclr/15827/nonvirtualcall.il new file mode 100644 index 0000000..0b9745a --- /dev/null +++ b/tests/src/Regressions/coreclr/15827/nonvirtualcall.il @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// +// Verifies that we're able to call virtual interface methods non-virtually. +// Corresponds to the C# "base(IFoo).Frob()" syntax. +// + +.assembly extern System.Runtime { } + +.assembly nonvirtualcall { } + +.class interface private abstract auto ansi IFoo +{ + .method public hidebysig newslot virtual instance int32 Frob() cil managed + { + ldc.i4 99 + ret + } +} + +.class interface private abstract auto ansi IBar + implements IFoo +{ + .method public hidebysig newslot virtual final instance int32 Frob() cil managed + { + .override IFoo::Frob + ldarg.0 + call instance int32 class IFoo::Frob() + ldc.i4.1 + add + ret + } +} + +.class private auto ansi beforefieldinit Fooer + extends [System.Runtime]System.Object + implements IBar +{ + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + ldarg.0 + call instance void [System.Runtime]System.Object::.ctor() + ret + } +} + +.method public hidebysig static int32 RunTest() cil managed +{ + newobj instance void Fooer::.ctor() + callvirt instance int32 IFoo::Frob() + ret +} + +.method public hidebysig static int32 Main() cil managed +{ + .entrypoint + + ldstr "DefaultImplementationsOfInterfaces" + call bool [System.Runtime]System.Runtime.CompilerServices.RuntimeFeature::IsSupported(string) + + // If default interfaces are not supported, consider the test successful. + brtrue DoRunTest + ldc.i4 100 + ret + +DoRunTest: + call int32 RunTest() + ret +} diff --git a/tests/src/Regressions/coreclr/15827/nonvirtualcall.ilproj b/tests/src/Regressions/coreclr/15827/nonvirtualcall.ilproj new file mode 100644 index 0000000..ade9cb7 --- /dev/null +++ b/tests/src/Regressions/coreclr/15827/nonvirtualcall.ilproj @@ -0,0 +1,35 @@ + + + + + nonvirtualcall + Debug + AnyCPU + 2.0 + {85DFC527-4DB1-595E-A7D7-E94EE1F8140D} + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 7a9bfb7d + true + true + Exe + BuildAndRun + 0 + + + + + False + + + + + + + + + + + + + -- 2.7.4