use std::ffi::c_void;
use std::panic::catch_unwind;
use std::sync::{Once, OnceLock};
use crate::{Any, Error, Function, INTERNAL_ERROR, Result};
#[doc(hidden)]
pub use linkme;
#[macro_export]
macro_rules! static_init_block {
($($body:tt)*) => {
const _: () = {
#[$crate::static_init::linkme::distributed_slice($crate::static_init::STATIC_INIT_BLOCKS)]
#[linkme(crate = $crate::static_init::linkme)]
static BLOCK: $crate::static_init::StaticInitBlock =
$crate::static_init::StaticInitBlock::__new(
::core::module_path!(),
::core::line!(),
match ::core::option_env!("CARGO_PKG_VERSION") {
::core::option::Option::Some(version) => version,
::core::option::Option::None => "",
},
::core::stringify!($($body)*),
__tvm_ffi_static_init_block_body,
);
#[allow(unreachable_code)]
fn __tvm_ffi_static_init_block_body() -> $crate::Result<()> {
$crate::static_init::IntoStaticInitResult::into_static_init_result({ $($body)* })
}
#[used]
#[cfg_attr(target_os = "linux", unsafe(link_section = ".init_array"))]
#[cfg_attr(target_os = "macos", unsafe(link_section = "__DATA,__mod_init_func"))]
#[cfg_attr(windows, unsafe(link_section = ".CRT$XCU"))]
static RUN_AT_LOAD: extern "C" fn() = $crate::static_init::__run_at_load;
};
};
}
#[doc(hidden)]
#[diagnostic::on_unimplemented(
message = "a static init block returns `Result<()>`, not `{Self}`",
label = "the value of the block",
note = "end the last statement of the block with `;`, or return a `Result<()>`"
)]
pub trait IntoStaticInitResult {
fn into_static_init_result(self) -> Result<()>;
}
impl IntoStaticInitResult for () {
fn into_static_init_result(self) -> Result<()> {
Ok(())
}
}
impl IntoStaticInitResult for Result<()> {
fn into_static_init_result(self) -> Result<()> {
self
}
}
#[doc(hidden)]
pub trait FnOutput {
type Output;
}
impl<T> FnOutput for fn() -> T {
type Output = T;
}
#[doc(hidden)]
pub type Never = <fn() -> ! as FnOutput>::Output;
impl IntoStaticInitResult for Never {
fn into_static_init_result(self) -> Result<()> {
self
}
}
#[doc(hidden)]
pub struct StaticInitBlock {
module: &'static str,
line: u32,
version: &'static str,
text_hash: u64,
body: fn() -> Result<()>,
outcome: OnceLock<std::result::Result<(), std::string::String>>,
}
#[doc(hidden)]
#[linkme::distributed_slice]
pub static STATIC_INIT_BLOCKS: [StaticInitBlock];
impl StaticInitBlock {
#[doc(hidden)]
pub const fn __new(
module: &'static str,
line: u32,
version: &'static str,
text: &str,
body: fn() -> Result<()>,
) -> Self {
let text_hash = text_hash(text);
Self {
module,
line,
version,
text_hash,
body,
outcome: OnceLock::new(),
}
}
#[doc(hidden)]
pub fn identity(&self) -> std::string::String {
format!(
"{}:{}@{}#{:016x}",
self.module, self.line, self.version, self.text_hash
)
}
#[doc(hidden)]
pub fn marker(&self) -> std::string::String {
format!("ffi.static_init_block.{}", self.identity())
}
#[doc(hidden)]
pub fn run(&self) -> Result<()> {
let mut failure = None;
let outcome = self.outcome.get_or_init(|| {
self.run_once().map_err(|error| {
let text = format!("{}: {}", error.kind(), error.message());
failure = Some(error);
text
})
});
match (failure, outcome) {
(Some(error), _) => Err(error),
(None, Ok(())) => Ok(()),
(None, Err(text)) => {
let message = format!("The static init block at {} failed: {text}", self.place());
Err(Error::new(INTERNAL_ERROR, &message))
}
}
}
fn run_once(&self) -> Result<()> {
let marker = self.marker();
if Function::get_global(&marker).is_err() {
catch_unwind(self.body).unwrap_or_else(|payload| Err(Error::from_panic(payload)))?;
Function::register_global(&marker, Function::from_packed(|_| Ok(Any::default())))?;
}
Ok(())
}
fn place(&self) -> std::string::String {
format!("{}:{}", self.module, self.line)
}
}
const fn text_hash(text: &str) -> u64 {
let bytes = text.as_bytes();
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
let mut i = 0;
while i < bytes.len() {
if !bytes[i].is_ascii_whitespace() {
hash = (hash ^ bytes[i] as u64).wrapping_mul(0x0100_0000_01b3);
}
i += 1;
}
hash
}
fn run_blocks() -> Vec<(&'static StaticInitBlock, Error)> {
let mut blocks: Vec<&StaticInitBlock> = STATIC_INIT_BLOCKS.iter().collect();
blocks.sort_by_key(|block| (block.module, block.line));
blocks
.into_iter()
.filter_map(|block| block.run().err().map(|e| (block, e)))
.collect()
}
pub fn run_static_init_blocks() -> Result<()> {
keep_loaded();
let mut failures = run_blocks();
if failures.len() <= 1 {
return failures.pop().map_or(Ok(()), |(_, error)| Err(error));
}
let mut message = format!("{} static init blocks failed:", failures.len());
for (block, error) in &failures {
message.push_str(&format!(
"\n {}: {}: {}",
block.place(),
error.kind(),
error.message()
));
}
Err(Error::new(failures[0].1.kind(), &message))
}
#[doc(hidden)]
pub extern "C" fn __run_at_load() {
static LOADED: Once = Once::new();
LOADED.call_once(|| {
let library = keep_loaded().unwrap_or_else(|| "static_init_block!".to_string());
match catch_unwind(run_blocks) {
Ok(failures) => {
for (block, error) in failures {
eprintln!(
"{library}: the static init block at {} failed: {}: {}",
block.place(),
error.kind(),
error.message()
);
}
}
Err(_) => eprintln!("{library}: running the static init blocks panicked"),
}
});
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn keep_loaded() -> Option<std::string::String> {
use std::ffi::{CStr, c_char, c_int};
#[repr(C)]
struct DlInfo {
fname: *const c_char,
fbase: *mut c_void,
sname: *const c_char,
saddr: *mut c_void,
}
unsafe extern "C" {
fn dladdr(addr: *const c_void, info: *mut DlInfo) -> c_int;
fn dlopen(filename: *const c_char, flags: c_int) -> *mut c_void;
}
#[cfg(target_os = "linux")]
const FLAGS: c_int = 0x1 | 0x4 | 0x1000;
#[cfg(target_os = "macos")]
const FLAGS: c_int = 0x1 | 0x10 | 0x80;
let null = std::ptr::null_mut();
let mut info = DlInfo {
fname: null as _,
fbase: null,
sname: null as _,
saddr: null,
};
unsafe {
if dladdr(keep_loaded as *const c_void, &mut info) == 0 || info.fname.is_null() {
return None;
}
dlopen(info.fname, FLAGS);
Some(CStr::from_ptr(info.fname).to_string_lossy().into_owned())
}
}
#[cfg(windows)]
fn keep_loaded() -> Option<std::string::String> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
type Module = *mut c_void;
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetModuleHandleExW(flags: u32, module_name: *const u16, module: *mut Module) -> i32;
fn GetModuleFileNameW(module: Module, filename: *mut u16, size: u32) -> u32;
}
const FLAGS: u32 = 0x1 | 0x4;
let mut module: Module = std::ptr::null_mut();
let mut path = vec![0u16; 32768];
unsafe {
let address = keep_loaded as *const c_void as *const u16;
if GetModuleHandleExW(FLAGS, address, &mut module) == 0 || module.is_null() {
return None;
}
let len = GetModuleFileNameW(module, path.as_mut_ptr(), path.len() as u32) as usize;
if len == 0 {
return None;
}
Some(
OsString::from_wide(&path[..len.min(path.len())])
.to_string_lossy()
.into_owned(),
)
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
fn keep_loaded() -> Option<std::string::String> {
None
}