Arc::get_mut_unchecked does not mention restrictions on drop
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Location
Summary
The safety comment on Arc::get_mut_unchecked states:
If any other Arc or Weak pointers to the same allocation exist, then they must not be dereferenced or have active borrows for the duration of the returned borrow, and their inner type must be exactly the same as the inner type of this Rc (including lifetimes).
But it mentions nothing about dropping Arcs, which triggers unsoundness in MIRI:
#![feature(get_mut_unchecked)]
use std::sync::Arc;
fn main() {
let mut x = Arc::new(0);
let y = x.clone();
let x_ref = unsafe { Arc::get_mut_unchecked(&mut x) }; // Create unchecked reference.
drop(y); // Drop a different Arc pointing to the same value while reference exists.
*x_ref = 42; // Use unchecked reference.
}
error: Undefined Behavior: attempting a write access using <1715> at alloc838[0x10], but that tag does not exist in the borrow stack for this location
--> src/main.rs:10:5
|
10 | *x_ref = 42;
| ^^^^^^^^^^^
| |
| attempting a write access using <1715> at alloc838[0x10], but that tag does not exist in the borrow stack for this location
| this error occurs as part of an access at alloc838[0x10..0x14]
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <1715> was created by a Unique retag at offsets [0x10..0x14]
--> src/main.rs:8:26
|
8 | let x_ref = unsafe { Arc::get_mut_unchecked(&mut x) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: <1715> was later invalidated at offsets [0x0..0x18] by a SharedReadOnly retag
--> src/main.rs:9:5
|
9 | drop(y);
| ^^^^^^^
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the linked Arc::get_mut_unchecked documentation and read its safety comment alongside the provided Miri reproducer. Verify how dropping another Arc or Weak affects the returned borrow, then update the safety wording to cover that restriction and check the example under Miri.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 65/100