Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialisation value of atomic types can no longer be observed if the first atomic access is a store #2164

Closed
cbeuw opened this issue May 29, 2022 · 0 comments · Fixed by rust-lang/rust#128942
Labels
A-weak-memory Area: emulation of weak memory effects (store buffers) C-bug Category: This is a bug.

Comments

@cbeuw
Copy link
Contributor

cbeuw commented May 29, 2022

use std::sync::atomic::Ordering::*;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::thread::spawn;

pub fn main() {
    // Give these 'static lifetime to avoid using Arc
    static X: AtomicUsize = AtomicUsize::new(0);
    static T1_DONE: AtomicBool = AtomicBool::new(false);

    let t1 = spawn(move || {
        X.store(1, Relaxed);
        
        T1_DONE.store(true, Relaxed);
    });

    let t2 = spawn(move || {
        while !T1_DONE.load(Relaxed) {}

        X.load(Relaxed)
    });

    t1.join().unwrap();
    let r = t2.join().unwrap();

    println!("{r}");
}

On platforms with weaker memory models (such as ARM), it should be possible for the above program to print 0, because X.store(1, Relaxed) does not happen before X.load(Relaxed) so it's allowed to observe an "outdated" value.

However, on Miri, once X.store(1, Relaxed) is executed, the initialisation value 0 can never be observed again. This is because the store buffers used to keep the atomic stores are lazily allocated on the first atomic access of a location. We can't obtain the value in the underlying location in the atomic write shim because calling read_scalar() on the location has side effects which implies that a read on the location happened in the MIR even though there isn't one.

To fix, rustc_const_eval needs to provide a method letting us obtain the value on a location for internal machine implementation rather than user program behaviour

@cbeuw cbeuw changed the title Initialisation value of atomic types can no longer be observed if the first atomic access is a write Initialisation value of atomic types can no longer be observed if the first atomic access is a store May 29, 2022
@RalfJung RalfJung added C-bug Category: This is a bug. A-weak-memory Area: emulation of weak memory effects (store buffers) labels Jun 5, 2022
cbeuw added a commit to cbeuw/miri that referenced this issue Jun 5, 2022
cbeuw added a commit to cbeuw/miri that referenced this issue Jun 5, 2022
RalfJung added a commit to RalfJung/miri that referenced this issue Jul 14, 2022
LegNeato pushed a commit to LegNeato/miri that referenced this issue Jul 14, 2022
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Aug 27, 2024
…saethlin

miri weak memory emulation: put previous value into initial store buffer

Fixes rust-lang/miri#2164 by doing a read before each atomic write so that we can initialize the store buffer. The read suppresses memory access hooks and UB exceptions, to avoid otherwise influencing the program behavior. If the read fails, we store that as `None` in the store buffer, so that when an atomic read races with the first atomic write to some memory and previously the memory was uninitialized, we can report UB due to reading uninit memory.

`@cbeuw` this changes a bit the way we initialize the store buffers. Not sure if you still remember all this code, but if you could have a look to make sure this still makes sense, that would be great. :)

r? `@saethlin`
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 27, 2024
Rollup merge of rust-lang#128942 - RalfJung:interpret-weak-memory, r=saethlin

miri weak memory emulation: put previous value into initial store buffer

Fixes rust-lang/miri#2164 by doing a read before each atomic write so that we can initialize the store buffer. The read suppresses memory access hooks and UB exceptions, to avoid otherwise influencing the program behavior. If the read fails, we store that as `None` in the store buffer, so that when an atomic read races with the first atomic write to some memory and previously the memory was uninitialized, we can report UB due to reading uninit memory.

``@cbeuw`` this changes a bit the way we initialize the store buffers. Not sure if you still remember all this code, but if you could have a look to make sure this still makes sense, that would be great. :)

r? ``@saethlin``
github-actions bot pushed a commit that referenced this issue Aug 28, 2024
miri weak memory emulation: put previous value into initial store buffer

Fixes #2164 by doing a read before each atomic write so that we can initialize the store buffer. The read suppresses memory access hooks and UB exceptions, to avoid otherwise influencing the program behavior. If the read fails, we store that as `None` in the store buffer, so that when an atomic read races with the first atomic write to some memory and previously the memory was uninitialized, we can report UB due to reading uninit memory.

``@cbeuw`` this changes a bit the way we initialize the store buffers. Not sure if you still remember all this code, but if you could have a look to make sure this still makes sense, that would be great. :)

r? ``@saethlin``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-weak-memory Area: emulation of weak memory effects (store buffers) C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants