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
- test(maitake): Miri test for rust-lang/miri#3780 This reproduces a potential UB where a `task::Cell` gets niche-optimized in the `Joined` case. This is based on the test added to Tokio in tokio-rs/to... — committed to hawkw/mycelium by hawkw 2 years ago
- fix(maitake): prevent task cell niche optimization See https://github.com/rust-lang/miri/issues/3780#issuecomment-2264999042 — committed to hawkw/mycelium by hawkw 2 years ago
- test(maitake): Miri test for rust-lang/miri#3780 (#483) This reproduces a potential UB where a `task::Cell` gets niche-optimized in the `Joined` case. This is based on the test added to Tokio in toki... — committed to hawkw/mycelium by hawkw 2 years ago
- fix(maitake): prevent task cell niche optimization (#483) See [this comment][1]. Adding `#[repr(C)]` to this type suppresses niche optimization, resolving potential unsoundness that could occur when ... — committed to hawkw/mycelium by hawkw 2 years ago
- test(maitake): Miri test for rust-lang/miri#3780 (#483) This reproduces a potential UB where a `task::Cell` gets niche-optimized in the `Joined` case. This is based on the test added to Tokio in toki... — committed to hawkw/mycelium by hawkw 2 years ago
- fix(maitake): prevent task cell niche optimization (#483) See [this comment][1]. Adding `#[repr(C)]` to this type suppresses niche optimization, resolving potential unsoundness that could occur when ... — committed to hawkw/mycelium by hawkw 2 years ago
- Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors Supress niches in coroutines to avoid aliasing violations As mentioned [here](https://github.com/rust-lang/rust/issues/63818#issu... — committed to rust-lang-ci/rust by bors 2 years ago
- Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors Supress niches in coroutines to avoid aliasing violations As mentioned [here](https://github.com/rust-lang/rust/issues/63818#issu... — committed to rust-lang-ci/rust by bors 2 years ago
- Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors Supress niches in coroutines to avoid aliasing violations As mentioned [here](https://github.com/rust-lang/rust/issues/63818#issu... — committed to rust-lang-ci/rust by bors 2 years ago
- Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors Supress niches in coroutines to avoid aliasing violations As mentioned [here](https://github.com/rust-lang/rust/issues/63818#issu... — committed to rust-lang-ci/rust by bors 2 years ago
- Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors Supress niches in coroutines to avoid aliasing violations As mentioned [here](https://github.com/rust-lang/rust/issues/63818#issu... — committed to RalfJung/miri by bors 2 years ago
- Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors Supress niches in coroutines to avoid aliasing violations As mentioned [here](https://github.com/rust-lang/rust/issues/63818#issu... — committed to lnicola/rust-analyzer by bors 2 years ago
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
&mutaccess, you could wrap the thing the mutable reference points to in anUnsafeCell. 😂UnsafeCell::get_mutis safe, after all.