Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions include/hx/GC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions include/hx/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -806,23 +806,28 @@ 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<t0> __this; \
name( ::hx::ObjectPtr<t0> __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 { \
struct name : public ::hx::Object { int __GetType() const HXCPP_OVERRIDE { return vtFunction; } \
HX_IS_INSTANCE_OF enum { _hx_ClassId = ::hx::clsIdClosure }; \
::hx::ObjectPtr<t0> __this; \
name( ::hx::ObjectPtr<t0> __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 \
Expand Down
5 changes: 5 additions & 0 deletions src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7994,6 +7994,11 @@ void MarkConservativePointer(void *inPtr,hx::MarkContext *__inCtx)
#endif
}

bool IsCollectedAlloc(const void *inPtr)
{
return inPtr && sGlobalAlloc->GetMemType(reinterpret_cast<uintptr_t>(inPtr))!=memUnmanaged;
}

} // namespace hx


Expand Down
59 changes: 59 additions & 0 deletions test/regression/DefaultFuncForeignThis/Main.hx
Original file line number Diff line number Diff line change
@@ -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 <Owner.h>')
@:cppFileCode('
#include <new>
#include <string.h>
// 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');
}
}
2 changes: 2 additions & 0 deletions test/regression/DefaultFuncForeignThis/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
1 change: 1 addition & 0 deletions test/regression/DefaultFuncForeignThis/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bytes written around the instance: 0
Loading