[release/8.0] [NativeAOT] Missing memory fence before bulk move of objects (#90941)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Wed, 23 Aug 2023 01:21:57 +0000 (18:21 -0700)
committerGitHub <noreply@github.com>
Wed, 23 Aug 2023 01:21:57 +0000 (18:21 -0700)
* Memory fence before bulk move of objects

* deleted GCMemoryHelpers.h

* Introduced a GCHeapMemoryBarrier helper.

---------

Co-authored-by: vsadov <8218165+VSadov@users.noreply.github.com>
src/coreclr/nativeaot/Runtime/GCMemoryHelpers.cpp
src/coreclr/nativeaot/Runtime/GCMemoryHelpers.h [deleted file]
src/coreclr/nativeaot/Runtime/MiscHelpers.cpp
src/coreclr/nativeaot/Runtime/gcrhenv.cpp
src/coreclr/nativeaot/Runtime/portable.cpp
src/coreclr/nativeaot/Runtime/threadstore.cpp

index 27126ac..30f2c5c 100644 (file)
@@ -10,7 +10,6 @@
 #include "PalRedhawkCommon.h"
 #include "CommonMacros.inl"
 
-#include "GCMemoryHelpers.h"
 #include "GCMemoryHelpers.inl"
 
 // This function clears a piece of memory in a GC safe way.
@@ -31,11 +30,26 @@ COOP_PINVOKE_CDECL_HELPER(void *, RhpGcSafeZeroMemory, (void * mem, size_t size)
     return mem;
 }
 
+#if defined(TARGET_X86) || defined(TARGET_AMD64) 
+    // 
+    // Memory writes are already ordered
+    // 
+    #define GCHeapMemoryBarrier() 
+#else
+    #define GCHeapMemoryBarrier() MemoryBarrier() 
+#endif 
+
 // Move memory, in a way that is compatible with a move onto the heap, but
 // does not require the destination pointer to be on the heap.
 
 COOP_PINVOKE_HELPER(void, RhBulkMoveWithWriteBarrier, (uint8_t* pDest, uint8_t* pSrc, size_t cbDest))
 {
+    // It is possible that the bulk write is publishing object references accessible so far only
+    // by the current thread to shared memory.
+    // The memory model requires that writes performed by current thread are observable no later
+    // than the writes that will actually publish the references.
+    GCHeapMemoryBarrier();
+
     if (pDest <= pSrc || pSrc + cbDest <= pDest)
         InlineForwardGCSafeCopy(pDest, pSrc, cbDest);
     else
@@ -43,8 +57,3 @@ COOP_PINVOKE_HELPER(void, RhBulkMoveWithWriteBarrier, (uint8_t* pDest, uint8_t*
 
     InlinedBulkWriteBarrier(pDest, cbDest);
 }
-
-void REDHAWK_CALLCONV RhpBulkWriteBarrier(void* pMemStart, uint32_t cbMemSize)
-{
-    InlinedBulkWriteBarrier(pMemStart, cbMemSize);
-}
diff --git a/src/coreclr/nativeaot/Runtime/GCMemoryHelpers.h b/src/coreclr/nativeaot/Runtime/GCMemoryHelpers.h
deleted file mode 100644 (file)
index 127b4d7..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-//
-// Unmanaged GC memory helpers
-//
-
-EXTERN_C void REDHAWK_CALLCONV RhpBulkWriteBarrier(void* pMemStart, uint32_t cbMemSize);
index ec2fabc..6df37cf 100644 (file)
@@ -35,7 +35,6 @@
 #include "MethodTable.inl"
 #include "CommonMacros.inl"
 #include "volatile.h"
-#include "GCMemoryHelpers.h"
 #include "GCMemoryHelpers.inl"
 #include "yieldprocessornormalized.h"
 #include "RhConfig.h"
index 3d09909..3ec4886 100644 (file)
@@ -42,7 +42,6 @@
 
 #include "daccess.h"
 
-#include "GCMemoryHelpers.h"
 #include "interoplibinterface.h"
 
 #include "holder.h"
index d45b3d0..8b425bf 100644 (file)
@@ -31,7 +31,6 @@
 #include "MethodTable.inl"
 #include "ObjectLayout.h"
 
-#include "GCMemoryHelpers.h"
 #include "GCMemoryHelpers.inl"
 
 #if defined(USE_PORTABLE_HELPERS)
index 67a6949..2e8369f 100644 (file)
@@ -24,7 +24,6 @@
 #include "yieldprocessornormalized.h"
 
 #include "slist.inl"
-#include "GCMemoryHelpers.h"
 
 EXTERN_C volatile uint32_t RhpTrapThreads;
 volatile uint32_t RhpTrapThreads = (uint32_t)TrapThreadsFlags::None;