Fix a series of possible race conditions in thread static variable initialization#127843
Open
davidwrighton wants to merge 4 commits intodotnet:mainfrom
Open
Fix a series of possible race conditions in thread static variable initialization#127843davidwrighton wants to merge 4 commits intodotnet:mainfrom
davidwrighton wants to merge 4 commits intodotnet:mainfrom
Conversation
…itialization - The two critical ones are the one just after the memcpy in the TLSIndexToMethodTableMap, as well as the VolatileStore when setting pIndex. Fixes dotnet#127776
Contributor
|
Tagging subscribers to this area: @agocke |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR targets a set of suspected race conditions in CoreCLR thread-static infrastructure by strengthening publication/visibility guarantees when updating TLS index metadata and the TLSIndex→MethodTable map. This aligns with the reported intermittent macOS arm64 crash in GetThreadLocalStaticBase by ensuring readers can’t observe partially-published state during concurrent thread-static access.
Changes:
- Use
VolatileStorewhen publishing a resizedTLSIndexToMethodTableMap(pMapandm_maxIndex) to ensure the copied/initialized map contents are visible before other threads observe the updated pointers/sizes. - Use
VolatileStorewhen writing/clearing individual map entries to prevent reordering and improve cross-thread visibility of per-index updates. - Use
VolatileStorewhen publishing the newly allocatedTLSIndex(*pIndex) so other threads that observe the allocated index have the intended ordering/visibility for preceding writes on the allocation path.
3 tasks
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
| static TLSIndex Unallocated() { LIMITED_METHOD_DAC_CONTRACT; return TLSIndex(0xFFFFFFFF); } | ||
| bool operator == (TLSIndex index) const { LIMITED_METHOD_DAC_CONTRACT; return TLSIndexRawIndex == index.TLSIndexRawIndex; } | ||
| bool operator != (TLSIndex index) const { LIMITED_METHOD_DAC_CONTRACT; return TLSIndexRawIndex != index.TLSIndexRawIndex; } | ||
| void VolatileStore(const TLSIndex &other) { LIMITED_METHOD_CONTRACT; ::VolatileStore(&TLSIndexRawIndex, other.TLSIndexRawIndex); } |
Comment on lines
771
to
776
| g_pThreadStaticCollectibleTypeIndices->Set(newTLSIndex, pMT, gcStatic); | ||
| pMT->GetLoaderAllocator()->GetTLSIndexList().Append(newTLSIndex); | ||
| } | ||
|
|
||
| *pIndex = newTLSIndex; | ||
| pIndex->VolatileStore(newTLSIndex); // Use a volatile store so that any other thread that sees the allocated index will also see the writes throughout this path. | ||
| } |
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.
Fixes #127776