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 a year ago
  • Reactions: 4
  • Comments: 28 (27 by maintainers)

Most upvoted comments

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.