Skip to content

Commit e96c80c

Browse files
Move live cppia instances onto a reloaded module's classes (#442)
A loaded cppia module's adoptInstances(previous), called after it boots and before the previous module is released, gives each live instance of a previous class the class of the same name in this module, when the two lay out their fields the same way, so code reloaded into a running process reaches objects made before the reload. It returns a row per previous class with live instances: how many moved, and why not for one that could not. docs/CppiaModuleRelease.md describes it. Fixes #265. # Conflicts: # test/regression/emscripten-skip.txt
2 parents 59c1edd + 48038ad commit e96c80c

10 files changed

Lines changed: 449 additions & 3 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.0 (unreleased)
33
------------------------------------------------------------
44

5+
* Added adoptInstances to a loaded cppia module: called with the module it replaces, before that one is released, it moves the live instances of each of the old module's classes onto the new class of the same name when the two lay out their fields the same way, so code reloaded into a running process reaches objects made before the reload. Calls on them run the new code, through interfaces and dynamic calls too, and Type.getClass gives the new class. It returns a row for each old class with live instances: how many moved, and for a class whose fields changed, which field differs. Bound methods and closures made before keep the old code. See docs/CppiaModuleRelease.md (libhxcpp #265)
56
* Fixed the incremental collector (HXCPP_GC_INCREMENTAL) freeing a thread object's arrays while the thread was being created, as the gc stress test's verifier caught on Windows. A slice could find the new object on the stack while its first array was being allocated, and the cycle could end before the constructor's barrier, with the arrays stored after that unscanned. The arrays are now made in a mutation scope, which the stop that finishes a cycle scans again
67
* cpp.encoding.Utf8.decode replaces each malformed UTF-8 sequence with U+FFFD instead of throwing, with smart strings or without. Haxe 5's Bytes.toString and getString decode with it, so they threw for bytes that Haxe 4's, which go through String::create, decode with U+FFFD in the same places. Valid input still takes the decoder that counts first; only malformed input takes the replacing one. Utf8.codepoint still throws
78
* Fixed String::utf8_str into a view, at API level 500, writing the bytes and then throwing when the view had room for them but not the terminator. It returns false before writing anything. It also stopped a UTF-16 string at its first NUL, which is a character, and reported that length (libhxcpp #408)

‎docs/CppiaModuleRelease.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,30 @@ for (row in (untyped __global__.__scriptable_cppia_retained() : Array<Dynamic>))
6666
It works in release builds, like `__hxcpp_gc_census()`, on which it builds. Not counted, since the module holds them itself while it is loaded: its `Class` objects, its enum values, and its static functions used as values. A host holding one of those keeps the module loaded too. Code of the module still running on a thread, and stale pointers to it left on a stack (see Limitations), keep it loaded without anything being counted.
6767

6868
To find what holds the instances of a class, build the host with `-debug` and call `cpp.vm.Gc.trace` with that class, kept from before the release, since `Type.resolveClass` no longer finds it: it prints the path from a root to each instance.
69+
70+
## Moving live instances onto the new module's classes
71+
72+
Instances made before a reload keep their class and its code, so a reload only changes objects made afterwards. A host can move them instead, after the new module has booted and before the old one is released:
73+
74+
```haxe
75+
var next = cpp.cppia.Module.fromData(bytes.getData());
76+
next.boot();
77+
var report:Array<Dynamic> = untyped next.adoptInstances(previous);
78+
untyped previous.release();
79+
```
80+
81+
`adoptInstances(previous)` runs a full collection, and in it gives each live instance of a previous module's class the class of the same name in this module, when the two lay out their fields the same way. Calls on a moved instance then run the new code, through the instance, its interfaces and dynamic calls alike, and `Type.getClass` gives the new class. It returns one row for each previous class that had live instances, in name order:
82+
83+
- `name`, the class's name.
84+
- `moved`, how many of its instances moved.
85+
- `reason`, null when they could move, otherwise why not, naming the first field that differs.
86+
87+
The rule is strict, as an instance stays where it is, at its size. Both classes must extend the same host class, through super classes of the same names, implement the same interfaces, and have the same fields, dynamic functions included, with the same names, storage types and offsets. Methods can differ in any way, so editing method bodies, the usual case, is covered. A class that adds, removes, retypes or reorders a field keeps its instances, which keep the old module loaded until they go.
88+
89+
What does not move:
90+
91+
- Bound methods and closures made from the old class's methods before the step keep calling the old code, and keep the old module loaded until they are dropped. The same goes for a dynamic function's value held in a field.
92+
- Code running on any thread finishes in the old code. The fields are laid out the same, so it reads and writes them correctly.
93+
- Statics: the new module has its own, and nothing is copied.
94+
95+
Calling it twice moves nothing the second time. A host that does not call it sees no change.

‎src/hx/cppia/CppiaModule.cpp‎

Lines changed: 205 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ void CppiaConst::fromStream(CppiaStream &stream)
441441

442442

443443

444+
static Array<Dynamic> adoptInstances(CppiaModule *inNext, CppiaModule *inPrevious);
445+
444446
// Cppia Object - manage
445447
class CppiaObject : public hx::CppiaLoadedModule_obj
446448
{
@@ -570,10 +572,47 @@ class CppiaObject : public hx::CppiaLoadedModule_obj
570572
#endif
571573
};
572574

575+
// Moves the live instances of the previous module's classes onto this
576+
// module's classes of the same name, where their fields are laid out the
577+
// same, and reports each class that had any (libhxcpp #265)
578+
Array<Dynamic> adopt(Dynamic inPrevious)
579+
{
580+
CppiaObject *previous = dynamic_cast<CppiaObject *>(inPrevious.mPtr);
581+
if (!previous)
582+
hx::Throw(HX_CSTRING("adoptInstances takes a cppia module"));
583+
if (previous==this)
584+
hx::Throw(HX_CSTRING("adoptInstances takes another module"));
585+
if (previous->cppia->released)
586+
hx::Throw(HX_CSTRING("adoptInstances takes a module before its release"));
587+
// Moved instances run this module's code, which needs its statics
588+
boot();
589+
return adoptInstances(cppia, previous->cppia);
590+
}
591+
592+
struct AdoptFunc : public hx::LocalFunc
593+
{
594+
hx::Object *module;
595+
AdoptFunc(hx::Object *inModule) : module(inModule) { }
596+
Dynamic __Run(const Array<Dynamic> &inArgs) HXCPP_OVERRIDE
597+
{
598+
return static_cast<CppiaObject *>(module)->adopt(inArgs->length ? inArgs[0] : Dynamic());
599+
}
600+
#if (HXCPP_API_LEVEL<500)
601+
Dynamic __run(const Dynamic &inPrevious) HXCPP_OVERRIDE { return static_cast<CppiaObject *>(module)->adopt(inPrevious); }
602+
#endif
603+
int __ArgCount() const HXCPP_OVERRIDE { return 1; }
604+
void __Mark(hx::MarkContext *__inCtx) HXCPP_OVERRIDE { HX_MARK_MEMBER(module); }
605+
#ifdef HXCPP_VISIT_ALLOCS
606+
void __Visit(hx::VisitContext *__inCtx) HXCPP_OVERRIDE { HX_VISIT_OBJECT(module); }
607+
#endif
608+
};
609+
573610
hx::Val __Field(const String &inName, hx::PropertyAccess inCallProp) HXCPP_OVERRIDE
574611
{
575612
if (inName==HX_CSTRING("release"))
576613
return Dynamic(new ReleaseFunc(this));
614+
if (inName==HX_CSTRING("adoptInstances"))
615+
return Dynamic(new AdoptFunc(this));
577616
return hx::CppiaLoadedModule_obj::__Field(inName, inCallProp);
578617
}
579618

@@ -1202,6 +1241,171 @@ static hx::Anon retainedRow(const char *inName, const RetainedTally &inTally)
12021241
return row;
12031242
}
12041243

1244+
// The census visitor is one global, so its users take turns
1245+
static std::mutex *sCensusVisitorLock = new std::mutex();
1246+
1247+
static void censusWithVisitor(void (*inVisitor)(hx::Object *, size_t))
1248+
{
1249+
{
1250+
hx::AutoGCFreeZone wait;
1251+
sCensusVisitorLock->lock();
1252+
}
1253+
gCensusVisitor = inVisitor;
1254+
__hxcpp_gc_census();
1255+
gCensusVisitor = 0;
1256+
sCensusVisitorLock->unlock();
1257+
}
1258+
1259+
1260+
// --- Moving live instances onto a replaced class (libhxcpp #265) ---
1261+
//
1262+
// An instance of a script class finds everything about its class through its
1263+
// script vtable, whose slot -1 is the class: its methods, its interfaces, how
1264+
// the collector marks it, Type.getClass. So an instance moves to another class
1265+
// by having that one pointer replaced. It stays where it is, at its size, so
1266+
// the new class must lay out every field as the old one did.
1267+
1268+
// A previous module's class with live instances, and the class they move to,
1269+
// or why they cannot
1270+
struct Adoption
1271+
{
1272+
CppiaClassInfo *to;
1273+
std::string reason;
1274+
size_t moved;
1275+
size_t live;
1276+
Adoption() : to(0), moved(0), live(0) { }
1277+
};
1278+
static std::map<CppiaClassInfo *, Adoption> *sAdoptions = 0;
1279+
1280+
static std::string typeName(TypeData *inType)
1281+
{
1282+
return std::string(inType->name.utf8_str());
1283+
}
1284+
1285+
// The interfaces a class and its script super classes implement, by name
1286+
static std::vector<std::string> interfaceNames(CppiaClassInfo *inInfo)
1287+
{
1288+
std::vector<std::string> names;
1289+
for(CppiaClassInfo *info=inInfo; info; info=info->superType ? info->superType->cppiaClass : 0)
1290+
for(size_t i=0;i<info->implements.size();i++)
1291+
names.push_back(typeName(info->cppia.types[info->implements[i]]));
1292+
std::sort(names.begin(), names.end());
1293+
return names;
1294+
}
1295+
1296+
// The first difference between two lists of a class's storage, its fields or
1297+
// its dynamic functions, which are linked with their super classes' first
1298+
static std::string slotDifference(const char *inWhat, CppiaClassInfo *inFrom, std::vector<CppiaVar *> &inOld,
1299+
CppiaClassInfo *inTo, std::vector<CppiaVar *> &inNew)
1300+
{
1301+
for(size_t i=0;i<inOld.size() || i<inNew.size();i++)
1302+
{
1303+
if (i>=inOld.size())
1304+
return std::string(inWhat) + " " + inTo->cppia.identStr(inNew[i]->nameId) + " is not in the old class";
1305+
std::string name = inFrom->cppia.identStr(inOld[i]->nameId);
1306+
if (i>=inNew.size())
1307+
return std::string(inWhat) + " " + name + " is not in the new class";
1308+
if (name!=inTo->cppia.identStr(inNew[i]->nameId))
1309+
return std::string(inWhat) + " " + name + " is not where it was";
1310+
if (inOld[i]->storeType!=inNew[i]->storeType || inOld[i]->offset!=inNew[i]->offset)
1311+
return std::string(inWhat) + " " + name + " changed its type";
1312+
}
1313+
return std::string();
1314+
}
1315+
1316+
// Why instances of inFrom cannot become instances of inTo, or empty when they can
1317+
static std::string incompatibility(CppiaClassInfo *inFrom, CppiaClassInfo *inTo)
1318+
{
1319+
if (inFrom->haxeBase!=inTo->haxeBase)
1320+
return "it extends another host class";
1321+
TypeData *from = inFrom->superType;
1322+
TypeData *to = inTo->superType;
1323+
while(from || to)
1324+
{
1325+
if (!from || !to || typeName(from)!=typeName(to))
1326+
return "its super classes are not the same";
1327+
from = from->cppiaClass ? from->cppiaClass->superType : 0;
1328+
to = to->cppiaClass ? to->cppiaClass->superType : 0;
1329+
}
1330+
if (interfaceNames(inFrom)!=interfaceNames(inTo))
1331+
return "it implements other interfaces";
1332+
std::string field = slotDifference("field", inFrom, inFrom->memberVars, inTo, inTo->memberVars);
1333+
if (field.empty())
1334+
field = slotDifference("dynamic function", inFrom, inFrom->dynamicFunctions, inTo, inTo->dynamicFunctions);
1335+
if (!field.empty())
1336+
return field;
1337+
if (inFrom->classSize!=inTo->classSize || inFrom->extraData!=inTo->extraData ||
1338+
inFrom->dynamicMapOffset!=inTo->dynamicMapOffset || inFrom->containsPointers!=inTo->containsPointers)
1339+
return "its instances are laid out otherwise";
1340+
return std::string();
1341+
}
1342+
1343+
// Called with each object the census marks, on one thread with every other stopped
1344+
static void adoptVisit(hx::Object *inObj, size_t)
1345+
{
1346+
// A module marks its own Class objects
1347+
if (getScriptClassInfo(inObj))
1348+
return;
1349+
void **vtable = inObj->__GetScriptVTable();
1350+
if (!vtable)
1351+
return;
1352+
std::map<CppiaClassInfo *, Adoption>::iterator found = sAdoptions->find((CppiaClassInfo *)vtable[-1]);
1353+
if (found==sAdoptions->end())
1354+
return;
1355+
Adoption &adoption = found->second;
1356+
adoption.live++;
1357+
if (!adoption.to)
1358+
return;
1359+
void ***slot = (void ***)((char *)inObj + found->first->getScriptVTableOffset());
1360+
if (*slot!=vtable)
1361+
return;
1362+
*slot = adoption.to->vtable;
1363+
adoption.moved++;
1364+
}
1365+
1366+
static Array<Dynamic> adoptInstances(CppiaModule *inNext, CppiaModule *inPrevious)
1367+
{
1368+
std::map<CppiaClassInfo *, Adoption> adoptions;
1369+
for(size_t i=0;i<inPrevious->classes.size();i++)
1370+
{
1371+
CppiaClassInfo *info = inPrevious->classes[i];
1372+
if (info->isInterface || info->isEnum || !info->vtable)
1373+
continue;
1374+
Adoption &adoption = adoptions[info];
1375+
CppiaClassInfo *to = inNext->findClass(String::create(info->name.c_str()));
1376+
if (!to || to->isInterface || to->isEnum || !to->vtable)
1377+
adoption.reason = "the new module has no class of that name";
1378+
else
1379+
{
1380+
adoption.reason = incompatibility(info, to);
1381+
if (adoption.reason.empty())
1382+
adoption.to = to;
1383+
}
1384+
}
1385+
1386+
sAdoptions = &adoptions;
1387+
censusWithVisitor(adoptVisit);
1388+
sAdoptions = 0;
1389+
1390+
std::vector< std::pair<std::string, Adoption> > rows;
1391+
for(std::map<CppiaClassInfo *, Adoption>::iterator i=adoptions.begin(); i!=adoptions.end(); ++i)
1392+
if (i->second.live)
1393+
rows.push_back(std::make_pair(i->first->name, i->second));
1394+
std::sort(rows.begin(), rows.end(), [](const std::pair<std::string, Adoption> &a, const std::pair<std::string, Adoption> &b)
1395+
{ return a.first < b.first; });
1396+
1397+
Array<Dynamic> result = Array_obj<Dynamic>::__new(0,(int)rows.size());
1398+
for(size_t r=0;r<rows.size();r++)
1399+
{
1400+
hx::Anon row = hx::Anon_obj::Create();
1401+
row->__SetField(HX_CSTRING("name"), String::create(rows[r].first.c_str()), hx::paccDynamic);
1402+
row->__SetField(HX_CSTRING("moved"), (int)std::min(rows[r].second.moved, (size_t)0x7fffffff), hx::paccDynamic);
1403+
row->__SetField(HX_CSTRING("reason"), rows[r].second.reason.empty() ? String() : String::create(rows[r].second.reason.c_str()), hx::paccDynamic);
1404+
result->push(row);
1405+
}
1406+
return result;
1407+
}
1408+
12051409
} // end namespace hx
12061410

12071411
// Collects, then reports each released module that is still loaded: its
@@ -1223,9 +1427,7 @@ Array<Dynamic> __scriptable_cppia_retained()
12231427
}
12241428

12251429
hx::sRetained = &retained;
1226-
hx::gCensusVisitor = hx::retainedVisit;
1227-
__hxcpp_gc_census();
1228-
hx::gCensusVisitor = 0;
1430+
hx::censusWithVisitor(hx::retainedVisit);
12291431
hx::sRetained = 0;
12301432

12311433
// Copied out under the lock, and turned into Haxe values after it: a
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import cpp.cppia.Host;
2+
import cpp.cppia.Module;
3+
4+
// A module's adoptInstances(previous) moves the live instances of each of the
5+
// previous module's classes onto the class of the same name, when the two lay
6+
// out their fields the same way, so their calls run the new code. Instances of
7+
// a class whose fields changed stay, and the report says why. A method bound
8+
// before stays on the old code and keeps the old module loaded until it is
9+
// dropped. openfl/libhxcpp#265
10+
//
11+
// Every call into script code runs on a joined native worker, so stale values
12+
// it leaves on a stack do not decide what keeps a module loaded. The main
13+
// thread only collects and prints.
14+
@:cppFileCode('
15+
#include <thread>
16+
#include <hx/Native.h>
17+
static void runJoined(Dynamic callback) {
18+
std::thread worker([callback]() mutable { hx::NativeAttach attach; callback(); });
19+
hx::AutoGCFreeZone freeZone;
20+
worker.join();
21+
}
22+
')
23+
class Main {
24+
static var dir:String;
25+
static var previous:Module;
26+
static var next:Module;
27+
static var counter:Dynamic;
28+
static var doubler:Dynamic;
29+
static var reshaped:Dynamic;
30+
static var bound:Void->Int;
31+
static var lines:Array<String>;
32+
33+
static function load(name:String):Module {
34+
var module = Module.fromData(sys.io.File.getBytes(haxe.io.Path.join([dir, name])).getData());
35+
module.boot();
36+
module.run();
37+
return module;
38+
}
39+
40+
static function loadOld() {
41+
previous = load("v1.cppia");
42+
counter = Type.createInstance(previous.resolveClass("Counter"), [5]);
43+
counter.step();
44+
doubler = Type.createInstance(previous.resolveClass("Doubler"), [0]);
45+
reshaped = Type.createInstance(previous.resolveClass("Reshaped"), []);
46+
bound = Reflect.field(counter, "step");
47+
}
48+
49+
static function adopt() {
50+
next = load("v2.cppia");
51+
var report:Array<Dynamic> = untyped next.adoptInstances(previous);
52+
untyped previous.release();
53+
previous = null;
54+
var rows = [for (r in report) '${r.name} moved ${r.moved}' + (r.reason == null ? "" : ': ${r.reason}')];
55+
rows.sort(Reflect.compare);
56+
lines.push('report: ${rows.join(", ")}');
57+
}
58+
59+
static function useMoved() {
60+
// Moved, with their fields as they were
61+
lines.push('counter runs the new code: ${counter.step() == 16 && counter.version() == "v2 counter"}');
62+
lines.push('the subclass runs its new override: ${doubler.step() == 20 && doubler.version() == "v2 counter"}');
63+
var stepper:Stepper = counter;
64+
lines.push('the host interface reaches the new code: ${stepper.step() == 26}');
65+
var counterClass = next.resolveClass("Counter");
66+
lines.push('they are of the new classes: ${Type.getClass(counter) == counterClass && Std.isOfType(counter, counterClass) && Std.isOfType(doubler, counterClass) && Type.getClass(doubler) == next.resolveClass("Doubler")}');
67+
}
68+
69+
static function useOld() {
70+
// Stayed
71+
lines.push('the reshaped instance keeps its class: ${reshaped.version() == "v1"}');
72+
lines.push('the bound method keeps the old code: ${bound() == 27 && counter.step() == 37}');
73+
}
74+
75+
static function dropOld() {
76+
bound = null;
77+
reshaped = null;
78+
}
79+
80+
static function dropAll() {
81+
counter = doubler = null;
82+
untyped next.release();
83+
next = null;
84+
}
85+
86+
static function retained():String {
87+
var modules:Array<Dynamic> = untyped __global__.__scriptable_cppia_retained();
88+
return [
89+
for (m in modules)
90+
[for (i in (m.instances : Array<Dynamic>)) '${i.name} x${i.count}'].concat(['closures x${m.closures.count}']).join(", ")
91+
].join("; ");
92+
}
93+
94+
static function main() {
95+
dir = haxe.io.Path.directory(Sys.programPath());
96+
for (jit in [false, true]) {
97+
Host.enableJit(jit);
98+
lines = [];
99+
untyped __cpp__("runJoined({0})", loadOld);
100+
untyped __cpp__("runJoined({0})", adopt);
101+
untyped __cpp__("runJoined({0})", useMoved);
102+
untyped __cpp__("runJoined({0})", useOld);
103+
lines.push('the old module keeps only what stayed: ${retained()}');
104+
untyped __cpp__("runJoined({0})", dropOld);
105+
lines.push('and is freed once they go: "${retained()}"');
106+
untyped __cpp__("runJoined({0})", dropAll);
107+
for (line in lines)
108+
Sys.println('jit $jit: $line');
109+
}
110+
}
111+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// A host interface the script classes implement
2+
interface Stepper {
3+
function step():Int;
4+
}

0 commit comments

Comments
 (0)