-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DynFuture is unsound #3
Comments
@taiki-e Thanks for finding this! This change would break at least the counter.rs example, which uses |
Note that the code that is broken by this change is the code that is actually affected by this unsoundness. BTW why don't you want to use the |
Well, I think the following function can do this, it won't probably be very convenient if compared to macro. pub fn pin<T, U>(mut v: T, f: impl FnOnce(Pin<&mut T>) -> U) -> U {
let v = unsafe { Pin::new_unchecked(&mut v) };
f(v)
} |
The original idea behind pasts was to make something that works like how the futures crate works but without any dependencies. Later, I decided it would be interesting to reimplement using traits instead of macros. I was pretty happy with how it turned out (except for the unsoundness issues discovered that I have now fixed, except for this one). The reason I don't like I'm currently leaning towards using your function, I'll experiment with it and see how inconvenient it is. |
Hmm, is this true even with the latest compilers? IIRC, in 1.29 and later compilers, macros that generate unsafe code are also compatible with Actually, pin-utils has a test to check compatible to |
You appear to be correct. This was not the case last I checked. |
I have completely removed |
@AldaronLau Thanks for the fix! |
Describe the bug
https://github.com/AldaronLau/pasts/blob/675bd309d609111fac52889602e31c9609e7f2ea/src/dyn_future.rs#L21-L22
This is
Pin<&mut Type>
toPin<Field>
projection and is unsound ifdyn Future
is notUnpin
(you can movedyn Future
afterDynFuture
dropped).repro: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b2564e36d16d7b2a8f14f763a9477a85
The correct projection is
Pin<&mut Type>
toPin<&mut Field>
. InDynFuture
, it isPin<&mut DynFuture<'_, T>>
toPin<&mut &mut dyn Future>
, and it needs to adddyn Future: Unpin
bounds to convertPin<&mut &mut dyn Future>
toPin<&mut dyn Future>
.Solution
Change
DynFuture
from&'a mut dyn Future<Output = T>
to&'a mut (dyn Future<Output = T> + Unpin)
.https://github.com/AldaronLau/pasts/blob/675bd309d609111fac52889602e31c9609e7f2ea/src/dyn_future.rs#L14
Additional context
I have fixed a similar bug on
tokio
in the past: tokio-rs/tokio#2612Also, #2, previously reported by @udoprog and already fixed by @AldaronLau, seems to be the same problem as this.
The text was updated successfully, but these errors were encountered: