ckb_vm/
lib.rs

1#[macro_use]
2extern crate derive_more;
3
4pub mod bits;
5pub mod cost_model;
6pub mod debugger;
7pub mod decoder;
8pub mod elf;
9pub mod error;
10pub mod instructions;
11pub mod machine;
12pub mod memory;
13pub mod snapshot;
14pub mod snapshot2;
15pub mod syscalls;
16
17pub use bytes;
18pub use ckb_vm_definitions;
19
20pub use crate::{
21    debugger::Debugger,
22    instructions::{Instruction, Register},
23    machine::{
24        trace::TraceMachine, CoreMachine, DefaultCoreMachine, DefaultMachine,
25        DefaultMachineBuilder, DefaultMachineRunner, FlattenedArgsReader, InstructionCycleFunc,
26        Machine, SupportMachine,
27    },
28    memory::{flat::FlatMemory, sparse::SparseMemory, wxorx::WXorXMemory, Memory},
29    syscalls::Syscalls,
30};
31pub use bytes::Bytes;
32
33pub use ckb_vm_definitions::{
34    registers, DEFAULT_STACK_SIZE, ISA_A, ISA_B, ISA_IMC, ISA_MOP, MEMORY_FRAMES, MEMORY_FRAMESIZE,
35    MEMORY_FRAME_SHIFTS, RISCV_GENERAL_REGISTER_NUMBER, RISCV_MAX_MEMORY, RISCV_PAGES,
36    RISCV_PAGESIZE, RISCV_PAGE_SHIFTS,
37};
38
39pub use error::Error;
40
41pub fn run<R: Register, M: Memory<REG = R>>(
42    program: &Bytes,
43    args: &[Bytes],
44    memory_size: usize,
45) -> Result<i8, Error> {
46    let core_machine = DefaultCoreMachine::<R, WXorXMemory<M>>::new_with_memory(
47        ISA_IMC | ISA_A | ISA_B | ISA_MOP,
48        machine::VERSION2,
49        u64::MAX,
50        memory_size,
51    );
52    let mut machine = TraceMachine::new(DefaultMachineBuilder::new(core_machine).build());
53    machine.load_program(program, args.iter().map(|e| Ok(e.clone())))?;
54    machine.run()
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_max_memory_must_be_multiple_of_pages() {
63        assert_eq!(RISCV_MAX_MEMORY % RISCV_PAGESIZE, 0);
64    }
65
66    #[test]
67    fn test_page_size_be_power_of_2() {
68        assert!(RISCV_PAGESIZE.is_power_of_two());
69    }
70}