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

Introduce version and fix various bugs #99

Merged
Merged
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,7 +8,7 @@ cache:
env:
global:
- RUST_BACKTRACE=full
- TEST_SUITE_COMMIT=5d65d4b410c1f4dd74988d620e84f49eaea4c1a1
- TEST_SUITE_COMMIT=4fe8219308a28f8b31910bad5ae14a5acdb396a2

matrix:
include:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -43,3 +43,13 @@ make test
```

CKB VM has already included RISC-V binaries used in tests, so you don't need a RISC-V compiler to build binaries. However if you do want to play with your own binaries, a RISC-V compiler might be needed. [riscv-tools](https://github.com/riscv/riscv-tools) can be a good starting point here, or if you are an expert on GNU toolchain, you might also compile upstream GCC from source with RISC-V support, [here](./examples/is13.rs) is an example. CKB VM is using standard RISC-V instructions and ELF binary format, so theoretically any RISC-V compatible compilers are able to produce contracts used in CKB VM(tho bug reports are very welcome if you find breakage).

## Notes on Different Modes

Right now CKB VM has 3 different modes:

* Rust interpreter mode
* Assembly based interpreter mode(ASM mode)
* Ahead-of-time compilation mode(AOT mode)

For consistent behavior, you should only use ASM or AOT mode, and it's best if you stick with either ASM or AOT mode depending on your use case. The Rust mode is developed more to assist development, and never used in production by us. In case of bugs, there might be inconsistent behaviors between Rust mode and ASM/AOT mode.
6 changes: 3 additions & 3 deletions benches/vm_benchmark.rs
Expand Up @@ -3,7 +3,7 @@ extern crate criterion;

use bytes::Bytes;
#[cfg(has_asm)]
use ckb_vm::machine::{aot::AotCompilingMachine, asm::AsmMachine};
use ckb_vm::machine::{aot::AotCompilingMachine, asm::AsmMachine, VERSION0};
use ckb_vm::{run, SparseMemory};
use criterion::Criterion;
use std::fs::File;
Expand Down Expand Up @@ -61,7 +61,7 @@ fn aot_benchmark(c: &mut Criterion) {
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();
let mut aot_machine = AotCompilingMachine::load(&buffer.clone(), None).unwrap();
let mut aot_machine = AotCompilingMachine::load(&buffer.clone(), None, VERSION0).unwrap();
let result = aot_machine.compile().unwrap();

b.iter(|| {
Expand All @@ -82,7 +82,7 @@ fn aot_compiling_benchmark(c: &mut Criterion) {
let buffer = Bytes::from(buffer);

b.iter(|| {
AotCompilingMachine::load(&buffer.clone(), None)
AotCompilingMachine::load(&buffer.clone(), None, VERSION0)
.unwrap()
.compile()
.unwrap()
Expand Down
7 changes: 7 additions & 0 deletions definitions/src/asm.rs
Expand Up @@ -40,6 +40,7 @@ pub struct AsmCoreMachine {
pub running: u8,
pub cycles: u64,
pub max_cycles: u64,
pub version: u32,
pub flags: [u8; RISCV_PAGES],
pub memory: [u8; RISCV_MAX_MEMORY],
pub frames: [u8; MEMORY_FRAMES],
Expand All @@ -56,6 +57,12 @@ impl Default for Box<AsmCoreMachine> {
}

impl AsmCoreMachine {
pub fn new(version: u32, max_cycles: u64) -> Box<AsmCoreMachine> {
let mut machine = Self::new_with_max_cycles(max_cycles);
machine.version = version;
machine
}

pub fn new_with_max_cycles(max_cycles: u64) -> Box<AsmCoreMachine> {
let mut machine = unsafe {
let layout = Layout::new::<AsmCoreMachine>();
Expand Down