Skip to content

Core library recognised only by file name: long-form primitive types in signatures when System.Runtime forwards to another core library #20976

Open

Description

@zion-sati

fsc recognises the assembly that defines System.Object only if its file name is System.Runtime, mscorlib, netstandard or System.Private.CoreLib. Suppose System.Runtime is a facade that forwards to a core library with another name, as on a runtime that names its own core library. Then fsc encodes primitive types in signatures in long form, as valuetype [CoreLib]System.Int32 and class [CoreLib]System.String, which ECMA-335 does not allow. Found while working on NetWasm.

Repro steps

  1. Save these three files in an empty folder.

    facade.cs copies .NET 11's reference pack and renames System.Runtime to MyCoreLib. It then adds a System.Runtime facade that forwards every public type to MyCoreLib:

    #:package Mono.Cecil@0.11.6
    // Copies a reference pack, renames System.Runtime (which defines System.Object) to the given name,
    // and adds a System.Runtime facade that forwards every public type to it.
    using Mono.Cecil;
    
    var (source, target, coreLibName) = (args[0], args[1], args[2]);
    Directory.CreateDirectory(target);
    foreach (var path in Directory.GetFiles(source, "*.dll"))
        if (Path.GetFileName(path) != "System.Runtime.dll")
            File.Copy(path, Path.Combine(target, Path.GetFileName(path)), overwrite: true);
    
    var coreLib = ModuleDefinition.ReadModule(Path.Combine(source, "System.Runtime.dll"), new ReaderParameters { InMemory = true });
    var version = coreLib.Assembly.Name.Version;
    coreLib.Assembly.Name.Name = coreLibName;
    coreLib.Name = coreLibName + ".dll";
    coreLib.Write(Path.Combine(target, coreLibName + ".dll"));
    
    var facade = AssemblyDefinition.CreateAssembly(
        new AssemblyNameDefinition("System.Runtime", version) { PublicKey = coreLib.Assembly.Name.PublicKey },
        "System.Runtime.dll", ModuleKind.Dll).MainModule;
    var coreLibRef = new AssemblyNameReference(coreLibName, version) { PublicKeyToken = coreLib.Assembly.Name.PublicKeyToken };
    facade.AssemblyReferences.Add(coreLibRef);
    foreach (var type in coreLib.Types.Where(t => t.IsPublic))
        facade.ExportedTypes.Add(new ExportedType(type.Namespace, type.Name, facade, coreLibRef) { Attributes = TypeAttributes.Forwarder });
    facade.Write(Path.Combine(target, "System.Runtime.dll"));

    sigs.cs prints each method's signature blob:

    using System.Reflection.Metadata;
    using System.Reflection.PortableExecutable;
    
    using var pe = new PEReader(File.OpenRead(args[0]));
    var md = pe.GetMetadataReader();
    foreach (var handle in md.MethodDefinitions)
    {
        var method = md.GetMethodDefinition(handle);
        Console.WriteLine($"{md.GetString(method.Name)}: {Convert.ToHexString(md.GetBlobBytes(method.Signature))}");
    }

    Program.fs

    [<EntryPoint>]
    let main argv =
        System.Console.WriteLine(Array.sum [| 1; 2; 3 |])
        0
  2. Build the facade set, compile Program.fs against the original reference pack and against the facade set, and print main's signature in each:

    DOTNET=$(dirname "$(realpath "$(which dotnet)")")
    REF=$DOTNET/packs/Microsoft.NETCore.App.Ref/11.0.0/ref/net11.0
    FSC=$DOTNET/sdk/11.0.100/FSharp
    dotnet run facade.cs -- "$REF" refs MyCoreLib
    dotnet "$FSC/fsc.dll" --nologo --noframework --targetprofile:netcore -o:original.dll -r:"$FSC/FSharp.Core.dll" $(printf -- '-r:%s ' "$REF"/*.dll) Program.fs
    dotnet "$FSC/fsc.dll" --nologo --noframework --targetprofile:netcore -o:facade.dll -r:"$FSC/FSharp.Core.dll" $(printf -- '-r:%s ' refs/*.dll) Program.fs
    dotnet run sigs.cs -- original.dll
    dotnet run sigs.cs -- facade.dll

Expected behavior

main has the same signature in both builds, int32 main(string[]):

main: 0001081D0E

08 is ELEMENT_TYPE_I4, 1D 0E is ELEMENT_TYPE_SZARRAY ELEMENT_TYPE_STRING.

Actual behavior

Both compile without errors or warnings, but the facade build is 5,120 bytes against 4,608, and main's signature is in long form:

main: 0001081D0E
main: 0001112D1D1239

11 2D is ELEMENT_TYPE_VALUETYPE with a TypeRef to [MyCoreLib]System.Int32. 1D 12 39 is ELEMENT_TYPE_SZARRAY ELEMENT_TYPE_CLASS with a TypeRef to [MyCoreLib]System.String. ECMA-335 II.23.2.16 (short form signatures) says "Only the short form is valid", and lists System.Int32 and System.String among the types that must use it.

The cause looks like tryFindEquivPrimaryAssembly in src/Compiler/Driver/CompilerImports.fs. It accepts an assembly as equivalent to the primary assembly if its manifest forwards System.Object. It also accepts one that defines System.Object, but only if PrimaryAssembly.IsPossiblePrimaryAssembly (src/Compiler/AbstractIL/il.fs) matches its file name against the four names. MyCoreLib defines System.Object but has another name. So types reached through System.Runtime's forwarders land in a scope that IsPossiblePrimaryAssemblyRef rejects, and are encoded like any other class.

A rule that does not depend on names would cover this: an assembly that defines System.Object and references no other assembly is the core library. That is how the C# compiler identifies it.

Known workarounds

Name the core library System.Private.CoreLib (or one of the other three names), or rename the reference before fsc runs and rename it back in the output.

Related information

  • macOS 26.6 (arm64)
  • .NET SDK 11.0.100, F# compiler 15.2.101.0 for F# 11.0
  • The same checks are on main (59c7477).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions