Skip to content

NativeModuleWriter - Could not create header #305

Description

@acizmarik

Currently native writer fails for some assemblies.
This is caused by an unimplemented feature at the end of CreateHeaderSection in class NativeModuleWriter:

    //TODO: Support this too
    throw new ModuleWriterException("Could not create header");

Reproduce issue

  1. Create clean console application using dotnet new console
  2. Publish with ubuntu RID, for example, dotnet publish --runtime ubuntu.18.04-x64
  3. Try to load published System.Private.CoreLib.dll and write it back to disk
    var module = AssemblyDef.Load("System.Private.CoreLib.dll");
    ((ModuleDefMD)module.ManifestModule).NativeWrite("System.Private.CorLib.dll-2");

Exprected result: Assembly is stored to disk
Actual result: An exception is thrown

dnlib.DotNet.Writer.ModuleWriterException: Could not create header
   at dnlib.DotNet.Writer.NativeModuleWriter.CreateHeaderSection(IChunk& extraHeaderData)
   at dnlib.DotNet.Writer.NativeModuleWriter.WriteFile()
   at dnlib.DotNet.Writer.NativeModuleWriter.WriteImpl()
   at dnlib.DotNet.Writer.ModuleWriterBase.Write(Stream dest)
   at dnlib.DotNet.Writer.ModuleWriterBase.Write(String fileName)

Is there any chance that this will be implemented?
Also let me know if you need additional info.

Activity

  1. acizmarik commented on Sep 4, 2019

    @acizmarik
    Author

    I tried setting IsILOnly to true and saving it as normal IL library. It no longer crashes on assembly write. Also I noticed that the new assembly is significantly smaller (~14MB -> ~2,5MB).

    However, now it crashes when I try to use it. Whenever I try to load an assembly which uses new System.Private.CoreLib it crashes with following message: Failed to initialize CoreCLR, HRESULT: 0x80004005. The result is same when trying both on Windows and on Linux.

    When I try to run crossgen on new System.Private.CoreLib it throws this:

    Could not load file or assembly '<PATH>\System.Private.CoreLib.dll'. Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
    Error: compilation failed for ".\System.Private.CoreLib.dll" (0x80004005)

    I forgot to mention that my original approach (using native write) worked on Windows and I have been actually using it for the past 3 or 4 months. I started to experience issues once I tried to get it working on Linux.

  2. acizmarik commented on Sep 4, 2019

    @acizmarik
    Author

    When testing new / instrumented assemblies I am doing it with following settings:
    COMPlus_ReadyToRun = 0 - disable R2R
    COMPlus_JITMinOpts = 1 - disable JIT optimizations

  3. acizmarik commented on Sep 7, 2019

    @acizmarik
    Author

    You were right with your first comment. I did not realize that there is actually an ILLibrary flag in Cor20 header. Once the flag is cleared it works.

    Just one more questions regarding the fix:

    • Can I break something by "stripping" native code from assemblies? I am just curious if I should expect any problems or whether this is a completely valid operation.

    Anyway thank you for your help with this issue. This is truly a great library.

  4. acizmarik commented on Sep 7, 2019

    @acizmarik
    Author

    Ok. Thanks for you help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions