From 4349d1ee3da730095dbb513811b40b97644e384a Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Fri, 27 Nov 2026 16:05:34 -0500 Subject: [PATCH] Copy a cppia closure's captures out to mark and visit them A cppia closure stores the variables it captures after the object, packed at 4-byte offsets as a frame is, after the 8-byte this. 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. The collector's markClosure and visitClosure read those captures in place: undefined behaviour, and a fault on a CPU that requires alignment. Everything else that touches captures, the constructor, the call and the JIT's createClosure, already copies them with memcpy. A temporary print in both functions showed a String capture at offset 12 and an object capture at offset 28, both at 4 modulo 8 on 64-bit Windows, interpreted and under the JIT. Marking and visiting now copy each capture out with getSlot, and the visit writes it back with setSlot when the moving collector moved it. CppiaClosureCaptureAlignment keeps one closure in fifty of 20000 that capture an Int, a String and an Array, collects with HXCPP_GC_DEBUG_ALWAYS_MOVE so the collector moves what they captured, and checks every kept closure still reads its captures. x86 tolerates the unaligned access, so the test passes before the fix too; UBSan reports it. Without the write-back it crashes. Tests: CppiaClosureCaptureAlignment --- Changes.md | 2 ++ src/hx/cppia/CppiaVars.cpp | 31 ++++++++++++++--- .../CppiaClosureCaptureAlignment/Main.hx | 19 +++++++++++ .../CppiaClosureCaptureAlignment/Script.hx | 33 +++++++++++++++++++ .../CppiaClosureCaptureAlignment/build.hxml | 9 +++++ .../CppiaClosureCaptureAlignment/stdout.txt | 4 +++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 test/regression/CppiaClosureCaptureAlignment/Main.hx create mode 100644 test/regression/CppiaClosureCaptureAlignment/Script.hx create mode 100644 test/regression/CppiaClosureCaptureAlignment/build.hxml create mode 100644 test/regression/CppiaClosureCaptureAlignment/stdout.txt diff --git a/Changes.md b/Changes.md index e52f55d48..c53256422 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,8 @@ 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 + libhxcpp 6.0.2 ------------------------------------------------------------ diff --git a/src/hx/cppia/CppiaVars.cpp b/src/hx/cppia/CppiaVars.cpp index 1a8f56435..e5b569e23 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 000000000..925717c5f --- /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 000000000..5208e23d1 --- /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 000000000..f5da0c6a4 --- /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 000000000..557fe4770 --- /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