implements PR-007 to PR011
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::fs::{FsBackend, FsState, VirtualFS};
|
||||
use prometeu_bytecode::{TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_TYPE, Value};
|
||||
use crate::CrashReport;
|
||||
use prometeu_bytecode::{Value, TRAP_INVALID_SYSCALL, TRAP_OOB, TRAP_TYPE};
|
||||
use prometeu_hal::asset::{BankType, LoadStatus, SlotRef};
|
||||
use prometeu_hal::button::Button;
|
||||
use prometeu_hal::cartridge::{AppMode, Cartridge};
|
||||
@@ -10,9 +11,9 @@ use prometeu_hal::syscalls::Syscall;
|
||||
use prometeu_hal::telemetry::{CertificationConfig, Certifier, TelemetryFrame};
|
||||
use prometeu_hal::tile::Tile;
|
||||
use prometeu_hal::vm_fault::VmFault;
|
||||
use prometeu_hal::{expect_bool, expect_int, HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
use prometeu_hal::{HardwareBridge, InputSignals};
|
||||
use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId, expect_bool, expect_int};
|
||||
use prometeu_vm::{LogicalFrameEndingReason, VirtualMachine, VmInitError};
|
||||
use prometeu_vm::{LogicalFrameEndingReason, VirtualMachine};
|
||||
use std::collections::HashMap;
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -55,6 +56,8 @@ pub struct VirtualMachineRuntime {
|
||||
pub telemetry_current: TelemetryFrame,
|
||||
/// The results of the last successfully completed logical frame.
|
||||
pub telemetry_last: TelemetryFrame,
|
||||
/// Last terminal crash report surfaced by the runtime, if any.
|
||||
pub last_crash_report: Option<CrashReport>,
|
||||
/// Logic for validating that the app obeys the console's Certification (CAP).
|
||||
pub certifier: Certifier,
|
||||
/// Pause state: When true, `tick()` will not advance the VM.
|
||||
@@ -104,6 +107,7 @@ impl VirtualMachineRuntime {
|
||||
logs_written_this_frame: HashMap::new(),
|
||||
telemetry_current: TelemetryFrame::default(),
|
||||
telemetry_last: TelemetryFrame::default(),
|
||||
last_crash_report: None,
|
||||
certifier: Certifier::new(cap_config.unwrap_or_default()),
|
||||
paused: false,
|
||||
debug_step_request: false,
|
||||
@@ -161,13 +165,34 @@ impl VirtualMachineRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset(&mut self, vm: &mut VirtualMachine) {
|
||||
*vm = VirtualMachine::default();
|
||||
self.tick_index = 0;
|
||||
fn clear_cartridge_state(&mut self) {
|
||||
self.logical_frame_index = 0;
|
||||
self.logical_frame_active = false;
|
||||
self.logical_frame_remaining_cycles = 0;
|
||||
self.last_frame_cpu_time_us = 0;
|
||||
|
||||
self.open_files.clear();
|
||||
self.next_handle = 1;
|
||||
|
||||
self.current_app_id = 0;
|
||||
self.current_cartridge_title.clear();
|
||||
self.current_cartridge_app_version.clear();
|
||||
self.current_cartridge_app_mode = AppMode::Game;
|
||||
self.current_entrypoint.clear();
|
||||
self.logs_written_this_frame.clear();
|
||||
|
||||
self.telemetry_current = TelemetryFrame::default();
|
||||
self.telemetry_last = TelemetryFrame::default();
|
||||
self.last_crash_report = None;
|
||||
|
||||
self.paused = false;
|
||||
self.debug_step_request = false;
|
||||
self.needs_prepare_entry_call = false;
|
||||
}
|
||||
|
||||
pub fn reset(&mut self, vm: &mut VirtualMachine) {
|
||||
*vm = VirtualMachine::default();
|
||||
self.clear_cartridge_state();
|
||||
}
|
||||
|
||||
/// Loads a cartridge into the PVM and resets the execution state.
|
||||
@@ -175,7 +200,8 @@ impl VirtualMachineRuntime {
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
cartridge: &Cartridge,
|
||||
) -> Result<(), VmInitError> {
|
||||
) -> Result<(), CrashReport> {
|
||||
self.clear_cartridge_state();
|
||||
vm.set_capabilities(cartridge.capabilities);
|
||||
|
||||
match vm.initialize(cartridge.program.clone(), &cartridge.entrypoint) {
|
||||
@@ -189,15 +215,17 @@ impl VirtualMachineRuntime {
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
let report = CrashReport::VmInit { error: e };
|
||||
self.last_crash_report = Some(report.clone());
|
||||
self.log(
|
||||
LogLevel::Error,
|
||||
LogSource::Vm,
|
||||
0,
|
||||
format!("Failed to initialize VM: {:?}", e),
|
||||
report.log_tag(),
|
||||
format!("Failed to initialize VM: {}", report),
|
||||
);
|
||||
// Fail fast: no program is installed, no app id is switched.
|
||||
// We don't update current_app_id or other fields.
|
||||
Err(e)
|
||||
Err(report)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,14 +235,29 @@ impl VirtualMachineRuntime {
|
||||
&mut self,
|
||||
vm: &mut VirtualMachine,
|
||||
hw: &mut dyn HardwareBridge,
|
||||
) -> Option<String> {
|
||||
) -> Option<CrashReport> {
|
||||
let mut ctx = HostContext::new(Some(hw));
|
||||
match vm.step(self, &mut ctx) {
|
||||
Ok(_) => None,
|
||||
Err(e) => {
|
||||
let err_msg = format!("PVM Fault during Step: {:?}", e);
|
||||
self.log(LogLevel::Error, LogSource::Vm, 0, err_msg.clone());
|
||||
Some(err_msg)
|
||||
let report = match e {
|
||||
LogicalFrameEndingReason::Trap(trap) => CrashReport::VmTrap { trap },
|
||||
LogicalFrameEndingReason::Panic(message) => {
|
||||
CrashReport::VmPanic { message, pc: Some(vm.pc() as u32) }
|
||||
}
|
||||
other => CrashReport::VmPanic {
|
||||
message: format!("Unexpected fault during step: {:?}", other),
|
||||
pc: Some(vm.pc() as u32),
|
||||
},
|
||||
};
|
||||
self.log(
|
||||
LogLevel::Error,
|
||||
LogSource::Vm,
|
||||
report.log_tag(),
|
||||
format!("PVM Fault during Step: {}", report),
|
||||
);
|
||||
self.last_crash_report = Some(report.clone());
|
||||
Some(report)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,7 +272,7 @@ impl VirtualMachineRuntime {
|
||||
vm: &mut VirtualMachine,
|
||||
signals: &InputSignals,
|
||||
hw: &mut dyn HardwareBridge,
|
||||
) -> Option<String> {
|
||||
) -> Option<CrashReport> {
|
||||
let start = Instant::now();
|
||||
self.tick_index += 1;
|
||||
|
||||
@@ -303,9 +346,28 @@ impl VirtualMachineRuntime {
|
||||
|
||||
// Handle Panics
|
||||
if let LogicalFrameEndingReason::Panic(err) = run.reason {
|
||||
let err_msg = format!("PVM Fault: \"{}\"", err);
|
||||
self.log(LogLevel::Error, LogSource::Vm, 0, err_msg.clone());
|
||||
return Some(err_msg);
|
||||
let report =
|
||||
CrashReport::VmPanic { message: err, pc: Some(vm.pc() as u32) };
|
||||
self.log(
|
||||
LogLevel::Error,
|
||||
LogSource::Vm,
|
||||
report.log_tag(),
|
||||
report.summary(),
|
||||
);
|
||||
self.last_crash_report = Some(report.clone());
|
||||
return Some(report);
|
||||
}
|
||||
|
||||
if let LogicalFrameEndingReason::Trap(trap) = &run.reason {
|
||||
let report = CrashReport::VmTrap { trap: trap.clone() };
|
||||
self.log(
|
||||
LogLevel::Error,
|
||||
LogSource::Vm,
|
||||
report.log_tag(),
|
||||
report.summary(),
|
||||
);
|
||||
self.last_crash_report = Some(report.clone());
|
||||
return Some(report);
|
||||
}
|
||||
|
||||
// 4. Frame Finalization (FRAME_SYNC reached or Entrypoint returned)
|
||||
@@ -351,9 +413,10 @@ impl VirtualMachineRuntime {
|
||||
}
|
||||
Err(e) => {
|
||||
// Fatal VM fault (division by zero, invalid memory access, etc).
|
||||
let err_msg = format!("PVM Fault: {:?}", e);
|
||||
self.log(LogLevel::Error, LogSource::Vm, 0, err_msg.clone());
|
||||
return Some(err_msg);
|
||||
let report = CrashReport::VmPanic { message: e, pc: Some(vm.pc() as u32) };
|
||||
self.log(LogLevel::Error, LogSource::Vm, report.log_tag(), report.summary());
|
||||
self.last_crash_report = Some(report.clone());
|
||||
return Some(report);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1129,8 +1192,12 @@ mod tests {
|
||||
use super::*;
|
||||
use prometeu_bytecode::assembler::assemble;
|
||||
use prometeu_bytecode::model::{BytecodeModule, FunctionMeta, SyscallDecl};
|
||||
use prometeu_bytecode::TRAP_TYPE;
|
||||
use prometeu_drivers::hardware::Hardware;
|
||||
use prometeu_hal::cartridge::Cartridge;
|
||||
use prometeu_hal::syscalls::caps;
|
||||
use prometeu_hal::InputSignals;
|
||||
use prometeu_vm::VmInitError;
|
||||
|
||||
fn cartridge_with_program(program: Vec<u8>, capabilities: u64) -> Cartridge {
|
||||
Cartridge {
|
||||
@@ -1183,7 +1250,10 @@ mod tests {
|
||||
|
||||
let res = runtime.initialize_vm(&mut vm, &cartridge);
|
||||
|
||||
assert!(res.is_err());
|
||||
assert!(matches!(
|
||||
res,
|
||||
Err(CrashReport::VmInit { error: VmInitError::LoaderPatchFailed(_) })
|
||||
));
|
||||
assert_eq!(runtime.current_app_id, 0);
|
||||
assert_eq!(vm.pc(), 0);
|
||||
assert_eq!(vm.operand_stack_top(1), Vec::<Value>::new());
|
||||
@@ -1212,4 +1282,196 @@ mod tests {
|
||||
assert_eq!(runtime.current_app_id, 42);
|
||||
assert!(!vm.is_halted());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_returns_error_when_vm_ends_slice_with_trap() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_BOOL 1\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module(
|
||||
code,
|
||||
vec![SyscallDecl {
|
||||
module: "gfx".into(),
|
||||
name: "clear".into(),
|
||||
version: 1,
|
||||
arg_slots: 1,
|
||||
ret_slots: 0,
|
||||
}],
|
||||
);
|
||||
let cartridge = cartridge_with_program(program, caps::GFX);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
|
||||
let report = runtime
|
||||
.tick(&mut vm, &signals, &mut hardware)
|
||||
.expect("trap must surface as runtime error");
|
||||
|
||||
match report {
|
||||
CrashReport::VmTrap { trap } => {
|
||||
assert_eq!(trap.code, TRAP_TYPE);
|
||||
assert!(trap.message.contains("Expected integer at index 0"));
|
||||
}
|
||||
other => panic!("expected VmTrap crash report, got {:?}", other),
|
||||
}
|
||||
assert!(matches!(runtime.last_crash_report, Some(CrashReport::VmTrap { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_returns_panic_report_distinct_from_trap() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::new(assemble("HOSTCALL 0\nHALT").expect("assemble"), vec![]);
|
||||
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
|
||||
let report = runtime
|
||||
.tick(&mut vm, &signals, &mut hardware)
|
||||
.expect("panic must surface as runtime error");
|
||||
|
||||
match report {
|
||||
CrashReport::VmPanic { message, pc } => {
|
||||
assert!(message.contains("HOSTCALL 0 reached execution without loader patching"));
|
||||
assert!(pc.is_some());
|
||||
}
|
||||
other => panic!("expected VmPanic crash report, got {:?}", other),
|
||||
}
|
||||
assert!(matches!(runtime.last_crash_report, Some(CrashReport::VmPanic { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialize_vm_success_clears_previous_crash_report() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
runtime.last_crash_report =
|
||||
Some(CrashReport::VmPanic { message: "stale".into(), pc: Some(1) });
|
||||
|
||||
let mut vm = VirtualMachine::default();
|
||||
let code = assemble("PUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module(
|
||||
code,
|
||||
vec![SyscallDecl {
|
||||
module: "gfx".into(),
|
||||
name: "clear".into(),
|
||||
version: 1,
|
||||
arg_slots: 1,
|
||||
ret_slots: 0,
|
||||
}],
|
||||
);
|
||||
let cartridge = cartridge_with_program(program, caps::GFX);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
assert!(runtime.last_crash_report.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_clears_cartridge_scoped_runtime_state() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
|
||||
runtime.tick_index = 77;
|
||||
runtime.logical_frame_index = 12;
|
||||
runtime.logical_frame_active = true;
|
||||
runtime.logical_frame_remaining_cycles = 345;
|
||||
runtime.last_frame_cpu_time_us = 678;
|
||||
runtime.fs_state = FsState::Mounted;
|
||||
runtime.open_files.insert(7, "/save.dat".into());
|
||||
runtime.next_handle = 9;
|
||||
runtime.current_app_id = 42;
|
||||
runtime.current_cartridge_title = "Cart".into();
|
||||
runtime.current_cartridge_app_version = "1.2.3".into();
|
||||
runtime.current_cartridge_app_mode = AppMode::System;
|
||||
runtime.current_entrypoint = "main".into();
|
||||
runtime.logs_written_this_frame.insert(42, 3);
|
||||
runtime.telemetry_current.frame_index = 8;
|
||||
runtime.telemetry_current.cycles_used = 99;
|
||||
runtime.telemetry_last.frame_index = 7;
|
||||
runtime.telemetry_last.completed_logical_frames = 2;
|
||||
runtime.last_crash_report =
|
||||
Some(CrashReport::VmPanic { message: "stale".into(), pc: Some(55) });
|
||||
runtime.paused = true;
|
||||
runtime.debug_step_request = true;
|
||||
runtime.needs_prepare_entry_call = true;
|
||||
|
||||
runtime.reset(&mut vm);
|
||||
|
||||
assert_eq!(runtime.tick_index, 77);
|
||||
assert_eq!(runtime.fs_state, FsState::Mounted);
|
||||
assert_eq!(runtime.logical_frame_index, 0);
|
||||
assert!(!runtime.logical_frame_active);
|
||||
assert_eq!(runtime.logical_frame_remaining_cycles, 0);
|
||||
assert_eq!(runtime.last_frame_cpu_time_us, 0);
|
||||
assert!(runtime.open_files.is_empty());
|
||||
assert_eq!(runtime.next_handle, 1);
|
||||
assert_eq!(runtime.current_app_id, 0);
|
||||
assert!(runtime.current_cartridge_title.is_empty());
|
||||
assert!(runtime.current_cartridge_app_version.is_empty());
|
||||
assert_eq!(runtime.current_cartridge_app_mode, AppMode::Game);
|
||||
assert!(runtime.current_entrypoint.is_empty());
|
||||
assert!(runtime.logs_written_this_frame.is_empty());
|
||||
assert_eq!(runtime.telemetry_current.frame_index, 0);
|
||||
assert_eq!(runtime.telemetry_current.cycles_used, 0);
|
||||
assert_eq!(runtime.telemetry_last.frame_index, 0);
|
||||
assert_eq!(runtime.telemetry_last.completed_logical_frames, 0);
|
||||
assert!(runtime.last_crash_report.is_none());
|
||||
assert!(!runtime.paused);
|
||||
assert!(!runtime.debug_step_request);
|
||||
assert!(!runtime.needs_prepare_entry_call);
|
||||
assert_eq!(vm.pc(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialize_vm_failure_clears_previous_identity_and_handles() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
|
||||
let good_program = serialized_single_function_module(
|
||||
assemble("PUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble"),
|
||||
vec![SyscallDecl {
|
||||
module: "gfx".into(),
|
||||
name: "clear".into(),
|
||||
version: 1,
|
||||
arg_slots: 1,
|
||||
ret_slots: 0,
|
||||
}],
|
||||
);
|
||||
let good_cartridge = cartridge_with_program(good_program, caps::GFX);
|
||||
runtime.initialize_vm(&mut vm, &good_cartridge).expect("runtime must initialize");
|
||||
|
||||
runtime.open_files.insert(5, "/save.dat".into());
|
||||
runtime.next_handle = 6;
|
||||
runtime.paused = true;
|
||||
runtime.debug_step_request = true;
|
||||
runtime.telemetry_current.cycles_used = 123;
|
||||
|
||||
let bad_program = serialized_single_function_module(
|
||||
assemble("PUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble"),
|
||||
vec![SyscallDecl {
|
||||
module: "gfx".into(),
|
||||
name: "clear".into(),
|
||||
version: 1,
|
||||
arg_slots: 1,
|
||||
ret_slots: 0,
|
||||
}],
|
||||
);
|
||||
let bad_cartridge = cartridge_with_program(bad_program, caps::NONE);
|
||||
|
||||
let res = runtime.initialize_vm(&mut vm, &bad_cartridge);
|
||||
|
||||
assert!(matches!(
|
||||
res,
|
||||
Err(CrashReport::VmInit { error: VmInitError::LoaderPatchFailed(_) })
|
||||
));
|
||||
assert_eq!(runtime.current_app_id, 0);
|
||||
assert!(runtime.current_cartridge_title.is_empty());
|
||||
assert!(runtime.current_cartridge_app_version.is_empty());
|
||||
assert_eq!(runtime.current_cartridge_app_mode, AppMode::Game);
|
||||
assert!(runtime.current_entrypoint.is_empty());
|
||||
assert!(runtime.open_files.is_empty());
|
||||
assert_eq!(runtime.next_handle, 1);
|
||||
assert!(!runtime.paused);
|
||||
assert!(!runtime.debug_step_request);
|
||||
assert_eq!(runtime.telemetry_current.cycles_used, 0);
|
||||
assert!(matches!(runtime.last_crash_report, Some(CrashReport::VmInit { .. })));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user