Skip to content

Commit

Permalink
Staged
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Nov 12, 2024
1 parent b0e4833 commit 300e54f
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 97 deletions.
77 changes: 36 additions & 41 deletions tests/machine_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,6 @@ impl<Mac: SupportMachine> Syscalls<Mac> for SleepSyscall {
}
}

#[cfg(has_asm)]
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);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::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_v1_imcb(
path: &str,
) -> TraceMachine<DefaultCoreMachine<u64, WXorXMemory<SparseMemory<u64>>>> {
let buffer: Bytes = std::fs::read(path).unwrap().into();
let core_machine = DefaultCoreMachine::<u64, WXorXMemory<SparseMemory<u64>>>::new(
ISA_IMC | ISA_B,
VERSION1,
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_v1_mop(path: &str, args: Vec<Bytes>) -> AsmMachine {
asm_mop(path, args, VERSION1)
}

#[cfg(has_asm)]
pub fn asm_mop(path: &str, args: Vec<Bytes>, version: u32) -> AsmMachine {
let buffer: Bytes = std::fs::read(path).unwrap().into();
Expand Down Expand Up @@ -150,3 +109,39 @@ pub fn int_v2_imacb(
.unwrap();
machine
}

#[cfg(has_asm)]
pub fn asm(path: &str, args: Vec<Bytes>, version: u32, isa: u8) -> AsmMachine {
let buffer: Bytes = std::fs::read(path).unwrap().into();
let asm_core = AsmCoreMachine::new(isa, version, u64::MAX);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.instruction_cycle_func(Box::new(constant_cycles))
.syscall(Box::new(SleepSyscall {}))
.build();
let mut machine = AsmMachine::new(core);
let mut argv = vec![Bytes::from("main")];
argv.extend_from_slice(&args);
machine.load_program(&buffer, &argv).unwrap();
machine
}

pub fn int(
path: &str,
args: Vec<Bytes>,
version: u32,
isa: u8,
) -> TraceMachine<DefaultCoreMachine<u64, WXorXMemory<SparseMemory<u64>>>> {
let buffer: Bytes = std::fs::read(path).unwrap().into();
let core_machine =
DefaultCoreMachine::<u64, WXorXMemory<SparseMemory<u64>>>::new(isa, version, u64::MAX);
let mut machine = TraceMachine::new(
DefaultMachineBuilder::new(core_machine)
.instruction_cycle_func(Box::new(constant_cycles))
.syscall(Box::new(SleepSyscall {}))
.build(),
);
let mut argv = vec![Bytes::from("main")];
argv.extend_from_slice(&args);
machine.load_program(&buffer, &argv).unwrap();
machine
}
61 changes: 49 additions & 12 deletions tests/test_b_extension.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use ckb_vm::{machine::VERSION1, ISA_B, ISA_IMC};

pub mod machine_build;

#[test]
pub fn test_clzw_bug() {
let mut machine = machine_build::int_v1_imcb("tests/programs/clzw_bug");
let mut machine =
machine_build::int("tests/programs/clzw_bug", vec![], VERSION1, ISA_IMC | ISA_B);
let ret = machine.run();
assert!(ret.is_ok());
assert_eq!(ret.unwrap(), 0);

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/clzw_bug");
let mut machine_asm =
machine_build::asm("tests/programs/clzw_bug", vec![], VERSION1, ISA_IMC | ISA_B);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
assert_eq!(ret_asm.unwrap(), 0);
Expand All @@ -18,14 +22,24 @@ pub fn test_clzw_bug() {

#[test]
pub fn test_sbinvi_aot_load_imm_bug() {
let mut machine = machine_build::int_v1_imcb("tests/programs/sbinvi_aot_load_imm_bug");
let mut machine = machine_build::int(
"tests/programs/sbinvi_aot_load_imm_bug",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret = machine.run();
assert!(ret.is_ok());
assert_eq!(ret.unwrap(), 0);

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/sbinvi_aot_load_imm_bug");
let mut machine_asm = machine_build::asm(
"tests/programs/sbinvi_aot_load_imm_bug",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
assert_eq!(ret_asm.unwrap(), 0);
Expand All @@ -35,14 +49,24 @@ pub fn test_sbinvi_aot_load_imm_bug() {
#[test]
pub fn test_rorw_in_end_of_aot_block() {
// The 1024th instruction will use one more temporary register than normal.
let mut machine = machine_build::int_v1_imcb("tests/programs/rorw_in_end_of_aot_block");
let mut machine = machine_build::int(
"tests/programs/rorw_in_end_of_aot_block",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret = machine.run();
assert!(ret.is_ok());
assert_eq!(ret.unwrap(), 0);

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/rorw_in_end_of_aot_block");
let mut machine_asm = machine_build::asm(
"tests/programs/rorw_in_end_of_aot_block",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
assert_eq!(ret_asm.unwrap(), 0);
Expand All @@ -51,14 +75,15 @@ pub fn test_rorw_in_end_of_aot_block() {

#[test]
pub fn test_pcnt() {
let mut machine = machine_build::int_v1_imcb("tests/programs/pcnt");
let mut machine = machine_build::int("tests/programs/pcnt", vec![], VERSION1, ISA_IMC | ISA_B);
let ret = machine.run();
assert!(ret.is_ok());
assert_eq!(ret.unwrap(), 0);

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/pcnt");
let mut machine_asm =
machine_build::asm("tests/programs/pcnt", vec![], VERSION1, ISA_IMC | ISA_B);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
assert_eq!(ret_asm.unwrap(), 0);
Expand All @@ -67,14 +92,24 @@ pub fn test_pcnt() {

#[test]
pub fn test_clmul_bug() {
let mut machine = machine_build::int_v1_imcb("tests/programs/clmul_bug");
let mut machine = machine_build::int(
"tests/programs/clmul_bug",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret = machine.run();
assert!(ret.is_ok());
assert_eq!(ret.unwrap(), 0);

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/clmul_bug");
let mut machine_asm = machine_build::asm(
"tests/programs/clmul_bug",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
assert_eq!(ret_asm.unwrap(), 0);
Expand All @@ -83,14 +118,16 @@ pub fn test_clmul_bug() {

#[test]
pub fn test_orc_bug() {
let mut machine = machine_build::int_v1_imcb("tests/programs/orc_bug");
let mut machine =
machine_build::int("tests/programs/orc_bug", vec![], VERSION1, ISA_IMC | ISA_B);
let ret = machine.run();
assert!(ret.is_ok());
assert_eq!(ret.unwrap(), 0);

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/orc_bug");
let mut machine_asm =
machine_build::asm("tests/programs/orc_bug", vec![], VERSION1, ISA_IMC | ISA_B);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
assert_eq!(ret_asm.unwrap(), 0);
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dy_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ckb_vm::{
asm::{AsmCoreMachine, AsmMachine},
DefaultMachineBuilder, VERSION0, VERSION2,
},
ISA_A, ISA_B, ISA_IMC, ISA_MOP,
ISA_B, ISA_IMC, ISA_MOP,
};
use std::fs;

Expand Down Expand Up @@ -79,7 +79,7 @@ fn test_memory_out_of_bounds() {
#[cfg(has_asm)]
{
let asm_core = AsmCoreMachine::new_with_memory(
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
ISA_IMC | ISA_B | ISA_MOP,
VERSION2,
u64::MAX,
memory_size,
Expand Down
18 changes: 14 additions & 4 deletions tests/test_misc.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use ckb_vm::cost_model::constant_cycles;
use ckb_vm::error::OutOfBoundKind;
use ckb_vm::machine::VERSION0;
use ckb_vm::machine::{VERSION0, VERSION1};
use ckb_vm::registers::{A0, A1, A2, A3, A4, A5, A7};
use ckb_vm::{
run, CoreMachine, Debugger, DefaultCoreMachine, DefaultMachineBuilder, Error, FlatMemory,
Memory, Register, SparseMemory, SupportMachine, Syscalls, WXorXMemory, DEFAULT_MEMORY_SIZE,
ISA_IMC, RISCV_PAGESIZE,
ISA_B, ISA_IMC, RISCV_PAGESIZE,
};
#[cfg(has_asm)]
use ckb_vm_definitions::asm::AsmCoreMachine;
Expand Down Expand Up @@ -435,14 +435,24 @@ pub fn test_outofcycles_in_syscall() {
#[test]
pub fn test_clang() {
{
let mut machine = machine_build::int_v1_imcb("tests/programs/clang_sample");
let mut machine = machine_build::int(
"tests/programs/clang_sample",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret = machine.run();
assert!(ret.is_ok());
}

#[cfg(has_asm)]
{
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/clang_sample");
let mut machine_asm = machine_build::asm(
"tests/programs/clang_sample",
vec![],
VERSION1,
ISA_IMC | ISA_B,
);
let ret_asm = machine_asm.run();
assert!(ret_asm.is_ok());
}
Expand Down
Loading

0 comments on commit 300e54f

Please sign in to comment.