diff --git a/Changes.md b/Changes.md index 81c0023f..54b69a82 100644 --- a/Changes.md +++ b/Changes.md @@ -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 diff --git a/src/hx/cppia/CppiaFunction.cpp b/src/hx/cppia/CppiaFunction.cpp index c3aabf6e..31a2bdc3 100644 --- a/src/hx/cppia/CppiaFunction.cpp +++ b/src/hx/cppia/CppiaFunction.cpp @@ -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 @@ -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() ); } diff --git a/src/hx/cppia/CppiaVars.cpp b/src/hx/cppia/CppiaVars.cpp index 1a8f5643..639ce479 100644 --- a/src/hx/cppia/CppiaVars.cpp +++ b/src/hx/cppia/CppiaVars.cpp @@ -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(slot).raw_ptr()) + CppiaCtx::setSlot(slot, module->strings[ inDefault.ival ]); } } else { - hx::Object *src = *(hx::Object **)(inCxt->frame + defaultStackPos); + hx::Object *src = CppiaCtx::getSlot(inCxt->frame + defaultStackPos); if (src) { @@ -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: diff --git a/test/regression/CppiaDefaultArgFrames/Main.hx b/test/regression/CppiaDefaultArgFrames/Main.hx new file mode 100644 index 00000000..896584b1 --- /dev/null +++ b/test/regression/CppiaDefaultArgFrames/Main.hx @@ -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); + } + } +} diff --git a/test/regression/CppiaDefaultArgFrames/Script.hx b/test/regression/CppiaDefaultArgFrames/Script.hx new file mode 100644 index 00000000..0d0a415f --- /dev/null +++ b/test/regression/CppiaDefaultArgFrames/Script.hx @@ -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)); + } +} diff --git a/test/regression/CppiaDefaultArgFrames/build.hxml b/test/regression/CppiaDefaultArgFrames/build.hxml new file mode 100644 index 00000000..d20d36bd --- /dev/null +++ b/test/regression/CppiaDefaultArgFrames/build.hxml @@ -0,0 +1,7 @@ +--main Script +-cppia bin/script.cppia +--next +--main Main +-cpp bin +-D scriptable +--dce no diff --git a/test/regression/CppiaDefaultArgFrames/stdout.txt b/test/regression/CppiaDefaultArgFrames/stdout.txt new file mode 100644 index 00000000..4ad749d5 --- /dev/null +++ b/test/regression/CppiaDefaultArgFrames/stdout.txt @@ -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