Skip to main content

ic_memory/runtime/
error.rs

1use crate::{
2    LedgerCommitError, PolicyIdentity, StableCellLedgerError, requests::MemoryDeclarationError,
3};
4
5///
6/// RuntimeGrowError
7///
8/// Failure to grow a runtime memory before assigning new manager buckets.
9/// Ordinary capacity failures preserve virtual extents and manager metadata.
10/// Backing traps and partial writes remain outside this guarantee.
11///
12
13#[non_exhaustive]
14#[derive(Clone, Copy, Debug, Eq, thiserror::Error, PartialEq)]
15pub enum RuntimeGrowError {
16    /// The requested virtual extent overflows the page count.
17    #[error("virtual memory page count overflows")]
18    ArithmeticOverflow,
19    /// The sole manager has insufficient bucket slots.
20    #[error("growth requires {required_buckets} buckets, exceeding capacity {capacity}")]
21    BucketExhausted {
22        required_buckets: u64,
23        capacity: u16,
24    },
25    /// The backing memory refused the physical capacity reservation.
26    #[error("backing memory refused growth by {additional_pages} pages")]
27    BackingRefused { additional_pages: u64 },
28    /// Growth re-entered while another handle held a capacity reservation.
29    #[error("runtime memory growth is already in progress")]
30    ReentrantAccess,
31}
32
33///
34/// RuntimeConstructionError
35///
36/// Failure to construct a memory runtime without overwriting unrecognized
37/// backing memory.
38///
39
40#[non_exhaustive]
41#[derive(Clone, Copy, Debug, Eq, thiserror::Error, PartialEq)]
42pub enum RuntimeConstructionError {
43    /// Fresh manager metadata could not reserve physical capacity.
44    #[error(transparent)]
45    Growth(#[from] RuntimeGrowError),
46    /// Zero pages cannot form a bucket.
47    #[error("bucket size must be nonzero")]
48    InvalidBucketSize,
49    /// Explicit policy differs from the actual durable setting.
50    #[error("persisted bucket size {persisted} pages differs from requested {requested}")]
51    BucketSizeMismatch { persisted: u16, requested: u16 },
52    /// Persisted manager metadata failed bounded validation.
53    #[error(transparent)]
54    Layout(#[from] super::MemoryManagerLayoutError),
55    /// Nonempty backing memory does not contain a `MemoryManager` header.
56    #[error(
57        "nonempty backing memory is not an ic-stable-structures MemoryManager \
58         (expected magic 'MGR', found bytes {observed_magic:?})"
59    )]
60    ForeignMemory {
61        /// First three bytes found in the nonempty backing memory.
62        observed_magic: [u8; 3],
63    },
64    /// Backing memory contains an unsupported `MemoryManager` layout version.
65    #[error(
66        "unsupported ic-stable-structures MemoryManager layout version {observed}; \
67         expected {supported}"
68    )]
69    UnsupportedMemoryManagerVersion {
70        /// Version byte found after the `MemoryManager` magic.
71        observed: u8,
72        /// Version supported by the pinned `ic-stable-structures` dependency.
73        supported: u8,
74    },
75}
76
77///
78/// RuntimeBootstrapError
79///
80/// Failure to bootstrap one `MemoryRuntime`.
81///
82
83#[non_exhaustive]
84#[derive(Debug, thiserror::Error)]
85pub enum RuntimeBootstrapError<P> {
86    /// A known-only historical selection failed before commitment.
87    #[error(transparent)]
88    Admission(#[from] super::BootstrapAdmissionError),
89    /// Consumer identity or key-set admission rejected this attempt.
90    #[error("bootstrap admission policy rejected recovered allocation metadata")]
91    AdmissionPolicy(P),
92    #[error(transparent)]
93    Resolution(#[from] MemoryResolutionError),
94    /// A bootstrapped runtime was called with a different declaration snapshot.
95    #[error("runtime bootstrap declaration snapshot differs from the established binding")]
96    DeclarationSnapshotMismatch,
97    /// A repeated bootstrap attempted to replace host allocation policy.
98    #[error("runtime allocation pool differs from the established host policy")]
99    AllocationPoolMismatch,
100    /// A bootstrapped runtime was called with a different policy identity.
101    #[error("runtime bootstrap policy identity changed from {established:?} to {requested:?}")]
102    PolicyIdentityMismatch {
103        /// Policy identity established by successful bootstrap.
104        established: PolicyIdentity,
105        /// Policy identity supplied by the repeated call.
106        requested: PolicyIdentity,
107    },
108    /// Protected ledger recovery or commit failed.
109    #[error(transparent)]
110    LedgerCommit(#[from] crate::LedgerCommitError),
111    /// Stable-cell ledger storage is corrupt before protected recovery can run.
112    #[error(transparent)]
113    StableCellLedger(#[from] StableCellLedgerError),
114    /// The encoded stable-cell ledger record exceeds its bounded size ceiling.
115    #[error("stable-cell ledger record size {value_size} cannot be written to stable memory")]
116    StableCellLedgerWriteTooLarge {
117        /// Encoded stable-cell ledger record size in bytes.
118        value_size: usize,
119    },
120    /// Stable-cell ledger capacity reservation failed before commitment.
121    #[error(transparent)]
122    LedgerGrowth(#[from] RuntimeGrowError),
123    /// Declaration validation failed.
124    #[error(transparent)]
125    Validation(#[from] crate::AllocationValidationError<P>),
126    /// Validated declarations could not be staged.
127    #[error(transparent)]
128    Staging(#[from] crate::AllocationStageError),
129}
130
131///
132/// RuntimeOpenError
133///
134/// Failure to open an allocation through one memory runtime.
135///
136
137#[non_exhaustive]
138#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
139pub enum RuntimeOpenError {
140    /// This runtime already issued a live lease for the allocation.
141    #[error("MemoryManager ID {id} already has a live allocation lease")]
142    AlreadyOpen { id: u8 },
143    /// This runtime has not published committed allocations.
144    #[error("ic-memory runtime has not completed bootstrap validation")]
145    NotBootstrapped,
146    /// Stable-key grammar failure.
147    #[error(transparent)]
148    StableKey(#[from] crate::StableKeyError),
149    /// The stable key was not present in this runtime's committed declaration set.
150    #[error("stable key '{0}' was not committed by ic-memory runtime bootstrap")]
151    StableKeyNotCommitted(String),
152    /// Runtime governance stable keys are internal and cannot be opened publicly.
153    #[error("stable key '{stable_key}' is reserved for ic-memory runtime governance")]
154    ReservedStableKey {
155        /// Reserved stable key.
156        stable_key: String,
157    },
158}
159
160///
161/// RuntimeDiagnosticError
162///
163/// Failure to build diagnostics for one memory runtime.
164///
165
166#[non_exhaustive]
167#[derive(Debug, thiserror::Error)]
168pub enum RuntimeDiagnosticError {
169    /// Persisted manager metadata is invalid or unsupported.
170    #[error(transparent)]
171    Construction(#[from] RuntimeConstructionError),
172    /// This operation requires completed bootstrap.
173    #[error("ic-memory runtime has not completed bootstrap validation")]
174    NotBootstrapped,
175    /// Growth currently holds the shared bucket accounting state.
176    #[error("runtime memory growth is already in progress")]
177    ReentrantAccess,
178    /// The recovered allocation ledger failed protected commit validation.
179    #[error(transparent)]
180    LedgerCommit(#[from] LedgerCommitError),
181    /// Stable-cell ledger storage is corrupt before protected recovery can run.
182    #[error(transparent)]
183    StableCellLedger(#[from] StableCellLedgerError),
184}
185
186///
187/// MemoryResolutionError
188///
189/// Logical placement failed before publishing allocation authority.
190///
191
192#[non_exhaustive]
193#[derive(Debug, thiserror::Error)]
194pub enum MemoryResolutionError {
195    /// Populated unmanaged memory lacks an explicit host exclusion.
196    #[error("unmanaged MemoryManager ID {id} needs an explicit host exclusion")]
197    UnmanagedAllocation { id: u8 },
198    #[error("no eligible free slot for {stable_key} under authority {authority}")]
199    Exhausted {
200        stable_key: crate::StableKey,
201        authority: String,
202    },
203    #[error(transparent)]
204    Pool(#[from] crate::MemoryAllocationPoolError),
205    #[error(transparent)]
206    Declarations(#[from] MemoryDeclarationError),
207}