dotnet / linker

389 stars 126 forks source link

NRE in CustomAttributeSource.GetAssemblyFromCustomAttributeProvider #2990

Closed sbomer closed 2 years ago

sbomer commented 2 years ago

@vitek-karas @sbomer there's an ILLink exception with this update:

ILLink : error IL1012: IL Trimmer has encountered an unexpected error. Please report the issue at https://github.com/dotnet/linker/issues [/__w/1/s/src/coreclr/tools/aot/ILCompiler/ILCompiler.csproj]
##[error]ILLink(0,0): error IL1012: (NETCORE_ENGINEERING_TELEMETRY=Build) IL Trimmer has encountered an unexpected error. Please report the issue at https://github.com/dotnet/linker/issues
  Fatal error in IL Linker
  Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
     at Mono.Linker.CustomAttributeSource.GetAssemblyFromCustomAttributeProvider(ICustomAttributeProvider provider)
     at Mono.Linker.CustomAttributeSource.TryGetEmbeddedXmlInfo(ICustomAttributeProvider provider, AttributeInfo& xmlInfo)
     at Mono.Linker.CustomAttributeSource.TryGetCustomAttributeOrigin(ICustomAttributeProvider provider, CustomAttribute customAttribute, MessageOrigin& origin)
     at Mono.Linker.UnconditionalSuppressMessageAttributeState.GetSuppressionOrigin(Suppression suppression)
     at Mono.Linker.Steps.CheckSuppressionsDispatcher.Process(LinkContext context)
     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
     at Mono.Linker.Pipeline.Process(LinkContext context)
     at Mono.Linker.Driver.Run(ILogger customLogger)
     at Mono.Linker.Driver.Main(String[] args)

Originally posted by @akoeplinger in https://github.com/dotnet/runtime/issues/74238#issuecomment-1220711661

sbomer commented 2 years ago

I debugged it a bit and this happens when the provider is the CryptoConfigForwarder cctor, from this suppression: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfigForwarder.cs#L7-L10

The methodreference`s Module is null at the point of the stacktrace. I believe this is because the type is unused, and SweepStep detaches it from its module.

I checked that https://github.com/dotnet/linker/compare/main...sbomer:suppressionsNRE?expand=1 fixes the issue in runtime, but it needs some testing in the linker. We should be able to come up with a testcase that reproduces the issue.

@jkurdek would you mind taking a look?