Skip to content

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

@jbevain

Description

@jbevain

Version Used:
.NET 7.0.400-preview.23225.8

Steps to Reproduce:

Started as an investigation of jbevain/cecil#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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions