External Publication
Visit Post

Include racy reads in Rust memory model with `MaybeInvalid<T>`

Rust Internals [Unofficial] May 25, 2026
Source

RalfJung:

I'm afraid this will remain hypothetical until someone actually works out a memory model that can do this.

At the risk of rehashing one of our previous discussions, I believe that inline assembly is a perfectly valid escape hatch for SeqLock. In other words, something like this could be considered sound in the context of SeqLock:

unsafe fn racy_read<T>(src_ptr: *const T) -> MaybeUninit<T> {
    let mut dst: MaybeUninit<T> = MaybeUninit::uninit();
    unsafe {
        core::arch::asm!(
            "<read T from src_ptr into dst>",
            in(reg) src_ptr,
            in(reg) size_of::<T>(),
            in(reg) &mut dst,
            options(preserves_flags),
        );
    }
    dst
}

Yes, we can not properly describe what the block does in terms of AM, but we can say that it reads something into dst and the resulting data can be interpreted as T (by calling assume_init) only if the SeqLock validity check has passed. The write probably also should be accompanied by an (empty) asm block to "observe" the write.

Validity of this reasoning relies on "external" knowledge of knowing that the assembly block does on the given CPU arch.

Whether such operation should be or could be exposed on the language level is a separate question.

Discussion in the ATmosphere

Loading comments...