Skip to content

Unsound: Atom accepts an unsafe memory ordering #15

Open
@yvt

Description

@yvt

This crate has the same issue as mystor/atomic_ref#5.

The following code segfaults on an AArch64 machine (but not on x86_64, which has a stronger memory model).

#![deny(unsafe_code)]
use atom::Atom;
use std::sync::atomic::Ordering;

fn main() {
    let channel = &*Box::leak(Box::new(Atom::<&&'static u32>::new(&&42u32)));

    std::thread::spawn(move || loop {
        if let Some(ptr) = channel.take(Ordering::Relaxed) {
            assert_eq!(**ptr, 42);
        }
    });

    for i in 0..10000000 {
        let b = Box::leak(Box::new(&42u32));
        channel.swap(b, Ordering::Relaxed);
    }
}

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