#![allow(dead_code, unused_macros)]
#![allow(clippy::doc_markdown)]
use crate::ffi::CStr;
use core::ffi::c_void;
use core::ptr::null_mut;
use core::sync::atomic::{self, AtomicPtr, Ordering};
use core::{marker, mem};
const NULL: *mut c_void = null_mut();
const INVALID: *mut c_void = 1 as *mut c_void;
macro_rules! weak {
($vis:vis fn $name:ident($($t:ty),*) -> $ret:ty) => (
#[allow(non_upper_case_globals)]
$vis static $name: $crate::backend::weak::Weak<unsafe extern fn($($t),*) -> $ret> =
$crate::backend::weak::Weak::new(concat!(stringify!($name), '\0'));
)
}
pub(crate) struct Weak<F> {
name: &'static str,
addr: AtomicPtr<c_void>,
_marker: marker::PhantomData<F>,
}
impl<F> Weak<F> {
pub(crate) const fn new(name: &'static str) -> Self {
Self {
name,
addr: AtomicPtr::new(INVALID),
_marker: marker::PhantomData,
}
}
pub(crate) fn get(&self) -> Option<F> {
assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
unsafe {
match self.addr.load(Ordering::Relaxed) {
INVALID => self.initialize(),
NULL => None,
addr => {
let func = mem::transmute_copy::<*mut c_void, F>(&addr);
atomic::fence(Ordering::Acquire);
Some(func)
}
}
}
}
#[cold]
unsafe fn initialize(&self) -> Option<F> {
let val = fetch(self.name);
self.addr.store(val, Ordering::Release);
match val {
NULL => None,
addr => Some(mem::transmute_copy::<*mut c_void, F>(&addr)),
}
}
}
unsafe fn fetch(name: &str) -> *mut c_void {
let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
Ok(c_str) => c_str,
Err(..) => return null_mut(),
};
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr().cast())
}
#[cfg(not(any(target_os = "android", target_os = "linux")))]
macro_rules! syscall {
(fn $name:ident($($arg_name:ident: $t:ty),*) via $_sys_name:ident -> $ret:ty) => (
unsafe fn $name($($arg_name: $t),*) -> $ret {
weak! { fn $name($($t),*) -> $ret }
if let Some(fun) = $name.get() {
fun($($arg_name),*)
} else {
libc_errno::set_errno(libc_errno::Errno(libc::ENOSYS));
-1
}
}
)
}
#[cfg(any(target_os = "android", target_os = "linux"))]
macro_rules! syscall {
(fn $name:ident($($arg_name:ident: $t:ty),*) via $sys_name:ident -> $ret:ty) => (
unsafe fn $name($($arg_name:$t),*) -> $ret {
use libc::*;
trait AsSyscallArg {
type SyscallArgType;
fn into_syscall_arg(self) -> Self::SyscallArgType;
}
impl<T> AsSyscallArg for *mut T {
type SyscallArgType = *mut T;
fn into_syscall_arg(self) -> Self::SyscallArgType { self }
}
impl<T> AsSyscallArg for *const T {
type SyscallArgType = *const T;
fn into_syscall_arg(self) -> Self::SyscallArgType { self }
}
impl AsSyscallArg for $crate::fd::BorrowedFd<'_> {
type SyscallArgType = c::c_long;
fn into_syscall_arg(self) -> Self::SyscallArgType {
$crate::fd::AsRawFd::as_raw_fd(&self) as _
}
}
impl AsSyscallArg for i32 {
type SyscallArgType = c::c_long;
fn into_syscall_arg(self) -> Self::SyscallArgType { self as _ }
}
impl AsSyscallArg for u32 {
type SyscallArgType = c::c_long;
fn into_syscall_arg(self) -> Self::SyscallArgType { self as _ }
}
impl AsSyscallArg for usize {
type SyscallArgType = c::c_long;
fn into_syscall_arg(self) -> Self::SyscallArgType { self as _ }
}
syscall($sys_name, $($arg_name.into_syscall_arg()),*) as $ret
}
)
}
macro_rules! weakcall {
($vis:vis fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
$vis unsafe fn $name($($arg_name: $t),*) -> $ret {
weak! { fn $name($($t),*) -> $ret }
if let Some(fun) = $name.get() {
fun($($arg_name),*)
} else {
libc_errno::set_errno(libc_errno::Errno(libc::ENOSYS));
-1
}
}
)
}
macro_rules! weak_or_syscall {
($vis:vis fn $name:ident($($arg_name:ident: $t:ty),*) via $sys_name:ident -> $ret:ty) => (
$vis unsafe fn $name($($arg_name: $t),*) -> $ret {
weak! { fn $name($($t),*) -> $ret }
if let Some(fun) = $name.get() {
fun($($arg_name),*)
} else {
syscall! { fn $name($($arg_name: $t),*) via $sys_name -> $ret }
$name($($arg_name),*)
}
}
)
}