holey-bytes/hbvm/fuzz/fuzz_targets/vm.rs

81 lines
2 KiB
Rust
Raw Normal View History

2023-07-25 18:01:53 -05:00
#![no_main]
use {
2023-08-08 19:33:03 -05:00
hbbytecode::valider::validate,
2023-07-25 18:11:21 -05:00
hbvm::{
2023-08-15 09:32:59 -05:00
mem::softpaging::{
2023-08-08 19:33:03 -05:00
paging::{PageTable, Permission},
HandlePageFault, PageSize, SoftPagedMem,
},
MemoryAccessReason, Vm,
2023-07-25 18:01:53 -05:00
},
libfuzzer_sys::fuzz_target,
};
fuzz_target!(|data: &[u8]| {
2023-08-08 19:33:03 -05:00
if validate(data).is_ok() {
let mut vm = unsafe {
Vm::<_, 16384>::new(
2023-08-16 20:45:57 -05:00
SoftPagedMem::<_, true> {
2023-08-08 19:33:03 -05:00
pf_handler: TestTrapHandler,
program: data,
root_pt: Box::into_raw(Default::default()),
2023-08-16 20:37:40 -05:00
icache: Default::default(),
2023-08-08 19:33:03 -05:00
},
0,
)
};
2023-07-26 05:22:28 -05:00
// Alloc and map some memory
let pages = [
alloc_and_map(&mut vm.memory, 0),
alloc_and_map(&mut vm.memory, 4096),
];
2023-07-26 05:22:28 -05:00
// Run VM
2023-07-25 18:01:53 -05:00
let _ = vm.run();
2023-07-26 05:22:28 -05:00
// Unmap and dealloc the memory
for (i, page) in pages.into_iter().enumerate() {
unmap_and_dealloc(&mut vm.memory, page, i as u64 * 4096);
}
2023-07-25 18:01:53 -05:00
}
});
2023-08-08 19:33:03 -05:00
fn alloc_and_map(memory: &mut SoftPagedMem<TestTrapHandler>, at: u64) -> *mut u8 {
2023-07-26 05:22:28 -05:00
let ptr = Box::into_raw(Box::<Page>::default()).cast();
unsafe {
memory
2023-08-08 19:33:03 -05:00
.map(ptr, at, Permission::Write, PageSize::Size4K)
2023-07-26 05:22:28 -05:00
.unwrap()
};
ptr
}
2023-08-08 19:33:03 -05:00
fn unmap_and_dealloc(memory: &mut SoftPagedMem<TestTrapHandler>, ptr: *mut u8, from: u64) {
2023-07-26 05:22:28 -05:00
memory.unmap(from).unwrap();
let _ = unsafe { Box::from_raw(ptr.cast::<Page>()) };
}
#[repr(align(4096))]
struct Page([u8; 4096]);
impl Default for Page {
fn default() -> Self {
unsafe { std::mem::MaybeUninit::zeroed().assume_init() }
}
}
2023-07-25 18:01:53 -05:00
struct TestTrapHandler;
impl HandlePageFault for TestTrapHandler {
fn page_fault(
&mut self,
_: MemoryAccessReason,
2023-08-08 19:33:03 -05:00
_: &mut PageTable,
2023-07-25 18:01:53 -05:00
_: u64,
_: PageSize,
_: *mut u8,
) -> bool {
false
}
}