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