Skip to content

Commit

Permalink
Provide More Detailed Information in System.TypeLoadException for Inc…
Browse files Browse the repository at this point in the history
…orrect MethodImplAttribute Marking (dotnet#95833)

* Provide More Detailed Information in System.TypeLoadException for Incorrect MethodImplAttribute Marking

* fix failing tests

* disable tests on mono
  • Loading branch information
pedrobsaila authored Dec 15, 2023
1 parent 5dc63a6 commit 80245dd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/dlls/mscorrc/mscorrc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ BEGIN
BFA_METHOD_WITH_NONZERO_RVA "Method with non-zero RVA in an Import."
BFA_ABSTRACT_METHOD_WITH_RVA "Abstract method with non-zero RVA."
BFA_RUNTIME_METHOD_WITH_RVA "Runtime-implemented method with non-zero RVA."
BFA_INTERNAL_METHOD_WITH_RVA "Internal call method with non_NULL RVA."
BFA_INTERNAL_METHOD_WITH_RVA "Internal call method '%1.%3' with non-zero RVA."
BFA_AB_METHOD_IN_AB_CLASS "Abstract method in non-abstract class."
BFA_NONVIRT_AB_METHOD "Non-virtual abstract method."
BFA_NONAB_NONCCTOR_METHOD_ON_INT "Non-abstract, non-.cctor method in an interface."
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/vm/methodtablebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2971,7 +2971,11 @@ MethodTableBuilder::EnumerateClassMethods()
}
if(IsMiInternalCall(dwImplFlags))
{
BuildMethodTableThrowException(BFA_INTERNAL_METHOD_WITH_RVA);
bmtError->resIDWhy = BFA_INTERNAL_METHOD_WITH_RVA;
bmtError->dMethodDefInError = tok;
bmtError->szMethodNameForError = NULL;
bmtError->cl = GetCl();
BuildMethodTableThrowException(BFA_INTERNAL_METHOD_WITH_RVA, *bmtError);
}
}

Expand Down
49 changes: 49 additions & 0 deletions src/tests/Loader/classloader/MethodImpl/InternalMethodImplTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 Xunit;

public class InternalMethodImplTest
{
[Fact, SkipOnMono("the error message is specific to coreclr")]
public static int TypeLoadExceptionMessageContainsMethodNameWhenInternalCallOnlyMethodIsCalled()
{
if (TestLibrary.Utilities.IsNativeAot)
{
return 100; // unsupported on NativeAOT
}

try
{
new F1();
return -1;
}
catch (TypeLoadException ex)
{
return ex.Message.Contains("Internal call method 'F2.Foo' with non-zero RVA.") ? 100 : -1;
}
catch (Exception ex)
{
return -1;
}
}
}

class F1
{
public F1()
{
var f2 = new F2();
}
}

class F2
{
[MethodImpl(MethodImplOptions.InternalCall)]
public void Foo()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Compile Include="InternalMethodImplTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>

0 comments on commit 80245dd

Please sign in to comment.