Skip to content

Mark only what the collector made from a default closure - #476

Merged
dimensionscape merged 1 commit into
mainfrom
fix/default-func-stack-this
Nov 20, 2026
Merged

dimensionscape merged 1 commit into
mainfrom
fix/default-func-stack-this

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

The main run on the merge of #449 failed in Haxe 4.3.7 / Windows 64 / gc and telemetry: the release telemetry test segfaulted, and it does so locally every run.

The cause is older than #449. 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, writing a mark byte into the stack and faulting 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 (in #449) changed that layout, and the telemetry test, which uses haxe.Timer and its dynamic run, crashed from then on. Old main with only that change crashes the same way, and so does stock hxcpp.

The default closure now marks and visits its instance only when hx::IsCollectedAlloc finds it in memory the collector manages.

Tests:

  • 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, HXCPP_GC_GENERATIONAL and HXCPP_GC_DEBUG_ALWAYS_MOVE.
  • test/telemetry in release passes 12 of 12, where it crashed 10 of 10; debug passes.
  • The GC, thread and Cppia regressions (100), test/haxe, the fuzz replay and the cppia suite under the interpreter and the JIT pass on Windows x64.

This blocks 6.0.0-rc.3, whose release workflow needs main green.

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.
@dimensionscape
dimensionscape merged commit ec235c6 into main Nov 20, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant