Skip to content

AtomicCell<Option<Box<T>>> triggers undefined behavior under Miri #908

Closed
@brunocodutra

Description

@brunocodutra

Pointer has no provenance

assert!(AtomicCell::<Option<Box<i32>>>::is_lock_free());
let cell = AtomicCell::new(None);
cell.store(Some(Box::new(1)));
error: Undefined Behavior: dereferencing pointer failed: 0x2c5ad8[noalloc] is a dangling pointer (it has no provenance)
   --> /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:487:1
    |
487 | pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereferencing pointer failed: 0x2c5ad8[noalloc] is a dangling pointer (it has no provenance)
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
    = note: BACKTRACE:
    = note: inside `core::ptr::drop_in_place::<alloc::boxed::Box<i32>> - shim(Some(alloc::boxed::Box<i32>))` at /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:487:1
    = note: inside `core::ptr::drop_in_place::<core::option::Option<alloc::boxed::Box<i32>>> - shim(Some(core::option::Option<alloc::boxed::Box<i32>>))` at /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:487:1
    = note: inside `core::ptr::mut_ptr::<impl *mut core::option::Option<alloc::boxed::Box<i32>>>::drop_in_place` at /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:1424:18
    = note: inside `<crossbeam_utils::atomic::AtomicCell<core::option::Option<alloc::boxed::Box<i32>>> as core::ops::Drop>::drop` at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.11/src/atomic/atomic_cell.rs:319:17
    = note: inside `core::ptr::drop_in_place::<crossbeam_utils::atomic::AtomicCell<core::option::Option<alloc::boxed::Box<i32>>>> - shim(Some(crossbeam_utils::atomic::AtomicCell<core::option::Option<alloc::boxed::Box<i32>>>))` at /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:487:1

Dangling box

assert!(AtomicCell::<Option<Box<i32>>>::is_lock_free());
let cell = AtomicCell::new(Some(Box::new(0)));
cell.store(Some(Box::new(1)));
error: Undefined Behavior: constructing invalid value at .value.<enum-variant(Some)>.0: encountered a dangling box (address 0x2c5a20 is unallocated)
    --> /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:1068:18
     |
1068 | ... { ptr::read(src as *const T as *const U) }
     |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .value.<enum-variant(Some)>.0: encountered a dangling box (address 0x2c5a20 is unallocated)
     |
     = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
     = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
     = note: BACKTRACE:
     = note: inside `core::mem::transmute_copy::<u64, core::option::Option<alloc::boxed::Box<i32>>>` at /home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:1068:18
     = note: inside `crossbeam_utils::atomic::atomic_cell::atomic_swap::<core::option::Option<alloc::boxed::Box<i32>>>` at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.11/src/atomic/atomic_cell.rs:1053:23
     = note: inside `crossbeam_utils::atomic::AtomicCell::<core::option::Option<alloc::boxed::Box<i32>>>::swap` at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.11/src/atomic/atomic_cell.rs:167:18
     = note: inside `crossbeam_utils::atomic::AtomicCell::<core::option::Option<alloc::boxed::Box<i32>>>::store` at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.11/src/atomic/atomic_cell.rs:145:18

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions