Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 31 additions & 9 deletions include/hx/GC.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,27 +541,50 @@ 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<const unsigned char *>(inObj) + HX_ENDIAN_MARK_ID_BYTE;
#if defined(__GNUC__) || defined(__clang__)
return __atomic_load_n(mark, __ATOMIC_RELAXED);
#else
return *reinterpret_cast<const volatile unsigned char *>(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<unsigned char *>(reinterpret_cast<const unsigned char *>(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<volatile unsigned char *>(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<unsigned char *>(_hx_inc_owner)[HX_ENDIAN_MARK_ID_BYTE] == ::hx::gByteMarkID) ::hx::IncrementalRemember(_hx_inc_owner,_hx_inc_value,ctx); } }
#define HX_OBJ_WB_PESSIMISTIC_CTX(obj,ctx) { if (::hx::gIncrementalActive.load(std::memory_order_relaxed)) { ::hx::Object *_hx_inc_owner = obj; if (!_hx_inc_owner || reinterpret_cast<unsigned char *>(_hx_inc_owner)[HX_ENDIAN_MARK_ID_BYTE] == ::hx::gByteMarkID) ::hx::IncrementalRemember(_hx_inc_owner,nullptr,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)
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/hx/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/hx/cppia/Cppia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 14 additions & 6 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (count<limit)
{
ioLogged = (cycle<<20) | (count+1);
#if defined(__GNUC__) || defined(__clang__)
__atomic_store_n(&ioLogged, (cycle<<20) | (count+1), __ATOMIC_RELAXED);
#else
*(volatile unsigned int *)&ioLogged = (cycle<<20) | (count+1);
#endif
for(int v=0;v<inCount;v++)
{
const void *value = inValues[v];
if (!value || ((unsigned char *)value)[HX_ENDIAN_MARK_ID_BYTE])
if (!value || BarrierMarkByte(value))
continue;
MarkChunk *chunk = inCtx->mYoungValues;
chunk->push( (hx::Object *)value );
Expand All @@ -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);
}

Expand Down
Loading