ic_memory/runtime/
layout.rs1use super::RuntimeConstructionError;
5use crate::WASM_PAGE_SIZE_BYTES;
6use ic_platform::stable_structures::Memory;
7
8pub(super) const IDS: usize = 255;
9pub(super) const BUCKET_CAPACITY: u16 = 32_768;
10pub(super) const BUCKETS: usize = BUCKET_CAPACITY as usize;
11pub(super) const HEADER_BYTES: usize = 40 + IDS * 8;
12pub(super) const METADATA_BYTES: usize = HEADER_BYTES + BUCKETS;
13
14#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
22#[non_exhaustive]
23pub enum MemoryManagerLayoutError {
24 #[error("unsupported big-endian manager layout")]
26 UnsupportedByteOrder,
27 #[error("persisted bucket size is zero")]
29 ZeroBucketSize,
30 #[error("nonzero reserved manager header bytes")]
32 ReservedHeader,
33 #[error("allocated bucket count {count} exceeds 32768")]
35 BucketCount { count: u16 },
36 #[error("invalid bucket table entry at index {index}")]
38 BucketTable { index: u16 },
39 #[error("virtual extent and bucket count disagree for memory ID {id}")]
41 VirtualExtent { id: u8 },
42 #[error("physical extent {physical_pages} pages is below assigned end {required_pages}")]
44 TruncatedBacking {
45 physical_pages: u64,
46 required_pages: u64,
47 },
48 #[error("backing memory extent overflows bytes")]
50 ExtentOverflow,
51 #[error("persisted manager metadata differs from runtime authority")]
53 RuntimeMismatch,
54}
55
56pub(super) struct Layout {
57 pub metadata: [u8; METADATA_BYTES],
58 pub physical_pages: u64,
59 pub bucket_pages: u16,
60 pub allocated_buckets: u16,
61 pub pages: [u64; IDS],
62 pub buckets: [u16; IDS],
63}
64
65pub(super) struct ManagerAuthority {
69 expected: Box<[u8]>,
70 poisoned: std::cell::Cell<bool>,
71}
72
73impl ManagerAuthority {
74 pub fn capture(memory: &impl Memory) -> Self {
75 let mut expected = vec![0; METADATA_BYTES].into_boxed_slice();
76 memory.read(0, &mut expected);
77 Self {
78 expected,
79 poisoned: std::cell::Cell::new(false),
80 }
81 }
82
83 pub fn allocated_buckets(&self) -> u16 {
84 u16::from_le_bytes([self.expected[4], self.expected[5]])
85 }
86
87 pub fn verify(&self, memory: &impl Memory) -> Result<(), MemoryManagerLayoutError> {
88 if self.poisoned.get() {
89 return Err(MemoryManagerLayoutError::RuntimeMismatch);
90 }
91 #[expect(
92 clippy::large_stack_arrays,
93 reason = "fixed manager metadata bound, no payload reads or heap allocation"
94 )]
95 let mut actual = [0; METADATA_BYTES];
96 let required_pages = 1 + u64::from(self.allocated_buckets())
97 * u64::from(u16::from_le_bytes([self.expected[6], self.expected[7]]));
98 if memory.size() < required_pages {
99 self.poisoned.set(true);
100 return Err(MemoryManagerLayoutError::RuntimeMismatch);
101 }
102 memory.read(0, &mut actual);
103 self.verify_observed(&actual)
104 }
105
106 pub fn poison(&self) {
107 self.poisoned.set(true);
108 }
109
110 pub fn verify_observed(&self, actual: &[u8]) -> Result<(), MemoryManagerLayoutError> {
111 #[cfg(target_arch = "wasm32")]
115 let matches = {
116 let (actual_words, actual_tail) = actual.as_chunks::<8>();
117 let (expected_words, expected_tail) = self.expected.as_chunks::<8>();
118 actual.len() == METADATA_BYTES
119 && self.expected.len() == METADATA_BYTES
120 && actual_tail.is_empty()
121 && expected_tail.is_empty()
122 && actual_words
123 .iter()
124 .zip(expected_words)
125 .all(|(actual, expected)| {
126 u64::from_le_bytes(*actual) == u64::from_le_bytes(*expected)
127 })
128 };
129 #[cfg(not(target_arch = "wasm32"))]
132 let matches = actual.len() == METADATA_BYTES && actual == self.expected.as_ref();
133 if self.poisoned.get() || !matches {
134 self.poisoned.set(true);
135 return Err(MemoryManagerLayoutError::RuntimeMismatch);
136 }
137 Ok(())
138 }
139
140 pub fn record_growth(&mut self, id: u8, pages: u64, allocated: u16) {
141 let old = usize::from(self.allocated_buckets());
142 self.expected[HEADER_BYTES + old..HEADER_BYTES + usize::from(allocated)].fill(id);
143 self.expected[4..6].copy_from_slice(&allocated.to_le_bytes());
144 let offset = 40 + usize::from(id) * 8;
145 self.expected[offset..offset + 8].copy_from_slice(&pages.to_le_bytes());
146 }
147}
148
149pub(super) fn read<M: Memory>(memory: &M) -> Result<Layout, RuntimeConstructionError> {
150 if cfg!(target_endian = "big") {
151 return Err(MemoryManagerLayoutError::UnsupportedByteOrder.into());
152 }
153 let physical_pages = memory.size();
154 physical_pages
155 .checked_mul(WASM_PAGE_SIZE_BYTES)
156 .ok_or(MemoryManagerLayoutError::ExtentOverflow)?;
157 if physical_pages == 0 {
159 return Err(MemoryManagerLayoutError::TruncatedBacking {
160 physical_pages,
161 required_pages: 1,
162 }
163 .into());
164 }
165 #[expect(
166 clippy::large_stack_arrays,
167 reason = "bounded manager image is also the diagnostic authority observation"
168 )]
169 let mut metadata = [0; METADATA_BYTES];
170 let (header, table) = metadata.split_at_mut(HEADER_BYTES);
171 memory.read(0, header);
172 let observed_magic = [header[0], header[1], header[2]];
173 if observed_magic != *b"MGR" {
174 return Err(RuntimeConstructionError::ForeignMemory { observed_magic });
175 }
176 if header[3] != 1 {
177 return Err(RuntimeConstructionError::UnsupportedMemoryManagerVersion {
178 observed: header[3],
179 supported: 1,
180 });
181 }
182 if header[8..40].iter().any(|byte| *byte != 0) {
183 return Err(MemoryManagerLayoutError::ReservedHeader.into());
184 }
185 let allocated_buckets = u16::from_le_bytes([header[4], header[5]]);
186 let bucket_pages = u16::from_le_bytes([header[6], header[7]]);
187 if bucket_pages == 0 {
188 return Err(MemoryManagerLayoutError::ZeroBucketSize.into());
189 }
190 if usize::from(allocated_buckets) > BUCKETS {
191 return Err(MemoryManagerLayoutError::BucketCount {
192 count: allocated_buckets,
193 }
194 .into());
195 }
196 let required_pages = 1 + u64::from(allocated_buckets) * u64::from(bucket_pages);
197 if physical_pages < required_pages {
198 return Err(MemoryManagerLayoutError::TruncatedBacking {
199 physical_pages,
200 required_pages,
201 }
202 .into());
203 }
204 memory.read(HEADER_BYTES as u64, table);
206 let mut buckets = [0_u16; IDS];
207 for (index, id) in (0_u16..32_768).zip(table.iter().copied()) {
208 if (index < allocated_buckets) != (id != 255) {
209 return Err(MemoryManagerLayoutError::BucketTable { index }.into());
210 }
211 if id != 255 {
212 buckets[usize::from(id)] += 1;
213 }
214 }
215 let mut pages = [0; IDS];
216 for (id, value) in (0_u8..255).zip(&mut pages) {
217 let offset = 40 + usize::from(id) * 8;
218 *value = u64::from_le_bytes(header[offset..offset + 8].try_into().expect("eight bytes"));
219 if value.div_ceil(u64::from(bucket_pages)) != u64::from(buckets[usize::from(id)]) {
220 return Err(MemoryManagerLayoutError::VirtualExtent { id }.into());
221 }
222 }
223 Ok(Layout {
224 metadata,
225 physical_pages,
226 bucket_pages,
227 allocated_buckets,
228 pages,
229 buckets,
230 })
231}