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

Fix DiagnosticMethodInfo.Create(Delegate) for valuetype methods #110782

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -313,7 +313,11 @@ internal DiagnosticMethodInfo GetDiagnosticMethodInfo()
}
else
{
functionPointer = ldftnResult;
nint unboxedPointer = RuntimeAugments.GetCodeTarget(ldftnResult);
if (unboxedPointer == ldftnResult)
unboxedPointer = RuntimeAugments.GetTargetOfUnboxingAndInstantiatingStub(ldftnResult);

functionPointer = unboxedPointer != 0 ? unboxedPointer : ldftnResult;
}
return RuntimeAugments.StackTraceCallbacksIfAvailable?.TryGetDiagnosticMethodInfoFromStartAddress(functionPointer);
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;

class Program
@@ -11,6 +13,7 @@ static int Main()
{
BodyFoldingTest.Run();
DiagnosticMethodInfoTests.Run();
Test108688Regression.Run();

string stackTrace = Environment.StackTrace;

@@ -83,4 +86,171 @@ public class Nested
}
}
}

class Test108688Regression
{
public static void Run()
{
{
DelStruct s;
Action del = s.InstanceMethod;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct s;
Action del = s.InstanceGenericMethod<int>;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceGenericMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct s;
Action del = s.InstanceGenericMethod<object>;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceGenericMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct<int> s;
Action del = s.InstanceMethod;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct<int> s;
Action del = s.InstanceGenericMethod<int>;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceGenericMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct<int> s;
Action del = s.InstanceGenericMethod<object>;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceGenericMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct<object> s;
Action del = s.InstanceMethod;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct<object> s;
Action del = s.InstanceGenericMethod<int>;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceGenericMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}

{
DelStruct<object> s;
Action del = s.InstanceGenericMethod<object>;
var dmi = DiagnosticMethodInfo.Create(del);
#if STRIPPED
if (dmi != null)
throw new Exception();
#else
if (dmi.Name != nameof(DelStruct.InstanceGenericMethod))
throw new Exception();
// Need to make sure it was stack trace metadata and not reflection metadata that provided this
if (GetMethodSecretly(del.Target.GetType(), dmi.Name) != null)
throw new Exception();
#endif
}
}

[UnconditionalSuppressMessage("", "IL2070")]
private static MethodInfo GetMethodSecretly(Type t, string name)
=> t.GetMethod(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

struct DelStruct
{
public void InstanceMethod() { }
public void InstanceGenericMethod<T>() { }
}

struct DelStruct<T>
{
public void InstanceMethod() { }
public void InstanceGenericMethod<U>() { }
}
}

}