Apply jkotas's patch to fix race condition in AllocateManagedClassObject (#81947)
authorEgor Bogatov <egorbo@gmail.com>
Fri, 10 Feb 2023 16:58:51 +0000 (17:58 +0100)
committerGitHub <noreply@github.com>
Fri, 10 Feb 2023 16:58:51 +0000 (08:58 -0800)
src/coreclr/vm/typehandle.cpp

index e3834ad..70c0397 100644 (file)
@@ -365,7 +365,7 @@ void TypeHandle::AllocateManagedClassObject(RUNTIMETYPEHANDLE* pDest)
         // Take a lock here since we don't want to allocate redundant objects which won't be collected
         CrstHolder exposedClassLock(AppDomain::GetMethodTableExposedClassObjectLock());
 
-        if (*pDest == NULL)
+        if (VolatileLoad(pDest) == NULL)
         {
             FrozenObjectHeapManager* foh = SystemDomain::GetFrozenObjectHeapManager();
             Object* obj = foh->TryAllocateObject(g_pRuntimeTypeClass, g_pRuntimeTypeClass->GetBaseSize());
@@ -377,7 +377,7 @@ void TypeHandle::AllocateManagedClassObject(RUNTIMETYPEHANDLE* pDest)
             RUNTIMETYPEHANDLE handle = (RUNTIMETYPEHANDLE)obj;
             // Set the bit to 1 (we'll have to reset it before use)
             handle |= 1;
-            *pDest = handle;
+            VolatileStore(pDest, handle);
         }
     }
     else