Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT] FormatException instead of ArgumentException in MethodBase.GetMethodFromHandle #113016

Closed
TickThunder opened this issue Feb 28, 2025 · 7 comments · Fixed by #113163
Closed
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged

Comments

@TickThunder
Copy link
Contributor

Description

The JIT throws the correct exception, but NativeAOT does not. And the if condition should use IsGenericType instead of IsConstructedGenericType to be consistent with the JIT and Mono.
like this 1e51807

Reproduction Steps

public class Test<T>
{
    public void f(int a)
    {
        Console.WriteLine(a);
    }
}
public static partial class Program
{
    public static void Main(string[] args)
    {
        MethodBase.GetMethodFromHandle(typeof(Test<int>).GetMethod("f").MethodHandle);
    }
}

Expected behavior

System.ArgumentException:“Cannot resolve method Void f(Int32) because the declaring type of the method handle PInvokeSamples.Test`1[T] is generic. Explicitly provide the declaring type to GetMethodFromHandle.”

Actual behavior

Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
   at System.ThrowHelper.ThrowFormatIndexOutOfRange() + 0x2b
   at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider, String, ReadOnlySpan`1) + 0x78b
   at System.String.FormatHelper(IFormatProvider, String, ReadOnlySpan`1) + 0x86
   at System.SR.Format(String, Object) + 0xb9
   at System.Reflection.Runtime.General.ReflectionCoreCallbacksImplementation.GetMethodFromHandle(RuntimeMethodHandle) + 0xdd

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Feb 28, 2025
@TickThunder
Copy link
Contributor Author

@MichalStrehovsky

Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

Actually it does aim to support the correct exception, however fails to format the message for the exception. You can take the implementation of CoreCLR at

Type? declaringType = m?.DeclaringType;
if (declaringType != null && declaringType.IsGenericType)
throw new ArgumentException(SR.Format(
SR.Argument_MethodDeclaringTypeGeneric,
m, declaringType.GetGenericTypeDefinition()));
.

@TickThunder
Copy link
Contributor Author

TickThunder commented Feb 28, 2025

I agree. I had assumed the inconsistencies in NativeAOT's conditional behavior were by design.
Maintain complete consistency with the JIT code. b58376b

@TickThunder
Copy link
Contributor Author

@agocke cc

@huoyaoyuan
Copy link
Member

Since you have found the root cause of the issue, you can submit a PR for it.

@TickThunder
Copy link
Contributor Author

Sure, let me try it.

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Mar 5, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants