Skip to content

Should atomic loads and failing atomic RMWs be writes for the purpose of the aliasing model and data races? #355

@RalfJung

Description

@RalfJung

Specifically, should this code be UB?

use std::sync::atomic::{AtomicI32, Ordering};

fn main() {
    let x = &AtomicI32::new(0);
    let y = x as *const AtomicI32 as *const i32;
    let y = unsafe { &*y };
    x.compare_exchange(1, 2, Ordering::Relaxed, Ordering::Relaxed).unwrap_err();
    let _val = y;
}

Right now Miri accepts this, since the failed RMW is just considered a read by the aliasing model. But maybe it should be considered a write? We almost certainly want to disallow it on read-only memory since that pagefaults on x86, so making it a write also for other concerns seems more consistent.

OTOH, that could mean that a failing RMW that races with a non-atomic read might be considered a data race, and I am not sure if that is the right semantics.

Thanks to @chorman0773 for bringing up the question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions