From 5fe3868d8b06a6743c3f4f714ce4b0f963fb4b02 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Sat, 28 Nov 2026 15:49:37 -0500 Subject: [PATCH] Clear the stack before CppiaAdoptInstances counts what a module keeps CppiaAdoptInstances failed once in the ASan job on Linux, on a pull request whose changes were all in the build tool, with "the old module keeps only what stayed: Reshaped x1, closures x2" in the JIT pass, where one closure stays. The next count, after the old values are dropped, was right, so nothing leaked. The census collection that counts what a released module keeps scans the frames it runs in for pointers, and a slot that nothing has written yet still holds what an earlier call left there. When that is the address of an object freed since, whose memory a closure of the old module now uses, the census counts the closure. Running every script call on a joined worker (cce2fed2) kept the workers' values off the main thread's stack, but not the main thread's own leftovers. Planting the address of a dropped closure of the old module across the stack below main's frame before the count reproduces the line exactly, in both passes. main now clears 64 KB of stack below its frame before each count, so the frames the census scans start from zeros. main's own frame is scanned at every collection, so what it points to is never freed and cannot become a closure. Tests: on Windows x64 with MSVC 14.50 and Haxe 4.3.7, CppiaAdoptInstances matches stdout.txt in 40 runs, and in 25 under -D HXCPP_SANITIZE=address, where the old test also passed 25 times. With the dropped closure's address planted below main's frame, the old test prints closures x2 and the new one matches. Keeping a second closure of the old module for real still fails it, with closures x2 and the module not freed. The Linux ASan job has not run it yet. --- test/regression/CppiaAdoptInstances/Main.hx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/regression/CppiaAdoptInstances/Main.hx b/test/regression/CppiaAdoptInstances/Main.hx index c644647a0..1f42b21dd 100644 --- a/test/regression/CppiaAdoptInstances/Main.hx +++ b/test/regression/CppiaAdoptInstances/Main.hx @@ -10,7 +10,12 @@ import cpp.cppia.Module; // // Every call into script code runs on a joined native worker, so stale values // it leaves on a stack do not decide what keeps a module loaded. The main -// thread only collects and prints. +// thread only collects and prints. The collection that counts what a module +// keeps scans the frames it runs in for pointers, and a slot nothing has +// written yet still holds what an earlier call left there, which can be the +// address of an object freed since and of a closure of the old module now: +// once, under ASan on Linux, the count found two closures where one stayed. +// So the stack below main's frame is cleared before each count @:cppFileCode(' #include #include @@ -19,6 +24,16 @@ static void runJoined(Dynamic callback) { hx::AutoGCFreeZone freeZone; worker.join(); } +#ifdef _MSC_VER +__declspec(noinline) +#else +__attribute__((noinline)) +#endif +static void clearStack() { + void * volatile area[8192]; + for (int i = 0; i < 8192; i++) + area[i] = 0; +} ') class Main { static var dir:String; @@ -100,8 +115,10 @@ class Main { untyped __cpp__("runJoined({0})", adopt); untyped __cpp__("runJoined({0})", useMoved); untyped __cpp__("runJoined({0})", useOld); + untyped __cpp__("clearStack()"); lines.push('the old module keeps only what stayed: ${retained()}'); untyped __cpp__("runJoined({0})", dropOld); + untyped __cpp__("clearStack()"); lines.push('and is freed once they go: "${retained()}"'); untyped __cpp__("runJoined({0})", dropAll); for (line in lines)