Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary Trim analysis warnings for internal static fields of a RequiresUnreferencedCode class #81864

Closed
Tracked by #93172
eerhardt opened this issue Feb 9, 2023 · 8 comments · Fixed by #84620
Closed
Tracked by #93172
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers

Comments

@eerhardt
Copy link
Member

eerhardt commented Feb 9, 2023

Description

In dotnet/aspnetcore#46518 I am trying to mark some ASP.NET classes as RequiresUnreferencedCode. However, in doing that, it is causing the ASP.NET LinkabilityChecker to fail with some unexpected warnings:

ILLink : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.ParameterBindingMethodCache: Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.TempSourceStringExpr' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types. [C:\git\aspnetcore2\src\Tools\LinkabilityChecker\LinkabilityChecker.csproj]
ILLink : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.ParameterBindingMethodCache: Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HttpContextExpr' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types. [C:\git\aspnetcore2\src\Tools\LinkabilityChecker\LinkabilityChecker.csproj]

image

These fields are only used by other RequiresUnreferencedCode code, so I don't expect the trimmer to warn about them.

Reproduction Steps

Using https://github.com/vitek-karas/illinkrepro, I was able to get a repro of this issue. Run illink with the rsp file in the repro.zip.

repro.zip

Expected behavior

There should be no ILLink warnings when running the linker.

Actual behavior

The linker reports 2 warnings for these internal static fields on a class annotated as RequiresUnreferencedCode.

Regression?

??

Known Workarounds

A workaround is to move the static fields to a separate type that isn't annotated with RequiresUnreferencedCode.

Configuration

No response

Other information

cc @sbomer @vitek-karas

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 9, 2023
@ghost
Copy link

ghost commented Feb 9, 2023

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

Issue Details

Description

In dotnet/aspnetcore#46518 I am trying to mark some ASP.NET classes as RequiresUnreferencedCode. However, in doing that, it is causing the ASP.NET LinkabilityChecker to fail with some unexpected warnings:

ILLink : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.ParameterBindingMethodCache: Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.TempSourceStringExpr' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types. [C:\git\aspnetcore2\src\Tools\LinkabilityChecker\LinkabilityChecker.csproj]
ILLink : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.ParameterBindingMethodCache: Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HttpContextExpr' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types. [C:\git\aspnetcore2\src\Tools\LinkabilityChecker\LinkabilityChecker.csproj]

image

Reproduction Steps

Using https://github.com/vitek-karas/illinkrepro, I was able to get a repro of this issue. Run illink with the rsp file in the repro.zip.

repro.zip

Expected behavior

There should be no ILLink warnings when running the linker.

Actual behavior

The linker reports 2 warnings for these internal static fields on a class annotated as RequiresUnreferencedCode.

Regression?

??

Known Workarounds

A workaround is to move the static fields to a separate type that isn't annotated with RequiresUnreferencedCode.

Configuration

No response

Other information

cc @sbomer @vitek-karas

Author: eerhardt
Assignees: -
Labels:

area-AssemblyLoader-coreclr

Milestone: -

@eerhardt eerhardt added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed area-AssemblyLoader-coreclr labels Feb 9, 2023
@ghost
Copy link

ghost commented Feb 9, 2023

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

Issue Details

Description

In dotnet/aspnetcore#46518 I am trying to mark some ASP.NET classes as RequiresUnreferencedCode. However, in doing that, it is causing the ASP.NET LinkabilityChecker to fail with some unexpected warnings:

ILLink : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.ParameterBindingMethodCache: Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.TempSourceStringExpr' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types. [C:\git\aspnetcore2\src\Tools\LinkabilityChecker\LinkabilityChecker.csproj]
ILLink : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.ParameterBindingMethodCache: Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HttpContextExpr' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types. [C:\git\aspnetcore2\src\Tools\LinkabilityChecker\LinkabilityChecker.csproj]

image

Reproduction Steps

Using https://github.com/vitek-karas/illinkrepro, I was able to get a repro of this issue. Run illink with the rsp file in the repro.zip.

repro.zip

Expected behavior

There should be no ILLink warnings when running the linker.

Actual behavior

The linker reports 2 warnings for these internal static fields on a class annotated as RequiresUnreferencedCode.

Regression?

??

Known Workarounds

A workaround is to move the static fields to a separate type that isn't annotated with RequiresUnreferencedCode.

Configuration

No response

Other information

cc @sbomer @vitek-karas

Author: eerhardt
Assignees: -
Labels:

untriaged, area-Tools-ILLink

Milestone: -

eerhardt added a commit to eerhardt/aspnetcore that referenced this issue Feb 9, 2023
@sbomer
Copy link
Member

sbomer commented Feb 9, 2023

The command-line arguments include -a Microsoft.AspNetCore.Http.Extensions visible. The visible root mode also includes internals if the assembly has InternalsVisibleTo, which it does:

var preserve_visible = TypePreserveMembers.Visible;
if (MarkInternalsVisibleTo (assembly))
preserve_visible |= TypePreserveMembers.Internal;
MarkAndPreserve (assembly, preserve_visible);
break;

In my mind this is somewhat expected, since the input is asking to preserve things for unknown reasons, and RequiresUnreferencedCode on the type means that the internal fields could be unsafe to access via reflection. Passing this assembly as a root assembly would produce these warnings as well.

I think we made some exceptions for similar circumstances where members were referenced via XML, but I don't remember the details. Maybe @vitek-karas does.

@eerhardt
Copy link
Member Author

eerhardt commented Feb 9, 2023

In my mind this is somewhat expected, since the input is asking to preserve things for unknown reasons, and RequiresUnreferencedCode on the type means that the internal fields could be unsafe to access via reflection.

I think warning at the places that use these fields is reasonable, since the class is marked RUC. However, my understanding is the warnings are happening simply because they are fields on a RUC type being exposed. That doesn't really make sense to me. Of course these fields are unsafe - that's why they are in the class that is marked RUC. It doesn't mean the trimmer needs to warn simply because they are visible. There should only be warnings at the places using the fields.

@vitek-karas
Copy link
Member

I "think" (didn't check the code) that we intentionally suppress RUC when caused by a direct reference from a descriptor XML. But otherwise we report it always.

As for this case, the -a assembly visible acts as if there's something in the app which directly references all visible APIs on the assembly. That's why it produces a warning, because the linker thinks that there's a real reference. This is very similar in behavior to what would happen if you added for example a DynamicDependency pointing to the fields in question. All of these avenues are basically telling linker "you don't understand this app... so trust me, you're going to need this API as well", and linker's response is "that's fine, but this API won't work when running in a trimmed app... that's probably bad".

The descriptor case should be similar, but I honestly don't remember why we chose not to warn in that case. My main guess would be: there are cases in corelib and similar where we ran into the warning and there's no good way to suppress it (since it doesn't originates in code).

For this specific use case in ASP.NET we have basically two options:

  • Invest into linker fully supporting the "library" mode (and actually defined what it means, since currently it's more like a "This seems to work, so keep it". Currently it's not a supported feature in any sense - we maintain it for our repos only. This might include changing the behavior in this case to not warn - not sure, depends on what would be the desired behavior of the feature.
  • Leave it as-is and ASP.NET will baseline these warnings. We already have a couple of places in runtime where we intentionally suppress warnings via XMLs for similar reasons.

@eerhardt
Copy link
Member Author

eerhardt commented Feb 9, 2023

The part that I still don't get is why static fields are treated specially here. We aren't warning for all the "visible" methods in the same class with RequiresUnreferencedCode on it. But they are just as "visible" as the static fields are.

For this specific use case in ASP.NET

I agree that this isn't critical to fix right now, especially considering this "library" mode isn't a fully supported feature. I was able to workaround it by moving the fields into a separate static class. dotnet/aspnetcore@a68e4c8

eerhardt added a commit to dotnet/aspnetcore that referenced this issue Feb 9, 2023
* Remove incorrect/unnecessary trimming suppressions.

These suppressions weren't not correct. Instead they should be RequiresUnreferencedCode.

* Work around dotnet/runtime#81864
@vitek-karas
Copy link
Member

We aren't warning for all the "visible" methods in the same class with RequiresUnreferencedCode on it.

That's a great point - and I missed this. We need to look into this some more. Whatever the behavior is (I could explain away both ;-)), it should be consistent.

@eerhardt
Copy link
Member Author

eerhardt commented Apr 7, 2023

FYI - I hit this again a couple times in #84468. It might make sense to fix this at the same time as #84433.

cc @agocke

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 11, 2023
vitek-karas added a commit that referenced this issue Apr 12, 2023
…84620)

Marking an assembly as "library" will mark all public APIs. If some of the marked members have RUC on them we decided we don't want to produce warnings, since there's nothing especially wrong with the library. The callers of those members would still get the right warnings.

Currently linker implements this correctly for methods and properties, but it fails for fields and events and it still produces some warnings, especially if the RUC is on the parent type.

This change fixes those cases and adds a special test for the library marked assembly in combination with several RUCs.

Fixes #81864
@ghost ghost removed in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner labels Apr 12, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants