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

[release/7.0-staging] [mono] Use underlying type in RuntimeHelpers.GetSpanDataFrom #87021

Merged
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
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/icall.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetSpanDataFrom (MonoCl
return NULL;
}

MonoType *type = targetTypeHandle;
MonoType *type = mono_type_get_underlying_type (targetTypeHandle);
if (MONO_TYPE_IS_REFERENCE (type) || type->type == MONO_TYPE_VALUETYPE) {
mono_error_set_argument (error, "array", "Cannot initialize array of non-primitive type");
return NULL;
Expand Down
71 changes: 71 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_86865/test86865.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Reflection;

namespace test86865;

public class test86865
{
public static int Main()
{

// Regression test for https://github.com/dotnet/runtime/issues/86865
// Verify that the RuntimeHelpers.GetSpanDataFrom method underlying RuntimeHelpers.CreateSpan<T>
// works correctly with enums.

ReadOnlySpan<MyEnum> myEnums = new[]
{
MyEnum.A,
MyEnum.B,
MyEnum.C,
MyEnum.B,
MyEnum.C,
};

if (string.Join(", ", myEnums.ToArray()) != "A, B, C, B, C")
return 1;

var types = new Type[] {
typeof(RuntimeFieldHandle),
typeof(RuntimeTypeHandle),
typeof(int).MakeByRefType(),
};
var mi = typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("GetSpanDataFrom", BindingFlags.Static | BindingFlags.NonPublic, types);
if (mi == null)
return 2;

var pid = typeof(MyEnum).Assembly.GetType("<PrivateImplementationDetails>");
if (pid == null)
return 3;

var fi = pid.GetField("0B77DC554B4A81403D62BE25FB5404020AD451151D4203D544BF60E3FEDBD8AE", BindingFlags.Static | BindingFlags.NonPublic);
Copy link
Member

Choose a reason for hiding this comment

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

just curious, is this some kind of internal hash? is it stable?

Copy link
Member

@lambdageek lambdageek Jun 7, 2023

Choose a reason for hiding this comment

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

I believe it's a hash of the constant value. It's fairly stable - in local testing the same field is generated with the net8.0 and net7.0 SDKs when targeting both the net8.0 and net7.0 runtimes. In CI testing, for some reason the 7.0 branch creates a field with a name that is 1 character longer (ie that final "E" is here on the release/7.0-staging version of the test, but not in the main branch).

I could just blindly take the first matching field in the assembly (and maybe do some minimal checking of its attributes to make sure it's plausibly the correct one), but it seemed more straightforward to just hardcode the name and to return a distinct failure code and a print out of the available fields.

if (fi == null)
{
Console.WriteLine("Could not find the expected array data in <PrivateImplementationDetails>. The available static non-public fields are:");
foreach (var f in pid.GetFields(BindingFlags.Static | BindingFlags.NonPublic)) {
Console.WriteLine($" - '{f}'");
}
return 4;
}

var parms = new object[] {
fi.FieldHandle,
typeof(MyEnum).TypeHandle,
new int()
};
var result = mi.Invoke(null, parms);
if (result == null)
return 6;
if ((int)parms[2] != myEnums.Length)
return 7;

return 100;
}
}

enum MyEnum
{
A,
B,
C
}

9 changes: 9 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_86865/test86865.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="test86865.cs" />
</ItemGroup>
</Project>