Skip to content

Make the generational write barrier's mark byte accesses atomic - #479

Closed
dimensionscape wants to merge 1 commit into
mainfrom
fix/young-log-race
Closed

dimensionscape wants to merge 1 commit into
mainfrom
fix/young-log-race

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

The TSan check on #474 reported a data race in GenerationalArrayYoungLog, a test already on main: under HXCPP_GC_GENERATIONAL, two threads storing into one old array read and set the array's mark byte with plain accesses, one in YoungStore setting the remembered bit and the other in the write barrier inlined into the generated code. Either value read is handled and both only set the same bit, but it was a data race, and any PR's TSan run could hit it.

The barrier reads the mark byte with a relaxed atomic load, which is the same plain byte load where it compiles to one, and sets the remembered bit with an atomic or, through hx::BarrierMarkByte and hx::BarrierRemember in GC.h. The barrier macros, YoungStore, the hash maps' barrier and the cppia JIT's pushWriteBarrier use them, and the young log's count is read and written with relaxed atomics.

Run on Windows x64: the 12 Generational regression tests, a cppia JIT test and gc stress, all built generational, and a default build. The TSan job here is the check for the race itself.

Under HXCPP_GC_GENERATIONAL the write barrier reads an object's mark
byte, and its slow path sets the remembered bit in it. Two threads
storing into one old array at once did both with plain accesses, so
one could set the bit while the other read the byte: ThreadSanitizer
reported it in GenerationalArrayYoungLog, between YoungStore and the
barrier inlined into the test's generated code. Either value read is
handled, and both threads only set the same bit, but it was a data
race all the same. So was the young log's count, ioLogged, which the
code already says may lose updates between threads.

hx::BarrierMarkByte reads the byte with a relaxed atomic load, a plain
byte load where it compiles to one, and hx::BarrierRemember sets the
bit with an atomic or. The barrier macros, YoungStore, the hash maps'
barrier and the cppia JIT's pushWriteBarrier use them, and the count
is read and written with relaxed atomics.

Test: the TSan job, which reported the race in
GenerationalArrayYoungLog; and the Generational regression tests, a
cppia JIT test and gc stress, all built generational, on Windows x64.
dimensionscape added a commit that referenced this pull request Nov 21, 2026
The fixes from the architecture pass, #474, #475, #477, #478 and #479 rebased together with what their reviews found, and the libhxcpp 6.0.0 release commit on top. Fixes 22 issues from #450 to #473; #459 and #460 wait on Haxe.
@dimensionscape

Copy link
Copy Markdown
Member Author

Landed in #481, rebased onto main with the fixes its review found.

@dimensionscape dimensionscape mentioned this pull request Nov 24, 2026
34 tasks done
@dimensionscape
dimensionscape deleted the fix/young-log-race branch November 25, 2026 02:17
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