use std::sync::OnceLock;
pub(crate) fn pin_current_module() -> Result<(), String> {
static PIN: OnceLock<Result<(), String>> = OnceLock::new();
PIN.get_or_init(pin_platform_module).clone()
}
#[cfg(windows)]
fn pin_platform_module() -> Result<(), String> {
use windows::core::PCWSTR;
use windows::Win32::Foundation::HMODULE;
use windows::Win32::System::LibraryLoader::{
GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GET_MODULE_HANDLE_EX_FLAG_PIN,
};
let mut module = HMODULE::default();
unsafe {
GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
PCWSTR(pin_platform_module as *const () as *const u16),
&mut module,
)
}
.map_err(|error| format!("could not pin the native module for detached workers: {error}"))
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn pin_platform_module() -> Result<(), String> {
use std::ffi::CStr;
use std::ptr;
let address = pin_platform_module as *const () as *const libc::c_void;
#[cfg(target_os = "linux")]
if address_in_main_executable(address) {
return Ok(());
}
let mut image = libc::Dl_info {
dli_fname: ptr::null(),
dli_fbase: ptr::null_mut(),
dli_sname: ptr::null(),
dli_saddr: ptr::null_mut(),
};
if unsafe { libc::dladdr(address, &mut image) } == 0 || image.dli_fname.is_null() {
return Err("could not locate the native module for detached workers".to_string());
}
#[cfg(target_os = "macos")]
{
extern "C" {
fn _dyld_get_image_header(index: u32) -> *const libc::c_void;
}
if image.dli_fbase.cast_const() == unsafe { _dyld_get_image_header(0) } {
return Ok(());
}
}
let handle = unsafe {
libc::dlopen(
image.dli_fname,
libc::RTLD_NOW | libc::RTLD_LOCAL | libc::RTLD_NOLOAD | libc::RTLD_NODELETE,
)
};
if !handle.is_null() {
return Ok(());
}
let detail = unsafe {
let error = libc::dlerror();
if error.is_null() {
"dynamic loader returned no error detail".to_string()
} else {
CStr::from_ptr(error).to_string_lossy().into_owned()
}
};
Err(format!(
"could not pin the native module for detached workers: {detail}"
))
}
#[cfg(target_os = "linux")]
fn address_in_main_executable(address: *const libc::c_void) -> bool {
struct Search {
address: usize,
program_headers: usize,
found: bool,
}
unsafe extern "C" fn inspect(
info: *mut libc::dl_phdr_info,
_size: usize,
data: *mut libc::c_void,
) -> libc::c_int {
let search = data.cast::<Search>();
let (info, search) = unsafe { (&*info, &mut *search) };
if info.dlpi_phdr as usize != search.program_headers {
return 0;
}
let headers =
unsafe { std::slice::from_raw_parts(info.dlpi_phdr, usize::from(info.dlpi_phnum)) };
search.found = headers.iter().any(|header| {
let start = (info.dlpi_addr as usize).wrapping_add(header.p_vaddr as usize);
header.p_type == libc::PT_LOAD
&& search.address.wrapping_sub(start) < header.p_memsz as usize
});
1
}
let program_headers = unsafe { libc::getauxval(libc::AT_PHDR) } as usize;
if program_headers == 0 {
return false;
}
let mut search = Search {
address: address as usize,
program_headers,
found: false,
};
unsafe { libc::dl_iterate_phdr(Some(inspect), std::ptr::from_mut(&mut search).cast()) };
search.found
}
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
fn pin_platform_module() -> Result<(), String> {
Err("native module lifetime protection is unavailable on this platform".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn main_executable_accepts_repeated_pinning() {
pin_current_module().unwrap();
pin_current_module().unwrap();
}
}