Repository navigation
[release/10.0] Preserve module initialization across ReadyToRun inlining #135376
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
Open
AaronRobinsonMSFT
wants to merge
2
commits into
dotnet:release/10.0
Choose a base branch
from
AaronRobinsonMSFT:arobins/backport-134359-net10
base: release/10.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace ModuleInitializerTest; | ||
|
|
||
| public sealed class FieldHolder | ||
| { | ||
| private static bool s_isSet; | ||
|
|
||
| public static bool IsSet => s_isSet; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="FieldLibrary.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
24 changes: 24 additions & 0 deletions
24
src/tests/readytorun/ModuleInitializer/InitializerLibrary.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // 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 ModuleInitializerTest; | ||
|
|
||
| public static class InitializerLibrary | ||
| { | ||
| [ModuleInitializer] | ||
| public static void Initialize() | ||
| { | ||
| GetIsSet(null) = true; | ||
| } | ||
|
|
||
| [UnsafeAccessor(UnsafeAccessorKind.StaticField, Name = "s_isSet")] | ||
| private static extern ref bool GetIsSet(FieldHolder holder); | ||
|
|
||
| // Inlining this empty call must not remove the module's initialization dependency. | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static void Touch() | ||
| { | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/tests/readytorun/ModuleInitializer/InitializerLibrary.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="InitializerLibrary.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="FieldLibrary.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
|
||
| .assembly extern mscorlib {} | ||
| .assembly RvaFieldLibrary {} | ||
| .module RvaFieldLibrary.dll | ||
|
|
||
| .method private specialname rtspecialname static void .cctor() cil managed | ||
| { | ||
| .maxstack 2 | ||
| ldstr "RvaFieldLibrary.Initialized" | ||
| ldc.i4.1 | ||
| call void [mscorlib]System.AppContext::SetSwitch(string, bool) | ||
| ret | ||
| } | ||
|
|
||
| .class public auto ansi beforefieldinit RvaFieldHolder extends [mscorlib]System.Object | ||
| { | ||
| .field public static int32 Value at ValueData | ||
| } | ||
|
|
||
| .data ValueData = int32(42) |
8 changes: 8 additions & 0 deletions
8
src/tests/readytorun/ModuleInitializer/RvaFieldLibrary.ilproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.IL"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="RvaFieldLibrary.il" /> | ||
| </ItemGroup> | ||
| </Project> |
36 changes: 36 additions & 0 deletions
36
src/tests/readytorun/ModuleInitializer/UnsafeAccessorModuleInitializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // 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.Runtime.CompilerServices; | ||
| using Xunit; | ||
|
|
||
| namespace ModuleInitializerTest; | ||
|
|
||
| public static class UnsafeAccessorModuleInitializer | ||
| { | ||
| [Fact] | ||
| public static void TestEntryPoint() | ||
| { | ||
| InitializerLibrary.Touch(); | ||
| Assert.True(FieldHolder.IsSet, "The module initializer did not update the private static field."); | ||
| } | ||
|
|
||
| [Fact] | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static void RvaFieldAccess() | ||
| { | ||
| Assert.Equal(42, RvaFieldHolder.Value); | ||
| Assert.True(AppContext.TryGetSwitch("RvaFieldLibrary.Initialized", out bool initialized) && initialized, | ||
| "Accessing the RVA field did not run its module initializer."); | ||
| } | ||
|
|
||
| [Fact] | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| public static void ValueTypeInstanceMethodAccess() | ||
| { | ||
| Assert.Equal(42, default(ValueTypeLibrary).GetValue()); | ||
| Assert.True(AppContext.TryGetSwitch("ValueTypeLibrary.Initialized", out bool initialized) && initialized, | ||
| "Calling the value-type instance method did not run its module initializer."); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/tests/readytorun/ModuleInitializer/UnsafeAccessorModuleInitializer.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <!-- Preserve separate modules and run the test through composite ReadyToRun. --> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <AlwaysUseCrossGen2>true</AlwaysUseCrossGen2> | ||
| <CLRTestPriority>1</CLRTestPriority> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="UnsafeAccessorModuleInitializer.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="FieldLibrary.csproj" /> | ||
| <ProjectReference Include="InitializerLibrary.csproj" /> | ||
| <ProjectReference Include="RvaFieldLibrary.ilproj" /> | ||
| <ProjectReference Include="ValueTypeLibrary.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
19 changes: 19 additions & 0 deletions
19
src/tests/readytorun/ModuleInitializer/ValueTypeLibrary.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace ModuleInitializerTest; | ||
|
|
||
| public struct ValueTypeLibrary | ||
| { | ||
| [ModuleInitializer] | ||
| public static void Initialize() | ||
| { | ||
| AppContext.SetSwitch("ValueTypeLibrary.Initialized", true); | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public int GetValue() => 42; | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/tests/readytorun/ModuleInitializer/ValueTypeLibrary.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="ValueTypeLibrary.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.