Skip to content

Commit

Permalink
Revert a92ad61 (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson authored Oct 9, 2022
1 parent 5aa09d8 commit b39561b
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 125 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trigger:

variables:
RUST_BACKTRACE: full
TEST_SUITE_COMMIT: a1fb578bf802ce1155565d509402e7e00a1c280b
TEST_SUITE_COMMIT: ff49a1dfb3c12be9dc18073133d660f0dd176662

jobs:
- job: WinCI
Expand Down
4 changes: 2 additions & 2 deletions benches/vm_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ fn aot_benchmark(c: &mut Criterion) {
"foo",
"bar"].into_iter().map(|a| a.into()).collect();
let mut aot_machine = AotCompilingMachine::load(&buffer.clone(), None, ISA_IMC, VERSION0).unwrap();
let result = std::sync::Arc::new(aot_machine.compile().unwrap());
let result = aot_machine.compile().unwrap();

b.iter(|| {
let asm_core = AsmCoreMachine::new(ISA_IMC, VERSION0, u64::max_value());
let core = DefaultMachineBuilder::new(asm_core).build();
let mut machine = AsmMachine::new(core, Some(result.clone()));
let mut machine = AsmMachine::new(core, Some(&result));
machine.load_program(&buffer, &args[..]).unwrap();
machine.run().unwrap()
});
Expand Down
3 changes: 1 addition & 2 deletions examples/ckb-vm-runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ fn main_aot(code: Bytes, args: Vec<Bytes>) -> Result<(), Box<dyn std::error::Err
.instruction_cycle_func(Box::new(instruction_cycles))
.syscall(Box::new(DebugSyscall {}))
.build();
let mut machine =
ckb_vm::machine::asm::AsmMachine::new(core, Some(std::sync::Arc::new(aot_code)));
let mut machine = ckb_vm::machine::asm::AsmMachine::new(core, Some(&aot_code));
machine.load_program(&code, &args)?;
let exit = machine.run();
let cycles = machine.machine.cycles();
Expand Down
45 changes: 23 additions & 22 deletions src/machine/asm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
use crate::{
decoder::{build_decoder, Decoder},
instructions::{
blank_instruction, execute_instruction, extract_opcode, instruction_length,
is_basic_block_end_instruction,
},
machine::VERSION0,
memory::{
fill_page_data, get_page_indices, memset, round_page_down, round_page_up, FLAG_DIRTY,
FLAG_EXECUTABLE, FLAG_FREEZED, FLAG_WRITABLE, FLAG_WXORX_BIT,
},
CoreMachine, DefaultMachine, Error, Machine, Memory, SupportMachine, MEMORY_FRAME_SHIFTS,
RISCV_MAX_MEMORY, RISCV_PAGES, RISCV_PAGESIZE,
};
use std::mem::transmute;

use byteorder::{ByteOrder, LittleEndian};
use bytes::Bytes;
pub use ckb_vm_definitions::asm::AsmCoreMachine;
Expand All @@ -29,8 +17,21 @@ use libc::c_uchar;
use memmap::Mmap;
use rand::{prelude::RngCore, SeedableRng};
use std::collections::HashMap;
use std::mem::transmute;
use std::sync::Arc;

use crate::{
decoder::{build_decoder, Decoder},
instructions::{
blank_instruction, execute_instruction, extract_opcode, instruction_length,
is_basic_block_end_instruction,
},
machine::VERSION0,
memory::{
fill_page_data, get_page_indices, memset, round_page_down, round_page_up, FLAG_DIRTY,
FLAG_EXECUTABLE, FLAG_FREEZED, FLAG_WRITABLE, FLAG_WXORX_BIT,
},
CoreMachine, DefaultMachine, Error, Machine, Memory, SupportMachine, MEMORY_FRAME_SHIFTS,
RISCV_MAX_MEMORY, RISCV_PAGES, RISCV_PAGESIZE,
};

impl CoreMachine for Box<AsmCoreMachine> {
type REG = u64;
Expand Down Expand Up @@ -432,9 +433,9 @@ impl AotCode {
}
}

pub struct AsmMachine {
pub machine: DefaultMachine<Box<AsmCoreMachine>>,
pub aot_code: Option<Arc<AotCode>>,
pub struct AsmMachine<'a> {
pub machine: DefaultMachine<'a, Box<AsmCoreMachine>>,
pub aot_code: Option<&'a AotCode>,
}

extern "C" {
Expand All @@ -444,10 +445,10 @@ extern "C" {
fn ckb_vm_asm_labels();
}

impl AsmMachine {
impl<'a> AsmMachine<'a> {
pub fn new(
machine: DefaultMachine<Box<AsmCoreMachine>>,
aot_code: Option<Arc<AotCode>>,
machine: DefaultMachine<'a, Box<AsmCoreMachine>>,
aot_code: Option<&'a AotCode>,
) -> Self {
Self { machine, aot_code }
}
Expand Down
30 changes: 15 additions & 15 deletions src/machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,20 @@ impl<R: Register, M: Memory + Default> DefaultCoreMachine<R, M> {

pub type InstructionCycleFunc = dyn Fn(Instruction) -> u64;

pub struct DefaultMachine<Inner> {
pub struct DefaultMachine<'a, Inner> {
inner: Inner,

// We have run benchmarks on secp256k1 verification, the performance
// cost of the Box wrapper here is neglectable, hence we are sticking
// with Box solution for simplicity now. Later if this becomes an issue,
// we can change to static dispatch.
instruction_cycle_func: Box<InstructionCycleFunc>,
debugger: Option<Box<dyn Debugger<Inner>>>,
syscalls: Vec<Box<dyn Syscalls<Inner>>>,
debugger: Option<Box<dyn Debugger<Inner> + 'a>>,
syscalls: Vec<Box<dyn Syscalls<Inner> + 'a>>,
exit_code: i8,
}

impl<Inner: CoreMachine> CoreMachine for DefaultMachine<Inner> {
impl<Inner: CoreMachine> CoreMachine for DefaultMachine<'_, Inner> {
type REG = <Inner as CoreMachine>::REG;
type MEM = <Inner as CoreMachine>::MEM;

Expand Down Expand Up @@ -462,7 +462,7 @@ impl<Inner: CoreMachine> CoreMachine for DefaultMachine<Inner> {
}
}

impl<Inner: SupportMachine> SupportMachine for DefaultMachine<Inner> {
impl<Inner: SupportMachine> SupportMachine for DefaultMachine<'_, Inner> {
fn cycles(&self) -> u64 {
self.inner.cycles()
}
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<Inner: SupportMachine> SupportMachine for DefaultMachine<Inner> {
}
}

impl<Inner: SupportMachine> Machine for DefaultMachine<Inner> {
impl<Inner: SupportMachine> Machine for DefaultMachine<'_, Inner> {
fn ecall(&mut self) -> Result<(), Error> {
let code = self.registers()[A7].to_u64();
match code {
Expand Down Expand Up @@ -533,7 +533,7 @@ impl<Inner: SupportMachine> Machine for DefaultMachine<Inner> {
}
}

impl<Inner: CoreMachine> Display for DefaultMachine<Inner> {
impl<Inner: CoreMachine> Display for DefaultMachine<'_, Inner> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "pc : 0x{:16X}", self.pc().to_u64())?;
for (i, name) in REGISTER_ABI_NAMES.iter().enumerate() {
Expand All @@ -548,7 +548,7 @@ impl<Inner: CoreMachine> Display for DefaultMachine<Inner> {
}
}

impl<Inner: SupportMachine> DefaultMachine<Inner> {
impl<'a, Inner: SupportMachine> DefaultMachine<'a, Inner> {
pub fn load_program(&mut self, program: &Bytes, args: &[Bytes]) -> Result<u64, Error> {
let elf_bytes = self.load_elf(program, true)?;
for syscall in &mut self.syscalls {
Expand Down Expand Up @@ -621,14 +621,14 @@ impl<Inner: SupportMachine> DefaultMachine<Inner> {
}
}

pub struct DefaultMachineBuilder<Inner> {
pub struct DefaultMachineBuilder<'a, Inner> {
inner: Inner,
instruction_cycle_func: Box<InstructionCycleFunc>,
debugger: Option<Box<dyn Debugger<Inner>>>,
syscalls: Vec<Box<dyn Syscalls<Inner>>>,
debugger: Option<Box<dyn Debugger<Inner> + 'a>>,
syscalls: Vec<Box<dyn Syscalls<Inner> + 'a>>,
}

impl<Inner> DefaultMachineBuilder<Inner> {
impl<'a, Inner> DefaultMachineBuilder<'a, Inner> {
pub fn new(inner: Inner) -> Self {
Self {
inner,
Expand All @@ -646,17 +646,17 @@ impl<Inner> DefaultMachineBuilder<Inner> {
self
}

pub fn syscall(mut self, syscall: Box<dyn Syscalls<Inner>>) -> Self {
pub fn syscall(mut self, syscall: Box<dyn Syscalls<Inner> + 'a>) -> Self {
self.syscalls.push(syscall);
self
}

pub fn debugger(mut self, debugger: Box<dyn Debugger<Inner>>) -> Self {
pub fn debugger(mut self, debugger: Box<dyn Debugger<Inner> + 'a>) -> Self {
self.debugger = Some(debugger);
self
}

pub fn build(self) -> DefaultMachine<Inner> {
pub fn build(self) -> DefaultMachine<'a, Inner> {
DefaultMachine {
inner: self.inner,
instruction_cycle_func: self.instruction_cycle_func,
Expand Down
12 changes: 6 additions & 6 deletions src/machine/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ fn calculate_slot(addr: u64) -> usize {
(addr as usize >> TRACE_ADDRESS_SHIFTS) & TRACE_MASK
}

pub struct TraceMachine<Inner> {
pub machine: DefaultMachine<Inner>,
pub struct TraceMachine<'a, Inner> {
pub machine: DefaultMachine<'a, Inner>,

traces: Vec<Trace>,
}

impl<Inner: SupportMachine> CoreMachine for TraceMachine<Inner> {
impl<Inner: SupportMachine> CoreMachine for TraceMachine<'_, Inner> {
type REG = <Inner as CoreMachine>::REG;
type MEM = <Inner as CoreMachine>::MEM;

Expand Down Expand Up @@ -79,7 +79,7 @@ impl<Inner: SupportMachine> CoreMachine for TraceMachine<Inner> {
}
}

impl<Inner: SupportMachine> Machine for TraceMachine<Inner> {
impl<Inner: SupportMachine> Machine for TraceMachine<'_, Inner> {
fn ecall(&mut self) -> Result<(), Error> {
self.machine.ecall()
}
Expand All @@ -89,8 +89,8 @@ impl<Inner: SupportMachine> Machine for TraceMachine<Inner> {
}
}

impl<Inner: SupportMachine> TraceMachine<Inner> {
pub fn new(machine: DefaultMachine<Inner>) -> Self {
impl<'a, Inner: SupportMachine> TraceMachine<'a, Inner> {
pub fn new(machine: DefaultMachine<'a, Inner>) -> Self {
Self {
machine,
traces: vec![],
Expand Down
15 changes: 8 additions & 7 deletions tests/machine_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ use ckb_vm::machine::asm::{AsmCoreMachine, AsmMachine};
#[cfg(has_aot)]
use ckb_vm::machine::{aot::AotCompilingMachine, asm::AotCode};

use bytes::Bytes;
use ckb_vm::machine::{trace::TraceMachine, DefaultCoreMachine, VERSION1};
use ckb_vm::{DefaultMachineBuilder, ISA_B, ISA_IMC, ISA_MOP};
use ckb_vm::{Instruction, SparseMemory, WXorXMemory};

use bytes::Bytes;

pub fn instruction_cycle_func(_: Instruction) -> u64 {
1
}

#[cfg(has_asm)]
pub fn asm_v1_imcb(path: &str) -> AsmMachine {
pub fn asm_v1_imcb<'a>(path: &str) -> AsmMachine<'a> {
let buffer: Bytes = std::fs::read(path).unwrap().into();
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B, VERSION1, u64::max_value());
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
Expand All @@ -35,14 +36,14 @@ pub fn aot_v1_imcb_code(path: &str) -> AotCode {
}

#[cfg(has_aot)]
pub fn aot_v1_imcb(path: &str, code: AotCode) -> AsmMachine {
pub fn aot_v1_imcb<'a>(path: &str, code: &'a AotCode) -> AsmMachine<'a> {
let buffer: Bytes = std::fs::read(path).unwrap().into();

let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B, VERSION1, u64::max_value());
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
.instruction_cycle_func(Box::new(instruction_cycle_func))
.build();
let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code)));
let mut machine = AsmMachine::new(core, Some(code));
machine
.load_program(&buffer, &vec![Bytes::from("main")])
.unwrap();
Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn int_v1_imcb(
}

#[cfg(has_asm)]
pub fn asm_v1_mop(path: &str, args: Vec<Bytes>) -> AsmMachine {
pub fn asm_v1_mop<'a>(path: &str, args: Vec<Bytes>) -> AsmMachine<'a> {
let buffer: Bytes = std::fs::read(path).unwrap().into();
let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, VERSION1, u64::max_value());
let core = DefaultMachineBuilder::<Box<AsmCoreMachine>>::new(asm_core)
Expand All @@ -92,7 +93,7 @@ pub fn aot_v1_mop_code(path: &str) -> AotCode {
}

#[cfg(has_aot)]
pub fn aot_v1_mop(path: &str, args: Vec<Bytes>, code: AotCode) -> AsmMachine {
pub fn aot_v1_mop<'a>(path: &str, args: Vec<Bytes>, code: &'a AotCode) -> AsmMachine<'a> {
let buffer: Bytes = std::fs::read(path).unwrap().into();

let asm_core = AsmCoreMachine::new(ISA_IMC | ISA_B | ISA_MOP, VERSION1, u64::max_value());
Expand All @@ -101,7 +102,7 @@ pub fn aot_v1_mop(path: &str, args: Vec<Bytes>, code: AotCode) -> AsmMachine {
.build();
let mut argv = vec![Bytes::from("main")];
argv.extend_from_slice(&args);
let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code)));
let mut machine = AsmMachine::new(core, Some(code));
machine.load_program(&buffer, &argv).unwrap();
machine
}
Expand Down
Loading

0 comments on commit b39561b

Please sign in to comment.