Skip to content

Commit b6981ce

Browse files
Check unused cppia casts to classes and interfaces under the JIT (#501)
The JIT compiled a cast whose value goes unused, such as cast(o, I) as a statement, as its value alone, for every kind of cast. A cast to a class or interface is a check that can fail, so one that failed ran on under the JIT, where the interpreter and compiled code throw. It was so in jitted methods before, and Haxe's unit suite met it once the JIT compiled local functions: TestCasts.testInterfaces makes such casts in closures, as exc(function() cast(v, I2)), and failed three assertions under the JIT on this branch while passing on main. Only the unused casts that cannot fail, to Dynamic and the unchecked Int, Float, Bool and String conversions, are left out now. A cast to a class or interface checks, and an array conversion runs, as the interpreter's do. CppiaJitUnusedCasts casts to an interface and to a class, passing and failing, as statements in closures and in methods, used, and before a statement that must not run, as compiled code and as a script interpreted and jitted, and checks that all three agree. Without the fix the jitted run let every failing cast through. Tests: CppiaJitUnusedCasts
1 parent 35cede7 commit b6981ce

6 files changed

Lines changed: 182 additions & 1 deletion

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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 a cppia cast to a class or interface whose value goes unused, cast(o, SomeInterface) as a statement, checking nothing under the JIT: a cast that fails ran on, where the interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes such casts in closures, which the JIT now compiles. The JIT leaves out only the unused casts that cannot fail
67
* 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
78
* 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
89
* 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)

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,10 @@ struct CastExpr : public CppiaDynamicExpr
14501450
return;
14511451
}
14521452

1453-
if (destType==etNull || destType==etVoid)
1453+
// An unused cast that cannot fail is only its value. A cast to a class or
1454+
// interface still checks, and an array conversion still runs, as the
1455+
// interpreter's do
1456+
if ((destType==etNull || destType==etVoid) && op!=castInstance && op!=castDataArray && op!=castDynArray)
14541457
{
14551458
value->genCode(compiler);
14561459
return;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Under the JIT, a cast to a class or interface whose value goes unused, as a
2+
// statement, checked nothing, so a cast that fails ran on, where the
3+
// interpreter and compiled code throw. Haxe's TestCasts.testInterfaces makes
4+
// such casts in closures, which the JIT compiles since local functions are
5+
// compiled.
6+
// The same cases run as compiled code, then as a script with the JIT off and on,
7+
// and print the same lines each time.
8+
function main() {
9+
Sys.println("-- compiled");
10+
Compiled.run();
11+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
12+
for (jit in [false, true]) {
13+
Sys.println(jit ? "-- script, JIT on" : "-- script, JIT off");
14+
cpp.cppia.Host.enableJit(jit);
15+
try {
16+
cpp.cppia.Host.runFile(script);
17+
} catch (e:Dynamic) {
18+
Sys.println("error: " + e);
19+
}
20+
}
21+
}
22+
23+
// The same cases as Script.hx, compiled into the host
24+
interface CompiledFirst {}
25+
interface CompiledSecond {}
26+
27+
class CompiledPlain implements CompiledFirst {
28+
public function new() {}
29+
}
30+
31+
class CompiledBoth extends CompiledPlain implements CompiledSecond {
32+
public function new() {
33+
super();
34+
}
35+
}
36+
37+
class Compiled {
38+
static function attempt(label:String, f:Void->Void) {
39+
try {
40+
f();
41+
Sys.println('$label passes');
42+
} catch (e:Dynamic) {
43+
Sys.println('$label throws');
44+
}
45+
}
46+
47+
// A cast whose value goes unused, as a statement of a method
48+
static function toSecond(o:Dynamic) {
49+
cast(o, CompiledSecond);
50+
}
51+
52+
static function toBoth(o:Dynamic) {
53+
cast(o, CompiledBoth);
54+
}
55+
56+
public static function run() {
57+
var plain = new CompiledPlain();
58+
var both = new CompiledBoth();
59+
attempt("plain to first in a closure", function() cast(plain, CompiledFirst));
60+
attempt("plain to second in a closure", function() cast(plain, CompiledSecond));
61+
attempt("both to second in a closure", function() cast(both, CompiledSecond));
62+
attempt("plain to both in a closure", function() cast(plain, CompiledBoth));
63+
attempt("plain to second in a method", () -> toSecond(plain));
64+
attempt("both to second in a method", () -> toSecond(both));
65+
attempt("plain to both in a method", () -> toBoth(plain));
66+
attempt("both to both in a method", () -> toBoth(both));
67+
attempt("plain to second, used", function() {
68+
var s:CompiledSecond = cast(plain, CompiledSecond);
69+
Sys.println('used ${s == null}');
70+
});
71+
var after = 0;
72+
attempt("a statement after a failed cast", function() {
73+
cast(plain, CompiledSecond);
74+
after++;
75+
});
76+
Sys.println('ran on past it $after');
77+
}
78+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// The same cases as Compiled in Main.hx, run as a script
2+
interface First {}
3+
interface Second {}
4+
5+
class Plain implements First {
6+
public function new() {}
7+
}
8+
9+
class Both extends Plain implements Second {
10+
public function new() {
11+
super();
12+
}
13+
}
14+
15+
class Script {
16+
static function attempt(label:String, f:Void->Void) {
17+
try {
18+
f();
19+
Sys.println('$label passes');
20+
} catch (e:Dynamic) {
21+
Sys.println('$label throws');
22+
}
23+
}
24+
25+
// A cast whose value goes unused, as a statement of a method
26+
static function toSecond(o:Dynamic) {
27+
cast(o, Second);
28+
}
29+
30+
static function toBoth(o:Dynamic) {
31+
cast(o, Both);
32+
}
33+
34+
static function main() {
35+
var plain = new Plain();
36+
var both = new Both();
37+
attempt("plain to first in a closure", function() cast(plain, First));
38+
attempt("plain to second in a closure", function() cast(plain, Second));
39+
attempt("both to second in a closure", function() cast(both, Second));
40+
attempt("plain to both in a closure", function() cast(plain, Both));
41+
attempt("plain to second in a method", () -> toSecond(plain));
42+
attempt("both to second in a method", () -> toSecond(both));
43+
attempt("plain to both in a method", () -> toBoth(plain));
44+
attempt("both to both in a method", () -> toBoth(both));
45+
attempt("plain to second, used", function() {
46+
var s:Second = cast(plain, Second);
47+
Sys.println('used ${s == null}');
48+
});
49+
var after = 0;
50+
attempt("a statement after a failed cast", function() {
51+
cast(plain, Second);
52+
after++;
53+
});
54+
Sys.println('ran on past it $after');
55+
}
56+
}
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- compiled
2+
plain to first in a closure passes
3+
plain to second in a closure throws
4+
both to second in a closure passes
5+
plain to both in a closure throws
6+
plain to second in a method throws
7+
both to second in a method passes
8+
plain to both in a method throws
9+
both to both in a method passes
10+
plain to second, used throws
11+
a statement after a failed cast throws
12+
ran on past it 0
13+
-- script, JIT off
14+
plain to first in a closure passes
15+
plain to second in a closure throws
16+
both to second in a closure passes
17+
plain to both in a closure throws
18+
plain to second in a method throws
19+
both to second in a method passes
20+
plain to both in a method throws
21+
both to both in a method passes
22+
plain to second, used throws
23+
a statement after a failed cast throws
24+
ran on past it 0
25+
-- script, JIT on
26+
plain to first in a closure passes
27+
plain to second in a closure throws
28+
both to second in a closure passes
29+
plain to both in a closure throws
30+
plain to second in a method throws
31+
both to second in a method passes
32+
plain to both in a method throws
33+
both to both in a method passes
34+
plain to second, used throws
35+
a statement after a failed cast throws
36+
ran on past it 0

0 commit comments

Comments
 (0)