Skip to content

Commit

Permalink
Use Arc::as_ptr
Browse files Browse the repository at this point in the history
This fixes stacked borrows violations with -Zmiri-tag-raw-pointers.
  • Loading branch information
taiki-e committed Jan 12, 2022
1 parent 72f4f80 commit 8f40f46
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -244,9 +244,9 @@ jobs:
- name: Install Rust
run: rustup toolchain install nightly --component miri && rustup default nightly
# futures-executor uses boxed futures so many tests trigger https://github.com/rust-lang/miri/issues/1038
- run: cargo miri test --workspace --exclude futures-executor --all-features
- run: cargo miri test --workspace --exclude futures-executor --all-features --no-fail-fast
env:
MIRIFLAGS: -Zmiri-disable-isolation # TODO: use -Zmiri-tag-raw-pointers
MIRIFLAGS: -Zmiri-disable-isolation -Zmiri-tag-raw-pointers

san:
name: cargo test -Z sanitizer=${{ matrix.sanitizer }}
Expand Down
2 changes: 1 addition & 1 deletion futures-task/src/waker_ref.rs
Expand Up @@ -55,7 +55,7 @@ where
{
// simply copy the pointer instead of using Arc::into_raw,
// as we don't actually keep a refcount by using ManuallyDrop.<
let ptr = (&**wake as *const W) as *const ();
let ptr = Arc::as_ptr(wake).cast::<()>();

let waker =
ManuallyDrop::new(unsafe { Waker::from_raw(RawWaker::new(ptr, waker_vtable::<W>())) });
Expand Down
4 changes: 2 additions & 2 deletions futures-util/src/stream/futures_unordered/mod.rs
Expand Up @@ -150,7 +150,7 @@ impl<Fut> FuturesUnordered<Fut> {
queued: AtomicBool::new(true),
ready_to_run_queue: Weak::new(),
});
let stub_ptr = &*stub as *const Task<Fut>;
let stub_ptr = Arc::as_ptr(&stub);
let ready_to_run_queue = Arc::new(ReadyToRunQueue {
waker: AtomicWaker::new(),
head: AtomicPtr::new(stub_ptr as *mut _),
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<Fut> FuturesUnordered<Fut> {
// The `ReadyToRunQueue` stub is never inserted into the `head_all`
// list, and its pointer value will remain valid for the lifetime of
// this `FuturesUnordered`, so we can make use of its value here.
&*self.ready_to_run_queue.stub as *const _ as *mut _
Arc::as_ptr(&self.ready_to_run_queue.stub) as *mut _
}
}

Expand Down
Expand Up @@ -83,7 +83,7 @@ impl<Fut> ReadyToRunQueue<Fut> {
}

pub(super) fn stub(&self) -> *const Task<Fut> {
&*self.stub
Arc::as_ptr(&self.stub)
}

// Clear the queue of tasks.
Expand Down
2 changes: 1 addition & 1 deletion futures-util/src/stream/futures_unordered/task.rs
Expand Up @@ -62,7 +62,7 @@ impl<Fut> ArcWake for Task<Fut> {
// still.
let prev = arc_self.queued.swap(true, SeqCst);
if !prev {
inner.enqueue(&**arc_self);
inner.enqueue(Arc::as_ptr(arc_self));
inner.waker.wake();
}
}
Expand Down

0 comments on commit 8f40f46

Please sign in to comment.