quality baseline
This commit is contained in:
@@ -7,3 +7,6 @@ license.workspace = true
|
||||
[dependencies]
|
||||
prometeu-bytecode = { path = "../prometeu-bytecode" }
|
||||
prometeu-hal = { path = "../prometeu-hal" }
|
||||
|
||||
[dev-dependencies]
|
||||
prometeu-test-support = { path = "../../dev/prometeu-test-support" }
|
||||
|
||||
@@ -2,4 +2,4 @@ pub struct CallFrame {
|
||||
pub return_pc: u32,
|
||||
pub stack_base: usize,
|
||||
pub func_idx: usize,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
mod virtual_machine;
|
||||
mod call_frame;
|
||||
mod scope_frame;
|
||||
pub mod local_addressing;
|
||||
mod scope_frame;
|
||||
pub mod verifier;
|
||||
mod virtual_machine;
|
||||
pub mod vm_init_error;
|
||||
|
||||
pub use prometeu_hal::{
|
||||
HostContext, HostReturn, NativeInterface, SyscallId,
|
||||
};
|
||||
pub use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
pub use virtual_machine::{BudgetReport, LogicalFrameEndingReason, VirtualMachine};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::call_frame::CallFrame;
|
||||
use prometeu_bytecode::{TrapInfo, TRAP_INVALID_LOCAL};
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
use prometeu_bytecode::{TRAP_INVALID_LOCAL, TrapInfo};
|
||||
|
||||
/// Computes the absolute stack index for the start of the current frame's locals (including args).
|
||||
pub fn local_base(frame: &CallFrame) -> usize {
|
||||
@@ -14,7 +14,12 @@ pub fn local_index(frame: &CallFrame, slot: u32) -> usize {
|
||||
|
||||
/// Validates that a local slot index is within the valid range for the function.
|
||||
/// Range: 0 <= slot < (param_slots + local_slots)
|
||||
pub fn check_local_slot(meta: &FunctionMeta, slot: u32, opcode: u16, pc: u32) -> Result<(), TrapInfo> {
|
||||
pub fn check_local_slot(
|
||||
meta: &FunctionMeta,
|
||||
slot: u32,
|
||||
opcode: u16,
|
||||
pc: u32,
|
||||
) -> Result<(), TrapInfo> {
|
||||
let limit = meta.param_slots as u32 + meta.local_slots as u32;
|
||||
if slot < limit {
|
||||
Ok(())
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub struct ScopeFrame {
|
||||
pub scope_stack_base: usize,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use prometeu_hal::syscalls::Syscall;
|
||||
use prometeu_bytecode::{decode_next, DecodeError};
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
use prometeu_bytecode::OpCode;
|
||||
use prometeu_bytecode::OpCodeSpecExt;
|
||||
use prometeu_bytecode::FunctionMeta;
|
||||
use prometeu_bytecode::{compute_function_layouts, FunctionLayout};
|
||||
use prometeu_bytecode::{DecodeError, decode_next};
|
||||
use prometeu_bytecode::{FunctionLayout, compute_function_layouts};
|
||||
use prometeu_hal::syscalls::Syscall;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -71,14 +71,23 @@ impl Verifier {
|
||||
while pc < func_code.len() {
|
||||
valid_pc.insert(pc);
|
||||
let instr = decode_next(pc, func_code).map_err(|e| match e {
|
||||
DecodeError::UnknownOpcode { pc: _, opcode } =>
|
||||
VerifierError::UnknownOpcode { pc: func_start + pc, opcode },
|
||||
DecodeError::TruncatedOpcode { pc: _ } =>
|
||||
VerifierError::TruncatedOpcode { pc: func_start + pc },
|
||||
DecodeError::TruncatedImmediate { pc: _, opcode, need, have } =>
|
||||
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need, have },
|
||||
DecodeError::ImmediateSizeMismatch { pc: _, opcode, expected, actual } =>
|
||||
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need: expected, have: actual },
|
||||
DecodeError::UnknownOpcode { pc: _, opcode } => {
|
||||
VerifierError::UnknownOpcode { pc: func_start + pc, opcode }
|
||||
}
|
||||
DecodeError::TruncatedOpcode { pc: _ } => {
|
||||
VerifierError::TruncatedOpcode { pc: func_start + pc }
|
||||
}
|
||||
DecodeError::TruncatedImmediate { pc: _, opcode, need, have } => {
|
||||
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need, have }
|
||||
}
|
||||
DecodeError::ImmediateSizeMismatch { pc: _, opcode, expected, actual } => {
|
||||
VerifierError::TruncatedImmediate {
|
||||
pc: func_start + pc,
|
||||
opcode,
|
||||
need: expected,
|
||||
have: actual,
|
||||
}
|
||||
}
|
||||
})?;
|
||||
pc = instr.next_pc;
|
||||
}
|
||||
@@ -113,9 +122,7 @@ impl Verifier {
|
||||
})?;
|
||||
(callee.param_slots, callee.return_slots)
|
||||
}
|
||||
OpCode::Ret => {
|
||||
(func.return_slots, 0)
|
||||
}
|
||||
OpCode::Ret => (func.return_slots, 0),
|
||||
OpCode::Syscall => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let syscall = Syscall::from_u32(id).ok_or_else(|| {
|
||||
@@ -127,7 +134,10 @@ impl Verifier {
|
||||
};
|
||||
|
||||
if in_height < pops {
|
||||
return Err(VerifierError::StackUnderflow { pc: func_start + pc, opcode: instr.opcode });
|
||||
return Err(VerifierError::StackUnderflow {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
});
|
||||
}
|
||||
|
||||
let out_height = in_height - pops + pushes;
|
||||
@@ -135,7 +145,11 @@ impl Verifier {
|
||||
|
||||
if instr.opcode == OpCode::Ret {
|
||||
if in_height != func.return_slots {
|
||||
return Err(VerifierError::BadRetStackHeight { pc: func_start + pc, height: in_height, expected: func.return_slots });
|
||||
return Err(VerifierError::BadRetStackHeight {
|
||||
pc: func_start + pc,
|
||||
height: in_height,
|
||||
expected: func.return_slots,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +157,8 @@ impl Verifier {
|
||||
if spec.is_branch {
|
||||
// Canonical contract: branch immediate is RELATIVE to function start.
|
||||
let target_rel = instr.imm_u32().unwrap() as usize;
|
||||
let func_end_abs = layouts.get(func_idx).map(|l| l.end).unwrap_or_else(|| code.len());
|
||||
let func_end_abs =
|
||||
layouts.get(func_idx).map(|l| l.end).unwrap_or_else(|| code.len());
|
||||
let func_len = func_end_abs - func_start;
|
||||
|
||||
if target_rel > func_len {
|
||||
@@ -168,23 +183,38 @@ impl Verifier {
|
||||
target_abs_expected,
|
||||
is_boundary_target_rel
|
||||
);
|
||||
return Err(VerifierError::InvalidJumpTarget { pc: pc_abs, target: target_abs_expected });
|
||||
return Err(VerifierError::InvalidJumpTarget {
|
||||
pc: pc_abs,
|
||||
target: target_abs_expected,
|
||||
});
|
||||
}
|
||||
|
||||
if target_rel == func_len {
|
||||
// salto para o fim da função
|
||||
if out_height != func.return_slots {
|
||||
return Err(VerifierError::BadRetStackHeight { pc: func_start + pc, height: out_height, expected: func.return_slots });
|
||||
return Err(VerifierError::BadRetStackHeight {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
expected: func.return_slots,
|
||||
});
|
||||
}
|
||||
// caminho termina aqui
|
||||
} else {
|
||||
if !valid_pc.contains(&target_rel) {
|
||||
return Err(VerifierError::JumpToMidInstruction { pc: func_start + pc, target: func_start + target_rel });
|
||||
return Err(VerifierError::JumpToMidInstruction {
|
||||
pc: func_start + pc,
|
||||
target: func_start + target_rel,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(&existing_height) = stack_height_in.get(&target_rel) {
|
||||
if existing_height != out_height {
|
||||
return Err(VerifierError::StackMismatchJoin { pc: func_start + pc, target: func_start + target_rel, height_in: out_height, height_target: existing_height });
|
||||
return Err(VerifierError::StackMismatchJoin {
|
||||
pc: func_start + pc,
|
||||
target: func_start + target_rel,
|
||||
height_in: out_height,
|
||||
height_target: existing_height,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
stack_height_in.insert(target_rel, out_height);
|
||||
@@ -199,7 +229,12 @@ impl Verifier {
|
||||
if next_pc < func_len {
|
||||
if let Some(&existing_height) = stack_height_in.get(&next_pc) {
|
||||
if existing_height != out_height {
|
||||
return Err(VerifierError::StackMismatchJoin { pc: func_start + pc, target: func_start + next_pc, height_in: out_height, height_target: existing_height });
|
||||
return Err(VerifierError::StackMismatchJoin {
|
||||
pc: func_start + pc,
|
||||
target: func_start + next_pc,
|
||||
height_in: out_height,
|
||||
height_target: existing_height,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
stack_height_in.insert(next_pc, out_height);
|
||||
@@ -221,18 +256,15 @@ mod tests {
|
||||
fn test_verifier_underflow() {
|
||||
// OpCode::Add (2 bytes)
|
||||
let code = vec![OpCode::Add as u8, 0x00];
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 2,
|
||||
..Default::default()
|
||||
}];
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 2, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::StackUnderflow { pc: 0, opcode: OpCode::Add }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_verifier_dup_underflow() {
|
||||
let code = vec![(OpCode::Dup as u16).to_le_bytes()[0], (OpCode::Dup as u16).to_le_bytes()[1]];
|
||||
let code =
|
||||
vec![(OpCode::Dup as u16).to_le_bytes()[0], (OpCode::Dup as u16).to_le_bytes()[1]];
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 2, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::StackUnderflow { pc: 0, opcode: OpCode::Dup }));
|
||||
@@ -243,11 +275,7 @@ mod tests {
|
||||
// Jmp (2 bytes) + 100u32 (4 bytes)
|
||||
let mut code = vec![OpCode::Jmp as u8, 0x00];
|
||||
code.extend_from_slice(&100u32.to_le_bytes());
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 6,
|
||||
..Default::default()
|
||||
}];
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 6, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::InvalidJumpTarget { pc: 0, target: 100 }));
|
||||
}
|
||||
@@ -261,12 +289,8 @@ mod tests {
|
||||
code.push(OpCode::Jmp as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&1u32.to_le_bytes());
|
||||
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 12,
|
||||
..Default::default()
|
||||
}];
|
||||
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 12, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::JumpToMidInstruction { pc: 6, target: 1 }));
|
||||
}
|
||||
@@ -295,11 +319,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_verifier_truncation_opcode() {
|
||||
let code = vec![OpCode::PushI32 as u8]; // Truncated u16 opcode
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 1,
|
||||
..Default::default()
|
||||
}];
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 1, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::TruncatedOpcode { pc: 0 }));
|
||||
}
|
||||
@@ -308,13 +328,17 @@ mod tests {
|
||||
fn test_verifier_truncation_immediate() {
|
||||
let mut code = vec![OpCode::PushI32 as u8, 0x00];
|
||||
code.push(0x42); // Only 1 byte of 4-byte immediate
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 3,
|
||||
..Default::default()
|
||||
}];
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 3, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::TruncatedImmediate { pc: 0, opcode: OpCode::PushI32, need: 4, have: 1 }));
|
||||
assert_eq!(
|
||||
res,
|
||||
Err(VerifierError::TruncatedImmediate {
|
||||
pc: 0,
|
||||
opcode: OpCode::PushI32,
|
||||
need: 4,
|
||||
have: 1
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -326,26 +350,41 @@ mod tests {
|
||||
// 15: PushI32 1
|
||||
// 21: Jmp 27
|
||||
// 27: Nop
|
||||
|
||||
|
||||
let mut code = Vec::new();
|
||||
code.push(OpCode::PushBool as u8); code.push(0x00); code.push(1); // 0: PushBool (3 bytes)
|
||||
code.push(OpCode::JmpIfTrue as u8); code.push(0x00); code.extend_from_slice(&15u32.to_le_bytes()); // 3: JmpIfTrue (6 bytes)
|
||||
code.push(OpCode::Jmp as u8); code.push(0x00); code.extend_from_slice(&27u32.to_le_bytes()); // 9: Jmp (6 bytes)
|
||||
code.push(OpCode::PushI32 as u8); code.push(0x00); code.extend_from_slice(&1u32.to_le_bytes()); // 15: PushI32 (6 bytes)
|
||||
code.push(OpCode::Jmp as u8); code.push(0x00); code.extend_from_slice(&27u32.to_le_bytes()); // 21: Jmp (6 bytes)
|
||||
code.push(OpCode::Nop as u8); code.push(0x00); // 27: Nop (2 bytes)
|
||||
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 29,
|
||||
..Default::default()
|
||||
}];
|
||||
code.push(OpCode::PushBool as u8);
|
||||
code.push(0x00);
|
||||
code.push(1); // 0: PushBool (3 bytes)
|
||||
code.push(OpCode::JmpIfTrue as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&15u32.to_le_bytes()); // 3: JmpIfTrue (6 bytes)
|
||||
code.push(OpCode::Jmp as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&27u32.to_le_bytes()); // 9: Jmp (6 bytes)
|
||||
code.push(OpCode::PushI32 as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&1u32.to_le_bytes()); // 15: PushI32 (6 bytes)
|
||||
code.push(OpCode::Jmp as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&27u32.to_le_bytes()); // 21: Jmp (6 bytes)
|
||||
code.push(OpCode::Nop as u8);
|
||||
code.push(0x00); // 27: Nop (2 bytes)
|
||||
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 29, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
// Path 0->3->9->27: height 1-1+0 = 0.
|
||||
// Path 0->3->15->21->27: height 1-1+1 = 1.
|
||||
// Mismatch at 27: 0 vs 1.
|
||||
|
||||
assert_eq!(res, Err(VerifierError::StackMismatchJoin { pc: 21, target: 27, height_in: 1, height_target: 0 }));
|
||||
|
||||
assert_eq!(
|
||||
res,
|
||||
Err(VerifierError::StackMismatchJoin {
|
||||
pc: 21,
|
||||
target: 27,
|
||||
height_in: 1,
|
||||
height_target: 0
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -356,7 +395,7 @@ mod tests {
|
||||
code.extend_from_slice(&1u32.to_le_bytes());
|
||||
code.push(OpCode::Ret as u8);
|
||||
code.push(0x00);
|
||||
|
||||
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 8,
|
||||
@@ -374,11 +413,17 @@ mod tests {
|
||||
// Add
|
||||
// Ret
|
||||
let mut code = Vec::new();
|
||||
code.push(OpCode::PushI32 as u8); code.push(0x00); code.extend_from_slice(&1u32.to_le_bytes());
|
||||
code.push(OpCode::PushI32 as u8); code.push(0x00); code.extend_from_slice(&2u32.to_le_bytes());
|
||||
code.push(OpCode::Add as u8); code.push(0x00);
|
||||
code.push(OpCode::Ret as u8); code.push(0x00);
|
||||
|
||||
code.push(OpCode::PushI32 as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&1u32.to_le_bytes());
|
||||
code.push(OpCode::PushI32 as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&2u32.to_le_bytes());
|
||||
code.push(OpCode::Add as u8);
|
||||
code.push(0x00);
|
||||
code.push(OpCode::Ret as u8);
|
||||
code.push(0x00);
|
||||
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 16,
|
||||
@@ -392,14 +437,11 @@ mod tests {
|
||||
#[test]
|
||||
fn test_verifier_invalid_syscall_id() {
|
||||
let mut code = Vec::new();
|
||||
code.push(OpCode::Syscall as u8); code.push(0x00);
|
||||
code.push(OpCode::Syscall as u8);
|
||||
code.push(0x00);
|
||||
code.extend_from_slice(&0xDEADBEEFu32.to_le_bytes()); // Unknown ID
|
||||
|
||||
let functions = vec![FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: 6,
|
||||
..Default::default()
|
||||
}];
|
||||
|
||||
let functions = vec![FunctionMeta { code_offset: 0, code_len: 6, ..Default::default() }];
|
||||
let res = Verifier::verify(&code, &functions);
|
||||
assert_eq!(res, Err(VerifierError::InvalidSyscallId { pc: 0, id: 0xDEADBEEF }));
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,29 @@
|
||||
//! Minimal, robust smoke test for the VM crate.
|
||||
//!
|
||||
//! Intentionally does not assert legacy ISA behavior. It only ensures that:
|
||||
//! - The VM type can be instantiated without panicking.
|
||||
//! - Deterministic test utilities work as expected (reproducible RNG).
|
||||
|
||||
use prometeu_test_support::{next_u64, rng_from_seed};
|
||||
use prometeu_vm::VirtualMachine;
|
||||
|
||||
#[test]
|
||||
fn vm_instantiation_is_stable() {
|
||||
// Create a VM with empty ROM and empty constant pool.
|
||||
let vm = VirtualMachine::new(vec![], vec![]);
|
||||
// Basic invariant checks that should remain stable across refactors.
|
||||
assert_eq!(vm.pc, 0);
|
||||
assert!(!vm.halted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deterministic_rng_sequences_match() {
|
||||
// Demonstrate deterministic behavior without relying on specific values.
|
||||
let mut a = rng_from_seed(12345);
|
||||
let mut b = rng_from_seed(12345);
|
||||
|
||||
for _ in 0..8 {
|
||||
// Same seed => same sequence
|
||||
assert_eq!(next_u64(&mut a), next_u64(&mut b));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user