atom icon indicating copy to clipboard operation
atom copied to clipboard

Unsound: `Atom` accepts an unsafe memory ordering

Open yvt opened this issue 4 years ago • 2 comments

This crate has the same issue as https://github.com/mystor/atomic_ref/issues/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);
    }
}

yvt avatar Sep 19 '21 03:09 yvt

Thank you for the report, can you review #17 and tell me if it's fixed, please?

torkleyy avatar Oct 08 '21 20:10 torkleyy

I hope this can be fixed at some point. I want to use this crate, because Atom is exactly the synchronization primitive that I've wanted for several of my projects, and I appreciate the work that's gone into developing it. But I can't bring myself to use a dependency that has been "known to be unsound" for nearly a year without being updated...

elidupree avatar Jun 08 '22 21:06 elidupree