Skip to content

Preserve invalid friend assembly error#127180

Merged
elinor-fung merged 8 commits into
mainfrom
copilot/fix-fileloadexception-net10
Jul 10, 2026
Merged

Preserve invalid friend assembly error#127180
elinor-fung merged 8 commits into
mainfrom
copilot/fix-fileloadexception-net10

Conversation

Copilot AI commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

When an assembly declares an invalid friend reference via InternalsVisibleTo (for example, specifying both PublicKey and PublicKeyToken), the runtime swallows the assembly name parse failure and throws a generic file load error. This change propagates the invalid assembly name error as an inner exception for the assembly load.

cc @dotnet/appmodel @AaronRobinsonMSFT

Before:

System.IO.FileLoadException: Unable to load file 'C:\...\InvalidFriendAssemblyName.dll'.

After:

System.IO.FileLoadException: Could not load file or assembly
   'InvalidFriendAssemblyName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. ... (0x80131621)
File name: 'InvalidFriendAssemblyName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
 ---> System.IO.FileLoadException: The given assembly name was invalid.
      FileName = 'Broken,PublicKey=…,PublicKeyToken=282361b99ded7e8e'

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

…T roundtrip

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/406c2ee2-d0ea-4ba1-a45b-52ac7d68918e

Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 20, 2026 21:49
Copilot AI changed the title [WIP] Fix FileLoadException when using old library with .NET 10 Preserve invalid friend assembly diagnostics by removing lossy HRESULT roundtrip Apr 20, 2026
Copilot AI requested a review from elinor-fung April 20, 2026 21:49
Wrap the friend-assembly name parse failure so the underlying reason (the invalid assembly identity) is kept as the inner exception, while the outer FileLoadException reports which assembly declared the invalid InternalsVisibleTo/friend reference.

Update the regression test to assert the outer exception identifies the declaring assembly and the inner exception carries the invalid name and message.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 8, 2026 22:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR changes the CoreCLR friend-assembly parsing error path so that failures while parsing InternalsVisibleTo / IgnoresAccessChecksTo assembly identities preserve the original parsing exception as an inner exception, rather than converting through a lossy HRESULT-only rethrow. It also adds a new loader regression test intended to validate the improved FileLoadException diagnostics for invalid friend assembly names.

Changes:

  • Wrap FriendAssemblyName_t::Init(displayName) in an exception-preserving EX_TRY/EX_CATCH, rethrowing as EEFileLoadException with the original exception as the inner exception.
  • Add a new loader regression test project InvalidFriendAssemblyName with an intentionally invalid InternalsVisibleTo string and assertions about the resulting FileLoadException chain.
Show a summary per file
File Description
src/coreclr/vm/assembly.cpp Preserve underlying assembly-name parse exceptions by wrapping Init failures as inner exceptions when building friend descriptors.
src/tests/Loader/classloader/regressions/InvalidFriendAssemblyName/InvalidFriendAssemblyName.csproj Adds a new regression test project to the loader/classloader regressions suite.
src/tests/Loader/classloader/regressions/InvalidFriendAssemblyName/InvalidFriendAssemblyName.cs Adds a test intended to validate diagnostics for invalid InternalsVisibleTo friend assembly identities.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

@elinor-fung elinor-fung changed the title Preserve invalid friend assembly diagnostics by removing lossy HRESULT roundtrip Preserve invalid friend assembly error Jul 8, 2026
Address review feedback: clarify that invoking the protected Object.MemberwiseClone drives the accessibility check that parses the invalid InternalsVisibleTo metadata.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 8, 2026 22:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

@elinor-fung elinor-fung marked this pull request as ready for review July 8, 2026 23:53
Mono silently ignores invalid InternalsVisibleTo/friend assembly names rather than throwing, so this diagnostic behavior is CoreCLR-only. Gate the test with CLRTestTargetUnsupported to avoid failing Mono test legs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 8, 2026 23:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new

Comment thread src/coreclr/vm/assembly.cpp
The test asserts the CoreCLR-specific FileLoadException thrown when parsing
an invalid InternalsVisibleTo friend name. The previous CLRTestTargetUnsupported
(RuntimeFlavor != coreclr) gate only excluded Mono; NativeAOT test legs run with
RuntimeFlavor=coreclr, so the test still ran there and failed because NativeAOT
resolves accessibility at compile time and never throws. Use a ConditionalFact
that runs only on CoreCLR JIT (not Mono, not NativeAOT).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c29dbe26-1e58-4183-a5f0-28a9fa5fd3ff
Copilot AI review requested due to automatic review settings July 9, 2026 19:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Comment thread src/coreclr/vm/assembly.cpp Outdated
EEFileLoadException::Throw asserts its inner exception is not transient. The
friend-name parse can fail with a transient exception (e.g. OutOfMemoryException),
which would trip that assertion or incorrectly report a transient failure as a
load failure. Switch the catch to EX_HOOK and only wrap non-transient
exceptions; transient exceptions are rethrown unchanged, matching the pattern
in PEAssembly::OpenSystem.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c29dbe26-1e58-4183-a5f0-28a9fa5fd3ff
Copilot AI review requested due to automatic review settings July 10, 2026 00:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 10, 2026 01:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 2

Comment thread src/coreclr/vm/assembly.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Weird FileLoadException when using old library and .NET 10

4 participants