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

Drop guards in slice sorting derive src pointers from &mut T, which is invalidated by interior mutation in comparison #92092

Merged
merged 1 commit into from
Jan 5, 2022

Conversation

saethlin
Copy link
Member

I tried to run https://github.com/rust-lang/miri-test-libstd on alloc with -Zmiri-track-raw-pointers, and got a failure on the test slice::panic_safe. The test failure has nothing to do with panic safety, it's from how the test tests for panic safety.

I minimized the test failure into this very silly program:

use std::cell::Cell;
use std::cmp::Ordering;

#[derive(Clone)]
struct Evil(Cell<usize>);

fn main() {
    let mut input = vec![Evil(Cell::new(0)); 3];

    // Hits the bug pattern via CopyOnDrop in core
    input.sort_unstable_by(|a, _b| {
        a.0.set(0);
        Ordering::Less
    });

    // Hits the bug pattern via InsertionHole in alloc
    input.sort_by(|_a, b| {
        b.0.set(0);
        Ordering::Less
    });
}

To fix this, I'm just removing the mutability/uniqueness where it wasn't required.

The src pointers in CopyOnDrop and InsertionHole used to be *mut T, and
were derived via automatic conversion from &mut T. According to Stacked
Borrows 2.1, this means that those pointers become invalidated by
interior mutation in the comparison function.

But there's no need for mutability in this code path. Thus, we can
change the drop guards to use *const and derive those from &T.
@rust-highfive
Copy link
Collaborator

r? @yaahc

(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 19, 2021
@camelid camelid added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 19, 2021
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

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

Unnecessary muts indeed!

@m-ou-se
Copy link
Member

m-ou-se commented Jan 5, 2022

@bors r=danielhenrymantilla

@bors
Copy link
Contributor

bors commented Jan 5, 2022

📌 Commit a5a91c8 has been approved by danielhenrymantilla

@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 Jan 5, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 5, 2022
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#92092 (Drop guards in slice sorting derive src pointers from &mut T, which is invalidated by interior mutation in comparison)
 - rust-lang#92388 (Fix a minor mistake in `String::try_reserve_exact` examples)
 - rust-lang#92442 (Add negative `impl` for `Ord`, `PartialOrd` on `LocalDefId`)
 - rust-lang#92483 (Stabilize `result_cloned` and `result_copied`)
 - rust-lang#92574 (Add RISC-V detection macro and more architecture instructions)
 - rust-lang#92575 (ast: Always keep a `NodeId` in `ast::Crate`)
 - rust-lang#92583 (:arrow_up: rust-analyzer)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 56d11a4 into rust-lang:master Jan 5, 2022
@rustbot rustbot added this to the 1.59.0 milestone Jan 5, 2022
@saethlin saethlin deleted the fix-sort-guards-sb branch May 16, 2022 04:36
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-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.

None yet

8 participants