The following code triggers a Miri error (playground):
fn main() {
let v = 123;
let r = &v as *const i32 as *const std::cell::UnsafeCell<i32>;
unsafe { &*r };
}
error: Undefined Behavior: trying to retag from <3119> for SharedReadWrite permission at alloc1656[0x0], but that tag only grants SharedReadOnly permission for this location
--> src/main.rs:4:14
|
4 | unsafe { &*r };
| ^^^
| |
| trying to retag from <3119> for SharedReadWrite permission at alloc1656[0x0], but that tag only grants SharedReadOnly permission for this location
| this error occurs as part of retag at alloc1656[0x0..0x4]
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <3119> was created by a SharedReadOnly retag at offsets [0x0..0x4]
--> src/main.rs:3:13
|
3 | let r = &v as *const i32 as *const std::cell::UnsafeCell<i32>;
| ^^
= note: BACKTRACE (of the first span):
= note: inside `main` at src/main.rs:4:14: 4:17
It only occurs if we cast to UnsafeCell
(or some other type containing it, e.g. Cell
). It seems that because of the UnsafeCell
, miri treates the immutable borrow as SharedReadWrite
, even though we never write into it.
Is this truly a UB or is this a false positive? If a false positive, how can I work around it so Miri won't report it?