Skip to content

Fix UnreachableBody with only ref to assembly.#127090

Open
mrvoorhe wants to merge 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-ub-issue-with-reference
Open

Fix UnreachableBody with only ref to assembly.#127090
mrvoorhe wants to merge 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.

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.

/// CodeRewriterStep must run before SweepStep to ensure that SweepStep.SweepAssemblyReferences doesn't see TypeReferences in method bodies that
/// are going to be removed by CodeRewriterStep.
/// </summary>
[SetupCompileBefore("other.dll", new[] { typeof(OtherAssembly2) })]
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

This test expects Foo.Method to be stubbed ([ExpectBodyModified]) and for the reference to other.dll to be removable. All other UnreachableBody tests explicitly enable the unreachablebodies optimization via [SetupLinkerArgument("--enable-opt", "unreachablebodies")]; without it, the body may not be rewritten and the test may not reproduce the scenario reliably. Add the --enable-opt unreachablebodies setup argument here as well.

Suggested change
[SetupCompileBefore("other.dll", new[] { typeof(OtherAssembly2) })]
[SetupCompileBefore("other.dll", new[] { typeof(OtherAssembly2) })]
[SetupLinkerArgument("--enable-opt", "unreachablebodies")]

Copilot uses AI. Check for mistakes.
@@ -1620,9 +1620,9 @@ static Pipeline GetStandardPipeline()
p.AppendStep(new ValidateVirtualMethodAnnotationsStep());
p.AppendStep(new ProcessWarningsStep());
p.AppendStep(new OutputWarningSuppressions());
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

CodeRewriterStep was previously executed after SweepStep, and there are existing comments/assumptions in the linker code that refer to that ordering (e.g., Linker.Steps/MarkStep.cs around the comment "CodeRewriterStep runs after sweeping"). With this pipeline reordering, please update any such references to avoid future confusion and keep the documentation consistent with the actual step order.

Suggested change
p.AppendStep(new OutputWarningSuppressions());
p.AppendStep(new OutputWarningSuppressions());
// Code rewriting happens before sweeping in the current pipeline order.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sbomer Do you have thoughts on this? Here is the code in the MarkStep

// CodeRewriterStep runs after sweeping, and may request the stubbed value for any preserved method
based on the comment it sounds intentional. I'm not that familiar with the FeatureGuardAttribute or FeatureSwitchDefinitionAttribute stuff that is mentioned in the comment.

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