Skip to content

Commit eb86a67

Browse files
Tutez64dimensionscape
authored andcommitted
Align the objects that jitted cppia code allocates in the nursery.
With HXCPP_GC_GENERATIONAL, ImmixAllocator::alloc skips 4 bytes when needed so that each nursery object is 8-byte aligned, and the nursery scan that finds conservatively referenced objects (GetEnclosingNurseryType) walks the holes with that alignment. The inline allocation that the JIT emits for a new did not align. After an object it placed off alignment, the scan read a wrong header and missed the objects that follow, so an object referenced only from the stack was freed by the next collection. The cppia host now builds with HXCPP_GC_GENERATIONAL. The new test has jitted code allocate two objects, then keeps an array only on the stack across a generational collection. Without the fix, the -jit run crashes. HaxeFoundation/hxcpp#1411 Fixes #26
1 parent da77567 commit eb86a67

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,12 @@ struct NewExpr : public CppiaDynamicExpr
16281628
// sJitReturnReg = alloc->spaceFirst - will be the result if all goes well
16291629
compiler->move(sJitReturnReg, sJitCtx.star(jtPointer, offsetof(hx::StackContext,spaceFirst) ) );
16301630

1631+
#ifdef HXCPP_ALIGN_ALLOC
1632+
// As ImmixAllocator::alloc: skip 4 bytes to align the object, which the nursery scan expects
1633+
compiler->bitOp(bitOpAnd, sJitTemp1.as(jtInt), sJitReturnReg.as(jtInt), (int)4 );
1634+
compiler->add(sJitReturnReg.as(jtPointer), sJitReturnReg.as(jtPointer), sJitTemp1.as(jtPointer) );
1635+
#endif
1636+
16311637
// sJitTemp1 = end = spaceFirst + size + sizeof(int)
16321638
compiler->add(sJitTemp1.as(jtPointer), sJitReturnReg.as(jtPointer), (int)(size + sizeof(int) ) );
16331639

‎test/cppia/Client.hx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ class ClientAbstractChild extends ClientAbstractBase {
2828
public function implemented() return 4;
2929
}
3030

31+
class ClientNursery {
32+
33+
public var value:Dynamic;
34+
35+
public function new() {}
36+
37+
// With -jit, the jitted code allocates these in the nursery itself.
38+
public static function allocate() {
39+
new ClientNursery();
40+
new ClientNursery();
41+
}
42+
}
43+
3144
class Client
3245
{
3346
public static var clientBool0 = true;

‎test/cppia/cases/TestCommon.hx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cases;
22

33
import cpp.cppia.Host;
4+
import cpp.vm.Gc;
45
import utest.Test;
56
import utest.Assert;
67

@@ -65,6 +66,19 @@ class TestCommon extends Test {
6566
Assert.raises(() -> cpp.cppia.Module.fromData(source.getData()), String, 'Linked a new of a class the host does not have');
6667
}
6768

69+
@:depends(testStatus)
70+
function testStackObjectAfterScriptAllocations() {
71+
final nursery = Type.resolveClass('ClientNursery');
72+
Reflect.callMethod(nursery, Reflect.field(nursery, 'allocate'), []);
73+
// Only the stack references it, so only the nursery scan finds it.
74+
final kept = [42];
75+
76+
Gc.run(false);
77+
final junk = [for (i in 0...100000) [i]];
78+
79+
Assert.equals(42, kept[0]);
80+
}
81+
6882
@:depends(testStatus)
6983
function testInterfaceCalling() {
7084
final obj : IFoo = Type.createInstance(Type.resolveClass('ClientFoo'), []);

‎test/cppia/compile-host.hxml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-m CppiaHost
22
HostExtendedRoot
33
-D scriptable
4+
-D HXCPP_GC_GENERATIONAL
45
-D dll_export=host_classes.info
56
-L utest
67
--dce no

0 commit comments

Comments
 (0)