Skip to content

Fix UnreachableBody with only ref to assembly.#127090

Merged
sbomer merged 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-ub-issue-with-reference
Apr 22, 2026
Merged

Fix UnreachableBody with only ref to assembly.#127090
sbomer merged 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-ub-issue-with-reference

Conversation

@mrvoorhe
Copy link
Copy Markdown
Contributor

When an unreachable body contains a reference to another assembly, and there are no other references to that assembly, the linker would retain the reference but delete the assembly.

This was happening because the SweepStep was running before CodeRewriterStep. Which meant that when SweepStep.SweepAssemblyReferences iterated all of the types references in the assembly it would visit type references to method bodies that were going to be stubbed. By moving the CodeRewriterStep ahead of the sweep step we ensure that the SweepStep is visiting methods as they will look in the end.

@mrvoorhe mrvoorhe requested a review from sbomer as a code owner April 17, 2026 18:28
Copilot AI review requested due to automatic review settings April 17, 2026 18:28
@github-actions github-actions Bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Apr 17, 2026
@dotnet-policy-service dotnet-policy-service Bot added linkable-framework Issues associated with delivering a linker friendly framework community-contribution Indicates that the PR has been added by a community member labels Apr 17, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a linker pipeline ordering issue where SweepStep could retain an assembly reference originating only from method bodies that would later be stubbed by CodeRewriterStep, leading to inconsistent output (reference retained while the referenced assembly is deleted).

Changes:

  • Reorders the standard linker pipeline to run CodeRewriterStep before SweepStep.
  • Adds a new regression test covering the “only reference is inside an unreachable body” scenario.
  • Adds a small dependency assembly used by the new test and updates the generated analyzer test list.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/tools/illink/src/linker/Linker/Driver.cs Moves CodeRewriterStep ahead of SweepStep in the standard pipeline to ensure sweeping observes the final rewritten bodies.
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBody/OnlyReferenceInUnreachableBody.cs New test validating that an assembly reference originating only in a stubbed body is removed (and the referenced assembly can be removed).
src/tools/illink/test/Mono.Linker.Tests.Cases/UnreachableBody/Dependencies/OtherAssembly2.cs New dependency type compiled into other.dll for the new regression test.
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/generated/.../UnreachableBodyTests.g.cs Adds the new test entry to the generated analyzer test suite.

Comment thread src/tools/illink/src/linker/Linker/Driver.cs
When an unreachable body contains a reference to another assembly, and there are no other references to that assembly, the linker would retain the reference but delete the assembly.

This was happening because the SweepStep was running before CodeRewriterStep.  Which meant that when SweepStep.SweepAssemblyReferences iterated all of the types references in the assembly it would visit
type references to method bodies that were going to be stubbed.  By moving the CodeRewriterStep ahead of the sweep step we ensure that the SweepStep is visiting methods as they will look in the end.
@mrvoorhe mrvoorhe force-pushed the linker-fix-ub-issue-with-reference branch from fc2a2d7 to a9db693 Compare April 21, 2026 14:39
@mrvoorhe
Copy link
Copy Markdown
Contributor Author

@sbomer the PR is ready to be merged. The failures look unrelated.

@sbomer sbomer enabled auto-merge (squash) April 21, 2026 23:25
@sbomer
Copy link
Copy Markdown
Member

sbomer commented Apr 22, 2026

/ba-g "CoreCLR_WasmBuildTests timeout"

@sbomer sbomer merged commit e3e5dc3 into dotnet:main Apr 22, 2026
90 of 92 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants