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

Cheat Miri #1742

Closed
nightkidxmd opened this issue Mar 12, 2021 · 1 comment
Closed

Cheat Miri #1742

nightkidxmd opened this issue Mar 12, 2021 · 1 comment
Labels
A-aliasing Area: This affects the aliasing model (Stacked/Tree Borrows)

Comments

@nightkidxmd
Copy link

Build borrow record like below,
Then I can access user.y via ptr successfully.

#![allow(unused)]
fn main() {
    use std::slice;
    struct Test {
        x: [i32; 3],
        y: i32,
    }
    let user = Test { x: [1, 2, 3], y: 4 };
    let ptr = &(user.x) as *const i32;
    let ptr2 = &(user.y) as *const i32; // build borrow record
    for i in 0..4 {
       unsafe { println!("ptr offset {} : {:?} addr{:?}", i, ptr.offset(i), *ptr.offset(i)) };
    }
    let slice = unsafe { slice::from_raw_parts(ptr, 4) };
    println!("slice[3] : {:?}", slice[3]);
}
@RalfJung
Copy link
Member

Thanks for the report!

This is expected behavior: you created ptr2, and since it is a raw pointer, Miri / Stacked Borrows make no attempt at distinguishing that from ptr. So now ptr also has the "permissions" that were granted to ptr2. You can use -Zmiri-track-raw-pointers to change this behavior and use more precise tracking, at the cost of not supporting ptr-int-casts any more.

See rust-lang/unsafe-code-guidelines#248 for the underlying Stacked Borrows issue.

@RalfJung RalfJung added the A-aliasing Area: This affects the aliasing model (Stacked/Tree Borrows) label Jan 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-aliasing Area: This affects the aliasing model (Stacked/Tree Borrows)
Projects
None yet
Development

No branches or pull requests

2 participants