Skip to content

Interior mutable shared ref to constant memory is UB even when not writing #246

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

Closed
RalfJung opened this issue Aug 14, 2020 · 3 comments
Closed
Labels
A-aliasing-model Topic: Related to the aliasing model (e.g. Stacked/Tree Borrows) C-open-question Category: An open question that we should revisit

Comments

@RalfJung
Copy link
Member

Current Stacked Borrows makes this UB:

fn main() { unsafe {
    let x = &0usize;
    let y: &std::cell::Cell<usize> = std::mem::transmute(x);
} }

To be honest I do not think there is a good reason for this. We cannot optimize much around interior mutable types, so I don't think we should require them to be writable unless we are actually mutating.

@RalfJung RalfJung added A-aliasing-model Topic: Related to the aliasing model (e.g. Stacked/Tree Borrows) C-open-question Category: An open question that we should revisit labels Aug 14, 2020
@programmerjake

This comment has been minimized.

@RalfJung
Copy link
Member Author

This is closely related to #227: basically, interior mutable shared references should inherit the parent pointer permission instead of asserting mutability, and again we have a close relationship between what we do for raw pointers and what we do for interior mutable shared references.

@programmerjake that's the exact same case. AtomicUsize and Cell<usize> look the same to the compiler (both are UnsafeCell<usize>).

@RalfJung
Copy link
Member Author

Closing as a duplicate of #303.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-aliasing-model Topic: Related to the aliasing model (e.g. Stacked/Tree Borrows) C-open-question Category: An open question that we should revisit
Projects
None yet
Development

No branches or pull requests

2 participants