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

FieldAccessor can't read RVA field correctly #103207

Closed
huoyaoyuan opened this issue Jun 9, 2024 · 1 comment · Fixed by #103705
Closed

FieldAccessor can't read RVA field correctly #103207

huoyaoyuan opened this issue Jun 9, 2024 · 1 comment · Fixed by #103705

Comments

@huoyaoyuan
Copy link
Member

#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.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 9, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@steveharter steveharter added this to the 9.0.0 milestone Jun 10, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jun 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants