Skip to content

[ILLink]: Using InternalsVisibleTo in System.Private.CoreLib breaks a runtime build #93247

Open
@arturek

Description

@arturek

Description

When the InternalsVisibleTo attribute is used in the System.Private.CoreLib project (where it is also defined), ILLink crashes (because of NullReferenceException) and prints ILLink : error IL1012: IL Trimmer has encountered an unexpected error.

I found this problem while working on adding tests to #92534.

Reproduction Steps

  1. apply the commit arturek/runtime@677f9c2
  2. run build.cmd clr+libs -c Debug in the x64 console (Release mode fails the same way)

Expected behavior

The runtime and libraries are successfully built.

Actual behavior

The following error is printed:

  sfx -> Trimming win-x64 shared framework assemblies with ILLinker...
ILLink : error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://aka.ms/report-illink [C:\r\gh
\dotnet\rt2\runtime\src\libraries\sfx.proj]
  Fatal error in IL Linker
  Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
     at Mono.Linker.UnconditionalSuppressMessageAttributeState.GetModuleFromProvider(ICustomAttributeProvider provider) in C:\r\gh\dotn
  et\rt2\runtime\src\tools\illink\src\linker\Linker\UnconditionalSuppressMessageAttributeState.cs:line 252
     at Mono.Linker.UnconditionalSuppressMessageAttributeState.TryGetSuppressionsForProvider(ICustomAttributeProvider provider, Diction
  ary`2& suppressions) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\UnconditionalSuppressMessageAttributeState.cs:l
  ine 175
     at Mono.Linker.UnconditionalSuppressMessageAttributeState.IsSuppressedOnElement(Int32 id, ICustomAttributeProvider provider, Suppr
  essMessageInfo& info) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\UnconditionalSuppressMessageAttributeState.cs:
  line 146
     at Mono.Linker.UnconditionalSuppressMessageAttributeState.IsSuppressed(Int32 id, ICustomAttributeProvider warningOrigin, SuppressM
  essageInfo& info) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\UnconditionalSuppressMessageAttributeState.cs:line
   111
     at Mono.Linker.UnconditionalSuppressMessageAttributeState.IsSuppressed(Int32 id, MessageOrigin warningOrigin, SuppressMessageInfo&
   info) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\UnconditionalSuppressMessageAttributeState.cs:line 82
     at Mono.Linker.LinkContext.IsWarningSuppressed(Int32 warningCode, String subcategory, MessageOrigin origin) in C:\r\gh\dotnet\rt2\
  runtime\src\tools\illink\src\linker\Linker\LinkContext.cs:line 744
     at Mono.Linker.MessageContainer.CreateWarningMessageContainer(LinkContext context, MessageOrigin origin, DiagnosticId id, WarnVers
  ion version, String subcategory, String[] args) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\MessageContainer.cs:
  line 185
     at Mono.Linker.MessageContainer.CreateWarningMessage(LinkContext context, MessageOrigin origin, DiagnosticId id, WarnVersion versi
  on, String[] args) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\MessageContainer.cs:line 132
     at Mono.Linker.LinkContext.LogWarning(MessageOrigin origin, DiagnosticId id, String[] args) in C:\r\gh\dotnet\rt2\runtime\src\tool
  s\illink\src\linker\Linker\LinkContext.cs:line 595
     at Mono.Linker.Steps.CheckSuppressionsDispatcher.Process(LinkContext context) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\l
  inker\Linker.Steps\CheckSuppressionsDispatcher.cs:line 33
     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Lin
  ker\Pipeline.cs:line 178
     at Mono.Linker.Pipeline.Process(LinkContext context) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\Pipeline.cs:
  line 167
     at Mono.Linker.Driver.Run(ILogger customLogger) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\Driver.cs:line 81
  4
     at Mono.Linker.Driver.Main(String[] args) in C:\r\gh\dotnet\rt2\runtime\src\tools\illink\src\linker\Linker\Driver.cs:line 68

Build FAILED.

ILLink : error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://aka.ms/report-illink [C:\r\gh
\dotnet\rt2\runtime\src\libraries\sfx.proj]
    0 Warning(s)
    1 Error(s)

Regression?

Probably not a regression, as this functionality was never used there.

Known Workarounds

no workarounds found

Configuration

.Net SDK 8.0.100-rc.1.23455.8 (x64)
Windows 11 Home 22H2
x64

Other information

The error happens because the DeclaringType property of the Mono.Cecil.MethodDefinition instance (representing the System.Int32 GetClassFactoryForTypeInContext(Internal.Runtime.InteropServices.ComActivationContextInternal*,System.IntPtr) method) is null. When this instance is created its DeclaringType is correct, but later it becomes null. I don't know when.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink.NET linker development as well as trimming analyzers

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions