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

Document subtleties of ManuallyDrop #130279

Merged
merged 4 commits into from
Sep 27, 2024
Merged

Conversation

theemathas
Copy link
Contributor

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

See also rust-lang/unsafe-code-guidelines#245

@rustbot
Copy link
Collaborator

rustbot commented Sep 12, 2024

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 12, 2024
@scottmcm scottmcm added the I-lang-nominated Nominated for discussion during a lang team meeting. label Sep 12, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 13, 2024
@Noratrieb
Copy link
Member

@scottmcm what did you nominate this for? should this PR be blocked on that or is this about a sperate issue than adding documentation warning about what is a potentially dangerous pattern right now?

@WaffleLapkin
Copy link
Member

cc @rust-lang/opsem

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some wording/style nitpicks

library/core/src/mem/manually_drop.rs Outdated Show resolved Hide resolved
library/core/src/mem/manually_drop.rs Outdated Show resolved Hide resolved
library/core/src/mem/manually_drop.rs Outdated Show resolved Hide resolved
library/core/src/mem/manually_drop.rs Outdated Show resolved Hide resolved
library/core/src/mem/manually_drop.rs Outdated Show resolved Hide resolved
@scottmcm
Copy link
Member

@Noratrieb I'd much rather just remove the subtleties so we don't have to document them :P

Dunno whether that's practical, though. I just don't want to do a bunch of ManuallyDrop -> MaybeUninit everywhere now, then undo it again shortly.

@CAD97
Copy link
Contributor

CAD97 commented Sep 13, 2024

The subtlety of “using the value after it's been [manually] dropped” continues to be annoying. The one for moving can and should be fixed, but the case of derives using the value after it's been dropped is a clear cut violation of the safety contract, unfortunately. (This is why unsafe postconditions on the caller are hard.)

The diff for patching move-based holes can be minimized by utilizing a temporary interim impl of ManuallyDrop on top of MaybeUninit, at the cost of niche optimizations. Then it should be just a matter of swapping out the import.

@theemathas
Copy link
Contributor Author

The diff for patching move-based holes can be minimized by utilizing a temporary interim impl of ManuallyDrop on top of MaybeUninit, at the cost of niche optimizations. Then it should be just a matter of swapping out the import.

Such an impl is available in Yandros's maybe-dangling crate. Should this link be added to the PR?

Copy link
Member

@thomcc thomcc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me, but I find it deeply unpleasant. I'll leave it open for the lang team nomination though.

@thomcc thomcc added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 15, 2024
@RalfJung
Copy link
Member

RalfJung commented Sep 18, 2024

Just saw this comment by @pnkfelix from the lang team meeting

We could also file a bug against Miri (if we are confident that Miri is the sole way to observe UB here, which I am not confident of currently).

A ManuallyDrop<Box> is marked as noalias in LLVM. This has to be reflected in Miri. The default aliasing model we use, Stacked Borrows, only supports noalias in combination with "dereferenceable at the moment of function invocation" (which is different from the LLVM dereferenceable attribute which means "dereferenceable for the entire duration of the function"). This means that when a Box value is moved, Stacked Borrows inevitably checks that it is dereferenceable, triggering UB if the Box was dropped already.

The upshot is that to make code like this not trigger Miri UB, we'd have to

  • Either entirely remove the aliasing treatment of Box (at least those wrapped in ManuallyDrop), thus also missing real UB caused by the Box noalias attribute
  • Or deprecate Stacked Borrows in favor of Tree Borrows

I don't think we should do either of these.

We could stop adding noalias to Box inside ManuallyDrop in codegen and in Miri -- that would basically be a partial implementation of MaybeDangling. Since ManuallyDrop is already a lang item, that shouldn't even be that hard.

@tmandry
Copy link
Member

tmandry commented Sep 18, 2024

The outcome we discussed in the lang team meeting is that we should document this behavior for now, but also document that it is likely to change in the future once RFC 3336 (MaybeDangling) is implemented. I see that the PR already does this, so I think we can move forward with merging it.

@rustbot label -I-lang-nominated

@rustbot rustbot removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Sep 18, 2024
@scottmcm scottmcm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 18, 2024
/// # Interaction with `Box`
///
/// Currently, once the `Box<T>` inside a `ManuallyDrop<Box<T>>` is dropped,
/// moving the `ManuallyDrop<Box<T>>` is [considered to be undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think documentation should clarify that this also applies to any types that directly contain Box<_>, like (Box<T>, u8) or struct Foo(Box<T>);.

I also believe that per current rules, calling ManuallyDrop::drop() on any type that directly contains Box<_> is insta-UB, even if you don’t move it afterwards. Technically, it doesn’t neccessarily count as producing an invalid value:

“Producing” a value happens any time a value is assigned to or read from a place, passed to a function/primitive operation or returned from a function/primitive operation.

but at least touching it in any way (like creating a reference) is certainly UB.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't name the place after calling ManuallyDrop::drop, everything is fine; the place isn't touched after that point. Because ManuallyDrop does not have any drop glue, it does not get a drop_in_place terminator at the end of scope, and thus the place is never "used" implicitly by the language, and no reference is created unless you create it yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reworded the text to clarify that the UB also applies to types containing Box.

I am pretty sure that calling ManuallyDrop::drop() on a type containing Box is not UB. rust-lang/unsafe-code-guidelines#245 only states that using the ManuallyDrop later is UB. If you believe that ManuallyDrop::drop() is insta-UB, then I would like a citation to prove otherwise.

Of note, Miri doesn't seem to complain anything if I create a reference to an already-dropped ManuallyDrop<Box<T>>. I believe this is because we are still undecided on whether the existence of a &T implies that the T must be valid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reading a reference, I don’t think I can prove that it’s insta-UB. This is certainly UB per reference though:

let mut x = ManuallyDrop::new(Box::new(42));
unsafe { ManuallyDrop::drop(&mut x); }
let _y = &x;

since it produces (by writing to a place) an invalid value. Reference takes a conservative stance and declares references to invalid values to be invalid:

A reference or Box that is dangling, misaligned, or points to an invalid value (in case of dynamically sized types, using the actual dynamic type of the pointee as determined by the metadata).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From rust-lang/unsafe-code-guidelines#412:

Note that the Rust reference currently answers ["whether a reference requires the pointed-to data to be valid"] with "yes", but in my view this is mostly because we haven't yet figured out what exactly the weaker requirement is that we actually want to impose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know. I still think that documentation should be coherent: if we document this as being unsound in reference, it should be also documented as being unsound in other places that deal with the same situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found out that the nightly version of the reference now states:

A reference or Box<T> must be aligned, it cannot be dangling, and it must point to a valid value (in case of dynamically sized types, using the actual dynamic type of the pointee as determined by the metadata). Note that the last point (about pointing to a valid value) remains a subject of some debate.

@theemathas
Copy link
Contributor Author

This PR is still labelled with S-waiting-for-team. Which team is it waiting on?

@traviscross
Copy link
Contributor

@bors r=thomcc,traviscross rollup=always

@bors
Copy link
Contributor

bors commented Sep 26, 2024

📌 Commit de2eba1 has been approved by thomcc,traviscross

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Sep 26, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 26, 2024
…llaumeGomez

Rollup of 11 pull requests

Successful merges:

 - rust-lang#130279 (Document subtleties of `ManuallyDrop`)
 - rust-lang#130517 (Add the library workspace to the suggested rust-analyzer config)
 - rust-lang#130820 (Fix diagnostics for coroutines with () as input.)
 - rust-lang#130833 (Fix the misleading diagnostic for `let_underscore_drop` on type without `Drop` implementation)
 - rust-lang#130845 (Utf8Chunks: add link to Utf8Chunk)
 - rust-lang#130850 (Pass Module Analysis Manager to Standard Instrumentations)
 - rust-lang#130861 (Use `mem::offset_of!` for `sockaddr_un.sun_path`)
 - rust-lang#130862 (rustdoc: do not animate :target when user prefers reduced motion)
 - rust-lang#130868 (Update FIXME comment in s390x_unknown_linux_*.rs)
 - rust-lang#130879 (Pass correct HirId to late_bound_vars in diagnostic code)
 - rust-lang#130880 (add missing FIXME(const-hack))

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8a827c7 into rust-lang:master Sep 27, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 27, 2024
@theemathas theemathas deleted the manually-drop-docs branch September 27, 2024 01:14
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.