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

Implement FlattenedArgsReader #450

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 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 2ef749d0f580958043264a798e560fe25fec0c6e
git checkout 898edc351eeb4de974ca4f0ff8d1e4943a95aecb
git submodule update --init --recursive
RISCV=`pwd`/../riscv ./test.sh

Expand Down Expand Up @@ -136,13 +136,13 @@ 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 2ef749d0f580958043264a798e560fe25fec0c6e
git checkout 898edc351eeb4de974ca4f0ff8d1e4943a95aecb
git submodule update --init --recursive
RISCV=`pwd`/../riscv ./test.sh --build-only
cd ..
- name: Run test suite
run: |
sudo apt install -y qemu binfmt-support qemu-user-static
sudo apt install -y qemu-system binfmt-support qemu-user-static
sudo apt install -y gcc-multilib
sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu clang
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
Expand Down
74 changes: 41 additions & 33 deletions benches/vm_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ use std::fs;
fn interpret_benchmark(c: &mut Criterion) {
c.bench_function("interpret secp256k1_bench", |b| {
let buffer = fs::read("benches/data/secp256k1_bench").unwrap().into();
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();

let args: Vec<Bytes> = vec![
"secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"
].into_iter().map(|a| a.into()).collect();
b.iter(|| run::<u64, SparseMemory<u64>>(&buffer, &args[..]).unwrap());
});
}
Expand All @@ -35,17 +36,18 @@ fn interpret_benchmark(c: &mut Criterion) {
fn asm_benchmark(c: &mut Criterion) {
c.bench_function("interpret secp256k1_bench via assembly", |b| {
let buffer = fs::read("benches/data/secp256k1_bench").unwrap().into();
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();

let args = [
Ok("secp256k1_bench".into()),
Ok("033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f".into()),
Ok("304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3".into()),
Ok("foo".into()),
Ok("bar".into()),
].into_iter();
b.iter(|| {
let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::MAX);
let core = DefaultMachineBuilder::new(asm_core).build();
let mut machine = AsmMachine::new(core);
machine.load_program(&buffer, &args[..]).unwrap();
machine.load_program(&buffer, args.clone()).unwrap();
machine.run().unwrap()
});
});
Expand All @@ -55,17 +57,19 @@ fn asm_benchmark(c: &mut Criterion) {
fn mop_benchmark(c: &mut Criterion) {
c.bench_function("interpret secp256k1_bench via assembly mop", |b| {
let buffer = fs::read("benches/data/secp256k1_bench").unwrap().into();
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();
let args = [
Ok("secp256k1_bench".into()),
Ok("033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f".into()),
Ok("304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3".into()),
Ok("foo".into()),
Ok("bar".into()),
].into_iter();
b.iter(|| {
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, VERSION2, u64::MAX);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.build();
let mut machine = AsmMachine::new(core);
machine.load_program(&buffer, &args).unwrap();
machine.load_program(&buffer, args.clone()).unwrap();
machine.run().unwrap()
});
});
Expand All @@ -77,25 +81,27 @@ fn mop_memoized_benchmark(c: &mut Criterion) {
let isa = ISA_IMC | ISA_B | ISA_MOP;
let version = VERSION2;
let buffer = fs::read("benches/data/secp256k1_bench").unwrap().into();
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();
let args = [
Ok("secp256k1_bench".into()),
Ok("033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f".into()),
Ok("304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3".into()),
Ok("foo".into()),
Ok("bar".into()),
].into_iter();
let mut decoder = MemoizedFixedTraceDecoder::new(build_decoder::<u64>(isa, version));
let asm_core = AsmCoreMachine::new(isa, version, u64::MAX);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.build();
let mut machine = AsmMachine::new(core);
machine.load_program(&buffer, &args).unwrap();
machine.load_program(&buffer, args.clone()).unwrap();
machine.run_with_decoder(&mut decoder).unwrap();

b.iter(|| {
let asm_core = AsmCoreMachine::new(isa, version, u64::MAX);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.build();
let mut machine = AsmMachine::new(core);
machine.load_program(&buffer, &args).unwrap();
machine.load_program(&buffer, args.clone()).unwrap();
decoder.clear_traces();
machine.run_with_decoder(&mut decoder).unwrap()
});
Expand All @@ -108,25 +114,27 @@ fn mop_memoized_dynamic_benchmark(c: &mut Criterion) {
let isa = ISA_IMC | ISA_B | ISA_MOP;
let version = VERSION2;
let buffer = fs::read("benches/data/secp256k1_bench").unwrap().into();
let args: Vec<Bytes> = vec!["secp256k1_bench",
"033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f",
"304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3",
"foo",
"bar"].into_iter().map(|a| a.into()).collect();
let args = [
Ok("secp256k1_bench".into()),
Ok("033f8cf9c4d51a33206a6c1c6b27d2cc5129daa19dbd1fc148d395284f6b26411f".into()),
Ok("304402203679d909f43f073c7c1dcf8468a485090589079ee834e6eed92fea9b09b06a2402201e46f1075afa18f306715e7db87493e7b7e779569aa13c64ab3d09980b3560a3".into()),
Ok("foo".into()),
Ok("bar".into()),
].into_iter();
let mut decoder = MemoizedDynamicTraceDecoder::new(build_decoder::<u64>(isa, version));
let asm_core = AsmCoreMachine::new(isa, version, u64::MAX);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.build();
let mut machine = AsmMachine::new(core);
machine.load_program(&buffer, &args).unwrap();
machine.load_program(&buffer, args.clone()).unwrap();
machine.run_with_decoder(&mut decoder).unwrap();

b.iter(|| {
let asm_core = AsmCoreMachine::new(isa, version, u64::MAX);
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.build();
let mut machine = AsmMachine::new(core);
machine.load_program(&buffer, &args).unwrap();
machine.load_program(&buffer, args.clone()).unwrap();
decoder.clear_traces();
machine.run_with_decoder(&mut decoder).unwrap()
});
Expand Down
10 changes: 8 additions & 2 deletions examples/check_real_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ fn check_asm(memory_size: usize) -> Result<(), ()> {
let core = DefaultMachineBuilder::new(asm_core).build();
let mut machine = AsmMachine::new(core);
machine
.load_program(&Bytes::from(BIN_PATH_BUFFER), &vec![Bytes::from(BIN_NAME)])
.load_program(
&Bytes::from(BIN_PATH_BUFFER),
[Ok(Bytes::from(BIN_NAME))].into_iter(),
)
.unwrap();
let result = machine.run();
assert!(result.is_ok());
Expand All @@ -192,7 +195,10 @@ fn check_asm_in_thread(memory_size: usize) -> Result<(), ()> {
let core = DefaultMachineBuilder::new(asm_core).build();
let mut machine = AsmMachine::new(core);
machine
.load_program(&Bytes::from(BIN_PATH_BUFFER), &vec![Bytes::from(BIN_NAME)])
.load_program(
&Bytes::from(BIN_PATH_BUFFER),
[Ok(Bytes::from(BIN_NAME))].into_iter(),
)
.unwrap();
let thread_join_handle = thread::spawn(move || {
let result = machine.run();
Expand Down
4 changes: 2 additions & 2 deletions examples/ckb_vm_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main_asm(code: Bytes, args: Vec<Bytes>) -> Result<(), Box<dyn std::error::Err
.syscall(Box::new(DebugSyscall {}))
.build();
let mut machine = ckb_vm::machine::asm::AsmMachine::new(core);
machine.load_program(&code, &args)?;
machine.load_program(&code, args.into_iter().map(Ok))?;
let exit = machine.run();
let cycles = machine.machine.cycles();
println!(
Expand All @@ -71,7 +71,7 @@ fn main_int(code: Bytes, args: Vec<Bytes>) -> Result<(), Box<dyn std::error::Err
let machine_builder = ckb_vm::DefaultMachineBuilder::new(core_machine)
.instruction_cycle_func(Box::new(estimate_cycles));
let mut machine = machine_builder.syscall(Box::new(DebugSyscall {})).build();
machine.load_program(&code, &args)?;
machine.load_program(&code, args.into_iter().map(Ok))?;
let exit = machine.run();
let cycles = machine.cycles();
println!(
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use crate::{
instructions::{Instruction, Register},
machine::{
trace::TraceMachine, CoreMachine, DefaultCoreMachine, DefaultMachine,
DefaultMachineBuilder, InstructionCycleFunc, Machine, SupportMachine,
DefaultMachineBuilder, FlattenedArgsReader, InstructionCycleFunc, Machine, SupportMachine,
},
memory::{flat::FlatMemory, sparse::SparseMemory, wxorx::WXorXMemory, Memory},
syscalls::Syscalls,
Expand All @@ -47,7 +47,7 @@ pub fn run<R: Register, M: Memory<REG = R> + Default>(
WXorXMemory::new(M::default()),
);
let mut machine = TraceMachine::new(DefaultMachineBuilder::new(core_machine).build());
machine.load_program(program, args)?;
machine.load_program(program, args.iter().map(|e| Ok(e.clone())))?;
machine.run()
}

Expand All @@ -63,7 +63,7 @@ pub fn run_with_memory<R: Register, M: Memory<REG = R>>(
WXorXMemory::new(memory),
);
let mut machine = TraceMachine::new(DefaultMachineBuilder::new(core_machine).build());
machine.load_program(program, args)?;
machine.load_program(program, args.iter().map(|e| Ok(e.clone())))?;
machine.run()
}

Expand Down
8 changes: 6 additions & 2 deletions src/machine/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,15 +666,19 @@ impl AsmMachine {
self.machine.inner.max_cycles = cycles;
}

pub fn load_program(&mut self, program: &Bytes, args: &[Bytes]) -> Result<u64, Error> {
pub fn load_program(
&mut self,
program: &Bytes,
args: impl ExactSizeIterator<Item = Result<Bytes, Error>>,
) -> Result<u64, Error> {
self.machine.load_program(program, args)
}

pub fn load_program_with_metadata(
&mut self,
program: &Bytes,
metadata: &ProgramMetadata,
args: &[Bytes],
args: impl ExactSizeIterator<Item = Result<Bytes, Error>>,
) -> Result<u64, Error> {
self.machine
.load_program_with_metadata(program, metadata, args)
Expand Down
Loading
Loading