implements PLN-0049 system profile abi gates
This commit is contained in:
@@ -84,6 +84,42 @@ impl VirtualMachineRuntime {
|
||||
fn int_arg_to_u16_status(value: i64) -> Result<u16, ComposerOpStatus> {
|
||||
u16::try_from(value).map_err(|_| ComposerOpStatus::ArgRangeInvalid)
|
||||
}
|
||||
|
||||
fn is_game_profile_syscall(syscall: Syscall) -> bool {
|
||||
matches!(
|
||||
syscall,
|
||||
Syscall::GfxClear
|
||||
| Syscall::GfxFillRect
|
||||
| Syscall::GfxDrawLine
|
||||
| Syscall::GfxDrawCircle
|
||||
| Syscall::GfxDrawDisc
|
||||
| Syscall::GfxDrawSquare
|
||||
| Syscall::GfxDrawText
|
||||
| Syscall::GfxClear565
|
||||
| Syscall::ComposerBindScene
|
||||
| Syscall::ComposerUnbindScene
|
||||
| Syscall::ComposerSetCamera
|
||||
| Syscall::ComposerEmitSprite
|
||||
| Syscall::AssetLoad
|
||||
| Syscall::AssetStatus
|
||||
| Syscall::AssetCommit
|
||||
| Syscall::AssetCancel
|
||||
| Syscall::BankInfo
|
||||
)
|
||||
}
|
||||
|
||||
fn ensure_game_profile_syscall(&self, syscall: Syscall) -> Result<(), VmFault> {
|
||||
if Self::is_game_profile_syscall(syscall)
|
||||
&& self.current_cartridge_app_mode != AppMode::Game
|
||||
{
|
||||
return Err(VmFault::Trap(
|
||||
TRAP_INVALID_SYSCALL,
|
||||
format!("{} is only available to the Game profile", syscall.name()),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NativeInterface for VirtualMachineRuntime {
|
||||
@@ -108,6 +144,8 @@ impl NativeInterface for VirtualMachineRuntime {
|
||||
_ => {}
|
||||
}
|
||||
|
||||
self.ensure_game_profile_syscall(syscall)?;
|
||||
|
||||
let hw = ctx.require_hw()?;
|
||||
|
||||
match syscall {
|
||||
|
||||
@@ -64,12 +64,16 @@ impl FsBackend for MemFsBackend {
|
||||
}
|
||||
}
|
||||
|
||||
fn cartridge_with_program(program: Vec<u8>, capabilities: u64) -> Cartridge {
|
||||
fn cartridge_with_program_and_mode(
|
||||
program: Vec<u8>,
|
||||
capabilities: u64,
|
||||
app_mode: AppMode,
|
||||
) -> Cartridge {
|
||||
Cartridge {
|
||||
app_id: 42,
|
||||
title: "Test Cart".into(),
|
||||
app_version: "1.0.0".into(),
|
||||
app_mode: AppMode::Game,
|
||||
app_mode,
|
||||
capabilities,
|
||||
program,
|
||||
assets: AssetsPayloadSource::empty(),
|
||||
@@ -78,6 +82,10 @@ fn cartridge_with_program(program: Vec<u8>, capabilities: u64) -> Cartridge {
|
||||
}
|
||||
}
|
||||
|
||||
fn cartridge_with_program(program: Vec<u8>, capabilities: u64) -> Cartridge {
|
||||
cartridge_with_program_and_mode(program, capabilities, AppMode::Game)
|
||||
}
|
||||
|
||||
fn serialized_single_function_module(code: Vec<u8>, syscalls: Vec<SyscallDecl>) -> Vec<u8> {
|
||||
serialized_single_function_module_with_consts(code, vec![], syscalls)
|
||||
}
|
||||
@@ -260,6 +268,130 @@ fn tick_returns_error_when_vm_ends_slice_with_trap() {
|
||||
assert!(matches!(runtime.last_crash_report, Some(CrashReport::VmTrap { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_system_profile_rejects_gfx_game_surface() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::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_and_mode(program, caps::GFX, AppMode::System);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report = runtime.tick(&mut vm, &signals, &mut hardware).expect("system gfx call must trap");
|
||||
|
||||
match report {
|
||||
CrashReport::VmTrap { trap } => {
|
||||
assert_eq!(trap.code, prometeu_bytecode::TRAP_INVALID_SYSCALL);
|
||||
assert!(trap.message.contains("GfxClear is only available to the Game profile"));
|
||||
}
|
||||
other => panic!("expected VmTrap crash report, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_system_profile_rejects_composer_game_surface() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module(
|
||||
code,
|
||||
vec![SyscallDecl {
|
||||
module: "composer".into(),
|
||||
name: "bind_scene".into(),
|
||||
version: 1,
|
||||
arg_slots: 1,
|
||||
ret_slots: 1,
|
||||
}],
|
||||
);
|
||||
let cartridge = cartridge_with_program_and_mode(program, caps::GFX, AppMode::System);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report =
|
||||
runtime.tick(&mut vm, &signals, &mut hardware).expect("system composer call must trap");
|
||||
|
||||
match report {
|
||||
CrashReport::VmTrap { trap } => {
|
||||
assert_eq!(trap.code, prometeu_bytecode::TRAP_INVALID_SYSCALL);
|
||||
assert!(
|
||||
trap.message.contains("ComposerBindScene is only available to the Game profile")
|
||||
);
|
||||
}
|
||||
other => panic!("expected VmTrap crash report, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_system_profile_rejects_bank_game_surface() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_I32 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module(
|
||||
code,
|
||||
vec![SyscallDecl {
|
||||
module: "bank".into(),
|
||||
name: "info".into(),
|
||||
version: 1,
|
||||
arg_slots: 1,
|
||||
ret_slots: 2,
|
||||
}],
|
||||
);
|
||||
let cartridge = cartridge_with_program_and_mode(program, caps::BANK, AppMode::System);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report =
|
||||
runtime.tick(&mut vm, &signals, &mut hardware).expect("system bank call must trap");
|
||||
|
||||
match report {
|
||||
CrashReport::VmTrap { trap } => {
|
||||
assert_eq!(trap.code, prometeu_bytecode::TRAP_INVALID_SYSCALL);
|
||||
assert!(trap.message.contains("BankInfo is only available to the Game profile"));
|
||||
}
|
||||
other => panic!("expected VmTrap crash report, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_system_profile_can_use_shared_log_transport() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut hardware = Hardware::new();
|
||||
let signals = InputSignals::default();
|
||||
let code = assemble("PUSH_I32 2\nPUSH_CONST 0\nHOSTCALL 0\nHALT").expect("assemble");
|
||||
let program = serialized_single_function_module_with_consts(
|
||||
code,
|
||||
vec![ConstantPoolEntry::String("system log".into())],
|
||||
vec![SyscallDecl {
|
||||
module: "log".into(),
|
||||
name: "write".into(),
|
||||
version: 1,
|
||||
arg_slots: 2,
|
||||
ret_slots: 0,
|
||||
}],
|
||||
);
|
||||
let cartridge = cartridge_with_program_and_mode(program, caps::LOG, AppMode::System);
|
||||
|
||||
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
|
||||
let report = runtime.tick(&mut vm, &signals, &mut hardware);
|
||||
|
||||
assert!(report.is_none(), "system log transport must remain internally shared");
|
||||
assert!(vm.is_halted());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_returns_panic_report_distinct_from_trap() {
|
||||
let mut runtime = VirtualMachineRuntime::new(None);
|
||||
|
||||
Reference in New Issue
Block a user