Skip to content

Commit

Permalink
Fix field accessor for RVA field (dotnet#103705)
Browse files Browse the repository at this point in the history
* Revert "Separate RVA reflection change out"

This reverts commit 089ae12.

* Fix FieldAccessor getting RVA field

* Disable test on NativeAot
  • Loading branch information
huoyaoyuan authored Jun 24, 2024
1 parent 71ab8f1 commit b79c57e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
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
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;
}
}
}

0 comments on commit b79c57e

Please sign in to comment.