runtime: Compiler can silently generate a bad image when there's 0xFFFF parameters in the module

Version Used: .NET 7.0.400-preview.23225.8

Steps to Reproduce:

Started as an investigation of https://github.com/jbevain/cecil/issues/913

  1. Compile this absolute unit of a compilation unit as the single file of a library project: https://gist.github.com/jbevain/9599e655adad445a6ee447add305a8bc
  2. Reference the library in a console project:
using System.Reflection;

var a = Assembly.LoadFile(typeof(P3).Assembly.Location);
foreach (var t in a.GetTypes())
{
    Console.WriteLine(t.FullName);
    foreach (var m in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
    {
        Console.WriteLine(m.Name);
        foreach (var p in m.GetParameters())
        {
            Console.WriteLine(p.Name);
        }
    }
}
  1. dotnet run the console project:

Expected Behavior: Should print:

...
F_65534
a

Actual Behavior:

...
F_65534
Unhandled exception. System.BadImageFormatException: File is corrupt. (0x8013110E)
   at System.Reflection.MetadataImport._Enum(IntPtr scope, Int32 type, Int32 parent, MetadataEnumResult& result)
   at System.Reflection.RuntimeParameterInfo.GetParameters(IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, ParameterInfo& returnParameter, Boolean fetchReturnParameter)
   at System.Reflection.RuntimeMethodInfo.GetParameters()
   at Program.<Main>$(String[] args) in C:\tmp\ReproFFFF\exe\Program.cs:line 10

Note that the last row of the Method table has a Param of 0x80000000 which is unexpected:

Screenshot 2023-06-23 at 5 50 21 PM

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 5
  • Comments: 30 (26 by maintainers)

Most upvoted comments

We seem to be affected by this or a similar issue with Ngen on .NET Framework 4.8.1.

An assembly is compiled with .NET 8 SDK targeting net48, and then passed to Ngen.exe to create a native image.

When the compiled assembly has a final Blob Heap entry with offset 0xFFFF, ngen generates a native image DLL that is corrupt. Ngen runs successfully with no error messages at the time of generating the image. However when a process tries to load the native image, it throws a BadImageFormatException.

None of these are concrete examples. A concrete example is a specific scenario. This is what we do today and it works great. This is how it would look like with the dummy and this is how it breaks. With details where it breaks and why that part cannot be adjusted to not break.