Skip to content

Commit e42a873

Browse files
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.
1 parent 613a13f commit e42a873

File tree

7 files changed

+62
-3
lines changed

7 files changed

+62
-3
lines changed

src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
99
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
1010
</type>
11-
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
12-
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
13-
</type>
1411
</assembly>
1512
</linker>

src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
<Compile Include="System\String.Intern.cs" />
222222
<Compile Include="System\Array.NativeAot.cs" />
223223
<Compile Include="System\Delegate.cs" />
224+
<Compile Include="System\StartupHookProvider.NativeAot.cs" />
224225
<Compile Include="System\RuntimeTypeHandle.cs" />
225226
<Compile Include="System\Exception.NativeAot.cs" />
226227
<Compile Include="System\RuntimeExceptionHelpers.cs" />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
namespace System
7+
{
8+
internal static partial class StartupHookProvider
9+
{
10+
#pragma warning disable CA2255
11+
[ModuleInitializer]
12+
#pragma warning restore CA2255
13+
internal static void Initialize()
14+
{
15+
if (IsSupported)
16+
ProcessStartupHooks(Environment.GetEnvironmentVariable("DOTNET_STARTUP_HOOKS"));
17+
}
18+
}
19+
}

src/coreclr/tools/aot/ILCompiler/repro/repro.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ReproResponseLines Include="--feature:System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported=false" />
4242
<ReproResponseLines Include="--feature:System.Globalization.Invariant=true" />
4343
<ReproResponseLines Include="--feature:System.Diagnostics.Debugger.IsSupported=false" />
44+
<ReproResponseLines Include="--feature:System.StartupHookProvider.IsSupported=false" />
4445
</ItemGroup>
4546

4647
<WriteLinesToFile File="$(OutputPath)\compile-with-$(LibrariesConfiguration)-libs.rsp"

src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.Shared.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
<type fullname="Internal.Runtime.InteropServices.ComponentActivator" feature="System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting" featurevalue="false">
3838
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
3939
</type>
40+
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
41+
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
42+
</type>
4043
</assembly>
4144
</linker>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
class Program
8+
{
9+
internal static int s_return;
10+
11+
[DynamicDependency(nameof(StartupHook.Initialize), typeof(StartupHook))]
12+
static int Main() => s_return;
13+
}
14+
15+
class StartupHook
16+
{
17+
public static void Initialize()
18+
{
19+
Console.WriteLine("Running startup hook");
20+
Program.s_return = 100;
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<CLRTestPriority>0</CLRTestPriority>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<StartupHookSupport>true</StartupHookSupport>
7+
<NoWarn>$(NoWarn);IL2026</NoWarn>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<Compile Include="StartupHook.cs" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<CLRTestEnvironmentVariable Include="DOTNET_STARTUP_HOOKS" Value="StartupHook" />
15+
</ItemGroup>
16+
</Project>

0 commit comments

Comments
 (0)