Skip to content

Commit

Permalink
fix(util): make intrusive list links never Unpin
Browse files Browse the repository at this point in the history
In light of rust-lang/rust#82834, we must ensure that the intrusive
linked list pointers never get mutable-noalias optimizations (see also
rust-lang/rust#63818). Adding a `PhantomPinned` to the `Links` struct
ensures it will always be `!Unpin`, disabling mutable-noalias.

Signed-off-by: Eliza Weisman <[email protected]>
  • Loading branch information
hawkw committed Mar 28, 2021
1 parent 45d632d commit fae54bc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions util/src/intrusive/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use core::fmt;
use core::mem::ManuallyDrop;
use core::ptr::NonNull;
use core::{fmt, marker::PhantomPinned, mem::ManuallyDrop, ptr::NonNull};

pub unsafe trait Linked {
type Handle;
Expand Down Expand Up @@ -38,6 +36,10 @@ pub struct List<T: ?Sized> {
pub struct Links<T: ?Sized> {
next: Option<NonNull<T>>,
prev: Option<NonNull<T>>,
/// Linked list links must always be `!Unpin`, in order to ensure that they
/// never recieve LLVM `noalias` annotations; see also
/// https://github.com/rust-lang/rust/issues/63818.
_unpin: PhantomPinned,
}

pub struct Cursor<'a, T: ?Sized + Linked> {
Expand Down Expand Up @@ -142,7 +144,7 @@ impl<T: ?Sized + Linked> List<T> {
pub unsafe fn remove(&mut self, item: NonNull<T>) -> Option<T::Handle> {
let links = T::links(item).as_mut().take();
tracing::trace!(?self, item.addr = ?item, item.links = ?links, "remove");
let Links { next, prev } = links;
let Links { next, prev, .. } = links;

if let Some(prev) = prev {
T::links(prev).as_mut().next = next;
Expand Down Expand Up @@ -193,13 +195,15 @@ impl<T: ?Sized> Links<T> {
Self {
next: None,
prev: None,
_unpin: PhantomPinned,
}
}

fn take(&mut self) -> Self {
Self {
next: self.next.take(),
prev: self.next.take(),
_unpin: PhantomPinned,
}
}

Expand Down

0 comments on commit fae54bc

Please sign in to comment.