From e01161aa66c0f0de5e23de10412978c917b4099b Mon Sep 17 00:00:00 2001 From: Jeremy Faivre Date: Sun, 22 Mar 2026 16:55:34 +0100 Subject: [PATCH 1/3] Add `HXCPP_IMMORTAL_STATICS` to heap-allocate static objects and never delete them (cherry picked from commit 69fa846e3d2f1409e99db393514cd297570886dd) --- include/hxcpp.h | 11 +++++++++++ src/hx/Debug.cpp | 4 ++-- src/hx/StdLibs.cpp | 2 +- src/hx/gc/Immix.cpp | 28 ++++++++++++++-------------- toolchain/common-defines.xml | 1 + 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/hxcpp.h b/include/hxcpp.h index 3942cb915..a3bedab01 100755 --- a/include/hxcpp.h +++ b/include/hxcpp.h @@ -385,5 +385,16 @@ typedef PropertyAccessMode PropertyAccess; #include #include +#ifdef HXCPP_IMMORTAL_STATICS +// Heap-allocate static objects and never delete them. Prevents static +// destruction order issues where CRT destroys objects in unpredictable +// order. OS reclaims all heap memory on process exit. +#define HX_IMMORTAL(Type, name) static Type &name = *new Type() +#define HX_IMMORTAL_GLOBAL(Type, name) Type &name = *new Type() +#else +#define HX_IMMORTAL(Type, name) static Type name +#define HX_IMMORTAL_GLOBAL(Type, name) Type name +#endif + #endif diff --git a/src/hx/Debug.cpp b/src/hx/Debug.cpp index c49cc935a..1b04de474 100644 --- a/src/hx/Debug.cpp +++ b/src/hx/Debug.cpp @@ -47,9 +47,9 @@ namespace hx const char* EXTERN_CLASS_NAME = "extern"; #ifdef HXCPP_STACK_IDS -static std::mutex sStackMapMutex; +HX_IMMORTAL(std::mutex, sStackMapMutex); typedef std::unordered_map StackMap; -static StackMap sStackMap; +HX_IMMORTAL(StackMap, sStackMap); #endif // User settable String->Void diff --git a/src/hx/StdLibs.cpp b/src/hx/StdLibs.cpp index 5126c1963..1e507b003 100644 --- a/src/hx/StdLibs.cpp +++ b/src/hx/StdLibs.cpp @@ -848,7 +848,7 @@ Dynamic __hxcpp_create_var_args(Dynamic &inArrayFunc) -static std::mutex sgFieldMapMutex; +HX_IMMORTAL(std::mutex, sgFieldMapMutex); typedef std::map StringToField; diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 367b2760a..1612611d1 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -48,7 +48,7 @@ namespace hx enum { gAlwaysMove = true }; typedef std::unordered_set PointerMovedSet; -PointerMovedSet sgPointerMoved; +HX_IMMORTAL_GLOBAL(PointerMovedSet, sgPointerMoved); #else enum { gAlwaysMove = false }; @@ -575,7 +575,7 @@ enum AllocType { allocNone, allocString, allocObject, allocMarked }; struct BlockDataInfo *gBlockStack = 0; typedef hx::QuickVec ObjectStack; -static std::mutex sThreadPoolLock; +HX_IMMORTAL(std::mutex, sThreadPoolLock); // For threaded marking/block reclaiming static unsigned int sRunningThreads = 0; @@ -690,7 +690,7 @@ struct GroupInfo }; -hx::QuickVec gAllocGroups; +HX_IMMORTAL_GLOBAL(hx::QuickVec, gAllocGroups); @@ -1779,7 +1779,7 @@ struct GlobalChunks }; -GlobalChunks sGlobalChunks; +HX_IMMORTAL_GLOBAL(GlobalChunks, sGlobalChunks); class MarkContext { @@ -2424,7 +2424,7 @@ class WeakRef; typedef hx::QuickVec WeakRefs; FILE_SCOPE std::mutex *sFinalizerLock = 0; -FILE_SCOPE WeakRefs sWeakRefs; +HX_IMMORTAL(WeakRefs, sWeakRefs); class WeakRef : public hx::Object { @@ -2457,26 +2457,26 @@ typedef hx::QuickVec FinalizerList; FILE_SCOPE FinalizerList *sgFinalizers = 0; typedef std::unordered_map FinalizerMap; -FILE_SCOPE FinalizerMap sFinalizerMap; +HX_IMMORTAL(FinalizerMap, sFinalizerMap); typedef void (*HaxeFinalizer)(Dynamic); typedef std::unordered_map HaxeFinalizerMap; -FILE_SCOPE HaxeFinalizerMap sHaxeFinalizerMap; +HX_IMMORTAL(HaxeFinalizerMap, sHaxeFinalizerMap); -hx::QuickVec sFreeObjectIds; +HX_IMMORTAL_GLOBAL(hx::QuickVec, sFreeObjectIds); typedef std::unordered_map ObjectIdMap; typedef hx::QuickVec IdObjectMap; -FILE_SCOPE ObjectIdMap sObjectIdMap; -FILE_SCOPE IdObjectMap sIdObjectMap; +HX_IMMORTAL(ObjectIdMap, sObjectIdMap); +HX_IMMORTAL(IdObjectMap, sIdObjectMap); typedef std::unordered_set MakeZombieSet; -FILE_SCOPE MakeZombieSet sMakeZombieSet; +HX_IMMORTAL(MakeZombieSet, sMakeZombieSet); typedef hx::QuickVec ZombieList; -FILE_SCOPE ZombieList sZombieList; +HX_IMMORTAL(ZombieList, sZombieList); typedef hx::QuickVec WeakHashList; -FILE_SCOPE WeakHashList sWeakHashList; +HX_IMMORTAL(WeakHashList, sWeakHashList); InternalFinalizer::InternalFinalizer(hx::Object *inObj, finalizer inFinalizer) @@ -2604,7 +2604,7 @@ struct Finalizable } }; typedef hx::QuickVec< Finalizable > FinalizableList; -FILE_SCOPE FinalizableList sFinalizableList; +HX_IMMORTAL(FinalizableList, sFinalizableList); static double tFinalizers; diff --git a/toolchain/common-defines.xml b/toolchain/common-defines.xml index e2f241bb4..564811b00 100644 --- a/toolchain/common-defines.xml +++ b/toolchain/common-defines.xml @@ -9,6 +9,7 @@ + From 22ab78c79a4d91d957b213509a7664881dedd0d5 Mon Sep 17 00:00:00 2001 From: Jeremy Faivre Date: Sun, 22 Mar 2026 19:59:04 +0100 Subject: [PATCH 2/3] Restore FILE_SCOPE on related statics, to make sure they are visible in debug builds (cherry picked from commit 34d8bc0981e89af135bccdcfeef9d4bcbbbc8cf8) --- include/hxcpp.h | 10 ++++++---- src/hx/gc/Immix.cpp | 18 +++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/include/hxcpp.h b/include/hxcpp.h index a3bedab01..5ee69053b 100755 --- a/include/hxcpp.h +++ b/include/hxcpp.h @@ -389,11 +389,13 @@ typedef PropertyAccessMode PropertyAccess; // Heap-allocate static objects and never delete them. Prevents static // destruction order issues where CRT destroys objects in unpredictable // order. OS reclaims all heap memory on process exit. -#define HX_IMMORTAL(Type, name) static Type &name = *new Type() -#define HX_IMMORTAL_GLOBAL(Type, name) Type &name = *new Type() +#define HX_IMMORTAL(Type, name) static Type &name = *new Type() +#define HX_IMMORTAL_GLOBAL(Type, name) Type &name = *new Type() +#define HX_IMMORTAL_FILE_SCOPE(Type, name) FILE_SCOPE Type &name = *new Type() #else -#define HX_IMMORTAL(Type, name) static Type name -#define HX_IMMORTAL_GLOBAL(Type, name) Type name +#define HX_IMMORTAL(Type, name) static Type name +#define HX_IMMORTAL_GLOBAL(Type, name) Type name +#define HX_IMMORTAL_FILE_SCOPE(Type, name) FILE_SCOPE Type name #endif #endif diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 1612611d1..f224a610f 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -2424,7 +2424,7 @@ class WeakRef; typedef hx::QuickVec WeakRefs; FILE_SCOPE std::mutex *sFinalizerLock = 0; -HX_IMMORTAL(WeakRefs, sWeakRefs); +HX_IMMORTAL_FILE_SCOPE(WeakRefs, sWeakRefs); class WeakRef : public hx::Object { @@ -2457,26 +2457,26 @@ typedef hx::QuickVec FinalizerList; FILE_SCOPE FinalizerList *sgFinalizers = 0; typedef std::unordered_map FinalizerMap; -HX_IMMORTAL(FinalizerMap, sFinalizerMap); +HX_IMMORTAL_FILE_SCOPE(FinalizerMap, sFinalizerMap); typedef void (*HaxeFinalizer)(Dynamic); typedef std::unordered_map HaxeFinalizerMap; -HX_IMMORTAL(HaxeFinalizerMap, sHaxeFinalizerMap); +HX_IMMORTAL_FILE_SCOPE(HaxeFinalizerMap, sHaxeFinalizerMap); HX_IMMORTAL_GLOBAL(hx::QuickVec, sFreeObjectIds); typedef std::unordered_map ObjectIdMap; typedef hx::QuickVec IdObjectMap; -HX_IMMORTAL(ObjectIdMap, sObjectIdMap); -HX_IMMORTAL(IdObjectMap, sIdObjectMap); +HX_IMMORTAL_FILE_SCOPE(ObjectIdMap, sObjectIdMap); +HX_IMMORTAL_FILE_SCOPE(IdObjectMap, sIdObjectMap); typedef std::unordered_set MakeZombieSet; -HX_IMMORTAL(MakeZombieSet, sMakeZombieSet); +HX_IMMORTAL_FILE_SCOPE(MakeZombieSet, sMakeZombieSet); typedef hx::QuickVec ZombieList; -HX_IMMORTAL(ZombieList, sZombieList); +HX_IMMORTAL_FILE_SCOPE(ZombieList, sZombieList); typedef hx::QuickVec WeakHashList; -HX_IMMORTAL(WeakHashList, sWeakHashList); +HX_IMMORTAL_FILE_SCOPE(WeakHashList, sWeakHashList); InternalFinalizer::InternalFinalizer(hx::Object *inObj, finalizer inFinalizer) @@ -2604,7 +2604,7 @@ struct Finalizable } }; typedef hx::QuickVec< Finalizable > FinalizableList; -HX_IMMORTAL(FinalizableList, sFinalizableList); +HX_IMMORTAL_FILE_SCOPE(FinalizableList, sFinalizableList); static double tFinalizers; From bad8984a53f62fe0d7dcec1fa746cd1ee6dd54fc Mon Sep 17 00:00:00 2001 From: Jeremy Faivre Date: Sun, 25 Oct 2026 13:52:41 +0100 Subject: [PATCH 3/3] Cover the remaining statics with non-trivial destructors under HXCPP_IMMORTAL_STATICS Threads: the ThreadLocal slot allocator behind Haxe 5's sys.thread.Tls uses a static mutex and a static list, and Impl::finalise runs from the GC, which can still collect while the process exits. A worker creating Tls values while main returns crashed 27 times in 200 runs, and still 2 times in 200 with the define before this change. Adds HX_IMMORTAL_STATIC_DECL for static data members. Lib, CFFI: module, primitive and kind maps. The kind maps are created on first use, so an extension calling hxcpp_kind_share from a static initializer never sees an unconstructed map. Debugger, telemetry, profiler, cppia host, init return buffers: the rest. With the define, the only exit-time destructor left in the runtime is the per-thread ThreadInfoHolder, which only removes a GC root. --- include/hxcpp.h | 4 ++++ src/hx/CFFI.cpp | 26 ++++++++++++++++++++------ src/hx/Debugger.cpp | 8 +++++--- src/hx/Lib.cpp | 8 ++++---- src/hx/Profiler.cpp | 4 ++-- src/hx/RunLibs.cpp | 2 +- src/hx/StdLibs.cpp | 2 +- src/hx/Telemetry.cpp | 18 ++++++++++-------- src/hx/TelemetryTracy.cpp | 6 +++--- src/hx/cppia/CppiaModule.cpp | 2 +- src/hx/cppia/CppiaVars.cpp | 3 ++- src/hx/thread/ThreadLocal.cpp | 12 ++++++++---- 12 files changed, 61 insertions(+), 34 deletions(-) diff --git a/include/hxcpp.h b/include/hxcpp.h index 5ee69053b..671e12984 100755 --- a/include/hxcpp.h +++ b/include/hxcpp.h @@ -392,10 +392,14 @@ typedef PropertyAccessMode PropertyAccess; #define HX_IMMORTAL(Type, name) static Type &name = *new Type() #define HX_IMMORTAL_GLOBAL(Type, name) Type &name = *new Type() #define HX_IMMORTAL_FILE_SCOPE(Type, name) FILE_SCOPE Type &name = *new Type() +// Static data member of a class: declare with this inside the class, then define +// it outside with HX_IMMORTAL_GLOBAL(Type, Owner::name) +#define HX_IMMORTAL_STATIC_DECL(Type, name) static Type &name #else #define HX_IMMORTAL(Type, name) static Type name #define HX_IMMORTAL_GLOBAL(Type, name) Type name #define HX_IMMORTAL_FILE_SCOPE(Type, name) FILE_SCOPE Type name +#define HX_IMMORTAL_STATIC_DECL(Type, name) static Type name #endif #endif diff --git a/src/hx/CFFI.cpp b/src/hx/CFFI.cpp index 264447c82..78f3d2494 100644 --- a/src/hx/CFFI.cpp +++ b/src/hx/CFFI.cpp @@ -153,8 +153,21 @@ static int sgKinds = (int)(vtAbstractBase + 5); typedef std::map KindMap; typedef std::map ReverseKindMap; -static KindMap sgKindMap; -static ReverseKindMap sgReverseKindMap; + +// Created on first use rather than at load time, so that an extension calling +// hxcpp_kind_share from a static initializer never sees an unconstructed map +// (with HXCPP_IMMORTAL_STATICS these maps are initialized dynamically) +static KindMap &GetKindMap() +{ + HX_IMMORTAL(KindMap, sgKindMap); + return sgKindMap; +} + +static ReverseKindMap &GetReverseKindMap() +{ + HX_IMMORTAL(ReverseKindMap, sgReverseKindMap); + return sgReverseKindMap; +} int hxcpp_alloc_kind() @@ -165,11 +178,11 @@ int hxcpp_alloc_kind() void hxcpp_kind_share(int &ioKind,const char *inName) { - int &kind = sgKindMap[inName]; + int &kind = GetKindMap()[inName]; if (kind==0) kind = hxcpp_alloc_kind(); ioKind = kind; - sgReverseKindMap[kind] = inName; + GetReverseKindMap()[kind] = inName; } String __hxcpp_get_kind(Dynamic inObject) @@ -179,8 +192,9 @@ String __hxcpp_get_kind(Dynamic inObject) return null(); if (type==(int)(size_t)k_cpp_pointer) return HX_CSTRING("cpp.Pointer"); - ReverseKindMap::const_iterator it = sgReverseKindMap.find(type); - if (it==sgReverseKindMap.end()) + const ReverseKindMap &reverseKindMap = GetReverseKindMap(); + ReverseKindMap::const_iterator it = reverseKindMap.find(type); + if (it==reverseKindMap.end()) return null(); return String::create(it->second.c_str(), it->second.size()); } diff --git a/src/hx/Debugger.cpp b/src/hx/Debugger.cpp index 1dce785dd..44415ab45 100644 --- a/src/hx/Debugger.cpp +++ b/src/hx/Debugger.cpp @@ -85,9 +85,11 @@ static void read_memory_barrier() const char *_hx_dbg_find_scriptable_class_name(String className); -static std::mutex gMutex; -static std::map gMap; -static std::list gList; +typedef std::map DebuggerContextMap; +typedef std::list DebuggerContextList; +HX_IMMORTAL(std::mutex, gMutex); +HX_IMMORTAL(DebuggerContextMap, gMap); +HX_IMMORTAL(DebuggerContextList, gList); class Breakpoints; Breakpoints *ReleaseBreakpointsLocked(Breakpoints *inBreakpoints); diff --git a/src/hx/Lib.cpp b/src/hx/Lib.cpp index e9770b7fa..0cc07712a 100644 --- a/src/hx/Lib.cpp +++ b/src/hx/Lib.cpp @@ -102,9 +102,9 @@ void hxFreeLibrary(Module inModule) { dlclose(inModule); } typedef std::map LoadedModule; -static LoadedModule sgLoadedModule; +HX_IMMORTAL(LoadedModule, sgLoadedModule); typedef std::vector ModuleList; -static ModuleList sgOrderedModules; +HX_IMMORTAL(ModuleList, sgOrderedModules); typedef hx::Object * (*prim_0)(); typedef hx::Object * (*prim_1)(hx::Object *); @@ -244,7 +244,7 @@ class ExternalPrimitive : public hx::Object namespace { typedef std::map LoadedMap; -LoadedMap sLoadedMap; +HX_IMMORTAL_GLOBAL(LoadedMap, sLoadedMap); } @@ -382,7 +382,7 @@ RegistrationMap *sgRegisteredPrims=0; -static std::vector sgLibPath; +HX_IMMORTAL(std::vector, sgLibPath); static bool sgLibPathIsInit = false; diff --git a/src/hx/Profiler.cpp b/src/hx/Profiler.cpp index b266041bc..9210436a4 100644 --- a/src/hx/Profiler.cpp +++ b/src/hx/Profiler.cpp @@ -232,11 +232,11 @@ class Profiler int mT0; std::map mProfileStats; - static std::mutex gThreadMutex; + HX_IMMORTAL_STATIC_DECL(std::mutex, gThreadMutex); static int gThreadRefCount; static int gProfileClock; }; -/* static */ std::mutex Profiler::gThreadMutex; +/* static */ HX_IMMORTAL_GLOBAL(std::mutex, Profiler::gThreadMutex); /* static */ int Profiler::gThreadRefCount; /* static */ int Profiler::gProfileClock; diff --git a/src/hx/RunLibs.cpp b/src/hx/RunLibs.cpp index 808c88b9e..f9496b1c2 100644 --- a/src/hx/RunLibs.cpp +++ b/src/hx/RunLibs.cpp @@ -10,7 +10,7 @@ extern "C" void __hxcpp_lib_main(); -std::string sgResultBuffer; +HX_IMMORTAL_GLOBAL(std::string, sgResultBuffer); HXCPP_EXTERN_CLASS_ATTRIBUTES const HX_CHAR *hxRunLibrary() { diff --git a/src/hx/StdLibs.cpp b/src/hx/StdLibs.cpp index 1e507b003..8b5cad174 100644 --- a/src/hx/StdLibs.cpp +++ b/src/hx/StdLibs.cpp @@ -192,7 +192,7 @@ Array __hxcpp_resource_bytes(String inName) extern "C" void __hxcpp_lib_main(); namespace hx { - static std::string initReturnBuffer; + HX_IMMORTAL(std::string, initReturnBuffer); const char *Init(bool stayAttached) { try diff --git a/src/hx/Telemetry.cpp b/src/hx/Telemetry.cpp index 3dd017322..ad17c1e1a 100644 --- a/src/hx/Telemetry.cpp +++ b/src/hx/Telemetry.cpp @@ -328,20 +328,22 @@ class Telemetry std::vector *allocation_data; - static std::recursive_mutex gStashMutex; - static std::recursive_mutex gThreadMutex; + typedef std::map AllocMap; + + HX_IMMORTAL_STATIC_DECL(std::recursive_mutex, gStashMutex); + HX_IMMORTAL_STATIC_DECL(std::recursive_mutex, gThreadMutex); static int gThreadRefCount; static int gProfileClock; - static std::recursive_mutex alloc_mutex; - static std::map alloc_map; + HX_IMMORTAL_STATIC_DECL(std::recursive_mutex, alloc_mutex); + HX_IMMORTAL_STATIC_DECL(AllocMap, alloc_map); }; -/* static */ std::recursive_mutex Telemetry::gStashMutex; -/* static */ std::recursive_mutex Telemetry::gThreadMutex; +/* static */ HX_IMMORTAL_GLOBAL(std::recursive_mutex, Telemetry::gStashMutex); +/* static */ HX_IMMORTAL_GLOBAL(std::recursive_mutex, Telemetry::gThreadMutex); /* static */ int Telemetry::gThreadRefCount; /* static */ int Telemetry::gProfileClock; -/* static */ std::recursive_mutex Telemetry::alloc_mutex; -/* static */ std::map Telemetry::alloc_map; +/* static */ HX_IMMORTAL_GLOBAL(std::recursive_mutex, Telemetry::alloc_mutex); +/* static */ HX_IMMORTAL_GLOBAL(Telemetry::AllocMap, Telemetry::alloc_map); diff --git a/src/hx/TelemetryTracy.cpp b/src/hx/TelemetryTracy.cpp index e73d1a4c3..e128595a0 100644 --- a/src/hx/TelemetryTracy.cpp +++ b/src/hx/TelemetryTracy.cpp @@ -13,9 +13,9 @@ namespace const char* sohName = "Small Object Heap"; const char* lohName = "Large Object Heap"; - std::vector created; - hx::QuickVec largeAllocs; - std::mutex largeAllocsLock; + HX_IMMORTAL_GLOBAL(std::vector, created); + HX_IMMORTAL_GLOBAL(hx::QuickVec, largeAllocs); + HX_IMMORTAL_GLOBAL(std::mutex, largeAllocsLock); bool isLargeObject(void* ptr) { diff --git a/src/hx/cppia/CppiaModule.cpp b/src/hx/cppia/CppiaModule.cpp index 2d029c52b..0fc6e2cc1 100644 --- a/src/hx/cppia/CppiaModule.cpp +++ b/src/hx/cppia/CppiaModule.cpp @@ -16,7 +16,7 @@ int sgNativeNameSlotCount = 0; Array gAllCppiaModules; -std::vector scriptResources; +HX_IMMORTAL_GLOBAL(std::vector, scriptResources); diff --git a/src/hx/cppia/CppiaVars.cpp b/src/hx/cppia/CppiaVars.cpp index aea9eb863..8b3803a60 100644 --- a/src/hx/cppia/CppiaVars.cpp +++ b/src/hx/cppia/CppiaVars.cpp @@ -326,7 +326,8 @@ void CppiaVar::visit(hx::VisitContext *__inCtx) // --- CppiaStackVar ---- -std::map sVarIdNameMap; +typedef std::map VarIdNameMap; +HX_IMMORTAL_GLOBAL(VarIdNameMap, sVarIdNameMap); int getStackVarNameId(int inVarId) diff --git a/src/hx/thread/ThreadLocal.cpp b/src/hx/thread/ThreadLocal.cpp index 9480101fb..f9d3af245 100644 --- a/src/hx/thread/ThreadLocal.cpp +++ b/src/hx/thread/ThreadLocal.cpp @@ -9,8 +9,12 @@ struct hx::thread::ThreadLocal_obj::Impl { - static std::mutex slotLock; - static std::list slots; + typedef std::list SlotList; + + // finalise() runs from the GC, which may still collect while the process exits: + // these must outlive the static destructors (see HXCPP_IMMORTAL_STATICS) + HX_IMMORTAL_STATIC_DECL(std::mutex, slotLock); + HX_IMMORTAL_STATIC_DECL(SlotList, slots); static int counter; const int slot; @@ -45,9 +49,9 @@ struct hx::thread::ThreadLocal_obj::Impl } }; -std::mutex hx::thread::ThreadLocal_obj::Impl::slotLock; +HX_IMMORTAL_GLOBAL(std::mutex, hx::thread::ThreadLocal_obj::Impl::slotLock); -std::list hx::thread::ThreadLocal_obj::Impl::slots; +HX_IMMORTAL_GLOBAL(hx::thread::ThreadLocal_obj::Impl::SlotList, hx::thread::ThreadLocal_obj::Impl::slots); int hx::thread::ThreadLocal_obj::Impl::counter = 0;