Skip to content

Commit 53f028d

Browse files
authored
Rollup merge of #96167 - CAD97:weak-dlsym-less-ptr-crime, r=thomcc
Replace sys/unix/weak AtomicUsize with AtomicPtr Should fix #96163. Can't easily test on Windows though...
2 parents 72cb094 + 620c0a4 commit 53f028d

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

library/std/src/sys/unix/weak.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
use crate::ffi::CStr;
2626
use crate::marker::PhantomData;
2727
use crate::mem;
28-
use crate::sync::atomic::{self, AtomicUsize, Ordering};
28+
use crate::ptr;
29+
use crate::sync::atomic::{self, AtomicPtr, Ordering};
2930

3031
// We can use true weak linkage on ELF targets.
3132
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
@@ -83,25 +84,25 @@ pub(crate) macro dlsym {
8384
}
8485
pub(crate) struct DlsymWeak<F> {
8586
name: &'static str,
86-
addr: AtomicUsize,
87+
func: AtomicPtr<libc::c_void>,
8788
_marker: PhantomData<F>,
8889
}
8990

9091
impl<F> DlsymWeak<F> {
9192
pub(crate) const fn new(name: &'static str) -> Self {
92-
DlsymWeak { name, addr: AtomicUsize::new(1), _marker: PhantomData }
93+
DlsymWeak { name, func: AtomicPtr::new(ptr::invalid_mut(1)), _marker: PhantomData }
9394
}
9495

9596
#[inline]
9697
pub(crate) fn get(&self) -> Option<F> {
9798
unsafe {
9899
// Relaxed is fine here because we fence before reading through the
99100
// pointer (see the comment below).
100-
match self.addr.load(Ordering::Relaxed) {
101-
1 => self.initialize(),
102-
0 => None,
103-
addr => {
104-
let func = mem::transmute_copy::<usize, F>(&addr);
101+
match self.func.load(Ordering::Relaxed) {
102+
func if func.addr() == 1 => self.initialize(),
103+
func if func.is_null() => None,
104+
func => {
105+
let func = mem::transmute_copy::<*mut libc::c_void, F>(&func);
105106
// The caller is presumably going to read through this value
106107
// (by calling the function we've dlsymed). This means we'd
107108
// need to have loaded it with at least C11's consume
@@ -129,25 +130,22 @@ impl<F> DlsymWeak<F> {
129130
// Cold because it should only happen during first-time initialization.
130131
#[cold]
131132
unsafe fn initialize(&self) -> Option<F> {
132-
assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
133+
assert_eq!(mem::size_of::<F>(), mem::size_of::<*mut libc::c_void>());
133134

134135
let val = fetch(self.name);
135136
// This synchronizes with the acquire fence in `get`.
136-
self.addr.store(val, Ordering::Release);
137+
self.func.store(val, Ordering::Release);
137138

138-
match val {
139-
0 => None,
140-
addr => Some(mem::transmute_copy::<usize, F>(&addr)),
141-
}
139+
if val.is_null() { None } else { Some(mem::transmute_copy::<*mut libc::c_void, F>(&val)) }
142140
}
143141
}
144142

145-
unsafe fn fetch(name: &str) -> usize {
143+
unsafe fn fetch(name: &str) -> *mut libc::c_void {
146144
let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
147145
Ok(cstr) => cstr,
148-
Err(..) => return 0,
146+
Err(..) => return ptr::null_mut(),
149147
};
150-
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize
148+
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr())
151149
}
152150

153151
#[cfg(not(any(target_os = "linux", target_os = "android")))]

0 commit comments

Comments
 (0)