miri: Undefined behavior from `poll_fn` + `futures::join!` / `std::future::join!`

Recently, when working on a library for observables, I found new miri test failures (I had been getting them on a specific test before, but didn’t investigate after ensuring it wasn’t my own UB). This lead to a bug report to tokio where Alice Ryhl showed that it didn’t actually require tokio::spawn (which I had failed to get rid of in minimizing the issue), and the same issue was reproducible with join! + futures_executor.

I have now reduced the example much further, to this (works with the nightly std::future::join!, or futures::join!; playground):

#![feature(future_join)]

use std::{
    future::{join, poll_fn, Future},
    pin::pin,
    sync::Arc,
    task::{Context, Poll, Wake},
};

fn main() {
    let mut fut = pin!(async {
        let mut thing = "hello".to_owned();
        let fut1 = async move {
            poll_fn(|_| {
                thing += ", world";
                Poll::<()>::Pending
            })
            .await;
        };

        let fut2 = async {};

        join!(fut1, fut2).await;
    });

    let waker = Arc::new(DummyWaker).into();
    let mut ctx = Context::from_waker(&waker);
    _ = fut.as_mut().poll(&mut ctx);
    _ = fut.as_mut().poll(&mut ctx);
}

struct DummyWaker;

impl Wake for DummyWaker {
    fn wake(self: Arc<Self>) {}
}

Alice suggested that this may be an instance of https://github.com/rust-lang/rust/issues/63818, is that correct? I was under the impression that the compiler for now doesn’t implement alias restrictions for !Unpin types, but maybe this only applies to the LLVM backend, not miri?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Here’s a proper work-around: https://github.com/rust-lang/rust/pull/129313. It increases the size of some futures, though…

Yeah I did not mean to imply that this is a reasonable work-around for this case, I just pointed out the possibility.

I don’t think there is a good work-around, this fundamentally needs rustc layout computation to change – either by special-casing generators, or by implementing https://github.com/rust-lang/rust/issues/125735.

If you only need &mut access, you could wrap the thing the mutable reference points to in an UnsafeCell. 😂 UnsafeCell::get_mut is safe, after all.