use core::ffi::{c_char, c_int, c_void};
use std::sync::atomic::{AtomicUsize, Ordering};
use libdd_profiling_heap_sampler::{
dd_alloc_req_t, dd_allocation_created, dd_allocation_freed, dd_allocation_realloc_commit,
dd_allocation_realloc_prepare, dd_allocation_requested, dd_tl_state_init,
};
pub(crate) static ORIG_MALLOC: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_FREE: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_CALLOC: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_REALLOC: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_POSIX_MEMALIGN: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_ALIGNED_ALLOC: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_DLOPEN: AtomicUsize = AtomicUsize::new(0);
pub(crate) static ORIG_PTHREAD_CREATE: AtomicUsize = AtomicUsize::new(0);
#[cfg(feature = "test-support")]
pub(crate) static HOOK_HITS: AtomicUsize = AtomicUsize::new(0);
#[inline]
unsafe fn load_fn<T>(slot: &AtomicUsize) -> Option<T> {
let v = slot.load(Ordering::Acquire);
if v == 0 {
None
} else {
Some(core::mem::transmute_copy::<usize, T>(&v))
}
}
type MallocFn = unsafe extern "C" fn(usize) -> *mut c_void;
type FreeFn = unsafe extern "C" fn(*mut c_void);
type CallocFn = unsafe extern "C" fn(usize, usize) -> *mut c_void;
type ReallocFn = unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void;
type PosixMemalignFn = unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int;
type AlignedAllocFn = unsafe extern "C" fn(usize, usize) -> *mut c_void;
type DlopenFn = unsafe extern "C" fn(*const c_char, c_int) -> *mut c_void;
const RTLD_DEEPBIND: c_int = 0x00008;
type StartRoutine = unsafe extern "C" fn(*mut c_void) -> *mut c_void;
type PthreadCreateFn = unsafe extern "C" fn(
*mut libc::pthread_t,
*const libc::pthread_attr_t,
StartRoutine,
*mut c_void,
) -> c_int;
#[no_mangle]
pub unsafe extern "C" fn gotter_malloc(size: usize) -> *mut c_void {
#[cfg(feature = "test-support")]
HOOK_HITS.fetch_add(1, Ordering::Relaxed);
let Some(real): Option<MallocFn> = load_fn(&ORIG_MALLOC) else {
return std::ptr::null_mut();
};
let req = dd_allocation_requested(size, core::mem::align_of::<*mut c_void>() * 2);
let raw = real(req.size);
dd_allocation_created(raw, req)
}
#[no_mangle]
pub unsafe extern "C" fn gotter_free(ptr: *mut c_void) {
#[cfg(feature = "test-support")]
HOOK_HITS.fetch_add(1, Ordering::Relaxed);
let Some(real): Option<FreeFn> = load_fn(&ORIG_FREE) else {
return;
};
let freed = dd_allocation_freed(ptr, 0, 0);
real(freed.ptr);
}
#[no_mangle]
pub unsafe extern "C" fn gotter_calloc(nmemb: usize, size: usize) -> *mut c_void {
let Some(real): Option<CallocFn> = load_fn(&ORIG_CALLOC) else {
return std::ptr::null_mut();
};
let Some(total) = nmemb.checked_mul(size) else {
return real(nmemb, size);
};
let req = dd_allocation_requested(total, core::mem::align_of::<*mut c_void>() * 2);
let raw = if req.weight == 0 {
real(nmemb, size)
} else {
real(1, req.size)
};
dd_allocation_created(raw, req)
}
#[no_mangle]
pub unsafe extern "C" fn gotter_realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
let Some(real): Option<ReallocFn> = load_fn(&ORIG_REALLOC) else {
return std::ptr::null_mut();
};
let prep = dd_allocation_realloc_prepare(ptr, size);
let new_raw = real(prep.raw_ptr, prep.raw_size);
dd_allocation_realloc_commit(ptr, new_raw, prep)
}
#[no_mangle]
pub unsafe extern "C" fn gotter_posix_memalign(
memptr: *mut *mut c_void,
alignment: usize,
size: usize,
) -> c_int {
let Some(real): Option<PosixMemalignFn> = load_fn(&ORIG_POSIX_MEMALIGN) else {
return libc::ENOMEM;
};
let req = dd_allocation_requested(size, alignment);
let ret = real(memptr, alignment, req.size);
if ret == 0 && !memptr.is_null() {
let raw = *memptr;
*memptr = dd_allocation_created(raw, req);
} else {
let _ = dd_allocation_created(std::ptr::null_mut(), req);
}
ret
}
#[no_mangle]
pub unsafe extern "C" fn gotter_aligned_alloc(alignment: usize, size: usize) -> *mut c_void {
let Some(real): Option<AlignedAllocFn> = load_fn(&ORIG_ALIGNED_ALLOC) else {
return std::ptr::null_mut();
};
let req = dd_allocation_requested(size, alignment);
let raw = real(alignment, req.size);
dd_allocation_created(raw, req)
}
#[no_mangle]
pub unsafe extern "C" fn gotter_dlopen(filename: *const c_char, flags: c_int) -> *mut c_void {
let Some(real): Option<DlopenFn> = load_fn(&ORIG_DLOPEN) else {
return libc::dlopen(filename, flags);
};
let handle = real(filename, flags);
if flags & RTLD_DEEPBIND != 0 {
return handle;
}
let _ = std::panic::catch_unwind(crate::update_heap_overrides);
handle
}
struct PthreadCreateArgs {
start: StartRoutine,
arg: *mut c_void,
}
unsafe extern "C" fn pthread_start_trampoline(arg: *mut c_void) -> *mut c_void {
let boxed: Box<PthreadCreateArgs> = Box::from_raw(arg as *mut PthreadCreateArgs);
dd_tl_state_init();
(boxed.start)(boxed.arg)
}
#[no_mangle]
pub unsafe extern "C" fn gotter_pthread_create(
thread: *mut libc::pthread_t,
attr: *const libc::pthread_attr_t,
start: StartRoutine,
arg: *mut c_void,
) -> c_int {
let Some(real): Option<PthreadCreateFn> = load_fn(&ORIG_PTHREAD_CREATE) else {
return libc::EAGAIN;
};
let boxed = Box::new(PthreadCreateArgs { start, arg });
let raw = Box::into_raw(boxed);
let rc = real(thread, attr, pthread_start_trampoline, raw as *mut c_void);
if rc != 0 {
drop(Box::from_raw(raw));
}
rc
}
#[allow(dead_code)]
fn _types_anchor() -> dd_alloc_req_t {
dd_alloc_req_t {
size: 0,
user_size: 0,
alignment: 0,
weight: 0,
}
}