Skip to main content

ic_memory/runtime/
error.rs

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