|
| 1 | +// Implementation derived from `weak` in Rust's |
| 2 | +// library/std/src/sys/unix/weak.rs at revision |
| 3 | +// fd0cb0cdc21dd9c06025277d772108f8d42cb25f. |
| 4 | + |
| 5 | +//! Support for "weak linkage" to symbols on Unix |
| 6 | +//! |
| 7 | +//! Some I/O operations we do in libstd require newer versions of OSes but we |
| 8 | +//! need to maintain binary compatibility with older releases for now. In order |
| 9 | +//! to use the new functionality when available we use this module for |
| 10 | +//! detection. |
| 11 | +//! |
| 12 | +//! One option to use here is weak linkage, but that is unfortunately only |
| 13 | +//! really workable on Linux. Hence, use dlsym to get the symbol value at |
| 14 | +//! runtime. This is also done for compatibility with older versions of glibc, |
| 15 | +//! and to avoid creating dependencies on `GLIBC_PRIVATE` symbols. It assumes |
| 16 | +//! that we've been dynamically linked to the library the symbol comes from, |
| 17 | +//! but that is currently always the case for things like libpthread/libc. |
| 18 | +//! |
| 19 | +//! A long time ago this used weak linkage for the `__pthread_get_minstack` |
| 20 | +//! symbol, but that caused Debian to detect an unnecessarily strict versioned |
| 21 | +//! dependency on libc6 (#23628). |
| 22 | +
|
| 23 | +// There are a variety of `#[cfg]`s controlling which targets are involved in |
| 24 | +// each instance of `weak!` and `syscall!`. Rather than trying to unify all of |
| 25 | +// that, we'll just allow that some unix targets don't use this module at all. |
| 26 | +#![allow(dead_code, unused_macros)] |
| 27 | +#![allow(clippy::doc_markdown)] |
| 28 | + |
| 29 | +use core::sync::atomic::{self, AtomicUsize, Ordering}; |
| 30 | +use core::{marker, mem}; |
| 31 | +use rustix::ffi::ZStr; |
| 32 | + |
| 33 | +macro_rules! weak { |
| 34 | + (fn $name:ident($($t:ty),*) -> $ret:ty) => ( |
| 35 | + #[allow(non_upper_case_globals)] |
| 36 | + static $name: $crate::weak::Weak<unsafe extern fn($($t),*) -> $ret> = |
| 37 | + $crate::weak::Weak::new(concat!(stringify!($name), '\0')); |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +pub(crate) struct Weak<F> { |
| 42 | + name: &'static str, |
| 43 | + addr: AtomicUsize, |
| 44 | + _marker: marker::PhantomData<F>, |
| 45 | +} |
| 46 | + |
| 47 | +impl<F> Weak<F> { |
| 48 | + pub(crate) const fn new(name: &'static str) -> Self { |
| 49 | + Self { |
| 50 | + name, |
| 51 | + addr: AtomicUsize::new(1), |
| 52 | + _marker: marker::PhantomData, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub(crate) fn get(&self) -> Option<F> { |
| 57 | + assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>()); |
| 58 | + unsafe { |
| 59 | + // Relaxed is fine here because we fence before reading through the |
| 60 | + // pointer (see the comment below). |
| 61 | + match self.addr.load(Ordering::Relaxed) { |
| 62 | + 1 => self.initialize(), |
| 63 | + 0 => None, |
| 64 | + addr => { |
| 65 | + let func = mem::transmute_copy::<usize, F>(&addr); |
| 66 | + // The caller is presumably going to read through this value |
| 67 | + // (by calling the function we've dlsymed). This means we'd |
| 68 | + // need to have loaded it with at least C11's consume |
| 69 | + // ordering in order to be guaranteed that the data we read |
| 70 | + // from the pointer isn't from before the pointer was |
| 71 | + // stored. Rust has no equivalent to memory_order_consume, |
| 72 | + // so we use an acquire fence (sorry, ARM). |
| 73 | + // |
| 74 | + // Now, in practice this likely isn't needed even on CPUs |
| 75 | + // where relaxed and consume mean different things. The |
| 76 | + // symbols we're loading are probably present (or not) at |
| 77 | + // init, and even if they aren't the runtime dynamic loader |
| 78 | + // is extremely likely have sufficient barriers internally |
| 79 | + // (possibly implicitly, for example the ones provided by |
| 80 | + // invoking `mprotect`). |
| 81 | + // |
| 82 | + // That said, none of that's *guaranteed*, and so we fence. |
| 83 | + atomic::fence(Ordering::Acquire); |
| 84 | + Some(func) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Cold because it should only happen during first-time initialization. |
| 91 | + #[cold] |
| 92 | + unsafe fn initialize(&self) -> Option<F> { |
| 93 | + let val = fetch(self.name); |
| 94 | + // This synchronizes with the acquire fence in `get`. |
| 95 | + self.addr.store(val, Ordering::Release); |
| 96 | + |
| 97 | + match val { |
| 98 | + 0 => None, |
| 99 | + addr => Some(mem::transmute_copy::<usize, F>(&addr)), |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +unsafe fn fetch(name: &str) -> usize { |
| 105 | + let name = match ZStr::from_bytes_with_nul(name.as_bytes()) { |
| 106 | + Ok(c_str) => c_str, |
| 107 | + Err(..) => return 0, |
| 108 | + }; |
| 109 | + libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr().cast()) as usize |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(not(any(target_os = "android", target_os = "linux")))] |
| 113 | +macro_rules! syscall { |
| 114 | + (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( |
| 115 | + unsafe fn $name($($arg_name: $t),*) -> $ret { |
| 116 | + weak! { fn $name($($t),*) -> $ret } |
| 117 | + |
| 118 | + if let Some(fun) = $name.get() { |
| 119 | + fun($($arg_name),*) |
| 120 | + } else { |
| 121 | + errno::set_errno(errno::Errno(libc::ENOSYS)); |
| 122 | + -1 |
| 123 | + } |
| 124 | + } |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(any(target_os = "android", target_os = "linux"))] |
| 129 | +macro_rules! syscall { |
| 130 | + (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( |
| 131 | + unsafe fn $name($($arg_name:$t),*) -> $ret { |
| 132 | + // This looks like a hack, but concat_idents only accepts idents |
| 133 | + // (not paths). |
| 134 | + use libc::*; |
| 135 | + |
| 136 | + syscall( |
| 137 | + concat_idents!(SYS_, $name), |
| 138 | + $($arg_name as c_long),* |
| 139 | + ) as $ret |
| 140 | + } |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +macro_rules! weakcall { |
| 145 | + ($vis:vis fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( |
| 146 | + $vis unsafe fn $name($($arg_name: $t),*) -> $ret { |
| 147 | + weak! { fn $name($($t),*) -> $ret } |
| 148 | + |
| 149 | + // Use a weak symbol from libc when possible, allowing `LD_PRELOAD` |
| 150 | + // interposition, but if it's not found just fail. |
| 151 | + if let Some(fun) = $name.get() { |
| 152 | + fun($($arg_name),*) |
| 153 | + } else { |
| 154 | + errno::set_errno(errno::Errno(libc::ENOSYS)); |
| 155 | + -1 |
| 156 | + } |
| 157 | + } |
| 158 | + ) |
| 159 | +} |
0 commit comments