Fix GC stack scanning bugs: memcpy direction in gc_save_context and volatile stopping_world#909
Closed
Fix GC stack scanning bugs: memcpy direction in gc_save_context and volatile stopping_world#909
Conversation
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Investigate HL segfaults in hxcoro tests
Fix GC stack scanning bugs: memcpy direction in gc_save_context and volatile stopping_world
Mar 12, 2026
Member
|
I have pushed the |
Simn
reviewed
Mar 12, 2026
Comment on lines
+242
to
+245
| if( size < 0 ) size = 0; | ||
| if( size > HL_MAX_EXTRA_STACK ) hl_fatal("GC_SAVE_CONTEXT"); | ||
| t->extra_stack_size = size; | ||
| memcpy(t->extra_stack_data, prev_stack, size*sizeof(void*)); | ||
| memcpy(t->extra_stack_data, stack_cur, size*sizeof(void*)); |
Member
There was a problem hiding this comment.
Alright, I'll stop with the AI stuff for HL because it doesn't appear to actually help here. Thanks for checking!
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.
Intermittent segfaults in multithreaded workloads (reproduced via hxcoro tests, ~40% failure rate on multi-core, 0% on single core). Crash is always in
hl_to_virtualaccessing a GC-collected object on a worker thread.Changes
src/gc.c— Fixgc_save_contextmemcpy source address: The memcpy copied fromprev_stack(caller's variable address) upward, capturing data above the gap that's already covered by the maingc_mark_stack(stack_cur, stack_top)scan. The actual callee-saved register spill area lives betweenstack_curandprev_stack— that's what needs preserving inextra_stack_databefore it gets overwritten by subsequent calls (e.g.hl_mutex_acquire). Also added asize < 0guard.src/hl.h— Makestopping_worldvolatile: This flag is written by the GC thread and read by worker threads inhl_blocking(false)andhl_gc_safepoint(). Withoutvolatile, the compiler is free to cache the value.Known remaining issue
There is a deeper race in
hl_blocking(false): a thread can transitiongc_blockingfrom 1→0 aftergc_stop_worldhas already passed it in its sequential check. In that window the thread runs JIT code with GC roots only in scratch registers, invisible to the stalegc_regs/stack_cur. The proposed fix is to spin-wait inhl_blocking(false)whilestopping_worldis true before allowinggc_blockingto reach 0, but this is not yet implemented in this PR.🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.