-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Fix double free when a comparator panics mid-split_off #158710
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
Closed
chatman-media
wants to merge
1
commit into
rust-lang:main
from
chatman-media:fix-btree-split-off-panic-safety
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use core::alloc::Allocator; | ||
| use core::borrow::Borrow; | ||
| use core::{intrinsics, mem}; | ||
|
|
||
| use super::node::ForceResult::*; | ||
| use super::node::Root; | ||
|
|
@@ -44,13 +45,40 @@ impl<K, V> Root<K, V> { | |
| let mut left_node = left_root.borrow_mut(); | ||
| let mut right_node = right_root.borrow_mut(); | ||
|
|
||
| // Once the first `move_suffix` below has run, `left_root` and | ||
| // `right_root` reference a common set of key-value pairs through two | ||
| // different tree structures, and neither is a valid, independently | ||
| // droppable `BTreeMap` until `fix_right_border`/`fix_left_border` | ||
| // have repaired them and the caller has recomputed both lengths. The | ||
| // only thing that can fail beyond this point is the caller-supplied | ||
| // `Ord`/`Borrow` impl invoked by `search_node`. If that panics, we | ||
| // cannot safely unwind with the trees in this intermediate state | ||
| // (doing so leads to a double free, see #158165), so abort instead, | ||
| // matching the panic-safety strategy used elsewhere in this module | ||
| // (see `mem::replace`). | ||
| struct AbortOnDrop; | ||
| impl Drop for AbortOnDrop { | ||
| fn drop(&mut self) { | ||
| intrinsics::abort() | ||
| } | ||
| } | ||
| let mut guard = None; | ||
|
|
||
| loop { | ||
| let mut split_edge = match left_node.search_node(key) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to help me understand: this call to |
||
| // key is going to the right tree | ||
| Found(kv) => kv.left_edge(), | ||
| GoDown(edge) => edge, | ||
| }; | ||
|
|
||
| // From here on, `left_root` and `right_root` are both | ||
| // unsound to drop until the loop finishes and the borders | ||
| // are fixed up below. Use `get_or_insert_with` rather than | ||
| // `get_or_insert`: the latter takes its argument by value, so | ||
| // it would construct (and immediately drop, aborting) a fresh | ||
| // `AbortOnDrop` on every iteration after the first. | ||
| guard.get_or_insert_with(|| AbortOnDrop); | ||
|
|
||
| split_edge.move_suffix(&mut right_node); | ||
|
|
||
| match (split_edge.force(), right_node.force()) { | ||
|
|
@@ -65,6 +93,7 @@ impl<K, V> Root<K, V> { | |
|
|
||
| left_root.fix_right_border(alloc.clone()); | ||
| right_root.fix_left_border(alloc); | ||
| mem::forget(guard); | ||
| right_root | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Let's also add the actual failing code from #158165 as a regression test.
View changes since the review