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

Do raw pointer conversions erase pointer rights? #459

Closed
joshlf opened this issue Sep 6, 2023 · 4 comments
Closed

Do raw pointer conversions erase pointer rights? #459

joshlf opened this issue Sep 6, 2023 · 4 comments

Comments

@joshlf
Copy link

joshlf commented Sep 6, 2023

The following passes Miri, but is it sound? In particular, I'm worried about the SharedReadWrite permission being "erased" at some point in the series of pointer casts.

fn main() {
    let x = UnsafeCell::new(0);
    let x = &x;
    let x_ptr: *const UnsafeCell<usize> = x;
    
    let x_ptr = x_ptr.cast::<usize>();
    let x_ptr = x_ptr.cast_mut();
    let x_ptr = NonNull::new(x_ptr).unwrap();
    let x_ptr = x_ptr.as_ptr();
    let x_ptr = x_ptr.cast_const();
    let x_ptr = x_ptr.cast::<UnsafeCell<usize>>();
    
    let _x = unsafe { &*x_ptr };
}
@RalfJung
Copy link
Member

RalfJung commented Sep 6, 2023

I'm pretty sure we can (and have to) promise that raw-pointer-to-raw-pointer operations such as cast or offset do not affect provenance, and hence this is sound.

The one aspect of this that is subtle (but that you're probably aware of) is that the initial cast from a reference to a raw pointer matters: x as *const _ and x as *mut _ are not equivalent if x is a mutable reference. I view this as a bug in SB that is fixed by TB, but maybe for some reason we won't be able to fix this.

@joshlf
Copy link
Author

joshlf commented Sep 6, 2023

The one aspect of this that is subtle (but that you're probably aware of) is that the initial cast from a reference to a raw pointer matters: x as *const _ and x as *mut _ are not equivalent if x is a mutable reference. I view this as a bug in SB that is fixed by TB, but maybe for some reason we won't be able to fix this.

Oh wow no, I had no idea. My mental model for the difference between *const and *mut has always been "they're exactly the same but it felt weird not to have two pointer types given that we have two reference types". That's reinforced by NonNull not having different variants for const/mut, and all its methods only operating on/returning *mut.

@RalfJung
Copy link
Member

RalfJung commented Sep 6, 2023

@RalfJung
Copy link
Member

RalfJung commented Apr 1, 2024

I think the question has been answered.

@RalfJung RalfJung closed this as completed Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants