Skip to content

Commit acc5a26

Browse files
Copy a cppia argument's default slots in and out of the script frame (#496)
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. 7a78380 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>, 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 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 7a78380 moved every live helper away from, so they go rather than wait to be called.
1 parent 9e8459d commit acc5a26

7 files changed

Lines changed: 68 additions & 28 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 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
56
* 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)
67

78
libhxcpp 6.0.2

‎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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,20 @@ void CppiaStackVar::linkDefault()
522522

523523
void CppiaStackVar::setDefault(CppiaCtx *inCxt, const CppiaConst &inDefault)
524524
{
525+
// The argument's slots are frame slots, which can be unaligned (see
526+
// StackContext::getSlot), so they are copied in and out
525527
if (argType==etString)
526528
{
527529
if (inDefault.type == CppiaConst::cString)
528530
{
529-
String *s = (String *)(inCxt->frame + defaultStackPos);
530-
if (!s->raw_ptr())
531-
*s = module->strings[ inDefault.ival ];
531+
unsigned char *slot = inCxt->frame + defaultStackPos;
532+
if (!CppiaCtx::getSlot<String>(slot).raw_ptr())
533+
CppiaCtx::setSlot(slot, module->strings[ inDefault.ival ]);
532534
}
533535
}
534536
else
535537
{
536-
hx::Object *src = *(hx::Object **)(inCxt->frame + defaultStackPos);
538+
hx::Object *src = CppiaCtx::getSlot<hx::Object *>(inCxt->frame + defaultStackPos);
537539

538540
if (src)
539541
{
@@ -550,19 +552,22 @@ void CppiaStackVar::setDefault(CppiaCtx *inCxt, const CppiaConst &inDefault)
550552
inDefault.type==CppiaConst::cInt ? inDefault.ival :
551553
inDefault.dval;
552554
break;
553-
case fsFloat: *(Float *)ptr =
555+
case fsFloat: SetFloatAligned(ptr,
554556
inDefault.type==CppiaConst::cInt ? inDefault.ival :
555-
inDefault.dval;
557+
inDefault.dval);
556558
break;
557559
case fsObject:
558560
{
559-
Dynamic &d = *(Dynamic *)ptr;
561+
Dynamic d;
560562
if (inDefault.type==CppiaConst::cInt)
561563
d = inDefault.ival;
562564
else if (inDefault.type==CppiaConst::cFloat)
563565
d = inDefault.dval;
564566
else if (inDefault.type==CppiaConst::cString)
565567
d = module->strings[ inDefault.ival ];
568+
else
569+
break;
570+
CppiaCtx::setSlot(ptr, d.mPtr);
566571
}
567572
break;
568573
case fsString:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// The cppia interpreter fills in an argument's default value in the function's
2+
// frame, which only keeps 4-byte alignment. It read the argument's String or
3+
// object slot in place, and wrote a Float or Dynamic default the same way, so
4+
// on 64-bit, where a default after an Int is at an odd multiple of 4, that was
5+
// undefined behaviour (UBSan reports it) and a fault on a CPU that requires
6+
// alignment. The jit compiles defaults into the function instead, so the jit
7+
// run is here for comparison.
8+
function main() {
9+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
10+
for (jit in [false, true]) {
11+
cpp.cppia.Host.enableJit(jit);
12+
try {
13+
cpp.cppia.Host.runFile(script);
14+
} catch (e:Dynamic) {
15+
Sys.println("error: " + e);
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Script {
2+
// Each default follows an Int, so it sits 4 bytes past an 8-byte boundary
3+
static function str(a:Int, ?b:String = "default") return '$a $b';
4+
static function dyn(a:Int, ?b:Dynamic = 5) return '$a $b';
5+
static function nullFloat(a:Int, ?b:Float = 1.5) return '$a $b';
6+
static function float(a:Int, b:Float = 1.5) return '$a $b';
7+
static function int(a:Int, b:Int = 7) return '$a $b';
8+
static function bool(a:Int, b:Bool = true) return '$a $b';
9+
10+
static function main() {
11+
Sys.println("str: " + str(1) + ", " + str(2, "given"));
12+
Sys.println("dyn: " + dyn(1) + ", " + dyn(2, "given"));
13+
Sys.println("nullFloat: " + nullFloat(1) + ", " + nullFloat(2, 2.25));
14+
Sys.println("float: " + float(1) + ", " + float(2, 2.25));
15+
Sys.println("int: " + int(1) + ", " + int(2, 9));
16+
Sys.println("bool: " + bool(1) + ", " + bool(2, false));
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--main Script
2+
-cppia bin/script.cppia
3+
--next
4+
--main Main
5+
-cpp bin
6+
-D scriptable
7+
--dce no
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
str: 1 default, 2 given
2+
dyn: 1 5, 2 given
3+
nullFloat: 1 1.5, 2 2.25
4+
float: 1 1.5, 2 2.25
5+
int: 1 7, 2 9
6+
bool: 1 true, 2 false
7+
str: 1 default, 2 given
8+
dyn: 1 5, 2 given
9+
nullFloat: 1 1.5, 2 2.25
10+
float: 1 1.5, 2 2.25
11+
int: 1 7, 2 9
12+
bool: 1 true, 2 false

0 commit comments

Comments
 (0)