Skip to content

Commit ba1f318

Browse files
authored
[mini] Don't add unbox tramopline on generic DIM calls (#58521)
* [mini] Don't add unbox tramopline on generic DIM calls Don't unbox a valuetype `this` if the generic method is a DIM Fixes #58394 * Add regression test
1 parent 23a58e3 commit ba1f318

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/mono/mono/mini/mini-trampolines.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,13 @@ common_call_trampoline (host_mgreg_t *regs, guint8 *code, MonoMethod *m, MonoVTa
632632
return NULL;
633633

634634
if (generic_virtual || variant_iface) {
635-
if (m_class_is_valuetype (vt->klass)) /*FIXME is this required variant iface?*/
635+
if (m_class_is_valuetype (vt->klass) && !mini_method_is_default_method (m)) /*FIXME is this required variant iface?*/
636636
need_unbox_tramp = TRUE;
637637
} else if (orig_vtable_slot) {
638-
if (m_class_is_valuetype (m->klass))
638+
if (m_class_is_valuetype (m->klass)) {
639+
g_assert (!mini_method_is_default_method (m));
639640
need_unbox_tramp = TRUE;
641+
}
640642
}
641643

642644
addr = mini_add_method_trampoline (m, compiled_method, need_rgctx_tramp, need_unbox_tramp);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace GenericDimValuetypeBug
8+
{
9+
class Program
10+
{
11+
static int Main()
12+
{
13+
if (RunOne() != 17)
14+
return 1;
15+
if (RunTwo() != 23)
16+
return 2;
17+
return 100;
18+
}
19+
20+
[MethodImpl(MethodImplOptions.NoInlining)]
21+
public static int RunOne()
22+
{
23+
return (new Foo() { x = 17 } as IFoo).NoCrash();
24+
}
25+
26+
[MethodImpl(MethodImplOptions.NoInlining)]
27+
public static int RunTwo()
28+
{
29+
return (new Foo() { x = 23 } as IFoo).Crash<int>();
30+
}
31+
}
32+
33+
interface IFoo
34+
{
35+
int Crash<T>() => Bla();
36+
37+
int NoCrash() => Bla();
38+
39+
int Bla();
40+
}
41+
42+
struct Foo: IFoo
43+
{
44+
public int x;
45+
public int Bla() => x;
46+
}
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4+
<OutputType>Exe</OutputType>
5+
<CLRTestKind>BuildAndRun</CLRTestKind>
6+
<CLRTestPriority>0</CLRTestPriority>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<Compile Include="$(MSBuildProjectName).cs" />
10+
</ItemGroup>
11+
</Project>

0 commit comments

Comments
 (0)