Skip to content
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

DispatchFromDyn rules allowing non-PhantomData ZSTs in additional fields is unsound #135220

Open
steffahn opened this issue Jan 7, 2025 · 0 comments · May be fixed by #135228
Open

DispatchFromDyn rules allowing non-PhantomData ZSTs in additional fields is unsound #135220

steffahn opened this issue Jan 7, 2025 · 0 comments · May be fixed by #135228
Labels
C-bug Category: This is a bug. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` F-dispatch_from_dyn `#![feature(dispatch_from_dyn)]` I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Jan 7, 2025

DispatchFromDyn implementations are only allowed when all fields besides the main “pointer” contain ZSTs. Beyond this, those field’s types aren’t really restricted. Since actually dispatching from dyn then involves effectively transmuting the whole struct acting as receiving pointer, this can transmute between ZSTs that encode additional properties at the type level.

This isn't sound, as it breaks e.g. assumptions in the API of qcell::TCell, where the encoded assumption of the ZST TCellOwner<T: 'static> is that – for each type T: 'static – only one value of type TCellOwner<T> can ever be created.

#![feature(arbitrary_self_types)]
#![feature(unsize)]
#![feature(dispatch_from_dyn)]

use std::marker::PhantomData;
use std::marker::Unsize;
use std::ops::Deref;
use std::ops::DispatchFromDyn;
use std::ptr;

use qcell::TCell;
use qcell::TCellOwner;

struct Dispatchable<T: ?Sized + 'static> {
    token: TCellOwner<PhantomData<T>>,
    _ptr: *const T,
}

impl<T, U> DispatchFromDyn<Dispatchable<U>> for Dispatchable<T>
where
    T: Unsize<U> + ?Sized,
    U: ?Sized,
{
}

trait Trait<S> {
    fn get_owned(self: Dispatchable<Self>) -> TCellOwner<PhantomData<S>>;
}
impl<S: 'static> Trait<S> for S {
    fn get_owned(self: Dispatchable<Self>) -> TCellOwner<PhantomData<S>> {
        self.token
    }
}

impl<U: ?Sized> Deref for Dispatchable<U> {
    type Target = U;
    fn deref(&self) -> &U {
        panic!()
    }
}

fn owner_from_dyn<S: 'static>(
    x: TCellOwner<PhantomData<dyn Trait<S>>>,
) -> TCellOwner<PhantomData<S>> {
    let s = Dispatchable {
        token: x,
        _ptr: ptr::dangling::<S>(),
    };
    Trait::get_owned(s)
}

fn main() {
    struct S;
    type T = PhantomData<S>;
    type O = TCellOwner<T>;
    let owner1: O = TCellOwner::new();
    let mut owner2: O = owner_from_dyn(TCellOwner::new());
    let c: TCell<T, Option<String>> = TCell::new(Some("Hello World!".into()));
    let r: &str = c.ro(&owner1).as_deref().unwrap();
    *c.rw(&mut owner2) = None;
    println!("{r}");
}

@rustbot label requires-nightly, I-unsound, F-dispatch_from_dyn, F-arbitrary_self_types, T-compiler

The rules should probably be changed to be like CoerceUnsized, requiring all fields besides the main “pointer” one to not only be zero-sized, but actually the type PhantomData.

Split from #135215 to track this issue independent of the question of its relevance to the stabilization of derive(CoercePointee).

@steffahn steffahn added the C-bug Category: This is a bug. label Jan 7, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-dispatch_from_dyn `#![feature(dispatch_from_dyn)]` I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way. labels Jan 7, 2025
@rustbot rustbot added F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 7, 2025
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` F-dispatch_from_dyn `#![feature(dispatch_from_dyn)]` I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants