Why loom is happy with this?

   use loom::sync::Arc;
   use loom::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

   loom::model(|| {
        let ptr = Box::into_raw(Box::new(0));
        let flag = Arc::new(AtomicBool::new(false));
        let flag2 = flag.clone();

        loom::thread::spawn(move || {
            unsafe { *ptr = 1 };
            flag.store(true, Ordering::Relaxed);
        });
        
        loom::thread::spawn(move || {
            if flag2.load(Ordering::Relaxed) {
                assert!(unsafe { *ptr == 1 });
            }
        });
    });

this passes, could someone please explain why? thanks!
edit: oh, UB?

Yeah, UB.

How about using: UnsafeCell in loom::cell - Rust

Also looks like MutPtr would be useful.

If I rewrite it so that Loom can track the raw pointer use, I get:

use loom::sync::Arc;
use loom::sync::atomic::{AtomicBool, Ordering};

fn main() {
    loom::model(|| {
        let cell = loom::cell::UnsafeCell::new(0);
        let flag = Arc::new(AtomicBool::new(false));

        loom::thread::spawn({
            let ptr = cell.get_mut();
            let flag = flag.clone();
            move || {
                unsafe { *ptr.deref() = 1 };
                flag.store(true, Ordering::Relaxed);
            }
        });

        loom::thread::spawn({
            let ptr = cell.get_mut();
            let flag = flag.clone();
            move || {
                if flag.load(Ordering::Relaxed) {
                    assert!(unsafe { *ptr.deref() == 1 });
                }
            }
        });
    });
}

(which does fail)

cool, thanks a lot to both of you! I wish there was a way to mark "solution" to both of the answers, anyways, thanks!

where does one put the SC fence, if I want to synchronize using it?

Prefer not to use SeqCst. A Release store and an Acquire load suffices here. (I suppose I don’t remember which orderings loom supports. Edit: looks like it should work fine, even if there are certain edge cases where loom would not detect UB.)

A release fence before a relaxed store basically makes that store a release store. An acquire fence after a relaxed load basically makes that load an acquire load. (Edit: my original comment accidentally swapped before<->after for release/acquire fences.)

Have you read Mara Bos’ Rust Atomics and Locks book, as is the usual recommendation?

Wait does it? If thread 2 executes the fence but gets suspended right afterwards, then thread 1 stores *ptr and flag, then thread 2 wakes up and reads just flag... it isn't guaranteed to see *ptr is it?

One puts it where the academic paper that you're relying on that explains why you need SeqCst here, and not Release (which goes before the store) or Acquire (which goes after the load) tells you to.

If you don't have an academic paper in hand that explains exactly why you need SeqCst and not Acquire/Release, you should generally avoid using it - it's both an expensive guarantee to provide (especially on high core count multi-socket systems) and weaker than the name implies.

ah, oops, I got the order backwards, you're right. Release fence goes before the store, acquire fence goes after the load.