Closed
Description
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)