Skip to content

Windows TLS: ManuallyDrop instead of mem::forget #79893

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

Merged
merged 1 commit into from
Dec 11, 2020

Conversation

RalfJung
Copy link
Member

The Windows TLS implementation still used mem::forget instead of ManuallyDrop, leading to the usual problem of "using" the Box when it should not be used any more.

@rust-highfive
Copy link
Contributor

r? @Mark-Simulacrum

(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 Dec 10, 2020
@@ -111,16 +111,13 @@ struct Node {
}

unsafe fn register_dtor(key: Key, dtor: Dtor) {
let mut node = Box::new(Node { key, dtor, next: ptr::null_mut() });
let mut node = ManuallyDrop::new(Box::new(Node { key, dtor, next: ptr::null_mut() }));
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we never drop this anyway, couldn't we also just Box::into_raw here and then compare_exchangeing the raw pointer directly? I guess that would mean node.next = head needs to be (*node).next = head and is now unsafe, so maybe the ManuallyDrop is better, but I am somewhat unhappy with the &mut **node (the previous &mut *node was not better imo). I know we can rely on the deref ops on boxes, but it still seems wrong to me to not be using Box::into_raw when this is technically what we want.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess that would mean node.next = head needs to be (*node).next = head and is now unsafe

I went with into_raw first and decided to use ManuallyDrop when I realized this.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 10, 2020

r? @oli-obk

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 10, 2020

📌 Commit 594b451 has been approved by oli-obk

@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 Dec 10, 2020
mem::forget(node);
return;
}
match DTORS.compare_exchange(head, &mut **node, SeqCst, SeqCst) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't really related, but shouldn't we use compare_exchange_weak here ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Possible; I don't really have a clear idea for what should be used when.
Since it's not related, that should then probably be fixed by someone else in a separate PR. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine, I'll do it when this PR is merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

@a1phyr Did this end up happening?

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 10, 2020
Windows TLS: ManuallyDrop instead of mem::forget

The Windows TLS implementation still used `mem::forget` instead of `ManuallyDrop`, leading to the usual problem of "using" the `Box` when it should not be used any more.
@bors
Copy link
Collaborator

bors commented Dec 11, 2020

⌛ Testing commit 594b451 with merge a2e29d6...

@bors
Copy link
Collaborator

bors commented Dec 11, 2020

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing a2e29d6 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 11, 2020
@bors bors merged commit a2e29d6 into rust-lang:master Dec 11, 2020
@rustbot rustbot added this to the 1.50.0 milestone Dec 11, 2020
bors added a commit to rust-lang/miri that referenced this pull request Dec 11, 2020
enable track-raw-ptr tests on Windows

With rust-lang/rust#79893 landed, raw-pointer-tracking now works even for the Windows runtime. :)
@RalfJung RalfJung deleted the forget-windows branch December 11, 2020 12:26
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants