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

chore: use Arc instead of lifetime parameter #276

Merged
merged 5 commits into from
Aug 9, 2022
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
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: ff49a1dfb3c12be9dc18073133d660f0dd176662
TEST_SUITE_COMMIT: a1fb578bf802ce1155565d509402e7e00a1c280b

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 = aot_machine.compile().unwrap();
let result = std::sync::Arc::new(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));
let mut machine = AsmMachine::new(core, Some(result.clone()));
machine.load_program(&buffer, &args[..]).unwrap();
machine.run().unwrap()
});
Expand Down
3 changes: 2 additions & 1 deletion examples/ckb-vm-runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ 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(&aot_code));
let mut machine =
ckb_vm::machine::asm::AsmMachine::new(core, Some(std::sync::Arc::new(aot_code)));
machine.load_program(&code, &args)?;
let exit = machine.run();
let cycles = machine.machine.cycles();
Expand Down
45 changes: 22 additions & 23 deletions src/machine/asm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use std::mem::transmute;

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 byteorder::{ByteOrder, LittleEndian};
use bytes::Bytes;
pub use ckb_vm_definitions::asm::AsmCoreMachine;
Expand All @@ -17,21 +29,8 @@ use libc::c_uchar;
use memmap::Mmap;
use rand::{prelude::RngCore, SeedableRng};
use std::collections::HashMap;

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 std::sync::Arc;

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

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

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

impl<'a> AsmMachine<'a> {
impl AsmMachine {
pub fn new(
machine: DefaultMachine<'a, Box<AsmCoreMachine>>,
aot_code: Option<&'a AotCode>,
machine: DefaultMachine<Box<AsmCoreMachine>>,
aot_code: Option<Arc<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 @@ -409,20 +409,20 @@ impl<R: Register, M: Memory + Default> DefaultCoreMachine<R, M> {
pub type InstructionCycleFunc = dyn Fn(Instruction) -> u64;

#[derive(Default)]
pub struct DefaultMachine<'a, Inner> {
pub struct DefaultMachine<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: Option<Box<InstructionCycleFunc>>,
debugger: Option<Box<dyn Debugger<Inner> + 'a>>,
syscalls: Vec<Box<dyn Syscalls<Inner> + 'a>>,
debugger: Option<Box<dyn Debugger<Inner>>>,
syscalls: Vec<Box<dyn Syscalls<Inner>>>,
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 @@ -463,7 +463,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 @@ -498,7 +498,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 @@ -534,7 +534,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 @@ -549,7 +549,7 @@ impl<Inner: CoreMachine> Display for DefaultMachine<'_, Inner> {
}
}

impl<'a, Inner: SupportMachine> DefaultMachine<'a, Inner> {
impl<Inner: SupportMachine> DefaultMachine<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 @@ -627,14 +627,14 @@ impl<'a, Inner: SupportMachine> DefaultMachine<'a, Inner> {
}

#[derive(Default)]
pub struct DefaultMachineBuilder<'a, Inner> {
pub struct DefaultMachineBuilder<Inner> {
inner: Inner,
instruction_cycle_func: Option<Box<InstructionCycleFunc>>,
debugger: Option<Box<dyn Debugger<Inner> + 'a>>,
syscalls: Vec<Box<dyn Syscalls<Inner> + 'a>>,
debugger: Option<Box<dyn Debugger<Inner>>>,
syscalls: Vec<Box<dyn Syscalls<Inner>>>,
}

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

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

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

pub fn build(self) -> DefaultMachine<'a, Inner> {
pub fn build(self) -> DefaultMachine<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<'a, Inner> {
pub machine: DefaultMachine<'a, Inner>,
pub struct TraceMachine<Inner> {
pub machine: DefaultMachine<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<'a, Inner: SupportMachine> TraceMachine<'a, Inner> {
pub fn new(machine: DefaultMachine<'a, Inner>) -> Self {
impl<Inner: SupportMachine> TraceMachine<Inner> {
pub fn new(machine: DefaultMachine<Inner>) -> Self {
Self {
machine,
traces: vec![],
Expand Down
15 changes: 7 additions & 8 deletions tests/machine_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ 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<'a>(path: &str) -> AsmMachine<'a> {
pub fn asm_v1_imcb(path: &str) -> AsmMachine {
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 @@ -36,14 +35,14 @@ pub fn aot_v1_imcb_code(path: &str) -> AotCode {
}

#[cfg(has_aot)]
pub fn aot_v1_imcb<'a>(path: &str, code: &'a AotCode) -> AsmMachine<'a> {
pub fn aot_v1_imcb(path: &str, code: AotCode) -> AsmMachine {
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(code));
let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code)));
machine
.load_program(&buffer, &vec![Bytes::from("main")])
.unwrap();
Expand Down Expand Up @@ -71,7 +70,7 @@ pub fn int_v1_imcb(
}

#[cfg(has_asm)]
pub fn asm_v1_mop<'a>(path: &str, args: Vec<Bytes>) -> AsmMachine<'a> {
pub fn asm_v1_mop(path: &str, args: Vec<Bytes>) -> AsmMachine {
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 @@ -93,7 +92,7 @@ pub fn aot_v1_mop_code(path: &str) -> AotCode {
}

#[cfg(has_aot)]
pub fn aot_v1_mop<'a>(path: &str, args: Vec<Bytes>, code: &'a AotCode) -> AsmMachine<'a> {
pub fn aot_v1_mop(path: &str, args: Vec<Bytes>, code: AotCode) -> AsmMachine {
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 @@ -102,7 +101,7 @@ pub fn aot_v1_mop<'a>(path: &str, args: Vec<Bytes>, code: &'a AotCode) -> AsmMac
.build();
let mut argv = vec![Bytes::from("main")];
argv.extend_from_slice(&args);
let mut machine = AsmMachine::new(core, Some(code));
let mut machine = AsmMachine::new(core, Some(std::sync::Arc::new(code)));
machine.load_program(&buffer, &argv).unwrap();
machine
}
Expand Down
Loading