diff --git a/Changes.md b/Changes.md index 54466e16..a246bdd5 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 863a08e5..d80487f1 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 e7b20c99..76b491c6 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 96dd2786..0cb24d8b 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 00000000..0f141bde --- /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 00000000..b484e67e --- /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 00000000..bcf9e98b --- /dev/null +++ b/test/regression/DefaultFuncForeignThis/stdout.txt @@ -0,0 +1 @@ +bytes written around the instance: 0