Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A is no longer enabled by default #443

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
ln -snf .. ckb-vm-test-suite/ckb-vm
docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv
cd ckb-vm-test-suite
git checkout 86480364649c9cb6ac01674fe51156e7cf50a31a
git checkout 2ef749d0f580958043264a798e560fe25fec0c6e
git submodule update --init --recursive
RISCV=`pwd`/../riscv ./test.sh

Expand Down Expand Up @@ -136,7 +136,7 @@ jobs:
ln -snf .. ckb-vm-test-suite/ckb-vm
docker run --rm -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20210804 cp -r /riscv /code/riscv
cd ckb-vm-test-suite
git checkout 86480364649c9cb6ac01674fe51156e7cf50a31a
git checkout 2ef749d0f580958043264a798e560fe25fec0c6e
git submodule update --init --recursive
RISCV=`pwd`/../riscv ./test.sh --build-only
cd ..
Expand Down
1 change: 1 addition & 0 deletions definitions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ pub const DEFAULT_MEMORY_SIZE: usize = 4 << 20; // 4 MB
pub const ISA_IMC: u8 = 0b0000_0000;
pub const ISA_B: u8 = 0b0000_0001;
pub const ISA_MOP: u8 = 0b0000_0010;
// Extension ISA_A is not enabled in ckb 2nd hardfork(aka, meepo hardfork)
pub const ISA_A: u8 = 0b0000_0100;
4 changes: 2 additions & 2 deletions examples/ckb-vm-runner.rs → examples/ckb_vm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<Mac: SupportMachine> Syscalls<Mac> for DebugSyscall {
#[cfg(has_asm)]
fn main_asm(code: Bytes, args: Vec<Bytes>) -> Result<(), Box<dyn std::error::Error>> {
let asm_core = ckb_vm::machine::asm::AsmCoreMachine::new(
ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP | ckb_vm::ISA_A,
ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP,
ckb_vm::machine::VERSION2,
u64::MAX,
);
Expand All @@ -64,7 +64,7 @@ fn main_asm(code: Bytes, args: Vec<Bytes>) -> Result<(), Box<dyn std::error::Err
#[cfg(not(has_asm))]
fn main_int(code: Bytes, args: Vec<Bytes>) -> Result<(), Box<dyn std::error::Error>> {
let core_machine = ckb_vm::DefaultCoreMachine::<u64, ckb_vm::SparseMemory<u64>>::new(
ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP | ckb_vm::ISA_A,
ckb_vm::ISA_IMC | ckb_vm::ISA_B | ckb_vm::ISA_MOP,
ckb_vm::machine::VERSION2,
u64::MAX,
);
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn run<R: Register, M: Memory<REG = R> + Default>(
args: &[Bytes],
) -> Result<i8, Error> {
let core_machine = DefaultCoreMachine::<R, WXorXMemory<M>>::new_with_memory(
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
ISA_IMC | ISA_B | ISA_MOP,
machine::VERSION2,
u64::MAX,
WXorXMemory::new(M::default()),
Expand All @@ -57,7 +57,7 @@ pub fn run_with_memory<R: Register, M: Memory<REG = R>>(
memory: M,
) -> Result<i8, Error> {
let core_machine = DefaultCoreMachine::<R, WXorXMemory<M>>::new_with_memory(
ISA_IMC | ISA_A | ISA_B | ISA_MOP,
ISA_IMC | ISA_B | ISA_MOP,
machine::VERSION2,
u64::MAX,
WXorXMemory::new(memory),
Expand Down
101 changes: 7 additions & 94 deletions tests/machine_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, VERSION1, 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, ISA_MOP,
};

pub struct SleepSyscall {}
Expand All @@ -30,50 +29,9 @@ impl<Mac: SupportMachine> Syscalls<Mac> for SleepSyscall {
}

#[cfg(has_asm)]
pub fn asm_v1_imcb(path: &str) -> AsmMachine {
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_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();
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, version, u64::MAX);
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 {}))
Expand All @@ -85,24 +43,15 @@ pub fn asm_mop(path: &str, args: Vec<Bytes>, version: u32) -> AsmMachine {
machine
}

pub fn int_v1_mop(
path: &str,
args: Vec<Bytes>,
) -> TraceMachine<DefaultCoreMachine<u64, WXorXMemory<SparseMemory<u64>>>> {
int_mop(path, args, VERSION1)
}

pub fn int_mop(
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_IMC | ISA_B | ISA_MOP,
version,
u64::MAX,
);
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))
Expand All @@ -114,39 +63,3 @@ pub fn int_mop(
machine.load_program(&buffer, &argv).unwrap();
machine
}

#[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::<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_v2_imacb(
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_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
}
57 changes: 47 additions & 10 deletions tests/test_a_extension.rs
Original file line number Diff line number Diff line change
@@ -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)));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Loading
Loading