Skip to content

FieldAccessor can't read RVA field correctly #103207

@huoyaoyuan

Description

@huoyaoyuan

#102739 (comment)

var array = new int[] { 1, 2, 3, 4, 5 }; // Uses RVA field for initialization

foreach (var type in typeof(Program).Assembly.GetTypes())
{
    if (type.Name.Contains("PrivateImplementationDetails"))
    {
        foreach (var field in type.GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic))
        {
            _ = field.GetValue(null);
            _ = field.GetValue(null);
        }
    }
}

Currently, the first call is correctly handled by the slow path. Subsequent calls are handled by FieldAccessor and failing. The problems are:

  • RuntimeFieldHandle.GetStaticFieldAddress is returning the RVA offset, not its address. Trying to access it will cause access violation, and translated to NullReferenceException because it's typically at page 0.
  • The managed side of FieldAccessor tries to read the field as boxed. RVA fields are not boxed.

Test is added in #102739. I'm already working on the fix.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions