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