Fix mutable aliasing UB: Replace Box<Entry> with NonNull<Entry>#302
Open
matteoldani wants to merge 1 commit into
Open
Fix mutable aliasing UB: Replace Box<Entry> with NonNull<Entry>#302matteoldani wants to merge 1 commit into
matteoldani wants to merge 1 commit into
Conversation
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`.
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 #264
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.