Skip to content

Commit

Permalink
Fix value type unboxing bug (#69815)
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed May 26, 2022
1 parent cb1fd54 commit 5734a23
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
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();

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

0 comments on commit 5734a23

Please sign in to comment.