# Possible false-positive in MIRI race detector

**URL:** <https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081>\
**Category:** Unsafe Code Guidelines\
**Created:** [December 26, 2023, 12:04am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081 "2023-12-26T00:04:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![AngelicosPhosphoros](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/angelicosphosphoros/32/7346_2.png) [@AngelicosPhosphoros](https://internals.rust-lang.org/u/AngelicosPhosphoros)\
**Post date:** [December 26, 2023, 12:04am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/1 "2023-12-26T00:04:56Z")

</div>

Should this code be legal? Main thread sends pointer to its local variable to another thread using relaxed AtomicPtr operations, second thread only writes to that variable then notifies main thread using Release - Acquire pair.

1. Since first thread do single write before starting second thread and never does any writes after, there should not be any need to synchronize any reads from variable made in second thread even if it had read it.
2. Since second thread does only writes to variable, it should not need to acquire it (e.g. make it current value visible to second thread) so it should not require synchronization with sender.

Why it is not allowed?

```rust
use std::sync::atomic::Ordering::*;
use std::sync::atomic::*;
use std::sync::*;

static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
static MUTEX: Mutex<()> = Mutex::new(());
static CONDVAR: Condvar = Condvar::new();
static IS_FINISHED: AtomicBool = AtomicBool::new(false);

fn main() {
    for _ in 0..1000 {
        P.store(core::ptr::null_mut(), Relaxed);
        IS_FINISHED.store(false, Relaxed);

        let mut val: u8 = 0;

        let _t1 = std::thread::spawn(|| {
            while P.load(Relaxed).is_null() {
                std::hint::spin_loop();
            }
            unsafe {
                let ptr = P.load(Relaxed);
                // Access 2
                *ptr = 127;
            }

            IS_FINISHED.store(true, Release);
            let _g = MUTEX.lock().unwrap();
            CONDVAR.notify_one();
        });

        // Access 1
        P.store(&mut val, Relaxed);
        let mut guard = MUTEX.lock().unwrap();
        while !IS_FINISHED.load(Acquire) {
            guard = CONDVAR.wait(guard).unwrap();
        }

        // Access 3
        assert_eq!(val, 127);
    }
}

```

rust version:

```rust
rustc 1.77.0-nightly (bf8716f1c 2023-12-24)
binary: rustc
commit-hash: bf8716f1cd6416266807706bcae0ecb2e51c9d4a
commit-date: 2023-12-24
host: x86_64-pc-windows-msvc
release: 1.77.0-nightly
LLVM version: 17.0.6

```

MIRI output:

```rust
error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `main` and (2) non-atomic write on thread `<unnamed>` at alloc1927. (2) just happened here
  --> src\main.rs:24:17
   |
24 | *ptr = 127;
   | ^^^^^^^^^^ Data race detected between (1) non-atomic write on thread `main` and (2) non-atomic write on thread `<unnamed>` at alloc1927. (2) just happened here
   |
help: and (1) occurred earlier here
  --> src\main.rs:33:17
   |
33 | P.store(&mut val, Relaxed);
   | ^^^^^^^^
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
   = note: BACKTRACE (of the first span):
   = note: inside closure at src\main.rs:24:17: 24:27

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

```

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 26, 2023, 12:45am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/2 "2023-12-26T00:45:16Z")

</div>

I can’t comment on the question whether this is intended behavior, but I can minimize your example:

```rust
use std::sync::atomic::{AtomicPtr, Ordering::Relaxed};

static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());

fn main() {
    let val: u8 = 0;

    // take reference here
    let r = &val;

    // start thread here
    let t = std::thread::spawn(|| loop {
        match P.load(Relaxed) {
            ptr if ptr.is_null() => std::hint::spin_loop(),
            ptr => {
                unsafe {
                    let _v = *ptr;
                }
                break;
            }
        }
    });

    P.store(r as *const u8 as _, Relaxed);

    t.join().unwrap();
}

```

miri is happy

* * *

```rust
use std::sync::atomic::{AtomicPtr, Ordering::Relaxed};

static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());

fn main() {
    let val: u8 = 0;

    // start thread here
    let t = std::thread::spawn(|| loop {
        match P.load(Relaxed) {
            ptr if ptr.is_null() => std::hint::spin_loop(),
            ptr => {
                unsafe {
                    let _v = *ptr;
                }
                break;
            }
        }
    });

    // take reference here
    let r = &val;

    P.store(r as *const u8 as _, Relaxed);

    t.join().unwrap();
}

```

```rust
error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `main` and (2) non-atomic read on thread `<unnamed>` at alloc1507. (2) just happened here
  --> src/main.rs:14:30
   |
14 | let _v = *ptr;
   | ^^^^ Data race detected between (1) non-atomic write on thread `main` and (2) non-atomic read on thread `<unnamed>` at alloc1507. (2) just happened here
   |
help: and (1) occurred earlier here
  --> src/main.rs:22:13
   |
22 | let r = &val;
   | ^^^^
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
   = note: BACKTRACE (of the first span):
   = note: inside closure at src/main.rs:14:30: 14:34

```

I’m not entirely sure why creating `&val` counts as a “non-atomic write” (to `val`), as far as I can tell.

---

<div class="post-metadata">

**Author:** ![AngelicosPhosphoros](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/angelicosphosphoros/32/7346_2.png) [@AngelicosPhosphoros](https://internals.rust-lang.org/u/AngelicosPhosphoros)\
**Post date:** [December 26, 2023, 12:53am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/3 "2023-12-26T00:53:23Z")

</div>

@steffahn You example is unsound because you modify contents of immutable variable.

It should definitely trigger MIRI error as a write using readonly reference. Do you think we should open an issue about that?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 26, 2023, 12:55am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/4 "2023-12-26T00:55:30Z")

</div>

The plot thickens… apparently, this alone makes a difference in my minimized example… (I’m not entirely sure what _exactly_ is going on here, and whether or not it’s all intended behavior).

```rust
use std::sync::atomic::{AtomicPtr, Ordering::Relaxed};

static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());

fn main() {
    let val: u8 = 0;

    // already take a (unused) reference here
    let _ = &val;

    // take reference here
    let t = std::thread::spawn(|| loop {
        match P.load(Relaxed) {
            ptr if ptr.is_null() => std::hint::spin_loop(),
            ptr => {
                unsafe {
                    let _v = *ptr;
                }
                break;
            }
        }
    });

    // take the actual reference here
    let r = &val;

    P.store(r as *const u8 as _, Relaxed);

    t.join().unwrap();
}

```

* * *

Regarding your original code, I would just _assume_ that Rust probably has a rule like “when `&mut …expr…` is evaluated, then `…expr…` must be valid for writes _at that point_”. And this means, for soundness purposes, a write _happens_ at that point, i.e. after the thread was started, and that write is unsynchronized. E.g. this is accepted (though I would probably even make sure to create the _pointer_ before the thread starts):

```rust
use std::sync::atomic::Ordering::*;
use std::sync::atomic::*;
use std::sync::*;

static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
static MUTEX: Mutex<()> = Mutex::new(());
static CONDVAR: Condvar = Condvar::new();
static IS_FINISHED: AtomicBool = AtomicBool::new(false);

fn main() {
    for _ in 0..10 {
        P.store(core::ptr::null_mut(), Relaxed);
        IS_FINISHED.store(false, Relaxed);

        let mut val: u8 = 0;
        let r = &mut val;

        let _t1 = std::thread::spawn(|| {
            while P.load(Relaxed).is_null() {
                std::hint::spin_loop();
            }
            unsafe {
                let ptr = P.load(Relaxed);
                // Access 2
                *ptr = 127;
            }

            IS_FINISHED.store(true, Release);
            let _g = MUTEX.lock().unwrap();
            CONDVAR.notify_one();
        });

        // Access 1
        P.store(r, Relaxed);
        let mut guard = MUTEX.lock().unwrap();
        while !IS_FINISHED.load(Acquire) {
            guard = CONDVAR.wait(guard).unwrap();
        }

        // Access 3
        assert_eq!(val, 127);
    }
}

```

but this isn’t

```rust
use std::sync::atomic::Ordering::*;
use std::sync::atomic::*;
use std::sync::*;

static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
static MUTEX: Mutex<()> = Mutex::new(());
static CONDVAR: Condvar = Condvar::new();
static IS_FINISHED: AtomicBool = AtomicBool::new(false);

fn main() {
    for _ in 0..10 {
        P.store(core::ptr::null_mut(), Relaxed);
        IS_FINISHED.store(false, Relaxed);

        let mut val: u8 = 0;
        let r = &mut val;

        let _t1 = std::thread::spawn(|| {
            while P.load(Relaxed).is_null() {
                std::hint::spin_loop();
            }
            unsafe {
                let ptr = P.load(Relaxed);
                // Access 2
                *ptr = 127;
            }

            IS_FINISHED.store(true, Release);
            let _g = MUTEX.lock().unwrap();
            CONDVAR.notify_one();
        });

        let r = &mut *r;

        // Access 1
        P.store(r, Relaxed);
        let mut guard = MUTEX.lock().unwrap();
        while !IS_FINISHED.load(Acquire) {
            guard = CONDVAR.wait(guard).unwrap();
        }

        // Access 3
        assert_eq!(val, 127);
    }
}

```

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 26, 2023, 12:57am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/5 "2023-12-26T00:57:28Z")

</div>

> [@AngelicosPhosphoros](#):
>
> You example is unsound because you modify contents of immutable variable.

No I don’t? I just read… `let _v = *ptr;` in my “minimized” (one might even say “altered”) example.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [December 26, 2023, 1:11am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/6 "2023-12-26T01:11:18Z")

</div>

> [@steffahn](#):
>
> Regarding your original code, I would just _assume_ that Rust probably has a rule like “when `&mut …expr…` is evaluated, then `…expr…` must be valid for writes _at that point_”. And this means, for soundness purposes, a write _happens_ at that point, i.e. after the thread was started, and that write is unsynchronized.

At least NLL wise, [`&mut LV` is a "deep write" of `LV`,](https://rust-lang.github.io/rfcs/2094-nll.html#borrow-checker-phase-2-reporting-errors) and I assumed with my own tinkering that was what Miri was talking about with `&mut val` being a non-atomic write. But that doesn't explain why `&val` would also be called a write (in NLL it's a "deep read").

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [December 26, 2023, 1:15am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/7 "2023-12-26T01:15:19Z")

</div>

> [@quinedot](#):
>
> But that doesn't explain why `&val` would also be called a write (in NLL it's a "deep read").

Intuitively, I'd guess maybe the compiler wants to be allowed to initialize `val` only after the thread was started, if it had otherwise been unused before, and thus calls out the `&val` as the first use site as a write even though the true write operation is the initialization. I'm not familiar with the formal rules that may or may not validate this interpretation.

* * *

Edit: Some minor testing suggests it might be less about “first usage” of a variable and more about “first usage that forces the variable to have a location in memory”.

> **(see my “minor testing” here)**
>
> Like… this fails:
> 
> ```rust
> use std::sync::atomic::{AtomicPtr, Ordering::Relaxed};
> 
> static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
> 
> fn main() {
> let val: u8 = 0;
> 
> let val2 = val;
> println!("{val2}");
> 
> // take reference here
> let t = std::thread::spawn(|| loop {
> match P.load(Relaxed) {
> ptr if ptr.is_null() => std::hint::spin_loop(),
> ptr => {
> unsafe {
> let _v = *ptr;
> }
> break;
> }
> }
> });
> 
> // already take a (unused) reference here
> let _ = &val;
> 
> // take the actual reference here
> let r = &val;
> 
> P.store(r as *const u8 as _, Relaxed);
> 
> t.join().unwrap();
> }
> 
> ```
> 
> but this doesn’t
> 
> ```rust
> use std::sync::atomic::{AtomicPtr, Ordering::Relaxed};
> 
> static P: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
> 
> fn main() {
> let val: u8 = 0;
> 
> let val2 = *&val; // or e.g. val.clone()
> println!("{val2}");
> 
> // take reference here
> let t = std::thread::spawn(|| loop {
> match P.load(Relaxed) {
> ptr if ptr.is_null() => std::hint::spin_loop(),
> ptr => {
> unsafe {
> let _v = *ptr;
> }
> break;
> }
> }
> });
> 
> // already take a (unused) reference here
> let _ = &val;
> 
> // take the actual reference here
> let r = &val;
> 
> P.store(r as *const u8 as _, Relaxed);
> 
> t.join().unwrap();
> }
> 
> ```

Of course, we might or might not be also dealing with some false negatives here, maybe that first usage by-reference does not _actually/officially_ guarantee a stable memory position like that, but miris implementation works with that approach.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [December 26, 2023, 1:33am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/8 "2023-12-26T01:33:43Z")

</div>

> [@steffahn](#):
>
> Rust probably has a rule like “when `&mut …expr…` is evaluated, then `…expr…` must be valid for writes _at that point_”.

Stacked Borrows has this rule, creation of a mutable reference triggers a "phantom" write. However, Tree Borrows is more permissive, and only triggers a phantom read. (Which will still cause your final example to fail)

---

<div class="post-metadata">

**Author:** ![AngelicosPhosphoros](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/angelicosphosphoros/32/7346_2.png) [@AngelicosPhosphoros](https://internals.rust-lang.org/u/AngelicosPhosphoros)\
**Post date:** [December 26, 2023, 2:31am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/9 "2023-12-26T02:31:43Z")

</div>

> [@steffahn](#):
>
> No I don’t? I just read… `let _v = *ptr;` in my “minimized” (one might even say “altered”) example.

Oh, sorry, I missed reversing of assignment in second thread.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [December 26, 2023, 2:39am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/10 "2023-12-26T02:39:02Z")

</div>

> [@steffahn](#):
>
> Edit: Some minor testing suggests it might be less about “first usage” of a variable and more about “first usage that forces the variable to have a location in memory”.

The difference in built MIR between the two is tiny, and seems due just to the additional scope entered for the extra `&val`. The relevant bits:

```diff
-// fail
+// pass
 let _1: u8;
+let mut _3: &u8;
 // …
 let _11: &u8;
 // …
 scope 1 {
     debug val => const 0_u8;
- scope 2 {
         let _2: u8;
- scope 3 {
+ scope 2 {
             debug val2 => _2;
- scope 4 {
+ scope 3 {
                 // …
- scope 5 {
+ scope 4 {
                     let _13: &u8;
- scope 6 {
+ scope 5 {
                         debug r => _13;
                     }
                 }
             }
         }
- }
 }

 bb0: {
     // …
     _1 = const 0_u8; // val
+ _3 = &_1;
- _2 = _1; // val2
+ _2 = *_3; // val2
     // …
     _11 = &_2; // for println!
     // …
     _12 = spawn::<{closure}, ()>(const ZeroSized: {closure}) -> [return: bb4, unwind continue];
 }
 bb4: {
     _13 = &_1; // r
     // …
 }

```

(Where scopes actually cover isn't notated in the human consumption only MIR format.) This doesn't seem like it should have semantic impact; everything w.r.t. the place should be happening in the same causual order in both cases.

Without having dived into Miri, it likely is a false positive due to places not being allocated into memory until their reference is taken. It's an optimization for most cases (there's a lot of bookkeeping happening for allocations in Miri) but results in a false positive here. Assuming that's a correct assessment, the fix would be to assign a timestamp corresponding to the local place initialization when reifying into an allocation, instead of the timestamp of the reification itself.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [December 26, 2023, 4:06pm UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/11 "2023-12-26T16:06:09Z")

</div>

The original program here is _not_ a false positive, Miri is right here. `&mut val` is, conceptually, a write to `val`, and that write races with the write in the other thread.

However, [this version](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=681360513dd76413fa9cb00317c9f32e) with a raw pointer still shows the same error. So I think @CAD97 is on to something, the "delayed allocation" trick doesn't interact properly with the data race detection. This could be tricky to fix...

I opened a Miri issue to track this:

> <https://github.com/rust-lang/miri/issues/3242>
>
> The following code should be fine, but Miri raises an error:
> \`\`\`rust
> use std::…sync::atomic::Ordering::\*;
> use std::sync::atomic::\*;
> use std::sync::\*;
> 
> static P: AtomicPtr\<u8\> = AtomicPtr::new(core::ptr::null\_mut());
> static MUTEX: Mutex\<()\> = Mutex::new(());
> static CONDVAR: Condvar = Condvar::new();
> static IS\_FINISHED: AtomicBool = AtomicBool::new(false);
> 
> fn main() {
> for \_ in 0..1000 {
> P.store(core::ptr::null\_mut(), Relaxed);
> IS\_FINISHED.store(false, Relaxed);
> 
> let mut val: u8 = 0;
> 
> let \_t1 = std::thread::spawn(|| {
> while P.load(Relaxed).is\_null() {
> std::hint::spin\_loop();
> }
> unsafe {
> // Initialize \`\*P\`.
> let ptr = P.load(Relaxed);
> \*ptr = 127;
> }
> 
> IS\_FINISHED.store(true, Release);
> let \_g = MUTEX.lock().unwrap();
> CONDVAR.notify\_one();
> });
> 
> P.store(std::ptr::addr\_of\_mut!(val), Relaxed);
> let mut guard = MUTEX.lock().unwrap();
> while !IS\_FINISHED.load(Acquire) {
> guard = CONDVAR.wait(guard).unwrap();
> }
> 
> // Read initialized value.
> assert\_eq!(val, 127);
> }
> }
> \`\`\`
> The issue has been described well by CAD97 \[here\](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/10?u=ralfjung):
> 
> \> Without having dived into Miri, it likely is a false positive due to places not being allocated into memory until their reference is taken. It's an optimization for most cases (there's a lot of bookkeeping happening for allocations in Miri) but results in a false positive here. Assuming that's a correct assessment, the fix would be to assign a timestamp corresponding to the local place initialization when reifying into an allocation, instead of the timestamp of the reification itself.

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [December 27, 2023, 4:10am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/12 "2023-12-27T04:10:35Z")

</div>

What's the rationale for considering `&mut x` a write to the location of `x`? Isn't this just the possibility of a write?

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [December 27, 2023, 4:50am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/13 "2023-12-27T04:50:18Z")

</div>

I imagine it is something along the lines of my analysis in a previous RFC:

[https://rust-lang.github.io/rfcs/3323-restrictions.html#where-does-a-mutation-occur](https://rust-lang.github.io/rfcs/3323-restrictions.html#where-does-a-mutation-occur)

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [December 27, 2023, 7:27am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/14 "2023-12-27T07:27:45Z")

</div>

The idea is that the compiler should be allowed to insert spurious writes even if you didn't do any explicit write. Having `&mut` act as a write ensures that this is possible.

As was mentioned above, in Tree Borrows this got relaxed to having `&mut x` (and `&x`) just be a read, not a write. This avoids a bunch of UB [found in the wild](https://github.com/rust-lang/unsafe-code-guidelines/issues/133). That gives up on spurious writes but still allows the compiler to insert spurious reads (we set the `dereferenceable` attribute to inform LLVM about this). The "implicit write on `&mut`" is somewhat experimental, but the implicit read is almost certainly going to happen, and that is sufficient to make the original program UB.

(Tree Borrows is another experimental aliasing model, a successor of sorts to Stacked Borrows. Where Stacked Borrows probably rejects too many programs, Tree Borrows probably accepts too many.)

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [December 27, 2023, 7:55pm UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/15 "2023-12-27T19:55:23Z")

</div>

But why could allowing spurious writes be desirable? Would it help optimizations? Are those optimizations done in practice today, or are they just theoretical?

If it reduces the number of UB found in the wild, while not giving up some very important optimization, it seems that tree borrows is right on this one.

(Spurious reads on the other hand helps enabling speculative reads / reads that are done in advance whether we will actually need them or not; this seems much more useful)

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [December 27, 2023, 11:10pm UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/16 "2023-12-27T23:10:21Z")

</div>

> [@dlight](#):
>
> But why could allowing spurious writes be desirable?

Allowing turning

```rust
unknown();
*p = 5;

```

into

```rust
*p = 5;
unknown();

```

and other code motion along those lines. The most obvious way doing so is beneficial is when it reduces register pressure.

Doing the write earlier counts as spurious because `unknown()` could read the memory location and then diverge, at least per TB's rules. The write after would be UB in that case, but it never happens if `unknown()` doesn't return normally.

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [December 28, 2023, 7:58am UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/17 "2023-12-28T07:58:15Z")

</div>

```rust
fn foo(x: usize, y: &mut usize) {
    for _ in 0..x {
        *y += 1;
    }
}

```

Allowing spurious writes through `&mut` allows optimizing that to `*y += x`, otherwise it has to branch for the case without a write: `if x != 0 { *y += x; }`

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 28, 2023, 8:23pm UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/18 "2023-12-28T20:23:36Z")

</div>

> [@dlight](#):
>
> But why could allowing spurious writes be desirable? Would it help optimizations?

A classic example would be something like

```rust
for x in … {
    *p = 1;
    foo(x);
}
*p = 0;

```

where being able to LICM it to

```rust
*p = 1;
for x in … {
    foo(x);
}
*p = 0;

```

can save a bunch of work.

But if the `for` loop never actually ran the body, that `*p = 1` wouldn't have happened, so in order to be allowed to do that you need to be allowed to do spurious writes.

(The read version of that is more commonly applicable, but the write version exists too.)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [June 20, 2025, 8:24pm UTC](https://internals.rust-lang.org/t/possible-false-positive-in-miri-race-detector/20081/19 "2025-06-20T20:24:18Z")

</div>

Closed on June 20, 2025, 8:24pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
