Arc/Rc is creating references to uninitialized values #119241
Labels
A-atomic
Area: Atomics, barriers, and sync primitives
C-cleanup
Category: PRs that clean code up or issues documenting cleanup.
C-discussion
Category: Discussion or questions that doesn't represent real issues.
T-opsem
Relevant to the opsem team
addr_of_mut doc says:
However, some code in Arc/Rc seems to create references to uninitialized values.
For example, in the following code,
inner
is a pointer to an uninitializedArcInner
, butLayout::for_value(&*inner)
creates a reference to it, and&mut (*inner).strong
and&mut (*inner).weak
creates references to (uninitialized) counters.rust/library/alloc/src/sync.rs
Lines 1825 to 1836 in 495203b
Such codes can also be found in some other places in Arc/Rc.
rust/library/alloc/src/sync.rs
Line 3386 in 495203b
rust/library/alloc/src/sync.rs
Line 1866 in 495203b
rust/library/alloc/src/sync.rs
Line 1901 in 495203b
rust/library/alloc/src/sync.rs
Line 1940 in 495203b
rust/library/alloc/src/rc.rs
Lines 1888 to 1890 in 495203b
rust/library/alloc/src/rc.rs
Line 1921 in 495203b
rust/library/alloc/src/rc.rs
Line 1955 in 495203b
rust/library/alloc/src/rc.rs
Line 1993 in 495203b
rust/library/alloc/src/rc.rs
Line 2527 in 495203b
At least the following code should run into this issue because it calls
Arc::initialize_arcinner
via<Arc<_> as From<Box<_>>::from
->Arc::from_box_in
->Arc::allocate_for_ptr_in
->Arc::allocate_for_layout
.Solution
This can be fixed in the following ways:
Layout::for_value_raw
(unstable) instead ofLayout::for_value
.addr_of_mut
for code creating reference to uninitialized conters (.strong
/.weak
).addr_of_mut
ornew_uninit
/new_unint_slice
for code creating reference to uninitialized data (.data
/.value
). (I think the latter would be clearer.)cc @RalfJung: Miri doesn't seem to report this as UB, but is my understanding that this is the kind of UB that Miri does not currently support detection correct? Or is it missed or allowed for some reason?
The text was updated successfully, but these errors were encountered: