-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Prevent Vec::drain_filter from double dropping on panic #61224
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
r? @RalfJung |
Cc @rust-lang/libs would appreciate if one of you could take over review, this is a bit outside my comfort zone. |
let del = self.del; | ||
let src: *const T = &v[i]; | ||
let dst: *mut T = &mut v[i - del]; | ||
// This is safe because self.vec has length 0 | ||
// thus its elements will not have Drop::drop | ||
// called on them in the event of a panic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment no longer true? Why is it safe instead then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the intent was to leak instead of double drop, but it didn't quite work. I've done some minor refactoring and added additional comments.
It's safe now because there are additional checks in DrainFilter::drop
that perform the cleanup in the event of a panic in the filter predicate.
Is this PR still being worked on? I'd love to see this unsound behavior disappear. |
From what I can see, this is waiting for a reviewer. As mentioned above, I don't feel comfortable reviewing this. Cc @rust-lang/libs |
r? @gankro |
@gankro Any feedback? |
yeah sorry, i plan to review this this morning, just caught me coming off a very draining work trip and leading into a long weekend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach looks good, assuming that panicking is guaranteed to behave as assumed here. (compiler devs have always been hand-wavy to me about this topic).
Mostly some cosmetic concerns, as "the right way to write unsafe code" has changed a fair bit since this code landed, and we should take the opportunity to bring this code up to snuff. In particular, creating a slice for the whole buffer when we're de-initializing parts of it is very dubious.
src/liballoc/vec.rs
Outdated
// but was not due to a panic in the filter predicate. This is | ||
// implemented via drop so that it's guaranteed to run even in | ||
// the event of a panic while consuming the remainder of the | ||
// DrainFilter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(currently checking with the lang team that we guarantee that this works)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @rust-lang/lang @matthewjasper @arielb1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
specifically: if we panic in a destructor and we weren't yet panicking:
- we unwind the
drop
impl, dropping its locals - then drop_in_place
self
's fields as normal - then proceed into a normal panic, unwinding the stack
(the middle point I'm not sure is necessary here, but is just my understanding of what is supposed to happen.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc also rust-lang/reference#514
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* then drop_in_place `self`'s fields as normal
This is indeed what we're currently doing:
rust/src/test/mir-opt/unusual-item-types.rs
Lines 48 to 74 in 254f201
// START rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir | |
// bb0: { | |
// goto -> bb7; | |
// } | |
// bb1: { | |
// return; | |
// } | |
// bb2 (cleanup): { | |
// resume; | |
// } | |
// bb3: { | |
// goto -> bb1; | |
// } | |
// bb4 (cleanup): { | |
// goto -> bb2; | |
// } | |
// bb5 (cleanup): { | |
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> bb4; | |
// } | |
// bb6: { | |
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> [return: bb3, unwind: bb4]; | |
// } | |
// bb7: { | |
// _2 = &mut (*_1); | |
// _3 = const <std::vec::Vec<i32> as std::ops::Drop>::drop(move _2) -> [return: bb6, unwind: bb5]; | |
// } | |
// END rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gankro your understanding is correct. If drop panics:
- we unwind the drop impl, dropping its locals,
- then drop_in_place self's fields as normal (where normal means in field declaration order - first field is dropped before second field is dropped),
- then we continue unwinding
Drop::drop
's caller stack
If dropping a value in Drop::drop
stack while its unwinding panics, or if dropping one of the type's fields panics, then we have a double-drop, and the process is guarantee to abort for all double drops.
Note also that the panic doesn't need to originate in the Drop::impl, this one can succeed, but then the fields are dropped in declaration order, and one of them can panic. When this happens, the remaining fields are still dropped in place, so if a second one of them panics, then you have a double drop. If the Drop::impl does not panic, but dropping one of the fields panics, and you don't get a double drop, you are guaranteed that the field that panicked is the only one that wasn't properly dropped.
EDIT: for all practical purposes, if dropping a value panics, you should treat that value as dropped, since "most" of the value will have been dropped in the process (at most one field wasn't dropped).
let dst: *mut T = &mut v[i - del]; | ||
ptr::copy_nonoverlapping(src, dst, 1); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this while loop exists. Surely this should just be:
if self.drain.idx < self.drain.old_len {
// It looks like `pred` panicked, so we didn't process all the elements.
// This is a pretty messed up state, and there isn't really an obviously right
// thing to do (and we don't want to keep trying to execute `pred`). So we
// just backshift all the unprocessed elements and tell the vec that they still
// exist, hoping that doesn't mess up anyone further along in the panic.
let idx = self.drain.idx;
let num_deleted = self.drain.del;
let tail_len = self.drain.old_len - idx;
let ptr = self.drain.vec.as_mut_ptr();
if num_deleted > 0 {
ptr.add(idx).copy_to(ptr.add(idx - num_deleted), tail_len);
}
}
self.drain.vec.set_len(self.drain.old_len - self.drain.del);
(Here I modernized the code a bit to use the newer raw pointer APIs, and some clearer names. It would be a nice cleanup to this code if you also did that to the Iterator's fields and its next impl as well, although not a blocker.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I incorporated this in the latest commit. I also consolidated the the self.drain.del
check into the parent if
and made the src
and dst
pointers explicit while still using the more modern pointer APIs.
@@ -2751,6 +2752,7 @@ pub struct DrainFilter<'a, T, F> | |||
del: usize, | |||
old_len: usize, | |||
pred: F, | |||
panic_flag: bool, | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has grown enough fields that they should really be documented. Either individually here, or with a high-level description of what we're trying to do in the implementation of next
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added docs to each field in the latest commit.
if (self.pred)(&mut v[i]) { | ||
self.panic_flag = true; | ||
let drained = (self.pred)(&mut v[i]); | ||
self.panic_flag = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a terrible compulsion to try to encode this state in some magic combination of old_len/idx/del, but this is probably clearest, and easiest for llvm to evaporate when it sees pred can't unwind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that route initially but came to the conclusion that even if it's possible, the simple flag would be much easier to understand and maintain.
This comment has been minimized.
This comment has been minimized.
@bors delegate=Gankro |
✌️ @gankro can now approve this pull request |
@bors r+ |
📌 Commit df5b32e has been approved by |
⌛ Testing commit df5b32e with merge 53cb0008f8a448df6461773fd42a8c81a77637dd... |
💔 Test failed - checks-azure |
|
@bors retry |
☀️ Test successful - checks-azure, checks-travis, status-appveyor |
This PR reverts rust-lang#48065, which aimed to optimize `Vec::retain` by making use of `Vec::drain_filter`. Unfortunately at that time, `drain_filter` was unsound. The soundness hole in `Vec::drain_filter` was fixed in rust-lang#61224 by guaranteeing that cleanup logic runs via a nested `Drop`, even in the event of a panic. Implementing this nested drop affects codegen (apparently?) and results in slower code. Fixes rust-lang#65970
Restore original implementation of Vec::retain This PR reverts rust-lang#48065, which aimed to optimize `Vec::retain` by making use of `Vec::drain_filter`. Unfortunately at that time, `drain_filter` was unsound. The soundness hole in `Vec::drain_filter` was fixed in rust-lang#61224 by guaranteeing that cleanup logic runs via a nested `Drop`, even in the event of a panic. Implementing this nested drop affects codegen (apparently?) and results in slower code. Fixes rust-lang#65970
Fixes: #60977
The changes in this PR prevent leaking and double-panicking in addition to double-drop.
Tracking issue: #43244