Skip to content

[Mono] FieldInfo.SetValue does not do an init check for readonly static fields #37903

Closed

Description

@lambdageek

Related to #37796 for Mono.

Given a program like this:

using System;
using System.Runtime.CompilerServices;

internal class Program
{
    private static void Main(string[] args)
    {
        typeof(InlineReadOnly).GetField(nameof(InlineReadOnly.InlineMagicNumber)).SetValue(null, 0x123456789);

        InlineReadOnly.Test();

	typeof(InlineReadOnly).GetField(nameof(InlineReadOnly.InlineMagicNumber)).SetValue(null, 42);

        InlineReadOnly.Test2();

    }
}

public static class InlineReadOnly
{
	public static readonly long InlineMagicNumber = 0; //InlineReadOnly.GetInlineMagicNumber();

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Test() => Console.WriteLine(InlineReadOnly.InlineMagicNumber);

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Test2() => Console.WriteLine(InlineReadOnly.InlineMagicNumber);

    public static long GetInlineMagicNumber() => 0;
}

Mono prints:

4886718345
42

The expected behavior is to throw a FieldAccessException (See #37849)

Note that Mono behavior here is different from CoreCLR - we can reset the field value multiple times.

(The two NoInlining methods are because we inline the value 0x123456789 when we JIT Test, so it's hard to observe that the second SetValue had an effect)

Activity

  1. added
    area-CodeGen-coreclr
    untriaged
    on Jun 15, 2020
  2. added and removed
    area-CodeGen-coreclr
    on Jun 15, 2020
  3. lambdageek commented on Jun 22, 2020

    @lambdageek
    MemberAuthor

    Note we will have to do the work twice: once for mini and once for the interpreter.

  4. added this to the 6.0.0 milestone on Jul 6, 2020
  5. removed
    untriaged
    on Jul 6, 2020
  6. modified the milestones: 6.0.0, 7.0.0 on Jul 16, 2021
  7. SamMonoRT commented on Jul 16, 2021

    @SamMonoRT
    Member

    Moving to 7.0.0

  8. modified the milestones: 7.0.0, 8.0.0 on Aug 4, 2022
  9. modified the milestones: 8.0.0, 9.0.0 on Jul 31, 2023
  10. lambdageek commented on Feb 3, 2024

    @lambdageek
    MemberAuthor

    #97829 has better notes of what is going on with SetValue and a hint for how to fix it (using a check of the type initialization hash).

    It looks like in this issue I thought there's work to be done in the JIT, too. But that needs to be verified (see the comment on 97829 about the stsfld outside a cctor

  11. modified the milestones: 9.0.0, Future on Feb 9, 2024
  12. lambdageek commented on Jul 26, 2024

    @lambdageek
    MemberAuthor

    Closing in favor of #97829 which has better notes

  13. locked and limited conversation to collaborators on Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions