fix(sync): close data race on catchup() count read (F-32, #112)#160
Merged
adequatelimited merged 1 commit intoaudit-fixesfrom Apr 13, 2026
Merged
fix(sync): close data race on catchup() count read (F-32, #112)#160adequatelimited merged 1 commit intoaudit-fixesfrom
adequatelimited merged 1 commit intoaudit-fixesfrom
Conversation
9512c23 to
7328cd6
Compare
The while(count && ecode == VEOK) condition at sync.c:138 reads the shared count outside any OMP_CRITICAL_ section, while the two decrements are inside critical sections. Per the OpenMP memory model, the flush on critical-exit is not paired with an unsynchronized reader, so the read can observe stale cached values (or be a data race by C11; TSan will flag it). Shadow the parameter with a local `volatile word32 count = count_in`. volatile forces a memory read each iteration; combined with the writers' implicit flush on critical-exit, this establishes the needed happens-before. Same treatment applied to the analogous ecode read in tfile.c under F-10. Analyzed the underflow claim from the original issue and confirmed it is not reachable -- each thread's loop body decrements at most once before breaking. See github.com//issues/112 comment for the trace.
7328cd6 to
b4c2d47
Compare
3 tasks
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.
Summary
Closes #112 (F-32) — partial. The underflow concern in the original report was analyzed and determined not to be a real risk; see this comment on the issue for the per-thread decrement trace.
The real issue is the data race on the shared `count` read at sync.c:136:
Per the OpenMP memory model, the flush on critical-exit is not paired with an unsynchronized reader, so the read can observe stale cached values (and TSan will flag it as a race).
Marking the parameter `volatile` forces a memory read each iteration. Combined with the writers' implicit flush on critical-exit, this establishes the needed happens-before. Same treatment applied to the analogous `ecode` read in `tfile.c` under F-10.
Test plan