diff --git a/Changes.md b/Changes.md index 816b4be7c..11c182b3e 100644 --- a/Changes.md +++ b/Changes.md @@ -21,6 +21,15 @@ * Fixed critial error handler returning the wrong callstack * Fixed ARM64 library names on Mac * Fixed generational GC when used with HXCPP_ALIGN_ALLOC +* Fixed heap corruption in the moving GC (HXCPP_GC_MOVING) with HXCPP_ALIGN_ALLOC: compaction did not charge the alignment padding of each moved object against the destination hole, so a block filled by moved objects overran its end +* Fixed the cppia jit write barrier treating new objects as old on the main thread until the first collection, which could let a collection free objects that only the stack held (#1196) +* Fixed HXCPP_GC_VERIFY reporting an unmarked row with the generational collector: the verification mark advanced the mark id twice per collection, so dead objects from 16 marks earlier could read as marked before the next table update cleared them (#1415) +* Fixed the moving GC updating the object id map through an iterator that the insert of the new address could have invalidated +* Fixed __hxcpp_id_obj reading one past the end of the id table for an id equal to its size +* Fixed the large allocation collection threshold overflowing once the block heap passes 2GB with HXCPP_GC_BIG_BLOCKS +* Fixed a crash in the next collection when a thread registered with the GC while a collection switched the generational collector on +* Fixed a spurious "Memory exhausted" trap when other threads claimed every block that the collecting thread had just added to the heap +* Fixed a thread hanging forever when it reached a GC safe point from inside a GC-free zone; it is now reported as a critical error (#947) * Fixed pthread structured being unaligned * Fixed cppia crash on functions with empty bodies * Fixed regression parsing integers which wrap around diff --git a/src/hx/Debug.cpp b/src/hx/Debug.cpp index c49cc935a..c5f437d7a 100644 --- a/src/hx/Debug.cpp +++ b/src/hx/Debug.cpp @@ -207,6 +207,13 @@ StackContext::StackContext() #endif #ifdef HXCPP_SCRIPTABLE + // The cppia jit's write barrier compares an object's mark byte with this copy of the + // collector's mark id, so it must match from the start. The collector only updates it at + // the end of each collection, and the main thread's context is created before any of them. + // Left uninitialised (0 in practice), every nursery object - whose mark byte is 0 - looked + // old to the barrier, which flagged it remembered and so hid the nursery objects allocated + // after it from the stack scan (#1196). + byteMarkId = ::hx::gByteMarkID; stack = new unsigned char[128*1024]; pointer = &stack[0]; push((hx::Object *)0); diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 367b2760a..f1aa6d675 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -411,6 +411,7 @@ static void VisitLocalAlloc(LocalAllocator *inAlloc,hx::VisitContext *__inCtx); static void WaitForSafe(LocalAllocator *inAlloc); static void ReleaseFromSafe(LocalAllocator *inAlloc); static void ClearPooledAlloc(LocalAllocator *inAlloc); +static void InitReferrersLocked(LocalAllocator *inAlloc); static void CollectFromThisThread(bool inMajor,bool inForceCompact); namespace hx @@ -3145,6 +3146,10 @@ class GlobalAllocator // Until we add ourselves, the collector will not wait // on us - ie, we are assumed ot be in a GC free zone. std::lock_guard lock(*gThreadStateChangeLock); + // The collector switches the gc mode and hands out or collects the referrer chunks of + // the registered allocators under this lock, so decide here, under the same lock, + // whether the new allocator needs one + InitReferrersLocked(inAlloc); mLocalAllocs.push(inAlloc); // TODO Attach debugger } @@ -3451,7 +3456,11 @@ class GlobalAllocator // malloc/new. This is not called very often, so the overhead should be minimal. // However, gcc inlines this function! requiring every alloc the have sjlj overhead. // Making it virtual prevents the overhead. - virtual bool AllocMoreBlocks(bool &outForceCompact, bool inJustBorrowing) + // + // If outReserved is given, the first new block is handed to the caller already owned and + // with its zero-lock held - the state GetNextFree returns blocks in - so that no other + // thread can claim it before the caller does. + virtual bool AllocMoreBlocks(bool &outForceCompact, bool inJustBorrowing, BlockDataInfo **outReserved = 0) { enum { newBlockCount = 1<<(IMMIX_BLOCK_GROUP_BITS) }; @@ -3536,6 +3545,13 @@ class GlobalAllocator BlockData *block = (BlockData *)(aligned + i*IMMIX_BLOCK_SIZE); BlockDataInfo *info = new BlockDataInfo(gid,block); + if (outReserved && !*outReserved) + { + info->mOwned = true; + info->mZeroLock = 1; + *outReserved = info; + } + mAllBlocks.push(info); mFreeBlocks.push(info); } @@ -3551,11 +3567,11 @@ class GlobalAllocator #if defined(SHOW_MEM_EVENTS_VERBOSE) || defined(SHOW_FRAGMENTATION_BLOCKS) if (inJustBorrowing) { - GCLOG("Borrow Blocks %d = %d k\n", mAllBlocks.size(), (mAllBlocks.size() << IMMIX_BLOCK_BITS)>>10); + GCLOG("Borrow Blocks %d = %d k\n", mAllBlocks.size(), (int)(GetWorkingMemory()>>10)); } else { - GCLOG("Blocks %d = %d k\n", mAllBlocks.size(), (mAllBlocks.size() << IMMIX_BLOCK_BITS)>>10); + GCLOG("Blocks %d = %d k\n", mAllBlocks.size(), (int)(GetWorkingMemory()>>10)); } #endif @@ -3609,11 +3625,15 @@ class GlobalAllocator result = GetNextFree(inRequiredBytes); #endif + // Threads released from a collection, or that never had to stop, keep claiming free + // blocks while this one holds the thread state lock, and can take every block that + // AllocMoreBlocks adds before GetNextFree runs again. So when the heap has to grow + // on this thread's behalf, have AllocMoreBlocks reserve one of the new blocks for it + // rather than reporting the memory as exhausted when the race is lost. bool forceCompact = false; if (!result && allowMoreBlocks() && (!sgInternalEnable || GetWorkingMemory()second] = inTo; - hx::sObjectIdMap[inTo] = id->second; + int objectId = id->second; + // Erase before inserting - the insert can rehash the map and invalidate the iterator hx::sObjectIdMap.erase(id); + hx::sObjectIdMap[inTo] = objectId; + hx::sIdObjectMap[objectId] = inTo; } // Maybe do something faster with weakmaps @@ -3802,7 +3823,14 @@ class GlobalAllocator } #ifdef HXCPP_ALIGN_ALLOC - destPos += ALIGN_PADDING(destPos); + // The fit test above reserved allocSize + padding, so the padding has to + // come out of destLen as well, or destPos + destLen drifts past the end + // of the hole and a later object is written over the next block + { + int alignPad = ALIGN_PADDING(destPos); + destPos += alignPad; + destLen -= alignPad; + } #endif int startRow = destPos>>IMMIX_LINE_BITS; @@ -4010,9 +4038,14 @@ class GlobalAllocator // TODO - not copy + paste - printf("Move!\n"); #ifdef HXCPP_ALIGN_ALLOC - destPos += ALIGN_PADDING(destPos); + // As in MoveBlocks: the padding was reserved by the fit test, so charge + // it against destLen too + { + int alignPad = ALIGN_PADDING(destPos); + destPos += alignPad; + destLen -= alignPad; + } #endif int startRow = destPos>>IMMIX_LINE_BITS; @@ -4204,7 +4237,7 @@ class GlobalAllocator void *GetIDObject(int inIndex) { std::lock_guard lock(*gSpecialObjectLock); - if (inIndex<0 || inIndex>hx::sIdObjectMap.size()) + if (inIndex<0 || inIndex>=hx::sIdObjectMap.size()) return 0; return hx::sIdObjectMap[inIndex]; } @@ -5046,6 +5079,10 @@ class GlobalAllocator { #ifdef HXCPP_GC_MOVING sgTimeToNextTableUpdate = 7; + #elif defined(HX_GC_VERIFY_GENERATIONAL) + // The verification mark advances the mark id a second time in each collection, so the + // count has to come down one sooner to keep the table update ahead of the 16 mark ids + sgTimeToNextTableUpdate = 14; #else sgTimeToNextTableUpdate = 15; #endif @@ -5251,7 +5288,7 @@ class GlobalAllocator #endif // Large alloc target - int blockSize = mAllBlocks.size()< mLargeAllocSpace) mLargeAllocSpace = blockSize; mLargeAllocForceRefresh = mLargeAllocated + mLargeAllocSpace; @@ -5847,11 +5884,7 @@ class LocalAllocator : public hx::StackContext { GCLOG("Uncleaned referrers\n"); } - - if (sGcMode==gcmGenerational) - mOldReferrers = hx::sGlobalChunks.alloc(); - else - mOldReferrers = 0; + // mOldReferrers is assigned by AddLocal, under the thread state lock #endif @@ -6050,6 +6083,11 @@ class LocalAllocator : public hx::StackContext if (sgIsCollecting) CriticalGCError("Bad Allocation while collecting - from finalizer?"); + // The collector neither waits for nor releases a thread in a GC-free zone, so waiting + // for mCollectDone from inside one would block this thread for good + if (mGCFreeZone) + CriticalGCError("PauseForCollect called from a GC-free zone - was ExitGCFreeZone missed?"); + mReadyForCollect.Set(); mCollectDone.Wait(); #endif @@ -6487,6 +6525,14 @@ void ClearPooledAlloc(LocalAllocator *inAlloc) inAlloc->Reset(); } +// Called with gThreadStateChangeLock held, as the allocator is registered +void InitReferrersLocked(LocalAllocator *inAlloc) +{ + #ifdef HXCPP_GC_GENERATIONAL + inAlloc->mOldReferrers = sGcMode==gcmGenerational ? hx::sGlobalChunks.alloc() : 0; + #endif +} + #ifdef HXCPP_VISIT_ALLOCS void VisitLocalAlloc(LocalAllocator *inAlloc,hx::VisitContext *__inCtx) { diff --git a/test/haxe/gc/TestGC.hx b/test/haxe/gc/TestGC.hx index 5b297d27a..904d0aa70 100644 --- a/test/haxe/gc/TestGC.hx +++ b/test/haxe/gc/TestGC.hx @@ -9,6 +9,15 @@ class CustomObject { public function new():Void {} } +class CompactValue { + public var id:Int; + public var prev:CompactValue; + public function new(id:Int, prev:CompactValue) { + this.id = id; + this.prev = prev; + } +} + class TestGC extends Test { function createDummy(val:Dynamic):Dynamic { return { dummy: val }; @@ -138,6 +147,25 @@ class TestGC extends Test { } #end + // A compaction moves live objects into other blocks. With HXCPP_GC_MOVING and the default + // HXCPP_ALIGN_ALLOC, the alignment padding of each moved object was not charged against the + // destination hole, so a block filled by moved objects overran its end and corrupted the next + // block (#1167, #1414). Without HXCPP_GC_MOVING this is an ordinary full collection. + public function testCompactKeepsObjects() { + var prev:CompactValue = null; + final values = [for (i in 0...200000) prev = new CompactValue(i, prev)]; + for (pass in 0...3) { + Gc.compact(); + var bad = 0; + for (i in 0...values.length) { + final value = values[i]; + if (value.id != i || value.prev != (i == 0 ? null : values[i - 1])) + bad++; + } + Assert.equals(0, bad, 'Compaction pass $pass corrupted $bad objects'); + } + } + public function testLargeAlloc() { var largeString = { var bytes = Bytes.alloc(4000);