2023-10-18 05:14:24 -05:00
|
|
|
|
//! Holey Bytes Experimental Runtime
|
|
|
|
|
mod mem;
|
|
|
|
|
|
|
|
|
|
use {
|
|
|
|
|
hbvm::{mem::Address, Vm, VmRunOk},
|
|
|
|
|
nix::sys::mman::{mmap, MapFlags, ProtFlags},
|
2023-10-21 21:21:45 -05:00
|
|
|
|
std::{env::args, fs::File, num::NonZeroUsize, process::exit},
|
2023-10-18 05:14:24 -05:00
|
|
|
|
};
|
2023-09-26 16:36:27 -05:00
|
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2023-10-18 05:14:24 -05:00
|
|
|
|
eprintln!("== HB×RT (Holey Bytes Linux Runtime) v0.1 ==");
|
|
|
|
|
eprintln!("[W] Currently supporting only flat images");
|
|
|
|
|
|
|
|
|
|
let Some(image_path) = args().nth(1) else {
|
|
|
|
|
eprintln!("[E] Missing image path");
|
|
|
|
|
exit(1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Load program
|
|
|
|
|
eprintln!("[I] Loading image from \"{image_path}\"");
|
|
|
|
|
let file = File::open(image_path)?;
|
|
|
|
|
let ptr = unsafe {
|
|
|
|
|
mmap(
|
|
|
|
|
None,
|
|
|
|
|
NonZeroUsize::new(file.metadata()?.len() as usize).ok_or("File is empty")?,
|
|
|
|
|
ProtFlags::PROT_READ,
|
|
|
|
|
MapFlags::MAP_PRIVATE,
|
|
|
|
|
Some(&file),
|
|
|
|
|
0,
|
|
|
|
|
)?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
eprintln!("[I] Image loaded at {ptr:p}");
|
|
|
|
|
|
|
|
|
|
// Execute program
|
|
|
|
|
let mut vm = unsafe { Vm::<_, 0>::new(mem::HostMemory, Address::new(ptr as u64)) };
|
2023-10-19 17:12:32 -05:00
|
|
|
|
|
|
|
|
|
// Memory access fault handling
|
|
|
|
|
unsafe {
|
|
|
|
|
use nix::sys::signal;
|
|
|
|
|
|
|
|
|
|
extern "C" fn action(
|
|
|
|
|
_: std::ffi::c_int,
|
|
|
|
|
info: *mut nix::libc::siginfo_t,
|
|
|
|
|
_: *mut std::ffi::c_void,
|
|
|
|
|
) {
|
|
|
|
|
unsafe {
|
|
|
|
|
eprintln!("[E] Memory access fault at {:p}", (*info).si_addr());
|
2023-11-02 13:53:03 -05:00
|
|
|
|
exit(2);
|
2023-10-19 17:12:32 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signal::sigaction(
|
|
|
|
|
signal::Signal::SIGSEGV,
|
|
|
|
|
&nix::sys::signal::SigAction::new(
|
|
|
|
|
signal::SigHandler::SigAction(action),
|
|
|
|
|
signal::SaFlags::SA_NODEFER,
|
|
|
|
|
nix::sys::signalfd::SigSet::empty(),
|
|
|
|
|
),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-18 05:14:24 -05:00
|
|
|
|
let stat = loop {
|
|
|
|
|
match vm.run() {
|
|
|
|
|
Ok(VmRunOk::Breakpoint) => eprintln!(
|
|
|
|
|
"[I] Hit breakpoint\nIP: {}\n== Registers ==\n{:?}",
|
|
|
|
|
vm.pc, vm.registers
|
|
|
|
|
),
|
|
|
|
|
Ok(VmRunOk::Timer) => (),
|
|
|
|
|
Ok(VmRunOk::Ecall) => unsafe {
|
|
|
|
|
std::arch::asm!(
|
|
|
|
|
"syscall",
|
|
|
|
|
inlateout("rax") vm.registers[1].0,
|
|
|
|
|
in("rdi") vm.registers[2].0,
|
|
|
|
|
in("rsi") vm.registers[3].0,
|
|
|
|
|
in("rdx") vm.registers[4].0,
|
|
|
|
|
in("r10") vm.registers[5].0,
|
|
|
|
|
in("r8") vm.registers[6].0,
|
|
|
|
|
in("r9") vm.registers[7].0,
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
Ok(VmRunOk::End) => break Ok(()),
|
|
|
|
|
Err(e) => break Err(e),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
eprintln!("\n== Registers ==\n{:?}", vm.registers);
|
|
|
|
|
if let Err(e) = stat {
|
|
|
|
|
eprintln!("\n[E] Runtime error: {e:?}");
|
|
|
|
|
exit(2);
|
|
|
|
|
}
|
2023-09-26 16:36:27 -05:00
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|