From b663302475e7d4cb8ef97074c376890fbe3e8015 Mon Sep 17 00:00:00 2001 From: mohanson Date: Tue, 12 Nov 2024 14:16:35 +0800 Subject: [PATCH] Staged --- tests/machine_build.rs | 39 +------------------------- tests/test_a_extension.rs | 57 +++++++++++++++++++++++++++++++------- tests/test_misc.rs | 16 +++++++++-- tests/test_resume.rs | 7 ++++- tests/test_signal_pause.rs | 33 ++++++++++++++++++---- 5 files changed, 95 insertions(+), 57 deletions(-) diff --git a/tests/machine_build.rs b/tests/machine_build.rs index 79a739c4..cb4fcc57 100644 --- a/tests/machine_build.rs +++ b/tests/machine_build.rs @@ -2,11 +2,10 @@ use bytes::Bytes; use ckb_vm::cost_model::constant_cycles; #[cfg(has_asm)] use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine}; -use ckb_vm::machine::{trace::TraceMachine, DefaultCoreMachine, VERSION2}; +use ckb_vm::machine::{trace::TraceMachine, DefaultCoreMachine}; use ckb_vm::registers::{A0, A7}; use ckb_vm::{ DefaultMachineBuilder, Error, Register, SparseMemory, SupportMachine, Syscalls, WXorXMemory, - ISA_A, ISA_B, ISA_IMC, }; pub struct SleepSyscall {} @@ -29,42 +28,6 @@ impl Syscalls for SleepSyscall { } } -#[cfg(has_asm)] -pub fn asm_v2_imacb(path: &str) -> AsmMachine { - let buffer: Bytes = std::fs::read(path).unwrap().into(); - let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_A | ISA_B, VERSION2, u64::MAX); - let core = DefaultMachineBuilder::>::new(asm_core) - .instruction_cycle_func(Box::new(constant_cycles)) - .syscall(Box::new(SleepSyscall {})) - .build(); - let mut machine = AsmMachine::new(core); - machine - .load_program(&buffer, &vec![Bytes::from("main")]) - .unwrap(); - machine -} - -pub fn int_v2_imacb( - path: &str, -) -> TraceMachine>>> { - let buffer: Bytes = std::fs::read(path).unwrap().into(); - let core_machine = DefaultCoreMachine::>>::new( - ISA_IMC | ISA_A | ISA_B, - VERSION2, - u64::MAX, - ); - let mut machine = TraceMachine::new( - DefaultMachineBuilder::new(core_machine) - .instruction_cycle_func(Box::new(constant_cycles)) - .syscall(Box::new(SleepSyscall {})) - .build(), - ); - machine - .load_program(&buffer, &vec![Bytes::from("main")]) - .unwrap(); - machine -} - #[cfg(has_asm)] pub fn asm(path: &str, args: Vec, version: u32, isa: u8) -> AsmMachine { let buffer: Bytes = std::fs::read(path).unwrap().into(); diff --git a/tests/test_a_extension.rs b/tests/test_a_extension.rs index 6c3c0a34..9ea49d2e 100644 --- a/tests/test_a_extension.rs +++ b/tests/test_a_extension.rs @@ -1,18 +1,28 @@ -use ckb_vm::Error; +use ckb_vm::{machine::VERSION2, Error, ISA_A, ISA_IMC}; #[cfg(has_asm)] use ckb_vm::{CoreMachine, Memory}; pub mod machine_build; #[test] pub fn test_write_permission_bug() { - let mut machine = machine_build::int_v2_imacb("tests/programs/amo_write_permission"); + let mut machine = machine_build::int( + "tests/programs/amo_write_permission", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let ret = machine.run(); assert!(ret.is_err()); assert_eq!(ret.err(), Some(Error::MemWriteOnExecutablePage(16))); #[cfg(has_asm)] { - let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/amo_write_permission"); + let mut machine_asm = machine_build::asm( + "tests/programs/amo_write_permission", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let ret_asm = machine_asm.run(); assert!(ret_asm.is_err()); assert_eq!(ret_asm.err(), Some(Error::MemWriteOnExecutablePage(16))); @@ -21,14 +31,24 @@ pub fn test_write_permission_bug() { #[test] pub fn test_sc_after_sc() { - let mut machine = machine_build::int_v2_imacb("tests/programs/sc_after_sc"); + let mut machine = machine_build::int( + "tests/programs/sc_after_sc", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let ret = machine.run(); assert!(ret.is_ok()); assert_eq!(ret.unwrap(), 0); #[cfg(has_asm)] { - let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/sc_after_sc"); + let mut machine_asm = machine_build::asm( + "tests/programs/sc_after_sc", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let ret_asm = machine_asm.run(); assert!(ret_asm.is_ok()); assert_eq!(ret_asm.unwrap(), 0); @@ -37,14 +57,16 @@ pub fn test_sc_after_sc() { #[test] pub fn test_sc_only() { - let mut machine = machine_build::int_v2_imacb("tests/programs/sc_only"); + let mut machine = + machine_build::int("tests/programs/sc_only", vec![], VERSION2, ISA_IMC | ISA_A); let ret = machine.run(); assert!(ret.is_ok()); assert_eq!(ret.unwrap(), 0); #[cfg(has_asm)] { - let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/sc_only"); + let mut machine_asm = + machine_build::asm("tests/programs/sc_only", vec![], VERSION2, ISA_IMC | ISA_A); let ret_asm = machine_asm.run(); assert!(ret_asm.is_ok()); assert_eq!(ret_asm.unwrap(), 0); @@ -53,14 +75,24 @@ pub fn test_sc_only() { #[test] pub fn test_amo_compare() { - let mut machine = machine_build::int_v2_imacb("tests/programs/amo_compare"); + let mut machine = machine_build::int( + "tests/programs/amo_compare", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let ret = machine.run(); assert!(ret.is_ok()); assert_eq!(ret.unwrap(), 0); #[cfg(has_asm)] { - let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/amo_compare"); + let mut machine_asm = machine_build::asm( + "tests/programs/amo_compare", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let ret_asm = machine_asm.run(); assert!(ret_asm.is_ok()); assert_eq!(ret_asm.unwrap(), 0); @@ -71,7 +103,12 @@ pub fn test_amo_compare() { pub fn test_amo_check_write() { #[cfg(has_asm)] { - let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/amo_check_write"); + let mut machine_asm = machine_build::asm( + "tests/programs/amo_check_write", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); let page_a = 0; let page_b = 17; let flag_a = machine_asm.machine.memory_mut().fetch_flag(page_a).unwrap(); diff --git a/tests/test_misc.rs b/tests/test_misc.rs index 19e195ad..bfc855ab 100644 --- a/tests/test_misc.rs +++ b/tests/test_misc.rs @@ -1,6 +1,6 @@ use ckb_vm::cost_model::constant_cycles; use ckb_vm::error::OutOfBoundKind; -use ckb_vm::machine::{VERSION0, VERSION1}; +use ckb_vm::machine::{VERSION0, VERSION1, VERSION2}; use ckb_vm::registers::{A0, A1, A2, A3, A4, A5, A7}; use ckb_vm::{ run, CoreMachine, Debugger, DefaultCoreMachine, DefaultMachineBuilder, Error, FlatMemory, @@ -458,14 +458,24 @@ pub fn test_clang() { } { - let mut machine = machine_build::int_v2_imacb("tests/programs/clang_sample"); + let mut machine = machine_build::int( + "tests/programs/clang_sample", + vec![], + VERSION2, + ISA_IMC | ISA_B, + ); let ret = machine.run(); assert!(ret.is_ok()); } #[cfg(has_asm)] { - let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/clang_sample"); + let mut machine_asm = machine_build::asm( + "tests/programs/clang_sample", + vec![], + VERSION2, + ISA_IMC | ISA_B, + ); let ret_asm = machine_asm.run(); assert!(ret_asm.is_ok()); } diff --git a/tests/test_resume.rs b/tests/test_resume.rs index 2abf7732..b42c95dd 100644 --- a/tests/test_resume.rs +++ b/tests/test_resume.rs @@ -299,7 +299,12 @@ impl Machine { #[test] pub fn test_sc_after_snapshot() { - let mut machine = machine_build::int_v2_imacb("tests/programs/sc_after_snapshot"); + let mut machine = machine_build::int( + "tests/programs/sc_after_snapshot", + vec![], + VERSION2, + ISA_IMC | ISA_A, + ); machine.machine.set_max_cycles(5); let ret = machine.run(); assert!(ret.is_err()); diff --git a/tests/test_signal_pause.rs b/tests/test_signal_pause.rs index d792568e..ff87059c 100644 --- a/tests/test_signal_pause.rs +++ b/tests/test_signal_pause.rs @@ -1,4 +1,5 @@ -use ckb_vm::{Error, SupportMachine}; +use ckb_vm::machine::VERSION2; +use ckb_vm::{Error, SupportMachine, ISA_B, ISA_IMC}; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::Arc; pub mod machine_build; @@ -6,13 +7,25 @@ pub mod machine_build; #[cfg(has_asm)] #[test] pub fn test_asm_pause() { + use ckb_vm::machine::VERSION2; + let expect_cycles = { - let mut machine = machine_build::asm_v2_imacb("tests/programs/pause_resume"); + let mut machine = machine_build::asm( + "tests/programs/pause_resume", + vec![], + VERSION2, + ISA_IMC | ISA_B, + ); machine.run().unwrap(); machine.machine.cycles() }; - let mut machine = machine_build::asm_v2_imacb("tests/programs/pause_resume"); + let mut machine = machine_build::asm( + "tests/programs/pause_resume", + vec![], + VERSION2, + ISA_IMC | ISA_B, + ); let branch_pause_cnt = Arc::new(AtomicU32::new(0)); let branch_pause_cnt_jh = branch_pause_cnt.clone(); @@ -40,12 +53,22 @@ pub fn test_asm_pause() { #[test] pub fn test_int_pause() { let expect_cycles = { - let mut machine = machine_build::int_v2_imacb("tests/programs/pause_resume"); + let mut machine = machine_build::int( + "tests/programs/pause_resume", + vec![], + VERSION2, + ISA_IMC | ISA_B, + ); machine.run().unwrap(); machine.machine.cycles() }; - let mut machine = machine_build::int_v2_imacb("tests/programs/pause_resume"); + let mut machine = machine_build::int( + "tests/programs/pause_resume", + vec![], + VERSION2, + ISA_IMC | ISA_B, + ); let branch_pause_cnt = Arc::new(AtomicU32::new(0)); let branch_pause_cnt_jh = branch_pause_cnt.clone(); let signal = machine.machine.pause();