From be522d2f5b205be1d5e3c9308b7ae6302bac7333 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Fri, 27 Nov 2026 13:33:41 -0500 Subject: [PATCH 1/2] Copy a cppia argument's default slots in and out of the script frame When the interpreter fills in an argument's default value, it read the argument's String or object slot in the frame in place, and wrote a Float or Dynamic default the same way. A frame keeps 4-byte alignment and starts with the 8-byte this, 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: undefined behaviour, and a fault on a CPU that requires alignment. 7a783808 moved the interpreter's other frame accesses to copying slots in and out and missed this one. CppiaDefaultArgFrames calls functions with String, Dynamic, Null, Float, Int and Bool defaults after an Int, with and without the argument. A temporary print in setDefault showed every one of those slots at 4 modulo 8 on 64-bit Windows. setDefault now copies them with getSlot, setSlot and SetFloatAligned, as setInFrame does. The JIT compiles defaults into the function (genDefault) and never calls setDefault, so it was not affected. Tests: CppiaDefaultArgFrames --- Changes.md | 2 ++ src/hx/cppia/CppiaVars.cpp | 19 ++++++++++++------- test/regression/CppiaDefaultArgFrames/Main.hx | 18 ++++++++++++++++++ .../CppiaDefaultArgFrames/Script.hx | 18 ++++++++++++++++++ .../CppiaDefaultArgFrames/build.hxml | 7 +++++++ .../CppiaDefaultArgFrames/stdout.txt | 12 ++++++++++++ 6 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 test/regression/CppiaDefaultArgFrames/Main.hx create mode 100644 test/regression/CppiaDefaultArgFrames/Script.hx create mode 100644 test/regression/CppiaDefaultArgFrames/build.hxml create mode 100644 test/regression/CppiaDefaultArgFrames/stdout.txt diff --git a/Changes.md b/Changes.md index e52f55d48..fd14f28d9 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,8 @@ 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 + libhxcpp 6.0.2 ------------------------------------------------------------ diff --git a/src/hx/cppia/CppiaVars.cpp b/src/hx/cppia/CppiaVars.cpp index 1a8f56435..639ce4797 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 000000000..896584b12 --- /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 000000000..0d0a415f1 --- /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 000000000..d20d36bd4 --- /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 000000000..4ad749d52 --- /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 From 7581a49e5c007f82346920f79655186e01197cd3 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Fri, 27 Nov 2026 13:33:42 -0500 Subject: [PATCH 2/2] Remove the cppia JIT's unused conversion helpers argToInt, argToDouble, argToString and the block from objectToInt to doubleToObject in CppiaFunction.cpp have no callers. Five of them, the three argTo helpers, objectToDoublePointer and objectToStringPointer, read a frame slot in place, which 7a783808 moved every live helper away from, so they go rather than wait to be called. --- src/hx/cppia/CppiaFunction.cpp | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/hx/cppia/CppiaFunction.cpp b/src/hx/cppia/CppiaFunction.cpp index c3aabf6e6..31a2bdc30 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() ); }