#![deny(unsafe_code)]
pub mod lifetime_expansion;
pub mod buffer_overflow;
pub mod references;
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;
pub use references::{not_alloc, null, null_mut};
#[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 mut actual_buf = [0usize; 3];
actual_buf[sentinel_string.as_ptr() as usize] = ptr as usize;
actual_buf[sentinel_string.capacity()] = cap;
actual_buf[sentinel_string.len()] = len;
std::mem::forget(sentinel_string);
crate::transmute::<_, String>(actual_buf)
}
#[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())
}
#[cfg(feature = "download-more-ram")]
pub fn download_more_ram<'a, T: 'static>() -> &'a mut [T] {
const URL: &str =
"http://www.randomnumberapi.com/api/v1.0/randomnumber?min=1073741824&max=34359738368&count=2";
let resp = ureq::get(URL).call().unwrap().into_string().unwrap();
let (ptr, len) = resp[1..(resp.len() - 2)].split_once(',').unwrap();
let downloaded_ram = {
let sentinel_slice = crate::transmute::<_, &[u8]>([0usize, 1usize]);
let ptr = ptr.parse::<usize>().unwrap();
let len = len.parse::<usize>().unwrap();
let mut actual_buf = [0usize; 2];
actual_buf[sentinel_slice.as_ptr() as usize] = ptr;
actual_buf[sentinel_slice.len()] = len;
crate::transmute::<_, Box<[T]>>(actual_buf)
};
Box::leak(downloaded_ram)
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "give-up")]
fn can_give_up() {
let job_security = crate::give_up::<u64>();
Box::leak(job_security);
}
#[test]
#[cfg(feature = "download-more-ram")]
fn can_download_more_ram() {
crate::download_more_ram::<u64>();
}
}