Skip to content

TypedByReference should be treated as a normal valuetype for calling convention purposes #71675

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

Merged
merged 3 commits into from
Jul 6, 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
7 changes: 7 additions & 0 deletions src/coreclr/vm/callingconvention.h
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,13 @@ int ArgIteratorTemplate<ARGITERATOR_BASE>::GetNextOffset()
TypeHandle thValueType;
CorElementType argType = this->GetNextArgumentType(m_argNum++, &thValueType);

// TypedReference behaves like a valuetype
if (argType == ELEMENT_TYPE_TYPEDBYREF)
{
argType = ELEMENT_TYPE_VALUETYPE;
thValueType = TypeHandle(g_TypedReferenceMT);
}

int argSize = MetaSig::GetElemSize(argType, thValueType);

m_argType = argType;
Expand Down
47 changes: 47 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_42732/test42732.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// This test is covering an issue where we would incorrectly compute the
// argument layout of the static method delegate thunk, due to an issue
// where we failed to handle ELEMENT_TYPE_TYPEDBYREF like the valuetype it is.
// This would not reproduce only Windows X64 due to the particular abi of that
// platform, but was found on Unix X64.

using System;
using System.Runtime.CompilerServices;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test is appropriate. The github number thing here makes sense with the JIT tests, but not with defined behaviors. I'd prefer to see this test added to an existing collection for TypedReference under src/tests/Loader/classloader or, since this is pure managed, in TypedReferenceTests.cs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable. I originally put the test here, as I guessed it was weird optimizer thing.... as it was only reproducing on Linux then I looked a bit deeper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of moving this, I'd also be fine with clearly documenting in the test the state we are validating here.


public class Test11611
{
struct TestStruct
{
public int a;
public int b;
}


public delegate void testDelegate(TypedReference tr);
public static testDelegate d;

static void test(TypedReference tr)
{
Type t = __reftype(tr);
Console.WriteLine($"tr = {t.Name}");
}
public static void Test()
{
TestStruct s = default;
var tr = __makeref(s);
test(tr);
d(tr); // this will crash due to __reftype(tr)
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static int Main(string[] args)
{
Console.WriteLine("About to run test");
d = test;
Test();
Console.WriteLine("Test complete run test");
return 100;
}
}
12 changes: 12 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_42732/test42732.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<ItemGroup>
<Compile Include="test42732.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
</ItemGroup>
</Project>