The Snapshot2Context manages the relationship between a VM's memory and an external DataSource. It tracks which memory pages are backed by the DataSource versus which pages are "dirty" (modified and requiring full storage).
Key Workflows:
1. Loading a Program efficiently
To reduce snapshot size, you should track memory pages belonging to the program using mark_program. This requires parsing the ELF first to get ProgramMetadata.
// 1. Parse ELF to get metadata
let metadata = elf::parse_elf(elf_data)?;
// 2. Load program into machine
machine.load_program_with_metadata(metadata.clone(), ...)?;
// 3. Mark pages in context to track them as coming from the DataSource
context.mark_program(&mut machine, &metadata, &program_id, program_offset)?;
2. Creating a Snapshot
Use make_snapshot to capture the current state of a SupportMachine. The resulting Snapshot2 contains:
pages_from_source: Pages that can be reconstructed from the DataSource.dirty_pages: Pages that have been modified and must be stored as raw bytes.- Machine state:
registers, pc, cycles, and max_cycles.
3. Resuming from a Snapshot
Use resume to restore a machine to a previously captured state. This clears the current context's page tracking and restores registers, PC, and memory (both from the source and dirty pages).
// Example: Creating a snapshot
let snapshot = context.make_snapshot(&mut machine)?;
// Example: Resuming a machine
context.resume(&mut machine, &snapshot)?;