rust-lang / rust-lang/rust

Using `Release` in the `store` operation for `make_mut` just prevent out-of-thin-air value?

Open
#133,284 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-atomic C-discussion T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In https://doc.rust-lang.org/src/alloc/sync.rs.html#2267, the make_mut is implemented as

if this.inner().strong.compare_exchange(1, 0, Acquire, Relaxed).is_err(){
  // ...
}else if this.inner().weak.load(Relaxed) != 1 {  // #0
 //...
}else{
  this.inner().strong.store(1, Release);  // #1
}

while upgrade is

if self.inner()?.strong.fetch_update(Acquire, Relaxed, checked_increment).is_ok(){
   Some(...)
}else{
 None
}

The Release at #1 concerns this case

// thread 1:
let mut_ref = Arc::make_mut(&mut my_arc);
*mut_ref = 10;

//thread 2: 
let arc = weak.upgrade().unwrap();
drop(weak);
println!("{}", *arc);

In this case, upgrade will return Some if it reads #1 and #1 is executed only #0 reads the value written by weak.fetch_sub(1, Release) in drop(weak);, so the model can be simplified as

weak = 2;
strong = 0;
// thread 1:
if weak.load(Relaxed) == 1{  // #1
   strong.store(1, Relaxed) // if the Release is changed to Relaxed  // #2
}
// thread 2:
while strong.load(Relaxed) !=0{ // #3
}
weak.store(1, Relaxed); // #4

#1 read #4, #4 is executed only if #3 read #2, #2 is executed only if #1 read #4. It depends on the out-of-thin-air value. Do we need a release memory order here to prevent OOTD?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with alloc/sync.rs at the linked make_mut implementation and compare its atomic orderings with upgrade. Analyze the proposed simplified execution and determine whether changing the Release store to Relaxed permits an out-of-thin-air value; done means providing a justified conclusion about whether Release is required.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.