Skip to content

Commit ec235c6

Browse files
Mark only what the collector made from a default closure (#476)
A class's __register copies its vtable from an instance made on the stack, whose constructor gives each dynamic function a default closure holding it. A stale word a stack scan found could reach that closure, and marking it followed the stack address as if it were an object, which crashed the release telemetry test on Windows x64. The default closure now marks and visits its instance only when the collector manages that memory.
2 parents 389df24 + 3ab68d6 commit ec235c6

7 files changed

Lines changed: 81 additions & 4 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+
* 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
56
* 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
67
* 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
78
* 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

‎include/hx/GC.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx);
205205
// Keeps what a cpp.Pointer points into, as a pointer on the stack would
206206
void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx);
207207

208+
// Whether inPtr is in memory the collector manages. Only while it collects,
209+
// when its lists of blocks and large allocations hold still
210+
bool IsCollectedAlloc(const void *inPtr);
211+
208212

209213
// Create/Remove a root.
210214
// All statics are explicitly registered - this saves adding the whole data segment

‎include/hx/Macros.h‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,23 +806,28 @@ static ::Dynamic __##class##func(const ::Array< ::Dynamic> &inArgs) \
806806
#define HX_DYNAMIC_CALL25(ret,func) HX_DYNAMIC_CALL(ret,func,HX_ARR_LIST25,HX_DYNAMIC_ARG_LIST25,HX_ARG_LIST25)
807807
#define HX_DYNAMIC_CALL26(ret,func) HX_DYNAMIC_CALL(ret,func,HX_ARR_LIST26,HX_DYNAMIC_ARG_LIST26,HX_ARG_LIST26)
808808

809+
// A class's __register copies its vtable from an instance it makes on the stack,
810+
// whose constructor gives each dynamic function a default closure holding that
811+
// instance. The closure is garbage at once, but a stale word a stack scan finds
812+
// can still reach it, and marking it went on into the stack as if it were an
813+
// object, writing mark bytes there. The closure marks only what the collector made
809814
#if (HXCPP_API_LEVEL>=500)
810815
#define HX_BEGIN_DEFAULT_FUNC(name,t0) \
811816
namespace { \
812817
struct name : public ::hx::Object { \
813818
::hx::ObjectPtr<t0> __this; \
814819
name( ::hx::ObjectPtr<t0> __0 = ::null()) : __this(__0) {} \
815-
void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { HX_MARK_MEMBER(__this); } \
816-
void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { HX_VISIT_MEMBER(__this); }
820+
void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_MARK_MEMBER(__this); } \
821+
void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_VISIT_MEMBER(__this); }
817822
#else
818823
#define HX_BEGIN_DEFAULT_FUNC(name,t0) \
819824
namespace { \
820825
struct name : public ::hx::Object { int __GetType() const HXCPP_OVERRIDE { return vtFunction; } \
821826
HX_IS_INSTANCE_OF enum { _hx_ClassId = ::hx::clsIdClosure }; \
822827
::hx::ObjectPtr<t0> __this; \
823828
name( ::hx::ObjectPtr<t0> __0 = ::null()) : __this(__0) {} \
824-
void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { HX_MARK_MEMBER(__this); } \
825-
void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { HX_VISIT_MEMBER(__this); }
829+
void __Mark( ::hx::MarkContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_MARK_MEMBER(__this); } \
830+
void __Visit( ::hx::VisitContext *__inCtx) HXCPP_OVERRIDE { if ( ::hx::IsCollectedAlloc(__this.mPtr)) HX_VISIT_MEMBER(__this); }
826831
#endif
827832

828833
#define HX_END_DEFAULT_FUNC \

‎src/hx/gc/Immix.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7994,6 +7994,11 @@ void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx)
79947994
#endif
79957995
}
79967996

7997+
bool IsCollectedAlloc(const void *inPtr)
7998+
{
7999+
return inPtr && sGlobalAlloc->GetMemType(reinterpret_cast<uintptr_t>(inPtr))!=memUnmanaged;
8000+
}
8001+
79978002
} // namespace hx
79988003

79998004

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// A class's __register copies its vtable from an instance it makes on the stack,
2+
// and that instance's constructor gives each dynamic function a default closure
3+
// holding it. A stale word on a stack can reach the closure, and marking it went
4+
// on into the stack as if it were an object: it wrote a mark into the bytes
5+
// before it, and faulted where the line marks it looked for were not mapped. The
6+
// telemetry test crashed in a mark thread this way on Windows x64.
7+
//
8+
// Here the instance is made in zeroed memory the collector does not manage, as
9+
// __register makes it on the stack, and its default closure is kept reachable.
10+
// Collecting must leave that memory as it was.
11+
class Owner {
12+
public function new() {}
13+
14+
public dynamic function run() {}
15+
}
16+
17+
@:headerCode('#include <Owner.h>')
18+
@:cppFileCode('
19+
#include <new>
20+
#include <string.h>
21+
// Room for the instance and the bytes around it, which the collector reads as
22+
// its header and the line marks of its block
23+
alignas(64) static unsigned char sForeign[4096];
24+
static const int sAt = 2048;
25+
26+
static ::Dynamic makeForeignOwner()
27+
{
28+
memset(sForeign, 0, sizeof(sForeign));
29+
::Owner_obj *owner = ::new (sForeign + sAt) ::Owner_obj();
30+
return owner->run;
31+
}
32+
33+
static int foreignBytesTouched()
34+
{
35+
unsigned char copy[sizeof(::Owner_obj)];
36+
memcpy(copy, sForeign + sAt, sizeof(copy));
37+
int touched = 0;
38+
for (int i = 0; i < sAt; i++)
39+
if (sForeign[i])
40+
touched++;
41+
for (int i = sAt + (int)sizeof(::Owner_obj); i < (int)sizeof(sForeign); i++)
42+
if (sForeign[i])
43+
touched++;
44+
return touched;
45+
}
46+
')
47+
class Main {
48+
static var kept:Dynamic;
49+
50+
static function main() {
51+
kept = untyped __cpp__("makeForeignOwner()");
52+
for (i in 0...3) {
53+
var junk = [for (j in 0...10000) [j]];
54+
cpp.vm.Gc.run(true);
55+
}
56+
var touched:Int = untyped __cpp__("foreignBytesTouched()");
57+
Sys.println('bytes written around the instance: $touched');
58+
}
59+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-m Main
2+
--cpp bin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bytes written around the instance: 0

0 commit comments

Comments
 (0)