From 3ab68d67c1b5c6f81fbb74248bbb28b94b3d8b73 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 19 Nov 2026 09:12:22 -0500 Subject: [PATCH] Mark only what the collector made from a default closure A class's __register copies its vtable from an instance it makes on the stack, and that instance's constructor gives each dynamic function a default closure holding it. The closure is garbage at once, but a stale word a stack scan finds can still reach it, and marking it followed the stack address as if it were an object: it wrote a mark byte there, and faulted in a marking thread where the line marks it then read were not mapped. Whether it faults depends on the stack's layout. Making a thread's scratch buffer only at API level 500 changed that layout, and the release telemetry test, which uses haxe.Timer and its dynamic run, crashed on Windows x64 every run; stock hxcpp crashes the same way. The default closure now marks and visits its instance only when hx::IsCollectedAlloc finds it in memory the collector manages, which holds while it collects. Test: DefaultFuncForeignThis makes such an instance in zeroed memory outside the heap and keeps its default closure. Before the change a collection wrote 1 byte there, after it none, at 32 and 64 bits and with HXCPP_GC_MOVING, GENERATIONAL and DEBUG_ALWAYS_MOVE. The telemetry test passes 12 of 12, where it crashed 10 of 10. The GC, thread and Cppia regressions (100), test/haxe, the fuzz replay and the cppia suite pass on Windows x64. --- Changes.md | 1 + include/hx/GC.h | 4 ++ include/hx/Macros.h | 13 ++-- src/hx/gc/Immix.cpp | 5 ++ .../regression/DefaultFuncForeignThis/Main.hx | 59 +++++++++++++++++++ .../DefaultFuncForeignThis/build.hxml | 2 + .../DefaultFuncForeignThis/stdout.txt | 1 + 7 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 test/regression/DefaultFuncForeignThis/Main.hx create mode 100644 test/regression/DefaultFuncForeignThis/build.hxml create mode 100644 test/regression/DefaultFuncForeignThis/stdout.txt diff --git a/Changes.md b/Changes.md index 54466e163..a246bdd5c 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Fixed a collection that could fault in a marking thread, or write a mark byte into a thread's stack, in a program with a class that has a dynamic method. A class's registration copies its vtable from an instance made on the stack, whose constructor gives each dynamic method a default closure holding that instance; a stale word a stack scan found could reach the closure, and marking it followed the stack address as if it were an object. The default closure now marks only what the collector made. The telemetry test crashed this way on Windows x64 every run, and stock hxcpp does too * Fixed a Haxe 4 haxe.atomic.AtomicInt kept in a field reading freed memory after a collection: Haxe 4's AtomicInt on C++ is a cpp.Pointer into a one-element array nothing else holds, and the collector ignored cpp.Pointer fields, so it freed the array (HaxeFoundation/hxcpp#1225; Haxe 5 changed AtomicInt instead, in HaxeFoundation/haxe#12236). A cpp.Pointer field, or a pointer boxed in a Dynamic, now keeps what it points into in the GC heap, as a pointer on the stack does, and pins it under HXCPP_GC_MOVING. Of 10,000 atomics in fields, 9,999 read freed memory after a collection and none do now * Fixed HXCPP_GC_INCREMENTAL failing to compile ("Incremental GC requires regenerated C++") with libhxcpp installed from a release, 6.0.0-rc.2 among them: the release workflow rewrote extraParams.hxml with the version and dropped its other line, the macro that turns on the generated write barriers the incremental collector needs. Only the version lines are rewritten now * Fixed a change of link flags or libraries alone leaving the program as it was: the build tool linked again only when an object or library file had changed, so -D HXCPP_LINK_STACK_SIZE, HXCPP_LINK_NO_MEMORY_GROWTH, HXCPP_LINK_NO_EMBIND or HXCPP_LINK_NODERAWFS on an Emscripten build, which change nothing that is compiled, or a flag or library added to a target in Build.xml, did nothing until the objects changed. The flags, libraries and library paths are now part of what a link is checked against, so each program links once more after updating diff --git a/include/hx/GC.h b/include/hx/GC.h index 863a08e5b..d80487f15 100644 --- a/include/hx/GC.h +++ b/include/hx/GC.h @@ -205,6 +205,10 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx); // Keeps what a cpp.Pointer points into, as a pointer on the stack would void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx); +// Whether inPtr is in memory the collector manages. Only while it collects, +// when its lists of blocks and large allocations hold still +bool IsCollectedAlloc(const void *inPtr); + // Create/Remove a root. // All statics are explicitly registered - this saves adding the whole data segment diff --git a/include/hx/Macros.h b/include/hx/Macros.h index e7b20c990..76b491c6a 100644 --- a/include/hx/Macros.h +++ b/include/hx/Macros.h @@ -806,14 +806,19 @@ static ::Dynamic __##class##func(const ::Array< ::Dynamic> &inArgs) \ #define HX_DYNAMIC_CALL25(ret,func) HX_DYNAMIC_CALL(ret,func,HX_ARR_LIST25,HX_DYNAMIC_ARG_LIST25,HX_ARG_LIST25) #define HX_DYNAMIC_CALL26(ret,func) HX_DYNAMIC_CALL(ret,func,HX_ARR_LIST26,HX_DYNAMIC_ARG_LIST26,HX_ARG_LIST26) +// A class's __register copies its vtable from an instance it makes on the stack, +// whose constructor gives each dynamic function a default closure holding that +// instance. The closure is garbage at once, but a stale word a stack scan finds +// can still reach it, and marking it went on into the stack as if it were an +// object, writing mark bytes there. The closure marks only what the collector made #if (HXCPP_API_LEVEL>=500) #define HX_BEGIN_DEFAULT_FUNC(name,t0) \ namespace { \ struct name : public ::hx::Object { \ ::hx::ObjectPtr __this; \ name( ::hx::ObjectPtr __0 = ::null()) : __this(__0) {} \ - void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { HX_MARK_MEMBER(__this); } \ - void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { HX_VISIT_MEMBER(__this); } + void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_MARK_MEMBER(__this); } \ + void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_VISIT_MEMBER(__this); } #else #define HX_BEGIN_DEFAULT_FUNC(name,t0) \ namespace { \ @@ -821,8 +826,8 @@ static ::Dynamic __##class##func(const ::Array< ::Dynamic> &inArgs) \ HX_IS_INSTANCE_OF enum { _hx_ClassId = ::hx::clsIdClosure }; \ ::hx::ObjectPtr __this; \ name( ::hx::ObjectPtr __0 = ::null()) : __this(__0) {} \ - void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { HX_MARK_MEMBER(__this); } \ - void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { HX_VISIT_MEMBER(__this); } + void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_MARK_MEMBER(__this); } \ + void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_VISIT_MEMBER(__this); } #endif #define HX_END_DEFAULT_FUNC \ diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 96dd27863..0cb24d8b2 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -7994,6 +7994,11 @@ void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx) #endif } +bool IsCollectedAlloc(const void *inPtr) +{ + return inPtr && sGlobalAlloc->GetMemType(reinterpret_cast(inPtr))!=memUnmanaged; +} + } // namespace hx diff --git a/test/regression/DefaultFuncForeignThis/Main.hx b/test/regression/DefaultFuncForeignThis/Main.hx new file mode 100644 index 000000000..0f141bde0 --- /dev/null +++ b/test/regression/DefaultFuncForeignThis/Main.hx @@ -0,0 +1,59 @@ +// A class's __register copies its vtable from an instance it makes on the stack, +// and that instance's constructor gives each dynamic function a default closure +// holding it. A stale word on a stack can reach the closure, and marking it went +// on into the stack as if it were an object: it wrote a mark into the bytes +// before it, and faulted where the line marks it looked for were not mapped. The +// telemetry test crashed in a mark thread this way on Windows x64. +// +// Here the instance is made in zeroed memory the collector does not manage, as +// __register makes it on the stack, and its default closure is kept reachable. +// Collecting must leave that memory as it was. +class Owner { + public function new() {} + + public dynamic function run() {} +} + +@:headerCode('#include ') +@:cppFileCode(' +#include +#include +// Room for the instance and the bytes around it, which the collector reads as +// its header and the line marks of its block +alignas(64) static unsigned char sForeign[4096]; +static const int sAt = 2048; + +static ::Dynamic makeForeignOwner() +{ + memset(sForeign, 0, sizeof(sForeign)); + ::Owner_obj *owner = ::new (sForeign + sAt) ::Owner_obj(); + return owner->run; +} + +static int foreignBytesTouched() +{ + unsigned char copy[sizeof(::Owner_obj)]; + memcpy(copy, sForeign + sAt, sizeof(copy)); + int touched = 0; + for (int i = 0; i < sAt; i++) + if (sForeign[i]) + touched++; + for (int i = sAt + (int)sizeof(::Owner_obj); i < (int)sizeof(sForeign); i++) + if (sForeign[i]) + touched++; + return touched; +} +') +class Main { + static var kept:Dynamic; + + static function main() { + kept = untyped __cpp__("makeForeignOwner()"); + for (i in 0...3) { + var junk = [for (j in 0...10000) [j]]; + cpp.vm.Gc.run(true); + } + var touched:Int = untyped __cpp__("foreignBytesTouched()"); + Sys.println('bytes written around the instance: $touched'); + } +} diff --git a/test/regression/DefaultFuncForeignThis/build.hxml b/test/regression/DefaultFuncForeignThis/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/DefaultFuncForeignThis/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/DefaultFuncForeignThis/stdout.txt b/test/regression/DefaultFuncForeignThis/stdout.txt new file mode 100644 index 000000000..bcf9e98b4 --- /dev/null +++ b/test/regression/DefaultFuncForeignThis/stdout.txt @@ -0,0 +1 @@ +bytes written around the instance: 0