feat(registry): checkpoint PLN-0176 foundations and validated partial integrations
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::{
|
||||
Value,
|
||||
registry::{BuiltinId, StorageLayout},
|
||||
};
|
||||
use prometeu_hal::{HostContext, color::Color};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@@ -53,14 +56,18 @@ impl IntrinsicKey {
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum BuiltinScalarType {
|
||||
Int,
|
||||
Int32,
|
||||
Int64,
|
||||
Float,
|
||||
Bool,
|
||||
Str,
|
||||
String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum AbiType {
|
||||
/// Internal group components; neither can establish a valid Dynamic alone.
|
||||
DynamicTag,
|
||||
DynamicPayload,
|
||||
Scalar(BuiltinScalarType),
|
||||
Builtin(BuiltinTypeKey),
|
||||
}
|
||||
@@ -75,6 +82,7 @@ pub enum BuiltinLayoutType {
|
||||
pub enum BuiltinTypeShape {
|
||||
Scalar,
|
||||
Aggregate,
|
||||
Dynamic,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -111,14 +119,33 @@ impl BuiltinTypeMeta {
|
||||
out
|
||||
}
|
||||
|
||||
pub fn storage_layout(
|
||||
&self,
|
||||
) -> Result<StorageLayout, prometeu_bytecode::load_limits::LoadFailure> {
|
||||
BuiltinId(self.id).layout()
|
||||
}
|
||||
|
||||
pub fn validate_flat_values(&self, values: &[Value]) -> Result<(), BuiltinValueError> {
|
||||
if self.shape == BuiltinTypeShape::Dynamic {
|
||||
if values.len() != 2 {
|
||||
return Err(BuiltinValueError::ArityMismatch { expected: 2, got: values.len() });
|
||||
}
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
// A non-null tag is a program TypeId, and its payload may be a box.
|
||||
// This context-free catalogue must never claim to validate it.
|
||||
return Err(BuiltinValueError::RegistryRequired);
|
||||
}
|
||||
let layout = self.flatten_layout();
|
||||
validate_values_against_layout(values, &layout)
|
||||
}
|
||||
|
||||
fn flatten_into(&self, out: &mut Vec<AbiType>) {
|
||||
match self.shape {
|
||||
BuiltinTypeShape::Scalar => out.extend_from_slice(self.flat_slot_layout),
|
||||
BuiltinTypeShape::Scalar | BuiltinTypeShape::Dynamic => {
|
||||
out.extend_from_slice(self.flat_slot_layout)
|
||||
}
|
||||
BuiltinTypeShape::Aggregate => {
|
||||
for field in self.fields {
|
||||
flatten_layout_type(field.field_type, out);
|
||||
@@ -134,7 +161,7 @@ pub enum BuiltinConstSlotValue {
|
||||
Int64(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: i32 },
|
||||
NominalBuiltin { builtin: BuiltinTypeKey, carrier: u32 },
|
||||
}
|
||||
|
||||
pub type BuiltinConstHook = fn() -> Vec<Value>;
|
||||
@@ -178,12 +205,12 @@ impl BuiltinConstMeta {
|
||||
pub enum IntrinsicExecutionError {
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
InvalidBuiltinCarrier { owner: &'static str, name: &'static str, carrier: i64 },
|
||||
HardwareUnavailable,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum BuiltinValueError {
|
||||
RegistryRequired,
|
||||
ArityMismatch { expected: usize, got: usize },
|
||||
TypeMismatch { index: usize, expected: AbiType },
|
||||
}
|
||||
@@ -218,14 +245,28 @@ impl IntrinsicMeta {
|
||||
}
|
||||
|
||||
pub fn validate_result_values(&self, values: &[Value]) -> Result<(), IntrinsicExecutionError> {
|
||||
if values.len() != self.ret_slots() {
|
||||
Self::validate_values(values, self.ret_layout)
|
||||
}
|
||||
|
||||
pub fn validate_argument_values(
|
||||
&self,
|
||||
values: &[Value],
|
||||
) -> Result<(), IntrinsicExecutionError> {
|
||||
Self::validate_values(values, self.arg_layout)
|
||||
}
|
||||
|
||||
fn validate_values(
|
||||
values: &[Value],
|
||||
layout: &[AbiType],
|
||||
) -> Result<(), IntrinsicExecutionError> {
|
||||
if values.len() != layout.len() {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch {
|
||||
expected: self.ret_slots(),
|
||||
expected: layout.len(),
|
||||
got: values.len(),
|
||||
});
|
||||
}
|
||||
|
||||
for (index, (value, expected)) in values.iter().zip(self.ret_layout.iter()).enumerate() {
|
||||
for (index, (value, expected)) in values.iter().zip(layout.iter()).enumerate() {
|
||||
if !value_matches_abi_type(value, *expected) {
|
||||
return Err(IntrinsicExecutionError::TypeMismatch { index, expected: *expected });
|
||||
}
|
||||
@@ -242,7 +283,9 @@ impl BuiltinConstSlotValue {
|
||||
BuiltinConstSlotValue::Int64(value) => Value::Int64(value),
|
||||
BuiltinConstSlotValue::Float(value) => Value::Float(value),
|
||||
BuiltinConstSlotValue::Bool(value) => Value::Boolean(value),
|
||||
BuiltinConstSlotValue::NominalBuiltin { carrier, .. } => Value::Int32(carrier),
|
||||
BuiltinConstSlotValue::NominalBuiltin { carrier, .. } => {
|
||||
Value::Int64(i64::from(carrier))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -250,21 +293,15 @@ impl BuiltinConstSlotValue {
|
||||
const COLOR: BuiltinTypeKey = BuiltinTypeKey::new("color", 1);
|
||||
const VEC2: BuiltinTypeKey = BuiltinTypeKey::new("vec2", 1);
|
||||
const PIXEL: BuiltinTypeKey = BuiltinTypeKey::new("pixel", 1);
|
||||
const INPUT_PAD: BuiltinTypeKey = BuiltinTypeKey::new("input.pad", 1);
|
||||
const INPUT_TOUCH: BuiltinTypeKey = BuiltinTypeKey::new("input.touch", 1);
|
||||
const INPUT_BUTTON: BuiltinTypeKey = BuiltinTypeKey::new("input.button", 1);
|
||||
|
||||
const COLOR_LAYOUT: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const VEC2_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const PIXEL_LAYOUT: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Builtin(COLOR),
|
||||
];
|
||||
const INPUT_PAD_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const INPUT_TOUCH_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const INPUT_BUTTON_LAYOUT: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
|
||||
const VEC2_FIELDS: [BuiltinFieldMeta; 2] = [
|
||||
BuiltinFieldMeta {
|
||||
@@ -285,13 +322,13 @@ const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
BuiltinFieldMeta {
|
||||
name: "x",
|
||||
start_slot: 0,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int64),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
name: "y",
|
||||
start_slot: 1,
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int),
|
||||
field_type: BuiltinLayoutType::Scalar(BuiltinScalarType::Int64),
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinFieldMeta {
|
||||
@@ -302,7 +339,7 @@ const PIXEL_FIELDS: [BuiltinFieldMeta; 3] = [
|
||||
},
|
||||
];
|
||||
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 6] = [
|
||||
const BUILTIN_TYPES: [BuiltinTypeMeta; 9] = [
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0001,
|
||||
name: COLOR.name,
|
||||
@@ -330,35 +367,34 @@ const BUILTIN_TYPES: [BuiltinTypeMeta; 6] = [
|
||||
flat_slot_layout: &PIXEL_LAYOUT,
|
||||
flat_slot_width: 3,
|
||||
},
|
||||
scalar_meta(0x10, "Int32", &[AbiType::Scalar(BuiltinScalarType::Int32)]),
|
||||
scalar_meta(0x11, "Int64", &[AbiType::Scalar(BuiltinScalarType::Int64)]),
|
||||
scalar_meta(0x12, "Float", &[AbiType::Scalar(BuiltinScalarType::Float)]),
|
||||
scalar_meta(0x13, "Bool", &[AbiType::Scalar(BuiltinScalarType::Bool)]),
|
||||
scalar_meta(0x14, "String", &[AbiType::Scalar(BuiltinScalarType::String)]),
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0100,
|
||||
name: INPUT_PAD.name,
|
||||
version: INPUT_PAD.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
id: 0x15,
|
||||
name: "Dynamic",
|
||||
version: 1,
|
||||
shape: BuiltinTypeShape::Dynamic,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_PAD_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0101,
|
||||
name: INPUT_TOUCH.name,
|
||||
version: INPUT_TOUCH.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_TOUCH_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
},
|
||||
BuiltinTypeMeta {
|
||||
id: 0x0102,
|
||||
name: INPUT_BUTTON.name,
|
||||
version: INPUT_BUTTON.version,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: &INPUT_BUTTON_LAYOUT,
|
||||
flat_slot_width: 1,
|
||||
flat_slot_layout: &[AbiType::DynamicTag, AbiType::DynamicPayload],
|
||||
flat_slot_width: 2,
|
||||
},
|
||||
];
|
||||
|
||||
const fn scalar_meta(id: u32, name: &'static str, layout: &'static [AbiType]) -> BuiltinTypeMeta {
|
||||
BuiltinTypeMeta {
|
||||
id,
|
||||
name,
|
||||
version: 1,
|
||||
shape: BuiltinTypeShape::Scalar,
|
||||
fields: &[],
|
||||
flat_slot_layout: layout,
|
||||
flat_slot_width: 1,
|
||||
}
|
||||
}
|
||||
|
||||
const VEC2_ZERO_LAYOUT: [AbiType; 2] =
|
||||
[AbiType::Scalar(BuiltinScalarType::Float), AbiType::Scalar(BuiltinScalarType::Float)];
|
||||
const VEC2_ZERO_SLOTS: [BuiltinConstSlotValue; 2] =
|
||||
@@ -434,27 +470,20 @@ const NO_ARGS: [AbiType; 0] = [];
|
||||
const COLOR_ARGS: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const COLOR_RET: [AbiType; 1] = [AbiType::Builtin(COLOR)];
|
||||
const COLOR_RGB_ARGS: [AbiType; 3] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
];
|
||||
const COLOR_RGBA_ARGS: [AbiType; 4] = [
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
];
|
||||
const COLOR_HTML_RGBA_ARGS: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Str)];
|
||||
const PAD_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const TOUCH_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const BUTTON_RET: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
const PAD_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_PAD)];
|
||||
const TOUCH_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_TOUCH)];
|
||||
const BUTTON_ARGS: [AbiType; 1] = [AbiType::Builtin(INPUT_BUTTON)];
|
||||
const BOOL_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Bool)];
|
||||
const INT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Int)];
|
||||
const COLOR_HTML_RGBA_ARGS: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::String)];
|
||||
const INT_RET: [AbiType; 1] = [AbiType::Scalar(BuiltinScalarType::Int64)];
|
||||
|
||||
const INTRINSICS: [IntrinsicMeta; 42] = [
|
||||
const INTRINSICS: [IntrinsicMeta; 21] = [
|
||||
IntrinsicMeta {
|
||||
id: 0x1000,
|
||||
owner: "vec2",
|
||||
@@ -686,237 +715,6 @@ const INTRINSICS: [IntrinsicMeta; 42] = [
|
||||
may_allocate: false,
|
||||
implementation: color_transparent,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2000,
|
||||
owner: "input",
|
||||
name: "pad",
|
||||
version: 1,
|
||||
arg_layout: &NO_ARGS,
|
||||
ret_layout: &PAD_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2001,
|
||||
owner: "input",
|
||||
name: "touch",
|
||||
version: 1,
|
||||
arg_layout: &NO_ARGS,
|
||||
ret_layout: &TOUCH_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2010,
|
||||
owner: "input.pad",
|
||||
name: "up",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_up,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2011,
|
||||
owner: "input.pad",
|
||||
name: "down",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_down,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2012,
|
||||
owner: "input.pad",
|
||||
name: "left",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_left,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2013,
|
||||
owner: "input.pad",
|
||||
name: "right",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_right,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2014,
|
||||
owner: "input.pad",
|
||||
name: "a",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_a,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2015,
|
||||
owner: "input.pad",
|
||||
name: "b",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_b,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2016,
|
||||
owner: "input.pad",
|
||||
name: "x",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_x,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2017,
|
||||
owner: "input.pad",
|
||||
name: "y",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_y,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2018,
|
||||
owner: "input.pad",
|
||||
name: "l",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_l,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2019,
|
||||
owner: "input.pad",
|
||||
name: "r",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_r,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x201A,
|
||||
owner: "input.pad",
|
||||
name: "start",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_start,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x201B,
|
||||
owner: "input.pad",
|
||||
name: "select",
|
||||
version: 1,
|
||||
arg_layout: &PAD_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_pad_select,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2020,
|
||||
owner: "input.touch",
|
||||
name: "button",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &BUTTON_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_button,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2021,
|
||||
owner: "input.touch",
|
||||
name: "x",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_x,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2022,
|
||||
owner: "input.touch",
|
||||
name: "y",
|
||||
version: 1,
|
||||
arg_layout: &TOUCH_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_touch_y,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2030,
|
||||
owner: "input.button",
|
||||
name: "pressed",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_pressed,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2031,
|
||||
owner: "input.button",
|
||||
name: "released",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_released,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2032,
|
||||
owner: "input.button",
|
||||
name: "down",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &BOOL_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_down,
|
||||
},
|
||||
IntrinsicMeta {
|
||||
id: 0x2033,
|
||||
owner: "input.button",
|
||||
name: "hold",
|
||||
version: 1,
|
||||
arg_layout: &BUTTON_ARGS,
|
||||
ret_layout: &INT_RET,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation: input_button_hold,
|
||||
},
|
||||
];
|
||||
|
||||
pub fn lookup_builtin_type(name: &str, version: u16) -> Option<&'static BuiltinTypeMeta> {
|
||||
@@ -944,11 +742,15 @@ pub fn materialize_builtin_constant(
|
||||
pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
|
||||
}
|
||||
|
||||
pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
|
||||
INTRINSICS.iter().find(|meta| meta.id == id)
|
||||
INTRINSICS
|
||||
.iter()
|
||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||
.find(|meta| meta.id == id)
|
||||
}
|
||||
|
||||
fn flatten_layout_type(layout_type: BuiltinLayoutType, out: &mut Vec<AbiType>) {
|
||||
@@ -967,20 +769,26 @@ fn expect_float_arg(args: &[Value], index: usize) -> Result<f64, IntrinsicExecut
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_float().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
})
|
||||
match value {
|
||||
Value::Float(value) => Ok(*value),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Float),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_int_arg(args: &[Value], index: usize) -> Result<i64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_integer().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
})
|
||||
match value {
|
||||
Value::Int64(value) => Ok(*value),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn expect_string_arg(args: &[Value], index: usize) -> Result<&str, IntrinsicExecutionError> {
|
||||
@@ -991,18 +799,24 @@ fn expect_string_arg(args: &[Value], index: usize) -> Result<&str, IntrinsicExec
|
||||
Value::String(value) => Ok(value.as_ref()),
|
||||
_ => Err(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Str),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::String),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn value_matches_abi_type(value: &Value, expected: AbiType) -> bool {
|
||||
match expected {
|
||||
AbiType::Scalar(BuiltinScalarType::Int) => value.as_integer().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::Float) => value.as_float().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::Int32) => matches!(value, Value::Int32(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64) => matches!(value, Value::Int64(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Float) => matches!(value, Value::Float(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Bool) => matches!(value, Value::Boolean(_)),
|
||||
AbiType::Scalar(BuiltinScalarType::Str) => matches!(value, Value::String(_)),
|
||||
AbiType::Builtin(_) => value.as_integer().is_some(),
|
||||
AbiType::Scalar(BuiltinScalarType::String) => {
|
||||
matches!(value, Value::String(_) | Value::Null)
|
||||
}
|
||||
AbiType::Builtin(key) if key == COLOR => {
|
||||
matches!(value, Value::Int64(raw) if u32::try_from(*raw).is_ok())
|
||||
}
|
||||
AbiType::Builtin(_) | AbiType::DynamicTag | AbiType::DynamicPayload => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1023,11 +837,6 @@ fn validate_values_against_layout(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const PAD_HANDLE: i64 = 1;
|
||||
const TOUCH_HANDLE: i64 = 1;
|
||||
const PAD_BUTTON_BASE: i64 = 0x100;
|
||||
const TOUCH_BUTTON_CARRIER: i64 = 0x200;
|
||||
|
||||
fn vec2_dot(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
@@ -1069,7 +878,7 @@ fn expect_channel(args: &[Value], index: usize) -> Result<u8, IntrinsicExecution
|
||||
let value = expect_int_arg(args, index)?;
|
||||
u8::try_from(value).map_err(|_| IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1088,7 +897,7 @@ fn color_from_raw(
|
||||
let raw = u32::try_from(expect_int_arg(args, 0)?).map_err(|_| {
|
||||
IntrinsicExecutionError::TypeMismatch {
|
||||
index: 0,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
}
|
||||
})?;
|
||||
Ok(color_value(Color::from_raw(raw)))
|
||||
@@ -1124,7 +933,7 @@ fn color_html_rgba(
|
||||
let color = parse_html_rgba(expect_string_arg(args, 0)?).ok_or({
|
||||
IntrinsicExecutionError::TypeMismatch {
|
||||
index: 0,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Str),
|
||||
expected: AbiType::Scalar(BuiltinScalarType::String),
|
||||
}
|
||||
})?;
|
||||
Ok(color_value(color))
|
||||
@@ -1227,254 +1036,6 @@ fn color_transparent(
|
||||
color_const(Color::TRANSPARENT)
|
||||
}
|
||||
|
||||
fn input_pad(
|
||||
_args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(PAD_HANDLE)])
|
||||
}
|
||||
|
||||
fn input_touch(
|
||||
_args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(TOUCH_HANDLE)])
|
||||
}
|
||||
|
||||
fn input_pad_up(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "up")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE)])
|
||||
}
|
||||
|
||||
fn input_pad_down(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "down")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 1)])
|
||||
}
|
||||
|
||||
fn input_pad_left(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "left")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 2)])
|
||||
}
|
||||
|
||||
fn input_pad_right(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "right")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 3)])
|
||||
}
|
||||
|
||||
fn input_pad_a(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "a")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 4)])
|
||||
}
|
||||
|
||||
fn input_pad_b(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "b")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 5)])
|
||||
}
|
||||
|
||||
fn input_pad_x(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "x")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 6)])
|
||||
}
|
||||
|
||||
fn input_pad_y(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "y")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 7)])
|
||||
}
|
||||
|
||||
fn input_pad_l(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "l")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 8)])
|
||||
}
|
||||
|
||||
fn input_pad_r(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "r")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 9)])
|
||||
}
|
||||
|
||||
fn input_pad_start(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "start")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 10)])
|
||||
}
|
||||
|
||||
fn input_pad_select(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_pad_handle(args, "select")?;
|
||||
Ok(vec![Value::Int64(PAD_BUTTON_BASE + 11)])
|
||||
}
|
||||
|
||||
fn input_touch_button(
|
||||
args: &[Value],
|
||||
_ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "button")?;
|
||||
Ok(vec![Value::Int64(TOUCH_BUTTON_CARRIER)])
|
||||
}
|
||||
|
||||
fn input_touch_x(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "x")?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(vec![Value::Int64(platform.input().touch().x() as i64)])
|
||||
}
|
||||
|
||||
fn input_touch_y(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
expect_touch_handle(args, "y")?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(vec![Value::Int64(platform.input().touch().y() as i64)])
|
||||
}
|
||||
|
||||
fn input_button_pressed(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "pressed", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.pressed)])
|
||||
}
|
||||
|
||||
fn input_button_released(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "released", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.released)])
|
||||
}
|
||||
|
||||
fn input_button_down(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "down", ctx)?;
|
||||
Ok(vec![Value::Boolean(button.down)])
|
||||
}
|
||||
|
||||
fn input_button_hold(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
let button = resolve_button(args, "hold", ctx)?;
|
||||
Ok(vec![Value::Int64(button.hold_frames as i64)])
|
||||
}
|
||||
|
||||
fn expect_pad_handle(args: &[Value], name: &'static str) -> Result<(), IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
if carrier != PAD_HANDLE {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.pad",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expect_touch_handle(args: &[Value], name: &'static str) -> Result<(), IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
if carrier != TOUCH_HANDLE {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.touch",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn expect_builtin_carrier(args: &[Value], index: usize) -> Result<i64, IntrinsicExecutionError> {
|
||||
let value = args
|
||||
.get(index)
|
||||
.ok_or(IntrinsicExecutionError::ArityMismatch { expected: index + 1, got: args.len() })?;
|
||||
value.as_integer().ok_or(IntrinsicExecutionError::TypeMismatch {
|
||||
index,
|
||||
expected: AbiType::Scalar(BuiltinScalarType::Int),
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_button(
|
||||
args: &[Value],
|
||||
name: &'static str,
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<prometeu_hal::button::Button, IntrinsicExecutionError> {
|
||||
let carrier = expect_builtin_carrier(args, 0)?;
|
||||
let platform =
|
||||
ctx.require_platform().map_err(|_| IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
let input = platform.input();
|
||||
if carrier == TOUCH_BUTTON_CARRIER {
|
||||
return Ok(*input.touch().f());
|
||||
}
|
||||
|
||||
let Some(index) = carrier.checked_sub(PAD_BUTTON_BASE) else {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.button",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
};
|
||||
let button = match index {
|
||||
0 => *input.pad().up(),
|
||||
1 => *input.pad().down(),
|
||||
2 => *input.pad().left(),
|
||||
3 => *input.pad().right(),
|
||||
4 => *input.pad().a(),
|
||||
5 => *input.pad().b(),
|
||||
6 => *input.pad().x(),
|
||||
7 => *input.pad().y(),
|
||||
8 => *input.pad().l(),
|
||||
9 => *input.pad().r(),
|
||||
10 => *input.pad().start(),
|
||||
11 => *input.pad().select(),
|
||||
_ => {
|
||||
return Err(IntrinsicExecutionError::InvalidBuiltinCarrier {
|
||||
owner: "input.button",
|
||||
name,
|
||||
carrier,
|
||||
});
|
||||
}
|
||||
};
|
||||
Ok(button)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1501,8 +1062,8 @@ mod tests {
|
||||
assert_eq!(
|
||||
pixel.flatten_layout(),
|
||||
vec![
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Scalar(BuiltinScalarType::Int64),
|
||||
AbiType::Builtin(COLOR),
|
||||
]
|
||||
);
|
||||
@@ -1570,8 +1131,8 @@ mod tests {
|
||||
assert_eq!(lookup_intrinsic_by_id(length.id).map(|meta| meta.key()), Some(length.key()));
|
||||
|
||||
let hold =
|
||||
lookup_intrinsic("input.button", "hold", 1).expect("input.button.hold must exist");
|
||||
assert_eq!(hold.arg_layout.len(), 1);
|
||||
lookup_intrinsic("input.pad.up", "hold", 1).expect("input.pad.up.hold must exist");
|
||||
assert_eq!(hold.arg_layout.len(), 0);
|
||||
assert_eq!(hold.ret_layout.len(), 1);
|
||||
assert!(hold.deterministic);
|
||||
assert!(!hold.may_allocate);
|
||||
@@ -1621,7 +1182,7 @@ mod tests {
|
||||
fn input_intrinsic_requires_hardware_context() {
|
||||
let mut ctx = HostContext::new(None);
|
||||
let touch_x = lookup_intrinsic("input.touch", "x", 1).expect("input.touch.x must exist");
|
||||
let err = (touch_x.implementation)(&[Value::Int64(TOUCH_HANDLE)], &mut ctx)
|
||||
let err = (touch_x.implementation)(&[], &mut ctx)
|
||||
.expect_err("touch.x must fail without hardware context");
|
||||
assert_eq!(err, IntrinsicExecutionError::HardwareUnavailable);
|
||||
}
|
||||
@@ -1629,9 +1190,23 @@ mod tests {
|
||||
#[test]
|
||||
fn pixel_flat_values_validate_with_color_carrier_slot() {
|
||||
let pixel = lookup_builtin_type("pixel", 1).expect("pixel builtin must exist");
|
||||
let values = vec![Value::Int32(1), Value::Int32(2), Value::Int32(0xF800)];
|
||||
let values = vec![Value::Int64(1), Value::Int64(2), Value::Int64(0xF800)];
|
||||
pixel.validate_flat_values(&values).expect("pixel flat values must validate");
|
||||
assert_eq!(pixel.flat_slot_width, 3);
|
||||
assert_eq!(pixel.field("color").map(|field| field.start_slot), Some(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// Loader-side lookup charges each catalogue record before examining it.
|
||||
pub(crate) fn metered_lookup_intrinsic(
|
||||
id: u32,
|
||||
budget: &mut prometeu_bytecode::load_limits::LoadBudget,
|
||||
) -> Result<Option<&'static IntrinsicMeta>, prometeu_bytecode::load_limits::LoadFailure> {
|
||||
for meta in INTRINSICS.iter().chain(crate::input_intrinsics::INPUT_INTRINSICS.iter()) {
|
||||
budget.work(1)?;
|
||||
if meta.id == id {
|
||||
return Ok(Some(meta));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,147 @@
|
||||
//! Exact-width immutable native storage for a present multi-slot Dynamic.
|
||||
use crate::resource_budget::{
|
||||
Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
|
||||
};
|
||||
use crate::value_contract::ValueError;
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_bytecode::registry::{Registry, TypeId};
|
||||
use std::alloc::{Layout, alloc, dealloc};
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct DynamicBox {
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
values: Box<[Value]>,
|
||||
charge: Reservation,
|
||||
}
|
||||
impl DynamicBox {
|
||||
/// Heap validates the exact non-Dynamic multi-slot group before construction.
|
||||
/// Source owners remain borrowed until publication; allocation never invokes GC.
|
||||
pub fn new(
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
values: &[Value],
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ResourceFailure> {
|
||||
let bytes = budget.bytes::<Value>(values.len(), Q::GuestBytes)?;
|
||||
let layout = Layout::array::<Value>(values.len())
|
||||
.map_err(|_| budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, bytes))?;
|
||||
let charge = budget.reserve(Q::GuestBytes, bytes)?;
|
||||
let ptr = if values.is_empty() {
|
||||
NonNull::dangling()
|
||||
} else {
|
||||
// SAFETY: checked nonzero array layout; a null allocator result is a
|
||||
// recoverable resource failure and drops the provisional reservation.
|
||||
NonNull::new(unsafe { alloc(layout) }.cast::<Value>()).ok_or_else(|| {
|
||||
budget.failure(Q::GuestBytes, ResourceReason::AllocationFailed, bytes)
|
||||
})?
|
||||
};
|
||||
let mut pending = PendingValues { ptr, layout, initialized: 0 };
|
||||
for value in values {
|
||||
let cloned = value.clone();
|
||||
// SAFETY: initialized advances within the exactly allocated Value array;
|
||||
// ptr::write moves each owner into an unused, aligned native slot.
|
||||
unsafe {
|
||||
ptr.as_ptr().add(pending.initialized).write(cloned);
|
||||
}
|
||||
pending.initialized += 1;
|
||||
}
|
||||
// SAFETY: allocation came from the global allocator with precisely the
|
||||
// Box<[Value]> layout; all elements are initialized. Transfer ownership
|
||||
// without Vec capacity/shrink reallocations or a second allocation.
|
||||
let boxed = unsafe {
|
||||
Box::from_raw(std::ptr::slice_from_raw_parts_mut(ptr.as_ptr(), values.len()))
|
||||
};
|
||||
std::mem::forget(pending);
|
||||
Ok(Self { registry, type_id, values: boxed, charge })
|
||||
}
|
||||
pub fn bytes(&self) -> u64 {
|
||||
self.charge.amount()
|
||||
}
|
||||
pub fn values(&self) -> &[Value] {
|
||||
&self.values
|
||||
}
|
||||
pub fn typed_values(
|
||||
&self,
|
||||
registry: &Registry,
|
||||
type_id: TypeId,
|
||||
) -> Result<&[Value], ValueError> {
|
||||
if !std::ptr::eq(self.registry.as_ref(), registry) || self.type_id != type_id {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
Ok(&self.values)
|
||||
}
|
||||
}
|
||||
|
||||
/// Owns the partially initialized allocation until Box takes over. A panic
|
||||
/// while constructing an owner drops only the initialized prefix, then frees it.
|
||||
struct PendingValues {
|
||||
ptr: NonNull<Value>,
|
||||
layout: Layout,
|
||||
initialized: usize,
|
||||
}
|
||||
impl Drop for PendingValues {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: initialized counts constructed Values only, all uniquely owned.
|
||||
unsafe {
|
||||
std::ptr::drop_in_place(std::ptr::slice_from_raw_parts_mut(
|
||||
self.ptr.as_ptr(),
|
||||
self.initialized,
|
||||
));
|
||||
if self.layout.size() != 0 {
|
||||
dealloc(self.ptr.as_ptr().cast(), self.layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn interrupted_native_initialization_drops_the_constructed_prefix_and_refunds() {
|
||||
let budget = RuntimeBudget::default();
|
||||
let text: Arc<str> = Arc::from("partially initialized");
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
let layout = Layout::array::<Value>(3).unwrap();
|
||||
let _charge = budget.reserve(Q::GuestBytes, layout.size() as u64).unwrap();
|
||||
// SAFETY: checked nonzero layout; only the first slot is initialized.
|
||||
let ptr = NonNull::new(unsafe { alloc(layout) }.cast::<Value>()).unwrap();
|
||||
let mut pending = PendingValues { ptr, layout, initialized: 0 };
|
||||
unsafe {
|
||||
ptr.as_ptr().write(Value::String(text.clone()));
|
||||
}
|
||||
pending.initialized = 1;
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
panic!("simulate interrupted construction before the next native slot");
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_width_storage_owns_no_allocation_and_is_not_a_valid_guest_box() {
|
||||
let registry = crate::typed_storage::tests::fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let empty = DynamicBox::new(registry, TypeId(5), &[], &budget).unwrap();
|
||||
assert!(empty.values().is_empty());
|
||||
assert_eq!(empty.bytes(), 0);
|
||||
drop(empty);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
// Heap's group validator rejects empty values before invoking this storage constructor.
|
||||
let mut heap = crate::heap::Heap::with_budget(&budget);
|
||||
assert_eq!(
|
||||
heap.allocate_dynamic_box(
|
||||
crate::typed_storage::tests::fixture_registry(),
|
||||
TypeId(5),
|
||||
&[]
|
||||
)
|
||||
.unwrap_err(),
|
||||
ValueError::Type
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,436 @@
|
||||
use super::*;
|
||||
use crate::resource_budget::{ResourceReason, RuntimeLimits};
|
||||
use crate::typed_storage::tests::fixture_registry;
|
||||
use crate::value_contract::{
|
||||
ValueError as E, can_unpack_dynamic as can_unpack, pack_dynamic as pack,
|
||||
unpack_dynamic as unpack,
|
||||
};
|
||||
use prometeu_bytecode::load_limits::LoadBudget;
|
||||
use prometeu_bytecode::registry::ImplementationDecl;
|
||||
|
||||
const I32: TypeId = TypeId(0);
|
||||
const I64: TypeId = TypeId(1);
|
||||
const COLOR: TypeId = TypeId(4);
|
||||
const VEC: TypeId = TypeId(5);
|
||||
const PIXEL: TypeId = TypeId(6);
|
||||
const STRING: TypeId = TypeId(7);
|
||||
const DYNAMIC: TypeId = TypeId(8);
|
||||
const ENEMY: TypeId = TypeId(9);
|
||||
const OTHER: TypeId = TypeId(10);
|
||||
const MIXED: TypeId = TypeId(11);
|
||||
const TRAIT: TypeId = TypeId(12);
|
||||
const INTS: TypeId = TypeId(13);
|
||||
const DYNAMICS: TypeId = TypeId(18);
|
||||
|
||||
fn payload_handle(values: &[Value]) -> HeapRef {
|
||||
let [Value::Int64(_), Value::HeapRef(r)] = values else { panic!("boxed Dynamic pair") };
|
||||
*r
|
||||
}
|
||||
fn forge_box(heap: &mut Heap, registry: Arc<Registry>, ty: TypeId, values: &[Value]) -> HeapRef {
|
||||
let dynamic_box = DynamicBox::new(registry, ty, values, &heap.budget).unwrap();
|
||||
heap.insert_object(StoredObject {
|
||||
header: ObjectHeader::new(ObjectKind::DynamicBox, values.len() as u32),
|
||||
payload: BudgetVec::new(&heap.budget, Q::GuestBytes),
|
||||
array_elems: None,
|
||||
closure_env: None,
|
||||
coroutine: None,
|
||||
typed: None,
|
||||
dynamic_box: Some(dynamic_box),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_slot_pack_has_one_exact_charged_box_and_immutable_copied_values() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
for (ty, mut input) in [
|
||||
(VEC, vec![Value::Float(1.25), Value::Float(-2.5)]),
|
||||
(
|
||||
PIXEL,
|
||||
vec![Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(u32::MAX.into())],
|
||||
),
|
||||
] {
|
||||
let before = budget.used(Q::GuestBytes);
|
||||
let original = input.clone();
|
||||
let dynamic = pack(®istry, &mut heap, ty, &input).unwrap();
|
||||
assert_eq!(
|
||||
budget.used(Q::GuestBytes) - before,
|
||||
(size_of::<StoredObject>() + input.len() * size_of::<Value>()) as u64
|
||||
);
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
assert_eq!(heap.header(r).unwrap().kind, ObjectKind::DynamicBox);
|
||||
assert_eq!(heap.dynamic_box_values(®istry, r, ty).unwrap().len(), input.len());
|
||||
input.fill(Value::Null);
|
||||
assert_eq!(unpack(®istry, &heap, ty, dynamic.as_slice()).unwrap().as_slice(), original);
|
||||
assert!(can_unpack(®istry, &heap, ty, dynamic.as_slice()).unwrap());
|
||||
assert!(
|
||||
!can_unpack(®istry, &heap, if ty == VEC { PIXEL } else { VEC }, dynamic.as_slice())
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
assert_eq!(heap.len(), 2);
|
||||
assert_eq!(budget.used(Q::GuestBytes), heap.used_bytes.load(Ordering::Relaxed) as u64);
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repack_and_dynamic_extraction_preserve_the_pair_without_another_box() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let first = pack(®istry, &mut heap, VEC, &[Value::Float(3.0), Value::Float(4.0)]).unwrap();
|
||||
for _ in 0..100 {
|
||||
let again = pack(®istry, &mut heap, DYNAMIC, first.as_slice()).unwrap();
|
||||
let extracted = unpack(®istry, &heap, DYNAMIC, again.as_slice()).unwrap();
|
||||
assert_eq!(extracted.as_slice(), first.as_slice());
|
||||
assert!(can_unpack(®istry, &heap, DYNAMIC, again.as_slice()).unwrap());
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), 1);
|
||||
assert_eq!(heap.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_string_and_reference_payloads_are_direct_and_exact() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let enemy = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let text: Arc<str> = Arc::from("shared");
|
||||
let before = heap.allocation_count();
|
||||
for (ty, input) in [
|
||||
(I32, Value::Int32(7)),
|
||||
(I64, Value::Int64(7)),
|
||||
(COLOR, Value::Int64(7)),
|
||||
(STRING, Value::String(text.clone())),
|
||||
(ENEMY, Value::HeapRef(enemy)),
|
||||
] {
|
||||
let dynamic = pack(®istry, &mut heap, ty, std::slice::from_ref(&input)).unwrap();
|
||||
let result = unpack(®istry, &heap, ty, dynamic.as_slice()).unwrap();
|
||||
assert_eq!(result.as_slice(), [input]);
|
||||
if let [Value::String(alias)] = result.as_slice() {
|
||||
assert!(Arc::ptr_eq(alias, &text));
|
||||
}
|
||||
if ty == I64 {
|
||||
assert!(!can_unpack(®istry, &heap, COLOR, dynamic.as_slice()).unwrap());
|
||||
}
|
||||
if ty == COLOR {
|
||||
assert!(!can_unpack(®istry, &heap, I64, dynamic.as_slice()).unwrap());
|
||||
}
|
||||
if ty == ENEMY {
|
||||
assert!(!can_unpack(®istry, &heap, TRAIT, dynamic.as_slice()).unwrap());
|
||||
}
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), before);
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_compatibility_preserves_typed_identity_and_rebuilds_absent_trait() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let untyped = [Value::Null, Value::Null];
|
||||
for ty in [STRING, ENEMY, OTHER, TRAIT, INTS] {
|
||||
assert!(can_unpack(®istry, &heap, ty, &untyped).unwrap());
|
||||
let absent = registry.type_layout(ty).unwrap().storage.default_values();
|
||||
assert_eq!(unpack(®istry, &heap, ty, &untyped).unwrap().as_slice(), absent);
|
||||
let typed = pack(®istry, &mut heap, ty, absent).unwrap();
|
||||
assert_eq!(typed.as_slice(), [Value::Int64(ty.0.into()), Value::Null]);
|
||||
assert_eq!(unpack(®istry, &heap, ty, typed.as_slice()).unwrap().as_slice(), absent);
|
||||
let other = if ty == STRING { ENEMY } else { STRING };
|
||||
assert!(!can_unpack(®istry, &heap, other, typed.as_slice()).unwrap());
|
||||
assert_eq!(unpack(®istry, &heap, other, typed.as_slice()).unwrap_err(), E::Type);
|
||||
}
|
||||
for ty in [I32, VEC, PIXEL] {
|
||||
assert!(!can_unpack(®istry, &heap, ty, &untyped).unwrap());
|
||||
assert_eq!(unpack(®istry, &heap, ty, &untyped).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(pack(®istry, &mut heap, DYNAMIC, &untyped).unwrap().as_slice(), untyped);
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_box_keeps_receiver_identity_and_gc_reaches_through_typed_dynamic_storage() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let source = [Value::HeapRef(receiver), Value::Int64(0)];
|
||||
// A safepoint before pack sees the original source reference.
|
||||
heap.mark_from_roots([receiver]).unwrap();
|
||||
heap.sweep();
|
||||
let dynamic = pack(®istry, &mut heap, TRAIT, &source).unwrap();
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
let holder = heap.allocate_typed_array(registry.clone(), DYNAMICS, 2).unwrap();
|
||||
heap.store_typed(®istry, holder, 0, dynamic.as_slice()).unwrap();
|
||||
heap.store_typed(®istry, holder, 1, dynamic.as_slice()).unwrap();
|
||||
assert_eq!(heap.len(), 3); // Receiver, one shared box, holder.
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(receiver));
|
||||
assert!(heap.is_valid(r));
|
||||
assert_eq!(unpack(®istry, &heap, TRAIT, dynamic.as_slice()).unwrap().as_slice(), source);
|
||||
heap.store_typed(®istry, holder, 0, &[Value::Null, Value::Null]).unwrap();
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(r)); // Second alias still owns reachability.
|
||||
heap.store_typed(®istry, holder, 1, &[Value::Null, Value::Null]).unwrap();
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(!heap.is_valid(r));
|
||||
assert!(!heap.is_valid(receiver));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_is_not_a_guest_instance_or_collection_and_cannot_be_a_trait_receiver() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let dynamic = pack(®istry, &mut heap, VEC, &[Value::Float(0.0), Value::Float(0.0)]).unwrap();
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
assert_eq!(heap.load_typed(®istry, r, 0).unwrap_err(), E::Type);
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &[Value::Float(1.0)]).unwrap_err(), E::Type);
|
||||
assert_eq!(
|
||||
value_contract::validate(®istry, &heap, ENEMY, &[Value::HeapRef(r)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(
|
||||
value_contract::validate(®istry, &heap, TRAIT, &[Value::HeapRef(r), Value::Int64(0)])
|
||||
.unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(heap.concrete_type(r, ®istry).unwrap_err(), E::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_dynamic_queries_fault_before_reporting_ordinary_mismatch() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
for bad in [
|
||||
vec![],
|
||||
vec![Value::Null],
|
||||
vec![Value::Null, Value::Int64(0)],
|
||||
vec![Value::Int64(DYNAMIC.0.into()), Value::Null],
|
||||
vec![Value::Int64(VEC.0.into()), Value::HeapRef(receiver)],
|
||||
vec![Value::Int64(VEC.0.into()), Value::HeapRef(HeapRef::NULL)],
|
||||
vec![Value::Int64(u32::MAX as i64 + 1), Value::Null],
|
||||
vec![Value::Int64(999), Value::Null],
|
||||
vec![Value::Int64(I32.0.into()), Value::Int64(0)],
|
||||
] {
|
||||
assert_eq!(can_unpack(®istry, &heap, STRING, &bad).unwrap_err(), E::Type);
|
||||
assert_eq!(unpack(®istry, &heap, STRING, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
let stale = heap
|
||||
.allocate_dynamic_box(registry.clone(), VEC, &[Value::Float(1.0), Value::Float(2.0)])
|
||||
.unwrap();
|
||||
heap.sweep();
|
||||
let live = heap
|
||||
.allocate_dynamic_box(registry.clone(), VEC, &[Value::Float(3.0), Value::Float(4.0)])
|
||||
.unwrap();
|
||||
assert_eq!(stale.index, live.index);
|
||||
assert_ne!(stale.generation, live.generation);
|
||||
for handle in [
|
||||
stale,
|
||||
HeapRef { index: live.index, generation: 0 },
|
||||
HeapRef { index: u32::MAX, generation: 1 },
|
||||
] {
|
||||
let bad = [Value::Int64(VEC.0.into()), Value::HeapRef(handle)];
|
||||
assert_eq!(can_unpack(®istry, &heap, STRING, &bad).unwrap_err(), E::InvalidHandle);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forged_box_category_type_width_group_and_registry_are_rejected() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let wrong_boxes = [
|
||||
forge_box(&mut heap, registry.clone(), VEC, &[Value::Float(1.0)]),
|
||||
forge_box(&mut heap, registry.clone(), VEC, &[Value::Float(1.0), Value::Int64(2)]),
|
||||
forge_box(&mut heap, registry.clone(), TRAIT, &[Value::HeapRef(receiver), Value::Int64(0)]),
|
||||
forge_box(&mut heap, fixture_registry(), VEC, &[Value::Float(1.0), Value::Float(2.0)]),
|
||||
heap.allocate_object(ObjectKind::DynamicBox, &[]).unwrap(),
|
||||
];
|
||||
for r in wrong_boxes {
|
||||
assert_eq!(
|
||||
can_unpack(®istry, &heap, STRING, &[Value::Int64(VEC.0.into()), Value::HeapRef(r)])
|
||||
.unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
}
|
||||
for contents in
|
||||
[vec![Value::Null, Value::Null], vec![Value::HeapRef(receiver), Value::Int64(1)]]
|
||||
{
|
||||
let r = forge_box(&mut heap, registry.clone(), TRAIT, &contents);
|
||||
assert_eq!(
|
||||
can_unpack(®istry, &heap, TRAIT, &[Value::Int64(TRAIT.0.into()), Value::HeapRef(r)])
|
||||
.unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
}
|
||||
let r = forge_box(&mut heap, registry.clone(), VEC, &[Value::Float(1.0), Value::Float(2.0)]);
|
||||
heap.objects[r.index as usize].as_mut().unwrap().header.payload_len = 3;
|
||||
assert_eq!(heap.dynamic_box_values(®istry, r, VEC).unwrap_err(), E::Type);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_pack_never_allocates_or_consumes_source_values() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
for (ty, input) in [
|
||||
(VEC, vec![Value::Float(1.0)]),
|
||||
(PIXEL, vec![Value::Int64(0), Value::Int64(0), Value::Int64(-1)]),
|
||||
(TRAIT, vec![Value::Null, Value::Int64(0)]),
|
||||
(DYNAMIC, vec![Value::Null, Value::Int64(0)]),
|
||||
] {
|
||||
let copy = input.clone();
|
||||
assert_eq!(pack(®istry, &mut heap, ty, &input).unwrap_err(), E::Type);
|
||||
assert_eq!(input, copy);
|
||||
assert_eq!(heap.len(), 0);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
// The lower-level allocator also rejects categories that must never box.
|
||||
for (ty, input) in [
|
||||
(I32, vec![Value::Int32(0)]),
|
||||
(TRAIT, vec![Value::Null, Value::Null]),
|
||||
(DYNAMIC, vec![Value::Null, Value::Null]),
|
||||
(DYNAMIC, vec![Value::Int64(I32.0.into()), Value::Int32(1)]),
|
||||
] {
|
||||
assert_eq!(heap.allocate_dynamic_box(registry.clone(), ty, &input).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn box_guest_quota_boundary_and_late_table_failure_refund_all_reservations() {
|
||||
let registry = fixture_registry();
|
||||
let bytes = (size_of::<StoredObject>() + 2 * size_of::<Value>()) as u64;
|
||||
for limit in [bytes - 1, bytes] {
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::GuestBytes, limit).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let source = [Value::Float(1.0), Value::Float(2.0)];
|
||||
let result = pack(®istry, &mut heap, VEC, &source);
|
||||
if limit == bytes {
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(budget.used(Q::GuestBytes), bytes);
|
||||
} else {
|
||||
assert!(
|
||||
matches!(result, Err(E::Resource(e)) if e.resource == Q::GuestBytes && e.reason == ResourceReason::Limit)
|
||||
);
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] {
|
||||
assert_eq!(budget.used(q), 0);
|
||||
}
|
||||
}
|
||||
assert_eq!(source, [Value::Float(1.0), Value::Float(2.0)]);
|
||||
}
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::AuxiliaryBytes, 0).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
assert!(
|
||||
matches!(pack(®istry, &mut heap, VEC, &[Value::Float(1.0), Value::Float(2.0)]), Err(E::Resource(e)) if e.resource == Q::AuxiliaryBytes)
|
||||
);
|
||||
assert_eq!(heap.len(), 0);
|
||||
for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] {
|
||||
assert_eq!(budget.used(q), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_and_unpack_succeed_at_full_heap_quota_without_allocating() {
|
||||
let registry = fixture_registry();
|
||||
let bytes = (size_of::<StoredObject>() + 2 * size_of::<Value>()) as u64;
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::GuestBytes, bytes).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let dynamic = pack(®istry, &mut heap, VEC, &[Value::Float(1.0), Value::Float(2.0)]).unwrap();
|
||||
for _ in 0..100 {
|
||||
assert!(can_unpack(®istry, &heap, VEC, dynamic.as_slice()).unwrap());
|
||||
assert_eq!(
|
||||
unpack(®istry, &heap, VEC, dynamic.as_slice()).unwrap().as_slice(),
|
||||
[Value::Float(1.0), Value::Float(2.0)]
|
||||
);
|
||||
assert_eq!(
|
||||
pack(®istry, &mut heap, DYNAMIC, dynamic.as_slice()).unwrap().as_slice(),
|
||||
dynamic.as_slice()
|
||||
);
|
||||
}
|
||||
assert_eq!(heap.allocation_count(), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), bytes);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collecting_a_trait_box_drops_transitive_native_owners_once() {
|
||||
let mut declarations = fixture_registry().declarations().clone();
|
||||
declarations.implementations.push(ImplementationDecl {
|
||||
owner: MIXED,
|
||||
trait_id: TRAIT,
|
||||
methods: vec![],
|
||||
});
|
||||
let registry = Arc::new(
|
||||
Registry::resolve(declarations, 0, &mut LoadBudget::new(Default::default())).unwrap(),
|
||||
);
|
||||
let mut heap = Heap::new();
|
||||
let text: Arc<str> = Arc::from("retained through boxed trait");
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
heap.store_typed(®istry, receiver, 3, &[Value::String(text.clone())]).unwrap();
|
||||
let dynamic =
|
||||
pack(®istry, &mut heap, TRAIT, &[Value::HeapRef(receiver), Value::Int64(2)]).unwrap();
|
||||
// Receiver -> Dynamic box -> receiver is a real typed heap cycle.
|
||||
heap.store_typed(®istry, receiver, 4, dynamic.as_slice()).unwrap();
|
||||
let r = payload_handle(dynamic.as_slice());
|
||||
heap.mark_from_roots([r]).unwrap();
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(heap.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_box_storage_drops_native_owners_even_for_rejected_groups() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let text: Arc<str> = Arc::from("malformed box fixture");
|
||||
let r = forge_box(
|
||||
&mut heap,
|
||||
registry.clone(),
|
||||
VEC,
|
||||
&[Value::String(text.clone()), Value::Float(1.0)],
|
||||
);
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
assert_eq!(heap.dynamic_box_values(®istry, r, VEC).unwrap_err(), E::Type);
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_trait_box_publication_preserves_source_roots_and_existing_table_capacities() {
|
||||
let registry = fixture_registry();
|
||||
let one_slot_aux =
|
||||
(size_of::<Option<StoredAllocation>>() + size_of::<u32>() + size_of::<usize>()) as u64;
|
||||
let budget = RuntimeBudget::new(
|
||||
RuntimeLimits::default().lowered(Q::AuxiliaryBytes, one_slot_aux).unwrap(),
|
||||
);
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let receiver = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let before = budget.used(Q::GuestBytes);
|
||||
let source = [Value::HeapRef(receiver), Value::Int64(0)];
|
||||
assert!(
|
||||
matches!(pack(®istry, &mut heap, TRAIT, &source), Err(E::Resource(e)) if e.resource == Q::AuxiliaryBytes)
|
||||
);
|
||||
assert_eq!(heap.len(), 1);
|
||||
assert_eq!(heap.allocation_count(), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), before);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), one_slot_aux);
|
||||
assert_eq!(budget.used(Q::HandleSlots), 1);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert_eq!(source, [Value::HeapRef(receiver), Value::Int64(0)]);
|
||||
heap.mark_from_roots(source.iter().filter_map(crate::roots::value_heap_ref)).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(receiver));
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
//! Fixed queries over the logical-frame input snapshot. No endpoint or selector
|
||||
//! crosses the guest boundary; names below identify operations, not value types.
|
||||
use crate::builtins::{AbiType, BuiltinScalarType, IntrinsicExecutionError, IntrinsicMeta};
|
||||
use prometeu_bytecode::Value;
|
||||
use prometeu_hal::{HostContext, InputPlatform, button::Button};
|
||||
|
||||
const BOOL: &[AbiType] = &[AbiType::Scalar(BuiltinScalarType::Bool)];
|
||||
const INT: &[AbiType] = &[AbiType::Scalar(BuiltinScalarType::Int64)];
|
||||
|
||||
const fn query(
|
||||
id: u32,
|
||||
owner: &'static str,
|
||||
name: &'static str,
|
||||
ret_layout: &'static [AbiType],
|
||||
implementation: crate::IntrinsicImplementation,
|
||||
) -> IntrinsicMeta {
|
||||
IntrinsicMeta {
|
||||
id,
|
||||
owner,
|
||||
name,
|
||||
version: 1,
|
||||
arg_layout: &[],
|
||||
ret_layout,
|
||||
deterministic: true,
|
||||
may_allocate: false,
|
||||
implementation,
|
||||
}
|
||||
}
|
||||
|
||||
fn snapshot<'a>(
|
||||
args: &[Value],
|
||||
ctx: &'a HostContext<'_>,
|
||||
) -> Result<&'a dyn InputPlatform, IntrinsicExecutionError> {
|
||||
if !args.is_empty() {
|
||||
return Err(IntrinsicExecutionError::ArityMismatch { expected: 0, got: args.len() });
|
||||
}
|
||||
let platform = ctx.platform.as_deref().ok_or(IntrinsicExecutionError::HardwareUnavailable)?;
|
||||
Ok(platform.input())
|
||||
}
|
||||
|
||||
fn read_button<const PROPERTY: u8>(button: &Button) -> Vec<Value> {
|
||||
vec![match PROPERTY {
|
||||
0 => Value::Boolean(button.pressed),
|
||||
1 => Value::Boolean(button.released),
|
||||
2 => Value::Boolean(button.down),
|
||||
3 => Value::Int64(i64::from(button.hold_frames)),
|
||||
_ => unreachable!("private input property outside the fixed catalogue"),
|
||||
}]
|
||||
}
|
||||
|
||||
// This single mapping defines IDs, canonical names and their snapshot readers.
|
||||
macro_rules! input_queries {
|
||||
($(($base:literal, $prefix:literal, $reader:ident, $device:ident, $button:ident)),* $(,)?) => {
|
||||
pub(crate) const INPUT_INTRINSICS: [IntrinsicMeta; 54] = [
|
||||
$(
|
||||
query($base, $prefix, "pressed", BOOL, $reader::<0>),
|
||||
query($base + 1, $prefix, "released", BOOL, $reader::<1>),
|
||||
query($base + 2, $prefix, "down", BOOL, $reader::<2>),
|
||||
query($base + 3, $prefix, "hold", INT, $reader::<3>),
|
||||
)*
|
||||
query(0x2134, "input.touch", "x", INT, touch_x),
|
||||
query(0x2135, "input.touch", "y", INT, touch_y),
|
||||
];
|
||||
$(
|
||||
fn $reader<const PROPERTY: u8>(
|
||||
args: &[Value], ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(read_button::<PROPERTY>(snapshot(args, ctx)?.$device().$button()))
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
input_queries!(
|
||||
(0x2100, "input.pad.up", up, pad, up),
|
||||
(0x2104, "input.pad.down", down, pad, down),
|
||||
(0x2108, "input.pad.left", left, pad, left),
|
||||
(0x210C, "input.pad.right", right, pad, right),
|
||||
(0x2110, "input.pad.a", a, pad, a),
|
||||
(0x2114, "input.pad.b", b, pad, b),
|
||||
(0x2118, "input.pad.x", x, pad, x),
|
||||
(0x211C, "input.pad.y", y, pad, y),
|
||||
(0x2120, "input.pad.l", l, pad, l),
|
||||
(0x2124, "input.pad.r", r, pad, r),
|
||||
(0x2128, "input.pad.start", start, pad, start),
|
||||
(0x212C, "input.pad.select", select, pad, select),
|
||||
(0x2130, "input.touch.button", touch_button, touch, f),
|
||||
);
|
||||
|
||||
fn touch_x(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().x()))])
|
||||
}
|
||||
|
||||
fn touch_y(
|
||||
args: &[Value],
|
||||
ctx: &mut HostContext<'_>,
|
||||
) -> Result<Vec<Value>, IntrinsicExecutionError> {
|
||||
Ok(vec![Value::Int64(i64::from(snapshot(args, ctx)?.touch().y()))])
|
||||
}
|
||||
@@ -1,12 +1,22 @@
|
||||
mod builtins;
|
||||
mod call_frame;
|
||||
mod input_intrinsics;
|
||||
mod local_addressing;
|
||||
// Keep the verifier internal in production builds, but expose it for integration tests
|
||||
// so the golden verifier suite can exercise it without widening the public API in releases.
|
||||
mod heap;
|
||||
mod object;
|
||||
pub mod resource_budget;
|
||||
mod roots;
|
||||
// Bounded Registry preflight is consumed by the upcoming v1 image loader.
|
||||
#[allow(dead_code)]
|
||||
mod registry_verifier;
|
||||
mod scheduler;
|
||||
// Registry-backed consumers are integrated with executable images in PLN-0176.
|
||||
#[allow(dead_code)]
|
||||
mod typed_storage;
|
||||
#[allow(dead_code)]
|
||||
mod value_contract;
|
||||
#[cfg(not(test))]
|
||||
mod verifier;
|
||||
#[cfg(test)]
|
||||
@@ -24,3 +34,5 @@ pub use builtins::{
|
||||
pub use prometeu_hal::{HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
pub use virtual_machine::{BudgetReport, LogicalFrameEndingReason, VirtualMachine};
|
||||
pub use vm_init_error::{LoaderPatchError, VmInitError};
|
||||
|
||||
pub use verifier::VerifierError;
|
||||
|
||||
@@ -88,6 +88,12 @@ pub enum ObjectKind {
|
||||
/// contained `HeapRef`s directly.
|
||||
/// - `payload_len` is 0 for this fixed-layout object.
|
||||
Coroutine = 6,
|
||||
|
||||
/// Registry-defined Struct with a compact typed instance payload.
|
||||
Struct = 7,
|
||||
|
||||
/// Internal immutable multi-slot Dynamic payload, never a nominal guest type.
|
||||
DynamicBox = 8,
|
||||
// Future kinds must be appended here to keep tag numbers stable.
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
//! Bounded structural preflight of declared Registry code ranges. This phase
|
||||
//! checks every instruction, including unreachable code. Stack/type-flow,
|
||||
//! bindings and entry validation must still succeed before image publication.
|
||||
use prometeu_bytecode::isa::core::{CoreOpCode as Op, CoreOpCodeSpecExt};
|
||||
use prometeu_bytecode::load_limits::{
|
||||
IdDomain, LoadBudget, LoadCode as C, LoadFailure as E, LoadReason as R, LoadResource as Q,
|
||||
};
|
||||
use prometeu_bytecode::registry::{FunctionId, Registry};
|
||||
use prometeu_bytecode::{DecodeError, decode_next};
|
||||
|
||||
pub(crate) fn validate_registry_instructions(
|
||||
code: &[u8],
|
||||
registry: &Registry,
|
||||
constant_entries: usize,
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<(), E> {
|
||||
let temporary_before = budget.used(Q::TemporaryBytes);
|
||||
let result = validate_inner(code, registry, constant_entries, budget);
|
||||
// All maps live inside validate_inner and are already dropped here. Failure
|
||||
// refunds scratch while preserving attempted work and prior load ownership.
|
||||
budget.refund(Q::TemporaryBytes, budget.used(Q::TemporaryBytes) - temporary_before);
|
||||
result
|
||||
}
|
||||
fn validate_inner(
|
||||
code: &[u8],
|
||||
registry: &Registry,
|
||||
constants: usize,
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<(), E> {
|
||||
budget.check(Q::CodeBytes, code.len() as u64)?;
|
||||
budget.check(Q::ConstantEntries, constants as u64)?;
|
||||
budget.check(Q::Functions, registry.declarations().functions.len() as u64)?;
|
||||
for (index, function) in registry.declarations().functions.iter().enumerate() {
|
||||
let function_id = index as u32;
|
||||
budget
|
||||
.work(1)
|
||||
.map_err(|e| e.in_section(2).in_function(function_id, Some(function.code_offset)))?;
|
||||
let start = function.code_offset as usize;
|
||||
let end = start
|
||||
.checked_add(function.code_len as usize)
|
||||
.ok_or_else(|| error(R::CodeRange, function_id, start))?;
|
||||
let contextual =
|
||||
|e: E, pc: usize| e.in_section(2).in_function(function_id, u32::try_from(pc).ok());
|
||||
let body = code.get(start..end).ok_or_else(|| error(R::CodeRange, function_id, start))?;
|
||||
let widths = registry.function_layout(FunctionId(function_id)).unwrap();
|
||||
let local_slots = u64::from(widths.parameter_slots) + u64::from(function.local_slots);
|
||||
let total = local_slots + u64::from(function.max_stack_slots);
|
||||
if total > budget.frame_limit() {
|
||||
return Err(error(R::FrameSlots, function_id, start));
|
||||
}
|
||||
if u64::from(widths.result_slots) > budget.result_limit() {
|
||||
return Err(error(R::ReturnSlots, function_id, start));
|
||||
}
|
||||
let function_scratch_before = budget.used(Q::TemporaryBytes);
|
||||
let mut at_pc = start;
|
||||
let result = (|| {
|
||||
let mut boundaries = boundary_map(
|
||||
body.len().checked_add(1).ok_or_else(|| E::new(C::ResourceLimit, R::Overflow))?,
|
||||
budget,
|
||||
)?;
|
||||
let mut pc = 0;
|
||||
while pc < body.len() {
|
||||
at_pc = start + pc;
|
||||
let instruction =
|
||||
metered_decode(pc, body, budget).map_err(|e| contextual(e, start + pc))?;
|
||||
budget.work(1)?; // Boundary write.
|
||||
boundaries[pc] = 1;
|
||||
pc = instruction.next_pc;
|
||||
}
|
||||
budget.work(1)?;
|
||||
boundaries[body.len()] = 1; // Existing jump-to-function-end return boundary.
|
||||
pc = 0;
|
||||
while pc < body.len() {
|
||||
at_pc = start + pc;
|
||||
let instruction =
|
||||
metered_decode(pc, body, budget).map_err(|e| contextual(e, start + pc))?;
|
||||
let fail = |reason| error(reason, function_id, start + pc);
|
||||
if instruction.opcode.requires_registry() {
|
||||
budget.work(
|
||||
if matches!(
|
||||
instruction.opcode,
|
||||
Op::DynPack | Op::DynUnpack | Op::DynCanUnpack
|
||||
) {
|
||||
2
|
||||
} else {
|
||||
4
|
||||
},
|
||||
)?;
|
||||
}
|
||||
instruction
|
||||
.registry_stack_effect(registry)
|
||||
.map_err(|e| contextual(e, start + pc))?;
|
||||
match instruction.opcode {
|
||||
Op::Jmp | Op::JmpIfTrue | Op::JmpIfFalse => {
|
||||
budget.work(1)?; // Branch edge.
|
||||
let target = instruction.imm_u32().unwrap() as usize;
|
||||
if target > body.len() {
|
||||
return Err(fail(R::JumpTarget));
|
||||
}
|
||||
budget.work(1)?; // Boundary read.
|
||||
if boundaries[target] == 0 {
|
||||
return Err(fail(R::InstructionBoundary));
|
||||
}
|
||||
}
|
||||
Op::GetLocal | Op::SetLocal => {
|
||||
let slot = instruction.imm_u32().unwrap();
|
||||
if u64::from(slot) >= local_slots {
|
||||
return Err(fail(R::LocalSlot).member(slot));
|
||||
}
|
||||
}
|
||||
Op::PushBool if instruction.imm_u8().unwrap() > 1 => {
|
||||
return Err(fail(R::BooleanImmediate));
|
||||
}
|
||||
Op::PushConst => {
|
||||
let id = instruction.imm_u32().unwrap();
|
||||
if id as usize >= constants {
|
||||
return Err(with_id(reference_error(fail(R::ConstantIndex)), id));
|
||||
}
|
||||
}
|
||||
Op::Call | Op::MakeClosure | Op::Spawn => {
|
||||
let id = if instruction.opcode == Op::Call {
|
||||
instruction.imm_u32().unwrap()
|
||||
} else {
|
||||
instruction.imm_u32x2().unwrap().0
|
||||
};
|
||||
let callee = registry.function_layout(FunctionId(id)).ok_or_else(|| {
|
||||
reference_error(fail(R::FunctionId)).reference(IdDomain::Function, id)
|
||||
})?;
|
||||
if instruction.opcode == Op::Spawn
|
||||
&& instruction.imm_u32x2().unwrap().1 != callee.parameter_slots
|
||||
{
|
||||
return Err(fail(R::Signature).reference(IdDomain::Function, id));
|
||||
}
|
||||
}
|
||||
Op::Hostcall => {
|
||||
return Err(with_id(
|
||||
fail(R::UnpatchedHostcall),
|
||||
instruction.imm_u32().unwrap(),
|
||||
));
|
||||
}
|
||||
Op::Syscall => {
|
||||
let id = instruction.imm_u32().unwrap();
|
||||
if prometeu_hal::syscalls::Syscall::from_u32(id).is_none() {
|
||||
return Err(with_id(reference_error(fail(R::NativeId)), id));
|
||||
}
|
||||
}
|
||||
Op::Intrinsic => {
|
||||
let id = instruction.imm_u32().unwrap();
|
||||
if crate::builtins::metered_lookup_intrinsic(id, budget)?.is_none() {
|
||||
return Err(with_id(reference_error(fail(R::NativeId)), id));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
pc = instruction.next_pc;
|
||||
}
|
||||
Ok(())
|
||||
})()
|
||||
.map_err(|e: E| if e.function.is_none() { contextual(e, at_pc) } else { e });
|
||||
// Free each function's map before the next. No aggregate unmetered CFG.
|
||||
budget.refund(Q::TemporaryBytes, budget.used(Q::TemporaryBytes) - function_scratch_before);
|
||||
result?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn boundary_map(count: usize, budget: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
||||
budget.charge(Q::TemporaryBytes, count as u64)?;
|
||||
let mut values = Vec::new();
|
||||
values.try_reserve_exact(count).map_err(|_| {
|
||||
let mut e = E::resource(Q::TemporaryBytes, count as u64, budget.limit(Q::TemporaryBytes));
|
||||
e.reason = R::Allocation;
|
||||
e
|
||||
})?;
|
||||
budget.charge(Q::TemporaryBytes, (values.capacity() - count) as u64)?;
|
||||
budget.work(count)?; // Initialize every map byte before it can be read.
|
||||
values.resize(count, 0);
|
||||
Ok(values)
|
||||
}
|
||||
fn metered_decode<'a>(
|
||||
pc: usize,
|
||||
bytes: &'a [u8],
|
||||
budget: &mut LoadBudget,
|
||||
) -> Result<prometeu_bytecode::DecodedInstr<'a>, E> {
|
||||
budget.work(bytes.len().saturating_sub(pc).min(2))?;
|
||||
let result = decode_next(pc, bytes).map_err(|e| {
|
||||
E::new(
|
||||
C::InvalidCode,
|
||||
match e {
|
||||
DecodeError::UnknownOpcode { .. } => R::UnknownOpcode,
|
||||
_ => R::Truncated,
|
||||
},
|
||||
)
|
||||
})?;
|
||||
budget.work(result.imm.len() + 1)?; // Immediate examination and instruction.
|
||||
Ok(result)
|
||||
}
|
||||
fn error(reason: R, function: u32, pc: usize) -> E {
|
||||
E::new(C::InvalidCode, reason).in_section(2).in_function(function, u32::try_from(pc).ok())
|
||||
}
|
||||
fn with_id(mut e: E, id: u32) -> E {
|
||||
e.referenced_id = Some(id);
|
||||
e
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
fn reference_error(mut e: E) -> E {
|
||||
e.code = C::InvalidReference;
|
||||
e
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
use super::*;
|
||||
use prometeu_bytecode::load_limits::LoadLimits;
|
||||
use prometeu_bytecode::registry::{
|
||||
BuiltinId as B, FunctionDecl, NamedType, RegistryDeclarations, Signature, TypeDecl, TypeId,
|
||||
TypeKind,
|
||||
};
|
||||
|
||||
fn op(code: &mut Vec<u8>, op: Op) {
|
||||
code.extend_from_slice(&(op as u16).to_le_bytes());
|
||||
}
|
||||
fn arg(code: &mut Vec<u8>, op_code: Op, n: u32) {
|
||||
op(code, op_code);
|
||||
code.extend_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
fn registry(code: &[u8], functions: Vec<FunctionDecl>) -> Registry {
|
||||
Registry::resolve(
|
||||
RegistryDeclarations {
|
||||
types: vec![
|
||||
TypeDecl { name: None, kind: TypeKind::Builtin(B::INT32) },
|
||||
TypeDecl { name: None, kind: TypeKind::Builtin(B::VEC2) },
|
||||
TypeDecl { name: None, kind: TypeKind::Struct(vec![]) },
|
||||
TypeDecl { name: None, kind: TypeKind::Trait(vec![]) },
|
||||
],
|
||||
functions,
|
||||
implementations: vec![],
|
||||
},
|
||||
code.len(),
|
||||
&mut LoadBudget::new(Default::default()),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
fn function(code: &[u8]) -> FunctionDecl {
|
||||
FunctionDecl { code_len: code.len() as u32, max_stack_slots: 4, ..Default::default() }
|
||||
}
|
||||
fn check(code: &[u8], registry: &Registry) -> Result<(), E> {
|
||||
validate_registry_instructions(code, registry, 1, &mut LoadBudget::new(Default::default()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_opcodes_and_relative_jumps_are_resolved_in_declared_ranges() {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, Op::DynPack, 1);
|
||||
arg(&mut code, Op::DynUnpack, 1);
|
||||
arg(&mut code, Op::DynCanUnpack, 1);
|
||||
for (operation, target) in
|
||||
[(Op::RefIsExact, 2), (Op::RefCast, 2), (Op::RefImplements, 3), (Op::RefToTrait, 3)]
|
||||
{
|
||||
arg(&mut code, operation, 2);
|
||||
code.extend_from_slice(&(target as u32).to_le_bytes());
|
||||
}
|
||||
let start = code.len();
|
||||
arg(&mut code, Op::Jmp, 6);
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: start as u32, ..function(&code) },
|
||||
FunctionDecl {
|
||||
code_offset: start as u32,
|
||||
code_len: 6,
|
||||
max_stack_slots: 0,
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
);
|
||||
assert!(check(&code, &r).is_ok());
|
||||
// This phase is structural: stack heights/type groups still need the next analysis.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_bad_type_ids_and_categories_preserve_function_and_pc() {
|
||||
for (operation, id) in [(Op::DynPack, 99), (Op::RefCast, 0)] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, operation, id);
|
||||
if operation == Op::RefCast {
|
||||
code.extend_from_slice(&2_u32.to_le_bytes());
|
||||
}
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!(e.code, C::InvalidReference);
|
||||
assert_eq!(e.function, Some(0));
|
||||
assert_eq!(e.pc, Some(2));
|
||||
assert_eq!(e.section, Some(2));
|
||||
assert_eq!(e.referenced_id, Some(id));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jumps_reject_cross_range_and_mid_instruction_even_when_unreachable() {
|
||||
for (target, reason) in [(7, R::JumpTarget), (3, R::InstructionBoundary)] {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, Op::Jmp, target);
|
||||
op(&mut code, Op::Halt);
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: 6, ..function(&code) },
|
||||
FunctionDecl { code_offset: 6, code_len: 2, ..function(&code) },
|
||||
],
|
||||
);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!((e.code, e.reason, e.pc), (C::InvalidCode, reason, Some(0)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_bounds_use_derived_parameter_widths_and_u32_local_counts() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, Op::GetLocal, 70);
|
||||
let mut f = function(&code);
|
||||
f.signature = Signature {
|
||||
parameters: vec![NamedType { type_id: TypeId(1), name: None }],
|
||||
results: vec![],
|
||||
};
|
||||
f.local_slots = 69; // vec2 contributes two parameter slots, total 71.
|
||||
let r = registry(&code, vec![f.clone()]);
|
||||
assert!(check(&code, &r).is_ok());
|
||||
for slot in [71, 65_536, u32::MAX] {
|
||||
let mut bad = vec![];
|
||||
arg(&mut bad, Op::SetLocal, slot);
|
||||
let e = check(&bad, &r).unwrap_err();
|
||||
assert_eq!((e.reason, e.member), (R::LocalSlot, Some(slot)));
|
||||
}
|
||||
let mut limits = LoadLimits::default();
|
||||
limits.frame_slots = 74;
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
let e = validate_registry_instructions(&code, &r, 1, &mut budget).unwrap_err();
|
||||
assert_eq!(e.reason, R::FrameSlots); // 2 parameters + 69 locals + 4 temporaries = 75.
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreachable_function_constant_boolean_and_native_operands_are_checked() {
|
||||
for (operation, id, reason) in [
|
||||
(Op::Call, u32::MAX, R::FunctionId),
|
||||
(Op::PushConst, 1, R::ConstantIndex),
|
||||
(Op::Intrinsic, u32::MAX, R::NativeId),
|
||||
(Op::Syscall, u32::MAX, R::NativeId),
|
||||
(Op::Hostcall, 0, R::UnpatchedHostcall),
|
||||
] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, operation, id);
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!((e.reason, e.pc, e.referenced_id), (reason, Some(2), Some(id)));
|
||||
assert_eq!(
|
||||
e.code,
|
||||
if operation == Op::Hostcall { C::InvalidCode } else { C::InvalidReference }
|
||||
);
|
||||
}
|
||||
let code = [
|
||||
(Op::Halt as u16).to_le_bytes().as_slice(),
|
||||
(Op::PushBool as u16).to_le_bytes().as_slice(),
|
||||
&[2],
|
||||
]
|
||||
.concat();
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert_eq!(check(&code, &r).unwrap_err().reason, R::BooleanImmediate);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spawn_arity_does_not_truncate_and_closure_function_ids_are_checked() {
|
||||
for (operation, function_id, count, reason) in
|
||||
[(Op::Spawn, 0, 65_536, R::Signature), (Op::MakeClosure, 1, 0, R::FunctionId)]
|
||||
{
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, operation, function_id);
|
||||
code.extend_from_slice(&(count as u32).to_le_bytes());
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert_eq!(check(&code, &r).unwrap_err().reason, reason);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declared_range_is_not_extended_into_gaps_or_neighbor_functions() {
|
||||
let code = vec![Op::Halt as u8, 0, 0xff, 0xff];
|
||||
let r = registry(&code, vec![FunctionDecl { code_len: 2, ..function(&code) }]);
|
||||
assert!(check(&code, &r).is_ok()); // Unowned gap is outside this phase's ranges.
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert_eq!(check(&code, &r).unwrap_err().reason, R::UnknownOpcode);
|
||||
let short = &code[..1];
|
||||
assert_eq!(check(short, &r).unwrap_err().reason, R::CodeRange);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scratch_is_refunded_per_function_and_preserves_prior_load_ownership() {
|
||||
let code = vec![Op::Halt as u8, 0, Op::Halt as u8, 0];
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: 2, ..function(&code) },
|
||||
FunctionDecl { code_offset: 2, code_len: 2, ..function(&code) },
|
||||
],
|
||||
);
|
||||
let mut limits = LoadLimits::default();
|
||||
limits.temporary_bytes = 8;
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
budget.charge(Q::TemporaryBytes, 5).unwrap();
|
||||
budget.charge(Q::RegistryBytes, 10).unwrap();
|
||||
validate_registry_instructions(&code, &r, 1, &mut budget).unwrap();
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 5); // Both 3-byte maps fit separately.
|
||||
assert_eq!(budget.used(Q::RegistryBytes), 10);
|
||||
assert!(budget.used(Q::Work) > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scratch_and_work_exhaustion_fail_with_context_and_no_counter_leak() {
|
||||
let code = vec![Op::Halt as u8, 0];
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
for limits in [
|
||||
LoadLimits { temporary_bytes: 2, ..Default::default() },
|
||||
LoadLimits { work: 4, ..Default::default() },
|
||||
] {
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
let e = validate_registry_instructions(&code, &r, 1, &mut budget).unwrap_err();
|
||||
assert_eq!(e.code, C::ResourceLimit);
|
||||
assert_eq!(e.function, Some(0));
|
||||
assert_eq!(e.pc, Some(0));
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert!(budget.used(Q::Work) <= budget.limit(Q::Work));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_immediates_cannot_borrow_bytes_from_the_next_function() {
|
||||
let code = vec![Op::PushI64 as u8, 0, Op::Halt as u8, 0];
|
||||
let r = registry(
|
||||
&code,
|
||||
vec![
|
||||
FunctionDecl { code_len: 2, ..function(&code) },
|
||||
FunctionDecl { code_offset: 2, code_len: 2, ..function(&code) },
|
||||
],
|
||||
);
|
||||
let e = check(&code, &r).unwrap_err();
|
||||
assert_eq!((e.code, e.reason, e.pc), (C::InvalidCode, R::Truncated, Some(0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_lookup_work_refusal_retains_the_actual_instruction_pc() {
|
||||
let mut code = vec![];
|
||||
op(&mut code, Op::Halt);
|
||||
arg(&mut code, Op::Intrinsic, 0x2100);
|
||||
let r = registry(&code, vec![function(&code)]);
|
||||
assert!(check(&code, &r).is_ok()); // Known input query, even in unreachable code.
|
||||
// One function + nine initialized map bytes + two decoding passes and
|
||||
// boundary writes consume 33 units before the first catalogue record.
|
||||
let limits = LoadLimits { work: 33, ..Default::default() };
|
||||
let mut budget = LoadBudget::new(limits);
|
||||
let e = validate_registry_instructions(&code, &r, 1, &mut budget).unwrap_err();
|
||||
assert_eq!(
|
||||
(e.code, e.resource, e.function, e.pc),
|
||||
(C::ResourceLimit, Some(Q::Work), Some(0), Some(2))
|
||||
);
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
assert_eq!(budget.used(Q::Work), 33);
|
||||
}
|
||||
@@ -0,0 +1,503 @@
|
||||
//! Session accounting shared by owners, with reservations refunded on failure/drop.
|
||||
//! These quotas account VM domains, not process RSS or allocator metadata.
|
||||
use std::collections::VecDeque;
|
||||
use std::fmt;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(usize)]
|
||||
pub enum RuntimeResource {
|
||||
GuestBytes,
|
||||
HandleSlots,
|
||||
AuxiliaryBytes,
|
||||
TemporaryBytes,
|
||||
StringBytes,
|
||||
StringAllocations,
|
||||
IndividualStringBytes,
|
||||
CallDepth,
|
||||
}
|
||||
const COUNT: usize = 8;
|
||||
const MIB: u64 = 1_048_576;
|
||||
const CEILINGS: [u64; COUNT] =
|
||||
[256 * MIB, 1_048_576, 64 * MIB, 64 * MIB, 64 * MIB, 262_144, 4 * MIB, 1_024];
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct RuntimeLimits([u64; COUNT]);
|
||||
impl Default for RuntimeLimits {
|
||||
fn default() -> Self {
|
||||
Self(CEILINGS)
|
||||
}
|
||||
}
|
||||
impl RuntimeLimits {
|
||||
pub fn get(&self, resource: RuntimeResource) -> u64 {
|
||||
self.0[resource as usize]
|
||||
}
|
||||
/// Test/host configurations may lower ceilings; guest code cannot raise them.
|
||||
pub fn lowered(
|
||||
mut self,
|
||||
resource: RuntimeResource,
|
||||
limit: u64,
|
||||
) -> Result<Self, ResourceFailure> {
|
||||
if limit > self.get(resource) {
|
||||
return Err(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::Limit,
|
||||
requested: limit,
|
||||
limit: self.get(resource),
|
||||
});
|
||||
}
|
||||
self.0[resource as usize] = limit;
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ResourceReason {
|
||||
Limit,
|
||||
SizeOverflow,
|
||||
AllocationFailed,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ResourceFailure {
|
||||
pub resource: RuntimeResource,
|
||||
pub reason: ResourceReason,
|
||||
pub requested: u64,
|
||||
pub limit: u64,
|
||||
}
|
||||
impl fmt::Display for ResourceFailure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{:?}: {:?}, requested {}, limit {}",
|
||||
self.resource, self.reason, self.requested, self.limit
|
||||
)
|
||||
}
|
||||
}
|
||||
impl std::error::Error for ResourceFailure {}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct State {
|
||||
limits: RuntimeLimits,
|
||||
used: [u64; COUNT],
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RuntimeBudget(Arc<Mutex<State>>);
|
||||
impl Default for RuntimeBudget {
|
||||
fn default() -> Self {
|
||||
Self::new(RuntimeLimits::default())
|
||||
}
|
||||
}
|
||||
impl RuntimeBudget {
|
||||
pub fn new(limits: RuntimeLimits) -> Self {
|
||||
Self(Arc::new(Mutex::new(State { limits, used: [0; COUNT] })))
|
||||
}
|
||||
fn state(&self) -> MutexGuard<'_, State> {
|
||||
self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
pub fn used(&self, resource: RuntimeResource) -> u64 {
|
||||
self.state().used[resource as usize]
|
||||
}
|
||||
pub fn limit(&self, resource: RuntimeResource) -> u64 {
|
||||
self.state().limits.get(resource)
|
||||
}
|
||||
pub fn check(&self, resource: RuntimeResource, requested: u64) -> Result<(), ResourceFailure> {
|
||||
let limit = self.limit(resource);
|
||||
if requested > limit {
|
||||
Err(ResourceFailure { resource, reason: ResourceReason::Limit, requested, limit })
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub fn failure(
|
||||
&self,
|
||||
resource: RuntimeResource,
|
||||
reason: ResourceReason,
|
||||
requested: u64,
|
||||
) -> ResourceFailure {
|
||||
ResourceFailure { resource, reason, requested, limit: self.limit(resource) }
|
||||
}
|
||||
pub fn bytes<T>(
|
||||
&self,
|
||||
count: usize,
|
||||
resource: RuntimeResource,
|
||||
) -> Result<u64, ResourceFailure> {
|
||||
count
|
||||
.checked_mul(size_of::<T>())
|
||||
.and_then(|bytes| u64::try_from(bytes).ok())
|
||||
.ok_or_else(|| self.failure(resource, ResourceReason::SizeOverflow, u64::MAX))
|
||||
}
|
||||
pub fn reserve(
|
||||
&self,
|
||||
resource: RuntimeResource,
|
||||
amount: u64,
|
||||
) -> Result<Reservation, ResourceFailure> {
|
||||
let mut state = self.state();
|
||||
let current = state.used[resource as usize];
|
||||
let limit = state.limits.get(resource);
|
||||
let requested = current.checked_add(amount).ok_or(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::SizeOverflow,
|
||||
requested: u64::MAX,
|
||||
limit,
|
||||
})?;
|
||||
if requested > limit {
|
||||
return Err(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::Limit,
|
||||
requested,
|
||||
limit,
|
||||
});
|
||||
}
|
||||
state.used[resource as usize] = requested;
|
||||
Ok(Reservation { budget: self.clone(), resource, amount })
|
||||
}
|
||||
pub(crate) fn empty_charge(&self, resource: RuntimeResource) -> Reservation {
|
||||
Reservation { budget: self.clone(), resource, amount: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// Unique accounting ownership; moving this token never charges a second time.
|
||||
#[derive(Debug)]
|
||||
pub struct Reservation {
|
||||
budget: RuntimeBudget,
|
||||
resource: RuntimeResource,
|
||||
amount: u64,
|
||||
}
|
||||
impl Reservation {
|
||||
pub fn amount(&self) -> u64 {
|
||||
self.amount
|
||||
}
|
||||
pub(crate) fn absorb(&mut self, mut other: Self) {
|
||||
assert!(Arc::ptr_eq(&self.budget.0, &other.budget.0));
|
||||
assert_eq!(self.resource, other.resource);
|
||||
self.amount += other.amount;
|
||||
other.amount = 0;
|
||||
}
|
||||
fn resize(&mut self, amount: u64) -> Result<(), ResourceFailure> {
|
||||
if amount > self.amount {
|
||||
self.absorb(self.budget.reserve(self.resource, amount - self.amount)?);
|
||||
} else {
|
||||
self.budget.state().used[self.resource as usize] -= self.amount - amount;
|
||||
self.amount = amount;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl Drop for Reservation {
|
||||
fn drop(&mut self) {
|
||||
self.budget.state().used[self.resource as usize] -= self.amount;
|
||||
}
|
||||
}
|
||||
|
||||
/// Scheduler storage owns its capacity charge, including unused capacity.
|
||||
/// During growth, the old allocation is also covered by a temporary reservation.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct BudgetQueue<T> {
|
||||
data: VecDeque<T>,
|
||||
charge: Reservation,
|
||||
}
|
||||
impl<T> BudgetQueue<T> {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn capacity_bytes(&self) -> u64 {
|
||||
self.charge.amount
|
||||
}
|
||||
|
||||
pub fn new(budget: &RuntimeBudget) -> Self {
|
||||
Self { data: VecDeque::new(), charge: budget.empty_charge(RuntimeResource::AuxiliaryBytes) }
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub fn len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.data.is_empty()
|
||||
}
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
self.data.pop_front()
|
||||
}
|
||||
pub fn reserve(&mut self, additional: usize) -> Result<(), ResourceFailure> {
|
||||
let b = &self.charge.budget;
|
||||
let q = RuntimeResource::AuxiliaryBytes;
|
||||
let required = self
|
||||
.data
|
||||
.len()
|
||||
.checked_add(additional)
|
||||
.ok_or_else(|| b.failure(q, ResourceReason::SizeOverflow, u64::MAX))?;
|
||||
if required <= self.data.capacity() {
|
||||
return Ok(());
|
||||
}
|
||||
// Exact growth prevents a geometric allocation from exceeding the available quota.
|
||||
let bytes = b.bytes::<T>(required, q)?;
|
||||
let mut extra = b.reserve(q, bytes - self.charge.amount)?;
|
||||
let _temporary = b.reserve(RuntimeResource::TemporaryBytes, self.charge.amount)?;
|
||||
let mut candidate = VecDeque::new();
|
||||
candidate
|
||||
.try_reserve_exact(required)
|
||||
.map_err(|_| b.failure(q, ResourceReason::AllocationFailed, bytes))?;
|
||||
let actual = b.bytes::<T>(candidate.capacity(), q)?;
|
||||
// If the allocator reports excess capacity, reject before publishing the new buffer.
|
||||
extra.resize(actual - self.charge.amount)?;
|
||||
candidate.append(&mut self.data);
|
||||
self.data = candidate;
|
||||
self.charge.absorb(extra);
|
||||
Ok(())
|
||||
}
|
||||
pub fn push_back(&mut self, value: T) -> Result<(), ResourceFailure> {
|
||||
self.reserve(1)?;
|
||||
self.data.push_back(value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn insert(&mut self, index: usize, value: T) -> Result<(), ResourceFailure> {
|
||||
self.reserve(1)?;
|
||||
self.data.insert(index, value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn partition_point(&self, predicate: impl FnMut(&T) -> bool) -> usize {
|
||||
self.data.partition_point(predicate)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use RuntimeResource::*;
|
||||
|
||||
#[test]
|
||||
fn accepted_limits_are_fixed_and_can_only_be_lowered() {
|
||||
let limits = RuntimeLimits::default();
|
||||
for (resource, expected) in [
|
||||
(GuestBytes, 268_435_456),
|
||||
(HandleSlots, 1_048_576),
|
||||
(AuxiliaryBytes, 67_108_864),
|
||||
(TemporaryBytes, 67_108_864),
|
||||
(StringBytes, 67_108_864),
|
||||
(StringAllocations, 262_144),
|
||||
(IndividualStringBytes, 4_194_304),
|
||||
(CallDepth, 1_024),
|
||||
] {
|
||||
assert_eq!(limits.get(resource), expected);
|
||||
assert!(limits.clone().lowered(resource, expected + 1).is_err());
|
||||
assert_eq!(limits.clone().lowered(resource, 0).unwrap().get(resource), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_reservations_refund_on_failure_and_follow_their_owner() {
|
||||
let budget = RuntimeBudget::new(RuntimeLimits::default().lowered(GuestBytes, 10).unwrap());
|
||||
let charge = budget.reserve(GuestBytes, 7).unwrap();
|
||||
let shared = budget.clone();
|
||||
let failure = shared.reserve(GuestBytes, 4).unwrap_err();
|
||||
assert_eq!((failure.resource, failure.requested, failure.limit), (GuestBytes, 11, 10));
|
||||
assert_eq!(budget.used(GuestBytes), 7);
|
||||
let transferred = charge;
|
||||
assert_eq!(shared.used(GuestBytes), 7);
|
||||
let tail = shared.reserve(GuestBytes, 3).unwrap();
|
||||
assert_eq!(shared.used(GuestBytes), 10);
|
||||
drop(transferred);
|
||||
assert_eq!(shared.used(GuestBytes), 3);
|
||||
drop(tail);
|
||||
assert_eq!(shared.used(GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn queue_capacity_is_retained_until_drop_and_transfer_does_not_charge_twice() {
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(AuxiliaryBytes, 16).unwrap());
|
||||
let mut queue = BudgetQueue::<u64>::new(&budget);
|
||||
queue.push_back(11).unwrap();
|
||||
queue.push_back(22).unwrap();
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 16);
|
||||
assert!(queue.push_back(33).is_err());
|
||||
assert_eq!(queue.pop_front(), Some(11));
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 16);
|
||||
queue.push_back(44).unwrap(); // reuse retained capacity
|
||||
let mut moved = queue;
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 16);
|
||||
assert_eq!(moved.pop_front(), Some(22));
|
||||
assert_eq!(moved.pop_front(), Some(44));
|
||||
drop(moved);
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 0);
|
||||
assert_eq!(budget.used(TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn temporary_growth_refusal_keeps_old_buffer_and_refunds_provisional_charge() {
|
||||
let limits = RuntimeLimits::default()
|
||||
.lowered(AuxiliaryBytes, 16)
|
||||
.unwrap()
|
||||
.lowered(TemporaryBytes, 0)
|
||||
.unwrap();
|
||||
let budget = RuntimeBudget::new(limits);
|
||||
let mut queue = BudgetQueue::<u64>::new(&budget);
|
||||
queue.push_back(11).unwrap();
|
||||
let error = queue.push_back(22).unwrap_err();
|
||||
assert_eq!((error.resource, error.requested), (TemporaryBytes, 8));
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 8);
|
||||
assert_eq!(queue.pop_front(), Some(11));
|
||||
assert_eq!(queue.pop_front(), None);
|
||||
assert_eq!(budget.used(TemporaryBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_and_length_overflow_fail_before_growth() {
|
||||
let budget = RuntimeBudget::default();
|
||||
assert_eq!(
|
||||
budget.bytes::<u64>(usize::MAX, AuxiliaryBytes).unwrap_err().reason,
|
||||
ResourceReason::SizeOverflow
|
||||
);
|
||||
let mut queue = BudgetQueue::<u64>::new(&budget);
|
||||
queue.push_back(11).unwrap();
|
||||
assert_eq!(queue.reserve(usize::MAX).unwrap_err().reason, ResourceReason::SizeOverflow);
|
||||
assert_eq!(budget.used(AuxiliaryBytes), 8);
|
||||
assert_eq!(queue.pop_front(), Some(11));
|
||||
}
|
||||
}
|
||||
|
||||
/// Contiguous execution storage. The capacity reservation travels with the buffer
|
||||
/// when a coroutine suspends/resumes, and is refunded only when storage is dropped.
|
||||
#[derive(Debug)]
|
||||
pub struct BudgetVec<T> {
|
||||
data: Vec<T>,
|
||||
charge: Reservation,
|
||||
}
|
||||
impl<T> BudgetVec<T> {
|
||||
pub fn new(budget: &RuntimeBudget, resource: RuntimeResource) -> Self {
|
||||
Self { data: Vec::new(), charge: budget.empty_charge(resource) }
|
||||
}
|
||||
pub(crate) fn prepare_growth(
|
||||
&self,
|
||||
additional: usize,
|
||||
) -> Result<Option<VecGrowth<T>>, ResourceFailure> {
|
||||
let b = &self.charge.budget;
|
||||
let q = self.charge.resource;
|
||||
let required = self
|
||||
.data
|
||||
.len()
|
||||
.checked_add(additional)
|
||||
.ok_or_else(|| b.failure(q, ResourceReason::SizeOverflow, u64::MAX))?;
|
||||
if required <= self.data.capacity() {
|
||||
return Ok(None);
|
||||
}
|
||||
let bytes = b.bytes::<T>(required, q)?;
|
||||
let mut extra = b.reserve(q, bytes - self.charge.amount)?;
|
||||
let temporary = b.reserve(RuntimeResource::TemporaryBytes, self.charge.amount)?;
|
||||
let mut candidate = Vec::new();
|
||||
candidate
|
||||
.try_reserve_exact(required)
|
||||
.map_err(|_| b.failure(q, ResourceReason::AllocationFailed, bytes))?;
|
||||
extra.resize(b.bytes::<T>(candidate.capacity(), q)? - self.charge.amount)?;
|
||||
Ok(Some(VecGrowth {
|
||||
candidate,
|
||||
extra,
|
||||
_temporary: temporary,
|
||||
old_capacity: self.data.capacity(),
|
||||
}))
|
||||
}
|
||||
pub(crate) fn commit_growth(&mut self, growth: Option<VecGrowth<T>>) {
|
||||
if let Some(mut growth) = growth {
|
||||
assert_eq!(self.data.capacity(), growth.old_capacity);
|
||||
growth.candidate.append(&mut self.data);
|
||||
self.data = growth.candidate;
|
||||
self.charge.absorb(growth.extra);
|
||||
}
|
||||
}
|
||||
pub fn reserve(&mut self, additional: usize) -> Result<(), ResourceFailure> {
|
||||
let growth = self.prepare_growth(additional)?;
|
||||
self.commit_growth(growth);
|
||||
Ok(())
|
||||
}
|
||||
pub(crate) fn accounted_bytes(&self) -> u64 {
|
||||
self.charge.amount
|
||||
}
|
||||
pub(crate) fn from_slice(
|
||||
budget: &RuntimeBudget,
|
||||
resource: RuntimeResource,
|
||||
values: &[T],
|
||||
) -> Result<Self, ResourceFailure>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
let mut result = Self::new(budget, resource);
|
||||
result.reserve(values.len())?;
|
||||
result.data.extend_from_slice(values);
|
||||
Ok(result)
|
||||
}
|
||||
pub fn push(&mut self, value: T) -> Result<(), ResourceFailure> {
|
||||
self.reserve(1)?;
|
||||
self.data.push(value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn resize(&mut self, len: usize, value: T) -> Result<(), ResourceFailure>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
self.reserve(len.saturating_sub(self.data.len()))?;
|
||||
self.data.resize(len, value);
|
||||
Ok(())
|
||||
}
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.data.pop()
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.data.clear();
|
||||
}
|
||||
pub fn truncate(&mut self, len: usize) {
|
||||
self.data.truncate(len);
|
||||
}
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.data.capacity()
|
||||
}
|
||||
pub fn as_slice(&self) -> &[T] {
|
||||
&self.data
|
||||
}
|
||||
pub fn drain(&mut self, range: impl std::ops::RangeBounds<usize>) -> std::vec::Drain<'_, T> {
|
||||
self.data.drain(range)
|
||||
}
|
||||
pub fn take(&mut self) -> Self {
|
||||
let empty = Self::new(&self.charge.budget, self.charge.resource);
|
||||
std::mem::replace(self, empty)
|
||||
}
|
||||
/// Only used with the bounded native result writer after reserving its result slots.
|
||||
pub(crate) fn native_result_buffer(&mut self) -> &mut Vec<T> {
|
||||
&mut self.data
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub(crate) fn fixture(data: Vec<T>) -> Self {
|
||||
let budget = RuntimeBudget::default();
|
||||
let bytes = budget.bytes::<T>(data.capacity(), RuntimeResource::AuxiliaryBytes).unwrap();
|
||||
let charge = budget.reserve(RuntimeResource::AuxiliaryBytes, bytes).unwrap();
|
||||
Self { data, charge }
|
||||
}
|
||||
}
|
||||
impl<T> std::ops::Deref for BudgetVec<T> {
|
||||
type Target = [T];
|
||||
fn deref(&self) -> &[T] {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
impl<T> std::ops::DerefMut for BudgetVec<T> {
|
||||
fn deref_mut(&mut self) -> &mut [T] {
|
||||
&mut self.data
|
||||
}
|
||||
}
|
||||
impl<'a, T> IntoIterator for &'a BudgetVec<T> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = std::slice::Iter<'a, T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.data.iter()
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
impl<T: PartialEq> PartialEq<Vec<T>> for BudgetVec<T> {
|
||||
fn eq(&self, other: &Vec<T>) -> bool {
|
||||
&self.data == other
|
||||
}
|
||||
}
|
||||
|
||||
/// Unpublished growth can be dropped to refund every provisional reservation.
|
||||
pub(crate) struct VecGrowth<T> {
|
||||
candidate: Vec<T>,
|
||||
extra: Reservation,
|
||||
_temporary: Reservation,
|
||||
old_capacity: usize,
|
||||
}
|
||||
@@ -7,9 +7,13 @@ pub trait RootVisitor {
|
||||
}
|
||||
|
||||
/// Helper: if `val` is a `Value::HeapRef`, call the visitor.
|
||||
pub(crate) fn value_heap_ref(value: &Value) -> Option<HeapRef> {
|
||||
if let Value::HeapRef(r) = value { Some(*r) } else { None }
|
||||
}
|
||||
|
||||
pub fn visit_value_for_roots<V: RootVisitor + ?Sized>(val: &Value, visitor: &mut V) {
|
||||
if let Value::HeapRef(r) = val {
|
||||
visitor.visit_heap_ref(*r);
|
||||
if let Some(r) = value_heap_ref(val) {
|
||||
visitor.visit_heap_ref(r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +35,11 @@ mod tests {
|
||||
fn visits_heapref_on_operand_stack() {
|
||||
let mut vm = VirtualMachine::default();
|
||||
// Place a HeapRef on the operand stack
|
||||
vm.push_operand_for_test(Value::HeapRef(HeapRef(123)));
|
||||
vm.push_operand_for_test(Value::HeapRef(HeapRef { index: 123, generation: 1 }));
|
||||
|
||||
let mut v = CollectVisitor { seen: vec![] };
|
||||
vm.visit_roots(&mut v);
|
||||
|
||||
assert_eq!(v.seen, vec![HeapRef(123)]);
|
||||
assert_eq!(v.seen, vec![HeapRef { index: 123, generation: 1 }]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use crate::resource_budget::{BudgetQueue, ResourceFailure, RuntimeBudget};
|
||||
|
||||
use prometeu_bytecode::HeapRef;
|
||||
|
||||
@@ -8,12 +8,12 @@ use prometeu_bytecode::HeapRef;
|
||||
/// - FIFO for ready coroutines.
|
||||
/// - Sleeping coroutines are ordered by `wake_tick` and moved to ready when `wake_tick <= current_tick`.
|
||||
/// - No randomness, no preemption, no context switching here.
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug)]
|
||||
pub struct Scheduler {
|
||||
/// Queue of runnable coroutines (FIFO)
|
||||
ready_queue: VecDeque<HeapRef>,
|
||||
ready_queue: BudgetQueue<HeapRef>,
|
||||
/// Sleeping list kept sorted by (wake_tick, insertion_order)
|
||||
sleeping: Vec<SleepEntry>,
|
||||
sleeping: BudgetQueue<SleepEntry>,
|
||||
/// Currently selected coroutine (purely informational here)
|
||||
current: Option<HeapRef>,
|
||||
/// Monotonic counter to preserve deterministic order for same wake_tick
|
||||
@@ -28,15 +28,34 @@ struct SleepEntry {
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn capacity_bytes(&self) -> u64 {
|
||||
self.ready_queue.capacity_bytes() + self.sleeping.capacity_bytes()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
Self::with_budget(&RuntimeBudget::default())
|
||||
}
|
||||
|
||||
pub fn with_budget(budget: &RuntimeBudget) -> Self {
|
||||
Self {
|
||||
ready_queue: BudgetQueue::new(budget),
|
||||
sleeping: BudgetQueue::new(budget),
|
||||
current: None,
|
||||
next_seq: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reserve_ready(&mut self) -> Result<(), ResourceFailure> {
|
||||
self.ready_queue.reserve(1)
|
||||
}
|
||||
|
||||
// ---------- Ready queue operations ----------
|
||||
|
||||
/// Enqueue a coroutine to the ready FIFO.
|
||||
pub fn enqueue_ready(&mut self, coro: HeapRef) {
|
||||
self.ready_queue.push_back(coro);
|
||||
pub fn enqueue_ready(&mut self, coro: HeapRef) -> Result<(), ResourceFailure> {
|
||||
self.ready_queue.push_back(coro)
|
||||
}
|
||||
|
||||
/// Dequeue next ready coroutine (front of FIFO).
|
||||
@@ -70,36 +89,25 @@ impl Scheduler {
|
||||
/// Put a coroutine to sleep until `wake_tick`.
|
||||
/// A coroutine is woken when `current_tick >= wake_tick`.
|
||||
/// The sleeping list is kept stably ordered to guarantee determinism.
|
||||
pub fn sleep_until(&mut self, coro: HeapRef, wake_tick: u64) {
|
||||
pub fn sleep_until(&mut self, coro: HeapRef, wake_tick: u64) -> Result<(), ResourceFailure> {
|
||||
let entry = SleepEntry { wake_tick, seq: self.next_seq, coro };
|
||||
let idx =
|
||||
self.sleeping.partition_point(|e| (e.wake_tick, e.seq) <= (entry.wake_tick, entry.seq));
|
||||
self.sleeping.insert(idx, entry)?;
|
||||
self.next_seq = self.next_seq.wrapping_add(1);
|
||||
|
||||
// Binary search insertion point by wake_tick, then by seq to keep total order deterministic
|
||||
let idx = match self.sleeping.binary_search_by(|e| {
|
||||
if e.wake_tick == entry.wake_tick {
|
||||
e.seq.cmp(&entry.seq)
|
||||
} else {
|
||||
e.wake_tick.cmp(&entry.wake_tick)
|
||||
}
|
||||
}) {
|
||||
Ok(i) => i, // equal element position; insert after to preserve FIFO among equals
|
||||
Err(i) => i,
|
||||
};
|
||||
self.sleeping.insert(idx, entry);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Move all sleeping coroutines with `wake_tick <= current_tick` to ready queue (FIFO by wake order).
|
||||
pub fn wake_ready(&mut self, current_tick: u64) {
|
||||
// Find split point where wake_tick > current_tick
|
||||
/// Reserve the destination before removing any sleeping entries. A failed
|
||||
/// transfer preserves both queues and never loses a runnable coroutine.
|
||||
pub fn wake_ready(&mut self, current_tick: u64) -> Result<(), ResourceFailure> {
|
||||
let split = self.sleeping.partition_point(|e| e.wake_tick <= current_tick);
|
||||
if split == 0 {
|
||||
return;
|
||||
}
|
||||
let mut ready_slice: Vec<SleepEntry> = self.sleeping.drain(0..split).collect();
|
||||
// Already in order by (wake_tick, seq); push in that order to preserve determinism
|
||||
for e in ready_slice.drain(..) {
|
||||
self.ready_queue.push_back(e.coro);
|
||||
self.ready_queue.reserve(split)?;
|
||||
for _ in 0..split {
|
||||
let entry = self.sleeping.pop_front().expect("validated sleeping prefix");
|
||||
self.ready_queue.push_back(entry.coro)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns true if there are any sleeping coroutines.
|
||||
@@ -113,15 +121,15 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hr(id: u32) -> HeapRef {
|
||||
HeapRef(id)
|
||||
HeapRef { index: id, generation: 1 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fifo_ready_queue_is_deterministic() {
|
||||
let mut s = Scheduler::new();
|
||||
s.enqueue_ready(hr(1));
|
||||
s.enqueue_ready(hr(2));
|
||||
s.enqueue_ready(hr(3));
|
||||
s.enqueue_ready(hr(1)).unwrap();
|
||||
s.enqueue_ready(hr(2)).unwrap();
|
||||
s.enqueue_ready(hr(3)).unwrap();
|
||||
|
||||
assert_eq!(s.ready_len(), 3);
|
||||
assert_eq!(s.dequeue_next(), Some(hr(1)));
|
||||
@@ -133,23 +141,44 @@ mod tests {
|
||||
#[test]
|
||||
fn sleeping_wake_is_stable_and_fifo_by_insertion() {
|
||||
let mut s = Scheduler::new();
|
||||
s.sleep_until(hr(10), 5); // first with wake at 5
|
||||
s.sleep_until(hr(11), 5); // second with same wake
|
||||
s.sleep_until(hr(12), 6); // later wake
|
||||
s.sleep_until(hr(10), 5).unwrap(); // first with wake at 5
|
||||
s.sleep_until(hr(11), 5).unwrap(); // second with same wake
|
||||
s.sleep_until(hr(12), 6).unwrap(); // later wake
|
||||
|
||||
// Before tick 5: nothing wakes
|
||||
s.wake_ready(4);
|
||||
s.wake_ready(4).unwrap();
|
||||
assert!(s.is_ready_empty());
|
||||
|
||||
// At tick 5: first two wake in insertion order
|
||||
s.wake_ready(5);
|
||||
s.wake_ready(5).unwrap();
|
||||
assert_eq!(s.dequeue_next(), Some(hr(10)));
|
||||
assert_eq!(s.dequeue_next(), Some(hr(11)));
|
||||
assert!(s.is_ready_empty());
|
||||
|
||||
// At tick 6: last one wakes
|
||||
s.wake_ready(6);
|
||||
s.wake_ready(6).unwrap();
|
||||
assert_eq!(s.dequeue_next(), Some(hr(12)));
|
||||
assert!(s.is_ready_empty());
|
||||
}
|
||||
#[test]
|
||||
fn wake_refusal_preserves_sleepers_and_fifo_order() {
|
||||
use crate::resource_budget::{RuntimeLimits, RuntimeResource};
|
||||
let budget = RuntimeBudget::new(
|
||||
RuntimeLimits::default()
|
||||
.lowered(RuntimeResource::AuxiliaryBytes, (2 * size_of::<SleepEntry>()) as u64)
|
||||
.unwrap(),
|
||||
);
|
||||
let mut s = Scheduler::with_budget(&budget);
|
||||
s.sleep_until(hr(1), 4).unwrap();
|
||||
s.sleep_until(hr(2), 4).unwrap();
|
||||
let charged = budget.used(RuntimeResource::AuxiliaryBytes);
|
||||
assert!(s.wake_ready(4).is_err());
|
||||
assert_eq!(budget.used(RuntimeResource::AuxiliaryBytes), charged);
|
||||
assert_eq!(budget.used(RuntimeResource::TemporaryBytes), 0);
|
||||
assert!(s.is_ready_empty());
|
||||
assert_eq!(s.sleeping.pop_front().unwrap().coro, hr(1));
|
||||
assert_eq!(s.sleeping.pop_front().unwrap().coro, hr(2));
|
||||
drop(s);
|
||||
assert_eq!(budget.used(RuntimeResource::AuxiliaryBytes), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
//! Compact native field/element storage backed by resolved Registry layouts.
|
||||
//! No collection opcodes: consumers use indices and the shared value contract.
|
||||
mod allocation;
|
||||
use crate::resource_budget::{ResourceReason, RuntimeBudget, RuntimeResource as Q};
|
||||
use crate::value_contract::{ValueError, ValueGroup, u32_carrier};
|
||||
use allocation::Allocation;
|
||||
use prometeu_bytecode::registry::{
|
||||
BuiltinId as B, DynamicSlot, Ownership, Registry, StringSlot, TypeId, TypeKind,
|
||||
};
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct TraitSlot {
|
||||
receiver: HeapRef,
|
||||
implementation: u32,
|
||||
padding: u32,
|
||||
}
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct PixelSlot {
|
||||
x: i64,
|
||||
y: i64,
|
||||
color: u32,
|
||||
padding: u32,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct TypedStorage {
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
// None for a Struct instance, Some(count) for an Array instance.
|
||||
count: Option<usize>,
|
||||
initialized: usize,
|
||||
data: Allocation,
|
||||
}
|
||||
impl TypedStorage {
|
||||
pub fn new_struct(
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ValueError> {
|
||||
let layout = registry
|
||||
.type_layout(type_id)
|
||||
.and_then(|l| l.instance.as_ref())
|
||||
.ok_or(ValueError::Type)?;
|
||||
let data = Allocation::new(layout.size, layout.alignment, budget)?;
|
||||
let mut storage = Self { registry, type_id, count: None, initialized: 0, data };
|
||||
storage.initialize();
|
||||
Ok(storage)
|
||||
}
|
||||
pub fn new_array(
|
||||
registry: Arc<Registry>,
|
||||
type_id: TypeId,
|
||||
count: u32,
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ValueError> {
|
||||
let layout = registry.type_layout(type_id).and_then(|l| l.array).ok_or(ValueError::Type)?;
|
||||
let size = layout
|
||||
.stride
|
||||
.checked_mul(count as usize)
|
||||
.ok_or_else(|| budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, u64::MAX))?;
|
||||
let data = Allocation::new(size, layout.element_alignment, budget)?;
|
||||
let mut storage =
|
||||
Self { registry, type_id, count: Some(count as usize), initialized: 0, data };
|
||||
storage.initialize();
|
||||
Ok(storage)
|
||||
}
|
||||
pub fn registry(&self) -> &Registry {
|
||||
&self.registry
|
||||
}
|
||||
pub fn type_id(&self) -> TypeId {
|
||||
self.type_id
|
||||
}
|
||||
pub fn bytes(&self) -> u64 {
|
||||
self.data.bytes()
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.count.unwrap_or_else(|| {
|
||||
self.registry.type_layout(self.type_id).unwrap().instance.as_ref().unwrap().fields.len()
|
||||
})
|
||||
}
|
||||
fn slot(&self, index: usize) -> Result<(TypeId, usize), ValueError> {
|
||||
if index >= self.len() {
|
||||
return Err(ValueError::Bounds);
|
||||
}
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
if let Some(array) = layout.array {
|
||||
Ok((array.element_type_id, array.stride * index))
|
||||
} else {
|
||||
let field = layout.instance.as_ref().unwrap().fields[index];
|
||||
Ok((field.type_id, field.offset))
|
||||
}
|
||||
}
|
||||
pub fn slot_type(&self, index: usize) -> Result<TypeId, ValueError> {
|
||||
self.slot(index).map(|s| s.0)
|
||||
}
|
||||
|
||||
fn initialize(&mut self) {
|
||||
for index in 0..self.len() {
|
||||
let (ty, offset) = self.slot(index).unwrap();
|
||||
// SAFETY: fresh resolved nonoverlapping field/element, no publication
|
||||
// until every native value is constructed. Count advances only after init.
|
||||
unsafe {
|
||||
match self.registry.declarations().types[ty.0 as usize].kind {
|
||||
TypeKind::Struct(_) | TypeKind::Array(_) => {
|
||||
self.data.init(offset, HeapRef::NULL)
|
||||
}
|
||||
TypeKind::Trait(_) => self.data.init(
|
||||
offset,
|
||||
TraitSlot { receiver: HeapRef::NULL, implementation: 0, padding: 0 },
|
||||
),
|
||||
TypeKind::Builtin(id) => match id {
|
||||
B::INT32 => self.data.init(offset, 0_i32),
|
||||
B::INT64 => self.data.init(offset, 0_i64),
|
||||
B::FLOAT => self.data.init(offset, 0_f64),
|
||||
B::BOOL => self.data.init(offset, 0_u8),
|
||||
B::COLOR => self.data.init(offset, 0_u32),
|
||||
B::VEC2 => self.data.init(offset, [0_f64; 2]),
|
||||
B::PIXEL => {
|
||||
self.data.init(offset, PixelSlot { x: 0, y: 0, color: 0, padding: 0 })
|
||||
}
|
||||
B::STRING => self.data.init::<StringSlot>(offset, None),
|
||||
B::DYNAMIC => self.data.init(offset, DynamicSlot::default()),
|
||||
_ => unreachable!("resolved builtin"),
|
||||
},
|
||||
}
|
||||
}
|
||||
self.initialized += 1;
|
||||
}
|
||||
}
|
||||
pub fn read(&self, index: usize) -> Result<ValueGroup, ValueError> {
|
||||
let (ty, offset) = self.slot(index)?;
|
||||
// SAFETY: private backing storage was initialized according to this exact
|
||||
// immutable Registry. Only matching typed writes can change its contents.
|
||||
let result = unsafe {
|
||||
match self.registry.declarations().types[ty.0 as usize].kind {
|
||||
TypeKind::Struct(_) | TypeKind::Array(_) => {
|
||||
let r = *self.data.get::<HeapRef>(offset);
|
||||
ValueGroup::new(&[if r.is_null() { Value::Null } else { Value::HeapRef(r) }])
|
||||
}
|
||||
TypeKind::Trait(_) => {
|
||||
let t = self.data.get::<TraitSlot>(offset);
|
||||
if t.receiver.is_null() {
|
||||
if t.implementation != 0 {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
ValueGroup::new(&[Value::Null, Value::Null])
|
||||
} else {
|
||||
ValueGroup::new(&[
|
||||
Value::HeapRef(t.receiver),
|
||||
Value::Int64(t.implementation.into()),
|
||||
])
|
||||
}
|
||||
}
|
||||
TypeKind::Builtin(id) => match id {
|
||||
B::INT32 => ValueGroup::new(&[Value::Int32(*self.data.get(offset))]),
|
||||
B::INT64 => ValueGroup::new(&[Value::Int64(*self.data.get(offset))]),
|
||||
B::FLOAT => ValueGroup::new(&[Value::Float(*self.data.get(offset))]),
|
||||
B::BOOL => match *self.data.get::<u8>(offset) {
|
||||
0 => ValueGroup::new(&[Value::Boolean(false)]),
|
||||
1 => ValueGroup::new(&[Value::Boolean(true)]),
|
||||
_ => return Err(ValueError::Type),
|
||||
},
|
||||
B::COLOR => {
|
||||
ValueGroup::new(&[Value::Int64((*self.data.get::<u32>(offset)).into())])
|
||||
}
|
||||
B::VEC2 => {
|
||||
let [x, y] = *self.data.get::<[f64; 2]>(offset);
|
||||
ValueGroup::new(&[Value::Float(x), Value::Float(y)])
|
||||
}
|
||||
B::PIXEL => {
|
||||
let p = self.data.get::<PixelSlot>(offset);
|
||||
ValueGroup::new(&[
|
||||
Value::Int64(p.x),
|
||||
Value::Int64(p.y),
|
||||
Value::Int64(p.color.into()),
|
||||
])
|
||||
}
|
||||
B::STRING => ValueGroup::new(&[self
|
||||
.data
|
||||
.get::<StringSlot>(offset)
|
||||
.as_ref()
|
||||
.map_or(Value::Null, |s| Value::String(s.clone()))]),
|
||||
B::DYNAMIC => {
|
||||
let d = self.data.get::<DynamicSlot>(offset);
|
||||
ValueGroup::new(&[
|
||||
d.type_id.map_or(Value::Null, |t| Value::Int64(t.0.into())),
|
||||
d.payload.clone(),
|
||||
])
|
||||
}
|
||||
_ => unreachable!("resolved builtin"),
|
||||
},
|
||||
}
|
||||
};
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Heap validates the complete group and provenance before calling this.
|
||||
/// All matching checks/conversions happen before replacement, so an error
|
||||
/// preserves both the old value and its native owners.
|
||||
pub fn write_validated(&mut self, index: usize, values: &[Value]) -> Result<(), ValueError> {
|
||||
let (ty, offset) = self.slot(index)?;
|
||||
// SAFETY: each arm uses the same resident type as initialize/read/drop;
|
||||
// &mut self gives exclusive access. Native assignment drops exactly once.
|
||||
unsafe {
|
||||
match (&self.registry.declarations().types[ty.0 as usize].kind, values) {
|
||||
(TypeKind::Struct(_) | TypeKind::Array(_), [Value::Null]) => {
|
||||
self.data.replace(offset, HeapRef::NULL)
|
||||
}
|
||||
(TypeKind::Struct(_) | TypeKind::Array(_), [Value::HeapRef(r)]) => {
|
||||
self.data.replace(offset, *r)
|
||||
}
|
||||
(TypeKind::Trait(_), [Value::Null, Value::Null]) => self.data.replace(
|
||||
offset,
|
||||
TraitSlot { receiver: HeapRef::NULL, implementation: 0, padding: 0 },
|
||||
),
|
||||
(TypeKind::Trait(_), [Value::HeapRef(r), id]) => self.data.replace(
|
||||
offset,
|
||||
TraitSlot { receiver: *r, implementation: u32_carrier(id)?, padding: 0 },
|
||||
),
|
||||
(TypeKind::Builtin(B::INT32), [Value::Int32(v)]) => self.data.replace(offset, *v),
|
||||
(TypeKind::Builtin(B::INT64), [Value::Int64(v)]) => self.data.replace(offset, *v),
|
||||
(TypeKind::Builtin(B::FLOAT), [Value::Float(v)]) => self.data.replace(offset, *v),
|
||||
(TypeKind::Builtin(B::BOOL), [Value::Boolean(v)]) => {
|
||||
self.data.replace(offset, u8::from(*v))
|
||||
}
|
||||
(TypeKind::Builtin(B::COLOR), [v]) => self.data.replace(offset, u32_carrier(v)?),
|
||||
(TypeKind::Builtin(B::VEC2), [Value::Float(x), Value::Float(y)]) => {
|
||||
self.data.replace(offset, [*x, *y])
|
||||
}
|
||||
(TypeKind::Builtin(B::PIXEL), [Value::Int64(x), Value::Int64(y), color]) => {
|
||||
self.data.replace(
|
||||
offset,
|
||||
PixelSlot { x: *x, y: *y, color: u32_carrier(color)?, padding: 0 },
|
||||
)
|
||||
}
|
||||
(TypeKind::Builtin(B::STRING), [Value::Null]) => {
|
||||
self.data.replace::<StringSlot>(offset, None)
|
||||
}
|
||||
(TypeKind::Builtin(B::STRING), [Value::String(s)]) => {
|
||||
self.data.replace::<StringSlot>(offset, Some(s.clone()))
|
||||
}
|
||||
(TypeKind::Builtin(B::DYNAMIC), [tag, payload]) => {
|
||||
let type_id = if matches!(tag, Value::Null) {
|
||||
None
|
||||
} else {
|
||||
Some(TypeId(u32_carrier(tag)?))
|
||||
};
|
||||
self.data.replace(offset, DynamicSlot { type_id, payload: payload.clone() });
|
||||
}
|
||||
_ => return Err(ValueError::Type),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Iterates only resolved ownership-map entries; padding/scalar fields are
|
||||
/// never interpreted as references. No temporary root buffer is allocated.
|
||||
pub fn root_count(&self) -> usize {
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
if let Some(array) = layout.array {
|
||||
let owner = self.registry.type_layout(array.element_type_id).unwrap().storage.ownership;
|
||||
if matches!(
|
||||
owner,
|
||||
Ownership::GuestReference | Ownership::TraitReceiver | Ownership::Dynamic
|
||||
) {
|
||||
self.len()
|
||||
} else {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
layout.instance.as_ref().unwrap().owned_fields.len()
|
||||
}
|
||||
}
|
||||
pub fn root_at(&self, index: usize) -> Option<HeapRef> {
|
||||
assert!(index < self.root_count());
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
let (offset, ownership) = if let Some(array) = layout.array {
|
||||
(
|
||||
index * array.stride,
|
||||
self.registry.type_layout(array.element_type_id).unwrap().storage.ownership,
|
||||
)
|
||||
} else {
|
||||
let field = layout.instance.as_ref().unwrap().owned_fields[index];
|
||||
(field.offset, field.ownership)
|
||||
};
|
||||
// SAFETY: private caller indexes the resolved map within root_count();
|
||||
// resident native values were all constructed before heap publication.
|
||||
let reference = unsafe {
|
||||
match ownership {
|
||||
Ownership::GuestReference => Some(*self.data.get::<HeapRef>(offset)),
|
||||
Ownership::TraitReceiver => Some(self.data.get::<TraitSlot>(offset).receiver),
|
||||
Ownership::Dynamic => {
|
||||
crate::roots::value_heap_ref(&self.data.get::<DynamicSlot>(offset).payload)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
reference.filter(|r| !r.is_null())
|
||||
}
|
||||
}
|
||||
impl Drop for TypedStorage {
|
||||
fn drop(&mut self) {
|
||||
let layout = self.registry.type_layout(self.type_id).unwrap();
|
||||
if let Some(array) = layout.array {
|
||||
let ownership =
|
||||
self.registry.type_layout(array.element_type_id).unwrap().storage.ownership;
|
||||
if !matches!(ownership, Ownership::NativeString | Ownership::Dynamic) {
|
||||
return;
|
||||
}
|
||||
for index in 0..self.initialized {
|
||||
drop_native(&mut self.data, index * array.stride, ownership);
|
||||
}
|
||||
} else {
|
||||
let instance = layout.instance.as_ref().unwrap();
|
||||
// An interrupted constructor owns only the prefix before this offset.
|
||||
let end = instance.fields.get(self.initialized).map_or(instance.size, |f| f.offset);
|
||||
for field in &instance.owned_fields {
|
||||
if field.offset >= end {
|
||||
break;
|
||||
}
|
||||
drop_native(&mut self.data, field.offset, field.ownership);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn drop_native(data: &mut Allocation, offset: usize, ownership: Ownership) {
|
||||
// SAFETY: caller visits each initialized native ownership-map entry once.
|
||||
// Allocation::drop deallocates the backing memory only after this traversal.
|
||||
unsafe {
|
||||
match ownership {
|
||||
Ownership::NativeString => data.drop_at::<StringSlot>(offset),
|
||||
Ownership::Dynamic => data.drop_at::<DynamicSlot>(offset),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests;
|
||||
@@ -0,0 +1,87 @@
|
||||
//! Private aligned allocation. TypedStorage constructs/drops native owners;
|
||||
//! this owner only manages the backing allocation and its quota reservation.
|
||||
use crate::resource_budget::{
|
||||
Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
|
||||
};
|
||||
use std::alloc::{Layout, alloc_zeroed, dealloc};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct Allocation {
|
||||
ptr: NonNull<u8>,
|
||||
layout: Layout,
|
||||
charge: Reservation,
|
||||
}
|
||||
// The allocation is uniquely owned. Typed access is private to TypedStorage,
|
||||
// whose resident StringSlot/DynamicSlot/plain carriers are all Send + Sync.
|
||||
unsafe impl Send for Allocation {}
|
||||
unsafe impl Sync for Allocation {}
|
||||
impl Allocation {
|
||||
pub fn new(
|
||||
size: usize,
|
||||
alignment: usize,
|
||||
budget: &RuntimeBudget,
|
||||
) -> Result<Self, ResourceFailure> {
|
||||
let layout = Layout::from_size_align(size, alignment).map_err(|_| {
|
||||
budget.failure(Q::GuestBytes, ResourceReason::SizeOverflow, size as u64)
|
||||
})?;
|
||||
let charge = budget.reserve(Q::GuestBytes, size as u64)?;
|
||||
let ptr = if size == 0 {
|
||||
// Never dereferenced/deallocated; no element exists in an empty payload.
|
||||
NonNull::dangling()
|
||||
} else {
|
||||
// SAFETY: nonzero checked Layout; null is handled as a resource failure.
|
||||
NonNull::new(unsafe { alloc_zeroed(layout) }).ok_or_else(|| {
|
||||
budget.failure(Q::GuestBytes, ResourceReason::AllocationFailed, size as u64)
|
||||
})?
|
||||
};
|
||||
Ok(Self { ptr, layout, charge })
|
||||
}
|
||||
pub fn bytes(&self) -> u64 {
|
||||
self.charge.amount()
|
||||
}
|
||||
|
||||
fn at<T>(&self, offset: usize) -> *mut T {
|
||||
assert!(offset.checked_add(size_of::<T>()).is_some_and(|end| end <= self.layout.size()));
|
||||
assert!(self.layout.align() >= align_of::<T>());
|
||||
assert!(offset.is_multiple_of(align_of::<T>()));
|
||||
// SAFETY: checked bounds/alignment, nonzero accessed type lies in allocation.
|
||||
unsafe { self.ptr.as_ptr().add(offset).cast() }
|
||||
}
|
||||
/// Caller guarantees T is initialized and is the resident type at offset.
|
||||
pub unsafe fn get<T>(&self, offset: usize) -> &T {
|
||||
// SAFETY: caller's typed initialization invariant and checked address.
|
||||
unsafe { &*self.at::<T>(offset) }
|
||||
}
|
||||
/// Caller guarantees exclusive access and a valid initialized T at offset.
|
||||
pub unsafe fn replace<T>(&mut self, offset: usize, value: T) {
|
||||
// SAFETY: caller guarantees initialized matching type; assignment drops old owner.
|
||||
unsafe {
|
||||
*self.at::<T>(offset) = value;
|
||||
}
|
||||
}
|
||||
/// Caller guarantees the destination is uninitialized storage for exactly T.
|
||||
pub unsafe fn init<T>(&mut self, offset: usize, value: T) {
|
||||
// SAFETY: checked address, ownership is moved into previously unused storage.
|
||||
unsafe {
|
||||
self.at::<T>(offset).write(value);
|
||||
}
|
||||
}
|
||||
/// Caller guarantees an initialized T which will never be used again.
|
||||
pub unsafe fn drop_at<T>(&mut self, offset: usize) {
|
||||
// SAFETY: caller walks each initialized native slot exactly once.
|
||||
unsafe {
|
||||
std::ptr::drop_in_place(self.at::<T>(offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Drop for Allocation {
|
||||
fn drop(&mut self) {
|
||||
if self.layout.size() != 0 {
|
||||
// SAFETY: same pointer/Layout returned by alloc_zeroed, unique owner.
|
||||
unsafe {
|
||||
dealloc(self.ptr.as_ptr(), self.layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,528 @@
|
||||
use super::*;
|
||||
use crate::heap::{Heap, StoredObject};
|
||||
use crate::resource_budget::{ResourceReason, RuntimeLimits};
|
||||
use crate::value_contract::{self, ValueError as E};
|
||||
use prometeu_bytecode::load_limits::LoadBudget;
|
||||
use prometeu_bytecode::registry::{ImplementationDecl, NamedType, RegistryDeclarations, TypeDecl};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
const I32: TypeId = TypeId(0);
|
||||
const I64: TypeId = TypeId(1);
|
||||
const FLOAT: TypeId = TypeId(2);
|
||||
const BOOL: TypeId = TypeId(3);
|
||||
const COLOR: TypeId = TypeId(4);
|
||||
const VEC: TypeId = TypeId(5);
|
||||
const PIXEL: TypeId = TypeId(6);
|
||||
const STRING: TypeId = TypeId(7);
|
||||
const DYNAMIC: TypeId = TypeId(8);
|
||||
const ENEMY: TypeId = TypeId(9);
|
||||
const OTHER: TypeId = TypeId(10);
|
||||
const MIXED: TypeId = TypeId(11);
|
||||
const TRAIT: TypeId = TypeId(12);
|
||||
const INTS: TypeId = TypeId(13);
|
||||
const VECS: TypeId = TypeId(14);
|
||||
const REFS: TypeId = TypeId(15);
|
||||
const STRINGS: TypeId = TypeId(16);
|
||||
const TRAITS: TypeId = TypeId(17);
|
||||
const DYNAMICS: TypeId = TypeId(18);
|
||||
const BOOLS: TypeId = TypeId(19);
|
||||
const PIXELS: TypeId = TypeId(20);
|
||||
const FLOATS: TypeId = TypeId(21);
|
||||
const LONGS: TypeId = TypeId(22);
|
||||
const COLORS: TypeId = TypeId(23);
|
||||
const ARRAYS: TypeId = TypeId(24);
|
||||
|
||||
pub(crate) fn fixture_registry() -> Arc<Registry> {
|
||||
let mut types: Vec<_> =
|
||||
[B::INT32, B::INT64, B::FLOAT, B::BOOL, B::COLOR, B::VEC2, B::PIXEL, B::STRING, B::DYNAMIC]
|
||||
.into_iter()
|
||||
.map(TypeKind::Builtin)
|
||||
.collect();
|
||||
types.extend([
|
||||
TypeKind::Struct(vec![]),
|
||||
TypeKind::Struct(vec![]),
|
||||
TypeKind::Struct(
|
||||
[BOOL, I32, BOOL, STRING, DYNAMIC, TRAIT, ENEMY, VEC, PIXEL, INTS]
|
||||
.into_iter()
|
||||
.map(|type_id| NamedType { type_id, name: None })
|
||||
.collect(),
|
||||
),
|
||||
TypeKind::Trait(vec![]),
|
||||
]);
|
||||
types.extend(
|
||||
[I32, VEC, ENEMY, STRING, TRAIT, DYNAMIC, BOOL, PIXEL, FLOAT, I64, COLOR, INTS]
|
||||
.into_iter()
|
||||
.map(TypeKind::Array),
|
||||
);
|
||||
Arc::new(
|
||||
Registry::resolve(
|
||||
RegistryDeclarations {
|
||||
types: types.into_iter().map(|kind| TypeDecl { kind, name: None }).collect(),
|
||||
functions: vec![],
|
||||
implementations: vec![
|
||||
ImplementationDecl { owner: ENEMY, trait_id: TRAIT, methods: vec![] },
|
||||
ImplementationDecl { owner: OTHER, trait_id: TRAIT, methods: vec![] },
|
||||
],
|
||||
},
|
||||
0,
|
||||
&mut LoadBudget::new(Default::default()),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
fn values(heap: &Heap, registry: &Registry, r: HeapRef, i: usize) -> Vec<Value> {
|
||||
heap.load_typed(registry, r, i).unwrap().as_slice().to_vec()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_are_typed_compact_and_construct_no_referenced_objects() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
for array in
|
||||
[INTS, VECS, REFS, STRINGS, TRAITS, DYNAMICS, BOOLS, PIXELS, FLOATS, LONGS, COLORS, ARRAYS]
|
||||
{
|
||||
let before = budget.used(Q::GuestBytes);
|
||||
let r = heap.allocate_typed_array(registry.clone(), array, 3).unwrap();
|
||||
let layout = registry.type_layout(array).unwrap().array.unwrap();
|
||||
assert_eq!(
|
||||
budget.used(Q::GuestBytes) - before,
|
||||
(size_of::<StoredObject>() + 3 * layout.stride) as u64
|
||||
);
|
||||
let default =
|
||||
registry.type_layout(layout.element_type_id).unwrap().storage.default_values();
|
||||
for i in 0..3 {
|
||||
assert_eq!(values(&heap, ®istry, r, i), default);
|
||||
}
|
||||
}
|
||||
assert_eq!(heap.len(), 12); // No children, subarrays, boxes or wrappers.
|
||||
let mixed = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
let fields = ®istry.type_layout(MIXED).unwrap().instance.as_ref().unwrap().fields;
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
assert_eq!(
|
||||
values(&heap, ®istry, mixed, i),
|
||||
registry.type_layout(field.type_id).unwrap().storage.default_values()
|
||||
);
|
||||
}
|
||||
let empty = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
assert!(heap.is_valid(empty));
|
||||
assert_eq!(heap.load_typed(®istry, empty, 0).unwrap_err(), E::Bounds);
|
||||
assert_eq!(heap.used_bytes.load(Ordering::Relaxed) as u64, budget.used(Q::GuestBytes));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_scalar_roundtrips_preserve_float_bits_pixel_width_and_color_range() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
for (ty, input) in [
|
||||
(INTS, vec![Value::Int32(i32::MIN)]),
|
||||
(LONGS, vec![Value::Int64(i64::MAX)]),
|
||||
(BOOLS, vec![Value::Boolean(true)]),
|
||||
(COLORS, vec![Value::Int64(u32::MAX.into())]),
|
||||
(VECS, vec![Value::Float(-0.0), Value::Float(f64::INFINITY)]),
|
||||
(PIXELS, vec![Value::Int64(i64::MIN), Value::Int64(i64::MAX), Value::Int64(0x11223344)]),
|
||||
] {
|
||||
let r = heap.allocate_typed_array(registry.clone(), ty, 1).unwrap();
|
||||
heap.store_typed(®istry, r, 0, &input).unwrap();
|
||||
assert_eq!(values(&heap, ®istry, r, 0), input);
|
||||
}
|
||||
let r = heap.allocate_typed_array(registry.clone(), FLOATS, 1).unwrap();
|
||||
for bits in [0_u64, (-0.0_f64).to_bits(), 0x7ff8000000000123] {
|
||||
heap.store_typed(®istry, r, 0, &[Value::Float(f64::from_bits(bits))]).unwrap();
|
||||
let output = values(&heap, ®istry, r, 0);
|
||||
let Value::Float(v) = output[0] else { panic!("exact Float carrier") };
|
||||
assert_eq!(v.to_bits(), bits);
|
||||
}
|
||||
let r = heap.allocate_typed_array(registry.clone(), COLORS, 1).unwrap();
|
||||
for value in [
|
||||
Value::Int64(-1),
|
||||
Value::Int64(u32::MAX as i64 + 1),
|
||||
Value::Int32(0),
|
||||
Value::Float(0.0),
|
||||
Value::Null,
|
||||
] {
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &[value]).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(values(&heap, ®istry, r, 0), [Value::Int64(0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_reads_share_identity_and_failed_overwrite_keeps_the_old_owner() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let r = heap.allocate_typed_array(registry.clone(), STRINGS, 2).unwrap();
|
||||
let text: Arc<str> = Arc::from("hello");
|
||||
let input = [Value::String(text.clone())];
|
||||
heap.store_typed(®istry, r, 0, &input).unwrap();
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &[Value::Int64(1)]).unwrap_err(), E::Type);
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
let copy = heap.load_typed(®istry, r, 0).unwrap();
|
||||
let [Value::String(alias)] = copy.as_slice() else { panic!("String carrier") };
|
||||
assert!(Arc::ptr_eq(alias, &text));
|
||||
assert_eq!(Arc::strong_count(&text), 4);
|
||||
heap.store_typed(®istry, r, 0, &[Value::Null]).unwrap();
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
heap.store_typed(®istry, r, 1, &[Value::String(Arc::from(""))]).unwrap();
|
||||
assert!(
|
||||
matches!(values(&heap, ®istry, r, 1).as_slice(), [Value::String(s)] if s.is_empty())
|
||||
);
|
||||
drop(copy);
|
||||
drop(input);
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
heap.sweep();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn complete_trait_pairs_accept_implementation_zero_and_preserve_receiver_aliases() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let enemy = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let r = heap.allocate_typed_array(registry.clone(), TRAITS, 2).unwrap();
|
||||
let pair = [Value::HeapRef(enemy), Value::Int64(0)];
|
||||
heap.store_typed(®istry, r, 0, &pair).unwrap();
|
||||
assert_eq!(values(&heap, ®istry, r, 0), pair);
|
||||
for bad in [
|
||||
vec![Value::HeapRef(enemy), Value::Int64(1)],
|
||||
vec![Value::HeapRef(enemy), Value::Null],
|
||||
vec![Value::Null, Value::Int64(0)],
|
||||
vec![Value::HeapRef(HeapRef::NULL), Value::Int64(0)],
|
||||
vec![Value::HeapRef(enemy), Value::Int32(0)],
|
||||
vec![Value::HeapRef(enemy), Value::Int64(-1)],
|
||||
vec![Value::HeapRef(enemy)],
|
||||
] {
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(values(&heap, ®istry, r, 0), pair);
|
||||
heap.mark_from_roots([r]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(enemy));
|
||||
heap.store_typed(®istry, r, 0, &[Value::Null, Value::Null]).unwrap();
|
||||
heap.mark_from_roots([r]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(!heap.is_valid(enemy));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_checks_semantic_ids_and_direct_or_typed_null_payloads() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let r = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
let text: Arc<str> = Arc::from("shared");
|
||||
for input in [
|
||||
vec![Value::Int64(I32.0.into()), Value::Int32(17)],
|
||||
vec![Value::Int64(COLOR.0.into()), Value::Int64(0x11223344)],
|
||||
vec![Value::Int64(STRING.0.into()), Value::String(text.clone())],
|
||||
vec![Value::Int64(STRING.0.into()), Value::Null],
|
||||
vec![Value::Int64(TRAIT.0.into()), Value::Null],
|
||||
vec![Value::Int64(ENEMY.0.into()), Value::Null],
|
||||
vec![Value::Null, Value::Null],
|
||||
] {
|
||||
heap.store_typed(®istry, r, 0, &input).unwrap();
|
||||
assert_eq!(values(&heap, ®istry, r, 0), input);
|
||||
}
|
||||
for bad in [
|
||||
vec![Value::Null, Value::Int32(0)],
|
||||
vec![Value::Int64(I32.0.into()), Value::Int64(0)],
|
||||
vec![Value::Int64(I32.0.into()), Value::Null],
|
||||
vec![Value::Int64(DYNAMIC.0.into()), Value::Null],
|
||||
vec![Value::Int64(999), Value::Null],
|
||||
vec![Value::Int64(-1), Value::Null],
|
||||
vec![Value::Int32(0), Value::Int32(0)],
|
||||
vec![Value::Int64(VEC.0.into()), Value::Float(0.0)],
|
||||
] {
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(heap.len(), 1); // No box for typed null, no direct-value boxing.
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compact_reference_arrays_require_exact_type_live_generation_and_registry() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let refs = heap.allocate_typed_array(registry.clone(), REFS, 1).unwrap();
|
||||
let enemy = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let other = heap.allocate_typed_struct(registry.clone(), OTHER).unwrap();
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(enemy)]).unwrap();
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(other)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
refs,
|
||||
0,
|
||||
&[Value::HeapRef(HeapRef { index: enemy.index, generation: enemy.generation + 1 })]
|
||||
)
|
||||
.unwrap_err(),
|
||||
E::InvalidHandle
|
||||
);
|
||||
heap.mark_from_roots([refs]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(enemy));
|
||||
assert!(!heap.is_valid(other));
|
||||
let next = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
assert_eq!(next.index, other.index);
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(other)]).unwrap_err(),
|
||||
E::InvalidHandle
|
||||
);
|
||||
let foreign = fixture_registry();
|
||||
assert_eq!(heap.load_typed(&foreign, refs, 0).unwrap_err(), E::Type);
|
||||
let foreign_enemy = heap.allocate_typed_struct(foreign, ENEMY).unwrap();
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, refs, 0, &[Value::HeapRef(foreign_enemy)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert_eq!(values(&heap, ®istry, refs, 0), [Value::HeapRef(enemy)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_struct_gc_uses_reference_maps_and_drops_native_dynamic_strings() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let holder = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
let child = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let dead = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let text: Arc<str> = Arc::from("owner");
|
||||
heap.store_typed(®istry, holder, 3, &[Value::String(text.clone())]).unwrap();
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
holder,
|
||||
4,
|
||||
&[Value::Int64(STRING.0.into()), Value::String(text.clone())],
|
||||
)
|
||||
.unwrap();
|
||||
heap.store_typed(®istry, holder, 6, &[Value::HeapRef(child)]).unwrap();
|
||||
// Scalar bits resembling a handle must never retain the unrelated object.
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
holder,
|
||||
8,
|
||||
&[
|
||||
Value::Int64(((dead.generation as u64) << 32 | dead.index as u64) as i64),
|
||||
Value::Int64(0),
|
||||
Value::Int64(0),
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(Arc::strong_count(&text), 3);
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(child));
|
||||
assert!(!heap.is_valid(dead));
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_reference_roots_and_array_references_preserve_identity() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let outer = heap.allocate_typed_array(registry.clone(), ARRAYS, 1).unwrap();
|
||||
let child = heap.allocate_typed_array(registry.clone(), INTS, 1).unwrap();
|
||||
let root = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
heap.store_typed(®istry, outer, 0, &[Value::HeapRef(child)]).unwrap();
|
||||
heap.store_typed(®istry, root, 0, &[Value::Int64(ARRAYS.0.into()), Value::HeapRef(outer)])
|
||||
.unwrap();
|
||||
heap.mark_from_roots([root]).unwrap();
|
||||
heap.sweep();
|
||||
assert_eq!(heap.len(), 3);
|
||||
heap.store_typed(®istry, child, 0, &[Value::Int32(42)]).unwrap();
|
||||
let [Value::HeapRef(alias)] = values(&heap, ®istry, outer, 0)[..] else {
|
||||
panic!("reference")
|
||||
};
|
||||
assert_eq!(values(&heap, ®istry, alias, 0), [Value::Int32(42)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_bounds_null_and_wrong_category_are_distinct() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let empty = heap.allocate_typed_array(registry.clone(), STRINGS, 0).unwrap();
|
||||
assert_eq!(heap.load_typed(®istry, empty, 0).unwrap_err(), E::Bounds);
|
||||
assert_eq!(
|
||||
heap.store_typed(®istry, empty, usize::MAX, &[Value::Null]).unwrap_err(),
|
||||
E::Bounds
|
||||
);
|
||||
assert_eq!(heap.load_typed(®istry, HeapRef::NULL, 0).unwrap_err(), E::NullReference);
|
||||
let closure = heap.alloc_closure(0, &[]).unwrap();
|
||||
assert_eq!(
|
||||
value_contract::validate(®istry, &heap, ENEMY, &[Value::HeapRef(closure)]).unwrap_err(),
|
||||
E::Type
|
||||
);
|
||||
assert!(matches!(heap.allocate_typed_struct(registry.clone(), STRING), Err(E::Type)));
|
||||
assert!(matches!(heap.allocate_typed_array(registry, TRAIT, 1), Err(E::Type)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_slots_are_aligned_and_corrupt_compact_pairs_are_rejected() {
|
||||
let registry = fixture_registry();
|
||||
let mut mixed =
|
||||
TypedStorage::new_struct(registry.clone(), MIXED, &RuntimeBudget::default()).unwrap();
|
||||
let offset = mixed.slot(3).unwrap().1;
|
||||
// SAFETY: exact initialized StringSlot, as established by the immutable layout.
|
||||
let addr = unsafe { mixed.data.get::<StringSlot>(offset) } as *const _ as usize;
|
||||
assert_eq!(addr % align_of::<StringSlot>(), 0);
|
||||
let offset = mixed.slot(4).unwrap().1;
|
||||
let addr = unsafe { mixed.data.get::<DynamicSlot>(offset) } as *const _ as usize;
|
||||
assert_eq!(addr % align_of::<DynamicSlot>(), 0);
|
||||
let offset = mixed.slot(5).unwrap().1;
|
||||
unsafe {
|
||||
mixed
|
||||
.data
|
||||
.replace(offset, TraitSlot { receiver: HeapRef::NULL, implementation: 1, padding: 0 });
|
||||
}
|
||||
assert_eq!(mixed.read(5).unwrap_err(), E::Type);
|
||||
let offset = mixed.slot(0).unwrap().1;
|
||||
unsafe {
|
||||
mixed.data.replace(offset, 2_u8);
|
||||
}
|
||||
assert_eq!(mixed.read(0).unwrap_err(), E::Type);
|
||||
assert_eq!(size_of::<TraitSlot>(), 16);
|
||||
assert_eq!(size_of::<PixelSlot>(), 24);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unpublished_native_prefix_cleanup_drops_only_initialized_slots() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let text: Arc<str> = Arc::from("prefix");
|
||||
let layout = registry.type_layout(STRINGS).unwrap().array.unwrap();
|
||||
let data = Allocation::new(layout.stride * 3, layout.element_alignment, &budget).unwrap();
|
||||
let mut storage =
|
||||
TypedStorage { registry, type_id: STRINGS, count: Some(3), initialized: 0, data };
|
||||
// Simulate cancellation during initialization. Remaining native slots were
|
||||
// never constructed; cleanup must not interpret their zero backing bytes.
|
||||
unsafe {
|
||||
storage.data.init::<StringSlot>(0, Some(text.clone()));
|
||||
}
|
||||
storage.initialized = 1;
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
drop(storage);
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allocation_failure_after_native_initialization_refunds_every_domain() {
|
||||
let registry = fixture_registry();
|
||||
let budget =
|
||||
RuntimeBudget::new(RuntimeLimits::default().lowered(Q::AuxiliaryBytes, 0).unwrap());
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
assert!(
|
||||
matches!(heap.allocate_typed_struct(registry, MIXED), Err(E::Resource(e)) if e.resource == Q::AuxiliaryBytes)
|
||||
);
|
||||
assert_eq!(heap.len(), 0);
|
||||
assert_eq!(heap.allocation_count(), 0);
|
||||
for q in [Q::GuestBytes, Q::HandleSlots, Q::AuxiliaryBytes, Q::TemporaryBytes] {
|
||||
assert_eq!(budget.used(q), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn huge_array_fails_before_initialization_and_reused_storage_gets_defaults() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
assert!(
|
||||
matches!(heap.allocate_typed_array(registry.clone(), PIXELS, u32::MAX), Err(E::Resource(e)) if matches!(e.reason, ResourceReason::Limit | ResourceReason::SizeOverflow))
|
||||
);
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
let first = heap.allocate_typed_array(registry.clone(), VECS, 1).unwrap();
|
||||
heap.store_typed(®istry, first, 0, &[Value::Float(1.0), Value::Float(2.0)]).unwrap();
|
||||
heap.sweep();
|
||||
let reused = heap.allocate_typed_array(registry.clone(), VECS, 1).unwrap();
|
||||
assert_eq!(first.index, reused.index);
|
||||
assert_ne!(first.generation, reused.generation);
|
||||
assert_eq!(values(&heap, ®istry, reused, 0), [Value::Float(0.0), Value::Float(0.0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_multislot_writes_are_atomic_and_do_not_drop_native_owners() {
|
||||
let registry = fixture_registry();
|
||||
let mut heap = Heap::new();
|
||||
let r = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
let text: Arc<str> = Arc::from("keep");
|
||||
heap.store_typed(
|
||||
®istry,
|
||||
r,
|
||||
0,
|
||||
&[Value::Int64(STRING.0.into()), Value::String(text.clone())],
|
||||
)
|
||||
.unwrap();
|
||||
let dead = HeapRef { index: 999, generation: 1 };
|
||||
for (bad, error) in [
|
||||
(vec![Value::Int64(STRING.0.into())], E::Type),
|
||||
(vec![Value::Null, Value::String(text.clone())], E::Type),
|
||||
(vec![Value::Int64(ENEMY.0.into()), Value::HeapRef(dead)], E::InvalidHandle),
|
||||
(vec![Value::Int64(VEC.0.into()), Value::HeapRef(dead)], E::InvalidHandle),
|
||||
(vec![Value::Int64(VEC.0.into()), Value::HeapRef(r)], E::Type),
|
||||
(vec![Value::Int64(TRAIT.0.into()), Value::HeapRef(r)], E::Type),
|
||||
] {
|
||||
let before = Arc::strong_count(&text);
|
||||
assert_eq!(heap.store_typed(®istry, r, 0, &bad).unwrap_err(), error);
|
||||
assert_eq!(Arc::strong_count(&text), before);
|
||||
let output = heap.load_typed(®istry, r, 0).unwrap();
|
||||
let [Value::Int64(tag), Value::String(alias)] = output.as_slice() else {
|
||||
panic!("original group")
|
||||
};
|
||||
assert_eq!(*tag, STRING.0 as i64);
|
||||
assert!(Arc::ptr_eq(alias, &text));
|
||||
}
|
||||
assert_eq!(Arc::strong_count(&text), 2);
|
||||
heap.sweep();
|
||||
assert_eq!(Arc::strong_count(&text), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_boundaries_never_use_cross_numeric_value_equality_or_partial_groups() {
|
||||
let registry = fixture_registry();
|
||||
let heap = Heap::new();
|
||||
for (ty, bad) in [
|
||||
(I32, vec![Value::Int64(0)]),
|
||||
(I64, vec![Value::Int32(0)]),
|
||||
(FLOAT, vec![Value::Int64(0)]),
|
||||
(BOOL, vec![Value::Int32(0)]),
|
||||
(VEC, vec![Value::Float(1.0)]),
|
||||
(VEC, vec![Value::Int64(1), Value::Float(1.0)]),
|
||||
(PIXEL, vec![Value::Int32(0), Value::Int64(0), Value::Int64(0)]),
|
||||
(PIXEL, vec![Value::Int64(0), Value::Int64(0), Value::Int64(u32::MAX as i64 + 1)]),
|
||||
(ENEMY, vec![Value::HeapRef(HeapRef::NULL)]),
|
||||
(TypeId(u32::MAX), vec![Value::Null]),
|
||||
] {
|
||||
assert_eq!(value_contract::validate(®istry, &heap, ty, &bad).unwrap_err(), E::Type);
|
||||
}
|
||||
assert_eq!(
|
||||
value_contract::validate(
|
||||
®istry,
|
||||
&heap,
|
||||
ENEMY,
|
||||
&[Value::HeapRef(HeapRef { index: 1, generation: 0 })]
|
||||
)
|
||||
.unwrap_err(),
|
||||
E::InvalidHandle
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_dynamic_self_cycle_uses_one_mark_worklist_entry() {
|
||||
let registry = fixture_registry();
|
||||
let budget = RuntimeBudget::new(
|
||||
RuntimeLimits::default().lowered(Q::TemporaryBytes, size_of::<HeapRef>() as u64).unwrap(),
|
||||
);
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let r = heap.allocate_typed_array(registry.clone(), DYNAMICS, 128).unwrap();
|
||||
for i in 0..128 {
|
||||
heap.store_typed(®istry, r, i, &[Value::Int64(DYNAMICS.0.into()), Value::HeapRef(r)])
|
||||
.unwrap();
|
||||
}
|
||||
heap.mark_from_roots(std::iter::repeat_n(r, 128)).unwrap();
|
||||
heap.sweep();
|
||||
assert!(heap.is_valid(r));
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
//! Exact Registry-aware boundary checks. No numeric coercion or object-graph walk.
|
||||
use crate::heap::Heap;
|
||||
use crate::resource_budget::ResourceFailure;
|
||||
use prometeu_bytecode::registry::{BuiltinId as B, Registry, TypeId, TypeKind};
|
||||
use prometeu_bytecode::{HeapRef, Value};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum ValueError {
|
||||
Type,
|
||||
InvalidHandle,
|
||||
NullReference,
|
||||
Bounds,
|
||||
Resource(ResourceFailure),
|
||||
}
|
||||
impl From<ResourceFailure> for ValueError {
|
||||
fn from(value: ResourceFailure) -> Self {
|
||||
Self::Resource(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// The accepted catalogue has at most three slots per stored value. This buffer
|
||||
/// clones native owners, but never allocates a temporary Vec for a read.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ValueGroup {
|
||||
values: [Value; 3],
|
||||
len: usize,
|
||||
}
|
||||
impl ValueGroup {
|
||||
pub fn new(values: &[Value]) -> Self {
|
||||
assert!(values.len() <= 3);
|
||||
let mut result =
|
||||
Self { values: [Value::Null, Value::Null, Value::Null], len: values.len() };
|
||||
result.values[..values.len()].clone_from_slice(values);
|
||||
result
|
||||
}
|
||||
pub fn as_slice(&self) -> &[Value] {
|
||||
&self.values[..self.len]
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn u32_carrier(value: &Value) -> Result<u32, ValueError> {
|
||||
match value {
|
||||
Value::Int64(n) => u32::try_from(*n).map_err(|_| ValueError::Type),
|
||||
_ => Err(ValueError::Type),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn validate(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
type_id: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<(), ValueError> {
|
||||
let layout = registry.type_layout(type_id).ok_or(ValueError::Type)?;
|
||||
if values.len() != layout.storage.slots as usize {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
let kind = ®istry.declarations().types[type_id.0 as usize].kind;
|
||||
let valid = match kind {
|
||||
TypeKind::Struct(_) | TypeKind::Array(_) => match &values[0] {
|
||||
Value::Null => true,
|
||||
Value::HeapRef(r) => {
|
||||
if r.is_null() {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
heap.concrete_type(*r, registry)? == type_id
|
||||
}
|
||||
_ => false,
|
||||
},
|
||||
TypeKind::Trait(_) => return validate_trait(registry, heap, type_id, values),
|
||||
TypeKind::Builtin(B::DYNAMIC) => return validate_dynamic(registry, heap, values),
|
||||
TypeKind::Builtin(id) => match (*id, values) {
|
||||
(B::INT32, [Value::Int32(_)])
|
||||
| (B::INT64, [Value::Int64(_)])
|
||||
| (B::FLOAT, [Value::Float(_)])
|
||||
| (B::BOOL, [Value::Boolean(_)])
|
||||
| (B::STRING, [Value::String(_) | Value::Null])
|
||||
| (B::VEC2, [Value::Float(_), Value::Float(_)]) => true,
|
||||
(B::COLOR, [color]) => u32_carrier(color).is_ok(),
|
||||
(B::PIXEL, [Value::Int64(_), Value::Int64(_), color]) => u32_carrier(color).is_ok(),
|
||||
_ => false,
|
||||
},
|
||||
};
|
||||
if valid { Ok(()) } else { Err(ValueError::Type) }
|
||||
}
|
||||
|
||||
fn validate_trait(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
trait_id: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<(), ValueError> {
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
let [Value::HeapRef(receiver), implementation] = values else { return Err(ValueError::Type) };
|
||||
if receiver.is_null() {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
let owner = heap.concrete_type(*receiver, registry)?;
|
||||
let id = u32_carrier(implementation)?;
|
||||
let implementation =
|
||||
registry.declarations().implementations.get(id as usize).ok_or(ValueError::Type)?;
|
||||
if implementation.owner == owner && implementation.trait_id == trait_id {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ValueError::Type)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_dynamic(registry: &Registry, heap: &Heap, values: &[Value]) -> Result<(), ValueError> {
|
||||
if values.len() != 2 {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
if matches!(values, [Value::Null, Value::Null]) {
|
||||
return Ok(());
|
||||
}
|
||||
let type_id = TypeId(u32_carrier(&values[0])?);
|
||||
let layout = registry.type_layout(type_id).ok_or(ValueError::Type)?;
|
||||
if matches!(
|
||||
registry.declarations().types[type_id.0 as usize].kind,
|
||||
TypeKind::Builtin(B::DYNAMIC)
|
||||
) {
|
||||
return Err(ValueError::Type); // Repacking is identity, never Dynamic<Dynamic>.
|
||||
}
|
||||
if matches!(values[1], Value::Null) {
|
||||
return if layout.storage.nullable() { Ok(()) } else { Err(ValueError::Type) };
|
||||
}
|
||||
if layout.storage.slots == 1 {
|
||||
return validate(registry, heap, type_id, &values[1..]);
|
||||
}
|
||||
let Value::HeapRef(handle) = values[1] else { return Err(ValueError::Type) };
|
||||
if handle == HeapRef::NULL {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
heap.dynamic_box_values(registry, handle, type_id)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Semantic pack helper. The interpreter must retain source stack roots until
|
||||
/// this returns and publish/consume the complete group atomically.
|
||||
pub(crate) fn pack_dynamic(
|
||||
registry: &std::sync::Arc<Registry>,
|
||||
heap: &mut Heap,
|
||||
type_id: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<ValueGroup, ValueError> {
|
||||
validate(registry, heap, type_id, values)?;
|
||||
if is_dynamic(registry, type_id) {
|
||||
return Ok(ValueGroup::new(values));
|
||||
}
|
||||
let payload = if values.iter().all(|v| matches!(v, Value::Null)) {
|
||||
Value::Null // Includes absent traits: typed null does not need a box.
|
||||
} else if values.len() == 1 {
|
||||
values[0].clone()
|
||||
} else {
|
||||
Value::HeapRef(heap.allocate_dynamic_box(registry.clone(), type_id, values)?)
|
||||
};
|
||||
Ok(ValueGroup::new(&[Value::Int64(type_id.0.into()), payload]))
|
||||
}
|
||||
|
||||
fn is_dynamic(registry: &Registry, type_id: TypeId) -> bool {
|
||||
matches!(registry.declarations().types[type_id.0 as usize].kind, TypeKind::Builtin(B::DYNAMIC))
|
||||
}
|
||||
|
||||
/// Compatibility validates malformed input even for a mismatched destination.
|
||||
/// No allocation, extraction, or Arc/box clone occurs in a query.
|
||||
pub(crate) fn can_unpack_dynamic(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
target: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<bool, ValueError> {
|
||||
let target_layout = registry.type_layout(target).ok_or(ValueError::Type)?;
|
||||
validate_dynamic(registry, heap, values)?;
|
||||
if is_dynamic(registry, target) {
|
||||
return Ok(true);
|
||||
}
|
||||
match &values[0] {
|
||||
Value::Null => Ok(target_layout.storage.nullable()),
|
||||
tag => Ok(TypeId(u32_carrier(tag)?) == target),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unpack_dynamic(
|
||||
registry: &Registry,
|
||||
heap: &Heap,
|
||||
target: TypeId,
|
||||
values: &[Value],
|
||||
) -> Result<ValueGroup, ValueError> {
|
||||
if !can_unpack_dynamic(registry, heap, target, values)? {
|
||||
return Err(ValueError::Type);
|
||||
}
|
||||
if is_dynamic(registry, target) {
|
||||
return Ok(ValueGroup::new(values));
|
||||
}
|
||||
let layout = registry.type_layout(target).unwrap().storage;
|
||||
if matches!(values[1], Value::Null) {
|
||||
return Ok(ValueGroup::new(layout.default_values()));
|
||||
}
|
||||
if layout.slots == 1 {
|
||||
return Ok(ValueGroup::new(&values[1..]));
|
||||
}
|
||||
let Value::HeapRef(r) = values[1] else { return Err(ValueError::Type) };
|
||||
Ok(ValueGroup::new(heap.dynamic_box_values(registry, r, target)?))
|
||||
}
|
||||
@@ -9,6 +9,18 @@ use std::collections::{HashMap, HashSet, VecDeque};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VerifierError {
|
||||
InvalidLocalSlot {
|
||||
pc: usize,
|
||||
slot: u32,
|
||||
slots: u32,
|
||||
},
|
||||
InvalidBoolImmediate {
|
||||
pc: usize,
|
||||
},
|
||||
RegistryRequired {
|
||||
pc: usize,
|
||||
opcode: OpCode,
|
||||
},
|
||||
UnknownOpcode {
|
||||
pc: usize,
|
||||
opcode: u16,
|
||||
@@ -36,19 +48,19 @@ pub enum VerifierError {
|
||||
},
|
||||
StackOverflow {
|
||||
pc: usize,
|
||||
height: u16,
|
||||
limit: u16,
|
||||
height: u32,
|
||||
limit: u32,
|
||||
},
|
||||
StackMismatchJoin {
|
||||
pc: usize,
|
||||
target: usize,
|
||||
height_in: u16,
|
||||
height_target: u16,
|
||||
height_in: u32,
|
||||
height_target: u32,
|
||||
},
|
||||
BadRetStackHeight {
|
||||
pc: usize,
|
||||
height: u16,
|
||||
expected: u16,
|
||||
height: u32,
|
||||
expected: u32,
|
||||
},
|
||||
FunctionOutOfBounds {
|
||||
func_idx: usize,
|
||||
@@ -95,19 +107,19 @@ pub enum VerifierError {
|
||||
/// User-provided arg_count for CALL_CLOSURE does not match callee signature
|
||||
BadClosureArgCount {
|
||||
pc: usize,
|
||||
expected: u16,
|
||||
got: u16,
|
||||
expected: u32,
|
||||
got: u32,
|
||||
},
|
||||
/// YIELD executed in an invalid context (minimal safety rule violation)
|
||||
InvalidYieldContext {
|
||||
pc: usize,
|
||||
height: u16,
|
||||
height: u32,
|
||||
},
|
||||
/// SPAWN arg_count does not match callee param_slots
|
||||
BadSpawnArgCount {
|
||||
pc: usize,
|
||||
expected: u16,
|
||||
got: u16,
|
||||
expected: u32,
|
||||
got: u32,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -178,6 +190,67 @@ impl Verifier {
|
||||
}
|
||||
}
|
||||
})?;
|
||||
if instr.opcode.requires_registry() {
|
||||
return Err(VerifierError::RegistryRequired {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
});
|
||||
}
|
||||
match instr.opcode {
|
||||
OpCode::Call | OpCode::MakeClosure | OpCode::Spawn => {
|
||||
let id = if instr.opcode == OpCode::Call {
|
||||
instr.imm_u32().unwrap()
|
||||
} else {
|
||||
instr.imm_u32x2().unwrap().0
|
||||
};
|
||||
let callee = all_functions
|
||||
.get(id as usize)
|
||||
.ok_or(VerifierError::InvalidFuncId { pc: func_start + pc, id })?;
|
||||
if instr.opcode == OpCode::Spawn {
|
||||
let got = instr.imm_u32x2().unwrap().1;
|
||||
if got != u32::from(callee.param_slots) {
|
||||
return Err(VerifierError::BadSpawnArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: u32::from(callee.param_slots),
|
||||
got,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
OpCode::GetLocal | OpCode::SetLocal => {
|
||||
let slot = instr.imm_u32().unwrap();
|
||||
let slots = u32::from(func.param_slots) + u32::from(func.local_slots);
|
||||
if slot >= slots {
|
||||
return Err(VerifierError::InvalidLocalSlot {
|
||||
pc: func_start + pc,
|
||||
slot,
|
||||
slots,
|
||||
});
|
||||
}
|
||||
}
|
||||
OpCode::Hostcall => {
|
||||
return Err(VerifierError::HostcallNotPatched {
|
||||
pc: func_start + pc,
|
||||
sysc_index: instr.imm_u32().unwrap(),
|
||||
});
|
||||
}
|
||||
OpCode::Syscall => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
if Syscall::from_u32(id).is_none() {
|
||||
return Err(VerifierError::InvalidSyscallId { pc: func_start + pc, id });
|
||||
}
|
||||
}
|
||||
OpCode::Intrinsic => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
if lookup_intrinsic_by_id(id).is_none() {
|
||||
return Err(VerifierError::InvalidIntrinsicId { pc: func_start + pc, id });
|
||||
}
|
||||
}
|
||||
OpCode::PushBool if instr.imm_u8().unwrap() > 1 => {
|
||||
return Err(VerifierError::InvalidBoolImmediate { pc: func_start + pc });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
pc = instr.next_pc;
|
||||
}
|
||||
|
||||
@@ -185,6 +258,29 @@ impl Verifier {
|
||||
return Err(VerifierError::TrailingBytes { func_idx, at_pc: func_start + pc });
|
||||
}
|
||||
|
||||
// Check branch immediates over the whole decoded range, before reachability
|
||||
// analysis, so dead code cannot hide an invalid target or boundary.
|
||||
let mut pc = 0;
|
||||
while pc < func_code.len() {
|
||||
let instr = decode_next(pc, func_code).unwrap();
|
||||
if instr.opcode.spec().is_branch {
|
||||
let target = instr.imm_u32().unwrap() as usize;
|
||||
if target > func_code.len() {
|
||||
return Err(VerifierError::InvalidJumpTarget {
|
||||
pc: func_start + pc,
|
||||
target: func_start + target,
|
||||
});
|
||||
}
|
||||
if target < func_code.len() && !valid_pc.contains(&target) {
|
||||
return Err(VerifierError::JumpToMidInstruction {
|
||||
pc: func_start + pc,
|
||||
target: func_start + target,
|
||||
});
|
||||
}
|
||||
}
|
||||
pc = instr.next_pc;
|
||||
}
|
||||
|
||||
// Minimal stack type lattice to validate closure semantics
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum SlotTy {
|
||||
@@ -215,14 +311,22 @@ impl Verifier {
|
||||
|
||||
while let Some(pc) = worklist.pop_front() {
|
||||
let in_types = stack_types_in.get(&pc).cloned().unwrap();
|
||||
let in_height = in_types.len() as u16;
|
||||
let in_height = in_types.len() as u64;
|
||||
let instr = decode_next(pc, func_code).unwrap(); // Guaranteed to succeed due to first pass
|
||||
let spec = instr.opcode.spec();
|
||||
let static_pops = spec.pops.ok_or(VerifierError::RegistryRequired {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
})?;
|
||||
let static_pushes = spec.pushes.ok_or(VerifierError::RegistryRequired {
|
||||
pc: func_start + pc,
|
||||
opcode: instr.opcode,
|
||||
})?;
|
||||
|
||||
// Resolve dynamic pops/_pushes
|
||||
let (pops, _pushes) = match instr.opcode {
|
||||
OpCode::PopN => {
|
||||
let n = instr.imm_u32().unwrap() as u16;
|
||||
let n = instr.imm_u32().unwrap() as u64;
|
||||
(n, 0)
|
||||
}
|
||||
OpCode::Call => {
|
||||
@@ -230,18 +334,18 @@ impl Verifier {
|
||||
let callee = all_functions.get(func_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: func_id }
|
||||
})?;
|
||||
(callee.param_slots, callee.return_slots)
|
||||
(u64::from(callee.param_slots), u64::from(callee.return_slots))
|
||||
}
|
||||
OpCode::MakeClosure => {
|
||||
// imm: fn_id (u32), capture_count (u32)
|
||||
let (_fn_id, capture_count_u32) = instr.imm_u32x2().unwrap();
|
||||
let capture_count = capture_count_u32 as u16;
|
||||
let capture_count = capture_count_u32 as u64;
|
||||
(capture_count, 1)
|
||||
}
|
||||
OpCode::CallClosure => {
|
||||
// imm: arg_count (u32). Pops closure_ref + arg_count, _pushes callee returns.
|
||||
// We can't determine _pushes here without looking at TOS type; will validate below.
|
||||
let arg_count = instr.imm_u32().unwrap() as u16;
|
||||
let arg_count = instr.imm_u32().unwrap() as u64;
|
||||
// Temporarily set _pushes=0; we'll compute real _pushes after type checks.
|
||||
(arg_count + 1, 0)
|
||||
}
|
||||
@@ -251,18 +355,18 @@ impl Verifier {
|
||||
let callee = all_functions.get(fn_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: fn_id }
|
||||
})?;
|
||||
let arg_count = arg_count_u32 as u16;
|
||||
let arg_count = arg_count_u32 as u64;
|
||||
// Enforce exact arity match for safety and determinism
|
||||
if callee.param_slots != arg_count {
|
||||
if u64::from(callee.param_slots) != arg_count {
|
||||
return Err(VerifierError::BadSpawnArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: callee.param_slots,
|
||||
got: arg_count,
|
||||
expected: u32::from(callee.param_slots),
|
||||
got: arg_count as u32,
|
||||
});
|
||||
}
|
||||
(arg_count, 0)
|
||||
}
|
||||
OpCode::Ret => (func.return_slots, 0),
|
||||
OpCode::Ret => (u64::from(func.return_slots), 0),
|
||||
OpCode::Hostcall => {
|
||||
return Err(VerifierError::HostcallNotPatched {
|
||||
pc: func_start + pc,
|
||||
@@ -274,16 +378,16 @@ impl Verifier {
|
||||
let syscall = Syscall::from_u32(id).ok_or_else(|| {
|
||||
VerifierError::InvalidSyscallId { pc: func_start + pc, id }
|
||||
})?;
|
||||
(syscall.args_count() as u16, syscall.results_count() as u16)
|
||||
(syscall.args_count() as u64, syscall.results_count() as u64)
|
||||
}
|
||||
OpCode::Intrinsic => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let intrinsic = lookup_intrinsic_by_id(id).ok_or_else(|| {
|
||||
VerifierError::InvalidIntrinsicId { pc: func_start + pc, id }
|
||||
})?;
|
||||
(intrinsic.arg_slots() as u16, intrinsic.ret_slots() as u16)
|
||||
(intrinsic.arg_slots() as u64, intrinsic.ret_slots() as u64)
|
||||
}
|
||||
_ => (spec.pops, spec.pushes),
|
||||
_ => (u64::from(static_pops), u64::from(static_pushes)),
|
||||
};
|
||||
|
||||
if in_height < pops {
|
||||
@@ -299,7 +403,7 @@ impl Verifier {
|
||||
{
|
||||
return Err(VerifierError::InvalidYieldContext {
|
||||
pc: func_start + pc,
|
||||
height: in_height,
|
||||
height: in_height as u32,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -326,21 +430,21 @@ impl Verifier {
|
||||
let callee = all_functions.get(fn_id as usize).ok_or_else(|| {
|
||||
VerifierError::InvalidFuncId { pc: func_start + pc, id: fn_id }
|
||||
})?;
|
||||
let user_argc = instr.imm_u32().unwrap() as u16;
|
||||
let user_argc = instr.imm_u32().unwrap() as u64;
|
||||
if callee.param_slots == 0 {
|
||||
// Hidden arg0 must exist in callee signature
|
||||
return Err(VerifierError::BadClosureArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: 0,
|
||||
got: user_argc,
|
||||
got: user_argc as u32,
|
||||
});
|
||||
}
|
||||
let expected_user = callee.param_slots - 1; // exclude hidden arg0
|
||||
if expected_user != user_argc {
|
||||
if u64::from(expected_user) != user_argc {
|
||||
return Err(VerifierError::BadClosureArgCount {
|
||||
pc: func_start + pc,
|
||||
expected: expected_user,
|
||||
got: user_argc,
|
||||
expected: u32::from(expected_user),
|
||||
got: user_argc as u32,
|
||||
});
|
||||
}
|
||||
// Push callee returns as Unknown types
|
||||
@@ -380,6 +484,8 @@ impl Verifier {
|
||||
opcode: OpCode::Dup,
|
||||
});
|
||||
}
|
||||
// Generic transfer popped the original; restore it and its copy.
|
||||
out_types.push(tos);
|
||||
out_types.push(tos);
|
||||
}
|
||||
// Swap swaps top-2 types; ensure enough stack
|
||||
@@ -390,8 +496,10 @@ impl Verifier {
|
||||
opcode: OpCode::Swap,
|
||||
});
|
||||
}
|
||||
let len = out_types.len();
|
||||
out_types.swap(len - 1, len - 2);
|
||||
// Generic transfer popped both values; rebuild the swapped pair.
|
||||
let len = in_types.len();
|
||||
out_types.push(in_types[len - 1]);
|
||||
out_types.push(in_types[len - 2]);
|
||||
}
|
||||
// Arithmetic/logic produce NonClosure but we don't strictly need it; mark Unknown conservatively
|
||||
// Calls and syscalls return Unknown
|
||||
@@ -405,21 +513,21 @@ impl Verifier {
|
||||
OpCode::Syscall => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let syscall = Syscall::from_u32(id).unwrap();
|
||||
for _ in 0..(syscall.results_count() as u16) {
|
||||
for _ in 0..syscall.results_count() {
|
||||
out_types.push(Unknown);
|
||||
}
|
||||
}
|
||||
OpCode::Intrinsic => {
|
||||
let id = instr.imm_u32().unwrap();
|
||||
let intrinsic = lookup_intrinsic_by_id(id).unwrap();
|
||||
for _ in 0..(intrinsic.ret_slots() as u16) {
|
||||
for _ in 0..intrinsic.ret_slots() {
|
||||
out_types.push(Unknown);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Default: push Unknown for any declared _pushes
|
||||
if spec.pushes > 0 {
|
||||
for _ in 0..spec.pushes {
|
||||
if static_pushes > 0 {
|
||||
for _ in 0..static_pushes {
|
||||
out_types.push(Unknown);
|
||||
}
|
||||
}
|
||||
@@ -432,29 +540,36 @@ impl Verifier {
|
||||
// If we reached here, we handled it above and set dynamic_pushes
|
||||
0
|
||||
}
|
||||
OpCode::Syscall => spec.pushes, // already added Unknowns above
|
||||
OpCode::Intrinsic => spec.pushes, // already added Unknowns above
|
||||
OpCode::Call => spec.pushes, // already added Unknowns above
|
||||
_ => spec.pushes,
|
||||
OpCode::Syscall => static_pushes, // already added Unknowns above
|
||||
OpCode::Intrinsic => static_pushes, // already added Unknowns above
|
||||
OpCode::Call => static_pushes, // already added Unknowns above
|
||||
_ => static_pushes,
|
||||
});
|
||||
|
||||
let out_height = (out_types.len()) as u16;
|
||||
max_stack = max_stack.max(out_height);
|
||||
|
||||
// Enforce declared max stack if provided (0 means "no explicit limit")
|
||||
if func.max_stack_slots != 0 && out_height > func.max_stack_slots {
|
||||
let out_height = out_types.len() as u32;
|
||||
if out_height > u32::from(u16::MAX) {
|
||||
return Err(VerifierError::StackOverflow {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
limit: func.max_stack_slots,
|
||||
limit: u32::from(u16::MAX),
|
||||
});
|
||||
}
|
||||
max_stack = max_stack.max(out_height as u16);
|
||||
|
||||
// Enforce declared max stack if provided (0 means "no explicit limit")
|
||||
if func.max_stack_slots != 0 && out_height > u32::from(func.max_stack_slots) {
|
||||
return Err(VerifierError::StackOverflow {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
limit: u32::from(func.max_stack_slots),
|
||||
});
|
||||
}
|
||||
|
||||
if instr.opcode == OpCode::Ret && in_height != func.return_slots {
|
||||
if instr.opcode == OpCode::Ret && in_height != u64::from(func.return_slots) {
|
||||
return Err(VerifierError::BadRetStackHeight {
|
||||
pc: func_start + pc,
|
||||
height: in_height,
|
||||
expected: func.return_slots,
|
||||
height: in_height as u32,
|
||||
expected: u32::from(func.return_slots),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -496,11 +611,11 @@ impl Verifier {
|
||||
|
||||
if target_rel == func_len {
|
||||
// jump to the end of function
|
||||
if out_height != func.return_slots {
|
||||
if out_height != u32::from(func.return_slots) {
|
||||
return Err(VerifierError::BadRetStackHeight {
|
||||
pc: func_start + pc,
|
||||
height: out_height,
|
||||
expected: func.return_slots,
|
||||
expected: u32::from(func.return_slots),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@@ -512,7 +627,7 @@ impl Verifier {
|
||||
}
|
||||
|
||||
if let Some(existing_types) = stack_types_in.get_mut(&target_rel) {
|
||||
let existing_height = existing_types.len() as u16;
|
||||
let existing_height = existing_types.len() as u32;
|
||||
if existing_height != out_height {
|
||||
return Err(VerifierError::StackMismatchJoin {
|
||||
pc: func_start + pc,
|
||||
@@ -546,7 +661,7 @@ impl Verifier {
|
||||
let func_len = layouts.get(func_idx).map(|l| l.end - l.start).unwrap_or_else(|| 0);
|
||||
if next_pc < func_len {
|
||||
if let Some(existing_types) = stack_types_in.get_mut(&next_pc) {
|
||||
let existing_height = existing_types.len() as u16;
|
||||
let existing_height = existing_types.len() as u32;
|
||||
if existing_height != out_height {
|
||||
return Err(VerifierError::StackMismatchJoin {
|
||||
pc: func_start + pc,
|
||||
@@ -951,6 +1066,18 @@ mod golden_ext {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn slot_only_verifier_rejects_registry_instruction_even_when_unreachable() {
|
||||
use super::{Verifier, VerifierError};
|
||||
use prometeu_bytecode::{FunctionMeta, assemble};
|
||||
let code = assemble("HALT\nDYN_PACK 0").unwrap();
|
||||
let f = FunctionMeta { code_len: code.len() as u32, ..Default::default() };
|
||||
assert!(matches!(
|
||||
Verifier::verify(&code, &[f]),
|
||||
Err(VerifierError::RegistryRequired { pc: 2, .. })
|
||||
));
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -1388,3 +1515,179 @@ mod tests {
|
||||
assert_eq!(res, Err(VerifierError::BadRetStackHeight { pc: 6, height: 2, expected: 1 }));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod width_regressions {
|
||||
use super::*;
|
||||
fn op(code: &mut Vec<u8>, opcode: OpCode) {
|
||||
code.extend_from_slice(&(opcode as u16).to_le_bytes());
|
||||
}
|
||||
fn arg(code: &mut Vec<u8>, opcode: OpCode, n: u32) {
|
||||
op(code, opcode);
|
||||
code.extend_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
fn one(code: &[u8]) -> Vec<FunctionMeta> {
|
||||
vec![FunctionMeta { code_len: code.len() as u32, ..Default::default() }]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn popn_u32_counts_do_not_wrap_to_zero() {
|
||||
for n in [65_536, u32::MAX] {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::PopN, n);
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::StackUnderflow { pc: 0, opcode: OpCode::PopN }
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn closure_capture_u32_counts_do_not_wrap_to_zero() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 0);
|
||||
code.extend_from_slice(&65_536_u32.to_le_bytes());
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::StackUnderflow { pc: 0, opcode: OpCode::MakeClosure }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn closure_argument_plus_receiver_cannot_overflow() {
|
||||
for n in [65_536, u32::MAX] {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 0);
|
||||
code.extend_from_slice(&0_u32.to_le_bytes());
|
||||
arg(&mut code, OpCode::CallClosure, n);
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::StackUnderflow { pc: 10, opcode: OpCode::CallClosure }
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn spawn_diagnostic_preserves_the_full_u32_argument_count() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::Spawn, 0);
|
||||
code.extend_from_slice(&65_536_u32.to_le_bytes());
|
||||
op(&mut code, OpCode::Halt);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::BadSpawnArgCount { pc: 0, expected: 0, got: 65_536 }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn inferred_stack_height_cannot_wrap_when_exported_to_current_function_metadata() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::Call, 1);
|
||||
op(&mut code, OpCode::Dup);
|
||||
op(&mut code, OpCode::Halt);
|
||||
let functions = vec![
|
||||
FunctionMeta { code_len: code.len() as u32, ..Default::default() },
|
||||
FunctionMeta {
|
||||
code_offset: code.len() as u32,
|
||||
return_slots: u16::MAX,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &functions).unwrap_err(),
|
||||
VerifierError::StackOverflow { pc: 6, height: 65_536, limit: 65_535 }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn local_indices_cannot_address_temporary_slots_even_in_dead_code() {
|
||||
for operation in [OpCode::GetLocal, OpCode::SetLocal] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
arg(&mut code, operation, 1);
|
||||
let mut functions = one(&code);
|
||||
functions[0].local_slots = 1;
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &functions).unwrap_err(),
|
||||
VerifierError::InvalidLocalSlot { pc: 2, slot: 1, slots: 1 }
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn unreachable_bad_functions_natives_booleans_and_jumps_are_rejected() {
|
||||
for operation in [OpCode::Call, OpCode::Intrinsic, OpCode::Syscall] {
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
arg(&mut code, operation, u32::MAX);
|
||||
assert!(Verifier::verify(&code, &one(&code)).is_err());
|
||||
}
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
op(&mut code, OpCode::PushBool);
|
||||
code.push(2);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::InvalidBoolImmediate { pc: 2 }
|
||||
);
|
||||
let mut code = vec![];
|
||||
op(&mut code, OpCode::Halt);
|
||||
arg(&mut code, OpCode::Jmp, 3);
|
||||
assert_eq!(
|
||||
Verifier::verify(&code, &one(&code)).unwrap_err(),
|
||||
VerifierError::JumpToMidInstruction { pc: 2, target: 3 }
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn dup_increases_height_and_restores_both_copies_for_arithmetic() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::PushI32, 7);
|
||||
op(&mut code, OpCode::Dup);
|
||||
op(&mut code, OpCode::Add);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let mut functions = one(&code);
|
||||
functions[0].return_slots = 1;
|
||||
assert_eq!(Verifier::verify(&code, &functions).unwrap(), vec![2]);
|
||||
}
|
||||
#[test]
|
||||
fn swap_preserves_height_and_known_closure_in_the_correct_position() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 1);
|
||||
code.extend_from_slice(&0_u32.to_le_bytes());
|
||||
arg(&mut code, OpCode::PushI32, 5);
|
||||
op(&mut code, OpCode::Swap);
|
||||
arg(&mut code, OpCode::CallClosure, 1);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let start = code.len();
|
||||
op(&mut code, OpCode::Ret);
|
||||
let functions = vec![
|
||||
FunctionMeta { code_len: start as u32, ..Default::default() },
|
||||
FunctionMeta {
|
||||
code_offset: start as u32,
|
||||
code_len: 2,
|
||||
param_slots: 2,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
assert_eq!(Verifier::verify(&code, &functions).unwrap(), vec![2, 0]);
|
||||
}
|
||||
#[test]
|
||||
fn dup_preserves_the_known_callee_in_each_closure_copy() {
|
||||
let mut code = vec![];
|
||||
arg(&mut code, OpCode::MakeClosure, 1);
|
||||
code.extend_from_slice(&0_u32.to_le_bytes());
|
||||
op(&mut code, OpCode::Dup);
|
||||
arg(&mut code, OpCode::CallClosure, 0);
|
||||
arg(&mut code, OpCode::CallClosure, 0);
|
||||
op(&mut code, OpCode::Ret);
|
||||
let start = code.len();
|
||||
op(&mut code, OpCode::Ret);
|
||||
let functions = vec![
|
||||
FunctionMeta { code_len: start as u32, ..Default::default() },
|
||||
FunctionMeta {
|
||||
code_offset: start as u32,
|
||||
code_len: 2,
|
||||
param_slots: 1,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
assert_eq!(Verifier::verify(&code, &functions).unwrap(), vec![2, 0]);
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,63 +1,59 @@
|
||||
use super::*;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn handle_safepoint(&mut self) {
|
||||
pub(super) fn handle_safepoint(&mut self) -> Result<(), ResourceFailure> {
|
||||
if self.gc_alloc_threshold > 0 {
|
||||
let live_now = self.heap.len();
|
||||
let since_last = live_now.saturating_sub(self.last_gc_live_count);
|
||||
if since_last >= self.gc_alloc_threshold {
|
||||
struct CollectRoots(Vec<prometeu_bytecode::HeapRef>);
|
||||
impl crate::roots::RootVisitor for CollectRoots {
|
||||
fn visit_heap_ref(&mut self, r: prometeu_bytecode::HeapRef) {
|
||||
self.0.push(r);
|
||||
}
|
||||
}
|
||||
let mut collector = CollectRoots(Vec::new());
|
||||
self.visit_roots(&mut collector);
|
||||
if let Some(cur) = self.current_coro {
|
||||
collector.0.push(cur);
|
||||
}
|
||||
let mut coro_roots = self.heap.suspended_coroutine_handles();
|
||||
collector.0.append(&mut coro_roots);
|
||||
self.heap.mark_from_roots(collector.0);
|
||||
// Parameters/locals are already in the operand buffer. Stream roots
|
||||
// without a duplicate collection; suspended contexts are scanned by Heap.
|
||||
let roots = self
|
||||
.operand_stack
|
||||
.iter()
|
||||
.chain(self.globals.iter())
|
||||
.filter_map(crate::roots::value_heap_ref)
|
||||
.chain(self.current_coro);
|
||||
self.heap.mark_session_roots(roots)?;
|
||||
self.heap.sweep();
|
||||
self.last_gc_live_count = self.heap.len();
|
||||
}
|
||||
}
|
||||
|
||||
self.current_tick = self.current_tick.wrapping_add(1);
|
||||
self.scheduler.wake_ready(self.current_tick);
|
||||
self.scheduler.wake_ready(self.current_tick)?;
|
||||
|
||||
let mut switched_out = false;
|
||||
if let Some(cur) = self.current_coro {
|
||||
if let Some(wake) = self.sleep_requested_until.take() {
|
||||
if let Some(wake) = self.sleep_requested_until {
|
||||
self.scheduler.sleep_until(cur, wake)?;
|
||||
self.sleep_requested_until = None;
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Sleeping;
|
||||
co.wake_tick = wake;
|
||||
}
|
||||
self.scheduler.sleep_until(cur, wake);
|
||||
self.current_coro = None;
|
||||
self.scheduler.clear_current();
|
||||
switched_out = true;
|
||||
} else if self.yield_requested {
|
||||
self.scheduler.enqueue_ready(cur)?;
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Ready;
|
||||
}
|
||||
self.scheduler.enqueue_ready(cur);
|
||||
self.current_coro = None;
|
||||
self.scheduler.clear_current();
|
||||
switched_out = true;
|
||||
} else if self.halted || self.pc >= self.program.rom.len() {
|
||||
if let Some(co) = self.heap.coroutine_data_mut(cur) {
|
||||
co.pc = self.pc;
|
||||
co.stack = std::mem::take(&mut self.operand_stack);
|
||||
co.frames = std::mem::take(&mut self.call_stack);
|
||||
co.stack = self.operand_stack.take();
|
||||
co.frames = self.call_stack.take();
|
||||
co.state = CoroutineState::Finished;
|
||||
}
|
||||
self.current_coro = None;
|
||||
@@ -70,8 +66,8 @@ impl VirtualMachine {
|
||||
if let Some(next) = self.scheduler.dequeue_next() {
|
||||
if let Some(co) = self.heap.coroutine_data_mut(next) {
|
||||
self.pc = co.pc;
|
||||
self.operand_stack = std::mem::take(&mut co.stack);
|
||||
self.call_stack = std::mem::take(&mut co.frames);
|
||||
self.operand_stack = co.stack.take();
|
||||
self.call_stack = co.frames.take();
|
||||
co.state = CoroutineState::Running;
|
||||
}
|
||||
self.current_coro = Some(next);
|
||||
@@ -84,6 +80,7 @@ impl VirtualMachine {
|
||||
}
|
||||
|
||||
self.yield_requested = false;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn visit_roots<V: RootVisitor + ?Sized>(&self, visitor: &mut V) {
|
||||
|
||||
@@ -74,13 +74,13 @@ impl VirtualMachine {
|
||||
self.operand_stack.clear();
|
||||
self.call_stack.clear();
|
||||
self.globals.clear();
|
||||
self.heap = Heap::new();
|
||||
self.heap = Heap::with_budget(&self.budget);
|
||||
self.cycles = 0;
|
||||
self.halted = true;
|
||||
self.last_gc_live_count = 0;
|
||||
self.current_tick = 0;
|
||||
self.sleep_requested_until = None;
|
||||
self.scheduler = Scheduler::new();
|
||||
self.scheduler = Scheduler::with_budget(&self.budget);
|
||||
self.current_coro = None;
|
||||
|
||||
let program = match prometeu_bytecode::BytecodeLoader::load(&program_bytes) {
|
||||
@@ -89,7 +89,7 @@ impl VirtualMachine {
|
||||
.map_err(VmInitError::LoaderPatchFailed)?;
|
||||
|
||||
let max_stacks = Verifier::verify(&module.code, &module.functions)
|
||||
.map_err(|e| VmInitError::VerificationFailed(format!("{:?}", e)))?;
|
||||
.map_err(VmInitError::VerificationFailed)?;
|
||||
|
||||
let mut program = ProgramImage::from(module);
|
||||
let mut functions = program.functions.as_ref().to_vec();
|
||||
@@ -128,22 +128,33 @@ impl VirtualMachine {
|
||||
self.capabilities = caps;
|
||||
}
|
||||
|
||||
pub fn prepare_boot_call(&mut self) {
|
||||
self.prepare_call_by_index(0);
|
||||
pub fn prepare_boot_call(&mut self) -> Result<(), ResourceFailure> {
|
||||
self.prepare_call_by_index(0)
|
||||
}
|
||||
|
||||
pub fn prepare_call(&mut self, entrypoint: &str) {
|
||||
pub fn prepare_call(&mut self, entrypoint: &str) -> Result<(), ResourceFailure> {
|
||||
let func_idx = if let Ok(idx) = entrypoint.parse::<usize>() {
|
||||
idx
|
||||
} else {
|
||||
self.program.exports.get(entrypoint).map(|&idx| idx as usize).ok_or(()).unwrap_or(0)
|
||||
};
|
||||
|
||||
self.prepare_call_by_index(func_idx);
|
||||
self.prepare_call_by_index(func_idx)
|
||||
}
|
||||
|
||||
fn prepare_call_by_index(&mut self, func_idx: usize) {
|
||||
fn prepare_call_by_index(&mut self, func_idx: usize) -> Result<(), ResourceFailure> {
|
||||
self.budget.check(RuntimeResource::CallDepth, 1)?;
|
||||
let callee = self.program.functions.get(func_idx).cloned().unwrap_or_default();
|
||||
let total_slots = callee.param_slots as usize + callee.local_slots as usize;
|
||||
self.operand_stack.reserve(total_slots.saturating_sub(self.operand_stack.len()))?;
|
||||
self.call_stack.reserve(1usize.saturating_sub(self.call_stack.len()))?;
|
||||
let main_href = self.heap.allocate_coroutine(
|
||||
callee.code_offset as usize,
|
||||
CoroutineState::Running,
|
||||
0,
|
||||
BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes),
|
||||
BudgetVec::new(&self.budget, RuntimeResource::AuxiliaryBytes),
|
||||
)?;
|
||||
self.pc = callee.code_offset as usize;
|
||||
self.halted = false;
|
||||
|
||||
@@ -153,7 +164,7 @@ impl VirtualMachine {
|
||||
if let Some(func) = self.program.functions.get(func_idx) {
|
||||
let total_slots = func.param_slots as u32 + func.local_slots as u32;
|
||||
for _ in 0..total_slots {
|
||||
self.operand_stack.push(Value::Null);
|
||||
self.operand_stack.push(Value::Null)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,16 +172,10 @@ impl VirtualMachine {
|
||||
return_pc: self.program.rom.len() as u32,
|
||||
stack_base: 0,
|
||||
func_idx,
|
||||
});
|
||||
})?;
|
||||
|
||||
let main_href = self.heap.allocate_coroutine(
|
||||
self.pc,
|
||||
CoroutineState::Running,
|
||||
0,
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
);
|
||||
self.current_coro = Some(main_href);
|
||||
self.scheduler.set_current(self.current_coro);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,26 @@
|
||||
use super::*;
|
||||
|
||||
impl VirtualMachine {
|
||||
pub(super) fn check_call_depth(&self) -> Result<(), ResourceFailure> {
|
||||
let depth = self.call_stack.len().checked_add(1).ok_or_else(|| {
|
||||
self.budget.failure(
|
||||
RuntimeResource::CallDepth,
|
||||
crate::resource_budget::ResourceReason::SizeOverflow,
|
||||
u64::MAX,
|
||||
)
|
||||
})?;
|
||||
self.budget.check(RuntimeResource::CallDepth, depth as u64)
|
||||
}
|
||||
|
||||
pub(super) fn resource_trap(
|
||||
&self,
|
||||
error: ResourceFailure,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
) -> LogicalFrameEndingReason {
|
||||
self.trap(TRAP_RESOURCE_LIMIT, opcode as u16, error.to_string(), pc)
|
||||
}
|
||||
|
||||
pub fn trap(
|
||||
&self,
|
||||
code: u32,
|
||||
@@ -11,8 +31,17 @@ impl VirtualMachine {
|
||||
LogicalFrameEndingReason::Trap(self.program.create_trap(code, opcode, message, pc))
|
||||
}
|
||||
|
||||
pub fn push(&mut self, val: Value) {
|
||||
self.operand_stack.push(val);
|
||||
pub fn push(&mut self, val: Value) -> Result<(), ResourceFailure> {
|
||||
self.operand_stack.push(val)
|
||||
}
|
||||
|
||||
pub(super) fn push_trap(
|
||||
&mut self,
|
||||
val: Value,
|
||||
opcode: OpCode,
|
||||
pc: u32,
|
||||
) -> Result<(), LogicalFrameEndingReason> {
|
||||
self.push(val).map_err(|e| self.resource_trap(e, opcode, pc))
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) -> Result<Value, String> {
|
||||
@@ -67,7 +96,7 @@ impl VirtualMachine {
|
||||
let a = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
|
||||
match f(a, b) {
|
||||
Ok(res) => {
|
||||
self.push(res);
|
||||
self.push_trap(res, opcode, start_pc)?;
|
||||
Ok(())
|
||||
}
|
||||
Err(OpError::Trap(code, msg)) => Err(self.trap(code, opcode as u16, msg, start_pc)),
|
||||
|
||||
@@ -10,10 +10,11 @@ pub enum LoaderPatchError {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum VmInitError {
|
||||
RuntimeResourceLimit(crate::resource_budget::ResourceFailure),
|
||||
InvalidFormat,
|
||||
UnsupportedFormat,
|
||||
ImageLoadFailed(prometeu_bytecode::LoadError),
|
||||
LoaderPatchFailed(LoaderPatchError),
|
||||
EntrypointNotFound,
|
||||
VerificationFailed(String),
|
||||
VerificationFailed(crate::verifier::VerifierError),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user