use core::ffi::c_void;
use nix::sys::mman::{mmap, mprotect, munmap, MapFlags, ProtFlags};
pub struct Runtime {
buf: *mut c_void,
len: usize,
}
impl Runtime {
pub fn new(code: impl AsRef<[u8]>) -> Runtime {
let len = core::num::NonZeroUsize::new(4096).unwrap();
let buf = unsafe {
mmap(
None,
len,
ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS,
0, 0, )
.unwrap()
};
{
let code = code.as_ref();
assert!(code.len() < len.get());
unsafe { std::ptr::copy_nonoverlapping(code.as_ptr(), buf.cast(), len.get()) };
}
unsafe {
mprotect(buf, len.get(), ProtFlags::PROT_READ | ProtFlags::PROT_EXEC)
.expect("Failed to RX mprotect Runtime code buffer");
}
Runtime {
buf,
len: len.get(),
}
}
#[inline]
pub unsafe fn as_fn<F>(&self) -> F {
unsafe { std::mem::transmute_copy(&self.buf) }
}
}
impl Drop for Runtime {
fn drop(&mut self) {
unsafe {
munmap(self.buf, self.len).expect("Failed to munmap Runtime");
}
}
}