Skip to content

Commit 5e19acf

Browse files
Tutez64dimensionscape
authored andcommitted
Mark every loaded cppia module on every collection.
A module is an ordinary GC object, so a generational collection stops traversing it once it is old. What it holds still changes after that: a literal boxes itself on first use, the JIT embeds that box, a script static is assigned. None of those writes has a barrier, so a minor collection freed the young object while the module still pointed at it, and the next use read reused memory. Modules are now marked with the class statics when the collector is generational. HaxeFoundation/hxcpp#1406 Fixes #27
1 parent 49eb7e2 commit 5e19acf

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎Changes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ The first libhxcpp version. It is based on hxcpp 4.3 (the changes listed under 4
192192
* Fixed building on Linux with musl, such as Alpine: Sys.cpp included xlocale.h, which musl doesn't have (hxcpp #1165)
193193
* Fixed HXCPP_GC_BIG_BLOCKS stalling or running out of memory once the heap passed 2 GiB: the large allocation target was computed in an int (hxcpp #1339)
194194
* Fixed a cppia script that constructs a class the host does not have crashing the process: it fails to load with Could not link new (hxcpp #1410)
195+
* Fixed cppia with HXCPP_GC_GENERATIONAL freeing young objects that an old script still used: a field set by name had no write barrier, and literals, JIT-embedded boxes and statics held by a loaded module were not traced by a generational collection (hxcpp #1408, #1406)
196+
* Fixed the cppia JIT allocating unaligned objects in the nursery, which a conservative scan could misread (hxcpp #1411)
195197
* 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
196198
* Added separate Linux ARMv7 and ARM64 build architectures (hxcpp #1355)
197199
* Fixed AndroidCompat.cpp closing its extern "C" section in the wrong place (hxcpp #1404)

‎src/hx/cppia/CppiaModule.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,39 @@ class CppiaObject : public hx::CppiaLoadedModule_obj
355355
};
356356

357357

358+
/*
359+
A loaded module is an ordinary GC object, so a generational collection stops traversing it once it
360+
is old. What it holds can still change after that: a literal boxes itself on first use, the JIT
361+
embeds that box in its code, a script static is assigned. None of those writes has a barrier, so a
362+
minor collection freed the young object while the old module still pointed at it.
363+
364+
So every loaded module is marked on every collection, the way class statics are.
365+
*/
366+
void scriptMarkModules(hx::MarkContext *__inCtx)
367+
{
368+
if (!gAllCppiaModules.mPtr)
369+
return;
370+
for(int i=0;i<gAllCppiaModules->length;i++)
371+
{
372+
CppiaObject *module = static_cast<CppiaObject *>(gAllCppiaModules[i].mPtr);
373+
if (module)
374+
module->cppia->mark(__inCtx);
375+
}
376+
}
377+
378+
#ifdef HXCPP_VISIT_ALLOCS
379+
void scriptVisitModules(hx::VisitContext *__inCtx)
380+
{
381+
if (!gAllCppiaModules.mPtr)
382+
return;
383+
for(int i=0;i<gAllCppiaModules->length;i++)
384+
{
385+
CppiaObject *module = static_cast<CppiaObject *>(gAllCppiaModules[i].mPtr);
386+
if (module)
387+
module->cppia->visit(__inCtx);
388+
}
389+
}
390+
#endif
358391

359392

360393
CppiaLoadedModule LoadCppia(const unsigned char *inData, int inDataLength)

‎src/hx/gc/Immix.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,10 @@ DECLARE_FAST_TLS_DATA(StackContext, tlsStackContext);
462462

463463
#ifdef HXCPP_SCRIPTABLE
464464
extern void scriptMarkStack(hx::MarkContext *);
465+
extern void scriptMarkModules(hx::MarkContext *);
466+
#ifdef HXCPP_VISIT_ALLOCS
467+
extern void scriptVisitModules(hx::VisitContext *);
468+
#endif
465469
#endif
466470
}
467471

@@ -4406,6 +4410,9 @@ class GlobalAllocator
44064410
VisitLocalAlloc(mLocalAllocs[i], inCtx);
44074411

44084412
hx::VisitClassStatics(inCtx);
4413+
#if defined(HXCPP_SCRIPTABLE) && defined(HXCPP_GC_GENERATIONAL)
4414+
hx::scriptVisitModules(inCtx);
4415+
#endif
44094416

44104417
{
44114418
// For the walks only: see sGCRootLock. The visit just patches moved
@@ -4856,6 +4863,9 @@ class GlobalAllocator
48564863
mMarker.init();
48574864

48584865
hx::MarkClassStatics(&mMarker);
4866+
#if defined(HXCPP_SCRIPTABLE) && defined(HXCPP_GC_GENERATIONAL)
4867+
hx::scriptMarkModules(&mMarker);
4868+
#endif
48594869

48604870
{
48614871
hx::AutoMarkPush info(&mMarker,"Roots","root");

0 commit comments

Comments
 (0)