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

Validity of ManuallyDrop, or: ManuallyDrop<Box<_>> is strange #245

Open
RalfJung opened this issue Aug 12, 2020 · 2 comments
Open

Validity of ManuallyDrop, or: ManuallyDrop<Box<_>> is strange #245

RalfJung opened this issue Aug 12, 2020 · 2 comments
Labels
A-validity Topic: Related to validity invariants C-open-question Category: An open question that we should revisit

Comments

@RalfJung
Copy link
Member

The ManuallyDrop docs say

ManuallyDrop is subject to the same layout optimizations as T. As a consequence, it has no effect on the assumptions that the compiler makes about its contents.

In particular, this means that a ManuallyDrop<Box<T>> must not dangle. But that means that after calling drop on a ManuallDrop<Box<T>>, that value violates the validity invariant and must not be used or passed around any more! I don't think that is the behavior we want.

We want ManuallyDrop to preserve niches, so we cannot really relax what it says about validity. But what we could relax is validity of Box -- we could say that validity is purely about the pointer value itself (it must be non-NULL and properly aligned). We could specify that the "dereferencability" only comes in through the aliasing model, and we could make that model stop looking into ManuallyDrop.

@RalfJung RalfJung added C-open-question Category: An open question that we should revisit A-validity Topic: Related to validity invariants labels Aug 12, 2020
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 26, 2024
…homcc,traviscross

Document subtleties of `ManuallyDrop`

After seeing rust-lang#130140 and rust-lang#130141, I figured that `ManuallyDrop` needs documentation explaining its subtleties, hence this PR.

See also rust-lang/unsafe-code-guidelines#245
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 27, 2024
Rollup merge of rust-lang#130279 - theemathas:manually-drop-docs, r=thomcc,traviscross

Document subtleties of `ManuallyDrop`

After seeing rust-lang#130140 and rust-lang#130141, I figured that `ManuallyDrop` needs documentation explaining its subtleties, hence this PR.

See also rust-lang/unsafe-code-guidelines#245
@theemathas
Copy link

theemathas commented Dec 25, 2024

Of note, ManuallyDrop<&mut T> and ManuallyDrop<&T> have similar behavior, although it's harder to accidentally run into. For example, the Miri says the following code has UB.

use std::mem::{ManuallyDrop, transmute};

fn main() {
    let x = Box::new(123);
    let y: &i32 = unsafe { transmute(&*x) };
    let mut z = ManuallyDrop::new(y);
    // Removing the following line does not affect the behavior
    unsafe { ManuallyDrop::drop(&mut z); }
    drop(x);
    let _moved = z;
}

@RalfJung
Copy link
Member Author

Yeah, but it's less strange there since dropping &mut T is a NOP anway. So this issue focuses on ManuallyDrop<Box<T>>; &mut is just in it for consistency reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validity Topic: Related to validity invariants C-open-question Category: An open question that we should revisit
Projects
None yet
Development

No branches or pull requests

2 participants