Skip to content

Commit bb99a9a

Browse files
committed
Auto merge of rust-lang#77611 - oli-obk:atomic_miri_leakage, r=nagisa
Directly use raw pointers in `AtomicPtr` store/load I was unable to find any reason for this limitation in the latest source of LLVM or in the documentation [here](http://llvm.org/docs/Atomics.html#libcalls-atomic). fixes rust-lang/miri#1574
2 parents 7c90e11 + c663da8 commit bb99a9a

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

core/src/sync/atomic.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,16 @@ impl<T> AtomicPtr<T> {
966966
#[inline]
967967
#[stable(feature = "rust1", since = "1.0.0")]
968968
pub fn load(&self, order: Ordering) -> *mut T {
969+
#[cfg(not(bootstrap))]
969970
// SAFETY: data races are prevented by atomic intrinsics.
970-
unsafe { atomic_load(self.p.get() as *mut usize, order) as *mut T }
971+
unsafe {
972+
atomic_load(self.p.get(), order)
973+
}
974+
#[cfg(bootstrap)]
975+
// SAFETY: data races are prevented by atomic intrinsics.
976+
unsafe {
977+
atomic_load(self.p.get() as *mut usize, order) as *mut T
978+
}
971979
}
972980

973981
/// Stores a value into the pointer.
@@ -994,6 +1002,12 @@ impl<T> AtomicPtr<T> {
9941002
#[inline]
9951003
#[stable(feature = "rust1", since = "1.0.0")]
9961004
pub fn store(&self, ptr: *mut T, order: Ordering) {
1005+
#[cfg(not(bootstrap))]
1006+
// SAFETY: data races are prevented by atomic intrinsics.
1007+
unsafe {
1008+
atomic_store(self.p.get(), ptr, order);
1009+
}
1010+
#[cfg(bootstrap)]
9971011
// SAFETY: data races are prevented by atomic intrinsics.
9981012
unsafe {
9991013
atomic_store(self.p.get() as *mut usize, ptr as usize, order);
@@ -1105,6 +1119,7 @@ impl<T> AtomicPtr<T> {
11051119
success: Ordering,
11061120
failure: Ordering,
11071121
) -> Result<*mut T, *mut T> {
1122+
#[cfg(bootstrap)]
11081123
// SAFETY: data races are prevented by atomic intrinsics.
11091124
unsafe {
11101125
let res = atomic_compare_exchange(
@@ -1119,6 +1134,11 @@ impl<T> AtomicPtr<T> {
11191134
Err(x) => Err(x as *mut T),
11201135
}
11211136
}
1137+
#[cfg(not(bootstrap))]
1138+
// SAFETY: data races are prevented by atomic intrinsics.
1139+
unsafe {
1140+
atomic_compare_exchange(self.p.get(), current, new, success, failure)
1141+
}
11221142
}
11231143

11241144
/// Stores a value into the pointer if the current value is the same as the `current` value.
@@ -1165,6 +1185,7 @@ impl<T> AtomicPtr<T> {
11651185
success: Ordering,
11661186
failure: Ordering,
11671187
) -> Result<*mut T, *mut T> {
1188+
#[cfg(bootstrap)]
11681189
// SAFETY: data races are prevented by atomic intrinsics.
11691190
unsafe {
11701191
let res = atomic_compare_exchange_weak(
@@ -1179,6 +1200,14 @@ impl<T> AtomicPtr<T> {
11791200
Err(x) => Err(x as *mut T),
11801201
}
11811202
}
1203+
#[cfg(not(bootstrap))]
1204+
// SAFETY: This intrinsic is unsafe because it operates on a raw pointer
1205+
// but we know for sure that the pointer is valid (we just got it from
1206+
// an `UnsafeCell` that we have by reference) and the atomic operation
1207+
// itself allows us to safely mutate the `UnsafeCell` contents.
1208+
unsafe {
1209+
atomic_compare_exchange_weak(self.p.get(), current, new, success, failure)
1210+
}
11821211
}
11831212

11841213
/// Fetches the value, and applies a function to it that returns an optional

std/src/sys/windows/thread_local_key.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,6 @@ struct Node {
110110
next: *mut Node,
111111
}
112112

113-
#[cfg(miri)]
114-
extern "Rust" {
115-
/// Miri-provided extern function to mark the block `ptr` points to as a "root"
116-
/// for some static memory. This memory and everything reachable by it is not
117-
/// considered leaking even if it still exists when the program terminates.
118-
///
119-
/// `ptr` has to point to the beginning of an allocated block.
120-
fn miri_static_root(ptr: *const u8);
121-
}
122-
123113
unsafe fn register_dtor(key: Key, dtor: Dtor) {
124114
let mut node = Box::new(Node { key, dtor, next: ptr::null_mut() });
125115

@@ -128,9 +118,6 @@ unsafe fn register_dtor(key: Key, dtor: Dtor) {
128118
node.next = head;
129119
match DTORS.compare_exchange(head, &mut *node, SeqCst, SeqCst) {
130120
Ok(_) => {
131-
#[cfg(miri)]
132-
miri_static_root(&*node as *const _ as *const u8);
133-
134121
mem::forget(node);
135122
return;
136123
}

0 commit comments

Comments
 (0)