Skip to content

NativeAOT: Custom runtime without AppContext class throw  #63862

Closed

Description

@kant2002

If you compile application with custom runtime where System.AppContext class is not defined, compiler crash

  Unhandled exception. ILCompiler.CodeGenerationFailedException: Code generation failed for method '[S.P.CompilerGenerated]Internal.CompilerGenerated.<Module>.SetAppContextSwitches()'
   ---> System.InvalidOperationException: Expected type 'System.AppContext' not found in module 'SeeSharpSnake'
     at Internal.IL.HelperExtensions.GetKnownType(ModuleDesc module, String namespace, String name) in ILCompiler.Compiler.dll:token 0x60000ff+0x35
     at Internal.IL.Stubs.StartupCode.AppContextInitializerMethod.EmitIL() in ILCompiler.Compiler.dll:token 0x6000144+0x5
     at Internal.IL.CoreRTILProvider.GetMethodIL(MethodDesc method) in ILCompiler.Compiler.dll:token 0x6000077+0x8f
     at ILCompiler.FeatureSwitchManager.GetMethodIL(MethodDesc method) in ILCompiler.Compiler.dll:token 0x60002c8+0x13
     at ILCompiler.Compilation.CombinedILProvider.GetMethodIL(MethodDesc method) in ILCompiler.Compiler.dll:token 0x6001049+0x0
     at ILCompiler.Compilation.ILCache.CreateValueFromKey(MethodDesc key) in ILCompiler.Compiler.dll:token 0x6001047+0x5
     at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) in ILCompiler.TypeSystem.dll:token 0x6000486+0x0
     at ILCompiler.Compilation.GetMethodIL(MethodDesc method) in ILCompiler.Compiler.dll:token 0x6000309+0x28
     at Internal.IL.ILImporter..ctor(ILScanner compilation, MethodDesc method, MethodIL methodIL) in ILCompiler.Compiler.dll:token 0x600008c+0x11
     at ILCompiler.ILScanner.CompileSingleMethod(ScannedMethodNode methodCodeNodeNeedingCode) in ILCompiler.Compiler.dll:token 0x60002ed+0x7
     --- End of inner exception stack trace ---
     at ILCompiler.ILScanner.CompileSingleMethod(ScannedMethodNode methodCodeNodeNeedingCode) in ILCompiler.Compiler.dll:token 0x60002ed+0x56
     at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() in System.Private.CoreLib.dll:token 0x6002ba2+0x14
     at System.Threading.ThreadPoolWorkQueue.Dispatch() in System.Private.CoreLib.dll:token 0x6002b7c+0xa0
     at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() in System.Private.CoreLib.dll:token 0x6002c66+0x67
     at System.Threading.Thread.StartCallback() in System.Private.CoreLib.dll:token 0x600280f+0xe

Which is there

MetadataType appContextType = Context.SystemModule.GetKnownType("System", "AppContext");

I think that initializer should produce empty method and do not crash the compiler. For example in UEFI maybe AppContext switches is not so good idea.

Activity

  1. ghost added
    untriaged
    on Jan 17, 2022
  2. MichalStrehovsky commented on Jan 17, 2022

    @MichalStrehovsky
    Member

    Add this to your project: https://github.com/MichalStrehovsky/zerosharp/blob/186dc165ec5cce3033200e1d4177b9bbb89fb7d9/no-runtime/noruntime.csproj#L35-L40

    The compiler needs to fail if it's told to set AppContext switches on the ILC command line but there's no AppContext class in the core library. It cannot ignore what the user told it to do.

  3. added and removed
    untriaged
    on Jan 17, 2022
  4. kant2002 commented on Jan 17, 2022

    @kant2002
    ContributorAuthor

    Add this to your project

    Done! Thanks.

    The compiler needs to fail if it's told to set AppContext switches on the ILC command line but there's no AppContext class in the core library.

    Maybe then we should produce nice error message which basically say something like that "AppContext switches passed, but no AppContext class not present in the runtime"?

  5. MichalStrehovsky commented on Jan 17, 2022

    @MichalStrehovsky
    Member

    People who don't use System.Private.CoreLib are already expected to read ILC source code because they're deep in unsupported territories. Expected type 'System.AppContext' not found in module 'SeeSharpSnake' is par for the course in that mode ("CoreLib doesn't have the thing I need in it").

    If you're up for submitting a pull request along the lines of

    if (initArgs == null)
    throw new Exception("Main() has parameters, but the class library doesn't support them");

    we can fix this, but otherwise I would just close this.

    But it will be just another TODO for "we need to throw something better than Exception" and "we'll need to put this into a RESX file so that it's localizable at some point".

  6. ghost locked as resolved and limited conversation to collaborators on Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions