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::new();
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!(
265            bytes,
266            crate::test_cbor::to_vec(&record).expect("re-encoded stable-cell fixture")
267        );
268        assert_eq!(
269            record
270                .store()
271                .recover()
272                .expect("fixture store recovers")
273                .current_generation(),
274            1
275        );
276    }
277
278    #[test]
279    fn malformed_payload_bytes_fail_closed_without_mutating_memory() {
280        let original = hex_fixture(include_str!(
281            "../fixtures/current/stable_cell_record.cbor.hex"
282        ));
283        let start = original
284            .windows(8)
285            .position(|bytes| bytes == b"\x67payload")
286            .unwrap()
287            + 8;
288        let oversized = u32::try_from(crate::constants::MAX_COMMITTED_PAYLOAD_BYTES + 1).unwrap();
289        let mut cases = Vec::new();
290        for replacement in [
291            vec![0x5a, 0xff, 0xff, 0xff, 0xff],
292            vec![0x5f, 0xff],
293            vec![0x41],
294            vec![0xff],
295        ] {
296            let mut bytes = original[..start].to_vec();
297            bytes.extend(replacement);
298            cases.push(bytes);
299        }
300        let mut bytes = original[..start].to_vec();
301        bytes.push(0x5a);
302        bytes.extend_from_slice(&oversized.to_be_bytes());
303        bytes.resize(bytes.len() + oversized as usize, 0);
304        cases.push(bytes);
305        for bytes in cases {
306            let memory = VectorMemory::default();
307            let pages = (bytes.len() + STABLE_CELL_HEADER_SIZE).div_ceil(65_536);
308            memory.grow(pages as u64);
309            memory.write(0, STABLE_CELL_MAGIC);
310            memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
311            memory.write(4, &u32::try_from(bytes.len()).unwrap().to_le_bytes());
312            memory.write(STABLE_CELL_VALUE_OFFSET, &bytes);
313            let before = memory.borrow().clone();
314            for _ in 0..2 {
315                assert!(matches!(
316                    decode_stable_cell_ledger_record_from_memory(&memory),
317                    Err(StableCellLedgerError::Record(_))
318                ));
319            }
320            assert_eq!(*memory.borrow(), before);
321        }
322    }
323
324    #[test]
325    fn stable_cell_ledger_record_rejects_trailing_bytes() {
326        let mut bytes = serialize_record(&StableCellLedgerRecord::default());
327        bytes.push(0);
328
329        let err =
330            decode_stable_cell_ledger_record(&bytes).expect_err("trailing bytes must fail closed");
331
332        assert!(err.to_string().contains("trailing bytes"));
333    }
334
335    #[test]
336    fn stable_cell_ledger_record_rejects_unknown_top_level_fields() {
337        use crate::test_cbor::Value;
338
339        let map = vec![
340            (
341                Value::Text("store".to_string()),
342                crate::test_cbor::to_value(LedgerCommitStore::default()).expect("store value"),
343            ),
344            (Value::Text("future_field".to_string()), Value::Bool(true)),
345        ];
346        let bytes = crate::test_cbor::to_vec(&Value::Map(map)).expect("unknown-field stable cell");
347
348        let err = decode_stable_cell_ledger_record(&bytes)
349            .expect_err("unknown stable-cell record field must fail closed");
350
351        assert!(err.to_string().contains("future_field"));
352    }
353
354    #[test]
355    fn stable_cell_ledger_record_requires_both_commit_slot_fields() {
356        use crate::test_cbor::Value;
357
358        for (missing, present) in [("slot0", "slot1"), ("slot1", "slot0")] {
359            let physical = vec![(Value::Text(present.to_string()), Value::Null)];
360            let store = vec![(Value::Text("physical".to_string()), Value::Map(physical))];
361            let record = vec![(Value::Text("store".to_string()), Value::Map(store))];
362            let bytes = crate::test_cbor::to_vec(&Value::Map(record)).expect("record bytes");
363
364            let err = decode_stable_cell_ledger_record(&bytes)
365                .expect_err("missing commit slot must fail closed");
366
367            assert!(err.to_string().contains(missing));
368        }
369    }
370
371    #[test]
372    fn stable_cell_payload_rejects_non_cell_memory() {
373        let memory = VectorMemory::default();
374        memory.grow(1);
375        memory.write(0, b"BAD");
376
377        assert_eq!(
378            decode_stable_cell_payload(&memory),
379            Err(StableCellPayloadError::NotStableCell)
380        );
381    }
382
383    #[test]
384    fn stable_cell_payload_rejects_empty_memory_without_panic() {
385        let memory = VectorMemory::default();
386
387        assert_eq!(
388            decode_stable_cell_payload(&memory),
389            Err(StableCellPayloadError::NotStableCell)
390        );
391        assert_eq!(
392            crate::decode_stable_cell_ledger_record_from_memory(&memory).unwrap(),
393            StableCellLedgerRecord::default()
394        );
395        assert_eq!(
396            memory.size(),
397            0,
398            "reading empty memory must not initialize it"
399        );
400    }
401
402    #[test]
403    fn stable_cell_ledger_memory_reader_classifies_bad_record_without_panic() {
404        let memory = VectorMemory::default();
405        memory.grow(1);
406        memory.write(0, STABLE_CELL_MAGIC);
407        memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
408        memory.write(4, &1_u32.to_le_bytes());
409        memory.write(STABLE_CELL_VALUE_OFFSET, &[0xff]);
410
411        let err = decode_stable_cell_ledger_record_from_memory(&memory)
412            .expect_err("bad record must be classified");
413
414        assert!(matches!(err, StableCellLedgerError::Record(_)));
415    }
416    #[test]
417    fn oversized_cell_length_is_rejected_before_payload_read() {
418        struct HeaderOnly {
419            value_len: u32,
420            pages: u64,
421        }
422        impl Memory for HeaderOnly {
423            fn size(&self) -> u64 {
424                self.pages
425            }
426            fn grow(&self, _: u64) -> i64 {
427                panic!("must not grow")
428            }
429            fn write(&self, _: u64, _: &[u8]) {
430                panic!("must not write")
431            }
432            fn read(&self, offset: u64, bytes: &mut [u8]) {
433                assert_eq!(offset, 0);
434                assert_eq!(bytes.len(), STABLE_CELL_HEADER_SIZE);
435                bytes[..4].copy_from_slice(b"SCL\x01");
436                bytes[4..8].copy_from_slice(&self.value_len.to_le_bytes());
437            }
438        }
439        for value_len in [
440            u32::try_from(crate::constants::MAX_LEDGER_RECORD_BYTES + 1).unwrap(),
441            u32::MAX,
442        ] {
443            // The advertised value fits the backing, but exceeds the recovery ceiling.
444            let memory = HeaderOnly {
445                value_len,
446                pages: 65_537,
447            };
448            assert!(matches!(
449                decode_stable_cell_ledger_record_from_memory(&memory),
450                Err(StableCellLedgerError::Payload(StableCellPayloadError::TooLarge {
451                    value_len: rejected,
452                })) if rejected == u64::from(value_len)
453            ));
454            // Physical-capacity rejection keeps precedence when both checks fail.
455            let memory = HeaderOnly {
456                value_len,
457                pages: 1,
458            };
459            assert_eq!(
460                decode_stable_cell_payload(&memory),
461                Err(StableCellPayloadError::InvalidLength {
462                    value_len: u64::from(value_len),
463                    available_bytes: WASM_PAGE_SIZE_BYTES - STABLE_CELL_VALUE_OFFSET,
464                })
465            );
466        }
467    }
468
469    #[test]
470    fn hostile_cbor_is_rejected_on_production_record_decode() {
471        let huge_array = [0x9b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
472        let huge_string = [0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
473        let mut nested = vec![0x81; crate::constants::MAX_LEDGER_NESTING + 1];
474        nested.push(0);
475        for bytes in [&huge_array[..], &huge_string[..], &nested, &[0xa1], &[0xff]] {
476            assert!(decode_stable_cell_ledger_record(bytes).is_err());
477        }
478    }
479}