Skip to content

Copy a cppia closure's captures out to mark and visit them - #498

Merged
dimensionscape merged 2 commits into
mainfrom
fix/cppia-closure-capture-alignment
Nov 28, 2026
Merged

dimensionscape merged 2 commits into
mainfrom
fix/cppia-closure-capture-alignment

Conversation

@dimensionscape

@dimensionscape dimensionscape commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

A cppia closure stores its captured variables after the object, packed at 4-byte offsets like a frame, after the 8-byte this. On 64-bit a String or object captured after an Int sits at an odd multiple of 4. CppiaStackVar::markClosure and visitClosure marked and visited those captures in place, which is undefined behaviour and a fault on a CPU that requires alignment.

They now copy each capture out with getSlot to mark or visit it, and the visit writes it back with setSlot when the moving collector moved it.

Confirmed: a temporary print in both functions showed a String capture at offset 12 and an object capture at offset 28, both at 4 modulo 8 on 64-bit Windows, with the JIT off and on.

JIT: not separately affected. Jitted closures are the same CppiaClosure objects, built by createClosure, and the constructor, createClosure and the call all copy captures with memcpy. Marking and visiting were the only in-place reads.

Test: CppiaClosureCaptureAlignment makes 20000 closures that capture an Int, then a String and an Array, keeps one in fifty, collects with HXCPP_GC_DEBUG_ALWAYS_MOVE, refills the heap, and checks every kept closure still reads its captures. It runs interpreted and jitted.

  • x86 tolerates the unaligned access, so the test also passes before the fix. UBSan is what reports the bug.
  • With the write-back removed the test crashes, so it does cover the visit path.

Checked locally (Windows, MSVC, 64-bit):

  • All 75 cppia regression tests pass.
  • test/cppia reports ALL TESTS OK with and without -jit.

There is no clang or UBSan here, so the check-sanitizers job in CI is the real check for the alignment itself.

Related: #496 fixes the same kind of read in CppiaStackVar::setDefault, and #492 fixed createEnum. This is rebased onto main at 150bdef, and its Changes.md line is under libhxcpp 6.0.3 (unreleased). #496 adds a line in the same place, so whichever lands second needs a one-line rebase there.

A cppia closure stores the variables it captures after the object,
packed at 4-byte offsets as a frame is, after the 8-byte this. On 64-bit
a String or object captured after an Int, as text in
var count = 1; var text = "x"; function() return count + text, sits at
an odd multiple of 4. The collector's markClosure and visitClosure read
those captures in place: undefined behaviour, and a fault on a CPU that
requires alignment. Everything else that touches captures, the
constructor, the call and the JIT's createClosure, already copies them
with memcpy.

A temporary print in both functions showed a String capture at offset
12 and an object capture at offset 28, both at 4 modulo 8 on 64-bit
Windows, interpreted and under the JIT. Marking and visiting now copy
each capture out with getSlot, and the visit writes it back with
setSlot when the moving collector moved it.

CppiaClosureCaptureAlignment keeps one closure in fifty of 20000 that
capture an Int, a String and an Array, collects with
HXCPP_GC_DEBUG_ALWAYS_MOVE so the collector moves what they captured,
and checks every kept closure still reads its captures. x86 tolerates
the unaligned access, so the test passes before the fix too; UBSan
reports it. Without the write-back it crashes.

Tests: CppiaClosureCaptureAlignment
@dimensionscape
dimensionscape force-pushed the fix/cppia-closure-capture-alignment branch from 9afeaaf to 4349d1e Compare November 27, 2026 22:06
@dimensionscape
dimensionscape merged commit 60b1e03 into main Nov 28, 2026
6 of 14 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