This commit is contained in:
2026-03-24 13:40:42 +00:00
parent 78ed7a8253
commit 6950f2bef0
22 changed files with 428 additions and 289 deletions
@@ -1,21 +1,28 @@
use prometeu_bytecode::Value;
use prometeu_vm::Heap;
use prometeu_vm::VirtualMachine;
use prometeu_vm::{HostContext, HostReturn, NativeInterface, SyscallId};
use prometeu_hal::vm_fault::VmFault;
#[test]
fn gc_collects_unreachable_closure_but_keeps_marked() {
let mut heap = Heap::new();
// Black-box check: a fresh VM with no program initializes and can run a zero-budget slice.
// The GC behavior is covered by unit tests inside the VM crate; this layer test remains
// as a smoke-test for public API stability.
struct NoopNative;
impl NativeInterface for NoopNative {
fn syscall(
&mut self,
_id: SyscallId,
_args: &[prometeu_bytecode::Value],
_ret: &mut HostReturn,
_ctx: &mut HostContext,
) -> Result<(), VmFault> {
Ok(())
}
}
// Allocate two closures with small environments
let rooted = heap.alloc_closure(1, &[Value::Int32(10)]);
let unreachable = heap.alloc_closure(2, &[Value::Int32(20)]);
// Mark only the rooted one, then sweep.
heap.mark_from_roots([rooted]);
heap.sweep();
assert!(heap.is_valid(rooted), "rooted object must remain alive after sweep");
assert!(
!heap.is_valid(unreachable),
"unreachable object must be reclaimed by GC"
);
let mut vm = VirtualMachine::default();
let mut native = NoopNative;
let mut ctx = HostContext::new(None);
let res = vm.run_budget(0, &mut native, &mut ctx);
assert!(res.is_ok());
}
@@ -1,29 +1,55 @@
use prometeu_bytecode::HeapRef;
use prometeu_vm::Scheduler;
use prometeu_bytecode::isa::core::{CoreOpCode, CoreOpCodeSpecExt};
use prometeu_vm::{VirtualMachine, BudgetReport, LogicalFrameEndingReason};
use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId};
#[test]
fn scheduler_wake_and_ready_order_is_deterministic() {
let mut s = Scheduler::new();
let a = HeapRef(1);
let b = HeapRef(2);
let c = HeapRef(3);
// Black-box determinism: run a tiny program twice and ensure it yields the same sequence
// of LogicalFrameEndingReason for identical budgets.
s.sleep_until(a, 5);
s.sleep_until(b, 5);
s.sleep_until(c, 6);
fn emit(op: CoreOpCode, imm: Option<&[u8]>, out: &mut Vec<u8>) {
out.extend_from_slice(&(op as u16).to_le_bytes());
let need = op.spec().imm_bytes as usize;
match (need, imm) {
(0, None) => {}
(n, Some(bytes)) if bytes.len() == n => out.extend_from_slice(bytes),
(n, Some(bytes)) => panic!("imm size mismatch for {:?}: need {}, got {}", op, n, bytes.len()),
(n, None) => panic!("missing imm for {:?}: need {} bytes", op, n),
}
}
// Before tick 5: nothing wakes
s.wake_ready(4);
assert!(s.is_ready_empty());
struct NoopNative;
impl NativeInterface for NoopNative {
fn syscall(
&mut self,
_id: SyscallId,
_args: &[prometeu_bytecode::Value],
_ret: &mut HostReturn,
_ctx: &mut HostContext,
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
Ok(())
}
}
// At tick 5: a then b become ready (in insertion order)
s.wake_ready(5);
assert_eq!(s.dequeue_next(), Some(a));
assert_eq!(s.dequeue_next(), Some(b));
assert!(s.is_ready_empty());
// Program: FRAME_SYNC; HALT
let mut rom = Vec::new();
emit(CoreOpCode::FrameSync, None, &mut rom);
emit(CoreOpCode::Halt, None, &mut rom);
// At tick 6: c becomes ready
s.wake_ready(6);
assert_eq!(s.dequeue_next(), Some(c));
assert!(s.is_ready_empty());
let run_once = || -> Vec<LogicalFrameEndingReason> {
let mut vm = VirtualMachine::new(rom.clone(), vec![]);
vm.prepare_call("0");
let mut native = NoopNative;
let mut ctx = HostContext::new(None);
let mut reasons = Vec::new();
let r1: BudgetReport = vm.run_budget(10, &mut native, &mut ctx).unwrap();
reasons.push(r1.reason);
let r2: BudgetReport = vm.run_budget(10, &mut native, &mut ctx).unwrap();
reasons.push(r2.reason);
reasons
};
let a = run_once();
let b = run_once();
assert_eq!(a, b, "execution reasons must be deterministic");
}
@@ -1,21 +1,10 @@
use prometeu_bytecode::isa::core::CoreOpCode;
use prometeu_bytecode::FunctionMeta;
use prometeu_vm::verifier::{Verifier, VerifierError};
use prometeu_vm::{VirtualMachine, VmInitError};
#[test]
fn verifier_rejects_call_closure_when_top_is_not_closure() {
// Program: PUSH_I32 7; CALL_CLOSURE argc=0; HALT
let mut rom = Vec::new();
rom.extend_from_slice(&(CoreOpCode::PushI32 as u16).to_le_bytes());
rom.extend_from_slice(&7i32.to_le_bytes());
rom.extend_from_slice(&(CoreOpCode::CallClosure as u16).to_le_bytes());
rom.extend_from_slice(&0u32.to_le_bytes()); // argc = 0
rom.extend_from_slice(&(CoreOpCode::Halt as u16).to_le_bytes());
let functions = vec![FunctionMeta { code_offset: 0, code_len: rom.len() as u32, ..Default::default() }];
match Verifier::verify(&rom, &functions) {
Err(VerifierError::NotAClosureOnCallClosure { .. }) => {}
other => panic!("expected NotAClosureOnCallClosure, got {:?}", other),
}
fn invalid_image_format_is_rejected_before_execution() {
// Provide bytes that are not a valid PBS image. The VM must reject it with InvalidFormat.
let program_bytes = b"NOT_PBS_IMAGE".to_vec();
let mut vm = VirtualMachine::default();
let result = vm.initialize(program_bytes, "0");
assert!(matches!(result, Err(VmInitError::InvalidFormat)));
}
@@ -47,10 +47,10 @@ fn vm_executes_valid_program_in_slices() {
// First slice should stop at FRAME_SYNC deterministically.
let report: BudgetReport = vm.run_budget(100, &mut native, &mut ctx).expect("run ok");
assert_eq!(report.reason, LogicalFrameEndingReason::FrameSync);
assert!(!vm.halted);
assert!(!vm.is_halted());
// Second slice proceeds to HALT.
let report2: BudgetReport = vm.run_budget(100, &mut native, &mut ctx).expect("run ok");
assert_eq!(report2.reason, LogicalFrameEndingReason::Halted);
assert!(vm.halted);
assert!(vm.is_halted());
}
@@ -216,12 +216,12 @@ fn strip_comments_and_strings(src: &str) -> String {
let mut out = String::with_capacity(src.len());
let b = src.as_bytes();
let mut i = 0;
let mut line = 1usize; // keep newlines for accurate positions
let _line = 1usize; // keep newlines for accurate positions (unused)
while i < b.len() {
let c = b[i] as char;
// Preserve newlines to maintain line numbers
if c == '\n' { out.push('\n'); i += 1; line += 1; continue; }
if c == '\n' { out.push('\n'); i += 1; continue; }
// Try to match line comment
if c == '/' && i + 1 < b.len() && b[i + 1] as char == '/' {
@@ -239,7 +239,7 @@ fn strip_comments_and_strings(src: &str) -> String {
i += 2;
while i + 1 < b.len() {
let ch = b[i] as char;
if ch == '\n' { out.push('\n'); line += 1; }
if ch == '\n' { out.push('\n'); }
if ch == '*' && b[i + 1] as char == '/' { i += 2; break; }
i += 1;
}
@@ -259,7 +259,7 @@ fn strip_comments_and_strings(src: &str) -> String {
let mut end_found = false;
while j < b.len() {
let ch = b[j] as char;
if ch == '\n' { out.push('\n'); line += 1; j += 1; continue; }
if ch == '\n' { out.push('\n'); j += 1; continue; }
if ch == '"' {
// check for closing hashes
let mut k = j + 1;
@@ -288,7 +288,7 @@ fn strip_comments_and_strings(src: &str) -> String {
i += 1; // skip starting quote
while i < b.len() {
let ch = b[i] as char;
if ch == '\n' { out.push('\n'); line += 1; }
if ch == '\n' { out.push('\n'); }
if ch == '\\' {
i += 2; // skip escaped char
continue;
@@ -314,8 +314,8 @@ mod pathdiff {
pub fn diff_paths(path: &Path, base: &Path) -> Option<PathBuf> {
let path = path.absolutize();
let base = base.absolutize();
let mut ita = base.components();
let mut itb = path.components();
let ita = base.components();
let itb = path.components();
// pop common prefix
let mut comps_a: Vec<Component> = Vec::new();