Fix assertion failure / crash in multi-core JIT (#53573)
authorKoundinya Veluri <kouvel@users.noreply.github.com>
Wed, 2 Jun 2021 14:28:59 +0000 (07:28 -0700)
committerGitHub <noreply@github.com>
Wed, 2 Jun 2021 14:28:59 +0000 (07:28 -0700)
* Fix assertion failure / crash in multi-core JIT

- When the recorder times out it doesn't actually stop profiling, but writes out the profile
- The app may later stop profiling, and then it tries to write the profile again
- PR https://github.com/dotnet/runtime/pull/48326 fairly expected that the profile is only written once (some state is mutated)
- The non-timeout stop-profile path was also not stopping the timer
- Fix for https://github.com/dotnet/runtime/issues/53014 in main

src/coreclr/vm/multicorejit.cpp
src/coreclr/vm/multicorejitimpl.h
src/tests/baseservices/TieredCompilation/McjRecorderTimeoutBeforeStop.cs [new file with mode: 0644]
src/tests/baseservices/TieredCompilation/McjRecorderTimeoutBeforeStop.csproj [new file with mode: 0644]

index 9a9ec83..1feb731 100644 (file)
@@ -1496,16 +1496,7 @@ void MulticoreJitManager::WriteMulticoreJitProfiler()
     CONTRACTL_END;
 
     CrstHolder hold(& m_playerLock);
-
-    // Avoid saving after MulticoreJitRecorder is deleted, and saving twice
-    if (!MulticoreJitRecorder::CloseTimer())
-    {
-        return;
-    }
-    if (m_pMulticoreJitRecorder != NULL)
-    {
-        m_pMulticoreJitRecorder->StopProfile(false);
-    }
+    StopProfile(false);
 }
 #endif // !TARGET_UNIX
 
index 35ea8a3..cd00e6f 100644 (file)
@@ -678,20 +678,15 @@ public:
     }
 
 #ifndef TARGET_UNIX
-    static bool CloseTimer()
+    static void CloseTimer()
     {
         LIMITED_METHOD_CONTRACT;
 
         TP_TIMER * pTimer = InterlockedExchangeT(& s_delayedWriteTimer, NULL);
-
-        if (pTimer == NULL)
+        if (pTimer != NULL)
         {
-            return false;
+            CloseThreadpoolTimer(pTimer);
         }
-
-        CloseThreadpoolTimer(pTimer);
-
-        return true;
     }
 
     ~MulticoreJitRecorder()
diff --git a/src/tests/baseservices/TieredCompilation/McjRecorderTimeoutBeforeStop.cs b/src/tests/baseservices/TieredCompilation/McjRecorderTimeoutBeforeStop.cs
new file mode 100644 (file)
index 0000000..14ecd47
--- /dev/null
@@ -0,0 +1,33 @@
+// 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;
+using System.Runtime.CompilerServices;
+using System.Threading;
+
+public static class BasicTest
+{
+    private static int Main()
+    {
+        const int Pass = 100;
+
+        ProfileOptimization.SetProfileRoot(Environment.CurrentDirectory);
+        ProfileOptimization.StartProfile("profile.mcj");
+
+        // Record a method
+        Foo();
+
+        // Let the multi-core JIT recorder time out. The timeout is set to 1 s in the test project.
+        Thread.Sleep(2000);
+
+        // Stop the profile again after timeout (just verifying that it works)
+        ProfileOptimization.StartProfile(null);
+        return Pass;
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    private static void Foo()
+    {
+    }
+}
diff --git a/src/tests/baseservices/TieredCompilation/McjRecorderTimeoutBeforeStop.csproj b/src/tests/baseservices/TieredCompilation/McjRecorderTimeoutBeforeStop.csproj
new file mode 100644 (file)
index 0000000..05feffa
--- /dev/null
@@ -0,0 +1,21 @@
+<Project Sdk="Microsoft.NET.Sdk">
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <Optimize>true</Optimize>
+    <CLRTestPriority>0</CLRTestPriority>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="McjRecorderTimeoutBeforeStop.cs" />
+  </ItemGroup>
+  <PropertyGroup>
+    <CLRTestBatchPreCommands><![CDATA[
+$(CLRTestBatchPreCommands)
+set COMPlus_MultiCoreJitProfileWriteDelay=1
+]]></CLRTestBatchPreCommands>
+    <BashCLRTestPreCommands><![CDATA[
+$(BashCLRTestPreCommands)
+export COMPlus_MultiCoreJitProfileWriteDelay=1
+]]></BashCLRTestPreCommands>
+  </PropertyGroup>
+</Project>