From fc1a6247e12302d3f6b47b4476cecfdd9d6f2b4e Mon Sep 17 00:00:00 2001 From: tignear Date: Mon, 26 Oct 2026 21:15:07 +0900 Subject: [PATCH] feat(runtime)!: store dynamic containers in the simulator heap ## Summary Add immutable, interned heap values for dynamic arrays, bounded and unbounded queues, and associative arrays. The heap helper supports resizing, element updates, queue operations, ordered traversal and four-state equality. Collection follows string and object elements, and checkpoints preserve container state. Intern completed updates so writing equal contents preserves handles and does not wake value-change event waits. Reject negative array sizes through runtime fault reporting. The SystemVerilog binding is the next member of this stack. ## Validation Validated this commit independently, without the frontend changes: - `cargo test --locked -p celox-heap -p celox-runtime -p celox --features celox/systemverilog,celox/cranelift,celox/wasm-runtime --lib`: 23 heap, 78 runtime and 231 Celox tests passed. - `cargo clippy --locked -p celox-heap -p celox-runtime -p celox --features celox/systemverilog,celox/cranelift,celox/wasm-runtime --lib`: passed. - `cargo fmt --all -- --check` and `git diff --check`: passed. BREAKING CHANGE: native image containers advance to version 45 and serialized core checkpoints to version 2. Regenerate artifacts produced by older versions. Change-Id: I21a75c9528004c0625b60f4263521c2e783f984e --- crates/celox-heap/src/containers.rs | 469 ++++++++++++++++++ crates/celox-heap/src/lib.rs | 25 +- crates/celox-heap/src/objects.rs | 6 + crates/celox-heap/src/ops.rs | 3 + crates/celox/src/backend/native/image_file.rs | 3 +- .../celox/src/simulation/checkpoint_file.rs | 2 +- 6 files changed, 501 insertions(+), 7 deletions(-) create mode 100644 crates/celox-heap/src/containers.rs diff --git a/crates/celox-heap/src/containers.rs b/crates/celox-heap/src/containers.rs new file mode 100644 index 00000000000..cf602f7bb14 --- /dev/null +++ b/crates/celox-heap/src/containers.rs @@ -0,0 +1,469 @@ +//! Value containers. Published entries are immutable; updates allocate a new +//! handle so ordinary state stores detect changes and array assignment copies +//! values. Class handles inside elements retain reference semantics. + +use std::collections::{BTreeMap, VecDeque}; +use std::sync::Arc; + +use crate::{Fault, Handle, Heap, Root}; + +pub const NEW: u64 = 80; +pub const LEN: u64 = 81; +pub const GET: u64 = 82; +pub const GET_MASK: u64 = 83; +pub const SET: u64 = 84; +/// Complete a newly created, unpublished SET/INSERT result with its X/Z mask. +pub const SET_MASK: u64 = 85; +pub const INSERT: u64 = 86; +pub const DELETE: u64 = 87; +pub const CLEAR: u64 = 88; +pub const EXISTS: u64 = 89; +pub const FIRST: u64 = 90; +pub const LAST: u64 = 91; +pub const NEXT: u64 = 92; +pub const PREV: u64 = 93; +pub const ITER_VALID: u64 = 94; +pub const EQUAL: u64 = 95; +pub const CASE_EQUAL: u64 = 96; + +/// The operation parameter encodes the element width (bits 0..6), whether +/// it is two-state (7), its handle kind (8..9: none/string/object), container +/// kind (10..11: dynamic/queue/associative), queue capacity (12..43, 0 means +/// unbounded), and associative key ordering (44 signed, 45 string). +pub const fn parameter( + width: u32, + two_state: bool, + handle: u64, + kind: u64, + capacity: u32, + signed_key: bool, + string_key: bool, +) -> u64 { + width as u64 + | (two_state as u64) << 7 + | handle << 8 + | kind << 10 + | (capacity as u64) << 12 + | (signed_key as u64) << 44 + | (string_key as u64) << 45 +} + +#[derive(Clone, Copy)] +struct Layout(u64); +impl Layout { + fn bits(self) -> u64 { + u64::MAX >> (64 - (self.0 & 127)) + } + fn default(self) -> Element { + Element { + value: if self.0 & 128 == 0 { self.bits() } else { 0 }, + mask: if self.0 & 128 == 0 { self.bits() } else { 0 }, + } + } + fn kind(self) -> u64 { + (self.0 >> 10) & 3 + } + fn capacity(self) -> usize { + let n = ((self.0 >> 12) & 0xffff_ffff) as usize; + if n == 0 { usize::MAX } else { n } + } + fn key(self, heap: &Heap, value: u64) -> Key { + if self.0 & (1 << 45) != 0 { + Key::String(Arc::from(heap.string(value as Handle))) + } else { + Key::Integer(value ^ if self.0 & (1 << 44) != 0 { 1 << 63 } else { 0 }) + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +struct Element { + value: u64, + mask: u64, +} +#[derive( + Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize, +)] +enum Key { + Integer(u64), + String(Arc<[u8]>), +} +#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +enum Contents { + Sequence(VecDeque), + Associative(BTreeMap), +} +#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] +pub(crate) struct Container { + layout: u64, + contents: Contents, +} +impl Container { + fn empty(layout: Layout) -> Self { + Self { + layout: layout.0, + contents: if layout.kind() == 2 { + Contents::Associative(BTreeMap::new()) + } else { + Contents::Sequence(VecDeque::new()) + }, + } + } + fn len(&self) -> usize { + match &self.contents { + Contents::Sequence(v) => v.len(), + Contents::Associative(v) => v.len(), + } + } + pub(crate) fn size(&self) -> usize { + self.len() * std::mem::size_of::() + + match &self.contents { + Contents::Associative(v) => v + .keys() + .map(|k| match k { + Key::String(s) => s.len(), + _ => 8, + }) + .sum(), + _ => 0, + } + } +} + +impl Heap { + fn publish_container(&mut self, container: Container, complete: bool) -> Handle { + if container.len() == 0 { + return 0; + } + let handle = self.next_object; + self.next_object = handle + .checked_add(1) + .expect("heap container handle space exhausted"); + self.containers.insert(handle, Arc::new(container)); + self.created += 1; + if complete { + self.finish_container(handle) + } else { + handle + } + } + fn finish_container(&mut self, handle: Handle) -> Handle { + let Some(container) = self.containers.get(&handle) else { + return handle; + }; + if let Some(&canonical) = self.container_lookup.get(container) { + if canonical != handle { + self.containers.remove(&handle); + } + canonical + } else { + self.container_lookup.insert(container.clone(), handle); + handle + } + } + pub(crate) fn container_roots(&self, handle: Handle) -> Vec { + let Some(container) = self.containers.get(&handle) else { + return Vec::new(); + }; + let kind = match (container.layout >> 8) & 3 { + 1 => crate::HandleKind::String, + 2 => crate::HandleKind::Object, + _ => return Vec::new(), + }; + let root = |element: &Element| Root { + kind, + handle: element.value as Handle, + }; + match &container.contents { + Contents::Sequence(v) => v.iter().map(root).collect(), + Contents::Associative(v) => v.values().map(root).collect(), + } + } + /// Execute a container helper. Indices have already undergone language + /// conversion; invalid four-state indices use an out-of-range sentinel. + pub(crate) fn container_call( + &mut self, + op: u64, + parameter: u64, + a: u64, + b: u64, + c: u64, + ) -> u64 { + let layout = Layout(parameter); + let key = layout.key(self, b); + let existing = self.containers.get(&(a as Handle)); + let get = || { + existing + .and_then(|entry| match &entry.contents { + Contents::Sequence(v) => usize::try_from(b).ok().and_then(|i| v.get(i)), + Contents::Associative(v) => v.get(&key), + }) + .copied() + .unwrap_or_else(|| layout.default()) + }; + match op { + EQUAL | CASE_EQUAL => { + let other = self.containers.get(&(b as Handle)); + let equal_element = |left: Element, right: Element| { + if op == CASE_EQUAL { + if left == right { 1 } else { 0 } + } else if (left.value ^ right.value) & !(left.mask | right.mask) != 0 { + 0 + } else if left.mask | right.mask != 0 { + 2 + } else { + 1 + } + }; + let mut unknown = false; + let mut compare = |left: Element, right: Element| { + let equal = equal_element(left, right); + unknown |= equal == 2; + equal != 0 + }; + let left = existing + .map(|entry| (**entry).clone()) + .unwrap_or_else(|| Container::empty(layout)); + let right = other + .map(|entry| (**entry).clone()) + .unwrap_or_else(|| Container::empty(layout)); + let equal = match (&left.contents, &right.contents) { + (Contents::Sequence(a), Contents::Sequence(b)) => { + a.len() == b.len() && a.iter().zip(b).all(|(&a, &b)| compare(a, b)) + } + (Contents::Associative(a), Contents::Associative(b)) => { + a.len() == b.len() + && a.iter() + .zip(b) + .all(|((ak, &a), (bk, &b))| ak == bk && compare(a, b)) + } + _ => false, + }; + return if !equal { + 0 + } else if unknown { + 3 + } else { + 1 + }; + } + LEN => return existing.map_or(0, |v| v.len() as u64), + GET => { + return if c == 0 { + get().value + } else { + layout.default().value + }; + } + GET_MASK => { + return if c == 0 { + get().mask + } else { + layout.default().mask + }; + } + EXISTS => { + return existing.is_some_and(|entry| match &entry.contents { + Contents::Associative(v) => v.contains_key(&key), + _ => false, + }) as u64; + } + CLEAR => return 0, + FIRST | LAST | NEXT | PREV | ITER_VALID => { + let traversal = if op == ITER_VALID { c } else { op }; + let found = existing.and_then(|entry| { + let Contents::Associative(v) = &entry.contents else { + return None; + }; + match traversal { + FIRST => v.first_key_value(), + LAST => v.last_key_value(), + NEXT => v + .range(( + std::ops::Bound::Excluded(key.clone()), + std::ops::Bound::Unbounded, + )) + .next(), + _ => v + .range(( + std::ops::Bound::Unbounded, + std::ops::Bound::Excluded(key.clone()), + )) + .next_back(), + } + .map(|(k, _)| k.clone()) + }); + if op == ITER_VALID { + return found.is_some() as u64; + } + return match found { + Some(Key::Integer(k)) => { + k ^ if layout.0 & (1 << 44) != 0 { + 1 << 63 + } else { + 0 + } + } + Some(Key::String(k)) => self.intern(&k) as u64, + None => 0, + }; + } + SET_MASK => { + if let Some(entry) = self.containers.get_mut(&(a as Handle)) { + let entry = Arc::make_mut(entry); + let element = match &mut entry.contents { + Contents::Sequence(v) => v.get_mut(b as usize), + Contents::Associative(v) => v.get_mut(&key), + }; + if let Some(element) = element { + element.mask = c & layout.bits(); + } + } + return self.finish_container(a as Handle) as u64; + } + NEW => { + // Bound allocation before converting to usize or reserving. + if (a as i64) < 0 { + self.fault.get_or_insert(Fault::NegativeArraySize); + return 0; + } + if a > 1 << 24 { + self.fault.get_or_insert(Fault::ContainerAllocationLimit); + return 0; + } + let mut result = Container::empty(layout); + let Contents::Sequence(v) = &mut result.contents else { + return 0; + }; + v.resize((a as usize).min(layout.capacity()), layout.default()); + if let Some(Container { + contents: Contents::Sequence(source), + .. + }) = self.containers.get(&(b as Handle)).map(AsRef::as_ref) + { + for (dst, src) in v.iter_mut().zip(source) { + *dst = *src; + } + } + return self.publish_container(result, true) as u64; + } + _ => {} + } + let mut result = existing + .map(|entry| (**entry).clone()) + .unwrap_or_else(|| Container::empty(layout)); + let value = Element { + value: c & layout.bits(), + mask: 0, + }; + match &mut result.contents { + Contents::Sequence(v) => { + let Ok(index) = usize::try_from(b) else { + return a; + }; + match op { + SET if index < v.len() => v[index] = value, + SET if layout.kind() == 1 && index == v.len() && index < layout.capacity() => { + v.push_back(value) + } + INSERT if index <= v.len() && index < layout.capacity() => { + v.insert(index, value); + if v.len() > layout.capacity() { + v.pop_back(); + } + } + DELETE if index < v.len() => { + v.remove(index); + } + _ => return a, + } + } + Contents::Associative(v) => match op { + SET => { + v.insert(key, value); + } + DELETE => { + if v.remove(&key).is_none() { + return a; + } + } + _ => return a, + }, + } + self.publish_container(result, !matches!(op, SET | INSERT)) as u64 + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::ops::encode; + fn call(heap: &mut Heap, op: u64, p: u64, a: u64, b: u64, c: u64) -> u64 { + heap.call(encode(op, p), a, b, c) + } + #[test] + fn completed_updates_intern_values_and_collection_releases_lookup_entries() { + let mut heap = Heap::new(); + let p = parameter(8, true, 0, 0, 0, false, false); + let a = call(&mut heap, NEW, p, 2, 0, 0); + let b = call(&mut heap, SET, p, a, 0, 7); + let b = call(&mut heap, SET_MASK, p, b, 0, 0); + let same = call(&mut heap, SET, p, b, 0, 7); + assert_eq!(call(&mut heap, SET_MASK, p, same, 0, 0), b); + assert_eq!(call(&mut heap, NEW, p, 2, b, 0), b); + heap.collect([Root::object(b as Handle)]); + assert_eq!(heap.container_lookup.len(), 1); + let old = b; + heap.collect([]); + assert!(heap.container_lookup.is_empty()); + let new = call(&mut heap, NEW, p, 2, 0, 0); + assert_ne!(old, new); + } + + #[test] + fn resize_copy_delete_and_four_state_defaults() { + let mut heap = Heap::new(); + let p = parameter(32, false, 0, 0, 0, false, false); + let a = call(&mut heap, NEW, p, 2, 0, 0); + assert_eq!(call(&mut heap, GET_MASK, p, a, 0, 0), u32::MAX as u64); + let b = call(&mut heap, SET, p, a, 0, 42); + assert_eq!(call(&mut heap, GET, p, a, 0, 0), u32::MAX as u64); + let c = call(&mut heap, NEW, p, 4, b, 0); + assert_eq!(call(&mut heap, GET, p, c, 0, 0), 42); + assert_eq!(call(&mut heap, GET_MASK, p, c, 3, 0), u32::MAX as u64); + assert_eq!(call(&mut heap, SET, p, c, u64::MAX, 3), c); + assert_eq!(call(&mut heap, CLEAR, p, c, 0, 0), 0); + call(&mut heap, NEW, p, u64::MAX, 0, 0); + assert_eq!(heap.take_fault(), Some(Fault::NegativeArraySize)); + } + #[test] + fn bounded_queue_insert_and_value_copies() { + let mut heap = Heap::new(); + let p = parameter(8, true, 0, 1, 2, false, false); + let a = call(&mut heap, INSERT, p, 0, 0, 10); + let b = call(&mut heap, INSERT, p, a, 0, 20); + let c = call(&mut heap, INSERT, p, b, 0, 30); + assert_eq!(call(&mut heap, LEN, p, c, 0, 0), 2); + assert_eq!(call(&mut heap, GET, p, c, 0, 0), 30); + assert_eq!(call(&mut heap, GET, p, c, 1, 0), 20); + assert_eq!(call(&mut heap, GET, p, b, 1, 0), 10); + assert_eq!(call(&mut heap, INSERT, p, c, 2, 40), c); + } + #[test] + fn associative_order_and_gc_follow_element_handles() { + let mut heap = Heap::new(); + let p = parameter(32, true, 1, 2, 0, true, false); + let string = heap.intern(b"live"); + let a = call(&mut heap, SET, p, 0, 3, string as u64); + let b = call(&mut heap, SET, p, a, (-2i64) as u64, string as u64); + assert_eq!(call(&mut heap, FIRST, p, b, 0, 0), (-2i64) as u64); + assert_eq!(call(&mut heap, NEXT, p, b, (-2i64) as u64, 0), 3); + let before = heap.clone(); + heap.collect([Root::object(b as Handle)]); + assert!(!heap.same_contents(&before)); + assert!(heap.contains(string)); + assert!(!heap.containers.contains_key(&(a as Handle))); + heap.collect([]); + assert!(!heap.contains(string)); + } +} diff --git a/crates/celox-heap/src/lib.rs b/crates/celox-heap/src/lib.rs index e280424d429..ad28602712c 100644 --- a/crates/celox-heap/src/lib.rs +++ b/crates/celox-heap/src/lib.rs @@ -16,6 +16,7 @@ use std::sync::Arc; use fxhash::{FxHashMap as HashMap, FxHashSet as HashSet}; +pub mod containers; mod objects; pub mod ops; @@ -64,7 +65,7 @@ impl Root { } } -/// Dynamic strings and class objects referenced by [`Handle`]. +/// Dynamic strings, value containers and class objects referenced by [`Handle`]. #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Heap { strings: HashMap>, @@ -77,6 +78,8 @@ pub struct Heap { objects: HashMap, /// The handle the next object gets. next_object: Handle, + containers: HashMap>, + container_lookup: HashMap, Handle>, /// The first heap operation error since the runtime last checked. fault: Option, /// Entries below this handle are never collected. @@ -106,6 +109,8 @@ impl Heap { free_buffers: Vec::new(), objects: HashMap::default(), next_object: NULL + 1, + containers: HashMap::default(), + container_lookup: HashMap::default(), fault: None, pinned: 1, created: 0, @@ -150,7 +155,7 @@ impl Heap { self.strings.contains_key(&handle) } - /// Whether `other` holds the same strings and objects under the same + /// Whether `other` holds the same strings, containers and objects under the same /// handles, including object RNG state and strings under construction, /// and assigns the same handles to future allocations. pub fn same_contents(&self, other: &Heap) -> bool { @@ -161,6 +166,7 @@ impl Heap { && self.pinned == other.pinned && self.objects == other.objects && self.next_object == other.next_object + && self.containers == other.containers } /// The number of live strings, including `""`. @@ -172,7 +178,7 @@ impl Heap { false } - /// Bytes of string contents and object properties, excluding allocator + /// Bytes of string contents, container elements and object properties, excluding allocator /// overhead. pub fn content_size(&self) -> usize { self.strings @@ -184,6 +190,11 @@ impl Heap { .values() .map(objects::Object::size) .sum::() + + self + .containers + .values() + .map(|container| container.size()) + .sum::() } /// Whether enough entries were created since the last collection to make @@ -205,12 +216,13 @@ impl Heap { strings.insert(root.handle); } HandleKind::Object => { - if self.objects.contains_key(&root.handle) && objects.insert(root.handle) { + if objects.insert(root.handle) { pending.extend( self.handles_of(root.handle) .into_iter() .map(|(kind, handle)| Root { kind, handle }), ); + pending.extend(self.container_roots(root.handle)); } } } @@ -225,11 +237,14 @@ impl Heap { live }); self.objects.retain(|handle, _| objects.contains(handle)); + self.containers.retain(|handle, _| objects.contains(handle)); + self.container_lookup + .retain(|_, handle| objects.contains(handle)); // No buffer outlives the kernel that built it. self.buffers.clear(); self.free_buffers.clear(); self.created = 0; - self.live_after_collection = self.len() + self.objects.len(); + self.live_after_collection = self.len() + self.objects.len() + self.containers.len(); } fn new_buffer(&mut self, bytes: Vec) -> u64 { diff --git a/crates/celox-heap/src/objects.rs b/crates/celox-heap/src/objects.rs index 097a345e198..19644b31990 100644 --- a/crates/celox-heap/src/objects.rs +++ b/crates/celox-heap/src/objects.rs @@ -49,6 +49,8 @@ impl Object { /// reports once control returns to it. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Fault { + NegativeArraySize, + ContainerAllocationLimit, /// A property access or method call through `null` or a handle of no /// live object (IEEE 1800-2023 8.4). NullDereference, @@ -59,6 +61,10 @@ pub enum Fault { impl std::fmt::Display for Fault { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + Self::NegativeArraySize => f.write_str("negative dynamic array size"), + Self::ContainerAllocationLimit => { + f.write_str("dynamic container allocation limit exceeded") + } Self::NullDereference => f.write_str("null object handle dereferenced"), Self::RandomizationClassMismatch => { f.write_str("class randomization through a base handle is unsupported") diff --git a/crates/celox-heap/src/ops.rs b/crates/celox-heap/src/ops.rs index 1b725feabc3..c1720c54a97 100644 --- a/crates/celox-heap/src/ops.rs +++ b/crates/celox-heap/src/ops.rs @@ -104,6 +104,9 @@ impl Heap { pub fn call(&mut self, code: u64, a: u64, b: u64, c: u64) -> u64 { let parameter = code >> 8; match code & 0xff { + op @ crate::containers::NEW..=crate::containers::CASE_EQUAL => { + self.container_call(op, parameter, a, b, c) + } COMPARE => ordering(self.string(a as Handle).cmp(self.string(b as Handle))), ICOMPARE => { let lower = |handle| { diff --git a/crates/celox/src/backend/native/image_file.rs b/crates/celox/src/backend/native/image_file.rs index 71fc64b00c4..fa0263d5096 100644 --- a/crates/celox/src/backend/native/image_file.rs +++ b/crates/celox/src/backend/native/image_file.rs @@ -6,7 +6,8 @@ use std::{fmt, path::Path}; use super::backend::NativeProgramImage; const TRAILER_MAGIC: &[u8; 8] = b"CELOXNPI"; -const CONTAINER_VERSION: u16 = 44; +// Container heap opcodes require a runtime that understands dynamic containers. +const CONTAINER_VERSION: u16 = 45; const TRAILER_SIZE: usize = 32; const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325; const FNV_PRIME: u64 = 0x0000_0100_0000_01b3; diff --git a/crates/celox/src/simulation/checkpoint_file.rs b/crates/celox/src/simulation/checkpoint_file.rs index 7ce107a4170..be5fafaf83c 100644 --- a/crates/celox/src/simulation/checkpoint_file.rs +++ b/crates/celox/src/simulation/checkpoint_file.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; const MAGIC: &[u8; 8] = b"CELOXCP1"; // Bump whenever a serialized core-state schema or its interpretation changes. -const VERSION: u32 = 1; +const VERSION: u32 = 2; #[derive(Debug, thiserror::Error)] pub enum CheckpointFileError {