#![deny(unsafe_code)]
pub mod lifetime_expansion;
pub mod buffer_overflow;
pub mod segfault;
pub mod transmute;
pub mod use_after_free;
pub use lifetime_expansion::*;
pub use buffer_overflow::buffer_overflow;
pub use segfault::segfault;
pub use transmute::transmute;
pub use use_after_free::use_after_free;
#[inline(always)]
pub fn construct_fake_string(ptr: *mut u8, cap: usize, len: usize) -> String {
let sentinel_string = crate::transmute::<_, String>([0usize, 1usize, 2usize]);
let fields = [ptr as usize, cap, len];
let mut actual_buf = [0usize; 3];
actual_buf[0] = fields[sentinel_string.as_ptr() as usize];
actual_buf[1] = fields[sentinel_string.capacity()];
actual_buf[2] = fields[sentinel_string.len()];
std::mem::forget(sentinel_string);
crate::transmute::<_, String>(actual_buf)
}
use std::hint::black_box;
#[inline(never)]
#[allow(clippy::borrowed_box, clippy::redundant_allocation)]
fn get_dropped_box<T: Default>() -> &'static mut Box<Box<T>> {
let mut boxx = black_box(Box::<Box<T>>::default());
lifetime_expansion::expand_mut(&mut boxx)
}
pub fn not_alloc<'a, T: Default + 'static>() -> &'a mut T {
get_dropped_box::<T>().as_mut().as_mut()
}
#[cfg(feature = "give-up")]
pub fn give_up<T: 'static>() -> Box<T> {
use std::time::SystemTime;
let size = std::mem::size_of::<T>();
let mut v = Vec::with_capacity(size);
let mut rng = {
let seed = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
oorandom::Rand32::new(seed.as_secs())
};
for _ in 0..size {
v.push((rng.rand_u32() % 256) as u8);
}
crate::transmute(v.into_boxed_slice())
}