Repository navigation
Copy a cppia closure's captures out to mark and visit them - #498
Merged
Merged
Conversation
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
force-pushed
the
fix/cppia-closure-capture-alignment
branch
from
November 27, 2026 22:06
9afeaaf to
4349d1e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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::markClosureandvisitClosuremarked 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
getSlotto mark or visit it, and the visit writes it back withsetSlotwhen 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
CppiaClosureobjects, built bycreateClosure, and the constructor,createClosureand the call all copy captures with memcpy. Marking and visiting were the only in-place reads.Test:
CppiaClosureCaptureAlignmentmakes 20000 closures that capture an Int, then a String and an Array, keeps one in fifty, collects withHXCPP_GC_DEBUG_ALWAYS_MOVE, refills the heap, and checks every kept closure still reads its captures. It runs interpreted and jitted.Checked locally (Windows, MSVC, 64-bit):
test/cppiareports ALL TESTS OK with and without-jit.There is no clang or UBSan here, so the
check-sanitizersjob in CI is the real check for the alignment itself.Related: #496 fixes the same kind of read in
CppiaStackVar::setDefault, and #492 fixedcreateEnum. This is rebased onto main at 150bdef, and itsChanges.mdline is underlibhxcpp 6.0.3 (unreleased). #496 adds a line in the same place, so whichever lands second needs a one-line rebase there.