Skip to content

Commit 60b1e03

Browse files
Copy a cppia closure's captures out to mark and visit them (#498)
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
1 parent acc5a26 commit 60b1e03

6 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
libhxcpp 6.0.3 (unreleased)
33
------------------------------------------------------------
44

5+
* 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
56
* 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
67
* 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)
78

‎src/hx/cppia/CppiaVars.cpp‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,29 +462,52 @@ Dynamic CppiaStackVar::getInFrame(const unsigned char *inFrame)
462462
}
463463

464464

465+
// A closure packs its captures at 4-byte offsets, as a frame does, so on 64-bit
466+
// a String or object after an Int is unaligned: each is copied out to be
467+
// marked or visited, and written back if the visit moved it
465468
void CppiaStackVar::markClosure(char *inBase, hx::MarkContext *__inCtx)
466469
{
470+
char *slot = inBase + capturePos;
467471
switch(storeType)
468472
{
469473
case fsString:
470-
HX_MARK_MEMBER(*(String *)(inBase + capturePos));
474+
{
475+
String value = CppiaCtx::getSlot<String>(slot);
476+
HX_MARK_MEMBER(value);
477+
}
471478
break;
472479
case fsObject:
473-
HX_MARK_MEMBER(*(hx::Object **)(inBase + capturePos));
480+
{
481+
hx::Object *value = CppiaCtx::getSlot<hx::Object *>(slot);
482+
HX_MARK_MEMBER(value);
483+
}
474484
break;
475485
default: ;
476486
}
477487
}
478488

479489
void CppiaStackVar::visitClosure(char *inBase, hx::VisitContext *__inCtx)
480490
{
491+
char *slot = inBase + capturePos;
481492
switch(storeType)
482493
{
483494
case fsString:
484-
HX_VISIT_MEMBER(*(String *)(inBase + capturePos));
495+
{
496+
String value = CppiaCtx::getSlot<String>(slot);
497+
const char *before = value.raw_ptr();
498+
HX_VISIT_MEMBER(value);
499+
if (value.raw_ptr()!=before)
500+
CppiaCtx::setSlot(slot, value);
501+
}
485502
break;
486503
case fsObject:
487-
HX_VISIT_MEMBER(*(hx::Object **)(inBase + capturePos));
504+
{
505+
hx::Object *value = CppiaCtx::getSlot<hx::Object *>(slot);
506+
hx::Object *before = value;
507+
HX_VISIT_MEMBER(value);
508+
if (value!=before)
509+
CppiaCtx::setSlot(slot, value);
510+
}
488511
break;
489512
default: ;
490513
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// A cppia closure packs the variables it captures at 4-byte offsets, after
2+
// the 8-byte this on 64-bit, so a String or object captured after an Int sits
3+
// at an odd multiple of 4. The collector marked and visited those captures in
4+
// place: undefined behaviour (UBSan reports it) and a fault on a CPU that
5+
// requires alignment. With HXCPP_GC_DEBUG_ALWAYS_MOVE every collection moves
6+
// objects, so the visit must also write each moved capture back, or the
7+
// closure reads a moved object. Interpreted and jitted script closures are the
8+
// same objects, so both runs reach it.
9+
function main() {
10+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
11+
for (jit in [false, true]) {
12+
cpp.cppia.Host.enableJit(jit);
13+
try {
14+
cpp.cppia.Host.runFile(script);
15+
} catch (e:Dynamic) {
16+
Sys.println("error: " + e);
17+
}
18+
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Script {
2+
// The closure captures count first, so text and list follow a 4-byte Int
3+
static function make(n:Int, s:String, values:Array<Int>):Void->String {
4+
var count = n;
5+
var text = s + "!";
6+
var list = values;
7+
return function() return count + " " + text + " " + list.length + " " + list[0];
8+
}
9+
10+
static function main() {
11+
var ok = true;
12+
var checked = 0;
13+
for (round in 0...5) {
14+
var all = [for (i in 0...20000) make(i, "text" + i, [i, round])];
15+
// Keep one closure in fifty, so the blocks holding what they captured
16+
// are mostly garbage and the collector moves them
17+
var kept = [for (i in 0...all.length) if (i % 50 == 0) all[i]];
18+
all = null;
19+
cpp.vm.Gc.run(true);
20+
// Fill the space the moved objects left
21+
var refill = [for (i in 0...20000) Std.string(i * round)];
22+
for (k in 0...kept.length) {
23+
var i = k * 50;
24+
if (kept[k]() != '$i text$i! 2 $i')
25+
ok = false;
26+
checked++;
27+
}
28+
refill = null;
29+
}
30+
Sys.println("captures after moving collections: " + ok);
31+
Sys.println("closures checked: " + checked);
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--main Script
2+
-cppia bin/script.cppia
3+
--next
4+
--main Main
5+
-cpp bin
6+
-D scriptable
7+
--dce no
8+
-D HXCPP_GC_MOVING
9+
-D HXCPP_GC_DEBUG_ALWAYS_MOVE
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
captures after moving collections: true
2+
closures checked: 2000
3+
captures after moving collections: true
4+
closures checked: 2000

0 commit comments

Comments
 (0)