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 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)

libhxcpp 6.0.2
Expand Down
21 changes: 0 additions & 21 deletions src/hx/cppia/CppiaFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ static int sTypeSize[] = { 0, 0, sizeof(hx::Object *), sizeof(String), sizeof(Fl
String sInvalidArgCount = HX_CSTRING("Invalid arguement count");


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



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


int SLJIT_FUNC objectToInt(hx::Object *obj) { return obj->__ToInt(); }
void SLJIT_FUNC frameToDouble(CppiaCtx *inCtx) { inCtx->returnFloat( inCtx->getObject() ); }
//void objectToInt(CppiaCtx *inCtx) { inCtx->returnInt( inCtx->getObject() ); }
void SLJIT_FUNC objectToDouble(CppiaCtx *inCtx) { inCtx->returnFloat( inCtx->getObject() ); }
void SLJIT_FUNC objectToDoublePointer(CppiaCtx *inCtx)
{
*(double *)inCtx->pointer = (*(hx::Object **)inCtx->pointer)->__ToDouble();
}
void SLJIT_FUNC objectToStringPointer(CppiaCtx *inCtx)
{
*(String *)inCtx->pointer = (*(hx::Object **)inCtx->pointer)->toString();
}
void SLJIT_FUNC objectToString(CppiaCtx *inCtx) { inCtx->returnString( inCtx->getObject() ); }
void SLJIT_FUNC stringToObject(CppiaCtx *inCtx) { inCtx->returnObject( inCtx->getString() ); }
void SLJIT_FUNC intToObject(CppiaCtx *inCtx) { inCtx->returnObject( inCtx->getInt() ); }
void SLJIT_FUNC doubleToObject(CppiaCtx *inCtx) { inCtx->returnObject( inCtx->getFloat() ); }



Expand Down
19 changes: 12 additions & 7 deletions src/hx/cppia/CppiaVars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,20 @@ void CppiaStackVar::linkDefault()

void CppiaStackVar::setDefault(CppiaCtx *inCxt, const CppiaConst &inDefault)
{
// The argument's slots are frame slots, which can be unaligned (see
// StackContext::getSlot), so they are copied in and out
if (argType==etString)
{
if (inDefault.type == CppiaConst::cString)
{
String *s = (String *)(inCxt->frame + defaultStackPos);
if (!s->raw_ptr())
*s = module->strings[ inDefault.ival ];
unsigned char *slot = inCxt->frame + defaultStackPos;
if (!CppiaCtx::getSlot<String>(slot).raw_ptr())
CppiaCtx::setSlot(slot, module->strings[ inDefault.ival ]);
}
}
else
{
hx::Object *src = *(hx::Object **)(inCxt->frame + defaultStackPos);
hx::Object *src = CppiaCtx::getSlot<hx::Object *>(inCxt->frame + defaultStackPos);

if (src)
{
Expand All @@ -550,19 +552,22 @@ void CppiaStackVar::setDefault(CppiaCtx *inCxt, const CppiaConst &inDefault)
inDefault.type==CppiaConst::cInt ? inDefault.ival :
inDefault.dval;
break;
case fsFloat: *(Float *)ptr =
case fsFloat: SetFloatAligned(ptr,
inDefault.type==CppiaConst::cInt ? inDefault.ival :
inDefault.dval;
inDefault.dval);
break;
case fsObject:
{
Dynamic &d = *(Dynamic *)ptr;
Dynamic d;
if (inDefault.type==CppiaConst::cInt)
d = inDefault.ival;
else if (inDefault.type==CppiaConst::cFloat)
d = inDefault.dval;
else if (inDefault.type==CppiaConst::cString)
d = module->strings[ inDefault.ival ];
else
break;
CppiaCtx::setSlot(ptr, d.mPtr);
}
break;
case fsString:
Expand Down
18 changes: 18 additions & 0 deletions test/regression/CppiaDefaultArgFrames/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The cppia interpreter fills in an argument's default value in the function's
// frame, which only keeps 4-byte alignment. It read the argument's String or
// object slot in place, and wrote a Float or Dynamic default the same way, so
// on 64-bit, where a default after an Int is at an odd multiple of 4, that was
// undefined behaviour (UBSan reports it) and a fault on a CPU that requires
// alignment. The jit compiles defaults into the function instead, so the jit
// run is here for comparison.
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);
}
}
}
18 changes: 18 additions & 0 deletions test/regression/CppiaDefaultArgFrames/Script.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Script {
// Each default follows an Int, so it sits 4 bytes past an 8-byte boundary
static function str(a:Int, ?b:String = "default") return '$a $b';
static function dyn(a:Int, ?b:Dynamic = 5) return '$a $b';
static function nullFloat(a:Int, ?b:Float = 1.5) return '$a $b';
static function float(a:Int, b:Float = 1.5) return '$a $b';
static function int(a:Int, b:Int = 7) return '$a $b';
static function bool(a:Int, b:Bool = true) return '$a $b';

static function main() {
Sys.println("str: " + str(1) + ", " + str(2, "given"));
Sys.println("dyn: " + dyn(1) + ", " + dyn(2, "given"));
Sys.println("nullFloat: " + nullFloat(1) + ", " + nullFloat(2, 2.25));
Sys.println("float: " + float(1) + ", " + float(2, 2.25));
Sys.println("int: " + int(1) + ", " + int(2, 9));
Sys.println("bool: " + bool(1) + ", " + bool(2, false));
}
}
7 changes: 7 additions & 0 deletions test/regression/CppiaDefaultArgFrames/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--main Script
-cppia bin/script.cppia
--next
--main Main
-cpp bin
-D scriptable
--dce no
12 changes: 12 additions & 0 deletions test/regression/CppiaDefaultArgFrames/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
str: 1 default, 2 given
dyn: 1 5, 2 given
nullFloat: 1 1.5, 2 2.25
float: 1 1.5, 2 2.25
int: 1 7, 2 9
bool: 1 true, 2 false
str: 1 default, 2 given
dyn: 1 5, 2 given
nullFloat: 1 1.5, 2 2.25
float: 1 1.5, 2 2.25
int: 1 7, 2 9
bool: 1 true, 2 false
Loading