Skip to content

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Sep 5, 2020

Fixes #76367.

@rust-highfive
Copy link
Contributor

r? @withoutboats

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 5, 2020
@matklad
Copy link
Contributor

matklad commented Sep 5, 2020

Wow, this is super-nice find, thanks @m-ou-se!

Can we add compile-fail tests here (maybe just onto the PhantomData field)?

https://github.com/matklad/once_cell/blob/0b65b1523453a0cfbdeeb756a77e0b85e03ea7bf/src/lib.rs#L980-L994

@matklad
Copy link
Contributor

matklad commented Sep 5, 2020

And I guess the dropcheck test still passes, so that's good:

#[test]
fn dropck() {
let cell = SyncOnceCell::new();
{
let s = String::new();
cell.set(&s).unwrap();
}
}
.

r? @KodrAus

This looks good awesome to me, but I will be traveling, so won't be able to r+

@rust-highfive rust-highfive assigned KodrAus and unassigned withoutboats Sep 5, 2020
@matklad
Copy link
Contributor

matklad commented Sep 5, 2020

@m-ou-se out of curiosity, how did you found out about this issue?

@matklad
Copy link
Contributor

matklad commented Sep 5, 2020

Also, I guess cc @RalfJung :) I find it surprising that UnsafeCell<MaybeUninit<T>> and PhantomData<T> differe in their effects on the drop check. Which of UnsafeCell / MaybeUninit / ManuallyDrop is responsible for that?

@m-ou-se
Copy link
Member Author

m-ou-se commented Sep 5, 2020

@m-ou-se out of curiosity, how did you found out about this issue?

Uh, I'm not sure. ^^' I have a lot of tabs open in my browser with interesting PRs I want to take a closer look at at some point, and #75648 was one of those. I don't remember why I had this one open. I guess I wanted to remind myself to take a closer look at how #[may_dangle] works? Came across it today while cleaning up my tabs, and played around with things a bit until it broke.

@m-ou-se
Copy link
Member Author

m-ou-se commented Sep 5, 2020

Also, I guess cc @RalfJung :) I find it surprising that UnsafeCell<MaybeUninit<T>> and PhantomData<T> differe in their effects on the drop check. Which of UnsafeCell / MaybeUninit / ManuallyDrop is responsible for that?

I think it's working as intended. It's perfectly safe to drop a MaybeUninit<T> with a dangling T, since it doesn't drop the T when dropped. Hard to discover problems like this though. Maybe the documentation of #[may_dangle] could use an extra warning about this situation.

@m-ou-se
Copy link
Member Author

m-ou-se commented Sep 5, 2020

Can we add compile-fail tests here (maybe just onto the PhantomData field)?

Sounds like a good idea. What's the best way of doing compile_fail tests in std? As a doctest on some private item, or does std have another preferred way of doing this?

@RalfJung
Copy link
Member

RalfJung commented Sep 5, 2020

Also, I guess cc @RalfJung :) I find it surprising that UnsafeCell<MaybeUninit<T>> and PhantomData<T> differe in their effects on the drop check. Which of UnsafeCell / MaybeUninit / ManuallyDrop is responsible for that?

I guess it's ManuallyDrop that has the effect here. dropck is about the code that runs on drop, and with ManuallyDrop there is no such code, so this makes sense (as @m-ou-se said). If a manual drop anyway drops some T, a PhantomData is required.

@m-ou-se
Copy link
Member Author

m-ou-se commented Sep 5, 2020

I've added a compile_fail test in the doc comment on the _marker: PhantomData.

@jyn514 jyn514 added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 5, 2020
@nagisa nagisa added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 5, 2020
@nagisa
Copy link
Member

nagisa commented Sep 5, 2020

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 5, 2020

📌 Commit e56ea68 has been approved by nagisa

@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. labels Sep 5, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Sep 5, 2020
…ll-soundness, r=nagisa

Fix dropck issue of SyncOnceCell.

Fixes rust-lang#76367.
@KodrAus
Copy link
Contributor

KodrAus commented Sep 6, 2020

Nice find! I’ll update our forge docs on #[may_dangle] to include this as something to watch out for.

@bors
Copy link
Collaborator

bors commented Sep 6, 2020

⌛ Testing commit e56ea68 with merge 23e49dd...

@bors
Copy link
Collaborator

bors commented Sep 6, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: nagisa
Pushing 23e49dd to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 6, 2020
@bors bors merged commit 23e49dd into rust-lang:master Sep 6, 2020
@rustbot rustbot added this to the 1.48.0 milestone Sep 6, 2020
@m-ou-se m-ou-se deleted the synconcecell-soundness branch December 30, 2020 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Soundness issue in Drop for SyncOnceCell
10 participants