pub const FREED_PATTERN: u8 = 0xCD;
pub const UNINIT_PATTERN: u8 = 0xAB;
pub const GUARD_PATTERN: u8 = 0xFD;
pub unsafe fn poison_freed(ptr: *mut u8, size: usize) {
std::ptr::write_bytes(ptr, FREED_PATTERN, size);
}
pub unsafe fn poison_uninit(ptr: *mut u8, size: usize) {
std::ptr::write_bytes(ptr, UNINIT_PATTERN, size);
}
pub fn is_freed_poison(ptr: *const u8, size: usize) -> bool {
for i in 0..size {
let byte = unsafe { *ptr.add(i) };
if byte != FREED_PATTERN {
return false;
}
}
true
}
pub fn check_use_after_free(ptr: *const u8, size: usize) -> bool {
let check_size = size.min(16);
is_freed_poison(ptr, check_size)
}