Skip to content

Commit

Permalink
Let snapshots stores the load_reservation_address
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Mar 14, 2024
1 parent b96530f commit 7f140f8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/snapshot2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
machine.memory_mut().set_flag(page, *flag)?;
}
}
machine
.memory_mut()
.set_lr(&M::REG::from_u64(snapshot.load_reservation_address));
Ok(())
}

Expand Down Expand Up @@ -200,6 +203,7 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
pc: machine.pc().to_u64(),
cycles: machine.cycles(),
max_cycles: machine.max_cycles(),
load_reservation_address: machine.memory().lr().to_u64(),
})
}

Expand Down Expand Up @@ -257,4 +261,5 @@ pub struct Snapshot2<I: Clone + PartialEq> {
pub pc: u64,
pub cycles: u64,
pub max_cycles: u64,
pub load_reservation_address: u64,
}
4 changes: 3 additions & 1 deletion tests/test_a_extension.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use ckb_vm::{CoreMachine, Error, Memory};
use ckb_vm::Error;
#[cfg(has_asm)]
use ckb_vm::{CoreMachine, Memory};
pub mod machine_build;

#[test]
Expand Down
34 changes: 29 additions & 5 deletions tests/test_resume2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use ckb_vm::elf::parse_elf;
use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine};
use ckb_vm::machine::trace::TraceMachine;
use ckb_vm::machine::{
CoreMachine, DefaultCoreMachine, DefaultMachine, SupportMachine, VERSION0, VERSION1,
CoreMachine, DefaultCoreMachine, DefaultMachine, SupportMachine, VERSION0, VERSION1, VERSION2,
};
use ckb_vm::memory::{sparse::SparseMemory, wxorx::WXorXMemory};
use ckb_vm::registers::{A0, A1, A7};
use ckb_vm::snapshot2::{DataSource, Snapshot2, Snapshot2Context};
use ckb_vm::{DefaultMachineBuilder, Error, Register, Syscalls, ISA_IMC};
use ckb_vm::{DefaultMachineBuilder, Error, Register, Syscalls, ISA_A, ISA_IMC};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -379,7 +379,7 @@ impl MachineTy {
match self {
MachineTy::Asm => {
let context = Arc::new(Mutex::new(Snapshot2Context::new(data_source)));
let asm_core1 = AsmCoreMachine::new(ISA_IMC, version, 0);
let asm_core1 = AsmCoreMachine::new(ISA_IMC | ISA_A, version, 0);
let core1 = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core1)
.instruction_cycle_func(Box::new(constant_cycles))
.syscall(Box::new(InsertDataSyscall(context.clone())))
Expand All @@ -389,7 +389,9 @@ impl MachineTy {
MachineTy::Interpreter => {
let context = Arc::new(Mutex::new(Snapshot2Context::new(data_source)));
let core_machine1 = DefaultCoreMachine::<u64, WXorXMemory<SparseMemory<u64>>>::new(
ISA_IMC, version, 0,
ISA_IMC | ISA_A,
version,
0,
);
Machine::Interpreter(
DefaultMachineBuilder::<DefaultCoreMachine<u64, WXorXMemory<SparseMemory<u64>>>>::new(
Expand All @@ -404,7 +406,9 @@ impl MachineTy {
MachineTy::InterpreterWithTrace => {
let context = Arc::new(Mutex::new(Snapshot2Context::new(data_source)));
let core_machine1 = DefaultCoreMachine::<u64, WXorXMemory<SparseMemory<u64>>>::new(
ISA_IMC, version, 0,
ISA_IMC | ISA_A,
version,
0,
);
Machine::InterpreterWithTrace(
TraceMachine::new(
Expand Down Expand Up @@ -596,3 +600,23 @@ impl Machine {
Ok(())
}
}

#[test]
pub fn test_sc_after_snapshot2() {
let data_source = load_program("tests/programs/sc_after_snapshot");

let mut machine1 = MachineTy::Interpreter.build(data_source.clone(), VERSION2);
machine1.set_max_cycles(5);
machine1.load_program(&vec!["main".into()]).unwrap();
let result1 = machine1.run();
assert!(result1.is_err());
assert_eq!(result1.unwrap_err(), Error::CyclesExceeded);
let snapshot = machine1.snapshot().unwrap();

let mut machine2 = MachineTy::Interpreter.build(data_source, VERSION2);
machine2.resume(snapshot).unwrap();
machine2.set_max_cycles(20);
let result2 = machine2.run();
assert!(result2.is_ok());
assert_eq!(result2.unwrap(), 0);
}

0 comments on commit 7f140f8

Please sign in to comment.