-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable TLS on linux/arm64 only for static resolver (#106052)
Fix arm64 behavior around assuming that a static resolver is possible for Arm64 TLS at all times. Instead, detect whether the static resolver path was taken, and if it was, use the static path. In addition, this change adds a new config variable DOTNET_DisableOptimizedThreadStaticAccess which can be used to mitigate this issue in case issues are found in other optimized thread static access paths. Finally, the test for this relies on adding a new switch to the corerun utility, which can pre-load a set of .so files into the process. --------- Co-authored-by: Kunal Pathak <[email protected]> Co-authored-by: Ubuntu <azureuser@davidwr-arm64-l2.sql0fxglchme5jhu4spsf5nbmb.xx.internal.cloudapp.net> Co-authored-by: Jan Kotas <[email protected]>
- Loading branch information
1 parent
194fec5
commit ade568b
Showing
11 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 @@ | ||
# Licensed to the .NET Foundation under one or more agreements. | ||
# The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
include_directories(${INC_PLATFORM_DIR}) | ||
|
||
add_library(usetls SHARED testtls.cpp) | ||
|
||
install (TARGETS usetls DESTINATION bin) |
This file contains 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,130 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
|
||
// This test is verifying that the runtime properly handles the cases where the TLS infra in the runtime is forced | ||
// to use a dynamic resolver. This is done by means of a private config variable to validate the behavior on Linux Arm64 | ||
// and a set of multithreaded tasks, that has been known to cause the runtime to crash when this is handled incorrectly. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Loader; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace TestTLSWithLoadedDlls | ||
{ | ||
static class TLSWithLoadedDlls | ||
{ | ||
private const int CountOfLibTlsToLoad = 40; | ||
|
||
static async Task DoLotsOfAsyncWork(int loopCount) | ||
{ | ||
for (int i = 0; i < loopCount; i++) | ||
{ | ||
Console.WriteLine("Starting a new batch of tasks..."); | ||
var tasks = Enumerable.Range(1, 100).Select(i => Task.Run(async () => | ||
{ | ||
await Task.Delay(1); | ||
})).ToArray(); | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
Console.WriteLine("Batch of tasks completed. Main loop sleeping for 20 ms..."); | ||
await Task.Delay(20); | ||
} | ||
} | ||
|
||
static int Main(string[] args) | ||
{ | ||
if ((args.Length == 1) && (args[0] == "RunLotsOfTasks")) | ||
{ | ||
DoLotsOfAsyncWork(100).GetAwaiter().GetResult(); | ||
return 100; | ||
} | ||
|
||
int CountOfLibTlsToLoad = 60; | ||
|
||
if (OperatingSystem.IsWindows()) // Windows does not have a really long command line length limit, and doesn't have a problem with many TLS using images used | ||
CountOfLibTlsToLoad = 10; | ||
|
||
StringBuilder arguments = new(); | ||
|
||
(string prefix, string suffix) = GetSharedLibraryPrefixSuffix(); | ||
|
||
string UseTlsFileName = GetSharedLibraryFileNameForCurrentPlatform("usetls"); | ||
string testDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); | ||
string UseTlsFilePath = Path.Combine(testDirectory, UseTlsFileName); | ||
|
||
for (int i = 0; i < CountOfLibTlsToLoad; i++) | ||
{ | ||
string tlsNumberSpecificPath = Path.Combine(testDirectory, i.ToString()); | ||
string finalUseTlsPath = Path.Combine(tlsNumberSpecificPath, prefix + "usetls" + suffix); | ||
|
||
Directory.CreateDirectory(tlsNumberSpecificPath); | ||
if (!File.Exists(finalUseTlsPath)) | ||
{ | ||
File.Copy( | ||
UseTlsFilePath, | ||
finalUseTlsPath); | ||
} | ||
|
||
arguments.Append(" -l "); | ||
arguments.Append(finalUseTlsPath); | ||
} | ||
|
||
arguments.Append(' '); | ||
arguments.Append(System.Reflection.Assembly.GetExecutingAssembly().Location); | ||
arguments.Append(" RunLotsOfTasks"); | ||
|
||
Process process = new Process(); | ||
process.StartInfo.FileName = GetCorerunPath(); | ||
process.StartInfo.Arguments = arguments.ToString(); | ||
process.StartInfo.UseShellExecute = false; | ||
process.StartInfo.EnvironmentVariables["DOTNET_AssertNotStaticTlsResolver"] = "1"; | ||
|
||
Console.WriteLine($"Launching {process.StartInfo.FileName} {process.StartInfo.Arguments}"); | ||
|
||
process.Start(); | ||
process.WaitForExit(); | ||
return process.ExitCode; | ||
} | ||
|
||
private static string GetCorerunPath() | ||
{ | ||
string corerunName; | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
corerunName = "CoreRun.exe"; | ||
} | ||
else | ||
{ | ||
corerunName = "corerun"; | ||
} | ||
|
||
return Path.Combine(Environment.GetEnvironmentVariable("CORE_ROOT"), corerunName); | ||
} | ||
|
||
public static (string, string) GetSharedLibraryPrefixSuffix() | ||
{ | ||
if (OperatingSystem.IsWindows()) | ||
return (string.Empty, ".dll"); | ||
|
||
if (OperatingSystem.IsMacOS()) | ||
return ("lib", ".dylib"); | ||
|
||
return ("lib", ".so"); | ||
} | ||
|
||
public static string GetSharedLibraryFileNameForCurrentPlatform(string libraryName) | ||
{ | ||
(string prefix, string suffix) = GetSharedLibraryPrefixSuffix(); | ||
return prefix + libraryName + suffix; | ||
} | ||
} | ||
} |
This file contains 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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<CLRTestPriority>0</CLRTestPriority> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator> | ||
<NativeAotIncompatible>true</NativeAotIncompatible> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<DebugType>PdbOnly</DebugType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
<CMakeProjectReference Include="CMakeLists.txt" /> | ||
</ItemGroup> | ||
</Project> |
This file contains 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,46 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#ifdef _MSC_VER | ||
#define DLLEXPORT __declspec(dllexport) | ||
#else | ||
#define DLLEXPORT __attribute__((visibility("default"))) | ||
#endif // _MSC_VER | ||
|
||
thread_local int tls0; | ||
thread_local int tls1; | ||
thread_local int tls2; | ||
thread_local int tls3; | ||
thread_local int tls4; | ||
thread_local int tls5; | ||
thread_local int tls6; | ||
thread_local int tls7; | ||
thread_local int tls8; | ||
thread_local int tls9; | ||
thread_local int tls10; | ||
thread_local int tls11; | ||
thread_local int tls12; | ||
thread_local int tls13; | ||
thread_local int tls14; | ||
thread_local int tls15; | ||
thread_local int tls16; | ||
|
||
extern "C" DLLEXPORT void initializeTLS() { | ||
tls0=0; | ||
tls1=0; | ||
tls2=0; | ||
tls3=0; | ||
tls4=0; | ||
tls5=0; | ||
tls6=0; | ||
tls7=0; | ||
tls8=0; | ||
tls9=0; | ||
tls10=0; | ||
tls11=0; | ||
tls12=0; | ||
tls13=0; | ||
tls14=0; | ||
tls15=0; | ||
tls16=0; | ||
} |