ic-memory 0.15.3

Durable stable-memory allocation governance for Internet Computer canisters
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use crate::{LedgerCommitStore, constants::WASM_PAGE_SIZE_BYTES};
use ic_stable_structures::{Memory, Storable, storable::Bound};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use thiserror::Error;

/// Stable-cell magic prefix written by `ic-stable-structures::Cell`.
pub const STABLE_CELL_MAGIC: &[u8; 3] = b"SCL";
/// Stable-cell layout version supported by this adapter.
pub const STABLE_CELL_LAYOUT_VERSION: u8 = 1;
/// Stable-cell header byte length.
pub const STABLE_CELL_HEADER_SIZE: usize = 8;
/// Byte offset where the stable-cell value payload starts.
pub const STABLE_CELL_VALUE_OFFSET: u64 = 8;

///
/// StableCellLedgerRecord
///
/// `ic-stable-structures::Cell` record containing an `ic-memory` allocation
/// ledger commit store.
///
/// This is a substrate adapter DTO. It owns no framework policy and does not
/// open application allocations.
///

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct StableCellLedgerRecord {
    store: LedgerCommitStore,
}

impl StableCellLedgerRecord {
    /// Construct a record from a commit store.
    #[must_use]
    pub const fn new(store: LedgerCommitStore) -> Self {
        Self { store }
    }

    /// Borrow the embedded commit store.
    #[must_use]
    pub const fn store(&self) -> &LedgerCommitStore {
        &self.store
    }

    /// Mutably borrow the embedded commit store.
    pub const fn store_mut(&mut self) -> &mut LedgerCommitStore {
        &mut self.store
    }

    /// Consume this record and return the embedded commit store.
    #[must_use]
    pub fn into_store(self) -> LedgerCommitStore {
        self.store
    }
}

impl Storable for StableCellLedgerRecord {
    const BOUND: Bound = Bound::Unbounded;

    fn to_bytes(&self) -> Cow<'_, [u8]> {
        Cow::Owned(serialize_record(self))
    }

    fn into_bytes(self) -> Vec<u8> {
        serialize_record(&self)
    }

    fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
        decode_stable_cell_ledger_record(&bytes).unwrap_or_else(|err| {
            panic!("StableCellLedgerRecord deserialize failed: {err}");
        })
    }
}

///
/// StableCellPayloadError
///
/// Stable-cell payload decode failure.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum StableCellPayloadError {
    /// Declared bytes exceed the current recovery ceiling before allocation.
    #[error("stable-cell ledger payload length {value_len} exceeds recovery limit")]
    TooLarge { value_len: u64 },
    /// Memory contents do not start with the stable-cell marker.
    #[error("memory is not an ic-stable-structures Cell")]
    NotStableCell,
    /// Stable-cell layout version does not match the adapter's current shape.
    #[error("unexpected stable-cell layout version {version}")]
    UnexpectedLayoutVersion {
        /// Observed stable-cell version.
        version: u8,
    },
    /// Stable-cell header length does not fit inside the memory.
    #[error("stable-cell payload length {value_len} exceeds available bytes {available_bytes}")]
    InvalidLength {
        /// Encoded value length.
        value_len: u64,
        /// Available payload bytes in memory.
        available_bytes: u64,
    },
    /// Stable-cell length cannot be represented on the current host.
    #[error("stable-cell payload length {value_len} cannot fit in usize")]
    LengthOverflow {
        /// Encoded value length.
        value_len: u64,
    },
}

///
/// StableCellLedgerError
///
/// Stable-cell ledger record validation failure.
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum StableCellLedgerError {
    /// Stable-cell envelope is corrupt or unexpected.
    #[error(transparent)]
    Payload(#[from] StableCellPayloadError),
    /// Stable-cell value bytes are not a valid ledger record.
    #[error("stable-cell ledger record decode failed")]
    Record(#[source] ciborium::de::Error<std::io::Error>),
}

/// Decode the raw value payload from an `ic-stable-structures::Cell` memory.
///
/// This helper is intentionally narrow: it recognizes the physical stable-cell
/// envelope and returns the value bytes. It does not deserialize those bytes or
/// decide whether they represent a valid allocation ledger.
pub fn decode_stable_cell_payload<M: Memory>(
    memory: &M,
) -> Result<Vec<u8>, StableCellPayloadError> {
    if memory.size() == 0 {
        return Err(StableCellPayloadError::NotStableCell);
    }

    let mut header = [0; STABLE_CELL_HEADER_SIZE];
    memory.read(0, &mut header);
    if &header[0..3] != STABLE_CELL_MAGIC {
        return Err(StableCellPayloadError::NotStableCell);
    }
    if header[3] != STABLE_CELL_LAYOUT_VERSION {
        return Err(StableCellPayloadError::UnexpectedLayoutVersion { version: header[3] });
    }

    let value_len = u64::from(u32::from_le_bytes([
        header[4], header[5], header[6], header[7],
    ]));
    let available_bytes = memory.size().saturating_mul(WASM_PAGE_SIZE_BYTES);
    let payload_capacity = available_bytes.saturating_sub(STABLE_CELL_VALUE_OFFSET);
    if value_len > payload_capacity {
        return Err(StableCellPayloadError::InvalidLength {
            value_len,
            available_bytes: payload_capacity,
        });
    }
    if value_len > crate::constants::MAX_LEDGER_RECORD_BYTES as u64 {
        return Err(StableCellPayloadError::TooLarge { value_len });
    }
    let value_len = usize::try_from(value_len)
        .map_err(|_| StableCellPayloadError::LengthOverflow { value_len })?;

    let mut bytes = vec![0; value_len];
    memory.read(STABLE_CELL_VALUE_OFFSET, &mut bytes);
    Ok(bytes)
}

/// Decode a `StableCellLedgerRecord` from stable-cell value bytes.
///
/// This decodes only the cell value payload, not the enclosing stable-cell
/// header. Use [`decode_stable_cell_payload`] first when inspecting raw stable
/// memory.
///
/// The returned record is decoded DTO state, not authority. Recover through the
/// embedded [`LedgerCommitStore`] before trusting any ledger payload.
pub fn decode_stable_cell_ledger_record(
    bytes: &[u8],
) -> Result<StableCellLedgerRecord, ciborium::de::Error<std::io::Error>> {
    crate::cbor::from_slice_exact(bytes)
}

pub fn decode_stable_cell_ledger_record_from_memory<M: Memory>(
    memory: &M,
) -> Result<StableCellLedgerRecord, StableCellLedgerError> {
    if memory.size() == 0 {
        return Ok(StableCellLedgerRecord::default());
    }

    let payload = decode_stable_cell_payload(memory)?;
    decode_stable_cell_ledger_record(&payload).map_err(StableCellLedgerError::Record)
}

/// Validate an existing stable-cell ledger record before opening it with
/// `ic-stable-structures::Cell`.
///
/// `Cell::init` decodes the existing value through [`Storable::from_bytes`].
/// That trait is panic-based, so the runtime preflights the raw memory with
/// this fallible helper first. Empty memory is treated as uninitialized and is
/// safe for `Cell::init` to create.
pub fn validate_stable_cell_ledger_memory<M: Memory>(
    memory: &M,
) -> Result<(), StableCellLedgerError> {
    decode_stable_cell_ledger_record_from_memory(memory)?;
    Ok(())
}

fn serialize_record(record: &StableCellLedgerRecord) -> Vec<u8> {
    let mut bytes = Vec::new();
    ciborium::into_writer(record, &mut bytes).unwrap_or_else(|err| {
        panic!("StableCellLedgerRecord serialize failed: {err}");
    });
    bytes
}

#[cfg(test)]
mod tests {
    use super::*;
    use ic_stable_structures::{Cell, VectorMemory};

    fn hex_fixture(contents: &str) -> Vec<u8> {
        let hex = contents
            .chars()
            .filter(|char| !char.is_whitespace())
            .collect::<String>();
        assert_eq!(hex.len() % 2, 0, "fixture hex must have byte pairs");
        hex.as_bytes()
            .as_chunks::<2>()
            .0
            .iter()
            .map(|pair| {
                let pair = std::str::from_utf8(pair).expect("fixture hex is utf8");
                u8::from_str_radix(pair, 16).expect("fixture hex byte")
            })
            .collect()
    }

    #[test]
    fn stable_cell_ledger_record_round_trips_through_cell() {
        let memory = VectorMemory::default();
        let record = StableCellLedgerRecord::default();
        let cell = Cell::init(memory.clone(), record.clone());

        assert_eq!(cell.get(), &record);
        let payload = decode_stable_cell_payload(&memory).expect("decode stable cell payload");
        let decoded = StableCellLedgerRecord::from_bytes(Cow::Owned(payload));
        assert_eq!(decoded, record);
    }

    #[test]
    fn current_stable_cell_record_fixture_recovers() {
        let bytes = hex_fixture(include_str!(
            "../fixtures/current/stable_cell_record.cbor.hex"
        ));
        let record = decode_stable_cell_ledger_record(&bytes).expect("stable-cell fixture");

        assert_eq!(
            bytes,
            crate::test_cbor::to_vec(&record).expect("re-encoded stable-cell fixture")
        );
        assert_eq!(
            record
                .store()
                .recover()
                .expect("fixture store recovers")
                .current_generation(),
            1
        );
    }

    #[test]
    fn malformed_payload_bytes_fail_closed_without_mutating_memory() {
        let original = hex_fixture(include_str!(
            "../fixtures/current/stable_cell_record.cbor.hex"
        ));
        let start = original
            .windows(8)
            .position(|bytes| bytes == b"\x67payload")
            .unwrap()
            + 8;
        let oversized = u32::try_from(crate::constants::MAX_COMMITTED_PAYLOAD_BYTES + 1).unwrap();
        let mut cases = Vec::new();
        for replacement in [
            vec![0x5a, 0xff, 0xff, 0xff, 0xff],
            vec![0x5f, 0xff],
            vec![0x41],
            vec![0xff],
        ] {
            let mut bytes = original[..start].to_vec();
            bytes.extend(replacement);
            cases.push(bytes);
        }
        let mut bytes = original[..start].to_vec();
        bytes.push(0x5a);
        bytes.extend_from_slice(&oversized.to_be_bytes());
        bytes.resize(bytes.len() + oversized as usize, 0);
        cases.push(bytes);
        for bytes in cases {
            let memory = VectorMemory::default();
            let pages = (bytes.len() + STABLE_CELL_HEADER_SIZE).div_ceil(65_536);
            memory.grow(pages as u64);
            memory.write(0, STABLE_CELL_MAGIC);
            memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
            memory.write(4, &u32::try_from(bytes.len()).unwrap().to_le_bytes());
            memory.write(STABLE_CELL_VALUE_OFFSET, &bytes);
            let before = memory.borrow().clone();
            for _ in 0..2 {
                assert!(matches!(
                    decode_stable_cell_ledger_record_from_memory(&memory),
                    Err(StableCellLedgerError::Record(_))
                ));
                assert!(matches!(
                    validate_stable_cell_ledger_memory(&memory),
                    Err(StableCellLedgerError::Record(_))
                ));
            }
            assert_eq!(*memory.borrow(), before);
        }
    }

    #[test]
    fn stable_cell_ledger_record_rejects_trailing_bytes() {
        let mut bytes = serialize_record(&StableCellLedgerRecord::default());
        bytes.push(0);

        let err =
            decode_stable_cell_ledger_record(&bytes).expect_err("trailing bytes must fail closed");

        assert!(err.to_string().contains("trailing bytes"));
    }

    #[test]
    fn stable_cell_ledger_record_rejects_unknown_top_level_fields() {
        use crate::test_cbor::Value;

        let mut map = Vec::new();
        crate::test_cbor::map_insert(
            &mut map,
            Value::Text("store".to_string()),
            crate::test_cbor::to_value(LedgerCommitStore::default()).expect("store value"),
        );
        crate::test_cbor::map_insert(
            &mut map,
            Value::Text("future_field".to_string()),
            Value::Bool(true),
        );
        let bytes = crate::test_cbor::to_vec(&Value::Map(map)).expect("unknown-field stable cell");

        let err = decode_stable_cell_ledger_record(&bytes)
            .expect_err("unknown stable-cell record field must fail closed");

        assert!(err.to_string().contains("future_field"));
    }

    #[test]
    fn stable_cell_ledger_record_requires_both_commit_slot_fields() {
        use crate::test_cbor::Value;

        for (missing, present) in [("slot0", "slot1"), ("slot1", "slot0")] {
            let mut physical = Vec::new();
            crate::test_cbor::map_insert(
                &mut physical,
                Value::Text(present.to_string()),
                Value::Null,
            );
            let mut store = Vec::new();
            crate::test_cbor::map_insert(
                &mut store,
                Value::Text("physical".to_string()),
                Value::Map(physical),
            );
            let mut record = Vec::new();
            crate::test_cbor::map_insert(
                &mut record,
                Value::Text("store".to_string()),
                Value::Map(store),
            );
            let bytes = crate::test_cbor::to_vec(&Value::Map(record)).expect("record bytes");

            let err = decode_stable_cell_ledger_record(&bytes)
                .expect_err("missing commit slot must fail closed");

            assert!(err.to_string().contains(missing));
        }
    }

    #[test]
    fn stable_cell_payload_rejects_non_cell_memory() {
        let memory = VectorMemory::default();
        memory.grow(1);
        memory.write(0, b"BAD");

        assert_eq!(
            decode_stable_cell_payload(&memory),
            Err(StableCellPayloadError::NotStableCell)
        );
    }

    #[test]
    fn stable_cell_payload_rejects_empty_memory_without_panic() {
        let memory = VectorMemory::default();

        assert_eq!(
            decode_stable_cell_payload(&memory),
            Err(StableCellPayloadError::NotStableCell)
        );
    }

    #[test]
    fn stable_cell_ledger_preflight_classifies_bad_record_without_panic() {
        let memory = VectorMemory::default();
        memory.grow(1);
        memory.write(0, STABLE_CELL_MAGIC);
        memory.write(3, &[STABLE_CELL_LAYOUT_VERSION]);
        memory.write(4, &1_u32.to_le_bytes());
        memory.write(STABLE_CELL_VALUE_OFFSET, &[0xff]);

        let err =
            validate_stable_cell_ledger_memory(&memory).expect_err("bad record must be classified");

        assert!(matches!(err, StableCellLedgerError::Record(_)));
    }
    #[test]
    fn oversized_cell_length_is_rejected_before_payload_read() {
        struct HeaderOnly;
        impl Memory for HeaderOnly {
            fn size(&self) -> u64 {
                2048
            }
            fn grow(&self, _: u64) -> i64 {
                panic!("must not grow")
            }
            fn write(&self, _: u64, _: &[u8]) {
                panic!("must not write")
            }
            fn read(&self, offset: u64, bytes: &mut [u8]) {
                assert_eq!(offset, 0);
                assert_eq!(bytes.len(), STABLE_CELL_HEADER_SIZE);
                bytes[..4].copy_from_slice(b"SCL\x01");
                bytes[4..8].copy_from_slice(
                    &u32::try_from(crate::constants::MAX_LEDGER_RECORD_BYTES + 1)
                        .unwrap()
                        .to_le_bytes(),
                );
            }
        }
        assert!(matches!(
            decode_stable_cell_ledger_record_from_memory(&HeaderOnly),
            Err(StableCellLedgerError::Payload(
                StableCellPayloadError::TooLarge { .. }
            ))
        ));
    }

    #[test]
    fn hostile_cbor_is_rejected_on_production_record_decode() {
        let huge_array = [0x9b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
        let huge_string = [0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
        let mut nested = vec![0x81; crate::constants::MAX_LEDGER_NESTING + 1];
        nested.push(0);
        for bytes in [&huge_array[..], &huge_string[..], &nested, &[0xa1], &[0xff]] {
            assert!(decode_stable_cell_ledger_record(bytes).is_err());
        }
    }
}