Skip to main content

ic_memory/
stable_cell.rs

1use crate::{LedgerCommitStore, constants::WASM_PAGE_SIZE_BYTES};
2use ic_stable_structures::{Memory, Storable, storable::Bound};
3use serde::{Deserialize, Serialize};
4use std::borrow::Cow;
5use thiserror::Error;
6
7/// Stable-cell magic prefix written by `ic-stable-structures::Cell`.
8pub const STABLE_CELL_MAGIC: &[u8; 3] = b"SCL";
9/// Stable-cell layout version supported by this adapter.
10pub const STABLE_CELL_LAYOUT_VERSION: u8 = 1;
11/// Stable-cell header byte length.
12pub const STABLE_CELL_HEADER_SIZE: usize = 8;
13/// Byte offset where the stable-cell value payload starts.
14pub const STABLE_CELL_VALUE_OFFSET: u64 = 8;
15
16///
17/// StableCellLedgerRecord
18///
19/// `ic-stable-structures::Cell` record containing an `ic-memory` allocation
20/// ledger commit store.
21///
22/// This is a substrate adapter DTO. It owns no framework policy and does not
23/// open application allocations.
24///
25
26#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
27#[serde(deny_unknown_fields)]
28pub struct StableCellLedgerRecord {
29    store: LedgerCommitStore,
30}
31
32impl StableCellLedgerRecord {
33    /// Construct a record from a commit store.
34    #[must_use]
35    pub const fn new(store: LedgerCommitStore) -> Self {
36        Self { store }
37    }
38
39    /// Borrow the embedded commit store.
40    #[must_use]
41    pub const fn store(&self) -> &LedgerCommitStore {
42        &self.store
43    }
44
45    /// Mutably borrow the embedded commit store.
46    pub const fn store_mut(&mut self) -> &mut LedgerCommitStore {
47        &mut self.store
48    }
49
50    /// Consume this record and return the embedded commit store.
51    #[must_use]
52    pub fn into_store(self) -> LedgerCommitStore {
53        self.store
54    }
55
56    /// Measure the current encoded record without allocating a payload buffer.
57    pub(crate) fn encoded_size(&self) -> usize {
58        let mut writer = CountingWriter(0);
59        encode_record(self, &mut writer);
60        writer.0
61    }
62}
63
64impl Storable for StableCellLedgerRecord {
65    const BOUND: Bound = Bound::Unbounded;
66
67    fn to_bytes(&self) -> Cow<'_, [u8]> {
68        Cow::Owned(serialize_record(self))
69    }
70
71    fn into_bytes(self) -> Vec<u8> {
72        serialize_record(&self)
73    }
74
75    fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
76        decode_stable_cell_ledger_record(&bytes).unwrap_or_else(|err| {
77            panic!("StableCellLedgerRecord deserialize failed: {err}");
78        })
79    }
80}
81
82///
83/// StableCellPayloadError
84///
85/// Stable-cell payload decode failure.
86///
87
88#[non_exhaustive]
89#[derive(Clone, Debug, Eq, Error, PartialEq)]
90pub enum StableCellPayloadError {
91    /// Declared bytes exceed the current recovery ceiling before allocation.
92    #[error("stable-cell ledger payload length {value_len} exceeds recovery limit")]
93    TooLarge { value_len: u64 },
94    /// Memory contents do not start with the stable-cell marker.
95    #[error("memory is not an ic-stable-structures Cell")]
96    NotStableCell,
97    /// Stable-cell layout version does not match the adapter's current shape.
98    #[error("unexpected stable-cell layout version {version}")]
99    UnexpectedLayoutVersion {
100        /// Observed stable-cell version.
101        version: u8,
102    },
103    /// Stable-cell header length does not fit inside the memory.
104    #[error("stable-cell payload length {value_len} exceeds available bytes {available_bytes}")]
105    InvalidLength {
106        /// Encoded value length.
107        value_len: u64,
108        /// Available payload bytes in memory.
109        available_bytes: u64,
110    },
111}
112
113///
114/// StableCellLedgerError
115///
116/// Stable-cell ledger record validation failure.
117#[non_exhaustive]
118#[derive(Debug, Error)]
119pub enum StableCellLedgerError {
120    /// Stable-cell envelope is corrupt or unexpected.
121    #[error(transparent)]
122    Payload(#[from] StableCellPayloadError),
123    /// Stable-cell value bytes are not a valid ledger record.
124    #[error("stable-cell ledger record decode failed: {0}")]
125    Record(#[source] ciborium::de::Error<std::io::Error>),
126}
127
128/// Decode the raw value payload from an `ic-stable-structures::Cell` memory.
129///
130/// This helper is intentionally narrow: it recognizes the physical stable-cell
131/// envelope and returns the value bytes. It does not deserialize those bytes or
132/// decide whether they represent a valid allocation ledger.
133pub fn decode_stable_cell_payload<M: Memory>(
134    memory: &M,
135) -> Result<Vec<u8>, StableCellPayloadError> {
136    if memory.size() == 0 {
137        return Err(StableCellPayloadError::NotStableCell);
138    }
139
140    let mut header = [0; STABLE_CELL_HEADER_SIZE];
141    memory.read(0, &mut header);
142    if &header[0..3] != STABLE_CELL_MAGIC {
143        return Err(StableCellPayloadError::NotStableCell);
144    }
145    if header[3] != STABLE_CELL_LAYOUT_VERSION {
146        return Err(StableCellPayloadError::UnexpectedLayoutVersion { version: header[3] });
147    }
148
149    let value_len = u64::from(u32::from_le_bytes([
150        header[4], header[5], header[6], header[7],
151    ]));
152    let available_bytes = memory.size().saturating_mul(WASM_PAGE_SIZE_BYTES);
153    let payload_capacity = available_bytes.saturating_sub(STABLE_CELL_VALUE_OFFSET);
154    if value_len > payload_capacity {
155        return Err(StableCellPayloadError::InvalidLength {
156            value_len,
157            available_bytes: payload_capacity,
158        });
159    }
160    if value_len > crate::constants::MAX_LEDGER_RECORD_BYTES as u64 {
161        return Err(StableCellPayloadError::TooLarge { value_len });
162    }
163    #[expect(
164        clippy::cast_possible_truncation,
165        reason = "the admitted recovery ceiling is representable as usize"
166    )]
167    let value_len = value_len as usize;
168
169    let mut bytes = vec![0; value_len];
170    memory.read(STABLE_CELL_VALUE_OFFSET, &mut bytes);
171    Ok(bytes)
172}
173
174/// Decode a `StableCellLedgerRecord` from stable-cell value bytes.
175///
176/// This decodes only the cell value payload, not the enclosing stable-cell
177/// header. Use [`decode_stable_cell_payload`] first when inspecting raw stable
178/// memory.
179///
180/// The returned record is decoded DTO state, not authority. Recover through the
181/// embedded [`LedgerCommitStore`] before trusting any ledger payload.
182pub fn decode_stable_cell_ledger_record(
183    bytes: &[u8],
184) -> Result<StableCellLedgerRecord, ciborium::de::Error<std::io::Error>> {
185    crate::cbor::from_slice_exact(bytes)
186}
187
188/// Fallibly decode a ledger record from its stable-cell envelope and value.
189///
190/// Empty memory returns an uninitialized record without writing a cell. Nonempty
191/// memory must pass the current envelope, byte bounds and record decoding. The
192/// returned DTO still requires protected ledger recovery before it is authority.
193/// Keep this record for recovery rather than decoding again through the
194/// panic-based [`Storable::from_bytes`] used by `Cell::init`.
195pub fn decode_stable_cell_ledger_record_from_memory<M: Memory>(
196    memory: &M,
197) -> Result<StableCellLedgerRecord, StableCellLedgerError> {
198    if memory.size() == 0 {
199        return Ok(StableCellLedgerRecord::default());
200    }
201
202    let payload = decode_stable_cell_payload(memory)?;
203    decode_stable_cell_ledger_record(&payload).map_err(StableCellLedgerError::Record)
204}
205
206fn serialize_record(record: &StableCellLedgerRecord) -> Vec<u8> {
207    let mut bytes = Vec::with_capacity(record.encoded_size());
208    encode_record(record, &mut bytes);
209    bytes
210}
211
212fn encode_record(record: &StableCellLedgerRecord, writer: impl std::io::Write) {
213    ciborium::into_writer(record, writer).unwrap_or_else(|err| {
214        panic!("StableCellLedgerRecord serialize failed: {err}");
215    });
216}
217
218struct CountingWriter(usize);
219
220impl std::io::Write for CountingWriter {
221    fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
222        self.0 = self
223            .0
224            .checked_add(bytes.len())
225            .ok_or_else(|| std::io::Error::other("encoded record length overflow"))?;
226        Ok(bytes.len())
227    }
228
229    fn flush(&mut self) -> std::io::Result<()> {
230        Ok(())
231    }
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237    use crate::test_cbor::hex_fixture;
238    use ic_stable_structures::{Cell, VectorMemory};
239
240    #[test]
241    fn stable_cell_ledger_record_round_trips_through_cell() {
242        let memory = VectorMemory::default();
243        let record = StableCellLedgerRecord::default();
244        let cell = Cell::init(memory.clone(), record.clone());
245
246        assert_eq!(cell.get(), &record);
247        let payload = decode_stable_cell_payload(&memory).expect("decode stable cell payload");
248        let decoded = StableCellLedgerRecord::from_bytes(Cow::Owned(payload));
249        assert_eq!(decoded, record);
250        assert_eq!(
251            crate::decode_stable_cell_ledger_record_from_memory(&memory).unwrap(),
252            record
253        );
254    }
255
256    #[test]
257    fn current_stable_cell_record_fixture_recovers() {
258        let bytes = hex_fixture(include_str!(
259            "../fixtures/current/stable_cell_record.cbor.hex"
260        ));
261        let record = decode_stable_cell_ledger_record(&bytes).expect("stable-cell fixture");
262
263        assert_eq!(record.encoded_size(), bytes.len());
264        assert_eq!(record.to_bytes().as_ref(), bytes.as_slice());
265        assert_eq!(record.clone().into_bytes(), bytes);
266        assert_eq!(
267            bytes,
268            crate::test_cbor::to_vec(&record).expect("re-encoded stable-cell fixture")
269        );
270        assert_eq!(
271            record
272                .store()
273                .recover()
274                .expect("fixture store recovers")
275                .current_generation(),
276            1
277        );
278    }
279
280    #[test]
281    fn malformed_payload_bytes_fail_closed_without_mutating_memory() {
282        let original = hex_fixture(include_str!(
283            "../fixtures/current/stable_cell_record.cbor.hex"
284        ));
285        let start = original
286            .windows(8)
287            .position(|bytes| bytes == b"\x67payload")
288            .unwrap()
289            + 8;
290        let oversized = u32::try_from(crate::constants::MAX_COMMITTED_PAYLOAD_BYTES + 1).unwrap();
291        let mut cases = Vec::new();
292        for replacement in [
293            vec![0x5a, 0xff, 0xff, 0xff, 0xff],
294            vec![0x5f, 0xff],
295            vec![0x41],
296            vec![0xff],
297        ] {
298            let mut bytes = original[..start].to_vec();
299            bytes.extend(replacement);
300            cases.push(bytes);
301        }
302        let mut bytes = original[..start].to_vec();
303        bytes.push(0x5a);
304        bytes.extend_from_slice(&oversized.to_be_bytes());
305        bytes.resize(bytes.len() + oversized as usize, 0);
306        cases.push(bytes);
307        for bytes in cases {
308            let memory = VectorMemory::default();
309            let pages = (bytes.len() + STABLE_CELL_HEADER_SIZE).div_ceil(65_536);
310            memory.grow(pages as u64);
311            memory.write(0, STABLE_CELL_MAGIC);
312            memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
313            memory.write(4, &u32::try_from(bytes.len()).unwrap().to_le_bytes());
314            memory.write(STABLE_CELL_VALUE_OFFSET, &bytes);
315            let before = memory.borrow().clone();
316            for _ in 0..2 {
317                assert!(matches!(
318                    decode_stable_cell_ledger_record_from_memory(&memory),
319                    Err(StableCellLedgerError::Record(_))
320                ));
321            }
322            assert_eq!(*memory.borrow(), before);
323        }
324    }
325
326    #[test]
327    fn stable_cell_ledger_record_rejects_trailing_bytes() {
328        let mut bytes = serialize_record(&StableCellLedgerRecord::default());
329        bytes.push(0);
330
331        let err =
332            decode_stable_cell_ledger_record(&bytes).expect_err("trailing bytes must fail closed");
333
334        assert!(err.to_string().contains("trailing bytes"));
335    }
336
337    #[test]
338    fn stable_cell_ledger_record_rejects_unknown_top_level_fields() {
339        use crate::test_cbor::Value;
340
341        let map = vec![
342            (
343                Value::Text("store".to_string()),
344                crate::test_cbor::to_value(LedgerCommitStore::default()).expect("store value"),
345            ),
346            (Value::Text("future_field".to_string()), Value::Bool(true)),
347        ];
348        let bytes = crate::test_cbor::to_vec(&Value::Map(map)).expect("unknown-field stable cell");
349
350        let err = decode_stable_cell_ledger_record(&bytes)
351            .expect_err("unknown stable-cell record field must fail closed");
352
353        assert!(err.to_string().contains("future_field"));
354    }
355
356    #[test]
357    fn stable_cell_ledger_record_requires_both_commit_slot_fields() {
358        use crate::test_cbor::Value;
359
360        for (missing, present) in [("slot0", "slot1"), ("slot1", "slot0")] {
361            let physical = vec![(Value::Text(present.to_string()), Value::Null)];
362            let store = vec![(Value::Text("physical".to_string()), Value::Map(physical))];
363            let record = vec![(Value::Text("store".to_string()), Value::Map(store))];
364            let bytes = crate::test_cbor::to_vec(&Value::Map(record)).expect("record bytes");
365
366            let err = decode_stable_cell_ledger_record(&bytes)
367                .expect_err("missing commit slot must fail closed");
368
369            assert!(err.to_string().contains(missing));
370        }
371    }
372
373    #[test]
374    fn stable_cell_payload_rejects_non_cell_memory() {
375        let memory = VectorMemory::default();
376        memory.grow(1);
377        memory.write(0, b"BAD");
378
379        assert_eq!(
380            decode_stable_cell_payload(&memory),
381            Err(StableCellPayloadError::NotStableCell)
382        );
383    }
384
385    #[test]
386    fn stable_cell_payload_rejects_empty_memory_without_panic() {
387        let memory = VectorMemory::default();
388
389        assert_eq!(
390            decode_stable_cell_payload(&memory),
391            Err(StableCellPayloadError::NotStableCell)
392        );
393        assert_eq!(
394            crate::decode_stable_cell_ledger_record_from_memory(&memory).unwrap(),
395            StableCellLedgerRecord::default()
396        );
397        assert_eq!(
398            memory.size(),
399            0,
400            "reading empty memory must not initialize it"
401        );
402    }
403
404    #[test]
405    fn stable_cell_ledger_memory_reader_classifies_bad_record_without_panic() {
406        let memory = VectorMemory::default();
407        memory.grow(1);
408        memory.write(0, STABLE_CELL_MAGIC);
409        memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
410        memory.write(4, &1_u32.to_le_bytes());
411        memory.write(STABLE_CELL_VALUE_OFFSET, &[0xff]);
412
413        let err = decode_stable_cell_ledger_record_from_memory(&memory)
414            .expect_err("bad record must be classified");
415
416        assert!(matches!(err, StableCellLedgerError::Record(_)));
417    }
418    #[test]
419    fn oversized_cell_length_is_rejected_before_payload_read() {
420        struct HeaderOnly {
421            value_len: u32,
422            pages: u64,
423        }
424        impl Memory for HeaderOnly {
425            fn size(&self) -> u64 {
426                self.pages
427            }
428            fn grow(&self, _: u64) -> i64 {
429                panic!("must not grow")
430            }
431            fn write(&self, _: u64, _: &[u8]) {
432                panic!("must not write")
433            }
434            fn read(&self, offset: u64, bytes: &mut [u8]) {
435                assert_eq!(offset, 0);
436                assert_eq!(bytes.len(), STABLE_CELL_HEADER_SIZE);
437                bytes[..4].copy_from_slice(b"SCL\x01");
438                bytes[4..8].copy_from_slice(&self.value_len.to_le_bytes());
439            }
440        }
441        for value_len in [
442            u32::try_from(crate::constants::MAX_LEDGER_RECORD_BYTES + 1).unwrap(),
443            u32::MAX,
444        ] {
445            // The advertised value fits the backing, but exceeds the recovery ceiling.
446            let memory = HeaderOnly {
447                value_len,
448                pages: 65_537,
449            };
450            assert!(matches!(
451                decode_stable_cell_ledger_record_from_memory(&memory),
452                Err(StableCellLedgerError::Payload(StableCellPayloadError::TooLarge {
453                    value_len: rejected,
454                })) if rejected == u64::from(value_len)
455            ));
456            // Physical-capacity rejection keeps precedence when both checks fail.
457            let memory = HeaderOnly {
458                value_len,
459                pages: 1,
460            };
461            assert_eq!(
462                decode_stable_cell_payload(&memory),
463                Err(StableCellPayloadError::InvalidLength {
464                    value_len: u64::from(value_len),
465                    available_bytes: WASM_PAGE_SIZE_BYTES - STABLE_CELL_VALUE_OFFSET,
466                })
467            );
468        }
469    }
470
471    #[test]
472    fn hostile_cbor_is_rejected_on_production_record_decode() {
473        let huge_array = [0x9b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
474        let huge_string = [0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
475        let mut nested = vec![0x81; crate::constants::MAX_LEDGER_NESTING + 1];
476        nested.push(0);
477        for bytes in [&huge_array[..], &huge_string[..], &nested, &[0xa1], &[0xff]] {
478            assert!(decode_stable_cell_ledger_record(bytes).is_err());
479        }
480    }
481}