dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.42k stars 4.76k forks source link

NativeAOT doesn't produce a warning if `StartupHookSupport` is enabled #96052

Closed vitek-karas closed 10 months ago

vitek-karas commented 11 months ago

StartupHookSupport is not compatible with trimming or AOT. It's a feature switch which is disabled by default if either PublishTrimmed or PublishAot is enabled. If it is explicitly enabled by the user the trimmer will produce a warning:

ILLink : Trim analysis error IL2026: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.Start
upHookProvider.CallStartupHook(StartupHookProvider.StartupHookNameOrPath)' which has 'RequiresUnreferencedCodeAttribute
' can break functionality when trimming application code. The StartupHookSupport feature switch has been enabled for th
is app which is being trimmed. Startup hook code is not observable by the trimmer and so required assemblies, types and
 members may be removed.

This is to maintain the promise that the app either works the same with trimming or we produce a warning.

NativeAOT doesn't produce the warning, but the app behavior will change since no startup hooks will be executed either.

The way the warning is generated in CoreCLR/trimmer is that the CoreLib's ILLink.Descriptor.xml contains the startup hook entry points - specifically the StartupHookProvider APIs, which are called by the native runtime during app startup. As such the trimmer will actually include a tiny bit of StartupHookProvider metadata and code in every application. If startup hooks are enabled, the trimmer will include all of the implementation of that class as well, and one of those methods intentionally produces IL2026 to create the warning.

NativeAOT doesn't have a similar descriptors file in its CoreLib and in fact the StartupHookProvider is completely removed in the final app. I think NativeAOT should either include a reference to StartupHookProvider in its startup path or inject a small piece of code which will produce the warning during the startup path if the feature is enabled.

Repro:

dotnet new console --aot

Add this to the project file

    <StartupHookSupport>true</StartupHookSupport>

And then:

dotnet publish

This should produce a warning, but it doesn't.

Note that there might be other features with similar problems (BuiltInCOM comes to mind?)

ghost commented 11 months ago

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

Issue Details
`StartupHookSupport` is not compatible with trimming or AOT. It's a feature switch which is disabled by default if either `PublishTrimmed` or `PublishAot` is enabled. If it is explicitly enabled by the user the trimmer will produce a warning: ``` ILLink : Trim analysis error IL2026: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.Start upHookProvider.CallStartupHook(StartupHookProvider.StartupHookNameOrPath)' which has 'RequiresUnreferencedCodeAttribute ' can break functionality when trimming application code. The StartupHookSupport feature switch has been enabled for th is app which is being trimmed. Startup hook code is not observable by the trimmer and so required assemblies, types and members may be removed. ``` This is to maintain the promise that the app either works the same with trimming or we produce a warning. NativeAOT doesn't produce the warning, but the app behavior will change since no startup hooks will be executed either. The way the warning is generated in CoreCLR/trimmer is that the CoreLib's `ILLink.Descriptor.xml` contains the startup hook entry points - specifically the `StartupHookProvider` APIs, which are called by the native runtime during app startup. As such the trimmer will actually include a tiny bit of `StartupHookProvider` metadata and code in every application. If startup hooks are enabled, the trimmer will include all of the implementation of that class as well, and one of those methods intentionally produces `IL2026` to create the warning. NativeAOT doesn't have a similar descriptors file in its CoreLib and in fact the `StartupHookProvider` is completely removed in the final app. I think NativeAOT should either include a reference to `StartupHookProvider` in its startup path or inject a small piece of code which will produce the warning during the startup path if the feature is enabled. Repro: ``` dotnet new console --aot ``` Add this to the project file ```xml true ``` And then: ``` dotnet publish ``` This should produce a warning, but it doesn't. Note that there might be other features with similar problems (BuiltInCOM comes to mind?)
Author: vitek-karas
Assignees: -
Labels: `area-NativeAOT-coreclr`
Milestone: -