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
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,14 @@ jobs:
working-directory: test/regression
run: |
failed=""
for test in GcMutationRoots GcContainerMutation IncrementalGcSlices IncrementalGcContainers IncrementalGcArrayChunks IncrementalGcDequeMoves IncrementalGcPacing IncrementalGcStartSweep IncrementalGcStats IncrementalGcFinishBoundary IncrementalGcVerifier IncrementalWindowsHeaders GcWeakMapChain GcWeakMapWalkCollect GenerationalFinalizerLifecycle GenerationalLargeBufferLifecycle; do
for test in GcMutationRoots GcContainerMutation IncrementalGcSlices IncrementalGcContainers IncrementalGcArrayChunks IncrementalGcDequeMoves IncrementalGcPacing IncrementalGcStartSweep IncrementalGcStats IncrementalGcFinishBoundary IncrementalGcVerifier IncrementalGcCounts IncrementalWindowsHeaders GcWeakMapChain GcWeakMapWalkCollect GenerationalFinalizerLifecycle GenerationalLargeBufferLifecycle; do
echo "::group::incremental gc: $test"
# The lifecycle fixtures' hxml selects generational mode; compile
# the same Main with incremental mode here instead.
if ! (cd "$test" && haxe -main Main -cpp bin -lib hxcpp -D HXCPP_GC_INCREMENTAL -D HXCPP_GC_INCREMENTAL_VERIFY -D HXCPP_GC_INCREMENTAL_TEST -D $HXCPP_ARCH_FLAG && bin/Main > out.txt && diff <(tr -d '\r' < stdout.txt) <(tr -d '\r' < out.txt)); then
# the same Main with incremental mode here instead. Gc.trace, which
# IncrementalGcCounts checks, counts in debug builds only.
extra=""
[ "$test" = IncrementalGcCounts ] && extra="-debug -D DEBUGSUFFIX="
if ! (cd "$test" && haxe -main Main -cpp bin -lib hxcpp -D HXCPP_GC_INCREMENTAL -D HXCPP_GC_INCREMENTAL_VERIFY -D HXCPP_GC_INCREMENTAL_TEST -D $HXCPP_ARCH_FLAG $extra && bin/Main > out.txt && diff <(tr -d '\r' < stdout.txt) <(tr -d '\r' < out.txt)); then
failed="$failed $test"
echo "::error::incremental gc failed: $test"
fi
Expand Down
7 changes: 7 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
libhxcpp 6.0.0 (unreleased)
------------------------------------------------------------

* Fixed two counts with HXCPP_GC_INCREMENTAL: Gc.memInfo64(MEM_INFO_LIVE) after an incremental cycle reported every used row as live, several times the bytes really live, where it is meant to report the last full reclaim, and Gc.trace with a cycle part done counted only the objects the cycle had not marked yet. MEM_INFO_LIVE changes only with a full reclaim now, and Gc.trace starts the cycle again, as __hxcpp_gc_census does (libhxcpp #453)
* Fixed a cpp.Pointer field, or a pointer boxed in a Dynamic, letting what it points into be freed when it points further in than 64 bytes, or 32 on 32-bit, as cpp.Pointer.arrayElem(a, 100) does: the fix for Haxe 4's AtomicInt kept only what a stack word would keep. A pointer field keeps its whole allocation now, large ones included, and pins it under HXCPP_GC_MOVING (libhxcpp #451)
* Fixed cppia hanging or crashing when several threads asked Std.isOfType about script objects, or cast them to host interfaces, at once: each question about an interface a class did not implement inserted into that class's table, unlocked, while other threads searched it. Loading modules on several threads at once could likewise fail with "No base defined for non-interface", or hang, as each load inserted the host class names it missed into a map the others walked. Both are only read after they are filled now (libhxcpp #466)
* Fixed Semaphore.tryAcquire with a negative timeout waiting for ever, as tryAcquire(deadline - now) does once the deadline has passed: since the fix for Haxe 5's Lock.wait() (libhxcpp #420), any negative timeout meant no limit. Only -1, which Haxe 5.0.0-preview.1's Lock.wait() passes for no timeout, waits with no limit now, and any other negative timeout returns at once, as 0 does (libhxcpp #461)
* Fixed 32-bit Windows programs stopping with "Bad local allocator - requesting memory from unregistered thread!" when the process had used 64 TLS slots before the runtime started, as a host with many DLLs, or a late-loaded hxcpp DLL, can: the collector read its per-thread context from the thread's inline TLS slots, of which there are only 64. Later slots are read with TlsGetValue, as on x64 (libhxcpp #458)
* Fixed Windows programs not starting on Windows 8.1, Server 2012 R2 and Windows 10 before 1607 with a missing entry point, HXCPP_WINXP_COMPAT or not: every program imported GetThreadDescription and SetThreadDescription, which those lack. They are looked up when used now, and where they are missing a thread's name is null and setting it does nothing (libhxcpp #456)
* Fixed Sys.sleep, Sys.command and Sys.getch ending the process with "GCFree Zone mismatch" when called inside a GC-free zone the caller had entered, as with cpp.vm.Gc.enterGCFreeZone around a blocking call: they left that zone themselves, so the caller's own exit failed. They keep the caller's zone, as Mutex, Lock and Semaphore do. CFFI's gc_safe_point, between gc_enter_blocking and gc_exit_blocking, no longer pauses as if outside the zone, which ended the process with "Bad Allocation while collecting" or waited for ever (libhxcpp #457)
* 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
18 changes: 8 additions & 10 deletions include/hx/Tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
#ifdef HXCPP_M64
mFastOffset = mSlot*sizeof(void *) + 0x1480;
#else
if (FAST || mSlot < kMaxInlineSlots)
mFastOffset = mSlot*sizeof(void *) + 0xE10;
else
mFastOffset = mSlot - kMaxInlineSlots;
// Only the first 64 slots are in the thread's environment block. The
// rest are in an array of their own, which a thread that never set
// one does not have yet, so they go through TlsGetValue, as on x64.
// Reading every slot inline, as FAST once did, read other fields of
// the block once the process had used 64 slots (libhxcpp #458)
mFastOffset = mSlot < kMaxInlineSlots ? mSlot*sizeof(void *) + 0xE10 : 0;
#endif
}
inline DATA *operator=(DATA *inData)
Expand All @@ -59,13 +61,9 @@
inline operator DATA *()
{
#if !defined(HXCPP_M64) && (_MSC_VER >= 1400)
const size_t kTibExtraTlsOffset = 0xF94;

if (FAST || mSlot < kMaxInlineSlots)
if (mSlot < kMaxInlineSlots)
return (DATA *)__readfsdword(mFastOffset);

DATA **extra = (DATA **)(__readfsdword(kTibExtraTlsOffset));
return extra[mFastOffset];
return (DATA *)TlsGetValue(mSlot);
#elif (_MSC_VER >= 1400) & !defined(HXCPP_DEBUG) && !defined(HXCPP_ARM64)// 64 bit version...
if (mSlot < 64)
return (DATA *)__readgsqword(mFastOffset);
Expand Down
6 changes: 4 additions & 2 deletions src/hx/CFFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,12 @@ bool gc_try_unblocking()
}


// The runtime's own safe point, which does nothing in a GC-free zone, as
// between gc_enter_blocking and gc_exit_blocking: pausing there was "Bad
// Allocation while collecting", or waited for a release that never came
void gc_safe_point()
{
if (hx::gPauseForCollect)
hx::PauseForCollect();
__hxcpp_gc_safe_point();
}

gcroot create_root(value) { return 0; }
Expand Down
7 changes: 5 additions & 2 deletions src/hx/cppia/CppiaClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2246,8 +2246,11 @@ void *hx::Object::_hx_getInterface(int inId)
if (!vtable)
return 0;
CppiaClassInfo *info = ((CppiaClassInfo *)vtable[-1]);
void *result = info->interfaceScriptTables[inId];
return info->interfaceScriptTables[inId];
// Filled when the class links. operator[] inserted an entry for each
// interface asked about that the class does not implement, from whichever
// thread asked, while others searched the same map (libhxcpp #466)
std::map<int, void *>::iterator it = info->interfaceScriptTables.find(inId);
return it==info->interfaceScriptTables.end() ? 0 : it->second;
}


Expand Down
3 changes: 2 additions & 1 deletion src/hx/cppia/CppiaModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace hx
{


static int sScriptId = 0;
// Modules load on any thread
static std::atomic<int> sScriptId(0);
static std::atomic<int> sLiveModules(0);


Expand Down
21 changes: 17 additions & 4 deletions src/hx/cppia/HaxeNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <hx/Scriptable.h>
#include "Cppia.h"
#include <algorithm>
#include <mutex>


namespace hx
Expand Down Expand Up @@ -101,18 +102,27 @@ ScriptNamedFunction HaxeNativeClass::findStaticFunction(String inName)



// The maps are filled as the host starts, and only read after, by any thread
// loading or running a script: a lookup with operator[] inserted the names it
// missed, under threads walking or searching the same map (libhxcpp #466)
HaxeNativeClass *HaxeNativeClass::findClass(const std::string &inName)
{
return sScriptRegistered ? (*sScriptRegistered)[inName] : 0;
if (!sScriptRegistered)
return 0;
ScriptRegisteredMap::iterator it = sScriptRegistered->find(inName);
return it==sScriptRegistered->end() ? 0 : it->second;
}

HaxeNativeClass *HaxeNativeClass::hxObject()
{
return sScriptRegistered ? (*sScriptRegistered)["hx.Object"] : 0;
return findClass("hx.Object");
}

void HaxeNativeClass::link()
{
// Each load links, and the super classes it sets are shared by every module
static std::mutex linkLock;
std::lock_guard<std::mutex> lock(linkLock);
HaxeNativeClass *hxObj = hxObject();
for(ScriptRegisteredMap::iterator i = sScriptRegistered->begin(); i!=sScriptRegistered->end();++i)
{
Expand All @@ -131,7 +141,7 @@ void HaxeNativeClass::link()
hx::Class superClass = cls->GetSuper();
if (superClass.mPtr)
{
HaxeNativeClass *superRef = (*sScriptRegistered)[superClass.mPtr->mName.utf8_str()];
HaxeNativeClass *superRef = findClass(superClass.mPtr->mName.utf8_str());
if (superRef)
{
DBGLOG("registered %s\n",superClass.mPtr->mName.utf8_str());
Expand Down Expand Up @@ -282,7 +292,10 @@ void ScriptableRegisterInterface( String inName,

HaxeNativeInterface *HaxeNativeInterface::findInterface(const std::string &inName)
{
return sScriptRegisteredInterface ? (*sScriptRegisteredInterface)[inName] : 0;
if (!sScriptRegisteredInterface)
return 0;
HaxeNativeIntefaceMap::iterator it = sScriptRegisteredInterface->find(inName);
return it==sScriptRegisteredInterface->end() ? 0 : it->second;
}


Expand Down
62 changes: 42 additions & 20 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ struct BlockDataInfo
}

#ifdef HXCPP_GC_NURSERY
AllocType GetEnclosingNurseryType(uintptr_t inOffset, uintptr_t* outPtr) const
AllocType GetEnclosingNurseryType(uintptr_t inOffset, uintptr_t* outPtr, uintptr_t inReach=sgCheckInternalOffset) const
{
// The block did not get used in the previous cycle, so allocStart is invalid and
// no new objects should be in here
Expand Down Expand Up @@ -1505,7 +1505,7 @@ struct BlockDataInfo

if (inOffset>=scan && inOffset<end)
{
if (inOffset > scan + sgCheckInternalOffset)
if (inOffset > scan + inReach)
return allocNone;

if (nullptr != outPtr)
Expand Down Expand Up @@ -1559,9 +1559,9 @@ struct BlockDataInfo
return GetAllocTypeChecked(inOffset,inAllowPrevious);
}

AllocType GetEnclosingAllocType(uintptr_t inOffset, uintptr_t* outPtr, bool inAllowPrevious) const
AllocType GetEnclosingAllocType(uintptr_t inOffset, uintptr_t* outPtr, bool inAllowPrevious, uintptr_t inReach=sgCheckInternalOffset) const
{
for (uintptr_t dx{ 0 }; dx <= sgCheckInternalOffset; dx += 4)
for (uintptr_t dx{ 0 }; dx <= inReach && dx <= inOffset; dx += 4)
{
uintptr_t blockOffset{ inOffset - dx };
if (blockOffset >= 0)
Expand Down Expand Up @@ -1601,7 +1601,7 @@ struct BlockDataInfo
}

#ifdef HXCPP_GC_NURSERY
return GetEnclosingNurseryType(inOffset,outPtr);
return GetEnclosingNurseryType(inOffset,outPtr,inReach);
#endif
// Not a actual start...
return allocNone;
Expand Down Expand Up @@ -6547,13 +6547,20 @@ class GlobalAllocator
{
hx::IncrementalState &state = hx::IncrementalData();
bool active = hx::gIncrementalActive.load(std::memory_order_relaxed);
bool requested = hx::sIncrementalRequestMs > 0 && !sgCensus;
// A census, or Gc.trace, counts what this collection marks, so a cycle
// part done, whose earlier slices marked objects already, is failed and
// everything marked again: finishing it undercounted (libhxcpp #453)
bool counting = sgCensus;
#ifdef HXCPP_DEBUG
counting = counting || gCollectTrace;
#endif
bool requested = hx::sIncrementalRequestMs > 0 && !counting;
if (active && !requested)
{
std::lock_guard<std::mutex> lock(state.statsLock);
state.forcedFinishes++;
}
if (active && sgCensus)
if (active && counting)
state.failed.store(true, std::memory_order_relaxed);
if (active && hx::sIncrementalRequestMs == 0)
{
Expand Down Expand Up @@ -7046,11 +7053,10 @@ class GlobalAllocator
STAMP(t5)

size_t mem = mRowsInUse<<IMMIX_LINE_BITS;
#ifdef HXCPP_GC_INCREMENTAL
if (full || cycleFinished)
#else
// Only a full reclaim counts the bytes of the objects that survived. A
// finished incremental cycle that is not one counts whole rows, which
// reported several times the live bytes as live (libhxcpp #453)
if (full)
#endif
mLiveBytes = stats.bytesInUse;
size_t baseMem = full ? bytesInUse : mem;
#ifdef HXCPP_GC_DYNAMIC_SIZE
Expand Down Expand Up @@ -7609,7 +7615,7 @@ class GlobalAllocator
// allocation finds it too, as GetEnclosingAllocType does in a block, and
// outBase is set to where it starts. Native code may hold only a field's
// address, which kept a small object alive but not a large one (libhxcpp #373)
MemType GetMemType(uintptr_t inPtr, uintptr_t *outBase = nullptr)
MemType GetMemType(uintptr_t inPtr, uintptr_t *outBase = nullptr, size_t inReach = 0)
{
BlockData* block{ reinterpret_cast<BlockData*>(inPtr & IMMIX_BLOCK_BASE_MASK) };

Expand All @@ -7621,9 +7627,10 @@ class GlobalAllocator
// Most stack words are nowhere near the large allocations - reject on
// the cached bounds before paying for the full list scan. The bounds
// are of the starts, so a pointer into the highest reaches past them
size_t reach = outBase ? sgCheckInternalOffset : 0;
// inReach, for a pointer field, goes further: see MarkConservativePointer
size_t reach = outBase ? (inReach ? inReach : sgCheckInternalOffset) : 0;
size_t p = (size_t)inPtr;
if (p < mLargeMin || p > mLargeMax + reach)
if (p < mLargeMin || (p > mLargeMax && p - mLargeMax > reach))
return memUnmanaged;
uintptr_t base = 0;

Expand Down Expand Up @@ -7791,7 +7798,9 @@ MarkChunk *MarkChunk::swapForNew()


HX_NO_SANITIZE_ADDRESS
void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
// inReach is how far into an allocation a pointer may be and still keep it:
// 0 for a stack word, which uses sgCheckInternalOffset
static void MarkConservativeReach(int *inBottom, int *inTop,hx::MarkContext *__inCtx, uintptr_t inReach)
{
#ifdef VERIFY_STACK_READ
VerifyStackRead(inBottom, inTop);
Expand Down Expand Up @@ -7853,7 +7862,7 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
hx::localCount++;
#endif
uintptr_t largeBase{};
MemType mem{ sGlobalAlloc->GetMemType(potentialObject, &largeBase) };
MemType mem{ sGlobalAlloc->GetMemType(potentialObject, &largeBase, inReach) };

#ifdef HX_WATCH
isWatch = false;
Expand Down Expand Up @@ -7898,8 +7907,8 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)

size_t pos{ potentialObject & IMMIX_BLOCK_OFFSET_MASK };
AllocType t{
sgCheckInternalOffset ?
info->GetEnclosingAllocType(pos - sizeof(int), &potentialObject, allowPrevious) :
(inReach || sgCheckInternalOffset) ?
info->GetEnclosingAllocType(pos - sizeof(int), &potentialObject, allowPrevious, inReach ? inReach : sgCheckInternalOffset) :
info->GetAllocType(pos - sizeof(int), allowPrevious) };

#ifdef HX_WATCH
Expand Down Expand Up @@ -7982,15 +7991,28 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
#endif
}

void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
{
MarkConservativeReach(inBottom, inTop, __inCtx, 0);
}

// A cpp.Pointer field, or a pointer boxed in a Dynamic, keeps what it points
// into wherever it points in it, as cpp.Pointer.arrayElem(a, 100) does. A
// stack word keeps an allocation only within sgCheckInternalOffset of its
// start, since a pointer further in is usually held from the start too, but a
// field may be all there is (libhxcpp #451). In a block, nothing bigger than
// the block, and a large allocation is checked against its own size
static const uintptr_t sPointerFieldReach = ~(uintptr_t)0 >> 1;

void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx)
{
void *word[1] = { inPtr };
// MarkConservative reads every pointer-sized word starting on an int in
// the range, so this covers the one word and nothing past it
#ifdef HXCPP_STACK_UP
MarkConservative(reinterpret_cast<int *>(word + 1) - 1, reinterpret_cast<int *>(word) + 1, __inCtx);
MarkConservativeReach(reinterpret_cast<int *>(word + 1) - 1, reinterpret_cast<int *>(word) + 1, __inCtx, sPointerFieldReach);
#else
MarkConservative(reinterpret_cast<int *>(word), reinterpret_cast<int *>(word + 1), __inCtx);
MarkConservativeReach(reinterpret_cast<int *>(word), reinterpret_cast<int *>(word + 1), __inCtx, sPointerFieldReach);
#endif
}

Expand Down
Loading
Loading