Skip to main content

javm_cap/
instance.rs

1//! `InstanceCap` — Instance cap with mutable working state.
2//!
3//! Holds the mutable working state of a running Cap::Instance:
4//! image reference (by hash, since Images are immutable), root
5//! cnode reference (by hash when clean / by ref while mutating),
6//! per-mapping rw overlays, register file, PC, gas counter.
7
8use alloc::vec::Vec;
9
10use super::cap::{CapHash, CapHashOrRef, NUM_REGS};
11
12#[derive(Clone, Debug, ssz_derive::HashTreeRoot)]
13pub struct InstanceCap {
14    /// Cumulative chain hash identifying the Instance's type.
15    pub image_hash_chain: CapHash,
16    /// Hash of the Image cap currently bound. Always content-
17    /// addressed (Images are immutable).
18    pub image_hash: CapHash,
19    /// Reference to the root cnode. `Hash` when clean / not yet
20    /// promoted for mutation; `Ref` while the running Instance is
21    /// mutating it via CoW.
22    pub root_cnode: CapHashOrRef,
23    /// Mutable byte overlays per memory mapping. Each entry's
24    /// `start` matches one of the Image's `MemoryMapping.start`
25    /// values; `bytes` is the per-instance content (initial state
26    /// at boot, then evolves under JIT writes).
27    pub rw_overlays: Vec<RwOverlay>,
28    /// Total addressable memory size for the Instance.
29    pub mem_size: u32,
30    /// PVM register file (`φ[0]..φ[12]`).
31    pub regs: [u64; NUM_REGS],
32    /// Current PC. Zero between calls; updated to entry_pc at
33    /// invoke start and to the post-execution PC on HALT.
34    pub pc: u64,
35    /// Gas left after the last call, for callers that want to
36    /// observe residual gas. Set to 0 between calls in V1.
37    pub gas_remaining: u64,
38}
39
40/// One byte overlay backing a memory mapping. `bytes.len()` ≤
41/// the mapping's `size`; trailing untouched bytes default to zero.
42#[derive(Clone, Debug, ssz_derive::Encode, ssz_derive::HashTreeRoot)]
43pub struct RwOverlay {
44    pub start: u32,
45    pub bytes: Vec<u8>,
46}