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

[AOT] Add RequiresDynamicCode annotation to Microsoft.AspNetCore.Routing #45580

Merged
merged 4 commits into from
Jan 3, 2023

Conversation

Nick-Stanton
Copy link
Member

@Nick-Stanton Nick-Stanton commented Dec 13, 2022

[AOT] Add RequiresDynamicCode annotation to Microsoft.AspNetCore.Routing

Adds RequiresDynamicCode annotations and appropriate warning suppressions to prepare Web UI's projects for AOT analysis.

Description

The addition of the AOT analysis into our projects brought in ~40 warnings for us, half of which were related to reflection. The other half consist of JSON warnings (#45527) and Dependency Injection (dotnet/runtime#79286) and will be handled in the linked issues. This PR addresses the reflection warnings with the exception of BindConverter.cs which requires further conversation (see #45578). Please note that reflection with MakeGenericType and MakeGenericMethod are allowed for reference types and not value types in AOT, so suppressions should only happen in cases where we know only a reference type is being passed.

Part of effort tracked in #45473

@Nick-Stanton Nick-Stanton added the area-blazor Includes: Blazor, Razor Components label Dec 13, 2022
@Nick-Stanton Nick-Stanton requested a review from JamesNK December 13, 2022 21:59
@Nick-Stanton Nick-Stanton requested review from a team and javiercn as code owners December 13, 2022 21:59
@davidfowl davidfowl requested a review from eerhardt December 13, 2022 22:35
[SuppressMessage(
"AOT",
"IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "The referenced methods are AOT safe with reference types.")]
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this suppression is valid. We won't ever call this class with a property of type int, double, or Guid, or any other Value Type?

Instead, if we want to make this AOT-compatible, we will need to do something like this:

https://github.com/dotnet/runtime/blob/66b11d7f2c2b25fc3e6685b02f016ef2f9f8c64f/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs#L1378-L1391

Basically - check if !RuntimeFeature.IsDynamicCodeSupported. When it isn't, and one of the types is a ValueType, then fallback to "slower" reflection based property setting that boxes.

@@ -375,6 +375,9 @@ private static (MethodInfo, Type[]) GetCachedMethodInfo(AssemblyKey assemblyKey,
"ReflectionAnalysis",
"IL2060:MakeGenericMethod",
Justification = "https://github.com/mono/linker/issues/1727")]
[SuppressMessage("AOT",
"IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "Methods being referenced do not have DynamicallyAccessedMembers.")]
Copy link
Member

Choose a reason for hiding this comment

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

  1. This isn't a valid justification for suppressing this warning.
  2. I don't believe this warning can be safely suppressed. From looking at the only callsite, this is used to convert a ValueTask<T> to a Task<T>. If the T is a ValueType, this code isn't guaranteed to work in Native AOT, since CreateValueTaskConverter<T> might not be generated ahead of time for that specific ValueType.

Copy link
Member

Choose a reason for hiding this comment

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

Further, how important is it for Microsoft.JSInterop to be AOT-compatible in .NET 8? Isn't it only used for WASM, which doesn't use Native AOT?

Copy link
Member Author

@Nick-Stanton Nick-Stanton Dec 21, 2022

Choose a reason for hiding this comment

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

It is used across WASM, Server and WebView. You've raised important points about these suppressions, and I will either pursue a fallback like you describe here or document my findings in an issue report for us to milestone and discuss like I did with BindConverter (#45578).

In the meantime, I have reverted my changes in JSInterop since @JamesNK's PR already disables analysis on the project.

Copy link
Member Author

Choose a reason for hiding this comment

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

Documented in #45795

@Nick-Stanton Nick-Stanton added this to the 8.0-preview1 milestone Dec 19, 2022
@Nick-Stanton Nick-Stanton self-assigned this Dec 19, 2022
@Nick-Stanton Nick-Stanton changed the title [AOT] Fix Web UI AOT Warnings [AOT] Add RequiresDynamicCode annotation to Microsoft.AspNetCore.Routing Dec 28, 2022
@Nick-Stanton
Copy link
Member Author

Upon further investigation, more work needs to be done than just suppressing these warnings. The scope of this PR is pivoting to labeling what we already know will be unsafe.

#45578 and #45795 track the remaining work for Web UI to complete as part of the AOT analysis warning effort.

@Nick-Stanton Nick-Stanton merged commit 97e26bd into dotnet:main Jan 3, 2023
@Nick-Stanton Nick-Stanton deleted the webui-aot branch January 3, 2023 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants