diff --git a/azure-pipelines.yml b/azure-pipelines.yml index cda3e725..2758bedc 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -8,7 +8,7 @@ trigger: variables: RUST_BACKTRACE: full - TEST_SUITE_COMMIT: ff49a1dfb3c12be9dc18073133d660f0dd176662 + TEST_SUITE_COMMIT: a1fb578bf802ce1155565d509402e7e00a1c280b jobs: - job: WinCI diff --git a/benches/vm_benchmark.rs b/benches/vm_benchmark.rs index 54ce4dd5..3638ccb1 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 = aot_machine.compile().unwrap(); + let result = std::sync::Arc::new(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)); + let mut machine = AsmMachine::new(core, Some(result.clone())); machine.load_program(&buffer, &args[..]).unwrap(); machine.run().unwrap() }); diff --git a/examples/ckb-vm-runner.rs b/examples/ckb-vm-runner.rs index 7c308b3d..7a404a59 100644 --- a/examples/ckb-vm-runner.rs +++ b/examples/ckb-vm-runner.rs @@ -105,7 +105,8 @@ fn main_aot(code: Bytes, args: Vec) -> Result<(), Box { type REG = u64; @@ -433,9 +432,9 @@ impl AotCode { } } -pub struct AsmMachine<'a> { - pub machine: DefaultMachine<'a, Box>, - pub aot_code: Option<&'a AotCode>, +pub struct AsmMachine { + pub machine: DefaultMachine>, + pub aot_code: Option>, } extern "C" { @@ -445,10 +444,10 @@ extern "C" { fn ckb_vm_asm_labels(); } -impl<'a> AsmMachine<'a> { +impl AsmMachine { pub fn new( - machine: DefaultMachine<'a, Box>, - aot_code: Option<&'a AotCode>, + machine: DefaultMachine>, + aot_code: Option>, ) -> Self { Self { machine, aot_code } } diff --git a/src/machine/mod.rs b/src/machine/mod.rs index ed2dd932..d9e4fe51 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -409,7 +409,7 @@ impl DefaultCoreMachine { pub type InstructionCycleFunc = dyn Fn(Instruction) -> u64; #[derive(Default)] -pub struct DefaultMachine<'a, Inner> { +pub struct DefaultMachine { inner: Inner, // We have run benchmarks on secp256k1 verification, the performance @@ -417,12 +417,12 @@ pub struct DefaultMachine<'a, Inner> { // with Box solution for simplicity now. Later if this becomes an issue, // we can change to static dispatch. instruction_cycle_func: Option>, - debugger: Option + 'a>>, - syscalls: Vec + 'a>>, + debugger: Option>>, + syscalls: Vec>>, exit_code: i8, } -impl CoreMachine for DefaultMachine<'_, Inner> { +impl CoreMachine for DefaultMachine { type REG = ::REG; type MEM = ::MEM; @@ -463,7 +463,7 @@ impl CoreMachine for DefaultMachine<'_, Inner> { } } -impl SupportMachine for DefaultMachine<'_, Inner> { +impl SupportMachine for DefaultMachine { fn cycles(&self) -> u64 { self.inner.cycles() } @@ -498,7 +498,7 @@ impl SupportMachine for DefaultMachine<'_, Inner> { } } -impl Machine for DefaultMachine<'_, Inner> { +impl Machine for DefaultMachine { fn ecall(&mut self) -> Result<(), Error> { let code = self.registers()[A7].to_u64(); match code { @@ -534,7 +534,7 @@ impl Machine for DefaultMachine<'_, Inner> { } } -impl Display for DefaultMachine<'_, Inner> { +impl Display for DefaultMachine { 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() { @@ -549,7 +549,7 @@ impl Display for DefaultMachine<'_, Inner> { } } -impl<'a, Inner: SupportMachine> DefaultMachine<'a, Inner> { +impl DefaultMachine { 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 { @@ -627,14 +627,14 @@ impl<'a, Inner: SupportMachine> DefaultMachine<'a, Inner> { } #[derive(Default)] -pub struct DefaultMachineBuilder<'a, Inner> { +pub struct DefaultMachineBuilder { inner: Inner, instruction_cycle_func: Option>, - debugger: Option + 'a>>, - syscalls: Vec + 'a>>, + debugger: Option>>, + syscalls: Vec>>, } -impl<'a, Inner> DefaultMachineBuilder<'a, Inner> { +impl DefaultMachineBuilder { pub fn new(inner: Inner) -> Self { Self { inner, @@ -652,17 +652,17 @@ impl<'a, Inner> DefaultMachineBuilder<'a, Inner> { self } - pub fn syscall(mut self, syscall: Box + 'a>) -> Self { + pub fn syscall(mut self, syscall: Box>) -> Self { self.syscalls.push(syscall); self } - pub fn debugger(mut self, debugger: Box + 'a>) -> Self { + pub fn debugger(mut self, debugger: Box>) -> Self { self.debugger = Some(debugger); self } - pub fn build(self) -> DefaultMachine<'a, Inner> { + pub fn build(self) -> DefaultMachine { DefaultMachine { inner: self.inner, instruction_cycle_func: self.instruction_cycle_func, diff --git a/src/machine/trace.rs b/src/machine/trace.rs index aede00f9..51bf69a9 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<'a, Inner> { - pub machine: DefaultMachine<'a, Inner>, +pub struct TraceMachine { + pub machine: DefaultMachine, traces: Vec, } -impl CoreMachine for TraceMachine<'_, Inner> { +impl CoreMachine for TraceMachine { type REG = ::REG; type MEM = ::MEM; @@ -79,7 +79,7 @@ impl CoreMachine for TraceMachine<'_, Inner> { } } -impl Machine for TraceMachine<'_, Inner> { +impl Machine for TraceMachine { fn ecall(&mut self) -> Result<(), Error> { self.machine.ecall() } @@ -89,8 +89,8 @@ impl Machine for TraceMachine<'_, Inner> { } } -impl<'a, Inner: SupportMachine> TraceMachine<'a, Inner> { - pub fn new(machine: DefaultMachine<'a, Inner>) -> Self { +impl TraceMachine { + pub fn new(machine: DefaultMachine) -> Self { Self { machine, traces: vec![], diff --git a/tests/machine_build.rs b/tests/machine_build.rs index d50fd78d..047d6e3b 100644 --- a/tests/machine_build.rs +++ b/tests/machine_build.rs @@ -3,18 +3,17 @@ 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<'a>(path: &str) -> AsmMachine<'a> { +pub fn asm_v1_imcb(path: &str) -> AsmMachine { 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) @@ -36,14 +35,14 @@ pub fn aot_v1_imcb_code(path: &str) -> AotCode { } #[cfg(has_aot)] -pub fn aot_v1_imcb<'a>(path: &str, code: &'a AotCode) -> AsmMachine<'a> { +pub fn aot_v1_imcb(path: &str, code: AotCode) -> AsmMachine { 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(code)); + let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); machine .load_program(&buffer, &vec![Bytes::from("main")]) .unwrap(); @@ -71,7 +70,7 @@ pub fn int_v1_imcb( } #[cfg(has_asm)] -pub fn asm_v1_mop<'a>(path: &str, args: Vec) -> AsmMachine<'a> { +pub fn asm_v1_mop(path: &str, args: Vec) -> AsmMachine { 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) @@ -93,7 +92,7 @@ pub fn aot_v1_mop_code(path: &str) -> AotCode { } #[cfg(has_aot)] -pub fn aot_v1_mop<'a>(path: &str, args: Vec, code: &'a AotCode) -> AsmMachine<'a> { +pub fn aot_v1_mop(path: &str, args: Vec, code: AotCode) -> AsmMachine { let buffer: Bytes = std::fs::read(path).unwrap().into(); let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, VERSION1, u64::max_value()); @@ -102,7 +101,7 @@ pub fn aot_v1_mop<'a>(path: &str, args: Vec, code: &'a AotCode) -> AsmMac .build(); let mut argv = vec![Bytes::from("main")]; argv.extend_from_slice(&args); - let mut machine = AsmMachine::new(core, Some(code)); + let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); machine.load_program(&buffer, &argv).unwrap(); machine } diff --git a/tests/test_aot.rs b/tests/test_aot.rs index f1b15075..c06d1716 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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code1)); + let mut machine1 = AsmMachine::new(core1, Some(Arc::new(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(&code2)); + let mut machine2 = AsmMachine::new(core2, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(Arc::new(code))); machine .load_program(&buffer, &vec!["simple64".into()]) .unwrap(); diff --git a/tests/test_b_extension.rs b/tests/test_b_extension.rs index 8d90df68..d691555c 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 caef47b3..a0204869 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 0fa7366e..64e3dfa8 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(&code)); + let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code))); machine.load_program(&buffer, &vec![]).unwrap(); let result = machine.run(); diff --git a/tests/test_resume.rs b/tests/test_resume.rs index eed299e0..0ce4b89c 100644 --- a/tests/test_resume.rs +++ b/tests/test_resume.rs @@ -199,7 +199,8 @@ 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(&code)); + let mut machine1 = + AotMachine::build(version, except_cycles - 30, Some(std::sync::Arc::new(code))); machine1 .load_program(&buffer, &vec!["alloc_many".into()]) .unwrap(); @@ -236,7 +237,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(&code)); + let mut machine2 = AotMachine::build(version, 40, Some(std::sync::Arc::new(code))); machine2.resume(&snapshot).unwrap(); let result2 = machine2.run(); let cycles2 = machine2.cycles(); @@ -281,7 +282,7 @@ enum MachineTy { } impl MachineTy { - fn build<'a>(self, version: u32, max_cycles: u64) -> Machine { + fn build(self, version: u32, max_cycles: u64) -> Machine { match self { MachineTy::Asm => { let asm_core1 = AsmCoreMachine::new(ISA_IMC, version, max_cycles); @@ -321,11 +322,9 @@ impl MachineTy { } enum Machine { - Asm(AsmMachine<'static>), - Interpreter(DefaultMachine<'static, DefaultCoreMachine>>>), - InterpreterWithTrace( - TraceMachine<'static, DefaultCoreMachine>>>, - ), + Asm(AsmMachine), + Interpreter(DefaultMachine>>>), + InterpreterWithTrace(TraceMachine>>>), } impl Machine { @@ -381,11 +380,15 @@ impl Machine { } #[cfg(has_aot)] -struct AotMachine<'a>(AsmMachine<'a>); +struct AotMachine(AsmMachine); #[cfg(has_aot)] -impl<'a> AotMachine<'a> { - fn build(version: u32, max_cycles: u64, program: Option<&'a AotCode>) -> AotMachine<'a> { +impl AotMachine { + fn build( + version: u32, + max_cycles: u64, + program: Option>, + ) -> AotMachine { 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 9d678b02..f5d5ba9f 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<'a>( +fn create_rust_machine( program: String, version: u32, -) -> DefaultMachine<'a, DefaultCoreMachine> { +) -> DefaultMachine> { 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<'a>( machine } -fn create_asm_machine<'a>(program: String, version: u32) -> AsmMachine<'a> { +fn create_asm_machine(program: String, version: u32) -> AsmMachine { 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<'a>(program: String, code: &'a AotCode, version: u32) -> AsmMachine<'a> { +fn create_aot_machine(program: String, code: AotCode, version: u32) -> AsmMachine { 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(code)); + let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(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(&code)); + let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(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);