From b1c4dfa7ddb4b9cab8dc8be8c2b74c1d8f0c9adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 16 Jan 2024 19:16:04 +0900 Subject: [PATCH] Make startup hooks kind of work with native AOT (#96894) Fixes #96052. Since we compile startup hooks anyway, just add a call to it. It will be deadcoded by default. Can be enabled and then it will at least work for hooks that were part of the app. It will throw PNSE for random file paths. --- .../src/ILLink/ILLink.Substitutions.xml | 3 --- .../src/System.Private.CoreLib.csproj | 1 + .../System/StartupHookProvider.NativeAot.cs | 19 ++++++++++++++++ .../tools/aot/ILCompiler/repro/repro.csproj | 1 + .../ILLink/ILLink.Substitutions.Shared.xml | 3 +++ .../nativeaot/StartupHook/StartupHook.cs | 22 +++++++++++++++++++ .../nativeaot/StartupHook/StartupHook.csproj | 16 ++++++++++++++ 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/coreclr/nativeaot/System.Private.CoreLib/src/System/StartupHookProvider.NativeAot.cs create mode 100644 src/tests/nativeaot/StartupHook/StartupHook.cs create mode 100644 src/tests/nativeaot/StartupHook/StartupHook.csproj diff --git a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml index 7453ebc25a8f3..fb07d67f37c68 100644 --- a/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml +++ b/src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml @@ -8,8 +8,5 @@ - - - diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj index 2523a6d807201..1c24f9cc9c7be 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj @@ -221,6 +221,7 @@ + diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/StartupHookProvider.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/StartupHookProvider.NativeAot.cs new file mode 100644 index 0000000000000..07446c26310d7 --- /dev/null +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/StartupHookProvider.NativeAot.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.CompilerServices; + +namespace System +{ + internal static partial class StartupHookProvider + { +#pragma warning disable CA2255 + [ModuleInitializer] +#pragma warning restore CA2255 + internal static void Initialize() + { + if (IsSupported) + ProcessStartupHooks(Environment.GetEnvironmentVariable("DOTNET_STARTUP_HOOKS")); + } + } +} diff --git a/src/coreclr/tools/aot/ILCompiler/repro/repro.csproj b/src/coreclr/tools/aot/ILCompiler/repro/repro.csproj index 6dd2d0b010911..09e2a5bec4c8d 100644 --- a/src/coreclr/tools/aot/ILCompiler/repro/repro.csproj +++ b/src/coreclr/tools/aot/ILCompiler/repro/repro.csproj @@ -41,6 +41,7 @@ + + + + diff --git a/src/tests/nativeaot/StartupHook/StartupHook.cs b/src/tests/nativeaot/StartupHook/StartupHook.cs new file mode 100644 index 0000000000000..ed85bdb060421 --- /dev/null +++ b/src/tests/nativeaot/StartupHook/StartupHook.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Diagnostics.CodeAnalysis; + +class Program +{ + internal static int s_return; + + [DynamicDependency(nameof(StartupHook.Initialize), typeof(StartupHook))] + static int Main() => s_return; +} + +class StartupHook +{ + public static void Initialize() + { + Console.WriteLine("Running startup hook"); + Program.s_return = 100; + } +} diff --git a/src/tests/nativeaot/StartupHook/StartupHook.csproj b/src/tests/nativeaot/StartupHook/StartupHook.csproj new file mode 100644 index 0000000000000..f6b9b2ae9af15 --- /dev/null +++ b/src/tests/nativeaot/StartupHook/StartupHook.csproj @@ -0,0 +1,16 @@ + + + Exe + 0 + true + true + $(NoWarn);IL2026 + + + + + + + + +