ic-memory 0.51.13

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
465
466
467
468
469
470
471
472
473
474
475
//! Validation and package receipts bound to selected source.
use super::*;

// Each edit keeps rollback bytes beside the exact source-derived replacement.
struct MetadataEdit<'a> {
    path: &'a str,
    expected: &'a str,
    before: Vec<u8>,
}

impl MetadataEdit<'_> {
    fn publish<E: Execute>(&self, repo: &Repository<E>) -> Result<()> {
        if self.before != self.expected.as_bytes() {
            repo.exec
                .write_bytes(&repo.root.join(self.path), self.expected.as_bytes())?;
        }
        Ok(())
    }

    fn restore<E: Execute>(
        &self,
        repo: &Repository<E>,
        selection: &ReleaseSelection,
    ) -> Result<()> {
        let current = fs::read(repo.root.join(self.path))?;
        if current == self.before {
            // Preserve file identity and permissions when no replacement needs undoing.
            return Ok(());
        }
        let owned = if self.path == "Cargo.lock" {
            std::str::from_utf8(&current).is_ok_and(|text| {
                check_lock_update(&self.before, text, &selection.previous, &selection.version)
                    .is_ok()
            })
        } else {
            current == self.expected.as_bytes()
        };
        if owned {
            repo.exec
                .write_bytes(&repo.root.join(self.path), &self.before)?;
        }
        Ok(())
    }
}

impl<E: Execute> Repository<E> {
    pub(super) fn validation_path(&self, version: &str) -> Result<PathBuf> {
        self.version_parts(version)?;
        Ok(self
            .target()?
            .join("release-validation")
            .join(format!("{version}-validated.json")))
    }

    pub(super) fn validation_command(&self) -> Result<Vec<String>> {
        Ok(vec![
            "make".to_owned(),
            "--no-print-directory".to_owned(),
            "validate".to_owned(),
            format!("VALIDATION_TOOLCHAIN={}", self.toolchain()?),
        ])
    }

    pub(super) fn run_command(&self, command: &[String]) -> Result<()> {
        self.run(
            &command[0],
            &command[1..].iter().map(String::as_str).collect::<Vec<_>>(),
        )
    }

    pub(super) fn preflight(&self, selection: &ReleaseSelection) -> Result<()> {
        self.configuration()?;
        self.check_selection(selection)?;
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source,
            "source HEAD differs from the saved release",
        )?;
        require(
            self.version(None)? == selection.previous,
            "preflight requires the saved base version",
        )?;
        let surfaces = self.surfaces(selection)?;
        self.check_surfaces(selection, &surfaces, None, true)?;
        self.check_index(selection, &surfaces)?;
        let lock = fs::read(self.root.join("Cargo.lock"))?;
        let interrupted = lock != self.text("Cargo.lock", Some(&selection.source))?.as_bytes();
        if interrupted {
            // The exact candidate lock can precede the manifest after an
            // interrupted write. Reuse source-bound successful validation;
            // locked Cargo fetching cannot reconcile that temporary mismatch.
            self.validated(selection)?;
        } else {
            check_lock_update(
                &lock,
                std::str::from_utf8(&lock)?,
                &selection.previous,
                &selection.previous,
            )?;
        }
        self.remote_ready(selection)?;
        // Remote inspection and offline cache verification cannot change the
        // tracked lockfile whose selection will qualify this attempt.
        if !interrupted {
            self.run(
                "cargo",
                &[
                    &format!("+{}", self.toolchain()?),
                    "fetch",
                    "--locked",
                    "--offline",
                ],
            )?;
            // Only coherent, admitted release sources may prepare tools. The
            // complete common selection is required by the offline full gate.
            self.run("make", &["--no-print-directory", "install-tools"])?;
        }
        self.run(
            "make",
            &["--no-print-directory", "tools-check", "format-tools-check"],
        )?;
        require(
            fs::read(self.root.join("Cargo.lock"))? == lock,
            "dependency selection changed during preflight",
        )?;
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source,
            "source changed during preflight",
        )?;
        self.check_surfaces(selection, &surfaces, None, true)?;
        self.check_index(selection, &surfaces)
    }

    pub(super) fn verify(&self, selection: &ReleaseSelection) -> Result<()> {
        self.run("bash", &["scripts/ci/check-make-execution.sh"])?;
        let configuration = self.configuration()?;
        self.check_selection(selection)?;
        self.clean()?;
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source
                && self.version(None)? == selection.previous,
            "validation requires the saved source and base version",
        )?;
        let lock = fs::read(self.root.join("Cargo.lock"))?;
        check_lock_update(
            &lock,
            std::str::from_utf8(&lock)?,
            &selection.previous,
            &selection.previous,
        )?;
        let evidence = ValidationEvidence {
            selection: selection.clone(),
            lock,
            identities: self.identities()?,
            configuration,
            command: self.validation_command()?,
        };
        self.run_command(&evidence.command)?;
        self.clean()?;
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source,
            "source changed during validation",
        )?;
        self.verify_inputs(&evidence, false)?;
        let path = self.validation_path(&selection.version)?;
        let stale_prepared = self.superseded_preparation(selection)?;
        self.retain_receipt(&path, "validated", &selection.version)?;
        if let Some(receipt) = &stale_prepared {
            self.retain_receipt(receipt, "prepared", &selection.version)?;
        }
        // Fresh successful validation supersedes only an admitted older source.
        // Preserve both receipts first; unchanged-source recovery keeps its active
        // prepared receipt and existing integrity checks.
        if let Some(receipt) = stale_prepared {
            fs::remove_file(receipt)?;
        }
        self.exec.write_json(&path, &evidence)
    }

    fn superseded_preparation(&self, selection: &ReleaseSelection) -> Result<Option<PathBuf>> {
        let path = self.receipt_path(&selection.version, Prepared)?;
        if !path.exists() {
            return Ok(None);
        }
        let prepared: PackageEvidence = serde_json::from_slice(&fs::read(&path)?)?;
        let previous = &prepared.validation.selection;
        if previous.source == selection.source {
            return Ok(None);
        }
        require(
            previous.kind == selection.kind
                && previous.previous == selection.previous
                && previous.version == selection.version
                && previous.remote == selection.remote
                && previous.branch == selection.branch,
            "superseded preparation identifies another release or destination",
        )?;
        require(
            prepared.package_head == previous.source,
            "superseded package evidence differs from its preparation source",
        )?;
        self.git(&[
            "merge-base",
            "--is-ancestor",
            &previous.source,
            &selection.source,
        ])?;
        Ok(Some(path))
    }

    fn retain_receipt(&self, receipt: &Path, kind: &str, version: &str) -> Result<()> {
        if receipt.exists() {
            let retained = receipt
                .parent()
                .ok_or("validation path has no parent")?
                .join("attempts")
                .join(format!(
                    "{version}-{kind}.{}.{}.json",
                    std::process::id(),
                    SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos()
                ));
            self.exec.write_bytes(&retained, &fs::read(receipt)?)?;
        }
        Ok(())
    }

    pub(super) fn verify_inputs(&self, evidence: &ValidationEvidence, updated: bool) -> Result<()> {
        require(
            evidence.lock
                == self
                    .text("Cargo.lock", Some(&evidence.selection.source))?
                    .as_bytes(),
            "validation lockfile differs from the saved source",
        )?;
        require(
            self.configuration()? == evidence.configuration,
            "build configuration differs from release validation",
        )?;
        require(
            self.identities()? == evidence.identities,
            "compiler identities differ from release validation",
        )?;
        require(
            evidence.command == self.validation_command()?,
            "validation evidence has unexpected commands",
        )?;
        self.check_selection(&evidence.selection)?;
        let current = fs::read(self.root.join("Cargo.lock"))?;
        if current != evidence.lock {
            require(updated, "selected lockfile differs from release validation")?;
            check_lock_update(
                &evidence.lock,
                std::str::from_utf8(&current)?,
                &evidence.selection.previous,
                &evidence.selection.version,
            )?;
        }
        Ok(())
    }

    pub(super) fn validated(&self, selection: &ReleaseSelection) -> Result<ValidationEvidence> {
        let evidence: ValidationEvidence =
            serde_json::from_slice(&fs::read(self.validation_path(&selection.version)?)?)?;
        require(
            evidence.selection == *selection,
            "validation identifies another release intent",
        )?;
        self.verify_inputs(&evidence, true)?;
        Ok(evidence)
    }

    pub(super) fn preparation_commands(&self) -> Result<[Vec<String>; 3]> {
        let pin = format!("+{}", self.toolchain()?);
        Ok([
            vec![
                "cargo".to_owned(),
                pin.clone(),
                "update".to_owned(),
                "--workspace".to_owned(),
                "--offline".to_owned(),
            ],
            vec![
                "make".to_owned(),
                "--no-print-directory".to_owned(),
                "fmt-check".to_owned(),
                format!("VALIDATION_TOOLCHAIN={}", self.toolchain()?),
            ],
            vec![
                "bash".to_owned(),
                "scripts/ci/package-workspace.sh".to_owned(),
                pin.trim_start_matches('+').to_owned(),
                "--allow-dirty".to_owned(),
            ],
        ])
    }

    pub(super) fn final_package_command(&self) -> Result<Vec<String>> {
        Ok(vec![
            "bash".to_owned(),
            "scripts/ci/package-workspace.sh".to_owned(),
            self.toolchain()?,
        ])
    }

    pub(super) fn write_evidence(
        &self,
        evidence: &PackageEvidence,
        stage: ReceiptStage,
    ) -> Result<()> {
        self.exec.write_json(
            &self.receipt_path(&evidence.validation.selection.version, stage)?,
            evidence,
        )
    }

    pub(super) fn prepare_selected(&self, selection: &ReleaseSelection) -> Result<()> {
        self.validated(selection)?;
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source,
            "source changed before preparation",
        )?;
        let expected = self.surfaces(selection)?;
        self.check_surfaces(selection, &expected, None, true)?;
        let edits = expected
            .iter()
            .map(|(path, surface)| {
                Ok(MetadataEdit {
                    path,
                    expected: &surface.expected,
                    before: fs::read(self.root.join(path))?,
                })
            })
            .collect::<Result<Vec<_>>>()?;
        let result = (|| -> Result<()> {
            // Publish the root manifest last: the candidate version must imply
            // that all other metadata, including its selected lock, is complete.
            for edit in edits
                .iter()
                .filter(|edit| edit.path != "Cargo.toml")
                .chain(edits.iter().filter(|edit| edit.path == "Cargo.toml"))
            {
                edit.publish(self)?;
            }
            // Source-derived candidate bytes remain the same throughout this
            // transaction. Re-admit the live HEAD, receipt and validation inputs
            // after publication, then qualify against this exact projection.
            self.qualify_prepared(self.preparation_validation(selection)?, &expected)?;
            Ok(())
        })();
        if let Err(source) = result {
            // Durable publication may report a parent-sync error after rename.
            // Inspect current bytes before restoring only owned replacements.
            // A failed restoration must not hide the preparation error or stop
            // restoration of other independent, owned metadata files.
            let mut rollback_errors = Vec::new();
            for edit in &edits {
                if let Err(error) = edit.restore(self, selection) {
                    rollback_errors.push((edit.path.to_owned(), error));
                }
            }
            if rollback_errors.is_empty() {
                Err(source)
            } else {
                Err(PreparationError {
                    source,
                    rollback_errors,
                }
                .into())
            }
        } else {
            result
        }
    }

    pub(super) fn verify_evidence(
        &self,
        selection: &ReleaseSelection,
        head: &str,
        stage: ReceiptStage,
        location: ArchiveLocation,
    ) -> Result<PackageEvidence> {
        let evidence: PackageEvidence =
            serde_json::from_slice(&fs::read(self.receipt_path(&selection.version, stage)?)?)?;
        require(
            evidence.validation.selection == *selection,
            "package evidence identifies another release intent",
        )?;
        self.verify_inputs(&evidence.validation, true)?;
        require(
            Self::sha256(&self.root.join("Cargo.lock"))? == evidence.lock_sha256,
            "selected lockfile differs from package qualification",
        )?;
        require(
            evidence.package_head == head,
            "package evidence identifies another Git HEAD",
        )?;
        for (package, digest) in evidence.archives() {
            require(
                Self::sha256(&self.retained_package(digest)?)? == digest,
                "retained archive differs from release validation",
            )?;
            if location == Working {
                require(
                    Self::sha256(&self.package_path(package, &selection.version)?)? == digest,
                    "working package differs from release validation",
                )?;
            }
        }
        let mut commands = self.preparation_commands()?.to_vec();
        if stage == Final {
            commands.push(self.final_package_command()?);
        }
        require(
            evidence.commands == commands,
            "package evidence has unexpected commands",
        )?;
        Ok(evidence)
    }

    pub(super) fn prepared(&self, selection: &ReleaseSelection) -> Result<PackageEvidence> {
        let validation = self.preparation_validation(selection)?;
        let surfaces = self.surfaces(selection)?;
        self.qualify_prepared(validation, &surfaces)
    }

    pub(super) fn preparation_validation(
        &self,
        selection: &ReleaseSelection,
    ) -> Result<ValidationEvidence> {
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source,
            "prepared source changed",
        )?;
        self.validated(selection)
    }

    pub(super) fn qualify_prepared(
        &self,
        validation: ValidationEvidence,
        surfaces: &BTreeMap<String, ReleaseSurface>,
    ) -> Result<PackageEvidence> {
        let selection = &validation.selection;
        self.check_surfaces(selection, surfaces, None, false)?;
        if self.receipt_path(&selection.version, Prepared)?.exists() {
            // Existing evidence is checked, never silently repaired or replaced.
            return self.verify_evidence(selection, &selection.source, Prepared, Working);
        }
        let commands = self.preparation_commands()?;
        let [refresh, formatting, packaging] = &commands;
        self.run_command(refresh)?;
        check_lock_update(
            &validation.lock,
            &fs::read_to_string(self.root.join("Cargo.lock"))?,
            &selection.previous,
            &selection.version,
        )?;
        self.run_command(formatting)?;
        let lock_sha256 = Self::sha256(&self.root.join("Cargo.lock"))?;
        self.run_command(packaging)?;
        self.check_surfaces(selection, surfaces, None, false)?;
        require(
            self.git(&["rev-parse", "HEAD"])? == selection.source,
            "source changed during preparation",
        )?;
        self.verify_inputs(&validation, true)?;
        require(
            Self::sha256(&self.root.join("Cargo.lock"))? == lock_sha256,
            "packaging changed selected dependencies",
        )?;
        let head = selection.source.clone();
        let evidence =
            PackageEvidence::record(self, validation, head, lock_sha256, commands.to_vec())?;
        self.write_evidence(&evidence, Prepared)?;
        Ok(evidence)
    }
}