#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
mod elf;
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
mod hooks;
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
use std::sync::{Mutex, MutexGuard};
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
use elf::SymbolOverrides;
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
static GLOBAL_OVERRIDES: Mutex<Option<SymbolOverrides>> = Mutex::new(None);
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
fn lock_global_overrides() -> MutexGuard<'static, Option<SymbolOverrides>> {
GLOBAL_OVERRIDES
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub fn install_heap_overrides() -> bool {
if !libdd_profiling_heap_sampler::heap_sampling_enabled() {
return false;
}
let mut guard = lock_global_overrides();
if guard.is_none() {
let mut so = SymbolOverrides::new();
register_all(&mut so);
*guard = Some(so);
}
guard.as_mut().unwrap().apply_overrides();
any_orig_resolved()
}
#[cfg(not(all(target_os = "linux", target_pointer_width = "64")))]
pub fn install_heap_overrides() -> bool {
false
}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub fn update_heap_overrides() {
let mut guard = lock_global_overrides();
if let Some(so) = guard.as_mut() {
so.update_overrides();
}
}
#[cfg(not(all(target_os = "linux", target_pointer_width = "64")))]
pub fn update_heap_overrides() {}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub fn set_default_sampling_distance(distance_bytes: u64) {
libdd_profiling_heap_sampler::set_default_sampling_distance(distance_bytes);
}
#[cfg(not(all(target_os = "linux", target_pointer_width = "64")))]
pub fn set_default_sampling_distance(_distance_bytes: u64) {}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub fn heap_overrides_are_installed() -> bool {
lock_global_overrides().is_some()
}
#[cfg(not(all(target_os = "linux", target_pointer_width = "64")))]
pub fn heap_overrides_are_installed() -> bool {
false
}
#[cfg(feature = "test-support")]
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
pub fn test_hook_hits() -> u64 {
hooks::HOOK_HITS.load(std::sync::atomic::Ordering::Relaxed) as u64
}
#[cfg(feature = "test-support")]
#[cfg(not(all(target_os = "linux", target_pointer_width = "64")))]
pub fn test_hook_hits() -> u64 {
0
}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
fn register_all(so: &mut SymbolOverrides) {
use hooks::*;
so.register("malloc", gotter_malloc as *const () as usize, &ORIG_MALLOC);
so.register("free", gotter_free as *const () as usize, &ORIG_FREE);
so.register("calloc", gotter_calloc as *const () as usize, &ORIG_CALLOC);
so.register(
"realloc",
gotter_realloc as *const () as usize,
&ORIG_REALLOC,
);
so.register(
"posix_memalign",
gotter_posix_memalign as *const () as usize,
&ORIG_POSIX_MEMALIGN,
);
so.register(
"aligned_alloc",
gotter_aligned_alloc as *const () as usize,
&ORIG_ALIGNED_ALLOC,
);
so.register("dlopen", gotter_dlopen as *const () as usize, &ORIG_DLOPEN);
so.register(
"pthread_create",
gotter_pthread_create as *const () as usize,
&ORIG_PTHREAD_CREATE,
);
}
#[cfg(all(target_os = "linux", target_pointer_width = "64"))]
fn any_orig_resolved() -> bool {
use hooks::*;
use std::sync::atomic::Ordering;
[
&ORIG_MALLOC,
&ORIG_FREE,
&ORIG_CALLOC,
&ORIG_REALLOC,
&ORIG_POSIX_MEMALIGN,
&ORIG_ALIGNED_ALLOC,
&ORIG_DLOPEN,
&ORIG_PTHREAD_CREATE,
]
.iter()
.any(|s| s.load(Ordering::Relaxed) != 0)
}
#[cfg(all(test, target_os = "linux", target_pointer_width = "64", not(miri)))]
mod tests {
use super::*;
#[test]
fn can_lookup_malloc() {
let r = elf::lookup_symbol("malloc", 0);
assert!(r.is_some(), "expected to find malloc in loaded libraries");
let r = r.unwrap();
assert!(r.address != 0);
}
#[test]
fn unknown_symbol_lookup_returns_none() {
let r = elf::lookup_symbol("definitely_not_a_real_libc_symbol_xyzzy", 0);
assert!(r.is_none());
}
}