dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.03k stars 4.68k forks source link

NativeAOT: type initialization crashing at runtime #72782

Closed hez2010 closed 2 years ago

hez2010 commented 2 years ago

Description

Repro:

var a = TypeUtilities.Get1(2);
var b = TypeUtilities.Get2(2);
var c = TypeUtilities.Get3(2);
var d = TypeUtilities.Get4(2);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);

static class TypeUtilities
{
    private static readonly int[] Conversions =
    {
        0b101111111111101, // Boolean
        0b100001111111110, // Char
        0b101111111111111, // SByte
        0b101111111111111, // Byte
        0b101111111111111, // Int16
        0b101111111111111, // UInt16
        0b101111111111111, // Int32
        0b101111111111111, // UInt32
        0b101111111111111, // Int64
        0b101111111111111, // UInt64
        0b101111111111101, // Single
        0b101111111111101, // Double
        0b101111111111101, // Decimal
        0b110000000000000, // DateTime
        0b111111111111111, // String
    };

    private static readonly int[] ImplicitConversions =
    {
        0b000000000000001, // Boolean
        0b001110111100010, // Char
        0b001110101010100, // SByte
        0b001111111111000, // Byte
        0b001110101010000, // Int16
        0b001111111100000, // UInt16
        0b001110101000000, // Int32
        0b001111110000000, // UInt32
        0b001110100000000, // Int64
        0b001111000000000, // UInt64
        0b000110000000000, // Single
        0b000100000000000, // Double
        0b001000000000000, // Decimal
        0b010000000000000, // DateTime
        0b100000000000000, // String
    };

    private static readonly Type[] InbuiltTypes =
    {
        typeof(Boolean),
        typeof(Char),
        typeof(SByte),
        typeof(Byte),
        typeof(Int16),
        typeof(UInt16),
        typeof(Int32),
        typeof(UInt32),
        typeof(Int64),
        typeof(UInt64),
        typeof(Single),
        typeof(Double),
        typeof(Decimal),
        typeof(DateTime),
        typeof(String),
    };

    private static readonly Type[] NumericTypes =
    {
        typeof(Byte),
        typeof(Decimal),
        typeof(Double),
        typeof(Int16),
        typeof(Int32),
        typeof(Int64),
        typeof(SByte),
        typeof(Single),
        typeof(UInt16),
        typeof(UInt32),
        typeof(UInt64),
    };

    public static Type Get1(int index) => NumericTypes[index];
    public static Type Get2(int index) => InbuiltTypes[index];
    public static int Get3(int index) => ImplicitConversions[index];
    public static int Get4(int index) => Conversions[index];
}

Reproduction Steps

Build with NativeAOT and run.

Expected behavior

No exception should be thrown.

Actual behavior

Unhandled Exception: System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   at Internal.Runtime.TypeLoader.ExternalReferencesTable.GetRvaFromIndex(UInt32) + 0x24
   at TypeUtilities..cctor() + 0x9
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xc6
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x167
   at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
   at Program.<Main>$(String[]) + 0xc
   at NaotTest!<BaseAddress>+0x1b6113

Regression?

No response

Known Workarounds

No response

Configuration

.NET: latest nightly version ILC: nightly

Other information

No response

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

jkotas commented 2 years ago

This is dup of https://github.com/dotnet/roslyn/issues/62864 that was fixed by https://github.com/dotnet/roslyn/pull/62868 .