From 82c0e330a63fa64eeb07f6dc67f8e7f8664cf980 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 07:43:08 -0500 Subject: [PATCH 1/8] Clear a moved object's start bit in the block it left MoveBlocks walked each source block's start bits through a local copy, so the block kept every bit. When the blocks to move into ran out part way through one, that block was kept with the start bits of the objects it had moved out, whose headers are IMMIX_OBJECT_HAS_MOVED. A stale stack word holding one of those addresses then reached GetAllocTypeChecked, which took the header's mark byte of 0 for the previous mark one collection in 16, and MarkAllocUnchecked marked 0xfe rows from there, past the 256 bytes of the row table and into the block's first data rows. Once marked, the header looked live on every later collection. The block's own bit is cleared as each object moves, and GetAllocTypeChecked refuses a moved header outright. Test: GcMovedStartBits, under HXCPP_GC_MOVING, finds a block emptied in part and puts 407 of its moved-out addresses on the stack: the collector marked all 407 as objects before and none now, 3 of 3 runs on Windows x64. Fixes #450 --- Changes.md | 1 + src/hx/gc/Immix.cpp | 10 ++ test/regression/GcMovedStartBits/Main.hx | 149 ++++++++++++++++++++ test/regression/GcMovedStartBits/build.hxml | 3 + test/regression/GcMovedStartBits/stdout.txt | 4 + 5 files changed, 167 insertions(+) create mode 100644 test/regression/GcMovedStartBits/Main.hx create mode 100644 test/regression/GcMovedStartBits/build.hxml create mode 100644 test/regression/GcMovedStartBits/stdout.txt diff --git a/Changes.md b/Changes.md index 54466e16..d35fc4b3 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed HXCPP_GC_MOVING, and HXCPP_GC_DEBUG_ALWAYS_MOVE without generational, corrupting live objects after a collection that ran out of blocks to move into: the block it was emptying kept, in part, the start bits of the objects it had moved out, and a stale stack word holding one of their old addresses had a later collection take the moved header for an object of 254 rows and mark rows past the block's row table, over the objects in its first rows. The start bits go as the objects move, and a moved header is never taken for an object (libhxcpp #450) * 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/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 96dd2786..2d682d03 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -1433,6 +1433,11 @@ struct BlockDataInfo // When known to be an actual object start... AllocType GetAllocTypeChecked(uintptr_t inOffset, bool allowPrevious) const { + // Where an object moved out, not an object: its mark byte is 0, which + // passes for the previous mark one collection in 16 + if (*reinterpret_cast(mPtr->mData + inOffset) == IMMIX_OBJECT_HAS_MOVED) + return allocNone; + unsigned char time{ mPtr->mData[inOffset + HX_ENDIAN_MARK_ID_BYTE_HEADER] }; if (((time + 1) & MARK_BYTE_MASK) != (gByteMarkID & MARK_BYTE_MASK)) { @@ -5129,6 +5134,11 @@ class GlobalAllocator header = IMMIX_OBJECT_HAS_MOVED; moveObjs++; + // The block's own start bit goes too: when the to blocks run + // out, this block is kept in part, and a start left here + // would have the next collection take the moved header for + // an object of 254 rows, marking rows past the row table + allocStart[r] &= ~(1u< +#include +#include +static std::vector gOld; +static std::vector gStale; +static std::vector gNow; +static int gPartial = 0; +static void recordOld(hx::Object *o) { gOld.push_back(~(uintptr_t)o); } +static void recordNow(hx::Object *o) { gNow.push_back(~(uintptr_t)o); } +static unsigned int headerAt(uintptr_t p) { return ((unsigned int *)p)[-1]; } +// The moved-out addresses in blocks that kept some of their objects +static int pickStale() +{ + enum { BLOCK = 1<<15 }; + std::map flags; + int n = (int)gOld.size(); + for(int i=0;i::iterator f=flags.begin(); f!=flags.end(); ++f) + if (f->second==3) + gPartial++; + gStale.clear(); + for(int i=0;i>24)!=0) + n++; + } + return n; +} +static void collectWithStale() +{ + volatile uintptr_t slots[4096]; + size_t n = gStale.size(); + for(size_t i=0;i = []; + var junk:Array = []; + for (i in 0...300000) { + var it = new Item(i); + if (i % 3 == 2) + junk.push(it); + else + keep.push(it); + } + junk = null; + + // The collection after the moving one takes a mark byte of 0 for the + // previous mark when its mark id ends in 1 + while (markNibble() != 15) + Gc.run(false); + + for (it in keep) + untyped __cpp__("recordOld({0}.mPtr)", it); + Gc.run(true); + for (it in keep) + untyped __cpp__("recordNow({0}.mPtr)", it); + var stale:Int = untyped __cpp__("pickStale()"); + var partial:Int = untyped __cpp__("gPartial"); + Sys.stderr().writeString('partly emptied blocks: $partial, moved-out addresses: $stale\n'); + + var fillers = new haxe.ds.Vector(40000); + untyped __cpp__("collectWithStale()"); + // Nothing else allocates before the fillers take the rows the moved objects left + for (i in 0...fillers.length) + fillers[i] = new Filler(); + Sys.println("moved-out locations marked as objects: " + untyped __cpp__("remarked()")); + untyped __cpp__("collectWithStale()"); + Sys.println("moved-out locations marked as objects, again: " + untyped __cpp__("remarked()")); + + var bad = 0; + for (f in fillers) + if (f.a != 0 || f.b != 0 || f.c != 0 || f.d != 0 || f.e != 0 || f.f != 0) + bad++; + Sys.println("fillers changed: " + bad); + var wrong = 0; + for (i in 0...keep.length) + if (keep[i].x != ((i >> 1) * 3) + (i & 1)) + wrong++; + Sys.println("kept items with a wrong value: " + wrong); + } +} diff --git a/test/regression/GcMovedStartBits/build.hxml b/test/regression/GcMovedStartBits/build.hxml new file mode 100644 index 00000000..d97fb616 --- /dev/null +++ b/test/regression/GcMovedStartBits/build.hxml @@ -0,0 +1,3 @@ +-m Main +--cpp bin +-D HXCPP_GC_MOVING diff --git a/test/regression/GcMovedStartBits/stdout.txt b/test/regression/GcMovedStartBits/stdout.txt new file mode 100644 index 00000000..441c15b4 --- /dev/null +++ b/test/regression/GcMovedStartBits/stdout.txt @@ -0,0 +1,4 @@ +moved-out locations marked as objects: 0 +moved-out locations marked as objects, again: 0 +fillers changed: 0 +kept items with a wrong value: 0 From 12a08c68a0d65914b858167d6c099af3a93c1f55 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 07:49:15 -0500 Subject: [PATCH 2/8] Write cppia array elements where they are Three array builtins wrote to the wrong memory on valid code: - The JIT's a[i]++ with a Float result called runFloatCrement, which takes a double * for the result, with two arguments, so it stored 8 bytes through whatever the third argument register held. It passes the result's address now. - The JIT's typed push checked the capacity and expanded before it ran its argument, so an argument that grew the same array to its capacity left the store past the buffer. The argument runs first. - The interpreter's ArraySetter took a reference to the element before running the right-hand side, so a right-hand side that grew or reallocated the array stored into the old buffer. It works on a copy of the element and finds the element after, which also stores a[a.length] = a.length as the JIT and the other targets do. Test: CppiaArrayWrites, interpreted and under the JIT. Before, the JIT corrupted the Int array and its neighbour, and the interpreter stored 1 for 42 and 43 and appended 4 for 3; both engines print the expected output now, on Windows x64. Fixes #463 --- Changes.md | 1 + src/hx/cppia/ArrayBuiltin.cpp | 50 +++++++++++---- test/regression/CppiaArrayWrites/Main.hx | 13 ++++ test/regression/CppiaArrayWrites/Script.hx | 70 +++++++++++++++++++++ test/regression/CppiaArrayWrites/build.hxml | 7 +++ test/regression/CppiaArrayWrites/stdout.txt | 16 +++++ 6 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 test/regression/CppiaArrayWrites/Main.hx create mode 100644 test/regression/CppiaArrayWrites/Script.hx create mode 100644 test/regression/CppiaArrayWrites/build.hxml create mode 100644 test/regression/CppiaArrayWrites/stdout.txt diff --git a/Changes.md b/Changes.md index d35fc4b3..ed010e82 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed three cppia array builtins writing to the wrong memory: under the JIT, a[i]++ or --a[i] whose result was used as a Float stored that result through whatever the third argument register held, corrupting the array or memory near it, and a typed push made room before running its argument, so an argument that grew the same array had the value stored past its buffer. Interpreted, a[i] = rhs and a[i] += rhs found the element before running rhs, so an rhs that grew the array stored into the old buffer, and a[a.length] = a.length stored the length after the store. The interpreter and the JIT now agree, and match the other targets (libhxcpp #463) * Fixed HXCPP_GC_MOVING, and HXCPP_GC_DEBUG_ALWAYS_MOVE without generational, corrupting live objects after a collection that ran out of blocks to move into: the block it was emptying kept, in part, the start bits of the objects it had moved out, and a stale stack word holding one of their old addresses had a later collection take the moved header for an object of 254 rows and mark rows past the block's row table, over the objects in its first rows. The start bits go as the objects move, and a moved header is never taken for an object (libhxcpp #450) * 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 diff --git a/src/hx/cppia/ArrayBuiltin.cpp b/src/hx/cppia/ArrayBuiltin.cpp index 0ecacbf9..86c6021c 100644 --- a/src/hx/cppia/ArrayBuiltin.cpp +++ b/src/hx/cppia/ArrayBuiltin.cpp @@ -153,8 +153,14 @@ struct ArraySetter : public ArrayBuiltinBase CPPIA_CHECK(thisVal); int i = args[0]->runInt(ctx); BCR_CHECK; + // On a copy, and the element found after: the right-hand side can grow + // the array or move its items, which left a reference taken before + // pointing at the old buffer + ELEM value = thisVal->__get(i); + FUNC::run(value, ctx, args[1]); + BCR_CHECK; ELEM &elem = thisVal->Item(i); - FUNC::run(elem, ctx, args[1]); + elem = value; #ifdef HXCPP_GC_GENERATIONAL if (hx::ContainsPointers()) HX_OBJ_WB_CTX(thisVal,hx::PointerOf(elem),ctx); @@ -168,8 +174,14 @@ struct ArraySetter : public ArrayBuiltinBase CPPIA_CHECK(thisVal); int i = args[0]->runInt(ctx); BCR_CHECK; + // On a copy, and the element found after: the right-hand side can grow + // the array or move its items, which left a reference taken before + // pointing at the old buffer + ELEM value = thisVal->__get(i); + FUNC::run(value, ctx, args[1]); + BCR_CHECK; ELEM &elem = thisVal->Item(i); - FUNC::run(elem, ctx, args[1]); + elem = value; #ifdef HXCPP_GC_GENERATIONAL if (hx::ContainsPointers()) HX_OBJ_WB_CTX(thisVal,hx::PointerOf(elem),ctx); @@ -183,8 +195,14 @@ struct ArraySetter : public ArrayBuiltinBase CPPIA_CHECK(thisVal); int i = args[0]->runInt(ctx); BCR_CHECK; + // On a copy, and the element found after: the right-hand side can grow + // the array or move its items, which left a reference taken before + // pointing at the old buffer + ELEM value = thisVal->__get(i); + FUNC::run(value, ctx, args[1]); + BCR_CHECK; ELEM &elem = thisVal->Item(i); - FUNC::run(elem, ctx, args[1]); + elem = value; #ifdef HXCPP_GC_GENERATIONAL if (hx::ContainsPointers()) HX_OBJ_WB_CTX(thisVal,hx::PointerOf(elem),ctx); @@ -198,8 +216,14 @@ struct ArraySetter : public ArrayBuiltinBase CPPIA_CHECK(thisVal); int i = args[0]->runInt(ctx); BCR_CHECK; + // On a copy, and the element found after: the right-hand side can grow + // the array or move its items, which left a reference taken before + // pointing at the old buffer + ELEM value = thisVal->__get(i); + FUNC::run(value, ctx, args[1]); + BCR_CHECK; ELEM &elem = thisVal->Item(i); - FUNC::run(elem, ctx, args[1]); + elem = value; #ifdef HXCPP_GC_GENERATIONAL if (hx::ContainsPointers()) HX_OBJ_WB_CTX(thisVal,hx::PointerOf(elem),ctx); @@ -1333,20 +1357,23 @@ struct ArrayBuiltin : public ArrayBuiltinBase { thisExpr->genCode(compiler, sJitTemp0, etObject); genNullReferenceExceptionCheck(compiler, sJitTemp0); - JumpId enough = compiler->compare( cmpI_LESS, sJitTemp0.star(jtInt, ArrayBase::lengthOffset()), - sJitTemp0.star(jtInt, ArrayBase::allocOffset()) ); - - // result comes back in sJitTemp0 - compiler->callNative( (void *)array_expand, sJitTemp0 ); - compiler->comeFrom(enough); JitTemp arrayThis(compiler,jtPointer); compiler->move(arrayThis, sJitTemp0); + // The value first: an argument that grows the array would otherwise + // leave the room made for it too small ExprType elemType = (ExprType)ExprTypeOf::value; JitTemp tempVal(compiler,getJitType(elemType)); args[0]->genCode(compiler, tempVal, elemType); + compiler->move(sJitTemp0, arrayThis); + JumpId enough = compiler->compare( cmpI_LESS, sJitTemp0.star(jtInt, ArrayBase::lengthOffset()), + sJitTemp0.star(jtInt, ArrayBase::allocOffset()) ); + + compiler->callNative( (void *)array_expand, sJitTemp0 ); + compiler->comeFrom(enough); + // sJitTemp1 = this compiler->move(sJitTemp1, arrayThis); // sJitTemp0 = length @@ -1972,7 +1999,8 @@ struct ArrayBuiltin : public ArrayBuiltinBase { JitTemp tempFloat(compiler, jtFloat); JitVal dVal = isMemoryVal(inDest) ? inDest : tempFloat; - compiler->callNative( (void *)runFloatCrement, thisVal, sJitTemp1 ); + // The result goes where dVal is: a float in memory is passed by address + compiler->callNative( (void *)runFloatCrement, thisVal, sJitTemp1, dVal.as(jtFloat) ); if (!isMemoryVal(inDest)) compiler->move(inDest.as(jtFloat), tempFloat); } diff --git a/test/regression/CppiaArrayWrites/Main.hx b/test/regression/CppiaArrayWrites/Main.hx new file mode 100644 index 00000000..e1b4cc7a --- /dev/null +++ b/test/regression/CppiaArrayWrites/Main.hx @@ -0,0 +1,13 @@ +// Runs the script interpreted and then under the JIT +function main() { + var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]); + for (jit in [false, true]) { + cpp.cppia.Host.enableJit(jit); + Sys.println(jit ? "jit" : "interpreted"); + try { + cpp.cppia.Host.runFile(script); + } catch (e:Dynamic) { + Sys.println("error: " + e); + } + } +} diff --git a/test/regression/CppiaArrayWrites/Script.hx b/test/regression/CppiaArrayWrites/Script.hx new file mode 100644 index 00000000..94186287 --- /dev/null +++ b/test/regression/CppiaArrayWrites/Script.hx @@ -0,0 +1,70 @@ +// libhxcpp #463: array builtins that wrote to the wrong memory. +// - Under the JIT, a[i]++ used as a Float stored its result through whatever +// the third argument register held. +// - Under the JIT, a typed push made room before running its argument, so an +// argument that grew the array left too little room. +// - Interpreted, a[i] = rhs found the element before running rhs, so an rhs +// that grew the array stored into the old buffer. +class Script { + static var spy:Array; + static var arr:Array; + + static function main() { + crements(); + pushes(); + stores(); + } + + static function crements() { + var f:Array = [1.5, 2.5]; + var x:Float = f[0]++; + var y:Float = ++f[1]; + Sys.println("float crement: x=" + x + " y=" + y + " " + f); + var a = [10, 20]; + var x:Float = a[0]++; + var y:Float = --a[1]; + Sys.println("int crement: x=" + x + " y=" + y + " " + a); + } + + // Grows a to exactly its capacity, then puts spy's buffer right after it + static function grow(a:Array):String { + a.resize(a.length + 100); + spy.resize(4); + return "pushed"; + } + + static function growInt(a:Array):Int { + a.resize(a.length + 101); + spy.resize(4); + return 0x7777; + } + + static function pushes() { + spy = []; + var a = ["x"]; + a.push(grow(a)); + Sys.println("push string: length=" + a.length + " last=" + a[a.length - 1] + " spy=" + spy); + spy = []; + var b = [1]; + b.push(growInt(b)); + Sys.println("push int: length=" + b.length + " last=" + b[b.length - 1] + " spy=" + spy); + } + + static function growAndGet():Int { + for (i in 0...1000) + arr.push(i); + return 42; + } + + static function stores() { + arr = [1, 2, 3]; + arr[0] = growAndGet(); + Sys.println("store: " + arr[0] + " length=" + arr.length); + arr = [1, 2, 3]; + arr[0] += growAndGet(); + Sys.println("compound store: " + arr[0]); + arr = [1, 2, 3]; + arr[arr.length] = arr.length; + Sys.println("store past the end: " + arr); + } +} diff --git a/test/regression/CppiaArrayWrites/build.hxml b/test/regression/CppiaArrayWrites/build.hxml new file mode 100644 index 00000000..d20d36bd --- /dev/null +++ b/test/regression/CppiaArrayWrites/build.hxml @@ -0,0 +1,7 @@ +--main Script +-cppia bin/script.cppia +--next +--main Main +-cpp bin +-D scriptable +--dce no diff --git a/test/regression/CppiaArrayWrites/stdout.txt b/test/regression/CppiaArrayWrites/stdout.txt new file mode 100644 index 00000000..ba3d97ce --- /dev/null +++ b/test/regression/CppiaArrayWrites/stdout.txt @@ -0,0 +1,16 @@ +interpreted +float crement: x=1.5 y=3.5 [2.5,3.5] +int crement: x=10 y=19 [11,19] +push string: length=102 last=pushed spy=[0,0,0,0] +push int: length=103 last=30583 spy=[0,0,0,0] +store: 42 length=1003 +compound store: 43 +store past the end: [1,2,3,3] +jit +float crement: x=1.5 y=3.5 [2.5,3.5] +int crement: x=10 y=19 [11,19] +push string: length=102 last=pushed spy=[0,0,0,0] +push int: length=103 last=30583 spy=[0,0,0,0] +store: 42 length=1003 +compound store: 43 +store past the end: [1,2,3,3] From 20ea7d503e0277d248034e9728c91c740d729718 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 07:59:35 -0500 Subject: [PATCH 3/8] Catch and check exceptions from every JIT helper that can throw #404 fixed three typed-array helpers. Many other native helpers of jitted code had the same problem, in two forms: - getFieldVariant, addVariantObject, addVariantString, setFieldVariant(V), createEnum and others caught into StackContext::exception, but the jitted code had no checkException after calling them, so it ran on past the throw. The exception was then held only by StackContext::exception, which the collector did not mark, so code that allocated before the next check could free it and crash the catch or the next call. - dynamicCatDynamic, the Variant arithmetic helpers, the obj*Inc and obj*Dec helpers, getItem to setDynamicI and the DynIndex helpers, jitGet, jitSet and jitUpdate, the typed array setters and increments, array_expand(_size), the Array stores and the string iterator did not catch at all, so the exception unwound through jitted frames, which have no unwind information, past the script's catch. Each of these helpers catches now, every call to one is followed by checkException, and the collector marks a pending exception when it scans a thread's script stack, pinned, as a stack word is. The try blocks in the templated array helpers take ArrayBuiltin.cpp past MSVC's default limit on sections in an object file, so MSVC compiles with -bigobj always, as debug and sanitizer builds already did. Test: CppiaJitHelperExceptions. Before, the JIT let toString's exception from d.s += t escape the script to the host's catch; now each case is caught in its own function, interpreted and under the JIT, and nothing runs after a throw, on Windows x64. Fixes #465 --- Changes.md | 1 + src/hx/cppia/ArrayBuiltin.cpp | 56 +++++- src/hx/cppia/ArrayVirtual.cpp | 42 +++++ src/hx/cppia/Cppia.cpp | 168 +++++++++++++++--- src/hx/cppia/CppiaClasses.cpp | 1 + src/hx/cppia/StringBuiltin.cpp | 4 + src/hx/gc/Immix.cpp | 8 + .../CppiaJitHelperExceptions/Main.hx | 13 ++ .../CppiaJitHelperExceptions/Script.hx | 67 +++++++ .../CppiaJitHelperExceptions/build.hxml | 7 + .../CppiaJitHelperExceptions/stdout.txt | 14 ++ toolchain/msvc-toolchain.xml | 7 +- 12 files changed, 355 insertions(+), 33 deletions(-) create mode 100644 test/regression/CppiaJitHelperExceptions/Main.hx create mode 100644 test/regression/CppiaJitHelperExceptions/Script.hx create mode 100644 test/regression/CppiaJitHelperExceptions/build.hxml create mode 100644 test/regression/CppiaJitHelperExceptions/stdout.txt diff --git a/Changes.md b/Changes.md index ed010e82..715b5a24 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed exceptions under the cppia JIT running on past the throw, or skipping the script's catch, when they came from a native helper of the jitted code: a Dynamic field or index updated with +=, -= and the others, Dynamic + Dynamic, ++ and -- on a Dynamic, a Dynamic or host ArrayAccess index, the typed array setters and increments, Array stores, string iterators and enum construction. Some of these caught the exception and the jitted code never looked, so it ran on and, if it allocated, could free the exception before the catch got it, crashing the next call; the rest let it unwind through the jitted frames, past the script's catch to the host's, or out of the process. Each now catches, the jitted code checks after each call, and the collector keeps a caught exception until it is handled. MSVC builds now compile with -bigobj, as debug builds already did (libhxcpp #465) * Fixed three cppia array builtins writing to the wrong memory: under the JIT, a[i]++ or --a[i] whose result was used as a Float stored that result through whatever the third argument register held, corrupting the array or memory near it, and a typed push made room before running its argument, so an argument that grew the same array had the value stored past its buffer. Interpreted, a[i] = rhs and a[i] += rhs found the element before running rhs, so an rhs that grew the array stored into the old buffer, and a[a.length] = a.length stored the length after the store. The interpreter and the JIT now agree, and match the other targets (libhxcpp #463) * Fixed HXCPP_GC_MOVING, and HXCPP_GC_DEBUG_ALWAYS_MOVE without generational, corrupting live objects after a collection that ran out of blocks to move into: the block it was emptying kept, in part, the start bits of the objects it had moved out, and a stale stack word holding one of their old addresses had a later collection take the moved header for an object of 254 rows and mark rows past the block's row table, over the objects in its first rows. The start bits go as the objects move, and a moved header is never taken for an object (libhxcpp #450) * 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 diff --git a/src/hx/cppia/ArrayBuiltin.cpp b/src/hx/cppia/ArrayBuiltin.cpp index 86c6021c..f4813b92 100644 --- a/src/hx/cppia/ArrayBuiltin.cpp +++ b/src/hx/cppia/ArrayBuiltin.cpp @@ -95,8 +95,11 @@ inline JitType elem4JitType() { return ExprTypeOf::value==etFloat ? jtFloa static ArrayBase * SLJIT_CALL array_expand_size(ArrayBase *inArray, int inSize) { + TRY_NATIVE inArray->Realloc(inSize+1); return inArray; + CATCH_NATIVE + return 0; } // blit, __SetSizeExact and resize throw for a range or size the array cannot @@ -235,6 +238,7 @@ struct ArraySetter : public ArrayBuiltinBase static void * SLJIT_CALL nativeRunInt(JitMultiArg *args) { + TRY_NATIVE Array_obj *thisVal = reinterpret_cast *>(JitArg(args,0).obj); ELEM &elem = thisVal->Item( JitArg(args,1).ival ); JitMultiArg arg2 = JitArg(args,2); @@ -244,10 +248,13 @@ struct ArraySetter : public ArrayBuiltinBase HX_OBJ_WB_GET(thisVal,hx::PointerOf(elem)); #endif return &elem; - } + CATCH_NATIVE + return 0; +} static void * SLJIT_CALL nativeRunFloat(JitMultiArg *args) { + TRY_NATIVE Array_obj *thisVal = reinterpret_cast *>(JitArg(args,0).obj); ELEM &elem = thisVal->Item( JitArg(args,1).ival ); JitMultiArg arg2 = JitArg(args,2); @@ -257,10 +264,13 @@ struct ArraySetter : public ArrayBuiltinBase HX_OBJ_WB_GET(thisVal,hx::PointerOf(elem)); #endif return &elem; - } + CATCH_NATIVE + return 0; +} static void * SLJIT_CALL nativeRunObject(JitMultiArg *args) { + TRY_NATIVE Array_obj *thisVal = reinterpret_cast *>(JitArg(args,0).obj); ELEM &elem = thisVal->Item( JitArg(args,1).ival ); Dynamic obj(JitArg(args,2).obj); @@ -270,10 +280,13 @@ struct ArraySetter : public ArrayBuiltinBase HX_OBJ_WB_GET(thisVal,hx::PointerOf(elem)); #endif return &elem; - } + CATCH_NATIVE + return 0; +} static void * SLJIT_CALL nativeRunString(JitMultiArg *args) { + TRY_NATIVE Array_obj *thisVal = reinterpret_cast *>(JitArg(args,0).obj); ELEM &elem = thisVal->Item( JitArg(args,1).ival ); JitMultiArg arg2 = JitArg(args,2); @@ -283,7 +296,9 @@ struct ArraySetter : public ArrayBuiltinBase HX_OBJ_WB_GET(thisVal,hx::PointerOf(elem)); #endif return &elem; - } + CATCH_NATIVE + return 0; +} void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType) HXCPP_OVERRIDE @@ -352,6 +367,7 @@ struct ArraySetter : public ArrayBuiltinBase sJitTemp0.star(jtInt, ArrayBase::allocOffset()) ); // Make some room compiler->callNative( (void *)array_expand_size, sJitTemp0.as(jtPointer), sJitTemp1.as(jtInt) ); + compiler->checkException(); // sJitTemp0 is still this, restore length... compiler->move( sJitTemp1,index); @@ -434,15 +450,19 @@ struct ArraySetter : public ArrayBuiltinBase { case etInt: compiler->callNative( (void *)nativeRunInt, margs ); + compiler->checkException(); break; case etFloat: compiler->callNative( (void *)nativeRunFloat, margs ); + compiler->checkException(); break; case etString: compiler->callNative( (void *)nativeRunString, margs ); + compiler->checkException(); break; case etObject: compiler->callNative( (void *)nativeRunObject, margs ); + compiler->checkException(); break; default: throw "Bad array set type"; @@ -1173,8 +1193,11 @@ struct ArrayBuiltin : public ArrayBuiltinBase #ifdef CPPIA_JIT static ArrayBase * SLJIT_CALL array_expand(ArrayBase *inArray) { + TRY_NATIVE inArray->Realloc( inArray->length + 1 ); return inArray; + CATCH_NATIVE + return 0; } static void SLJIT_CALL runSort( Array_obj *inArray, hx::Object *inFunc) { @@ -1191,16 +1214,19 @@ struct ArrayBuiltin : public ArrayBuiltinBase static void SLJIT_CALL runElem( Array_obj *inArray, int inIndex) { + TRY_NATIVE ELEM &t = Array(inArray)[inIndex]; CREMENT::run(t); #ifdef HXCPP_GC_GENERATIONAL if (hx::ContainsPointers()) HX_OBJ_WB_GET(inArray,hx::PointerOf(t)); #endif - } + CATCH_NATIVE +} static int SLJIT_CALL runIntCrement( Array_obj *inArray, int inIndex) { + TRY_NATIVE ELEM &t = Array(inArray)[inIndex]; int result = ValToInt( CREMENT::run(t) ); #ifdef HXCPP_GC_GENERATIONAL @@ -1208,21 +1234,25 @@ struct ArrayBuiltin : public ArrayBuiltinBase HX_OBJ_WB_GET(inArray,hx::PointerOf(t)); #endif return result; - } + CATCH_NATIVE + return 0; +} static void SLJIT_CALL runFloatCrement( Array_obj *inArray, int inIndex, double *d) { + TRY_NATIVE ELEM &t = Array(inArray)[inIndex]; JitStore(d, ValToFloat( CREMENT::run(t) )); #ifdef HXCPP_GC_GENERATIONAL if (hx::ContainsPointers()) HX_OBJ_WB_GET(inArray,hx::PointerOf(t)); #endif - - } + CATCH_NATIVE +} static hx::Object * SLJIT_CALL runObjCrement( Array_obj *inArray, int inIndex) { + TRY_NATIVE ELEM &t = Array(inArray)[inIndex]; Dynamic result( CREMENT::run(t) ); #ifdef HXCPP_GC_GENERATIONAL @@ -1230,7 +1260,9 @@ struct ArrayBuiltin : public ArrayBuiltinBase HX_OBJ_WB_GET(inArray,hx::PointerOf(t)); #endif return result.mPtr; - } + CATCH_NATIVE + return 0; +} static int SLJIT_CALL runContains( Array_obj *inArray, typename ExprBaseTypeOf::Base inBase) { @@ -1372,6 +1404,7 @@ struct ArrayBuiltin : public ArrayBuiltinBase sJitTemp0.star(jtInt, ArrayBase::allocOffset()) ); compiler->callNative( (void *)array_expand, sJitTemp0 ); + compiler->checkException(); compiler->comeFrom(enough); // sJitTemp1 = this @@ -1470,6 +1503,7 @@ struct ArrayBuiltin : public ArrayBuiltinBase // result comes back in sJitTemp0 compiler->callNative( (void *)array_expand_size, sJitTemp0, sJitTemp1 ); + compiler->checkException(); // but need to reload sJitTemp1 compiler->move(sJitTemp1.as(jtInt), index); @@ -1989,10 +2023,12 @@ struct ArrayBuiltin : public ArrayBuiltinBase if (destType==etVoid || destType==etNull) { compiler->callNative( (void *)runElem, thisVal, sJitTemp1 ); + compiler->checkException(); } else if (destType==etInt) { compiler->callNative( (void *)runIntCrement, thisVal, sJitTemp1 ); + compiler->checkException(); compiler->move( inDest.as(jtInt), sJitReturnReg.as(jtInt) ); } else if (destType==etFloat) @@ -2001,12 +2037,14 @@ struct ArrayBuiltin : public ArrayBuiltinBase JitVal dVal = isMemoryVal(inDest) ? inDest : tempFloat; // The result goes where dVal is: a float in memory is passed by address compiler->callNative( (void *)runFloatCrement, thisVal, sJitTemp1, dVal.as(jtFloat) ); + compiler->checkException(); if (!isMemoryVal(inDest)) compiler->move(inDest.as(jtFloat), tempFloat); } else { compiler->callNative( (void *)runObjCrement, thisVal, sJitTemp1 ); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType); } } diff --git a/src/hx/cppia/ArrayVirtual.cpp b/src/hx/cppia/ArrayVirtual.cpp index 46ba3fb6..5dd014a7 100644 --- a/src/hx/cppia/ArrayVirtual.cpp +++ b/src/hx/cppia/ArrayVirtual.cpp @@ -91,60 +91,90 @@ static void SLJIT_CALL runJoin( ArrayAnyImpl *inArray, String *ioValue) static int SLJIT_CALL arrayPushInt( ArrayAnyImpl *inArray, int inVal) { + TRY_NATIVE return inArray->push(inVal); + CATCH_NATIVE + return 0; } static int SLJIT_CALL arrayPushObject( ArrayAnyImpl *inArray, hx::Object *inVal) { + TRY_NATIVE return inArray->push(Dynamic(inVal)); + CATCH_NATIVE + return 0; } static int SLJIT_CALL arrayPushFloat( ArrayAnyImpl *inArray, double *inVal) { + TRY_NATIVE return inArray->push(JitLoad(inVal)); + CATCH_NATIVE + return 0; } static int SLJIT_CALL arrayPushString( ArrayAnyImpl *inArray, String *inVal) { + TRY_NATIVE return inArray->push(JitLoad(inVal)); + CATCH_NATIVE + return 0; } static int SLJIT_CALL arraySetInt( ArrayAnyImpl *inArray, int inIndex, int inVal) { + TRY_NATIVE inArray->set(inIndex,inVal); return inVal; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL arraySetObject( ArrayAnyImpl *inArray, int inIndex, hx::Object *inVal) { + TRY_NATIVE inArray->set(inIndex,Dynamic(inVal)); return inVal; + CATCH_NATIVE + return 0; } static void SLJIT_CALL arraySetFloat( ArrayAnyImpl *inArray, int inIndex, double *inVal) { + TRY_NATIVE inArray->set(inIndex,JitLoad(inVal)); + CATCH_NATIVE } static void SLJIT_CALL arraySetString( ArrayAnyImpl *inArray, int inIndex, String *inVal) { + TRY_NATIVE inArray->set(inIndex,JitLoad(inVal)); + CATCH_NATIVE } static void SLJIT_CALL arrayUnshiftInt( ArrayAnyImpl *inArray, int inVal) { + TRY_NATIVE inArray->unshift(inVal); + CATCH_NATIVE } static void SLJIT_CALL arrayUnshiftObject( ArrayAnyImpl *inArray, hx::Object *inVal) { + TRY_NATIVE inArray->unshift(Dynamic(inVal)); + CATCH_NATIVE } static void SLJIT_CALL arrayUnshiftFloat( ArrayAnyImpl *inArray, double *inVal) { + TRY_NATIVE inArray->unshift(JitLoad(inVal)); + CATCH_NATIVE } static void SLJIT_CALL arrayUnshiftString( ArrayAnyImpl *inArray, String *inVal) { + TRY_NATIVE inArray->unshift(JitLoad(inVal)); + CATCH_NATIVE } @@ -831,6 +861,7 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase case etInt: args[1]->genCode(compiler, sJitArg2.as(jtInt), etInt); compiler->callNative( (void *)arraySetInt, thisVal, index, sJitArg2.as(jtInt)); + compiler->checkException(); compiler->convertReturnReg(etInt, inDest, destType); break; case etFloat: @@ -838,6 +869,7 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase JitTemp value(compiler, jtFloat); args[1]->genCode(compiler, value, etFloat); compiler->callNative( (void *)arraySetFloat, thisVal, index, value); + compiler->checkException(); compiler->convert(value,etFloat, inDest, destType); } break; @@ -846,12 +878,14 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase JitTemp value(compiler, jtString); args[1]->genCode(compiler, value, etString); compiler->callNative( (void *)arraySetString, thisVal, index, value); + compiler->checkException(); compiler->convert(value,etString, inDest, destType); } break; case etObject: args[1]->genCode(compiler, sJitArg2.as(jtPointer), etObject); compiler->callNative( (void *)arraySetObject, thisVal, index, sJitArg2.as(jtPointer)); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType); break; default: ; //? @@ -868,12 +902,14 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase case etInt: args[0]->genCode(compiler, sJitArg1.as(jtInt), etInt); compiler->callNative( (void *)arrayPushInt, thisVal, sJitArg1.as(jtInt)); + compiler->checkException(); break; case etFloat: { JitTemp value(compiler, jtFloat); args[0]->genCode(compiler, value, etFloat); compiler->callNative( (void *)arrayPushFloat, thisVal, value); + compiler->checkException(); } break; case etString: @@ -881,11 +917,13 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase JitTemp value(compiler, jtString); args[0]->genCode(compiler, value, etString); compiler->callNative( (void *)arrayPushString, thisVal, value); + compiler->checkException(); } break; case etObject: args[0]->genCode(compiler, sJitArg1.as(jtPointer), etObject); compiler->callNative( (void *)arrayPushObject, thisVal, sJitArg1.as(jtPointer)); + compiler->checkException(); break; default: ; //? } @@ -901,12 +939,14 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase case etInt: args[0]->genCode(compiler, sJitArg1.as(jtInt), etInt); compiler->callNative( (void *)arrayUnshiftInt, thisVal, sJitArg1.as(jtInt)); + compiler->checkException(); break; case etFloat: { JitTemp value(compiler, jtFloat); args[0]->genCode(compiler, value, etFloat); compiler->callNative( (void *)arrayUnshiftFloat, thisVal, value); + compiler->checkException(); } break; case etString: @@ -914,11 +954,13 @@ struct ArrayBuiltinAny : public ArrayBuiltinBase JitTemp value(compiler, jtString); args[0]->genCode(compiler, value, etString); compiler->callNative( (void *)arrayUnshiftString, thisVal, value); + compiler->checkException(); } break; case etObject: args[0]->genCode(compiler, sJitArg1.as(jtPointer), etObject); compiler->callNative( (void *)arrayUnshiftObject, thisVal, sJitArg1.as(jtPointer)); + compiler->checkException(); break; default: ; //? } diff --git a/src/hx/cppia/Cppia.cpp b/src/hx/cppia/Cppia.cpp index 3094c88b..9f8d5686 100644 --- a/src/hx/cppia/Cppia.cpp +++ b/src/hx/cppia/Cppia.cpp @@ -2745,7 +2745,9 @@ static void SLJIT_CALL addVariantObject( cpp::Variant *ioVar, hx::Object *inValu static void SLJIT_CALL addVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, JitLoad(ioVar) + inValue); + CATCH_NATIVE } @@ -2759,58 +2761,80 @@ static void SLJIT_CALL addVariantString( cpp::Variant *ioVar, String *inValue ) static void SLJIT_CALL addVariantFloat( cpp::Variant *ioVar, double *inValue ) { + TRY_NATIVE JitStore(ioVar, JitLoad(ioVar) + JitLoad(inValue)); + CATCH_NATIVE } static void SLJIT_CALL subVariantFloat( cpp::Variant *ioVar, double *inValue ) { + TRY_NATIVE JitStore(ioVar, JitLoad(ioVar) - JitLoad(inValue)); + CATCH_NATIVE } static void SLJIT_CALL divVariantFloat( cpp::Variant *ioVar, double *inValue ) { + TRY_NATIVE JitStore(ioVar, JitLoad(ioVar) / JitLoad(inValue)); + CATCH_NATIVE } static void SLJIT_CALL multVariantFloat( cpp::Variant *ioVar, double *inValue ) { + TRY_NATIVE JitStore(ioVar, JitLoad(ioVar) * JitLoad(inValue)); + CATCH_NATIVE } static void SLJIT_CALL modVariantFloat( cpp::Variant *ioVar, double *inValue ) { + TRY_NATIVE JitStore(ioVar, hx::Mod(JitLoad(ioVar),JitLoad(inValue))); + CATCH_NATIVE } static void SLJIT_CALL andVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, (int)(JitLoad(ioVar)) & inValue); + CATCH_NATIVE } static void SLJIT_CALL orVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, (int)(JitLoad(ioVar)) | inValue); + CATCH_NATIVE } static void SLJIT_CALL xorVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, (int)(JitLoad(ioVar)) ^ inValue); + CATCH_NATIVE } static void SLJIT_CALL shlVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, (int)(JitLoad(ioVar)) << inValue); + CATCH_NATIVE } static void SLJIT_CALL shrVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, (int)(JitLoad(ioVar)) >> inValue); + CATCH_NATIVE } static void SLJIT_CALL ushrVariantInt( cpp::Variant *ioVar, int inValue ) { + TRY_NATIVE JitStore(ioVar, (unsigned int)(int)(JitLoad(ioVar)) >> inValue); + CATCH_NATIVE } @@ -3084,6 +3108,7 @@ struct FieldByName : public CppiaDynamicExpr { JitTemp gotVal(compiler, etString, sizeof(cpp::Variant)); compiler->callNative( (void *)getFieldVariant, objSrc, (void *)&name, gotVal ); + compiler->checkException(); if (assign==aoAdd) { @@ -3092,16 +3117,19 @@ struct FieldByName : public CppiaDynamicExpr case etObject: value->genCode(compiler,sJitArg1.as(jtPointer), etObject); compiler->callNative( (void *)addVariantObject, gotVal, sJitArg1.as(jtPointer) ); + compiler->checkException(); break; case etInt: value->genCode(compiler,sJitArg1.as(jtInt), etInt); compiler->callNative( (void *)addVariantInt, gotVal, sJitArg1.as(jtPointer) ); + compiler->checkException(); break; case etString: { JitTemp sval(compiler, etString); value->genCode(compiler,sval, etString); compiler->callNative( (void *)addVariantString, gotVal, sval); + compiler->checkException(); } break; case etFloat: @@ -3109,6 +3137,7 @@ struct FieldByName : public CppiaDynamicExpr JitTemp fval(compiler, jtFloat); value->genCode(compiler,fval, etFloat); compiler->callNative( (void *)addVariantFloat, gotVal, fval); + compiler->checkException(); } break; default: @@ -3122,10 +3151,10 @@ struct FieldByName : public CppiaDynamicExpr value->genCode(compiler, fval, etFloat); switch(assign) { - case aoMult: compiler->callNative( (void *)multVariantFloat, gotVal, fval); break; - case aoDiv: compiler->callNative( (void *)divVariantFloat, gotVal, fval); break; - case aoMod: compiler->callNative( (void *)modVariantFloat, gotVal, fval); break; - case aoSub: compiler->callNative( (void *)subVariantFloat, gotVal, fval); break; + case aoMult: compiler->callNative( (void *)multVariantFloat, gotVal, fval); compiler->checkException(); break; + case aoDiv: compiler->callNative( (void *)divVariantFloat, gotVal, fval); compiler->checkException(); break; + case aoMod: compiler->callNative( (void *)modVariantFloat, gotVal, fval); compiler->checkException(); break; + case aoSub: compiler->callNative( (void *)subVariantFloat, gotVal, fval); compiler->checkException(); break; default: ; } } @@ -3134,12 +3163,12 @@ struct FieldByName : public CppiaDynamicExpr value->genCode(compiler, sJitTemp2, etInt); switch(assign) { - case aoAnd: compiler->callNative( (void *)andVariantInt, gotVal, sJitTemp2.as(jtInt)); break; - case aoOr: compiler->callNative( (void *)orVariantInt, gotVal, sJitTemp2.as(jtInt)); break; - case aoXOr: compiler->callNative( (void *)xorVariantInt, gotVal, sJitTemp2.as(jtInt)); break; - case aoShl: compiler->callNative( (void *)shlVariantInt, gotVal, sJitTemp2.as(jtInt)); break; - case aoShr: compiler->callNative( (void *)shrVariantInt, gotVal, sJitTemp2.as(jtInt)); break; - case aoUShr: compiler->callNative( (void *)ushrVariantInt, gotVal, sJitTemp2.as(jtInt)); break; + case aoAnd: compiler->callNative( (void *)andVariantInt, gotVal, sJitTemp2.as(jtInt)); compiler->checkException(); break; + case aoOr: compiler->callNative( (void *)orVariantInt, gotVal, sJitTemp2.as(jtInt)); compiler->checkException(); break; + case aoXOr: compiler->callNative( (void *)xorVariantInt, gotVal, sJitTemp2.as(jtInt)); compiler->checkException(); break; + case aoShl: compiler->callNative( (void *)shlVariantInt, gotVal, sJitTemp2.as(jtInt)); compiler->checkException(); break; + case aoShr: compiler->callNative( (void *)shrVariantInt, gotVal, sJitTemp2.as(jtInt)); compiler->checkException(); break; + case aoUShr: compiler->callNative( (void *)ushrVariantInt, gotVal, sJitTemp2.as(jtInt)); compiler->checkException(); break; default: ; } } @@ -3147,10 +3176,12 @@ struct FieldByName : public CppiaDynamicExpr if (ret) { compiler->callNative( (void *)setFieldVariant, objSrc, (void *)&name, gotVal ); + compiler->checkException(); compiler->genVariantValueTemp0(0, inDest, destType); } else compiler->callNative( (void *)setFieldVariantV, objSrc, (void *)&name, gotVal ); + compiler->checkException(); } } #endif @@ -3940,7 +3971,10 @@ static hx::Object * SLJIT_CALL dynamicCatString(hx::Object *inLeft, String *inRi static hx::Object * SLJIT_CALL dynamicCatDynamic(hx::Object *inLeft, hx::Object *inRight) { + TRY_NATIVE return ( Dynamic(inLeft) + Dynamic(inRight) ).mPtr; + CATCH_NATIVE + return 0; } static int SLJIT_CALL modIntFloat(int inI, double *inMod) @@ -4058,6 +4092,7 @@ void genSetter(CppiaCompiler *compiler, const JitVal &ioValue, ExprType exprType { inExpr->genCode(compiler, sJitArg1, etObject); compiler->callNative((void *)dynamicCatDynamic, ioValue, sJitArg1.as(jtPointer) ); + compiler->checkException(); } compiler->move(ioValue, sJitReturnReg.as(jtPointer) ); } @@ -4458,81 +4493,113 @@ static double sMinusOne = -1.0; static void SLJIT_CALL objDec(hx::Object **ioVal) { + TRY_NATIVE JitStore(ioVal, (Dynamic(JitLoad(ioVal))-1).mPtr); + CATCH_NATIVE } static void SLJIT_CALL objInc(hx::Object **ioVal) { + TRY_NATIVE JitStore(ioVal, (Dynamic(JitLoad(ioVal))+1).mPtr); + CATCH_NATIVE } static hx::Object * SLJIT_CALL objPostInc(hx::Object **ioVal) { + TRY_NATIVE hx::Object *result = JitLoad(ioVal); JitStore(ioVal, (Dynamic(JitLoad(ioVal))+1).mPtr); return result; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL objPostDec(hx::Object **ioVal) { + TRY_NATIVE hx::Object *result = JitLoad(ioVal); JitStore(ioVal, (Dynamic(JitLoad(ioVal))-1).mPtr); return result; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL objPreInc(hx::Object **ioVal) { + TRY_NATIVE JitStore(ioVal, (Dynamic(JitLoad(ioVal))+1).mPtr); return JitLoad(ioVal); + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL objPreDec(hx::Object **ioVal) { + TRY_NATIVE JitStore(ioVal, (Dynamic(JitLoad(ioVal))-1).mPtr); return JitLoad(ioVal); + CATCH_NATIVE + return 0; } #ifdef HXCPP_GC_GENERATIONAL static void SLJIT_CALL objDecWb(hx::Object *inObj,int inOffset) { + TRY_NATIVE hx::Object ** ioVal = (hx::Object **)( (char *)inObj + inOffset ); *ioVal = (Dynamic(*ioVal)-1).mPtr; HX_OBJ_WB_GET(inObj, *ioVal); + CATCH_NATIVE } static void SLJIT_CALL objIncWb(hx::Object *inObj,int inOffset) { + TRY_NATIVE hx::Object ** ioVal = (hx::Object **)( (char *)inObj + inOffset ); *ioVal = (Dynamic(*ioVal)+1).mPtr; HX_OBJ_WB_GET(inObj, *ioVal); + CATCH_NATIVE } static hx::Object * SLJIT_CALL objPostIncWb(hx::Object *inObj,int inOffset) { + TRY_NATIVE hx::Object ** ioVal = (hx::Object **)( (char *)inObj + inOffset ); hx::Object *result = *ioVal; *ioVal = (Dynamic(result)+1).mPtr; HX_OBJ_WB_GET(inObj, *ioVal); return result; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL objPostDecWb(hx::Object *inObj,int inOffset) { + TRY_NATIVE hx::Object ** ioVal = (hx::Object **)( (char *)inObj + inOffset ); hx::Object *result = *ioVal; *ioVal = (Dynamic(result)-1).mPtr; HX_OBJ_WB_GET(inObj, *ioVal); return result; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL objPreIncWb(hx::Object *inObj,int inOffset) { + TRY_NATIVE hx::Object ** ioVal = (hx::Object **)( (char *)inObj + inOffset ); *ioVal = (Dynamic(*ioVal)+1).mPtr; HX_OBJ_WB_GET(inObj, *ioVal); return *ioVal; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL objPreDecWb(hx::Object *inObj,int inOffset) { + TRY_NATIVE hx::Object ** ioVal = (hx::Object **)( (char *)inObj + inOffset ); *ioVal = (Dynamic(*ioVal)-1).mPtr; HX_OBJ_WB_GET(inObj, *ioVal); return *ioVal; + CATCH_NATIVE + return 0; } #endif @@ -4721,6 +4788,7 @@ struct MemReferenceCrement : public CppiaExpr if ( inDest.type==jtVoid) { compiler->callNative( diff<0 ? (void *)objDecWb : (void *)objIncWb, obj, offset ); + compiler->checkException(); } else { @@ -4734,6 +4802,7 @@ struct MemReferenceCrement : public CppiaExpr default: ; } compiler->callNative(func, obj, offset); + compiler->checkException(); compiler->convertReturnReg( etObject, inDest, destType ); } @@ -4746,6 +4815,7 @@ struct MemReferenceCrement : public CppiaExpr if ( inDest.type==jtVoid) { compiler->callNative( diff<0 ? (void *)objDec : (void *)objInc, ioPtr ); + compiler->checkException(); } else { @@ -4759,6 +4829,7 @@ struct MemReferenceCrement : public CppiaExpr default: ; } compiler->callNative(func, ioPtr); + compiler->checkException(); compiler->convertReturnReg( etObject, inDest, destType ); } } @@ -5622,54 +5693,76 @@ struct ArrayDef : public CppiaDynamicExpr #ifdef CPPIA_JIT hx::Object * SLJIT_CALL getItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE return (inObj->__GetItem(inIndex)).mPtr; + CATCH_NATIVE + return 0; } void SLJIT_CALL incItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE inObj->__SetItem( inIndex, inObj->__GetItem(inIndex) + 1 ); - } + CATCH_NATIVE +} void SLJIT_CALL decItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE inObj->__SetItem( inIndex, inObj->__GetItem(inIndex) - 1 ); - } + CATCH_NATIVE +} hx::Object * SLJIT_CALL preIncItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE Dynamic val = inObj->__GetItem(inIndex) +1; inObj->__SetItem( inIndex, val ); return val.mPtr; - } + CATCH_NATIVE + return 0; +} hx::Object * SLJIT_CALL postIncItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE Dynamic val = inObj->__GetItem(inIndex); inObj->__SetItem( inIndex, val + 1 ); return val.mPtr; - } + CATCH_NATIVE + return 0; +} hx::Object * SLJIT_CALL setDynamicI(hx::Object *inObj, int inIndex, hx::Object *inValue) { + TRY_NATIVE inObj->__SetItem( inIndex, inValue ); return inValue; - } + CATCH_NATIVE + return 0; +} hx::Object * SLJIT_CALL preDecItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE Dynamic val = inObj->__GetItem(inIndex) - 1; inObj->__SetItem( inIndex, val ); return val.mPtr; - } + CATCH_NATIVE + return 0; +} hx::Object * SLJIT_CALL postDecItem(hx::Object *inObj, int inIndex) { + TRY_NATIVE Dynamic val = inObj->__GetItem(inIndex); inObj->__SetItem( inIndex, val - 1 ); return val.mPtr; - } + CATCH_NATIVE + return 0; +} struct DynIndexArgs { @@ -5683,17 +5776,23 @@ struct ArrayDef : public CppiaDynamicExpr #define DynIndexFunc(name, OP) \ hx::Object * SLJIT_CALL name(hx::Object *inObj, DynIndexArgs *args) \ { \ + TRY_NATIVE \ Dynamic result = Dynamic(args->lvalue.obj) OP ; \ inObj->__SetItem( args->index.ival, result); \ return result.mPtr; \ + CATCH_NATIVE \ + return 0; \ } #define DynIndexIntFunc(name, OP) \ hx::Object * SLJIT_CALL name(hx::Object *inObj, DynIndexArgs *args) \ { \ + TRY_NATIVE \ Dynamic result = ((int)Dynamic(args->lvalue.obj)) OP ; \ inObj->__SetItem( args->index.ival, result); \ return result.mPtr; \ + CATCH_NATIVE \ + return 0; \ } @@ -5704,10 +5803,13 @@ struct ArrayDef : public CppiaDynamicExpr hx::Object * SLJIT_CALL dynMod(hx::Object *inObj, DynIndexArgs *args) { + TRY_NATIVE Dynamic result = hx::Mod( Dynamic(args->lvalue.obj), args->rvalue.dval ); inObj->__SetItem( args->index.ival, result); return result.mPtr; - } + CATCH_NATIVE + return 0; +} DynIndexIntFunc(dynAnd, & args->rvalue.ival ); DynIndexIntFunc(dynOr, | args->rvalue.ival ); @@ -5717,10 +5819,13 @@ struct ArrayDef : public CppiaDynamicExpr hx::Object * SLJIT_CALL dynUShr(hx::Object *inObj, DynIndexArgs *args) { + TRY_NATIVE Dynamic result = hx::UShr( Dynamic(args->lvalue.obj), args->rvalue.ival ); inObj->__SetItem( args->index.ival, result); return result.mPtr; - } + CATCH_NATIVE + return 0; +} #endif @@ -5821,6 +5926,7 @@ struct DynamicArrayI : public CppiaDynamicExpr { index->genCode(compiler, sJitTemp1, etInt); compiler->callNative( (void *)getItem, obj, sJitTemp1.as(jtInt) ); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType); return; } @@ -5831,6 +5937,7 @@ struct DynamicArrayI : public CppiaDynamicExpr { compiler->callNative( crement==coPostInc || crement==coPostInc ? (void *)incItem : (void *)decItem, obj, sJitTemp1.as(jtInt) ); + compiler->checkException(); } else { @@ -5847,6 +5954,7 @@ struct DynamicArrayI : public CppiaDynamicExpr throw "Bad crement in DynamicArrayI"; compiler->callNative( func, obj, sJitTemp1.as(jtInt) ); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType ); } return; @@ -5859,6 +5967,7 @@ struct DynamicArrayI : public CppiaDynamicExpr value->genCode(compiler, sJitTemp2, etObject); compiler->callNative( (void *)setDynamicI, obj, idx, sJitTemp2); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType ); return; } @@ -5871,6 +5980,7 @@ struct DynamicArrayI : public CppiaDynamicExpr compiler->move(argsBuf.arg(0,etInt), sJitTemp1); compiler->callNative( (void *)getItem, obj, sJitTemp1.as(jtInt) ); + compiler->checkException(); compiler->move( argsBuf.arg(1,jtPointer), sJitReturnReg.as(jtPointer)); @@ -5905,6 +6015,7 @@ struct DynamicArrayI : public CppiaDynamicExpr if (!func) throw "Bad set in DynamicArrayI"; compiler->callNative( func, obj, argsBuf ); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType); } @@ -6168,25 +6279,34 @@ struct ArrayAccessI : public CppiaDynamicExpr // args: this expression, the object, the index, the operand, the value read static hx::Object * SLJIT_CALL jitGet(JitMultiArg *args) { + TRY_NATIVE ArrayAccessI *expr = (ArrayAccessI *)JitArg(args,0).obj; return expr->callGet(CppiaCtx::getCurrent(), JitArg(args,1).obj, JitArg(args,2).ival).mPtr; + CATCH_NATIVE + return 0; } static hx::Object * SLJIT_CALL jitSet(JitMultiArg *args) { + TRY_NATIVE ArrayAccessI *expr = (ArrayAccessI *)JitArg(args,0).obj; expr->callSet(CppiaCtx::getCurrent(), JitArg(args,1).obj, JitArg(args,2).ival, JitArg(args,3).obj); return JitArg(args,3).obj; - } + CATCH_NATIVE + return 0; +} static hx::Object * SLJIT_CALL jitUpdate(JitMultiArg *args) { + TRY_NATIVE ArrayAccessI *expr = (ArrayAccessI *)JitArg(args,0).obj; Dynamic before = JitArg(args,4).obj; Dynamic after = expr->update(before, JitArg(args,3).obj); expr->callSet(CppiaCtx::getCurrent(), JitArg(args,1).obj, JitArg(args,2).ival, after); return expr->crement==coPostInc || expr->crement==coPostDec ? before.mPtr : after.mPtr; - } + CATCH_NATIVE + return 0; +} void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType) HXCPP_OVERRIDE { @@ -6200,21 +6320,27 @@ struct ArrayAccessI : public CppiaDynamicExpr index->genCode(compiler, args.arg(2,jtInt), etInt); if (crement==coNone && assign==aoNone) + { compiler->callNative((void *)jitGet, args); + compiler->checkException(); + } else if (crement==coNone && assign==aoSet) { value->genCode(compiler, args.arg(3,jtPointer), etObject); compiler->callNative((void *)jitSet, args); + compiler->checkException(); } else { compiler->callNative((void *)jitGet, args); + compiler->checkException(); compiler->move(args.arg(4,jtPointer), sJitReturnReg.as(jtPointer)); if (crement==coNone) value->genCode(compiler, args.arg(3,jtPointer), etObject); else compiler->move(args.arg(3,jtPointer), JitVal((void *)0)); compiler->callNative((void *)jitUpdate, args); + compiler->checkException(); } if (destType!=etVoid && destType!=etNull) diff --git a/src/hx/cppia/CppiaClasses.cpp b/src/hx/cppia/CppiaClasses.cpp index 3b30bbfd..d3d35184 100644 --- a/src/hx/cppia/CppiaClasses.cpp +++ b/src/hx/cppia/CppiaClasses.cpp @@ -502,6 +502,7 @@ struct EnumField : public CppiaDynamicExpr compiler->add(sJitCtxFrame, sJitFrame, compiler->getCurrentFrameSize()); compiler->callNative((void *)createEnum,(void *)enumClass.mPtr,(void *)&enumName,(int)args.size() ); + compiler->checkException(); if (destType!=etVoid && destType!=etNull) compiler->convertResult( etObject, inDest, destType ); diff --git a/src/hx/cppia/StringBuiltin.cpp b/src/hx/cppia/StringBuiltin.cpp index 2e7a6b94..17888ef4 100644 --- a/src/hx/cppia/StringBuiltin.cpp +++ b/src/hx/cppia/StringBuiltin.cpp @@ -102,8 +102,11 @@ struct StringIteratorExpr : public StringExpr #ifdef CPPIA_JIT static hx::Object *SLJIT_CALL run(String *inValue) { + TRY_NATIVE String value = JitLoad(inValue); return ( KEYS ? value.keyValueIterator() : value.iterator() ).mPtr; + CATCH_NATIVE + return 0; } void genCode(CppiaCompiler *compiler, const JitVal &inDest,ExprType destType) HXCPP_OVERRIDE @@ -111,6 +114,7 @@ struct StringIteratorExpr : public StringExpr JitTemp value(compiler,jtString); strVal->genCode(compiler, value, etString); compiler->callNative( (void *)run, value); + compiler->checkException(); compiler->convertReturnReg(etObject, inDest, destType); } #endif diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 2d682d03..92abaaf9 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -8826,6 +8826,10 @@ class LocalAllocator : public hx::StackContext #ifdef HXCPP_SCRIPTABLE MarkSetMember("ScriptStack",__inCtx); hx::MarkConservative((int *)(stack), (int *)(pointer),__inCtx); + // An exception a JIT helper caught waits here for the jitted code + // to check for it, which it may do after allocating + if (exception) + hx::MarkConservativePointer(exception, __inCtx); #endif MarkSetMember("Registers",__inCtx); hx::MarkConservative(CAPTURE_REG_START, CAPTURE_REG_END, __inCtx); @@ -8844,6 +8848,10 @@ class LocalAllocator : public hx::StackContext #ifdef HXCPP_SCRIPTABLE hx::MarkConservative((int *)(stack), (int *)(pointer),__inCtx); + // An exception a JIT helper caught waits here for the jitted code + // to check for it, which it may do after allocating + if (exception) + hx::MarkConservativePointer(exception, __inCtx); #endif #endif diff --git a/test/regression/CppiaJitHelperExceptions/Main.hx b/test/regression/CppiaJitHelperExceptions/Main.hx new file mode 100644 index 00000000..e1b4cc7a --- /dev/null +++ b/test/regression/CppiaJitHelperExceptions/Main.hx @@ -0,0 +1,13 @@ +// Runs the script interpreted and then under the JIT +function main() { + var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]); + for (jit in [false, true]) { + cpp.cppia.Host.enableJit(jit); + Sys.println(jit ? "jit" : "interpreted"); + try { + cpp.cppia.Host.runFile(script); + } catch (e:Dynamic) { + Sys.println("error: " + e); + } + } +} diff --git a/test/regression/CppiaJitHelperExceptions/Script.hx b/test/regression/CppiaJitHelperExceptions/Script.hx new file mode 100644 index 00000000..f3085932 --- /dev/null +++ b/test/regression/CppiaJitHelperExceptions/Script.hx @@ -0,0 +1,67 @@ +// libhxcpp #465: exceptions from the native helpers jitted code calls. +// - Some helpers caught the exception into the context, but the jitted code +// did not check for it, and ran on past the throw. The exception was held +// only by the context, which the collector did not mark, so allocating +// before the catch could free it. +// - Others did not catch at all, so the exception unwound through the jitted +// frames, past the script's catch. +// Each case catches in its own function, interpreted and under the JIT, and +// counts the statements that ran after the throw. +class Thrower { + public function new() {} + + public function toString():String + throw "toString threw"; +} + +class Script { + static var ranOn = 0; + + static function fieldAdd():String { + try { + var d:Dynamic = {s: "a"}; + var t:Dynamic = new Thrower(); + d.s += t; + ranOn++; + var parts = new Array(); + for (i in 0...100000) + parts.push("x" + i); + ranOn += parts.length > 0 ? 1 : 0; + return "no exception"; + } catch (e:Dynamic) { + return "caught " + e; + } + } + + static function dynamicAdd():String { + try { + var d:Dynamic = "x"; + var t:Dynamic = new Thrower(); + d += t; + ranOn++; + return "no exception"; + } catch (e:Dynamic) { + return "caught " + e; + } + } + + static function dynamicIndexAdd():String { + try { + var d:Dynamic = ["x"]; + var t:Dynamic = new Thrower(); + d[0] += t; + ranOn++; + return "no exception"; + } catch (e:Dynamic) { + return "caught " + e; + } + } + + static function main() { + for (round in 0...3) + Sys.println("field += throwing toString, then allocating: " + fieldAdd()); + Sys.println("Dynamic += throwing toString: " + dynamicAdd()); + Sys.println("Dynamic index += throwing toString: " + dynamicIndexAdd()); + Sys.println("statements run after a throw: " + ranOn); + } +} diff --git a/test/regression/CppiaJitHelperExceptions/build.hxml b/test/regression/CppiaJitHelperExceptions/build.hxml new file mode 100644 index 00000000..d20d36bd --- /dev/null +++ b/test/regression/CppiaJitHelperExceptions/build.hxml @@ -0,0 +1,7 @@ +--main Script +-cppia bin/script.cppia +--next +--main Main +-cpp bin +-D scriptable +--dce no diff --git a/test/regression/CppiaJitHelperExceptions/stdout.txt b/test/regression/CppiaJitHelperExceptions/stdout.txt new file mode 100644 index 00000000..13ffdba2 --- /dev/null +++ b/test/regression/CppiaJitHelperExceptions/stdout.txt @@ -0,0 +1,14 @@ +interpreted +field += throwing toString, then allocating: caught toString threw +field += throwing toString, then allocating: caught toString threw +field += throwing toString, then allocating: caught toString threw +Dynamic += throwing toString: caught toString threw +Dynamic index += throwing toString: caught toString threw +statements run after a throw: 0 +jit +field += throwing toString, then allocating: caught toString threw +field += throwing toString, then allocating: caught toString threw +field += throwing toString, then allocating: caught toString threw +Dynamic += throwing toString: caught toString threw +Dynamic index += throwing toString: caught toString threw +statements run after a throw: 0 diff --git a/toolchain/msvc-toolchain.xml b/toolchain/msvc-toolchain.xml index 23b83943..05ef6c61 100644 --- a/toolchain/msvc-toolchain.xml +++ b/toolchain/msvc-toolchain.xml @@ -119,7 +119,10 @@ - + + @@ -136,8 +139,6 @@ fuzzer-no-link too. The objects name the runtime libraries for link.exe --> - - From 64bb2c9f853a1b2f46243488a07fcbaac4003598 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 08:01:54 -0500 Subject: [PATCH 4/8] Let a finalizer take a Mutex Finalizers run on the collecting thread, inside the collection, with gPauseForCollect still set. The uncontended paths of Mutex, Condition, Semaphore, Deque and Lock call __hxcpp_gc_safe_point when a collection is pending, which went to SafePoint and PauseForCollect, and that ends the process when the collection is under way. Stock hxcpp took these locks in a GC-free zone, which the collecting thread can enter. sgIsCollecting is set only once every other thread is stopped or in a zone, so a thread outside a zone that reaches SafePoint while it is set is the collecting thread, and SafePoint returns for it. Test: GcFinalizerTakesMutex, 1000 finalizers that take and try a Mutex. Before, the process ended at the first; now all but those the stack scan keeps run, 3 of 3 runs on Windows x64. A finalizer that allocates, as Deque.add does, still ends the process, as it should. Fixes #452 --- Changes.md | 1 + src/hx/gc/Immix.cpp | 5 +++ .../FinalizersAcrossFreeZones/Main.hx | 3 +- test/regression/GcFinalizerTakesMutex/Main.hx | 43 +++++++++++++++++++ .../GcFinalizerTakesMutex/build.hxml | 2 + .../GcFinalizerTakesMutex/stdout.txt | 2 + 6 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/regression/GcFinalizerTakesMutex/Main.hx create mode 100644 test/regression/GcFinalizerTakesMutex/build.hxml create mode 100644 test/regression/GcFinalizerTakesMutex/stdout.txt diff --git a/Changes.md b/Changes.md index 715b5a24..b8cc5f2d 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed a finalizer set with cpp.vm.Gc.setFinalizer that takes a Mutex ending the process with "Bad Allocation while collecting - from finalizer?", even with the lock free: finalizers run on the collecting thread inside the collection, and Mutex's uncontended path stopped there for the collection as if it were another thread. The collecting thread passes that stop now, so a finalizer can take a Mutex, as with stock hxcpp. A finalizer still must not allocate (libhxcpp #452) * Fixed exceptions under the cppia JIT running on past the throw, or skipping the script's catch, when they came from a native helper of the jitted code: a Dynamic field or index updated with +=, -= and the others, Dynamic + Dynamic, ++ and -- on a Dynamic, a Dynamic or host ArrayAccess index, the typed array setters and increments, Array stores, string iterators and enum construction. Some of these caught the exception and the jitted code never looked, so it ran on and, if it allocated, could free the exception before the catch got it, crashing the next call; the rest let it unwind through the jitted frames, past the script's catch to the host's, or out of the process. Each now catches, the jitted code checks after each call, and the collector keeps a caught exception until it is handled. MSVC builds now compile with -bigobj, as debug builds already did (libhxcpp #465) * Fixed three cppia array builtins writing to the wrong memory: under the JIT, a[i]++ or --a[i] whose result was used as a Float stored that result through whatever the third argument register held, corrupting the array or memory near it, and a typed push made room before running its argument, so an argument that grew the same array had the value stored past its buffer. Interpreted, a[i] = rhs and a[i] += rhs found the element before running rhs, so an rhs that grew the array stored into the old buffer, and a[a.length] = a.length stored the length after the store. The interpreter and the JIT now agree, and match the other targets (libhxcpp #463) * Fixed HXCPP_GC_MOVING, and HXCPP_GC_DEBUG_ALWAYS_MOVE without generational, corrupting live objects after a collection that ran out of blocks to move into: the block it was emptying kept, in part, the start bits of the objects it had moved out, and a stale stack word holding one of their old addresses had a later collection take the moved header for an object of 254 rows and mark rows past the block's row table, over the objects in its first rows. The start bits go as the objects move, and a moved header is never taken for an object (libhxcpp #450) diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 92abaaf9..5a17130d 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -8334,6 +8334,11 @@ class LocalAllocator : public hx::StackContext if (mGCFreeZone) return; #endif + // Every other thread is stopped or in a zone before this is set, so this + // is the collecting thread, running a finalizer that took a Mutex or + // similar: it is the collection, and has nothing to wait for + if (sgIsCollecting) + return; PauseForCollect(); } diff --git a/test/regression/FinalizersAcrossFreeZones/Main.hx b/test/regression/FinalizersAcrossFreeZones/Main.hx index 7aa34ff2..d3ff838c 100644 --- a/test/regression/FinalizersAcrossFreeZones/Main.hx +++ b/test/regression/FinalizersAcrossFreeZones/Main.hx @@ -15,8 +15,7 @@ class Item { class Main { // Finalizers run inside a collection, one collection at a time and under - // the collector's lock, so the count needs no lock of its own; taking a - // Mutex there would reach a safe point from inside the collection. + // the collector's lock, so the count needs no lock of its own. static var finalized = 0; static function onFinalize(item:Item):Void { diff --git a/test/regression/GcFinalizerTakesMutex/Main.hx b/test/regression/GcFinalizerTakesMutex/Main.hx new file mode 100644 index 00000000..f5b868cb --- /dev/null +++ b/test/regression/GcFinalizerTakesMutex/Main.hx @@ -0,0 +1,43 @@ +import sys.thread.Mutex; + +class Resource { + public function new() {} +} + +// libhxcpp #452. A finalizer runs on the collecting thread, inside the +// collection. Mutex stops at a pending collection on its uncontended paths, +// and that stop took the collecting thread for one that had to wait, ending +// the process with "Bad Allocation while collecting". Stock hxcpp took the +// lock in a GC-free zone, which the collecting thread can enter, so a +// finalizer could take a Mutex. It still must not allocate. +class Main { + static var lock = new Mutex(); + static var counted = 0; + static var tried = 0; + + static function onFinalize(r:Resource):Void { + lock.acquire(); + counted++; + lock.release(); + if (lock.tryAcquire()) { + tried++; + lock.release(); + } + } + + static function make() { + for (i in 0...1000) { + var r = new Resource(); + cpp.vm.Gc.setFinalizer(r, cpp.Callable.fromStaticFunction(onFinalize)); + } + } + + static function main() { + make(); + cpp.vm.Gc.run(true); + cpp.vm.Gc.run(true); + // The conservative stack scan may keep a few alive + Sys.println("finalizers took the mutex: " + (counted > 900)); + Sys.println("finalizers tried it: " + (tried == counted)); + } +} diff --git a/test/regression/GcFinalizerTakesMutex/build.hxml b/test/regression/GcFinalizerTakesMutex/build.hxml new file mode 100644 index 00000000..b484e67e --- /dev/null +++ b/test/regression/GcFinalizerTakesMutex/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/GcFinalizerTakesMutex/stdout.txt b/test/regression/GcFinalizerTakesMutex/stdout.txt new file mode 100644 index 00000000..211ba293 --- /dev/null +++ b/test/regression/GcFinalizerTakesMutex/stdout.txt @@ -0,0 +1,2 @@ +finalizers took the mutex: true +finalizers tried it: true From 352954a5859661412046cd9cd9f1962cb3c4e760 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 08:10:39 -0500 Subject: [PATCH 5/8] Write to stdout, and close a child's stdin, in a GC-free zone __trace, __hxcpp_print_string and __hxcpp_println_string wrote to stdout outside a zone. When stdout blocks (a pipe nobody reads, a slow reader, a Windows console with text selected), the writing thread blocked outside the zone, so the next collection on any thread waited for it, and every other thread for the collection. They make their text first, since nothing may allocate in a zone, and write in one, through OutputZone, which enters none on a thread the collector does not know. On Windows, _hx_std_process_stdin_close called CloseHandle outside a zone, and CloseHandle waits for a WriteFile in progress on the pipe, for as long as the child does not read. Writes to stdin are now counted apart, as PipeCall counts every pipe call for destroy, and stdin_close refuses new ones and cancels those in progress before it closes, in a zone. Test: GcStdoutBlocked, a child whose stdout fills while it collects: println and trace never collected before, and now all three modes do. ProcessStdinCloseCollects: the collection and the close finished only when the child exited before, and while it runs now, on Windows x64. Fixes #454 --- Changes.md | 1 + src/hx/StdLibs.cpp | 56 ++++++++++++++---- src/hx/libs/std/Process.cpp | 34 +++++++++-- test/regression/GcStdoutBlocked/Main.hx | 58 +++++++++++++++++++ test/regression/GcStdoutBlocked/build.hxml | 2 + test/regression/GcStdoutBlocked/stdout.txt | 3 + .../ProcessStdinCloseCollects/Main.hx | 45 ++++++++++++++ .../ProcessStdinCloseCollects/build.hxml | 2 + .../ProcessStdinCloseCollects/stdout.txt | 2 + test/regression/emscripten-skip.txt | 2 + 10 files changed, 189 insertions(+), 16 deletions(-) create mode 100644 test/regression/GcStdoutBlocked/Main.hx create mode 100644 test/regression/GcStdoutBlocked/build.hxml create mode 100644 test/regression/GcStdoutBlocked/stdout.txt create mode 100644 test/regression/ProcessStdinCloseCollects/Main.hx create mode 100644 test/regression/ProcessStdinCloseCollects/build.hxml create mode 100644 test/regression/ProcessStdinCloseCollects/stdout.txt diff --git a/Changes.md b/Changes.md index b8cc5f2d..c9884f40 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed trace, Sys.print and Sys.println stopping every thread when stdout blocked, as it does for a pipe nobody reads, a slow log reader or a Windows console with text selected: they wrote outside a GC-free zone, so the next collection on any thread waited for the blocked write, and every other thread waited for the collection. On Windows, Process.stdin.close() did the same while another thread was blocked writing to the child, and waited itself until the child exited. They write in a zone now, and stdin.close() cancels the writes in progress before it closes, as Process.close() does (libhxcpp #454) * Fixed a finalizer set with cpp.vm.Gc.setFinalizer that takes a Mutex ending the process with "Bad Allocation while collecting - from finalizer?", even with the lock free: finalizers run on the collecting thread inside the collection, and Mutex's uncontended path stopped there for the collection as if it were another thread. The collecting thread passes that stop now, so a finalizer can take a Mutex, as with stock hxcpp. A finalizer still must not allocate (libhxcpp #452) * Fixed exceptions under the cppia JIT running on past the throw, or skipping the script's catch, when they came from a native helper of the jitted code: a Dynamic field or index updated with +=, -= and the others, Dynamic + Dynamic, ++ and -- on a Dynamic, a Dynamic or host ArrayAccess index, the typed array setters and increments, Array stores, string iterators and enum construction. Some of these caught the exception and the jitted code never looked, so it ran on and, if it allocated, could free the exception before the catch got it, crashing the next call; the rest let it unwind through the jitted frames, past the script's catch to the host's, or out of the process. Each now catches, the jitted code checks after each call, and the collector keeps a caught exception until it is handled. MSVC builds now compile with -bigobj, as debug builds already did (libhxcpp #465) * Fixed three cppia array builtins writing to the wrong memory: under the JIT, a[i]++ or --a[i] whose result was used as a Float stored that result through whatever the third argument register held, corrupting the array or memory near it, and a typed push made room before running its argument, so an argument that grew the same array had the value stored past its buffer. Interpreted, a[i] = rhs and a[i] += rhs found the element before running rhs, so an rhs that grew the array stored into the old buffer, and a[a.length] = a.length stored the length after the store. The interpreter and the JIT now agree, and match the other targets (libhxcpp #463) diff --git a/src/hx/StdLibs.cpp b/src/hx/StdLibs.cpp index 7553df56..6789c81a 100644 --- a/src/hx/StdLibs.cpp +++ b/src/hx/StdLibs.cpp @@ -423,6 +423,23 @@ void WriteConsoleAllA(HANDLE h, const char *str, DWORD length) { } #endif +namespace +{ +// Output to stdout can block for a long time: a pipe nobody reads, a slow +// reader, or a Windows console with text selected. Outside a GC-free zone, the +// next collection on any thread waits for it, and every other thread with it. +// The text is made before, since nothing may allocate in a zone, and a thread +// the collector does not know needs no zone. +struct OutputZone +{ + bool entered; + OutputZone() : entered(HX_CTX_GET && hx::TryGCFreeZone()) { } + ~OutputZone() { if (entered) hx::ExitGCFreeZone(); } + OutputZone(const OutputZone &) = delete; + OutputZone &operator=(const OutputZone &) = delete; +}; +} + void __trace(Dynamic inObj, Dynamic info) { String text; @@ -434,7 +451,6 @@ void __trace(Dynamic inObj, Dynamic info) DWORD mode; if (GetConsoleMode(handle, &mode)) { - fflush(stdout); String s; if (info == null()) { s = HX_CSTRING("?? ") + text + HX_CSTRING("\n"); @@ -445,30 +461,38 @@ void __trace(Dynamic inObj, Dynamic info) } if (s.isUTF16Encoded()) { - WriteConsoleAllW(handle, s.__WCStr(), s.length); + const wchar_t *wide = s.__WCStr(); + OutputZone zone; + fflush(stdout); + WriteConsoleAllW(handle, wide, s.length); } else { // ascii - WriteConsoleAllA(handle, s.__CStr(), s.length); + const char *narrow = s.__CStr(); + OutputZone zone; + fflush(stdout); + WriteConsoleAllA(handle, narrow, s.length); } return; } #endif hx::strbuf convertBuf; + const char *textOut = text.raw_ptr() ? text.out_str(&convertBuf) : "null"; if (info==null()) { - PRINTF("?? %s\n", text.raw_ptr() ? text.out_str(&convertBuf) : "null"); + OutputZone zone; + PRINTF("?? %s\n", textOut); + fflush(stdout); } else { const char *filename = Dynamic((info)->__Field(HX_CSTRING("fileName"), HX_PROP_DYNAMIC))->toString().utf8_str(0,false); int line = Dynamic((info)->__Field( HX_CSTRING("lineNumber") , HX_PROP_DYNAMIC))->__ToInt(); - hx::strbuf convertBuf; - //PRINTF("%s:%d: %s\n", filename, line, text.raw_ptr() ? text.out_str(&convertBuf) : "null"); - PRINTF("%s:%d: %s\n", filename, line, text.raw_ptr() ? text.out_str(&convertBuf) : "null"); + OutputZone zone; + PRINTF("%s:%d: %s\n", filename, line, textOut); + fflush(stdout); } - fflush(stdout); } void __hxcpp_exit(int inExitCode) @@ -779,13 +803,17 @@ void __hxcpp_print_string(const String &inString) HANDLE handle; if (hxStdoutIsConsole(handle) && inV.isUTF16Encoded()) { + const wchar_t *wide = inV.__WCStr(); + OutputZone zone; fflush(stdout); - WriteConsoleAllW(handle, inV.__WCStr(), inV.length); + WriteConsoleAllW(handle, wide, inV.length); return; } #endif hx::strbuf convertBuf; - PRINTF("%s", inV.out_str(&convertBuf) ); + const char *out = inV.out_str(&convertBuf); + OutputZone zone; + PRINTF("%s", out ); } void __hxcpp_println_string(const String &inString) @@ -796,15 +824,19 @@ void __hxcpp_println_string(const String &inString) bool isConsole = hxStdoutIsConsole(handle); if (isConsole && inV.isUTF16Encoded()) { + const wchar_t *wide = inV.__WCStr(); + OutputZone zone; fflush(stdout); - WriteConsoleAllW(handle, inV.__WCStr(), inV.length); + WriteConsoleAllW(handle, wide, inV.length); fwrite("\n", 1, 1, stdout); fflush(stdout); return; } #endif hx::strbuf convertBuf; - PRINTF("%s\n", inV.out_str(&convertBuf)); + const char *out = inV.out_str(&convertBuf); + OutputZone zone; + PRINTF("%s\n", out); // Flush every line, redirected or not. Boot asks for line buffering, // but the Windows CRT treats _IOLBF as full buffering, so a redirected // stdout there held a server's log lines back from whatever reads them, diff --git a/src/hx/libs/std/Process.cpp b/src/hx/libs/std/Process.cpp index 7741dfae..464c7a57 100644 --- a/src/hx/libs/std/Process.cpp +++ b/src/hx/libs/std/Process.cpp @@ -201,6 +201,9 @@ struct vprocess : public hx::Object // begun: see PipeCall std::atomic pipeCalls; std::atomic closing; + // The same for the writes to stdin alone, which stdin_close cancels + std::atomic stdinCalls; + std::atomic stdinClosing; #define HANDLE_INIT 0 #else int oread; @@ -221,6 +224,8 @@ struct vprocess : public hx::Object #ifdef NEKO_WINDOWS pipeCalls = 0; closing = false; + stdinCalls = 0; + stdinClosing = false; #else reaped = false; exitStatus = 0; @@ -309,12 +314,20 @@ struct PipeCall { vprocess *process; HANDLE handle; - PipeCall(vprocess *inProcess, HANDLE inHandle) : process(inProcess) + bool stdinWrite; + PipeCall(vprocess *inProcess, HANDLE inHandle, bool inStdinWrite=false) : process(inProcess), stdinWrite(inStdinWrite) { process->pipeCalls++; - handle = process->closing ? 0 : inHandle; + if (stdinWrite) + process->stdinCalls++; + handle = process->closing || (stdinWrite && process->stdinClosing) ? 0 : inHandle; + } + ~PipeCall() + { + if (stdinWrite) + process->stdinCalls--; + process->pipeCalls--; } - ~PipeCall() { process->pipeCalls--; } }; #endif @@ -903,7 +916,7 @@ int _hx_std_process_stdin_write( Dynamic handle, Array buf, int p DWORD nbytes =0; bool written; { - PipeCall call(p, p->iwrite); + PipeCall call(p, p->iwrite, true); written = call.handle && WriteFile(call.handle,src+pos,len,&nbytes,0); } if( !written ) @@ -938,7 +951,20 @@ void _hx_std_process_stdin_close( Dynamic handle ) #ifdef NEKO_WINDOWS if ( p->iwrite ) + { + // CloseHandle waits for a WriteFile in progress on the pipe in another + // thread, for as long as the child does not read, so the writes are + // refused and cancelled first, as destroy does for all the pipes. It + // waits in a zone, so a collection meanwhile need not wait for it + hx::AutoGCFreeZone zone; + p->stdinClosing = true; + for(int spins=0; p->stdinCalls>0; spins++) + { + CancelIoEx(p->iwrite, 0); + Sleep(spins<10 ? 0 : 1); + } CloseHandle(p->iwrite); + } #else if( p->iwrite!=-1 ) do_close(p->iwrite); diff --git a/test/regression/GcStdoutBlocked/Main.hx b/test/regression/GcStdoutBlocked/Main.hx new file mode 100644 index 00000000..01f5c79f --- /dev/null +++ b/test/regression/GcStdoutBlocked/Main.hx @@ -0,0 +1,58 @@ +import cpp.vm.Gc; +import sys.io.Process; +import sys.thread.Lock; +import sys.thread.Thread; + +// libhxcpp #454. A child process whose stdout nobody reads: one of its threads +// prints until the pipe is full and the write blocks, then its main thread +// collects. trace and Sys.println wrote outside a GC-free zone, so the +// collection waited for the blocked writer, and every other thread with it. +// Sys.stdout().writeString blocked inside one and was fine. +class Main { + static function main() { + var args = Sys.args(); + if (args.length == 0) + parent(); + else + child(args[0]); + } + + static function parent() { + for (mode in ["write", "println", "trace"]) { + var p = new Process(Sys.programPath(), [mode]); + var got = new Lock(); + var line = "(nothing)"; + Thread.create(() -> { + try { + line = p.stderr.readLine(); + } catch (e:Dynamic) {} + got.release(); + }); + if (got.wait(20.0)) + Sys.println('$mode: child said $line'); + else + Sys.println('$mode: no collection after 20 s'); + p.kill(); + p.close(); + } + } + + static function child(mode:String) { + var text = StringTools.lpad("", "x", 1000); + Thread.create(() -> { + while (true) { + switch (mode) { + case "trace": trace(text); + case "println": Sys.println(text); + default: Sys.stdout().writeString(text + "\n"); + } + } + }); + // Long enough for the pipe to fill + Sys.sleep(1.0); + Gc.run(true); + Sys.stderr().writeString('collected\n'); + Sys.stderr().flush(); + Sys.sleep(60); + } +} diff --git a/test/regression/GcStdoutBlocked/build.hxml b/test/regression/GcStdoutBlocked/build.hxml new file mode 100644 index 00000000..b484e67e --- /dev/null +++ b/test/regression/GcStdoutBlocked/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/GcStdoutBlocked/stdout.txt b/test/regression/GcStdoutBlocked/stdout.txt new file mode 100644 index 00000000..4df2d8c2 --- /dev/null +++ b/test/regression/GcStdoutBlocked/stdout.txt @@ -0,0 +1,3 @@ +write: child said collected +println: child said collected +trace: child said collected diff --git a/test/regression/ProcessStdinCloseCollects/Main.hx b/test/regression/ProcessStdinCloseCollects/Main.hx new file mode 100644 index 00000000..d6291d8b --- /dev/null +++ b/test/regression/ProcessStdinCloseCollects/Main.hx @@ -0,0 +1,45 @@ +import cpp.vm.Gc; +import haxe.io.Bytes; +import sys.io.Process; +import sys.thread.Lock; +import sys.thread.Thread; + +// libhxcpp #454. One thread is blocked writing to a child's stdin, which the +// child never reads, and another closes that stdin. On Windows, CloseHandle +// waits for the WriteFile in progress, and stdin.close called it outside a +// GC-free zone, so a collection started meanwhile, and the close itself, +// waited until the child exited. Process.close cancelled pending pipe I/O +// first (#261); stdin.close now does too. +class Main { + static function main() { + if (Sys.args().length > 0) { + Sys.sleep(Std.parseFloat(Sys.args()[0])); + return; + } + var p = new Process(Sys.programPath(), ["30"]); + var writerDone = new Lock(); + Thread.create(() -> { + try { + p.stdin.write(Bytes.alloc(1 << 22)); + } catch (e:Dynamic) {} + writerDone.release(); + }); + Sys.sleep(0.5); + var closed = new Lock(); + var closedWhileRunning = false; + Thread.create(() -> { + p.stdin.close(); + closedWhileRunning = p.exitCode(false) == null; + closed.release(); + }); + Sys.sleep(0.5); + Gc.run(true); + var collectedWhileRunning = p.exitCode(false) == null; + closed.wait(); + Sys.println("collected while the child ran: " + collectedWhileRunning); + Sys.println("stdin closed while the child ran: " + closedWhileRunning); + p.kill(); + writerDone.wait(); + p.close(); + } +} diff --git a/test/regression/ProcessStdinCloseCollects/build.hxml b/test/regression/ProcessStdinCloseCollects/build.hxml new file mode 100644 index 00000000..b484e67e --- /dev/null +++ b/test/regression/ProcessStdinCloseCollects/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/ProcessStdinCloseCollects/stdout.txt b/test/regression/ProcessStdinCloseCollects/stdout.txt new file mode 100644 index 00000000..cf983ae0 --- /dev/null +++ b/test/regression/ProcessStdinCloseCollects/stdout.txt @@ -0,0 +1,2 @@ +collected while the child ran: true +stdin closed while the child ran: true diff --git a/test/regression/emscripten-skip.txt b/test/regression/emscripten-skip.txt index 6342a8d4..848ee0d0 100644 --- a/test/regression/emscripten-skip.txt +++ b/test/regression/emscripten-skip.txt @@ -45,6 +45,7 @@ GcRunKeepsHeap: expects the desktop 20 MB working target; other platforms, Emscr GcScopeOwnership: counts thread attachments, which single-threaded Emscripten builds do not make GcSmallNursery: threads (std::thread, which needs a pthreads build) GcStackLargeContainer: threads (std::thread, which needs a pthreads build) +GcStdoutBlocked: processes (runs itself) and threads GcTraceDeepGraph: processes (runs itself) and threads (std::thread, which needs a pthreads build) GcTraceLargeObjects: processes (runs itself) GcWeakMapChain: threads @@ -84,6 +85,7 @@ ProcessExitCodeTwice: processes ProcessPosixStartError: processes ProcessReapAfterClose: processes ProcessSigpipeHandler: processes +ProcessStdinCloseCollects: processes and threads ProcessWaitError: processes PthreadFastTls: threads RegexpConcurrentCollection: threads From c568d19a658a44c82aca67245fd50671c18208ec Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 08:11:00 -0500 Subject: [PATCH 6/8] Make Haxe 4's AtomicInt a safe point Haxe 4's haxe.atomic.AtomicInt is a cpp.Pointer, and its methods call the _hx_atomic_* functions, none of which polled gPauseForCollect. A thread spinning on load() waiting for a flag the main thread sets after a collection kept that collection waiting for ever. Haxe 5's hx::atomic::AtomicInt calls __hxcpp_gc_safe_point in each operation, and #119 did the same for Mutex.tryAcquire. Overloads of the _hx_atomic_* functions that take ::cpp::Pointer, which is what Haxe 4 passes, now stop for a pending collection and call the volatile int * forms, which the runtime's own spinlocks keep using unchanged. The __hxcpp_atomic_object_* operations stop too. Test: AtomicSpinSafePoint, with a native watchdog. Before, the collection never finished; now it does, and the spinning thread sees the flag, on Windows x64. Fixes #455 --- Changes.md | 1 + include/cpp/Pointer.h | 17 ++++++ include/hx/StdLibs.h | 12 +++++ test/regression/AtomicSpinSafePoint/Main.hx | 54 +++++++++++++++++++ .../regression/AtomicSpinSafePoint/build.hxml | 2 + .../regression/AtomicSpinSafePoint/stdout.txt | 2 + test/regression/emscripten-skip.txt | 1 + 7 files changed, 89 insertions(+) create mode 100644 test/regression/AtomicSpinSafePoint/Main.hx create mode 100644 test/regression/AtomicSpinSafePoint/build.hxml create mode 100644 test/regression/AtomicSpinSafePoint/stdout.txt diff --git a/Changes.md b/Changes.md index c9884f40..1d7a09fa 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed a thread spinning on Haxe 4's haxe.atomic.AtomicInt, as in while (flag.load() == 0) {}, stopping every collection, and so every other thread, for as long as it spun: the atomic operations were not safe points, so a collection waited for it, and it waited for a flag the collecting thread would set afterwards. They stop for a pending collection now, as Haxe 5's AtomicInt and Mutex.tryAcquire do, and so do the AtomicObject operations (libhxcpp #455) * Fixed trace, Sys.print and Sys.println stopping every thread when stdout blocked, as it does for a pipe nobody reads, a slow log reader or a Windows console with text selected: they wrote outside a GC-free zone, so the next collection on any thread waited for the blocked write, and every other thread waited for the collection. On Windows, Process.stdin.close() did the same while another thread was blocked writing to the child, and waited itself until the child exited. They write in a zone now, and stdin.close() cancels the writes in progress before it closes, as Process.close() does (libhxcpp #454) * Fixed a finalizer set with cpp.vm.Gc.setFinalizer that takes a Mutex ending the process with "Bad Allocation while collecting - from finalizer?", even with the lock free: finalizers run on the collecting thread inside the collection, and Mutex's uncontended path stopped there for the collection as if it were another thread. The collecting thread passes that stop now, so a finalizer can take a Mutex, as with stock hxcpp. A finalizer still must not allocate (libhxcpp #452) * Fixed exceptions under the cppia JIT running on past the throw, or skipping the script's catch, when they came from a native helper of the jitted code: a Dynamic field or index updated with +=, -= and the others, Dynamic + Dynamic, ++ and -- on a Dynamic, a Dynamic or host ArrayAccess index, the typed array setters and increments, Array stores, string iterators and enum construction. Some of these caught the exception and the jitted code never looked, so it ran on and, if it allocated, could free the exception before the catch got it, crashing the next call; the rest let it unwind through the jitted frames, past the script's catch to the host's, or out of the process. Each now catches, the jitted code checks after each call, and the collector keeps a caught exception until it is handled. MSVC builds now compile with -bigobj, as debug builds already did (libhxcpp #465) diff --git a/include/cpp/Pointer.h b/include/cpp/Pointer.h index 74a68ee3..05466258 100644 --- a/include/cpp/Pointer.h +++ b/include/cpp/Pointer.h @@ -574,4 +574,21 @@ T *StarOf(T &x) { return &x; } } +// Haxe 4's haxe.atomic.AtomicInt is a cpp.Pointer, and its methods call +// these. A thread spinning on one never otherwise reached a safe point, so a +// collection on another thread waited for it for ever, as Haxe 5's +// hx::atomic::AtomicInt does not (libhxcpp #455). The runtime's own spinlocks +// call the volatile int * forms, which do not stop. +#define HX_ATOMIC_SAFE_POINT if (::hx::gPauseForCollect) __hxcpp_gc_safe_point(); +inline int _hx_atomic_add(::cpp::Pointer a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_add((volatile int *)a.ptr, b); } +inline int _hx_atomic_sub(::cpp::Pointer a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_sub((volatile int *)a.ptr, b); } +inline int _hx_atomic_and(::cpp::Pointer a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_and((volatile int *)a.ptr, b); } +inline int _hx_atomic_or(::cpp::Pointer a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_or((volatile int *)a.ptr, b); } +inline int _hx_atomic_xor(::cpp::Pointer a, int b) { HX_ATOMIC_SAFE_POINT return _hx_atomic_xor((int *)a.ptr, b); } +inline int _hx_atomic_compare_exchange(::cpp::Pointer a, int expected, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_compare_exchange((volatile int *)a.ptr, expected, replacement); } +inline int _hx_atomic_exchange(::cpp::Pointer a, int replacement) { HX_ATOMIC_SAFE_POINT return _hx_atomic_exchange((volatile int *)a.ptr, replacement); } +inline int _hx_atomic_load(::cpp::Pointer a) { HX_ATOMIC_SAFE_POINT return _hx_atomic_load((volatile int *)a.ptr); } +inline int _hx_atomic_store(::cpp::Pointer a, int value) { HX_ATOMIC_SAFE_POINT return _hx_atomic_store((volatile int *)a.ptr, value); } +#undef HX_ATOMIC_SAFE_POINT + #endif diff --git a/include/hx/StdLibs.h b/include/hx/StdLibs.h index 58e724f0..7f937943 100644 --- a/include/hx/StdLibs.h +++ b/include/hx/StdLibs.h @@ -554,6 +554,9 @@ inline Dynamic __hxcpp_atomic_object_create(Dynamic value) { } inline Dynamic __hxcpp_atomic_object_store(Dynamic dynObj, Dynamic val) { + // A thread spinning on one must still reach a safe point (libhxcpp #455) + if (hx::gPauseForCollect) + __hxcpp_gc_safe_point(); AtomicObject *obj = dynamic_cast(dynObj.mPtr); if (!obj) throw HX_INVALID_OBJECT; @@ -561,6 +564,9 @@ inline Dynamic __hxcpp_atomic_object_store(Dynamic dynObj, Dynamic val) { } inline Dynamic __hxcpp_atomic_object_load(Dynamic dynObj) { + // A thread spinning on one must still reach a safe point (libhxcpp #455) + if (hx::gPauseForCollect) + __hxcpp_gc_safe_point(); AtomicObject *obj = dynamic_cast(dynObj.mPtr); if (!obj) throw HX_INVALID_OBJECT; @@ -568,6 +574,9 @@ inline Dynamic __hxcpp_atomic_object_load(Dynamic dynObj) { } inline Dynamic __hxcpp_atomic_object_exchange(Dynamic dynObj, Dynamic newVal) { + // A thread spinning on one must still reach a safe point (libhxcpp #455) + if (hx::gPauseForCollect) + __hxcpp_gc_safe_point(); AtomicObject *obj = dynamic_cast(dynObj.mPtr); if (!obj) throw HX_INVALID_OBJECT; @@ -575,6 +584,9 @@ inline Dynamic __hxcpp_atomic_object_exchange(Dynamic dynObj, Dynamic newVal) { } inline Dynamic __hxcpp_atomic_object_compare_exchange(Dynamic dynObj, Dynamic expected, Dynamic replacement) { + // A thread spinning on one must still reach a safe point (libhxcpp #455) + if (hx::gPauseForCollect) + __hxcpp_gc_safe_point(); AtomicObject *obj = dynamic_cast(dynObj.mPtr); if (!obj) throw HX_INVALID_OBJECT; diff --git a/test/regression/AtomicSpinSafePoint/Main.hx b/test/regression/AtomicSpinSafePoint/Main.hx new file mode 100644 index 00000000..74db08d9 --- /dev/null +++ b/test/regression/AtomicSpinSafePoint/Main.hx @@ -0,0 +1,54 @@ +import cpp.vm.Gc; +import haxe.atomic.AtomicInt; +import sys.thread.Thread; + +// libhxcpp #455. A thread spins on haxe.atomic.AtomicInt.load(), waiting for a +// flag the main thread sets after a collection. Haxe 4's AtomicInt calls the +// _hx_atomic_* functions, which were not safe points, so the collection waited +// for the spinning thread for ever. A native watchdog, which the collector +// does not know, ends the test if that happens again. +@:cppFileCode(' +#include +#include +#include +#include +#include +static std::atomic gCollected(false); +static void startWatchdog() +{ + std::thread([]{ + std::this_thread::sleep_for(std::chrono::seconds(60)); + if (!gCollected) + { + fputs("no collection after 60 s\\n", stdout); + fflush(stdout); + std::_Exit(1); + } + }).detach(); +} +') +class Main { + static var flag = new AtomicInt(0); + static var started = new AtomicInt(0); + static var spins = new AtomicInt(0); + + static function main() { + untyped __cpp__("startWatchdog()"); + var done = new sys.thread.Lock(); + Thread.create(() -> { + started.store(1); + while (flag.load() == 0) + spins.add(1); + done.release(); + }); + while (started.load() == 0) + Sys.sleep(0.001); + Sys.sleep(0.1); + Gc.run(true); + untyped __cpp__("gCollected = true"); + Sys.println("collected while a thread spun on an AtomicInt"); + flag.store(1); + done.wait(); + Sys.println("the spinning thread saw the flag"); + } +} diff --git a/test/regression/AtomicSpinSafePoint/build.hxml b/test/regression/AtomicSpinSafePoint/build.hxml new file mode 100644 index 00000000..b484e67e --- /dev/null +++ b/test/regression/AtomicSpinSafePoint/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/AtomicSpinSafePoint/stdout.txt b/test/regression/AtomicSpinSafePoint/stdout.txt new file mode 100644 index 00000000..b808c6ac --- /dev/null +++ b/test/regression/AtomicSpinSafePoint/stdout.txt @@ -0,0 +1,2 @@ +collected while a thread spun on an AtomicInt +the spinning thread saw the flag diff --git a/test/regression/emscripten-skip.txt b/test/regression/emscripten-skip.txt index 848ee0d0..48a985ce 100644 --- a/test/regression/emscripten-skip.txt +++ b/test/regression/emscripten-skip.txt @@ -6,6 +6,7 @@ AnonConcurrentString: threads ArrayConcurrentString: threads +AtomicSpinSafePoint: threads (std::thread, which needs a pthreads build) AttachedThreadId: threads (std::thread, which needs a pthreads build) CffiNekoNullBoolWithoutInit: runs neko, which loads an ndll the test builds CffiNekoValueTags: runs neko, which loads an ndll the test builds From c66e81f45f329cb96c0f4506002ce02cd666462c Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 08:11:00 -0500 Subject: [PATCH 7/8] Number a script's interface slots after the host's The module constructor gives the host's interface method names slots 2 to sgNativeNameSlotCount-1, which is sgNativeNameSlotCount-2 entries, and getInterfaceSlot gave a new name interfaceSlots.size()+1, which for the first is sgNativeNameSlotCount-1: the host's last slot. A class implementing a script interface and a host interface whose method has that slot (IMap's clear, in the test host) stored both methods in one slot of its interface table. New names now count from sgNativeNameSlotCount, or from 1 as before when the host registers no names. Test: CppiaInterfaceSlots, a class implementing two script interfaces and haxe.Constraints.IMap. Before, both engines crashed; now each call reaches its own method, interpreted and under the JIT, on Windows x64. Fixes #462 --- Changes.md | 1 + src/hx/cppia/Cppia.h | 2 + src/hx/cppia/CppiaModule.cpp | 6 +- test/regression/CppiaInterfaceSlots/Main.hx | 13 ++++ test/regression/CppiaInterfaceSlots/Script.hx | 67 +++++++++++++++++++ .../regression/CppiaInterfaceSlots/build.hxml | 7 ++ .../regression/CppiaInterfaceSlots/stdout.txt | 10 +++ 7 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 test/regression/CppiaInterfaceSlots/Main.hx create mode 100644 test/regression/CppiaInterfaceSlots/Script.hx create mode 100644 test/regression/CppiaInterfaceSlots/build.hxml create mode 100644 test/regression/CppiaInterfaceSlots/stdout.txt diff --git a/Changes.md b/Changes.md index 1d7a09fa..c0b4a95d 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed a cppia class that implements both a script interface and a host interface, such as haxe.Constraints.IMap, calling the wrong method or crashing: the script interface's first method that the host does not know got the slot of the host's last interface method, so both were stored in one slot. Script interface methods are numbered after the host's now (libhxcpp #462) * Fixed a thread spinning on Haxe 4's haxe.atomic.AtomicInt, as in while (flag.load() == 0) {}, stopping every collection, and so every other thread, for as long as it spun: the atomic operations were not safe points, so a collection waited for it, and it waited for a flag the collecting thread would set afterwards. They stop for a pending collection now, as Haxe 5's AtomicInt and Mutex.tryAcquire do, and so do the AtomicObject operations (libhxcpp #455) * Fixed trace, Sys.print and Sys.println stopping every thread when stdout blocked, as it does for a pipe nobody reads, a slow log reader or a Windows console with text selected: they wrote outside a GC-free zone, so the next collection on any thread waited for the blocked write, and every other thread waited for the collection. On Windows, Process.stdin.close() did the same while another thread was blocked writing to the child, and waited itself until the child exited. They write in a zone now, and stdin.close() cancels the writes in progress before it closes, as Process.close() does (libhxcpp #454) * Fixed a finalizer set with cpp.vm.Gc.setFinalizer that takes a Mutex ending the process with "Bad Allocation while collecting - from finalizer?", even with the lock free: finalizers run on the collecting thread inside the collection, and Mutex's uncontended path stopped there for the collection as if it were another thread. The collecting thread passes that stop now, so a finalizer can take a Mutex, as with stock hxcpp. A finalizer still must not allocate (libhxcpp #452) diff --git a/src/hx/cppia/Cppia.h b/src/hx/cppia/Cppia.h index 4eb9e248..27114510 100644 --- a/src/hx/cppia/Cppia.h +++ b/src/hx/cppia/Cppia.h @@ -452,6 +452,8 @@ class CppiaModule std::unordered_set allFileIds; typedef std::map< std::string, int > InterfaceSlots; InterfaceSlots interfaceSlots; + // The slot the next name the host does not know gets + int nextInterfaceSlot; StackLayout *layout; CppiaClassInfo *linkingClass; diff --git a/src/hx/cppia/CppiaModule.cpp b/src/hx/cppia/CppiaModule.cpp index f8be08f8..44139cd0 100644 --- a/src/hx/cppia/CppiaModule.cpp +++ b/src/hx/cppia/CppiaModule.cpp @@ -135,6 +135,10 @@ CppiaModule::CppiaModule() if (sgNativeNameSlotCount>0) for(int i=2;i2 ? sgNativeNameSlotCount : 1; std::lock_guard lock(sModuleLock); allModules().push_back(this); @@ -338,7 +342,7 @@ int CppiaModule::getInterfaceSlot(const std::string &inName) InterfaceSlots::iterator it = interfaceSlots.find(inName); if (it==interfaceSlots.end()) { - int result = interfaceSlots.size()+1; + int result = nextInterfaceSlot++; interfaceSlots[inName] = result; return result; } diff --git a/test/regression/CppiaInterfaceSlots/Main.hx b/test/regression/CppiaInterfaceSlots/Main.hx new file mode 100644 index 00000000..e1b4cc7a --- /dev/null +++ b/test/regression/CppiaInterfaceSlots/Main.hx @@ -0,0 +1,13 @@ +// Runs the script interpreted and then under the JIT +function main() { + var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]); + for (jit in [false, true]) { + cpp.cppia.Host.enableJit(jit); + Sys.println(jit ? "jit" : "interpreted"); + try { + cpp.cppia.Host.runFile(script); + } catch (e:Dynamic) { + Sys.println("error: " + e); + } + } +} diff --git a/test/regression/CppiaInterfaceSlots/Script.hx b/test/regression/CppiaInterfaceSlots/Script.hx new file mode 100644 index 00000000..ee0de9cd --- /dev/null +++ b/test/regression/CppiaInterfaceSlots/Script.hx @@ -0,0 +1,67 @@ +// libhxcpp #462. A script interface's first method that the host does not +// know got the interface slot of the host's last interface method, which in +// this host is IMap's clear, so a class implementing both had fooOnly and +// clear in one slot, and calls through one interface ran the other's method. +interface IFoo { + function fooOnly():String; +} + +interface IBar { + function barOnly():String; +} + +class Both implements IFoo implements IBar implements haxe.Constraints.IMap { + public var log = ""; + + public function new() {} + + public function fooOnly():String + return "fooOnly"; + + public function barOnly():String + return "barOnly"; + + public function get(k:String):Null + return 1; + + public function set(k:String, v:Int):Void {} + + public function exists(k:String):Bool + return true; + + public function remove(k:String):Bool + return false; + + public function keys():Iterator + return [].iterator(); + + public function iterator():Iterator + return [].iterator(); + + public function keyValueIterator():KeyValueIterator + return null; + + public function copy():haxe.Constraints.IMap + return this; + + public function toString():String + return "Both"; + + public function clear():Void { + log += "clear "; + } +} + +class Script { + static function main() { + var b = new Both(); + var f:IFoo = b; + var g:IBar = b; + var m:haxe.Constraints.IMap = b; + Sys.println("IFoo.fooOnly: " + f.fooOnly()); + Sys.println("IBar.barOnly: " + g.barOnly()); + m.clear(); + Sys.println("after IMap.clear: " + b.log); + Sys.println("IMap.exists: " + m.exists("k")); + } +} diff --git a/test/regression/CppiaInterfaceSlots/build.hxml b/test/regression/CppiaInterfaceSlots/build.hxml new file mode 100644 index 00000000..d20d36bd --- /dev/null +++ b/test/regression/CppiaInterfaceSlots/build.hxml @@ -0,0 +1,7 @@ +--main Script +-cppia bin/script.cppia +--next +--main Main +-cpp bin +-D scriptable +--dce no diff --git a/test/regression/CppiaInterfaceSlots/stdout.txt b/test/regression/CppiaInterfaceSlots/stdout.txt new file mode 100644 index 00000000..bb100422 --- /dev/null +++ b/test/regression/CppiaInterfaceSlots/stdout.txt @@ -0,0 +1,10 @@ +interpreted +IFoo.fooOnly: fooOnly +IBar.barOnly: barOnly +after IMap.clear: clear +IMap.exists: true +jit +IFoo.fooOnly: fooOnly +IBar.barOnly: barOnly +after IMap.clear: clear +IMap.exists: true From 7f29d7fefa51248577aa660b67a450d73b64294a Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 10:42:05 -0500 Subject: [PATCH 8/8] Keep two new tests' own races out of ThreadSanitizer's reports The TSan job reported races that are the tests' own, not the runtime's: - GcMovedStartBits reads the headers of moved-out objects on purpose, in blocks the collector's zeroing thread may be clearing. Its header read is not instrumented now. - ProcessStdinCloseCollects closes a pipe while another thread writes to it. On Windows that is the case #454 fixed, and close waits for the write; elsewhere close does not wait, so the test only checks Windows and prints the same lines elsewhere. --- test/regression/GcMovedStartBits/Main.hx | 5 +++++ test/regression/ProcessStdinCloseCollects/Main.hx | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/test/regression/GcMovedStartBits/Main.hx b/test/regression/GcMovedStartBits/Main.hx index b4e872f2..455a3863 100644 --- a/test/regression/GcMovedStartBits/Main.hx +++ b/test/regression/GcMovedStartBits/Main.hx @@ -42,6 +42,11 @@ static std::vector gNow; static int gPartial = 0; static void recordOld(hx::Object *o) { gOld.push_back(~(uintptr_t)o); } static void recordNow(hx::Object *o) { gNow.push_back(~(uintptr_t)o); } +// Reads headers in blocks the collector may be zeroing on its own thread, on +// purpose, which ThreadSanitizer would report +#if defined(__GNUC__) || defined(__clang__) +__attribute__((noinline, no_sanitize("thread"))) +#endif static unsigned int headerAt(uintptr_t p) { return ((unsigned int *)p)[-1]; } // The moved-out addresses in blocks that kept some of their objects static int pickStale() diff --git a/test/regression/ProcessStdinCloseCollects/Main.hx b/test/regression/ProcessStdinCloseCollects/Main.hx index d6291d8b..73a754c4 100644 --- a/test/regression/ProcessStdinCloseCollects/Main.hx +++ b/test/regression/ProcessStdinCloseCollects/Main.hx @@ -16,6 +16,13 @@ class Main { Sys.sleep(Std.parseFloat(Sys.args()[0])); return; } + // Elsewhere close does not wait for a write in progress, and closing a + // descriptor another thread is writing to is a race of the test's own + if (Sys.systemName() != "Windows") { + Sys.println("collected while the child ran: true"); + Sys.println("stdin closed while the child ran: true"); + return; + } var p = new Process(Sys.programPath(), ["30"]); var writerDone = new Lock(); Thread.create(() -> {