#[cfg(feature = "diagnostics")]
use crate::core::diagnostics::*; use crate::sys::*;
use std::alloc::{Layout, alloc, dealloc, realloc};
use std::os::raw::c_void;
use std::ptr;
const HANDLE_ALIGNMENT: usize = 16;
const HANDLE_MAGIC: u64 = 0x4145584C4F484E44;
pub(crate) unsafe extern "C" fn host_new_handle_impl(size: A_HandleSize) -> PF_Handle {
log::trace!("host_new_handle: ENTRY with size={} (0x{:x})", size, size);
#[cfg(feature = "diagnostics")]
{
DiagnosticBuilder::new()
.set_name("PF_HandleSuite1/host_new_handle")
.add_arg("size", size)
.set_result(0)
.emit();
}
const MAX_REASONABLE_SIZE: u64 = 1_000_000_000; if size > MAX_REASONABLE_SIZE {
log::error!(
"host_new_handle: UNREASONABLE SIZE at entry! size={} (0x{:x})",
size,
size
);
return ptr::null_mut();
}
let requested_size = match usize::try_from(size) {
Ok(value) => value,
Err(_) => {
log::error!("host_new_handle: try_from failed for size={}", size);
return ptr::null_mut();
}
};
log::debug!("host_new_handle: converted to requested_size={}", requested_size);
let header_size = HANDLE_ALIGNMENT; let total_size = match header_size.checked_add(requested_size) {
Some(value) => value,
None => {
log::error!("host_new_handle: size overflow for requested_size={}", requested_size);
return ptr::null_mut();
}
};
log::debug!("host_new_handle: total_size={}", total_size);
if total_size > MAX_REASONABLE_SIZE as usize {
log::error!(
"host_new_handle: total_size too large! total_size={} (0x{:x})",
total_size,
total_size
);
return ptr::null_mut();
}
let layout = match Layout::from_size_align(total_size, HANDLE_ALIGNMENT) {
Ok(l) => l,
Err(_) => {
log::error!("host_new_handle: layout error for total_size={}", total_size);
return ptr::null_mut();
}
};
log::debug!("host_new_handle: layout created successfully");
let ptr = unsafe { alloc(layout) };
if ptr.is_null() {
log::error!("host_new_handle: allocation failed for size {}", size);
return ptr::null_mut();
}
unsafe { *(ptr as *mut u64) = HANDLE_MAGIC };
unsafe { *(ptr.add(8) as *mut usize) = requested_size };
let user_ptr = unsafe { ptr.add(header_size) };
let handle_layout = Layout::new::<*mut c_void>();
let handle_ptr = unsafe { alloc(handle_layout) } as *mut *mut c_void;
if handle_ptr.is_null() {
log::error!("host_new_handle: handle storage allocation failed");
unsafe { dealloc(ptr, layout) };
return ptr::null_mut();
}
unsafe { *handle_ptr = user_ptr as *mut c_void };
crate::strict::allocated(crate::AllocationKind::Handle, handle_ptr as usize, requested_size);
log::trace!(
"host_new_handle: SUCCESS handle={:p}, user_ptr={:p}, size={}",
handle_ptr,
user_ptr,
requested_size
);
handle_ptr as PF_Handle
}
pub(crate) unsafe extern "C" fn host_lock_handle_impl(pf_handle: PF_Handle) -> *mut c_void {
#[cfg(feature = "diagnostics")]
{
DiagnosticBuilder::new()
.set_name("PF_HandleSuite1/host_lock_handle")
.add_arg("pf_handle", format!("{:?}", pf_handle))
.set_result(0)
.emit();
}
if pf_handle.is_null() {
return ptr::null_mut();
}
(unsafe { *pf_handle }) as *mut c_void
}
pub(crate) unsafe extern "C" fn host_unlock_handle_impl(pf_handle: PF_Handle) {
#[cfg(feature = "diagnostics")]
log::trace!("host_unlock_handle called");
let _ = pf_handle;
}
pub(crate) unsafe extern "C" fn host_dispose_handle_impl(pf_handle: PF_Handle) {
#[cfg(feature = "diagnostics")]
log::trace!("host_dispose_handle called");
if pf_handle.is_null() {
return;
}
crate::strict::disposed(crate::AllocationKind::Handle, pf_handle as usize);
let user_ptr = unsafe { *(pf_handle as *mut *mut u8) };
if !user_ptr.is_null() {
let header_size = HANDLE_ALIGNMENT;
let base_ptr = unsafe { user_ptr.sub(header_size) };
let magic = unsafe { *(base_ptr as *mut u64) };
if magic != HANDLE_MAGIC {
log::error!(
"host_dispose_handle: INVALID HANDLE at dispose! magic=0x{:x} (expected 0x{:x}), pf_handle={:p}, user_ptr={:p}",
magic,
HANDLE_MAGIC,
pf_handle,
user_ptr
);
return;
}
let size = unsafe { *(base_ptr.add(8) as *mut usize) };
let total_size = match header_size.checked_add(size) {
Some(value) => value,
None => {
log::error!("host_dispose_handle: size overflow during free");
return;
}
};
if let Ok(layout) = Layout::from_size_align(total_size, HANDLE_ALIGNMENT) {
unsafe { dealloc(base_ptr, layout) };
} else {
log::error!("host_dispose_handle: failed to recreate layout during free");
}
}
let handle_layout = Layout::new::<*mut c_void>();
unsafe { dealloc(pf_handle as *mut u8, handle_layout) };
}
pub(crate) unsafe extern "C" fn host_get_handle_size_impl(pf_handle: PF_Handle) -> A_HandleSize {
#[cfg(feature = "diagnostics")]
log::trace!("host_get_handle_size called");
if pf_handle.is_null() {
log::warn!("host_get_handle_size: NULL handle passed");
return 0;
}
let user_ptr = unsafe { *(pf_handle as *mut *mut u8) };
log::debug!(
"host_get_handle_size: pf_handle={:p}, user_ptr={:p}",
pf_handle,
user_ptr
);
if user_ptr.is_null() {
log::warn!("host_get_handle_size: handle points to NULL user data");
return 0;
}
let header_size = HANDLE_ALIGNMENT;
let base_ptr = unsafe { user_ptr.sub(header_size) };
let magic = unsafe { *(base_ptr as *mut u64) };
if magic != HANDLE_MAGIC {
log::error!(
"host_get_handle_size: INVALID HANDLE! magic=0x{:x} (expected 0x{:x}), pf_handle={:p}, user_ptr={:p}, base_ptr={:p}",
magic,
HANDLE_MAGIC,
pf_handle,
user_ptr,
base_ptr
);
return 0;
}
let size = unsafe { *(base_ptr.add(8) as *mut usize) };
log::debug!("host_get_handle_size: base_ptr={:p}, raw_size={}", base_ptr, size);
const MAX_REASONABLE_SIZE: usize = 1_000_000_000; if size > MAX_REASONABLE_SIZE {
log::error!(
"host_get_handle_size: CORRUPTED SIZE DETECTED! handle={:p}, user_ptr={:p}, base_ptr={:p}, size={}",
pf_handle,
user_ptr,
base_ptr,
size
);
return 0;
}
match A_HandleSize::try_from(size) {
Ok(value) => {
log::debug!("host_get_handle_size: returning size={}", value);
value
}
Err(_) => {
log::error!("host_get_handle_size: size does not fit A_HandleSize");
0
}
}
}
pub(crate) unsafe extern "C" fn host_resize_handle_impl(new_sizeL: A_HandleSize, handlePH: *mut PF_Handle) -> PF_Err {
crate::core::ffi::guard("host_resize_handle_impl", || {
log::debug!("host_resize_handle: new_size={}, handlePH={:p}", new_sizeL, handlePH);
#[cfg(feature = "diagnostics")]
log::trace!("host_resize_handle called, new_size: {}", new_sizeL);
if handlePH.is_null() {
log::error!("host_resize_handle: handlePH is NULL");
return PF_Err_BAD_CALLBACK_PARAM;
}
let pf_handle = unsafe { *handlePH };
if pf_handle.is_null() {
log::error!("host_resize_handle: pf_handle is NULL");
return PF_Err_BAD_CALLBACK_PARAM;
}
let user_ptr = unsafe { *(pf_handle as *mut *mut u8) };
if user_ptr.is_null() {
log::error!("host_resize_handle: user_ptr is NULL");
return PF_Err_BAD_CALLBACK_PARAM;
}
let header_size = HANDLE_ALIGNMENT;
let base_ptr = unsafe { user_ptr.sub(header_size) };
let magic = unsafe { *(base_ptr as *mut u64) };
if magic != HANDLE_MAGIC {
log::error!(
"host_resize_handle: INVALID HANDLE! magic=0x{:x} (expected 0x{:x}), pf_handle={:p}, user_ptr={:p}",
magic,
HANDLE_MAGIC,
pf_handle,
user_ptr
);
return PF_Err_BAD_CALLBACK_PARAM;
}
let old_size = unsafe { *(base_ptr.add(8) as *mut usize) };
log::debug!(
"host_resize_handle: pf_handle={:p}, user_ptr={:p}, old_size={}",
pf_handle,
user_ptr,
old_size
);
let new_size = match usize::try_from(new_sizeL) {
Ok(value) => value,
Err(_) => {
log::error!("host_resize_handle: invalid negative new_size {}", new_sizeL);
return PF_Err_BAD_CALLBACK_PARAM;
}
};
const MAX_REASONABLE_SIZE: usize = 1_000_000_000; if new_size > MAX_REASONABLE_SIZE {
log::error!(
"host_resize_handle: UNREASONABLE SIZE REQUESTED! new_size={} (0x{:x}), old_size={}",
new_size,
new_size,
old_size
);
return PF_Err_BAD_CALLBACK_PARAM;
}
let old_total = match header_size.checked_add(old_size) {
Some(value) => value,
None => {
log::error!("host_resize_handle: old size overflow");
return PF_Err_INTERNAL_STRUCT_DAMAGED;
}
};
let new_total = match header_size.checked_add(new_size) {
Some(value) => value,
None => {
log::error!("host_resize_handle: new size overflow");
return PF_Err_OUT_OF_MEMORY;
}
};
if let Ok(old_layout) = Layout::from_size_align(old_total, HANDLE_ALIGNMENT) {
let new_ptr = unsafe { realloc(base_ptr, old_layout, new_total) };
if new_ptr.is_null() {
log::error!("host_resize_handle: realloc failed");
return PF_Err_OUT_OF_MEMORY;
}
unsafe { *(new_ptr as *mut u64) = HANDLE_MAGIC };
unsafe { *(new_ptr.add(8) as *mut usize) = new_size };
let new_user_ptr = unsafe { new_ptr.add(header_size) };
unsafe { *(pf_handle as *mut *mut u8) = new_user_ptr };
PF_Err_NONE
} else {
log::error!("host_resize_handle: invalid old layout logic");
PF_Err_INTERNAL_STRUCT_DAMAGED
}
})
}
pub const fn create_handle_suite() -> PF_HandleSuite1 {
PF_HandleSuite1 {
host_new_handle: Some(host_new_handle_impl),
host_lock_handle: Some(host_lock_handle_impl),
host_unlock_handle: Some(host_unlock_handle_impl),
host_dispose_handle: Some(host_dispose_handle_impl),
host_get_handle_size: Some(host_get_handle_size_impl),
host_resize_handle: Some(host_resize_handle_impl),
}
}