Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 27 additions & 4 deletions src/hx/cppia/CppiaVars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,29 +462,52 @@ 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<String>(slot);
HX_MARK_MEMBER(value);
}
break;
case fsObject:
HX_MARK_MEMBER(*(hx::Object **)(inBase + capturePos));
{
hx::Object *value = CppiaCtx::getSlot<hx::Object *>(slot);
HX_MARK_MEMBER(value);
}
break;
default: ;
}
}

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<String>(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<hx::Object *>(slot);
hx::Object *before = value;
HX_VISIT_MEMBER(value);
if (value!=before)
CppiaCtx::setSlot(slot, value);
}
break;
default: ;
}
Expand Down
19 changes: 19 additions & 0 deletions test/regression/CppiaClosureCaptureAlignment/Main.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
33 changes: 33 additions & 0 deletions test/regression/CppiaClosureCaptureAlignment/Script.hx
Original file line number Diff line number Diff line change
@@ -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<Int>):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);
}
}
9 changes: 9 additions & 0 deletions test/regression/CppiaClosureCaptureAlignment/build.hxml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/regression/CppiaClosureCaptureAlignment/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
captures after moving collections: true
closures checked: 2000
captures after moving collections: true
closures checked: 2000
Loading