1mod admission;
3mod custody;
4mod directory_model;
5
6use crate::{Memory, StableKey};
7pub use admission::{
8 AdmissionError, AllocationGrants, BootstrapAdmission, GenericAllocationPolicy, MemoryAuthority,
9 MemoryRequest, PolicyIdentity, RuntimeBootstrapPolicy, SealedDeclarationSnapshot,
10};
11pub use custody::{BackingIdentity, MemoryBacking};
12pub use directory_model::Error as DirectoryError;
13use directory_model::leases::{Lease, Owner};
14pub use directory_model::{AllocationId, StorageConfig};
15use std::collections::BTreeSet;
16
17#[derive(Debug, thiserror::Error)]
19pub enum RuntimeConstructionError {
20 #[error("backing already claimed")]
22 BackingAlreadyClaimed,
23 #[error(transparent)]
25 Directory(#[from] DirectoryError),
26}
27
28#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
30pub enum RuntimeGrowError {
31 #[error("allocation page count overflow")]
33 ArithmeticOverflow,
34 #[error("allocation capacity exceeded")]
36 CapacityExceeded,
37 #[error("backing refused growth by {additional_pages} pages")]
39 BackingRefused { additional_pages: u64 },
40 #[error("allocation owner is poisoned")]
42 Poisoned,
43 #[error("allocation directory validation failed")]
45 InvalidDirectory,
46}
47
48#[derive(Debug, thiserror::Error)]
50pub enum RuntimeOpenError {
51 #[error("runtime is not bootstrapped")]
53 NotBootstrapped,
54 #[error("key is not admitted: {0}")]
56 NotAdmitted(StableKey),
57 #[error("allocation {id} already open")]
59 AlreadyOpen { id: u64 },
60 #[error(transparent)]
62 Directory(#[from] DirectoryError),
63}
64
65#[derive(Debug, thiserror::Error)]
67pub enum RuntimeBootstrapError<E> {
68 #[error(transparent)]
70 Admission(#[from] AdmissionError),
71 #[error("host policy rejected allocation")]
73 Policy(E),
74 #[error("bootstrap binding mismatch")]
76 BindingMismatch,
77 #[error("runtime is not bootstrapped")]
79 NotBootstrapped,
80 #[error(transparent)]
82 Directory(#[from] DirectoryError),
83}
84
85struct Binding {
86 source: SealedDeclarationSnapshot,
87 grants: AllocationGrants,
88 policy: PolicyIdentity,
89 admitted: BTreeSet<StableKey>,
90}
91
92pub struct MemoryRuntime<M: Memory> {
101 owner: Owner<M>,
102 config: StorageConfig,
103 binding: Option<Binding>,
104}
105
106impl<M: Memory> MemoryRuntime<M> {
107 pub fn new(memory: M) -> Result<Self, RuntimeConstructionError>
112 where
113 M: MemoryBacking,
114 {
115 Self::new_with_config(memory, StorageConfig::default())
116 }
117 pub fn new_with_config(
122 memory: M,
123 config: StorageConfig,
124 ) -> Result<Self, RuntimeConstructionError>
125 where
126 M: MemoryBacking,
127 {
128 let owner = Owner::init(memory, config).map_err(|error| match error {
129 DirectoryError::AlreadyClaimed => RuntimeConstructionError::BackingAlreadyClaimed,
130 error => RuntimeConstructionError::Directory(error),
131 })?;
132 Ok(Self {
133 owner,
134 config,
135 binding: None,
136 })
137 }
138 #[must_use]
140 pub const fn is_bootstrapped(&self) -> bool {
141 self.binding.is_some()
142 }
143 pub fn committed_generation(&self) -> Result<u64, RuntimeOpenError> {
148 if self.binding.is_none() {
149 return Err(RuntimeOpenError::NotBootstrapped);
150 }
151 Ok(self.owner.generation())
152 }
153 #[must_use]
155 pub const fn config(&self) -> StorageConfig {
156 self.config
157 }
158 pub fn bootstrap<P: RuntimeBootstrapPolicy>(
163 &mut self,
164 source: &SealedDeclarationSnapshot,
165 grants: &AllocationGrants,
166 policy: &P,
167 ) -> Result<u64, RuntimeBootstrapError<P::Error>> {
168 if self.binding.is_some() {
169 return self.verify_bootstrap(source, grants, policy);
170 }
171 self.owner.verify()?;
172 let policy_identity = policy.runtime_bootstrap_identity();
173 let bindings = self.owner.bindings();
174 let mut admission =
175 BootstrapAdmission::new(source, grants, &bindings, self.config.allocations)?;
176 let result = policy.prepare_bootstrap(&mut admission);
177 let (requests, admitted) = admission.complete()?;
178 result.map_err(RuntimeBootstrapError::Policy)?;
179 for request in requests {
180 policy
181 .validate_allocation(request.stable_key())
182 .map_err(RuntimeBootstrapError::Policy)?;
183 }
184 self.owner.bind(&admitted)?;
185 self.binding = Some(Binding {
186 source: source.clone(),
187 grants: grants.clone(),
188 policy: policy_identity,
189 admitted,
190 });
191 Ok(self.owner.generation())
192 }
193 pub fn verify_bootstrap<P: RuntimeBootstrapPolicy>(
198 &self,
199 source: &SealedDeclarationSnapshot,
200 grants: &AllocationGrants,
201 policy: &P,
202 ) -> Result<u64, RuntimeBootstrapError<P::Error>> {
203 let binding = self
204 .binding
205 .as_ref()
206 .ok_or(RuntimeBootstrapError::NotBootstrapped)?;
207 if binding.source != *source
208 || binding.grants != *grants
209 || binding.policy != policy.runtime_bootstrap_identity()
210 {
211 return Err(RuntimeBootstrapError::BindingMismatch);
212 }
213 Ok(self.owner.generation())
214 }
215 pub fn open_memory(&self, key: &StableKey) -> Result<RuntimeMemory<M>, RuntimeOpenError> {
225 let binding = self
226 .binding
227 .as_ref()
228 .ok_or(RuntimeOpenError::NotBootstrapped)?;
229 if !binding.admitted.contains(key) {
230 return Err(RuntimeOpenError::NotAdmitted(key.clone()));
231 }
232 let lease = self.owner.lease(key).map_err(|error| match error {
233 DirectoryError::AlreadyOpen => RuntimeOpenError::AlreadyOpen {
234 id: self.allocation_id(key).unwrap().get(),
235 },
236 error => RuntimeOpenError::Directory(error),
237 })?;
238 Ok(RuntimeMemory { lease })
239 }
240 pub fn verify_authority(
245 &self,
246 requirements: &SealedDeclarationSnapshot,
247 authority: &str,
248 ) -> Result<(), RuntimeOpenError> {
249 let binding = self
250 .binding
251 .as_ref()
252 .ok_or(RuntimeOpenError::NotBootstrapped)?;
253 for request in requirements.requests() {
254 if request.authority() != authority
255 || !binding.admitted.contains(request.stable_key())
256 || binding.grants.check(request).is_err()
257 {
258 return Err(RuntimeOpenError::NotAdmitted(request.stable_key().clone()));
259 }
260 }
261 Ok(())
262 }
263 #[must_use]
265 pub fn allocation_id(&self, key: &StableKey) -> Option<AllocationId> {
266 self.owner.allocation_id(key)
267 }
268 pub fn memory_allocations(&self) -> Result<Vec<MemoryAllocation>, DirectoryError> {
273 self.owner.verify()?;
274 Ok(self
275 .owner
276 .bindings()
277 .into_iter()
278 .map(|(stable_key, id, pages)| MemoryAllocation {
279 stable_key,
280 id,
281 pages,
282 })
283 .collect())
284 }
285
286 pub fn diagnostics(&self) -> Result<StorageDiagnostics, DirectoryError> {
293 let mut report = self.owner.diagnostics()?;
294 report.admitted_allocations = self.binding.as_ref().map_or(0, |b| b.admitted.len());
295 Ok(report)
296 }
297}
298
299#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize)]
309pub struct StorageDiagnostics {
310 pub generation: u64,
312 pub allocations: usize,
314 pub admitted_allocations: usize,
316 pub live_leases: usize,
318 pub extents: usize,
320 pub data_pages: u64,
322 pub metadata_pages: u64,
324 pub directory_bytes: u64,
326 pub physical_pages: u64,
328 pub unassigned_pages: u64,
330}
331
332#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
338pub struct MemoryAllocation {
339 pub stable_key: StableKey,
341 pub id: AllocationId,
343 pub pages: u64,
345}
346
347pub struct RuntimeMemory<M: Memory> {
355 lease: Lease<M>,
356}
357impl<M: Memory> RuntimeMemory<M> {
358 #[cfg(not(target_arch = "wasm32"))]
359 pub(crate) fn poison_on_unwind(&self) -> directory_model::leases::PanicGuard<M> {
360 self.lease.poison_on_unwind()
361 }
362 #[must_use]
364 pub fn size(&self) -> u64 {
365 self.lease.size()
366 }
367 pub fn grow(&self, pages: u64) -> Result<u64, RuntimeGrowError> {
372 self.lease.try_grow(pages).map_err(|error| match error {
373 DirectoryError::ArithmeticOverflow => RuntimeGrowError::ArithmeticOverflow,
374 DirectoryError::Growth => RuntimeGrowError::BackingRefused {
375 additional_pages: pages,
376 },
377 DirectoryError::Budget => RuntimeGrowError::CapacityExceeded,
378 DirectoryError::Poisoned => RuntimeGrowError::Poisoned,
379 _ => RuntimeGrowError::InvalidDirectory,
380 })
381 }
382 pub fn read(&self, offset: u64, bytes: &mut [u8]) {
384 self.lease.read(offset, bytes);
385 }
386 pub fn write(&self, offset: u64, bytes: &[u8]) {
388 self.lease.write(offset, bytes);
389 }
390}
391impl<M: Memory> Memory for RuntimeMemory<M> {
392 fn size(&self) -> u64 {
393 self.size()
394 }
395 fn grow(&self, pages: u64) -> i64 {
396 self.grow(pages)
397 .map_or(-1, |previous| i64::try_from(previous).expect("page count"))
398 }
399 fn read(&self, offset: u64, bytes: &mut [u8]) {
400 self.read(offset, bytes);
401 }
402 #[expect(
403 unsafe_code,
404 reason = "Forward the raw-read contract through the governed lease"
405 )]
406 unsafe fn read_unsafe(&self, offset: u64, dst: *mut u8, count: usize) {
407 unsafe { self.lease.read_unsafe(offset, dst, count) }
409 }
410 fn write(&self, offset: u64, bytes: &[u8]) {
411 self.write(offset, bytes);
412 }
413}