Skip to content

Commit f77dd2f

Browse files
Merge remote-tracking branch 'origin/main' into HEAD
2 parents 1162606 + 60b1e03 commit f77dd2f

16 files changed

Lines changed: 182 additions & 76 deletions

File tree

‎Changes.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ libhxcpp 6.0.3 (unreleased)
33
------------------------------------------------------------
44

55
* Fixed the C++ build tool, built with haxe compile-cpp.hxml in tools/hxcpp, not compiling since 6.0.0: ThreadPool.hx declared the class that ends a compile thread's job before one of its imports, which Haxe refuses with "import and using may not appear after a declaration". The neko build tool, hxcpp.n, was unaffected, and run.n uses a C++ build tool only once one is built, so only rebuilding it failed. CI now type-checks it (libhxcpp #471)
6+
* 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
7+
* 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
8+
* 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)
69

710
libhxcpp 6.0.2
811
------------------------------------------------------------

‎src/hx/cppia/CppiaFunction.cpp‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ static int sTypeSize[] = { 0, 0, sizeof(hx::Object *), sizeof(String), sizeof(Fl
1515
String sInvalidArgCount = HX_CSTRING("Invalid arguement count");
1616

1717

18-
#ifdef CPPIA_JIT
19-
void SLJIT_FUNC argToInt(CppiaCtx *ctx) { ctx->pushInt( (* (hx::Object **)(ctx->pointer))->__ToInt() ); }
20-
void SLJIT_FUNC argToDouble(CppiaCtx *ctx) { ctx->pushFloat( (* (hx::Object **)(ctx->pointer))->__ToDouble() ); }
21-
void SLJIT_FUNC argToString(CppiaCtx *ctx) { ctx->pushString( (* (hx::Object **)(ctx->pointer))->__ToString() ); }
22-
#endif
2318

2419

2520

@@ -322,22 +317,6 @@ int ScriptCallable::Hash(int value, const char *inString)
322317
#ifdef CPPIA_JIT
323318

324319

325-
int SLJIT_FUNC objectToInt(hx::Object *obj) { return obj->__ToInt(); }
326-
void SLJIT_FUNC frameToDouble(CppiaCtx *inCtx) { inCtx->returnFloat( inCtx->getObject() ); }
327-
//void objectToInt(CppiaCtx *inCtx) { inCtx->returnInt( inCtx->getObject() ); }
328-
void SLJIT_FUNC objectToDouble(CppiaCtx *inCtx) { inCtx->returnFloat( inCtx->getObject() ); }
329-
void SLJIT_FUNC objectToDoublePointer(CppiaCtx *inCtx)
330-
{
331-
*(double *)inCtx->pointer = (*(hx::Object **)inCtx->pointer)->__ToDouble();
332-
}
333-
void SLJIT_FUNC objectToStringPointer(CppiaCtx *inCtx)
334-
{
335-
*(String *)inCtx->pointer = (*(hx::Object **)inCtx->pointer)->toString();
336-
}
337-
void SLJIT_FUNC objectToString(CppiaCtx *inCtx) { inCtx->returnString( inCtx->getObject() ); }
338-
void SLJIT_FUNC stringToObject(CppiaCtx *inCtx) { inCtx->returnObject( inCtx->getString() ); }
339-
void SLJIT_FUNC intToObject(CppiaCtx *inCtx) { inCtx->returnObject( inCtx->getInt() ); }
340-
void SLJIT_FUNC doubleToObject(CppiaCtx *inCtx) { inCtx->returnObject( inCtx->getFloat() ); }
341320

342321

343322

‎src/hx/cppia/CppiaVars.cpp‎

Lines changed: 39 additions & 11 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
}
@@ -522,18 +545,20 @@ void CppiaStackVar::linkDefault()
522545

523546
void CppiaStackVar::setDefault(CppiaCtx *inCxt, const CppiaConst &inDefault)
524547
{
548+
// The argument's slots are frame slots, which can be unaligned (see
549+
// StackContext::getSlot), so they are copied in and out
525550
if (argType==etString)
526551
{
527552
if (inDefault.type == CppiaConst::cString)
528553
{
529-
String *s = (String *)(inCxt->frame + defaultStackPos);
530-
if (!s->raw_ptr())
531-
*s = module->strings[ inDefault.ival ];
554+
unsigned char *slot = inCxt->frame + defaultStackPos;
555+
if (!CppiaCtx::getSlot<String>(slot).raw_ptr())
556+
CppiaCtx::setSlot(slot, module->strings[ inDefault.ival ]);
532557
}
533558
}
534559
else
535560
{
536-
hx::Object *src = *(hx::Object **)(inCxt->frame + defaultStackPos);
561+
hx::Object *src = CppiaCtx::getSlot<hx::Object *>(inCxt->frame + defaultStackPos);
537562

538563
if (src)
539564
{
@@ -550,19 +575,22 @@ void CppiaStackVar::setDefault(CppiaCtx *inCxt, const CppiaConst &inDefault)
550575
inDefault.type==CppiaConst::cInt ? inDefault.ival :
551576
inDefault.dval;
552577
break;
553-
case fsFloat: *(Float *)ptr =
578+
case fsFloat: SetFloatAligned(ptr,
554579
inDefault.type==CppiaConst::cInt ? inDefault.ival :
555-
inDefault.dval;
580+
inDefault.dval);
556581
break;
557582
case fsObject:
558583
{
559-
Dynamic &d = *(Dynamic *)ptr;
584+
Dynamic d;
560585
if (inDefault.type==CppiaConst::cInt)
561586
d = inDefault.ival;
562587
else if (inDefault.type==CppiaConst::cFloat)
563588
d = inDefault.dval;
564589
else if (inDefault.type==CppiaConst::cString)
565590
d = module->strings[ inDefault.ival ];
591+
else
592+
break;
593+
CppiaCtx::setSlot(ptr, d.mPtr);
566594
}
567595
break;
568596
case fsString:

‎src/hx/thread/CountingSemaphore.apple.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,10 @@ void hx::thread::CountingSemaphore_obj::release()
5454

5555
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
5656
{
57-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
58-
// given no timeout. Any other negative timeout has run out already, as in
59-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
60-
// waited for ever before (libhxcpp #461)
61-
if (timeout.Default(0) == -1)
62-
{
63-
acquire();
64-
return true;
65-
}
66-
57+
// A timeout that is not positive, null and NaN among them, tries once and
58+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
59+
// has once the deadline passes, and -1 is no exception: waiting with no
60+
// limit is acquire()'s job (libhxcpp #461)
6761
hx::AutoGCFreeZone zone;
6862

6963
double seconds = timeout.Default(0);

‎src/hx/thread/CountingSemaphore.posix.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,10 @@ void hx::thread::CountingSemaphore_obj::release()
7575

7676
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
7777
{
78-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
79-
// given no timeout. Any other negative timeout has run out already, as in
80-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
81-
// waited for ever before (libhxcpp #461)
82-
if (timeout.Default(0) == -1)
83-
{
84-
acquire();
85-
return true;
86-
}
87-
78+
// A timeout that is not positive, null and NaN among them, tries once and
79+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
80+
// has once the deadline passes, and -1 is no exception: waiting with no
81+
// limit is acquire()'s job (libhxcpp #461)
8882
if (hx::gPauseForCollect)
8983
__hxcpp_gc_safe_point();
9084
double seconds = timeout.Default(0);

‎src/hx/thread/CountingSemaphore.singlethreaded.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,10 @@ void hx::thread::CountingSemaphore_obj::release()
5454

5555
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
5656
{
57-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
58-
// given no timeout. Any other negative timeout has run out already, as in
59-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
60-
// waited for ever before (libhxcpp #461)
61-
if (timeout.Default(0) == -1)
62-
{
63-
acquire();
64-
return true;
65-
}
66-
57+
// A timeout that is not positive, null and NaN among them, tries once and
58+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
59+
// has once the deadline passes, and -1 is no exception: waiting with no
60+
// limit is acquire()'s job (libhxcpp #461)
6761
if (impl->permits > 0)
6862
{
6963
impl->permits--;

‎src/hx/thread/CountingSemaphore.win32.cpp‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,10 @@ void hx::thread::CountingSemaphore_obj::release()
5959

6060
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
6161
{
62-
// -1 waits with no limit: Haxe 5.0.0-preview.1's Lock.wait() passes it when
63-
// given no timeout. Any other negative timeout has run out already, as in
64-
// tryAcquire(deadline - now), and returns at once like 0, as does NaN; it
65-
// waited for ever before (libhxcpp #461)
66-
if (timeout.Default(0) == -1)
67-
{
68-
acquire();
69-
return true;
70-
}
71-
62+
// A timeout that is not positive, null and NaN among them, tries once and
63+
// returns. A negative one has run out already, as tryAcquire(deadline - now)
64+
// has once the deadline passes, and -1 is no exception: waiting with no
65+
// limit is acquire()'s job (libhxcpp #461)
7266
if (hx::gPauseForCollect)
7367
__hxcpp_gc_safe_point();
7468
// Zero-timeout poll cannot block - no free zone needed. Also try a
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

0 commit comments

Comments
 (0)