Skip to content

Fix mutable aliasing UB: Replace Box<Entry> with NonNull<Entry>#302

Open
matteoldani wants to merge 1 commit into
servo:mainfrom
matteoldani:fix-mutable-aliasing
Open

Fix mutable aliasing UB: Replace Box<Entry> with NonNull<Entry>#302
matteoldani wants to merge 1 commit into
servo:mainfrom
matteoldani:fix-mutable-aliasing

Conversation

@matteoldani

Copy link
Copy Markdown

Fixes #264

This PR resolves Undefined Behavior caused by mutable aliasing in the dynamic_set.rs implementation.

Previously, the Set traversal logic temporarily dropped and re-boxed the Option<Box<Entry>> links, which violated Stacked Borrows and invalidated raw pointers pointing to the entry during insert and remove operations.

This has been fixed by replacing Option<Box<Entry>> with Option<NonNull<Entry>>, adding proper manual Drop semantics to prevent memory leaks, and implementing explicit Send and Sync bounds.

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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UB detected by miri

1 participant