diff --git a/Changes.md b/Changes.md index 54466e16..095b6fea 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed a data race in the HXCPP_GC_GENERATIONAL write barrier, which ThreadSanitizer reported when two threads stored into one old array at once: the barrier read an object's mark byte while another thread set the remembered bit in it, and the young log's count was shared the same way. Both are atomic accesses now, as cheap as before where they compile to plain loads * Fixed a Haxe 4 haxe.atomic.AtomicInt kept in a field reading freed memory after a collection: Haxe 4's AtomicInt on C++ is a cpp.Pointer into a one-element array nothing else holds, and the collector ignored cpp.Pointer fields, so it freed the array (HaxeFoundation/hxcpp#1225; Haxe 5 changed AtomicInt instead, in HaxeFoundation/haxe#12236). A cpp.Pointer field, or a pointer boxed in a Dynamic, now keeps what it points into in the GC heap, as a pointer on the stack does, and pins it under HXCPP_GC_MOVING. Of 10,000 atomics in fields, 9,999 read freed memory after a collection and none do now * Fixed HXCPP_GC_INCREMENTAL failing to compile ("Incremental GC requires regenerated C++") with libhxcpp installed from a release, 6.0.0-rc.2 among them: the release workflow rewrote extraParams.hxml with the version and dropped its other line, the macro that turns on the generated write barriers the incremental collector needs. Only the version lines are rewritten now * Fixed a change of link flags or libraries alone leaving the program as it was: the build tool linked again only when an object or library file had changed, so -D HXCPP_LINK_STACK_SIZE, HXCPP_LINK_NO_MEMORY_GROWTH, HXCPP_LINK_NO_EMBIND or HXCPP_LINK_NODERAWFS on an Emscripten build, which change nothing that is compiled, or a flag or library added to a target in Build.xml, did nothing until the objects changed. The flags, libraries and library paths are now part of what a link is checked against, so each program links once more after updating diff --git a/include/hx/GC.h b/include/hx/GC.h index 863a08e5..cf48edad 100644 --- a/include/hx/GC.h +++ b/include/hx/GC.h @@ -541,6 +541,31 @@ class ImmixAllocator typedef ImmixAllocator GcAllocator; typedef ImmixAllocator Ctx; +// An object's mark byte, as the generational write barrier reads it. A +// barrier on another thread may be setting the remembered bit in it at the +// same time, which plain accesses made a data race, though either value read +// is handled. A relaxed load is a plain byte load where it compiles to one +inline unsigned char BarrierMarkByte(const void *inObj) +{ + const unsigned char *mark = reinterpret_cast(inObj) + HX_ENDIAN_MARK_ID_BYTE; + #if defined(__GNUC__) || defined(__clang__) + return __atomic_load_n(mark, __ATOMIC_RELAXED); + #else + return *reinterpret_cast(mark); + #endif +} + +// Sets the remembered bit, which is all a mutator changes in a mark byte +inline void BarrierRemember(const void *inObj) +{ + unsigned char *mark = const_cast(reinterpret_cast(inObj)) + HX_ENDIAN_MARK_ID_BYTE; + #if defined(__GNUC__) || defined(__clang__) + __atomic_fetch_or(mark, (unsigned char)HX_GC_REMEMBERED, __ATOMIC_RELAXED); + #else + *reinterpret_cast(mark) |= HX_GC_REMEMBERED; + #endif +} + #if defined(HXCPP_GC_INCREMENTAL) #define HX_OBJ_WB_CTX(obj,value,ctx) { const void *_hx_inc_value = value; if (_hx_inc_value && ::hx::gIncrementalActive.load(std::memory_order_relaxed)) { ::hx::Object *_hx_inc_owner = obj; if (!_hx_inc_owner || reinterpret_cast(_hx_inc_owner)[HX_ENDIAN_MARK_ID_BYTE] == ::hx::gByteMarkID) ::hx::IncrementalRemember(_hx_inc_owner,_hx_inc_value,ctx); } } @@ -548,20 +573,18 @@ typedef ImmixAllocator Ctx; #define HX_OBJ_WB_NEW_MARKED_OBJECT(obj) HX_OBJ_WB_PESSIMISTIC_CTX(obj,HX_CTX_GET) #elif defined(HXCPP_GC_GENERATIONAL) #define HX_OBJ_WB_CTX(obj,value,ctx) { \ - unsigned char &mark = ((unsigned char *)(obj))[ HX_ENDIAN_MARK_ID_BYTE]; \ - if (mark == ::hx::gByteMarkID && value && !((unsigned char *)(value))[ HX_ENDIAN_MARK_ID_BYTE ] ) { \ - mark|=HX_GC_REMEMBERED; \ + if (::hx::BarrierMarkByte(obj) == ::hx::gByteMarkID && value && !::hx::BarrierMarkByte(value) ) { \ + ::hx::BarrierRemember(obj); \ ctx->pushReferrer(obj); \ } } #define HX_OBJ_WB_PESSIMISTIC_CTX(obj,ctx) { \ - unsigned char &mark = ((unsigned char *)(obj))[ HX_ENDIAN_MARK_ID_BYTE]; \ - if (mark == ::hx::gByteMarkID) { \ - mark|=HX_GC_REMEMBERED; \ + if (::hx::BarrierMarkByte(obj) == ::hx::gByteMarkID) { \ + ::hx::BarrierRemember(obj); \ ctx->pushReferrer(obj); \ } } // I'm not sure if this will ever trigger... #define HX_OBJ_WB_NEW_MARKED_OBJECT(obj) { \ - if (((unsigned char *)(obj))[ HX_ENDIAN_MARK_ID_BYTE]==::hx::gByteMarkID) ::hx::NewMarkedObject(obj); \ + if (::hx::BarrierMarkByte(obj)==::hx::gByteMarkID) ::hx::NewMarkedObject(obj); \ } #else #define HX_OBJ_WB_CTX(obj,value,ctx) @@ -575,8 +598,7 @@ typedef ImmixAllocator Ctx; #elif defined(HXCPP_GC_GENERATIONAL) // Storing a young value in an old array: see ArrayYoungStore #define HX_ARRAY_WB(array,index,value) { \ - unsigned char mark = ((unsigned char *)(array))[ HX_ENDIAN_MARK_ID_BYTE]; \ - if (mark == ::hx::gByteMarkID && value && !((unsigned char *)(value))[ HX_ENDIAN_MARK_ID_BYTE ] ) \ + if (::hx::BarrierMarkByte(array) == ::hx::gByteMarkID && value && !::hx::BarrierMarkByte(value) ) \ ::hx::ArrayYoungStore(array, value, _hx_ctx); \ } #else diff --git a/src/hx/Hash.h b/src/hx/Hash.h index b6ee9ee5..e4833270 100644 --- a/src/hx/Hash.h +++ b/src/hx/Hash.h @@ -840,7 +840,7 @@ struct Hash : public HashBase< typename ELEMENT::Key > static inline bool IsNursery(const void *inPtr) { - return inPtr && !(((unsigned char *)inPtr)[ HX_ENDIAN_MARK_ID_BYTE]); + return inPtr && !hx::BarrierMarkByte(inPtr); } #ifdef HXCPP_GC_GENERATIONAL @@ -858,12 +858,11 @@ struct Hash : public HashBase< typename ELEMENT::Key > if (inC) hx::IncrementalRemember(this,inC,HX_CTX_GET); } #else - unsigned char &mark = ((unsigned char *)(this))[ HX_ENDIAN_MARK_ID_BYTE]; - if (mark == hx::gByteMarkID && (IsNursery(inA) || IsNursery(inB) || IsNursery(inC)) ) + if (hx::BarrierMarkByte(this) == hx::gByteMarkID && (IsNursery(inA) || IsNursery(inB) || IsNursery(inC)) ) { if (Element::WeakKeys) { - mark|=HX_GC_REMEMBERED; + hx::BarrierRemember(this); (HX_CTX_GET)->pushReferrer(this); } else diff --git a/src/hx/cppia/Cppia.cpp b/src/hx/cppia/Cppia.cpp index 3094c88b..e14d767d 100644 --- a/src/hx/cppia/Cppia.cpp +++ b/src/hx/cppia/Cppia.cpp @@ -4147,8 +4147,7 @@ inline static void * getPointerFrom(String *s) { return (void *)s->raw_ptr(); } #if defined(HXCPP_GC_GENERATIONAL) && defined(CPPIA_JIT) static void SLJIT_CALL pushWriteBarrier(hx::StackContext *inCtx, hx::Object *inObj) { - unsigned char &mark = ((unsigned char *)(inObj))[ HX_ENDIAN_MARK_ID_BYTE]; - mark|=HX_GC_REMEMBERED; + hx::BarrierRemember(inObj); inCtx->pushReferrer(inObj); } diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 96dd2786..a999cebb 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -2136,18 +2136,27 @@ void YoungStore(hx::Object *inOwner, unsigned int &ioLogged, int inLength, const if (inCtx->mYoungValues && inLength>=YOUNG_LOG_MIN_LENGTH) { // Counted per cycle: the cycle is in the top 12 bits. A lost update - // between threads only logs more than the limit - unsigned int logged = ioLogged; + // between threads only logs more than the limit, so relaxed accesses + // do, but plain ones were a data race + #if defined(__GNUC__) || defined(__clang__) + unsigned int logged = __atomic_load_n(&ioLogged, __ATOMIC_RELAXED); + #else + unsigned int logged = *(volatile unsigned int *)&ioLogged; + #endif unsigned int cycle = sCollectCycle & 0xfff; unsigned int count = (logged>>20)==cycle ? (logged & 0xfffff) : 0; unsigned int limit = std::min( (unsigned int)inLength>>5, 0xfffffu ); if (countmYoungValues; chunk->push( (hx::Object *)value ); @@ -2164,8 +2173,7 @@ void YoungStore(hx::Object *inOwner, unsigned int &ioLogged, int inLength, const return; } } - unsigned char &mark = ((unsigned char *)inOwner)[HX_ENDIAN_MARK_ID_BYTE]; - mark |= HX_GC_REMEMBERED; + BarrierRemember(inOwner); inCtx->pushReferrer(inOwner); }