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

Miri not detecting aliased mutable reference #1666

Closed
Kestrer opened this issue Jan 10, 2021 · 2 comments
Closed

Miri not detecting aliased mutable reference #1666

Kestrer opened this issue Jan 10, 2021 · 2 comments

Comments

@Kestrer
Copy link

Kestrer commented Jan 10, 2021

I think that this code contains UB:

fn use_mut(r: &mut i32) {
    *r += 1;
    let _ = &mut *r;
}

fn main() {
    let r = &mut 5;
    let p = r as *mut _;
    use_mut(r);
    use_mut(unsafe { &mut *p });
    use_mut(r);
}

But when running under Miri, no errors are produced.

@RalfJung
Copy link
Member

Interesting example, thanks for the report!

This is caused by the fact that the r that is passed to use_mut is a two-phase borrow. Miri treats two-phase borrows like raw pointers -- with the current version of Stacked Borrows, we cannot really do much better. Hopefully a future version of Stacked Borrows can restrict two-phase borrows better.

If you use use_mut(&mut *r) instead of use_mut(r), "real" reborrows happen and you get the expected error.

@RalfJung
Copy link
Member

Closing in favor of rust-lang/unsafe-code-guidelines#85.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants