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 value type unboxing bug #69815

Merged
merged 3 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/coreclr/vm/comdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,7 @@ FCIMPL1(ReflectMethodObject *, COMDelegate::FindMethodHandle, Object* refThisIn)
HELPER_METHOD_FRAME_BEGIN_RET_1(refThis);

pMD = GetMethodDesc(refThis);
pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, TypeHandle(pMD->GetMethodTable()), pMD->GetMethodInstantiation());
pRet = pMD->GetStubMethodInfo();
HELPER_METHOD_FRAME_END();

Expand Down
11 changes: 3 additions & 8 deletions src/coreclr/vm/genmeth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,12 +1236,11 @@ MethodDesc::FindOrCreateAssociatedMethodDesc(MethodDesc* pDefMD,
if (methodInst.GetNumArgs() != pMethod->GetNumGenericMethodArgs())
COMPlusThrow(kArgumentException);

// we base the creation of an unboxing stub on whether the original method was one already
// that keeps the reflection logic the same for value types
// we need unboxing stubs for virtual methods on value types
pInstMD = MethodDesc::FindOrCreateAssociatedMethodDesc(
pMethod,
pMT,
pMethod->IsUnboxingStub(),
instType.IsValueType() && pMethod->IsVirtual(),
methodInst,
FALSE, /* no allowInstParam */
TRUE /* force remotable method (i.e. inst wrappers for non-generic methods on generic interfaces) */);
Expand All @@ -1263,12 +1262,8 @@ MethodDesc::FindOrCreateAssociatedMethodDesc(MethodDesc* pDefMD,
// - non generic method on a generic interface
//

// we base the creation of an unboxing stub on whether the original method was one already
// that keeps the reflection logic the same for value types

// we need unboxing stubs for virtual methods on value types unless the method is generic
BOOL fNeedUnboxingStub = pMethod->IsUnboxingStub() ||
( instType.IsValueType() && pMethod->IsVirtual() );
BOOL fNeedUnboxingStub = instType.IsValueType() && pMethod->IsVirtual();
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

pInstMD = MethodDesc::FindOrCreateAssociatedMethodDesc(
pMethod, /* the original MD */
Expand Down
45 changes: 45 additions & 0 deletions src/libraries/System.Runtime/tests/System/DelegateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -435,6 +436,50 @@ private static void IntIntMethod(int expected, int actual)
Assert.Equal(expected, actual);
}

[Fact]
public static void SameMethodObtainedViaDelegateAndReflectionAreSameForClass()
{
var m1 = ((MethodCallExpression)((Expression<Action>)(() => new Class().M())).Body).Method;
var m2 = new Action(new Class().M).Method;
Assert.True(m1.Equals(m2));
}

[Fact]
public static void SameMethodObtainedViaDelegateAndReflectionAreSameForStruct()
{
var m1 = ((MethodCallExpression)((Expression<Action>)(() => new Struct().M())).Body).Method;
var m2 = new Action(new Struct().M).Method;
Assert.True(m1.Equals(m2));
}

[Fact]
public static void SameGenericMethodObtainedViaDelegateAndReflectionAreSameForClass()
{
var m1 = ((MethodCallExpression)((Expression<Action>)(() => new ClassG().M<string, object>())).Body).Method;
var m2 = new Action(new ClassG().M<string, object>).Method;
Assert.True(m1.Equals(m2));
Assert.True(m1.GetHashCode().Equals(m2.GetHashCode()));
Assert.Equal(m1.MethodHandle.Value, m2.MethodHandle.Value);
}

[Fact]
public static void SameGenericMethodObtainedViaDelegateAndReflectionAreSameForStruct()
{
var m1 = ((MethodCallExpression)((Expression<Action>)(() => new StructG().M<string, object>())).Body).Method;
var m2 = new Action(new StructG().M<string, object>).Method;
Assert.True(m1.Equals(m2));
Assert.True(m1.GetHashCode().Equals(m2.GetHashCode()));
Assert.Equal(m1.MethodHandle.Value, m2.MethodHandle.Value);
}

class Class { internal void M() { } }

struct Struct { internal void M() { } }

class ClassG { internal void M<Key, Value>() { } }

struct StructG { internal void M<Key, Value>() { } }

private delegate void IntIntDelegate(int expected, int actual);
private delegate void IntIntDelegateWithDefault(int expected, int actual = 7);

Expand Down