Skip to content
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
14 changes: 8 additions & 6 deletions src/coreclr/vm/reflectioninvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,16 @@ FCIMPL1(void*, RuntimeFieldHandle::GetStaticFieldAddress, ReflectFieldObject *pF
// IsFastPathSupported needs to checked before calling this method.
_ASSERTE(IsFastPathSupportedHelper(pFieldDesc));

PTR_BYTE base = 0;
if (!pFieldDesc->IsRVA())
if (pFieldDesc->IsRVA())
{
// For RVA the base is ignored and offset is used.
base = pFieldDesc->GetBase();
Module* pModule = pFieldDesc->GetModule();
return pModule->GetRvaField(pFieldDesc->GetOffset());
}
else
{
PTR_BYTE base = pFieldDesc->GetBase();
return PTR_VOID(base + pFieldDesc->GetOffset());
}

return PTR_VOID(base + pFieldDesc->GetOffset());
}
FCIMPLEND

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ private void Initialize()
{
_addressOrOffset = RuntimeFieldHandle.GetStaticFieldAddress(_fieldInfo);

if (fieldType.IsValueType)
if ((_fieldInfo.Attributes & FieldAttributes.HasFieldRVA) != 0)
{
_methodTable = (MethodTable*)fieldType.TypeHandle.Value;
_fieldAccessType = FieldAccessorType.StaticValueType;
}
else if (fieldType.IsValueType)
{
if (fieldType.IsEnum)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;

#pragma warning disable 0414
Expand Down Expand Up @@ -121,6 +123,30 @@ public void GetValueWithFunctionPointers(Type type, string name, object obj, obj
Assert.Equal(expected, fieldInfo.GetValue(obj));
}

// RVA field reflection is not supported in NativeAot
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]
public void GetValueFromRvaField()
{
byte[] valueArray = new byte[] { 1, 2, 3, 4, 5 };

// Roslyn uses SHA256 of raw data as data field name
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting; this is likely why the original RVA tests added with the fast field feature didn't fail.

FieldInfo fieldInfo = GetField(
typeof(FieldInfoTests).Assembly.GetType("<PrivateImplementationDetails>"),
"74F81FE167D99B4CB41D6D0CCDA82278CAEE9F3E2F25D5E5A3936FF3DCEC60D0");
Assert.NotNull(fieldInfo);
Assert.True(fieldInfo.IsStatic);
Assert.True((fieldInfo.Attributes & FieldAttributes.HasFieldRVA) != 0);

for (int i = 0; i < 5; i++)
{
// FieldAccessor uses slow path until the class is initialized.
// Make sure subsequent invocations also succeed.

object value = fieldInfo.GetValue(null);
Assert.Equal(valueArray, MemoryMarshal.CreateReadOnlySpan(ref Unsafe.As<object, RawData>(ref value).Data, valueArray.Length));
}
}

[Fact]
public void GetAndSetValueTypeFromStatic()
{
Expand Down Expand Up @@ -839,5 +865,10 @@ public static class SettingsForMyTypeThatThrowsInClassInitializer
{
public static bool s_shouldThrow = true;
}

private class RawData
{
public byte Data;
}
}
}