Skip to content

Why is this code illegal? #8

@dfoxfranke

Description

@dfoxfranke

My understanding of this crate was that it ought to make this sort of thing legal:

use std::ptr::addr_of_mut;
use aliasable::boxed::AliasableBox;

fn main() {
    let mut b = AliasableBox::from_unique(Box::new(1usize));
    let p = addr_of_mut!(*b);
    *b = 2;
    assert_eq!(unsafe { p.read() }, 2);
}

It does seem to run fine, even if I hide various pieces of it inside #[inline(never)] functions. However, miri complains about it:

error: Undefined Behavior: attempting a read access using <2514> at alloc1093[0x0], but that tag does not exist in the borrow stack for this location
 --> src/main.rs:8:25
  |
8 |     assert_eq!(unsafe { p.read() }, 2);
  |                         ^^^^^^^^
  |                         |
  |                         attempting a read access using <2514> at alloc1093[0x0], but that tag does not exist in the borrow stack for this location
  |                         this error occurs as part of an access at alloc1093[0x0..0x8]
  |
  = 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: <2514> was created by a SharedReadWrite retag at offsets [0x0..0x8]
 --> src/main.rs:6:13
  |
6 |     let p = addr_of_mut!(*b);
  |             ^^^^^^^^^^^^^^^^
help: <2514> was later invalidated at offsets [0x0..0x8] by a Unique retag
 --> src/main.rs:7:5
  |
7 |     *b = 2;
  |     ^^
  = note: BACKTRACE (of the first span):
  = note: inside `main` at src/main.rs:8:25: 8:33
  = note: this error originates in the macro `addr_of_mut` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

Is this a bug in miri, a bug in aliasable, or a bug in my understanding?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions