std/sys/pal/unix/
weak.rs

1//! Support for "weak linkage" to symbols on Unix
2//!
3//! Some I/O operations we do in std require newer versions of OSes but we need
4//! to maintain binary compatibility with older releases for now. In order to
5//! use the new functionality when available we use this module for detection.
6//!
7//! One option to use here is weak linkage, but that is unfortunately only
8//! really workable with ELF. Otherwise, use dlsym to get the symbol value at
9//! runtime. This is also done for compatibility with older versions of glibc,
10//! and to avoid creating dependencies on GLIBC_PRIVATE symbols. It assumes that
11//! we've been dynamically linked to the library the symbol comes from, but that
12//! is currently always the case for things like libpthread/libc.
13//!
14//! A long time ago this used weak linkage for the __pthread_get_minstack
15//! symbol, but that caused Debian to detect an unnecessarily strict versioned
16//! dependency on libc6 (#23628) because it is GLIBC_PRIVATE. We now use `dlsym`
17//! for a runtime lookup of that symbol to avoid the ELF versioned dependency.
18
19// There are a variety of `#[cfg]`s controlling which targets are involved in
20// each instance of `weak!` and `syscall!`. Rather than trying to unify all of
21// that, we'll just allow that some unix targets don't use this module at all.
22#![allow(dead_code, unused_macros)]
23#![forbid(unsafe_op_in_unsafe_fn)]
24
25use crate::ffi::{CStr, c_char, c_void};
26use crate::marker::{FnPtr, PhantomData};
27use crate::sync::atomic::{Atomic, AtomicPtr, Ordering};
28use crate::{mem, ptr};
29
30// We currently only test `dlsym!`, but that doesn't work on all platforms, so
31// we gate the tests to only the platforms where it is actually used.
32//
33// FIXME(joboet): add more tests, reorganise the whole module and get rid of
34//                `#[allow(dead_code, unused_macros)]`.
35#[cfg(any(
36    target_vendor = "apple",
37    all(target_os = "linux", target_env = "gnu"),
38    target_os = "freebsd",
39))]
40#[cfg(test)]
41mod tests;
42
43// We can use true weak linkage on ELF targets.
44#[cfg(all(unix, not(target_vendor = "apple")))]
45pub(crate) macro weak {
46    (fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
47        let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = {
48            unsafe extern "C" {
49                #[linkage = "extern_weak"]
50                static $name: Option<unsafe extern "C" fn($($t),*) -> $ret>;
51            }
52            #[allow(unused_unsafe)]
53            ExternWeak::new(unsafe { $name })
54        };
55    )
56}
57
58// On non-ELF targets, use the dlsym approximation of weak linkage.
59#[cfg(target_vendor = "apple")]
60pub(crate) use self::dlsym as weak;
61
62pub(crate) struct ExternWeak<F: Copy> {
63    weak_ptr: Option<F>,
64}
65
66impl<F: Copy> ExternWeak<F> {
67    #[inline]
68    pub(crate) fn new(weak_ptr: Option<F>) -> Self {
69        ExternWeak { weak_ptr }
70    }
71
72    #[inline]
73    pub(crate) fn get(&self) -> Option<F> {
74        self.weak_ptr
75    }
76}
77
78pub(crate) macro dlsym {
79    (fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
80        dlsym!(
81            #[link_name = stringify!($name)]
82            fn $name($($param : $t),*) -> $ret;
83        );
84    ),
85    (
86        #[link_name = $sym:expr]
87        fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;
88    ) => (
89        static DLSYM: DlsymWeak<unsafe extern "C" fn($($t),*) -> $ret> = {
90            let Ok(name) = CStr::from_bytes_with_nul(concat!($sym, '\0').as_bytes()) else {
91                panic!("symbol name may not contain NUL")
92            };
93
94            // SAFETY: Whoever calls the function pointer returned by `get()`
95            // is responsible for ensuring that the signature is correct. Just
96            // like with extern blocks, this is syntactically enforced by making
97            // the function pointer be unsafe.
98            unsafe { DlsymWeak::new(name) }
99        };
100
101        let $name = &DLSYM;
102    )
103}
104
105pub(crate) struct DlsymWeak<F> {
106    /// A pointer to the nul-terminated name of the symbol.
107    // Use a pointer instead of `&'static CStr` to save space.
108    name: *const c_char,
109    func: Atomic<*mut libc::c_void>,
110    _marker: PhantomData<F>,
111}
112
113impl<F: FnPtr> DlsymWeak<F> {
114    /// # Safety
115    ///
116    /// If the signature of `F` does not match the signature of the symbol (if
117    /// it exists), calling the function pointer returned by `get()` is
118    /// undefined behaviour.
119    pub(crate) const unsafe fn new(name: &'static CStr) -> Self {
120        DlsymWeak {
121            name: name.as_ptr(),
122            func: AtomicPtr::new(ptr::without_provenance_mut(1)),
123            _marker: PhantomData,
124        }
125    }
126
127    #[inline]
128    pub(crate) fn get(&self) -> Option<F> {
129        // The caller is presumably going to read through this value
130        // (by calling the function we've dlsymed). This means we'd
131        // need to have loaded it with at least C11's consume
132        // ordering in order to be guaranteed that the data we read
133        // from the pointer isn't from before the pointer was
134        // stored. Rust has no equivalent to memory_order_consume,
135        // so we use an acquire load (sorry, ARM).
136        //
137        // Now, in practice this likely isn't needed even on CPUs
138        // where relaxed and consume mean different things. The
139        // symbols we're loading are probably present (or not) at
140        // init, and even if they aren't the runtime dynamic loader
141        // is extremely likely have sufficient barriers internally
142        // (possibly implicitly, for example the ones provided by
143        // invoking `mprotect`).
144        //
145        // That said, none of that's *guaranteed*, so we use acquire.
146        match self.func.load(Ordering::Acquire) {
147            func if func.addr() == 1 => self.initialize(),
148            func if func.is_null() => None,
149            // SAFETY:
150            // `func` is not null and `F` implements `FnPtr`, thus this
151            // transmutation is well-defined. It is the responsibility of the
152            // creator of this `DlsymWeak` to ensure that calling the resulting
153            // function pointer does not result in undefined behaviour (though
154            // the `dlsym!` macro delegates this responsibility to the caller
155            // of the function by using `unsafe` function pointers).
156            // FIXME: use `transmute` once it stops complaining about generics.
157            func => Some(unsafe { mem::transmute_copy::<*mut c_void, F>(&func) }),
158        }
159    }
160
161    // Cold because it should only happen during first-time initialization.
162    #[cold]
163    fn initialize(&self) -> Option<F> {
164        // SAFETY: `self.name` was created from a `&'static CStr` and is
165        // therefore a valid C string pointer.
166        let val = unsafe { libc::dlsym(libc::RTLD_DEFAULT, self.name) };
167        // This synchronizes with the acquire load in `get`.
168        self.func.store(val, Ordering::Release);
169
170        if val.is_null() {
171            None
172        } else {
173            // SAFETY: see the comment in `get`.
174            // FIXME: use `transmute` once it stops complaining about generics.
175            Some(unsafe { mem::transmute_copy::<*mut libc::c_void, F>(&val) })
176        }
177    }
178}
179
180unsafe impl<F> Send for DlsymWeak<F> {}
181unsafe impl<F> Sync for DlsymWeak<F> {}
182
183#[cfg(not(any(target_os = "linux", target_os = "android")))]
184pub(crate) macro syscall {
185    (fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
186        unsafe fn $name($($param: $t),*) -> $ret {
187            weak!(fn $name($($param: $t),*) -> $ret;);
188
189            if let Some(fun) = $name.get() {
190                unsafe { fun($($param),*) }
191            } else {
192                super::os::set_errno(libc::ENOSYS);
193                -1
194            }
195        }
196    )
197}
198
199#[cfg(any(target_os = "linux", target_os = "android"))]
200pub(crate) macro syscall {
201    (
202        fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;
203    ) => (
204        unsafe fn $name($($param: $t),*) -> $ret {
205            weak!(fn $name($($param: $t),*) -> $ret;);
206
207            // Use a weak symbol from libc when possible, allowing `LD_PRELOAD`
208            // interposition, but if it's not found just use a raw syscall.
209            if let Some(fun) = $name.get() {
210                unsafe { fun($($param),*) }
211            } else {
212                unsafe { libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret }
213            }
214        }
215    )
216}
217
218#[cfg(any(target_os = "linux", target_os = "android"))]
219pub(crate) macro raw_syscall {
220    (fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
221        unsafe fn $name($($param: $t),*) -> $ret {
222            unsafe { libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret }
223        }
224    )
225}