This commit is contained in:
2026-03-24 13:40:44 +00:00
parent 2a79f641dd
commit 96062bf1d0
10 changed files with 101 additions and 10 deletions
@@ -67,6 +67,10 @@ pub enum VerifierError {
pc: usize,
id: u32,
},
HostcallNotPatched {
pc: usize,
sysc_index: u32,
},
/// Execution can fall through past the end of the function without a valid terminator
/// (e.g., RET, JMP to end, HALT/TRAP). Verifier requires every reachable path to end
/// in a terminator.
@@ -254,6 +258,12 @@ impl Verifier {
(arg_count, 0)
}
OpCode::Ret => (func.return_slots, 0),
OpCode::Hostcall => {
return Err(VerifierError::HostcallNotPatched {
pc: func_start + pc,
sysc_index: instr.imm_u32().unwrap(),
});
}
OpCode::Syscall => {
let id = instr.imm_u32().unwrap();
let syscall = Syscall::from_u32(id).ok_or_else(|| {
@@ -1145,6 +1155,18 @@ mod tests {
assert_eq!(res, Err(VerifierError::InvalidSyscallId { pc: 0, id: 0xDEADBEEF }));
}
#[test]
fn test_verifier_rejects_unpatched_hostcall() {
let mut code = Vec::new();
code.push(OpCode::Hostcall as u8);
code.push(0x00);
code.extend_from_slice(&3u32.to_le_bytes());
let functions = vec![FunctionMeta { code_offset: 0, code_len: 6, ..Default::default() }];
let res = Verifier::verify(&code, &functions);
assert_eq!(res, Err(VerifierError::HostcallNotPatched { pc: 0, sysc_index: 3 }));
}
#[test]
fn test_function_without_terminator_is_rejected() {
// Single NOP with no RET/JMP/TRAP/HALT at the end → fallthrough to end
@@ -699,6 +699,15 @@ impl VirtualMachine {
self.handle_safepoint();
return Err(LogicalFrameEndingReason::FrameSync);
}
OpCode::Hostcall => {
let sysc_index = instr
.imm_u32()
.map_err(|e| LogicalFrameEndingReason::Panic(format!("{:?}", e)))?;
return Err(LogicalFrameEndingReason::Panic(format!(
"HOSTCALL {} reached execution without loader patching",
sysc_index
)));
}
OpCode::MakeClosure => {
// Immediate carries (fn_id, capture_count)
let (fn_id, cap_count) = instr
@@ -2639,9 +2648,16 @@ mod tests {
#[test]
fn test_loader_hardening_entrypoint_not_found() {
let mut vm = VirtualMachine::default();
// Valid empty PBS v0 module
let mut header = vec![0u8; 32];
header[0..4].copy_from_slice(b"PBS\0");
let header = prometeu_bytecode::model::BytecodeModule {
version: 0,
const_pool: vec![],
functions: vec![],
code: vec![],
debug_info: None,
exports: vec![],
syscalls: vec![],
}
.serialize();
// Try to initialize with numeric entrypoint 10 (out of bounds for empty ROM)
let res = vm.initialize(header, "10");
@@ -2657,8 +2673,16 @@ mod tests {
let mut vm = VirtualMachine::default();
vm.pc = 123; // Pollution
let mut header = vec![0u8; 32];
header[0..4].copy_from_slice(b"PBS\0");
let header = prometeu_bytecode::model::BytecodeModule {
version: 0,
const_pool: vec![],
functions: vec![],
code: vec![],
debug_info: None,
exports: vec![],
syscalls: vec![],
}
.serialize();
let res = vm.initialize(header, "");
assert!(res.is_ok());