Fix mutable aliasing UB: Replace Box<Entry> with NonNull<Entry> - #302
Merged
Conversation
SimonSapin
approved these changes
Aug 19, 2026
Comment on lines
128
to
140
| impl Drop for Set { | ||
| fn drop(&mut self) { | ||
| for bucket in self.buckets.iter_mut() { | ||
| let mut current = bucket.get_mut().take(); | ||
| while let Some(ptr) = current { | ||
| // SAFETY: The Set is being dropped, meaning no other Atom references can exist. | ||
| // We own the pointers and must reconstruct the Box to safely free the memory. | ||
| let mut entry = unsafe { Box::from_raw(ptr.as_ptr()) }; | ||
| current = entry.next_in_bucket.take(); | ||
| } | ||
| } | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
This is dead code, since the only Set ever created in a process lives in static DYNAMIC_SET: OnceLock<Set> and is never dropped. I’ve pushed a commit to remove it
Member
There was a problem hiding this comment.
The Set is being dropped, meaning no other Atom references can exist
There is no such guarantee (which is why the Set can never be dropped)
SimonSapin
force-pushed
the
fix-mutable-aliasing
branch
from
August 19, 2026 19:58
59f0600 to
fa86b3e
Compare
Member
Removing this because, while this PR does improve things, I’m still getting another error from miri (a data race between |
In Rust's Stacked Borrows and Tree Borrows memory models, moving a `Box<T>` asserts unique access and invalidates all existing raw pointers to its contents. In `string-cache`, dynamic string entries are allocated via `Box<Entry>` and immediately handed out as raw pointers (`*mut Entry`) to be held by `Atom`. However, because these `Box`es are moved into the global linked list (`*linked_list = Some(entry)`), and moved again whenever a hash collision occurs (`next_in_bucket: linked_list.take()`), the raw pointers held by active `Atom`s were being continuously invalidated. This led to pervasive UB when `Atom` subsequently dereferenced them in `clone` and `drop`. This commit re-architects the linked list to use `Option<NonNull<Entry>>`. By manually allocating (`Box::into_raw`) and destroying (`Box::from_raw`) the entries, we sever the compiler's strict aliasing assumptions over the pointers, preserving their provenance. We also provide a custom `Drop` implementation for `Set` to prevent memory leaks during tests, manually re-implement `Send` and `Sync`, and include the missing integer overflow guard inside `Set::insert`.
SimonSapin
force-pushed
the
fix-mutable-aliasing
branch
from
August 19, 2026 20:09
fa86b3e to
60da8bf
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.
This PR resolves Undefined Behavior caused by mutable aliasing in the
dynamic_set.rsimplementation.Previously, the
Settraversal logic temporarily dropped and re-boxed theOption<Box<Entry>>links, which violated Stacked Borrows and invalidated raw pointers pointing to the entry duringinsertandremoveoperations.This has been fixed by replacing
Option<Box<Entry>>withOption<NonNull<Entry>>, adding proper manualDropsemantics to prevent memory leaks, and implementing explicitSendandSyncbounds.