feat(vm): add deterministic gameplay math and domain coverage gates
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
This commit is contained in:
@@ -32,6 +32,12 @@ pub const TRAP_INVALID_HANDLE: u32 = 0x0000_000F;
|
||||
pub const TRAP_RESOURCE_LIMIT: u32 = 0x0000_0010;
|
||||
/// Known input query unavailable on this platform.
|
||||
pub const TRAP_UNAVAILABLE: u32 = 0x0000_0011;
|
||||
/// Mathematical domain is invalid for the supplied finite arguments.
|
||||
pub const TRAP_NUMERIC_DOMAIN: u32 = 0x0000_0012;
|
||||
/// Finite input produces an unrepresentable result or integer destination.
|
||||
pub const TRAP_NUMERIC_RANGE: u32 = 0x0000_0013;
|
||||
/// A NaN or infinite input cannot enter guest numeric computation.
|
||||
pub const TRAP_NON_FINITE: u32 = 0x0000_0014;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
@@ -13,8 +13,9 @@ mod value;
|
||||
pub use abi::{
|
||||
SourceSpan, TrapInfo, TRAP_BAD_RET_SLOTS, TRAP_DIV_ZERO, TRAP_EXPLICIT,
|
||||
TRAP_ILLEGAL_INSTRUCTION, TRAP_INVALID_FUNC, TRAP_INVALID_HANDLE, TRAP_INVALID_INTRINSIC,
|
||||
TRAP_INVALID_LOCAL, TRAP_INVALID_SYSCALL, TRAP_NULL_REFERENCE, TRAP_OOB, TRAP_RESOURCE_LIMIT,
|
||||
TRAP_STACK_UNDERFLOW, TRAP_TYPE, TRAP_UNAVAILABLE,
|
||||
TRAP_INVALID_LOCAL, TRAP_INVALID_SYSCALL, TRAP_NON_FINITE, TRAP_NULL_REFERENCE,
|
||||
TRAP_NUMERIC_DOMAIN, TRAP_NUMERIC_RANGE, TRAP_OOB, TRAP_RESOURCE_LIMIT, TRAP_STACK_UNDERFLOW,
|
||||
TRAP_TYPE, TRAP_UNAVAILABLE,
|
||||
};
|
||||
pub use assembler::{assemble, AsmError};
|
||||
pub use decoder::{decode_next, DecodeError, DecodedInstr};
|
||||
|
||||
@@ -74,6 +74,7 @@ pub enum LoadReason {
|
||||
Limit,
|
||||
Allocation,
|
||||
ConstantTag,
|
||||
NonFiniteValue,
|
||||
DebugRange,
|
||||
DuplicateSyscall,
|
||||
UnknownOpcode,
|
||||
|
||||
@@ -229,9 +229,17 @@ impl<'a> Reader<'a, '_> {
|
||||
(|| {
|
||||
match self.byte()? {
|
||||
0 => {}
|
||||
1 | 2 => {
|
||||
1 => {
|
||||
self.take(8)?;
|
||||
}
|
||||
2 => {
|
||||
let at = self.pos;
|
||||
let bits = u64::from_le_bytes(self.take(8)?.try_into().unwrap());
|
||||
if bits & 0x7ff0_0000_0000_0000 == 0x7ff0_0000_0000_0000 {
|
||||
return Err(E::new(C::InvalidFormat, R::NonFiniteValue)
|
||||
.at(self.section.kind, self.section.offset + at));
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
if self.byte()? > 1 {
|
||||
return Err(self.error(C::InvalidFormat, R::InvalidFlag));
|
||||
|
||||
@@ -8,6 +8,41 @@ fn budget() -> LoadBudget {
|
||||
LoadBudget::new(LoadLimits::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonfinite_constant_payloads_are_rejected_with_index_and_exact_offset() {
|
||||
use prometeu_bytecode::model::ConstantPoolEntry as K;
|
||||
let registry = minimal_registry();
|
||||
let constants = [K::Float64(1.0)];
|
||||
let module = PbxModule {
|
||||
registry: ®istry,
|
||||
constants: &constants,
|
||||
code: &[1, 0],
|
||||
debug: None,
|
||||
exports: &[],
|
||||
syscalls: &[],
|
||||
};
|
||||
let valid = module.serialize(&mut budget()).unwrap();
|
||||
let envelope = PbxEnvelope::parse(&valid, &mut budget()).unwrap();
|
||||
let offset = envelope.section(0).unwrap().offset + 5;
|
||||
for bits in [0x7ff0000000000000u64, 0xfff0000000000000, 0x7ff8000000000042, 0x7ff0000000000001]
|
||||
{
|
||||
let mut bytes = valid.clone();
|
||||
bytes[offset..offset + 8].copy_from_slice(&bits.to_le_bytes());
|
||||
let error = OwnedPbx::parse(&bytes, &mut budget()).unwrap_err();
|
||||
assert_eq!(
|
||||
(error.code, error.reason, error.section, error.offset, error.record),
|
||||
(LoadCode::InvalidFormat, LoadReason::NonFiniteValue, Some(0), Some(offset), Some(0))
|
||||
);
|
||||
}
|
||||
for bits in [0u64, 1, 0x000fffffffffffff, 0x7fefffffffffffff, 0x8000000000000000] {
|
||||
let mut bytes = valid.clone();
|
||||
bytes[offset..offset + 8].copy_from_slice(&bits.to_le_bytes());
|
||||
let owned = OwnedPbx::parse(&bytes, &mut budget()).unwrap();
|
||||
let K::Float64(x) = owned.constants[0] else { panic!() };
|
||||
assert_eq!(x.to_bits(), bits);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fn_keys_ignore_names_but_preserve_logical_parameter_and_result_order() {
|
||||
let signature = Signature { parameters: vec![named(0)], results: vec![TypeId(1)] };
|
||||
@@ -222,7 +257,7 @@ fn owned_payloads_preserve_utf8_float_bits_names_and_binding_declarations() {
|
||||
K::Null,
|
||||
K::Int32(i32::MIN),
|
||||
K::Int64(i64::MAX),
|
||||
K::Float64(f64::from_bits(0x7ff8000000000042)),
|
||||
K::Float64(f64::from_bits(0x0008000000000042)),
|
||||
K::Boolean(true),
|
||||
K::String("é猫".into()),
|
||||
K::String(String::new()),
|
||||
@@ -253,7 +288,7 @@ fn owned_payloads_preserve_utf8_float_bits_names_and_binding_declarations() {
|
||||
let module = OwnedPbx::parse(&bytes, &mut b).unwrap();
|
||||
assert_eq!(&module.constants[..3], &constants[..3]);
|
||||
let K::Float64(value) = module.constants[3] else { panic!() };
|
||||
assert_eq!(value.to_bits(), 0x7ff8000000000042);
|
||||
assert_eq!(value.to_bits(), 0x0008000000000042);
|
||||
assert_eq!(&module.constants[4..], &constants[4..]);
|
||||
assert_eq!(module.debug.as_ref(), Some(&debug));
|
||||
assert_eq!(module.exports, exports);
|
||||
@@ -319,7 +354,7 @@ fn complete_module_writer_preserves_non_registry_encodings() {
|
||||
let constants = [
|
||||
K::Null,
|
||||
K::Int64(-1),
|
||||
K::Float64(f64::from_bits(0x7ff8000000001234)),
|
||||
K::Float64(f64::from_bits(0x0008000000001234)),
|
||||
K::Boolean(true),
|
||||
K::String("é".into()),
|
||||
K::Int32(-2),
|
||||
@@ -351,7 +386,7 @@ fn complete_module_writer_preserves_non_registry_encodings() {
|
||||
parsed.section(0).unwrap().bytes,
|
||||
hex("07 00 00 00 00
|
||||
01 ff ff ff ff ff ff ff ff
|
||||
02 34 12 00 00 00 00 f8 7f
|
||||
02 34 12 00 00 00 00 08 00
|
||||
03 01 04 02 00 00 00 c3 a9 05 fe ff ff ff 04 00 00 00 00")
|
||||
);
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user