Skip to content

Commit 80245dd

Browse files
authored
Provide More Detailed Information in System.TypeLoadException for Incorrect MethodImplAttribute Marking (dotnet#95833)
* Provide More Detailed Information in System.TypeLoadException for Incorrect MethodImplAttribute Marking * fix failing tests * disable tests on mono
1 parent 5dc63a6 commit 80245dd

4 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/coreclr/dlls/mscorrc/mscorrc.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ BEGIN
586586
BFA_METHOD_WITH_NONZERO_RVA "Method with non-zero RVA in an Import."
587587
BFA_ABSTRACT_METHOD_WITH_RVA "Abstract method with non-zero RVA."
588588
BFA_RUNTIME_METHOD_WITH_RVA "Runtime-implemented method with non-zero RVA."
589-
BFA_INTERNAL_METHOD_WITH_RVA "Internal call method with non_NULL RVA."
589+
BFA_INTERNAL_METHOD_WITH_RVA "Internal call method '%1.%3' with non-zero RVA."
590590
BFA_AB_METHOD_IN_AB_CLASS "Abstract method in non-abstract class."
591591
BFA_NONVIRT_AB_METHOD "Non-virtual abstract method."
592592
BFA_NONAB_NONCCTOR_METHOD_ON_INT "Non-abstract, non-.cctor method in an interface."

src/coreclr/vm/methodtablebuilder.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,11 @@ MethodTableBuilder::EnumerateClassMethods()
29712971
}
29722972
if(IsMiInternalCall(dwImplFlags))
29732973
{
2974-
BuildMethodTableThrowException(BFA_INTERNAL_METHOD_WITH_RVA);
2974+
bmtError->resIDWhy = BFA_INTERNAL_METHOD_WITH_RVA;
2975+
bmtError->dMethodDefInError = tok;
2976+
bmtError->szMethodNameForError = NULL;
2977+
bmtError->cl = GetCl();
2978+
BuildMethodTableThrowException(BFA_INTERNAL_METHOD_WITH_RVA, *bmtError);
29752979
}
29762980
}
29772981

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
using Xunit;
7+
8+
public class InternalMethodImplTest
9+
{
10+
[Fact, SkipOnMono("the error message is specific to coreclr")]
11+
public static int TypeLoadExceptionMessageContainsMethodNameWhenInternalCallOnlyMethodIsCalled()
12+
{
13+
if (TestLibrary.Utilities.IsNativeAot)
14+
{
15+
return 100; // unsupported on NativeAOT
16+
}
17+
18+
try
19+
{
20+
new F1();
21+
return -1;
22+
}
23+
catch (TypeLoadException ex)
24+
{
25+
return ex.Message.Contains("Internal call method 'F2.Foo' with non-zero RVA.") ? 100 : -1;
26+
}
27+
catch (Exception ex)
28+
{
29+
return -1;
30+
}
31+
}
32+
}
33+
34+
class F1
35+
{
36+
public F1()
37+
{
38+
var f2 = new F2();
39+
}
40+
}
41+
42+
class F2
43+
{
44+
[MethodImpl(MethodImplOptions.InternalCall)]
45+
public void Foo()
46+
{
47+
48+
}
49+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<ItemGroup>
3+
<Compile Include="InternalMethodImplTest.cs" />
4+
</ItemGroup>
5+
<ItemGroup>
6+
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
7+
</ItemGroup>
8+
</Project>

0 commit comments

Comments
 (0)