Skip to content

Commit 46bb64f

Browse files
Load objects the cppia JIT uses from their holders at run time
The JIT wrote the address of some objects straight into its code: boxed string literals, the SourceInfo behind PosInfos, and enum constants. They are rooted, but a moving collection moves them and updates the fields that hold them, not the code, so jitted code went on using where they had been. The code now loads each object from the field that holds it. The test moves objects on every collection; without the fix it crashes in some of its runs. Follows HaxeFoundation/hxcpp#1406 Fixes #52
1 parent e3bf69f commit 46bb64f

8 files changed

Lines changed: 82 additions & 9 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ The first libhxcpp version. It is based on hxcpp 4.3 (the changes listed under 4
213213
* Added MSVC builds with clang-cl and the LLVM tools, with -D HXCPP_MSVC_LLVM, and cross-compiling them from Linux with -D HXCPP_MSVC_ROOT (hxcpp #1361)
214214
* Fixed an SSL server crashing when an accepted socket handshakes after its listener was closed and collected, and, on Haxe 5, any SNI handshake after a collection: the SNI callback, certificate, key and CA the configuration uses were not kept alive
215215
* Fixed the cppia JIT corrupting memory or crashing on Array<cpp.Float32>, and failing to load a Float32 array literal with Type mismatch
216+
* Fixed the cppia JIT with the moving collector using stale addresses for boxed string literals, PosInfos and script enum constants, which it had written into its code
216217
* Added the libhxcpp define: a build that uses the library gets -D libhxcpp=<version>. Compare it with version("x.y.z"); a plain string comparison compares text
217218
* Added separate Linux ARMv7 and ARM64 build architectures (hxcpp #1355)
218219
* Fixed AndroidCompat.cpp closing its extern "C" section in the wrong place (hxcpp #1404)

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,10 +4802,9 @@ struct StringVal : public CppiaExprWithValue
48024802
switch(destType)
48034803
{
48044804
case etObject:
4805-
//TODO - GC!
48064805
if (!value.mPtr)
48074806
value = strVal;
4808-
compiler->move(inDest, (void *) value.mPtr );
4807+
genLoadHeldObject(compiler, inDest, &value.mPtr);
48094808
break;
48104809

48114810

@@ -4951,8 +4950,7 @@ struct PosInfo : public CppiaExprWithValue
49514950
#ifdef CPPIA_JIT
49524951
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType) HXCPP_OVERRIDE
49534952
{
4954-
// TODO GC
4955-
compiler->move(inDest, (void *)value.mPtr );
4953+
genLoadHeldObject(compiler, inDest, &value.mPtr);
49564954
}
49574955
#endif
49584956

‎src/hx/cppia/Cppia.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,16 @@ template<> struct ExprTypeOf<float> { enum { value = etFloat }; };
966966
template<> struct ExprTypeOf<String> { enum { value = etString }; };
967967

968968

969+
#ifdef CPPIA_JIT
970+
// Loads the object a field holds when the code runs. A moving collection
971+
// updates the field, while an address written into the code would go stale.
972+
inline void genLoadHeldObject(CppiaCompiler *compiler, const JitVal &inDest, hx::Object **inHolder)
973+
{
974+
compiler->move( sJitTemp0, JitVal( (void *)inHolder ) );
975+
compiler->move( inDest, sJitTemp0.star(jtPointer,0) );
976+
}
977+
#endif
978+
969979
template<typename T>
970980
struct ExprTypeIsBool { enum { value = false }; };
971981
template<> struct ExprTypeIsBool<bool> { enum { value = true }; };

‎src/hx/cppia/CppiaClasses.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,20 @@ struct EnumField : public CppiaDynamicExpr
236236
return this;
237237
}
238238

239-
hx::Object *getValue()
239+
hx::Object **getValueHolder()
240240
{
241241
if (value)
242-
return value->value.mPtr;
242+
return &value->value.mPtr;
243243
if (!enumValue.mPtr)
244244
{
245245
enumValue = enumClass->ConstructEnum(enumName,null());
246246
}
247-
return enumValue.mPtr;
247+
return &enumValue.mPtr;
248+
}
249+
250+
hx::Object *getValue()
251+
{
252+
return *getValueHolder();
248253
}
249254

250255
hx::Object *runObject(CppiaCtx *ctx) HXCPP_OVERRIDE
@@ -285,9 +290,8 @@ struct EnumField : public CppiaDynamicExpr
285290
int s = args.size();
286291
if (s==0)
287292
{
288-
// TODO GC pin
289293
if (destType==etObject)
290-
compiler->move(inDest, (void *)getValue());
294+
genLoadHeldObject(compiler, inDest, getValueHolder());
291295
}
292296
else if (value)
293297
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function main() {
2+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
3+
for (jit in [false, true]) {
4+
cpp.cppia.Host.enableJit(jit);
5+
try {
6+
cpp.cppia.Host.runFile(script);
7+
} catch (e:Dynamic) {
8+
Sys.println("error: " + e);
9+
}
10+
}
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
enum Constant {
2+
First;
3+
Second;
4+
}
5+
6+
// The JIT used to embed these objects' addresses in its code, and the moving
7+
// collector moves them
8+
class Script {
9+
static function literal():Dynamic {
10+
return "a boxed string literal";
11+
}
12+
13+
static function constant():Constant {
14+
return Second;
15+
}
16+
17+
static function position(?pos:haxe.PosInfos):String {
18+
return pos.methodName;
19+
}
20+
21+
static function main() {
22+
var ok = true;
23+
for (round in 0...30) {
24+
var churn = [for (i in 0...20000) [i, round]];
25+
churn = null;
26+
cpp.vm.Gc.run(true);
27+
// Fill the space the moved objects left
28+
var refill = [for (i in 0...20000) Std.string(i * round)];
29+
if (literal() != "a boxed string literal")
30+
ok = false;
31+
if (constant() != Second)
32+
ok = false;
33+
if (position() != "main")
34+
ok = false;
35+
refill = null;
36+
}
37+
Sys.println(ok ? "constants ok" : "constants moved");
38+
}
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--main Script
2+
-cppia bin/script.cppia
3+
--next
4+
--main Main
5+
-cpp bin
6+
-D scriptable
7+
--dce no
8+
-D HXCPP_GC_DEBUG_ALWAYS_MOVE
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
constants ok
2+
constants ok

0 commit comments

Comments
 (0)