diff --git a/Changes.md b/Changes.md index 54b69a82..18585704 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.3 (unreleased) ------------------------------------------------------------ +* Fixed the collector marking and moving the variables a cppia closure captures in place in the closure: a closure packs its captures at 4-byte offsets, so on 64-bit a String or object captured after an Int, as text in var count = 1; var text = "x"; function() return count + text, sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. Each capture is copied out to be marked or visited now, and written back if the moving collector moved it. Interpreted and jitted closures were both affected * Fixed the cppia interpreter reading an argument's slot in place in the script frame when it fills in the argument's default value, and writing a Float or Dynamic default the same way: a frame keeps 4-byte alignment, so on 64-bit an argument after an Int, as b in f(a:Int, ?b:String = "x"), sits at an odd multiple of 4, which is undefined behaviour, reported by UBSan, and a fault on a CPU that requires alignment. The slots are copied in and out now, as the interpreter's other frame accesses have been since 6.0.0. The JIT, which compiles defaults into the function, was not affected * Fixed Semaphore.tryAcquire(-1) waiting for ever where any other negative timeout returns at once, so a timeout worked out in whole seconds as deadline - now could wait with no limit once the deadline had passed. It was kept for Haxe 5.0.0-preview.1's Lock.wait() (libhxcpp #461), but preview.1's Lock waits through the runtime's own lock, which still takes a negative timeout as no limit, as Haxe 4's does. Only Haxe 5 nightlies from March to November 2026, which libhxcpp does not support, passed -1 to the semaphore; later ones call acquire() for no timeout. Every negative timeout returns at once now, as with stock hxcpp on Linux and macOS (HaxeFoundation/haxe#13114) diff --git a/src/hx/cppia/CppiaVars.cpp b/src/hx/cppia/CppiaVars.cpp index 639ce479..2aa36a15 100644 --- a/src/hx/cppia/CppiaVars.cpp +++ b/src/hx/cppia/CppiaVars.cpp @@ -462,15 +462,25 @@ Dynamic CppiaStackVar::getInFrame(const unsigned char *inFrame) } +// A closure packs its captures at 4-byte offsets, as a frame does, so on 64-bit +// a String or object after an Int is unaligned: each is copied out to be +// marked or visited, and written back if the visit moved it void CppiaStackVar::markClosure(char *inBase, hx::MarkContext *__inCtx) { + char *slot = inBase + capturePos; switch(storeType) { case fsString: - HX_MARK_MEMBER(*(String *)(inBase + capturePos)); + { + String value = CppiaCtx::getSlot(slot); + HX_MARK_MEMBER(value); + } break; case fsObject: - HX_MARK_MEMBER(*(hx::Object **)(inBase + capturePos)); + { + hx::Object *value = CppiaCtx::getSlot(slot); + HX_MARK_MEMBER(value); + } break; default: ; } @@ -478,13 +488,26 @@ void CppiaStackVar::markClosure(char *inBase, hx::MarkContext *__inCtx) void CppiaStackVar::visitClosure(char *inBase, hx::VisitContext *__inCtx) { + char *slot = inBase + capturePos; switch(storeType) { case fsString: - HX_VISIT_MEMBER(*(String *)(inBase + capturePos)); + { + String value = CppiaCtx::getSlot(slot); + const char *before = value.raw_ptr(); + HX_VISIT_MEMBER(value); + if (value.raw_ptr()!=before) + CppiaCtx::setSlot(slot, value); + } break; case fsObject: - HX_VISIT_MEMBER(*(hx::Object **)(inBase + capturePos)); + { + hx::Object *value = CppiaCtx::getSlot(slot); + hx::Object *before = value; + HX_VISIT_MEMBER(value); + if (value!=before) + CppiaCtx::setSlot(slot, value); + } break; default: ; } diff --git a/test/regression/CppiaClosureCaptureAlignment/Main.hx b/test/regression/CppiaClosureCaptureAlignment/Main.hx new file mode 100644 index 00000000..925717c5 --- /dev/null +++ b/test/regression/CppiaClosureCaptureAlignment/Main.hx @@ -0,0 +1,19 @@ +// A cppia closure packs the variables it captures at 4-byte offsets, after +// the 8-byte this on 64-bit, so a String or object captured after an Int sits +// at an odd multiple of 4. The collector marked and visited those captures in +// place: undefined behaviour (UBSan reports it) and a fault on a CPU that +// requires alignment. With HXCPP_GC_DEBUG_ALWAYS_MOVE every collection moves +// objects, so the visit must also write each moved capture back, or the +// closure reads a moved object. Interpreted and jitted script closures are the +// same objects, so both runs reach it. +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); + try { + cpp.cppia.Host.runFile(script); + } catch (e:Dynamic) { + Sys.println("error: " + e); + } + } +} diff --git a/test/regression/CppiaClosureCaptureAlignment/Script.hx b/test/regression/CppiaClosureCaptureAlignment/Script.hx new file mode 100644 index 00000000..5208e23d --- /dev/null +++ b/test/regression/CppiaClosureCaptureAlignment/Script.hx @@ -0,0 +1,33 @@ +class Script { + // The closure captures count first, so text and list follow a 4-byte Int + static function make(n:Int, s:String, values:Array):Void->String { + var count = n; + var text = s + "!"; + var list = values; + return function() return count + " " + text + " " + list.length + " " + list[0]; + } + + static function main() { + var ok = true; + var checked = 0; + for (round in 0...5) { + var all = [for (i in 0...20000) make(i, "text" + i, [i, round])]; + // Keep one closure in fifty, so the blocks holding what they captured + // are mostly garbage and the collector moves them + var kept = [for (i in 0...all.length) if (i % 50 == 0) all[i]]; + all = null; + cpp.vm.Gc.run(true); + // Fill the space the moved objects left + var refill = [for (i in 0...20000) Std.string(i * round)]; + for (k in 0...kept.length) { + var i = k * 50; + if (kept[k]() != '$i text$i! 2 $i') + ok = false; + checked++; + } + refill = null; + } + Sys.println("captures after moving collections: " + ok); + Sys.println("closures checked: " + checked); + } +} diff --git a/test/regression/CppiaClosureCaptureAlignment/build.hxml b/test/regression/CppiaClosureCaptureAlignment/build.hxml new file mode 100644 index 00000000..f5da0c6a --- /dev/null +++ b/test/regression/CppiaClosureCaptureAlignment/build.hxml @@ -0,0 +1,9 @@ +--main Script +-cppia bin/script.cppia +--next +--main Main +-cpp bin +-D scriptable +--dce no +-D HXCPP_GC_MOVING +-D HXCPP_GC_DEBUG_ALWAYS_MOVE diff --git a/test/regression/CppiaClosureCaptureAlignment/stdout.txt b/test/regression/CppiaClosureCaptureAlignment/stdout.txt new file mode 100644 index 00000000..557fe477 --- /dev/null +++ b/test/regression/CppiaClosureCaptureAlignment/stdout.txt @@ -0,0 +1,4 @@ +captures after moving collections: true +closures checked: 2000 +captures after moving collections: true +closures checked: 2000