diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 32782509..d0e0c6d7 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -8,7 +8,7 @@ trigger: variables: RUST_BACKTRACE: full - TEST_SUITE_COMMIT: a1fb578bf802ce1155565d509402e7e00a1c280b + TEST_SUITE_COMMIT: ff49a1dfb3c12be9dc18073133d660f0dd176662 jobs: - job: WinCI diff --git a/benches/vm_benchmark.rs b/benches/vm_benchmark.rs index 3638ccb1..54ce4dd5 100644 --- a/benches/vm_benchmark.rs +++ b/benches/vm_benchmark.rs @@ -58,12 +58,12 @@ fn aot_benchmark(c: &mut Criterion) { "foo", "bar"].into_iter().map(|a| a.into()).collect(); let mut aot_machine = AotCompilingMachine::load(&buffer.clone(), None, ISA_IMC, VERSION0).unwrap(); - let result = std::sync::Arc::new(aot_machine.compile().unwrap()); + let result = aot_machine.compile().unwrap(); b.iter(|| { let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(result.clone())); + let mut machine = AsmMachine::new(core, Some(&result)); machine.load_program(&buffer, &args[..]).unwrap(); machine.run().unwrap() }); diff --git a/examples/ckb-vm-runner.rs b/examples/ckb-vm-runner.rs index 7a404a59..7c308b3d 100644 --- a/examples/ckb-vm-runner.rs +++ b/examples/ckb-vm-runner.rs @@ -105,8 +105,7 @@ fn main_aot(code: Bytes, args: Vec) -> Result<(), Box { type REG = u64; @@ -432,9 +433,9 @@ impl AotCode { } } -pub struct AsmMachine { - pub machine: DefaultMachine>, - pub aot_code: Option>, +pub struct AsmMachine<'a> { + pub machine: DefaultMachine<'a, Box>, + pub aot_code: Option<&'a AotCode>, } extern "C" { @@ -444,10 +445,10 @@ extern "C" { fn ckb_vm_asm_labels(); } -impl AsmMachine { +impl<'a> AsmMachine<'a> { pub fn new( - machine: DefaultMachine>, - aot_code: Option>, + machine: DefaultMachine<'a, Box>, + aot_code: Option<&'a AotCode>, ) -> Self { Self { machine, aot_code } } diff --git a/src/machine/mod.rs b/src/machine/mod.rs index 0da75a0e..ccfc5898 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -408,7 +408,7 @@ impl DefaultCoreMachine { pub type InstructionCycleFunc = dyn Fn(Instruction) -> u64; -pub struct DefaultMachine { +pub struct DefaultMachine<'a, Inner> { inner: Inner, // We have run benchmarks on secp256k1 verification, the performance @@ -416,12 +416,12 @@ pub struct DefaultMachine { // with Box solution for simplicity now. Later if this becomes an issue, // we can change to static dispatch. instruction_cycle_func: Box, - debugger: Option>>, - syscalls: Vec>>, + debugger: Option + 'a>>, + syscalls: Vec + 'a>>, exit_code: i8, } -impl CoreMachine for DefaultMachine { +impl CoreMachine for DefaultMachine<'_, Inner> { type REG = ::REG; type MEM = ::MEM; @@ -462,7 +462,7 @@ impl CoreMachine for DefaultMachine { } } -impl SupportMachine for DefaultMachine { +impl SupportMachine for DefaultMachine<'_, Inner> { fn cycles(&self) -> u64 { self.inner.cycles() } @@ -497,7 +497,7 @@ impl SupportMachine for DefaultMachine { } } -impl Machine for DefaultMachine { +impl Machine for DefaultMachine<'_, Inner> { fn ecall(&mut self) -> Result<(), Error> { let code = self.registers()[A7].to_u64(); match code { @@ -533,7 +533,7 @@ impl Machine for DefaultMachine { } } -impl Display for DefaultMachine { +impl Display for DefaultMachine<'_, Inner> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "pc : 0x{:16X}", self.pc().to_u64())?; for (i, name) in REGISTER_ABI_NAMES.iter().enumerate() { @@ -548,7 +548,7 @@ impl Display for DefaultMachine { } } -impl DefaultMachine { +impl<'a, Inner: SupportMachine> DefaultMachine<'a, Inner> { pub fn load_program(&mut self, program: &Bytes, args: &[Bytes]) -> Result { let elf_bytes = self.load_elf(program, true)?; for syscall in &mut self.syscalls { @@ -621,14 +621,14 @@ impl DefaultMachine { } } -pub struct DefaultMachineBuilder { +pub struct DefaultMachineBuilder<'a, Inner> { inner: Inner, instruction_cycle_func: Box, - debugger: Option>>, - syscalls: Vec>>, + debugger: Option + 'a>>, + syscalls: Vec + 'a>>, } -impl DefaultMachineBuilder { +impl<'a, Inner> DefaultMachineBuilder<'a, Inner> { pub fn new(inner: Inner) -> Self { Self { inner, @@ -646,17 +646,17 @@ impl DefaultMachineBuilder { self } - pub fn syscall(mut self, syscall: Box>) -> Self { + pub fn syscall(mut self, syscall: Box + 'a>) -> Self { self.syscalls.push(syscall); self } - pub fn debugger(mut self, debugger: Box>) -> Self { + pub fn debugger(mut self, debugger: Box + 'a>) -> Self { self.debugger = Some(debugger); self } - pub fn build(self) -> DefaultMachine { + pub fn build(self) -> DefaultMachine<'a, Inner> { DefaultMachine { inner: self.inner, instruction_cycle_func: self.instruction_cycle_func, diff --git a/src/machine/trace.rs b/src/machine/trace.rs index 76f79ac8..aed2dbb0 100644 --- a/src/machine/trace.rs +++ b/src/machine/trace.rs @@ -32,13 +32,13 @@ fn calculate_slot(addr: u64) -> usize { (addr as usize >> TRACE_ADDRESS_SHIFTS) & TRACE_MASK } -pub struct TraceMachine { - pub machine: DefaultMachine, +pub struct TraceMachine<'a, Inner> { + pub machine: DefaultMachine<'a, Inner>, traces: Vec, } -impl CoreMachine for TraceMachine { +impl CoreMachine for TraceMachine<'_, Inner> { type REG = ::REG; type MEM = ::MEM; @@ -79,7 +79,7 @@ impl CoreMachine for TraceMachine { } } -impl Machine for TraceMachine { +impl Machine for TraceMachine<'_, Inner> { fn ecall(&mut self) -> Result<(), Error> { self.machine.ecall() } @@ -89,8 +89,8 @@ impl Machine for TraceMachine { } } -impl TraceMachine { - pub fn new(machine: DefaultMachine) -> Self { +impl<'a, Inner: SupportMachine> TraceMachine<'a, Inner> { + pub fn new(machine: DefaultMachine<'a, Inner>) -> Self { Self { machine, traces: vec![], diff --git a/tests/machine_build.rs b/tests/machine_build.rs index 047d6e3b..d50fd78d 100644 --- a/tests/machine_build.rs +++ b/tests/machine_build.rs @@ -3,17 +3,18 @@ use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine}; #[cfg(has_aot)] use ckb_vm::machine::{aot::AotCompilingMachine, asm::AotCode}; -use bytes::Bytes; use ckb_vm::machine::{trace::TraceMachine, DefaultCoreMachine, VERSION1}; use ckb_vm::{DefaultMachineBuilder, ISA_B, ISA_IMC, ISA_MOP}; use ckb_vm::{Instruction, SparseMemory, WXorXMemory}; +use bytes::Bytes; + pub fn instruction_cycle_func(_: Instruction) -> u64 { 1 } #[cfg(has_asm)] -pub fn asm_v1_imcb(path: &str) -> AsmMachine { +pub fn asm_v1_imcb<'a>(path: &str) -> AsmMachine<'a> { let buffer: Bytes = std::fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B, VERSION1, u64::max_value()); let core = DefaultMachineBuilder::>::new(asm_core) @@ -35,14 +36,14 @@ pub fn aot_v1_imcb_code(path: &str) -> AotCode { } #[cfg(has_aot)] -pub fn aot_v1_imcb(path: &str, code: AotCode) -> AsmMachine { +pub fn aot_v1_imcb<'a>(path: &str, code: &'a AotCode) -> AsmMachine<'a> { let buffer: Bytes = std::fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B, VERSION1, u64::max_value()); let core = DefaultMachineBuilder::>::new(asm_core) .instruction_cycle_func(Box::new(instruction_cycle_func)) .build(); - let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(code)); machine .load_program(&buffer, &vec![Bytes::from("main")]) .unwrap(); @@ -70,7 +71,7 @@ pub fn int_v1_imcb( } #[cfg(has_asm)] -pub fn asm_v1_mop(path: &str, args: Vec) -> AsmMachine { +pub fn asm_v1_mop<'a>(path: &str, args: Vec) -> AsmMachine<'a> { let buffer: Bytes = std::fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, VERSION1, u64::max_value()); let core = DefaultMachineBuilder::>::new(asm_core) @@ -92,7 +93,7 @@ pub fn aot_v1_mop_code(path: &str) -> AotCode { } #[cfg(has_aot)] -pub fn aot_v1_mop(path: &str, args: Vec, code: AotCode) -> AsmMachine { +pub fn aot_v1_mop<'a>(path: &str, args: Vec, code: &'a AotCode) -> AsmMachine<'a> { let buffer: Bytes = std::fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, VERSION1, u64::max_value()); @@ -101,7 +102,7 @@ pub fn aot_v1_mop(path: &str, args: Vec, code: AotCode) -> AsmMachine { .build(); let mut argv = vec![Bytes::from("main")]; argv.extend_from_slice(&args); - let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(code)); machine.load_program(&buffer, &argv).unwrap(); machine } diff --git a/tests/test_aot.rs b/tests/test_aot.rs index c06d1716..f1b15075 100644 --- a/tests/test_aot.rs +++ b/tests/test_aot.rs @@ -22,7 +22,7 @@ pub fn test_aot_simple64() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["simple".into()]) .unwrap(); @@ -63,7 +63,7 @@ pub fn test_aot_with_custom_syscall() { let core = DefaultMachineBuilder::new(asm_core) .syscall(Box::new(CustomSyscall {})) .build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["syscall".into()]) .unwrap(); @@ -100,7 +100,7 @@ pub fn test_aot_ebreak() { value: Arc::clone(&value), })) .build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["ebreak".into()]) .unwrap(); @@ -125,7 +125,7 @@ pub fn test_aot_simple_cycles() { AotCompilingMachine::load(&buffer, Some(Box::new(dummy_cycle_func)), ISA_IMC, VERSION0) .unwrap(); let code = aot_machine.compile().unwrap(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["syscall".into()]) .unwrap(); @@ -148,7 +148,7 @@ pub fn test_aot_simple_max_cycles_reached() { AotCompilingMachine::load(&buffer, Some(Box::new(dummy_cycle_func)), ISA_IMC, VERSION0) .unwrap(); let code = aot_machine.compile().unwrap(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["syscall".into()]) .unwrap(); @@ -164,7 +164,7 @@ pub fn test_aot_trace() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["simple".into()]) .unwrap(); @@ -180,7 +180,7 @@ pub fn test_aot_jump0() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["jump0_64".into()]) .unwrap(); @@ -198,7 +198,7 @@ pub fn test_aot_write_large_address() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["write_large_address64".into()]) .unwrap(); @@ -214,7 +214,7 @@ pub fn test_aot_misaligned_jump64() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["write_large_address64".into()]) .unwrap(); @@ -229,7 +229,7 @@ pub fn test_aot_mulw64() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["mulw64".into()]) .unwrap(); @@ -245,7 +245,7 @@ pub fn test_aot_invalid_read64() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["invalid_read64".into()]) .unwrap(); @@ -261,7 +261,7 @@ pub fn test_aot_load_elf_crash_64() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["load_elf_crash_64".into()]) .unwrap(); @@ -276,7 +276,7 @@ pub fn test_aot_wxorx_crash_64() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["wxorx_crash_64".into()]) .unwrap(); @@ -316,7 +316,7 @@ pub fn test_aot_alloc_many() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["alloc_many".into()]) .unwrap(); @@ -334,7 +334,7 @@ pub fn test_aot_chaos_seed() { asm_core1.chaos_mode = 1; asm_core1.chaos_seed = 100; let core1 = DefaultMachineBuilder::>::new(asm_core1).build(); - let mut machine1 = AsmMachine::new(core1, Some(Arc::new(code1))); + let mut machine1 = AsmMachine::new(core1, Some(&code1)); machine1 .load_program(&buffer, &vec!["read_memory".into()]) .unwrap(); @@ -347,7 +347,7 @@ pub fn test_aot_chaos_seed() { asm_core2.chaos_mode = 1; asm_core2.chaos_seed = 100; let core2 = DefaultMachineBuilder::>::new(asm_core2).build(); - let mut machine2 = AsmMachine::new(core2, Some(Arc::new(code2))); + let mut machine2 = AsmMachine::new(core2, Some(&code2)); machine2 .load_program(&buffer, &vec!["read_memory".into()]) .unwrap(); @@ -367,7 +367,7 @@ pub fn test_aot_rvc_pageend() { let code = aot_machine.compile().unwrap(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["rvc_pageend".into()]) .unwrap(); @@ -411,7 +411,7 @@ pub fn test_aot_outofcycles_in_syscall() { .instruction_cycle_func(Box::new(|_| 1)) .syscall(Box::new(OutOfCyclesSyscall {})) .build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["syscall".into()]) .unwrap(); @@ -434,7 +434,7 @@ pub fn test_aot_cycles_overflow() { let core = DefaultMachineBuilder::>::new(asm_core) .instruction_cycle_func(Box::new(|_| 1)) .build(); - let mut machine = AsmMachine::new(core, Some(Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine .load_program(&buffer, &vec!["simple64".into()]) .unwrap(); diff --git a/tests/test_b_extension.rs b/tests/test_b_extension.rs index d691555c..8d90df68 100644 --- a/tests/test_b_extension.rs +++ b/tests/test_b_extension.rs @@ -18,7 +18,7 @@ pub fn test_clzw_bug() { #[cfg(has_aot)] { let code = machine_build::aot_v1_imcb_code("tests/programs/clzw_bug"); - let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/clzw_bug", code); + let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/clzw_bug", &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -44,7 +44,7 @@ pub fn test_sbinvi_aot_load_imm_bug() { { let code = machine_build::aot_v1_imcb_code("tests/programs/sbinvi_aot_load_imm_bug"); let mut machine_aot = - machine_build::aot_v1_imcb("tests/programs/sbinvi_aot_load_imm_bug", code); + machine_build::aot_v1_imcb("tests/programs/sbinvi_aot_load_imm_bug", &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -71,7 +71,7 @@ pub fn test_rorw_in_end_of_aot_block() { { let code = machine_build::aot_v1_imcb_code("tests/programs/rorw_in_end_of_aot_block"); let mut machine_aot = - machine_build::aot_v1_imcb("tests/programs/rorw_in_end_of_aot_block", code); + machine_build::aot_v1_imcb("tests/programs/rorw_in_end_of_aot_block", &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -96,7 +96,7 @@ pub fn test_pcnt() { #[cfg(has_aot)] { let code = machine_build::aot_v1_imcb_code("tests/programs/pcnt"); - let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/pcnt", code); + let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/pcnt", &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -121,7 +121,7 @@ pub fn test_clmul_bug() { #[cfg(has_aot)] { let code = machine_build::aot_v1_imcb_code("tests/programs/clmul_bug"); - let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/clmul_bug", code); + let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/clmul_bug", &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -146,7 +146,7 @@ pub fn test_orc_bug() { #[cfg(has_aot)] { let code = machine_build::aot_v1_imcb_code("tests/programs/orc_bug"); - let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/orc_bug", code); + let mut machine_aot = machine_build::aot_v1_imcb("tests/programs/orc_bug", &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); diff --git a/tests/test_mop.rs b/tests/test_mop.rs index a0204869..caef47b3 100644 --- a/tests/test_mop.rs +++ b/tests/test_mop.rs @@ -31,7 +31,7 @@ pub fn test_mop_wide_multiply() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_wide_multiply"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_wide_multiply", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_wide_multiply", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -66,7 +66,7 @@ pub fn test_mop_wide_divide() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_wide_divide"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_wide_divide", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_wide_divide", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -100,7 +100,7 @@ pub fn test_mop_far_jump() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_far_jump"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_far_jump", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_far_jump", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -134,7 +134,7 @@ pub fn test_mop_ld_32_constants() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_ld_signextend_32"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_ld_signextend_32", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_ld_signextend_32", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -171,7 +171,7 @@ pub fn test_mop_secp256k1() { { let code = machine_build::aot_v1_mop_code("benches/data/secp256k1_bench"); let mut machine_aot = - machine_build::aot_v1_mop("benches/data/secp256k1_bench", args.clone(), code); + machine_build::aot_v1_mop("benches/data/secp256k1_bench", args.clone(), &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -205,7 +205,7 @@ pub fn test_mop_adc() { #[cfg(has_aot)] { let code = machine_build::aot_v1_mop_code("tests/programs/mop_adc"); - let mut machine_aot = machine_build::aot_v1_mop("tests/programs/mop_adc", vec![], code); + let mut machine_aot = machine_build::aot_v1_mop("tests/programs/mop_adc", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -239,7 +239,7 @@ pub fn test_mop_sbb() { #[cfg(has_aot)] { let code = machine_build::aot_v1_mop_code("tests/programs/mop_sbb"); - let mut machine_aot = machine_build::aot_v1_mop("tests/programs/mop_sbb", vec![], code); + let mut machine_aot = machine_build::aot_v1_mop("tests/programs/mop_sbb", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -275,7 +275,7 @@ pub fn test_mop_random_adc_sbb() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_random_adc_sbb"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_random_adc_sbb", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_random_adc_sbb", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -307,7 +307,7 @@ pub fn test_mop_ld_signextend_32_overflow_bug() { let mut machine_aot = machine_build::aot_v1_mop( "tests/programs/mop_ld_signextend_32_overflow_bug", vec![], - code, + &code, ); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); @@ -334,7 +334,7 @@ pub fn test_mop_wide_mul_zero() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_wide_mul_zero"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_wide_mul_zero", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_wide_mul_zero", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); @@ -360,7 +360,7 @@ pub fn test_mop_wide_div_zero() { { let code = machine_build::aot_v1_mop_code("tests/programs/mop_wide_div_zero"); let mut machine_aot = - machine_build::aot_v1_mop("tests/programs/mop_wide_div_zero", vec![], code); + machine_build::aot_v1_mop("tests/programs/mop_wide_div_zero", vec![], &code); let ret_aot = machine_aot.run(); assert!(ret_aot.is_ok()); assert_eq!(ret_aot.unwrap(), 0); diff --git a/tests/test_reset.rs b/tests/test_reset.rs index 64e3dfa8..0fa7366e 100644 --- a/tests/test_reset.rs +++ b/tests/test_reset.rs @@ -128,7 +128,7 @@ pub fn test_reset_aot() { .instruction_cycle_func(Box::new(machine_build::instruction_cycle_func)) .syscall(Box::new(CustomSyscall {})) .build(); - let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); machine.load_program(&buffer, &vec![]).unwrap(); let result = machine.run(); diff --git a/tests/test_resume.rs b/tests/test_resume.rs index 0ce4b89c..eed299e0 100644 --- a/tests/test_resume.rs +++ b/tests/test_resume.rs @@ -199,8 +199,7 @@ pub fn resume_aot_2_asm(version: u32, except_cycles: u64) { AotCompilingMachine::load(&buffer, Some(Box::new(dummy_cycle_func)), ISA_IMC, VERSION1) .unwrap(); let code = aot_machine.compile().unwrap(); - let mut machine1 = - AotMachine::build(version, except_cycles - 30, Some(std::sync::Arc::new(code))); + let mut machine1 = AotMachine::build(version, except_cycles - 30, Some(&code)); machine1 .load_program(&buffer, &vec!["alloc_many".into()]) .unwrap(); @@ -237,7 +236,7 @@ pub fn resume_asm_2_aot(version: u32, except_cycles: u64) { AotCompilingMachine::load(&buffer, Some(Box::new(dummy_cycle_func)), ISA_IMC, VERSION1) .unwrap(); let code = aot_machine.compile().unwrap(); - let mut machine2 = AotMachine::build(version, 40, Some(std::sync::Arc::new(code))); + let mut machine2 = AotMachine::build(version, 40, Some(&code)); machine2.resume(&snapshot).unwrap(); let result2 = machine2.run(); let cycles2 = machine2.cycles(); @@ -282,7 +281,7 @@ enum MachineTy { } impl MachineTy { - fn build(self, version: u32, max_cycles: u64) -> Machine { + fn build<'a>(self, version: u32, max_cycles: u64) -> Machine { match self { MachineTy::Asm => { let asm_core1 = AsmCoreMachine::new(ISA_IMC, version, max_cycles); @@ -322,9 +321,11 @@ impl MachineTy { } enum Machine { - Asm(AsmMachine), - Interpreter(DefaultMachine>>>), - InterpreterWithTrace(TraceMachine>>>), + Asm(AsmMachine<'static>), + Interpreter(DefaultMachine<'static, DefaultCoreMachine>>>), + InterpreterWithTrace( + TraceMachine<'static, DefaultCoreMachine>>>, + ), } impl Machine { @@ -380,15 +381,11 @@ impl Machine { } #[cfg(has_aot)] -struct AotMachine(AsmMachine); +struct AotMachine<'a>(AsmMachine<'a>); #[cfg(has_aot)] -impl AotMachine { - fn build( - version: u32, - max_cycles: u64, - program: Option>, - ) -> AotMachine { +impl<'a> AotMachine<'a> { + fn build(version: u32, max_cycles: u64, program: Option<&'a AotCode>) -> AotMachine<'a> { let asm_core1 = AsmCoreMachine::new(ISA_IMC, version, max_cycles); let core1 = DefaultMachineBuilder::>::new(asm_core1) .instruction_cycle_func(Box::new(dummy_cycle_func)) diff --git a/tests/test_versions.rs b/tests/test_versions.rs index f5d5ba9f..9d678b02 100644 --- a/tests/test_versions.rs +++ b/tests/test_versions.rs @@ -16,10 +16,10 @@ use std::fs; type Mem = WXorXMemory>; -fn create_rust_machine( +fn create_rust_machine<'a>( program: String, version: u32, -) -> DefaultMachine> { +) -> DefaultMachine<'a, DefaultCoreMachine> { let path = format!("tests/programs/{}", program); let buffer = fs::read(path).unwrap().into(); let core_machine = DefaultCoreMachine::::new(ISA_IMC, version, u64::max_value()); @@ -31,7 +31,7 @@ fn create_rust_machine( machine } -fn create_asm_machine(program: String, version: u32) -> AsmMachine { +fn create_asm_machine<'a>(program: String, version: u32) -> AsmMachine<'a> { let path = format!("tests/programs/{}", program); let buffer = fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC, version, u64::max_value()); @@ -52,12 +52,12 @@ fn compile_aot_code(program: String, version: u32) -> AotCode { } #[cfg(has_aot)] -fn create_aot_machine(program: String, code: AotCode, version: u32) -> AsmMachine { +fn create_aot_machine<'a>(program: String, code: &'a AotCode, version: u32) -> AsmMachine<'a> { let path = format!("tests/programs/{}", program); let buffer = fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC, version, u64::max_value()); let core = DefaultMachineBuilder::>::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(code)); machine .load_program(&buffer, &vec![program.into()]) .unwrap(); @@ -260,7 +260,7 @@ pub fn test_asm_version1_write_at_boundary() { #[cfg(has_aot)] pub fn test_aot_version0_argv_null() { let code = compile_aot_code("argv_null_test".to_string(), VERSION0); - let mut machine = create_aot_machine("argv_null_test".to_string(), code, VERSION0); + let mut machine = create_aot_machine("argv_null_test".to_string(), &code, VERSION0); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 1); @@ -270,7 +270,7 @@ pub fn test_aot_version0_argv_null() { #[cfg(has_aot)] pub fn test_aot_version0_sp_alignment() { let code = compile_aot_code("sp_alignment_test".to_string(), VERSION0); - let mut machine = create_aot_machine("sp_alignment_test".to_string(), code, VERSION0); + let mut machine = create_aot_machine("sp_alignment_test".to_string(), &code, VERSION0); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 1); @@ -280,7 +280,7 @@ pub fn test_aot_version0_sp_alignment() { #[cfg(has_aot)] pub fn test_aot_version0_jalr_bug() { let code = compile_aot_code("jalr_bug".to_string(), VERSION0); - let mut machine = create_aot_machine("jalr_bug".to_string(), code, VERSION0); + let mut machine = create_aot_machine("jalr_bug".to_string(), &code, VERSION0); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), -1); @@ -290,7 +290,7 @@ pub fn test_aot_version0_jalr_bug() { #[cfg(has_aot)] pub fn test_aot_version0_jalr_bug_noc() { let code = compile_aot_code("jalr_bug_noc".to_string(), VERSION0); - let mut machine = create_aot_machine("jalr_bug_noc".to_string(), code, VERSION0); + let mut machine = create_aot_machine("jalr_bug_noc".to_string(), &code, VERSION0); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), -1); @@ -300,7 +300,7 @@ pub fn test_aot_version0_jalr_bug_noc() { #[cfg(has_aot)] pub fn test_aot_version0_read_at_boundary() { let code = compile_aot_code("read_at_boundary64".to_string(), VERSION0); - let mut machine = create_aot_machine("read_at_boundary64".to_string(), code, VERSION0); + let mut machine = create_aot_machine("read_at_boundary64".to_string(), &code, VERSION0); let result = machine.run(); assert!(result.is_err()); assert_eq!(result.err(), Some(Error::MemOutOfBound)); @@ -310,7 +310,7 @@ pub fn test_aot_version0_read_at_boundary() { #[cfg(has_aot)] pub fn test_aot_version0_write_at_boundary() { let code = compile_aot_code("write_at_boundary64".to_string(), VERSION0); - let mut machine = create_aot_machine("write_at_boundary64".to_string(), code, VERSION0); + let mut machine = create_aot_machine("write_at_boundary64".to_string(), &code, VERSION0); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -320,7 +320,7 @@ pub fn test_aot_version0_write_at_boundary() { #[cfg(has_aot)] pub fn test_aot_version1_argv_null() { let code = compile_aot_code("argv_null_test".to_string(), VERSION1); - let mut machine = create_aot_machine("argv_null_test".to_string(), code, VERSION1); + let mut machine = create_aot_machine("argv_null_test".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -330,7 +330,7 @@ pub fn test_aot_version1_argv_null() { #[cfg(has_aot)] pub fn test_aot_version1_sp_alignment() { let code = compile_aot_code("sp_alignment_test".to_string(), VERSION1); - let mut machine = create_aot_machine("sp_alignment_test".to_string(), code, VERSION1); + let mut machine = create_aot_machine("sp_alignment_test".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -340,7 +340,7 @@ pub fn test_aot_version1_sp_alignment() { #[cfg(has_aot)] pub fn test_aot_version1_jalr_bug() { let code = compile_aot_code("jalr_bug".to_string(), VERSION1); - let mut machine = create_aot_machine("jalr_bug".to_string(), code, VERSION1); + let mut machine = create_aot_machine("jalr_bug".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -350,7 +350,7 @@ pub fn test_aot_version1_jalr_bug() { #[cfg(has_aot)] pub fn test_aot_version1_jalr_bug_noc() { let code = compile_aot_code("jalr_bug_noc".to_string(), VERSION1); - let mut machine = create_aot_machine("jalr_bug_noc".to_string(), code, VERSION1); + let mut machine = create_aot_machine("jalr_bug_noc".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -360,7 +360,7 @@ pub fn test_aot_version1_jalr_bug_noc() { #[cfg(has_aot)] pub fn test_aot_version1_read_at_boundary() { let code = compile_aot_code("read_at_boundary64".to_string(), VERSION1); - let mut machine = create_aot_machine("read_at_boundary64".to_string(), code, VERSION1); + let mut machine = create_aot_machine("read_at_boundary64".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -370,7 +370,7 @@ pub fn test_aot_version1_read_at_boundary() { #[cfg(has_aot)] pub fn test_aot_version1_write_at_boundary() { let code = compile_aot_code("write_at_boundary64".to_string(), VERSION1); - let mut machine = create_aot_machine("write_at_boundary64".to_string(), code, VERSION1); + let mut machine = create_aot_machine("write_at_boundary64".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0); @@ -430,7 +430,7 @@ pub fn test_aot_version0_unaligned64() { .into(); let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value()); let core = DefaultMachineBuilder::>::new(asm_core).build(); - let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); + let mut machine = AsmMachine::new(core, Some(&code)); let result = machine.load_program(&buffer, &vec![program.into()]); assert!(result.is_err()); assert_eq!(result.err(), Some(Error::MemWriteOnExecutablePage)); @@ -440,7 +440,7 @@ pub fn test_aot_version0_unaligned64() { #[cfg(has_aot)] pub fn test_aot_version1_unaligned64() { let code = compile_aot_code("unaligned64".to_string(), VERSION1); - let mut machine = create_aot_machine("unaligned64".to_string(), code, VERSION1); + let mut machine = create_aot_machine("unaligned64".to_string(), &code, VERSION1); let result = machine.run(); assert!(result.is_ok()); assert_eq!(result.unwrap(), 0);