This commit is contained in:
2026-03-24 13:40:39 +00:00
parent de3ccabd62
commit a5b332a362
2 changed files with 39 additions and 50 deletions
@@ -21,6 +21,10 @@ pub enum VerifierError {
InvalidSyscallId { pc: usize, id: u32 },
TrailingBytes { func_idx: usize, at_pc: usize },
InvalidFuncId { pc: usize, id: 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.
UnterminatedPath { func_idx: usize, at_pc: usize },
}
pub struct Verifier;
@@ -250,6 +254,12 @@ impl Verifier {
stack_height_in.insert(next_pc, out_height);
worklist.push_back(next_pc);
}
} else if next_pc == func_len {
// Reaching end of function by falling through (no terminator) is invalid.
return Err(VerifierError::UnterminatedPath {
func_idx,
at_pc: func_start + pc,
});
}
}
}
@@ -483,4 +493,33 @@ mod tests {
let res = Verifier::verify(&code, &functions);
assert_eq!(res, Err(VerifierError::InvalidSyscallId { pc: 0, id: 0xDEADBEEF }));
}
#[test]
fn test_function_without_terminator_is_rejected() {
// Single NOP with no RET/JMP/TRAP/HALT at the end → fallthrough to end
let mut code = Vec::new();
code.push(OpCode::Nop as u8);
code.push(0x00);
let functions = vec![FunctionMeta { code_offset: 0, code_len: 2, ..Default::default() }];
let res = Verifier::verify(&code, &functions);
assert_eq!(res, Err(VerifierError::UnterminatedPath { func_idx: 0, at_pc: 0 }));
}
#[test]
fn test_function_with_proper_terminator_passes() {
// Minimal function that returns immediately
let mut code = Vec::new();
code.push(OpCode::Ret as u8);
code.push(0x00);
let functions = vec![FunctionMeta {
code_offset: 0,
code_len: 2,
return_slots: 0,
..Default::default()
}];
let res = Verifier::verify(&code, &functions).unwrap();
assert_eq!(res[0], 0);
}
}