Skip to content

Commit

Permalink
Refactoring DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed May 8, 2024
1 parent a83ab4c commit eac5bbf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
8 changes: 4 additions & 4 deletions fuzz/fuzz_targets/snapshot2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use ckb_vm::{
machine::VERSION2,
memory::{round_page_down, round_page_up, FLAG_EXECUTABLE, FLAG_FREEZED},
snapshot2::{DataSource, Snapshot2Context},
Bytes, CoreMachine, DefaultMachine, DefaultMachineBuilder, Error, Memory, ISA_A, ISA_B,
ISA_IMC, ISA_MOP, RISCV_MAX_MEMORY, RISCV_PAGESIZE,
Bytes, CoreMachine, DefaultMachine, DefaultMachineBuilder, Memory, ISA_A, ISA_B, ISA_IMC,
ISA_MOP, RISCV_MAX_MEMORY, RISCV_PAGESIZE,
};
use ckb_vm_definitions::asm::AsmCoreMachine;
use libfuzzer_sys::fuzz_target;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct DummyData {
}

impl DataSource<u32> for DummyData {
fn load_data(&self, id: &u32, offset: u64, length: u64) -> Result<(Bytes, u64), Error> {
fn load_data(&self, id: &u32, offset: u64, length: u64) -> Option<(Bytes, u64)> {
let data = match *id {
DATA_SOURCE_PROGRAM => &self.program,
DATA_SOURCE_CONTENT => &self.content,
Expand All @@ -59,7 +59,7 @@ impl DataSource<u32> for DummyData {
} else {
full_size
};
Ok((data.slice(offset..offset + real_size), full_size as u64))
Some((data.slice(offset..offset + real_size), full_size as u64))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pub enum Error {
#[display(fmt = "asm error: {}", "_0")]
Asm(u8),
#[display(fmt = "script error: index out of bound")]
CkbScriptIndexOutOfBound,
#[display(fmt = "cycles error: max cycles exceeded")]
CyclesExceeded,
#[display(fmt = "cycles error: overflow")]
Expand Down Expand Up @@ -51,6 +49,8 @@ pub enum Error {
MemWriteOnFreezedPage,
#[display(fmt = "pause")]
Pause,
#[display(fmt = "snapshot data load error")]
SnapshotDataLoadError,
#[display(fmt = "unexpected error")]
Unexpected(String),
#[display(fmt = "unimplemented")]
Expand Down
16 changes: 7 additions & 9 deletions src/snapshot2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const PAGE_SIZE: u64 = RISCV_PAGESIZE as u64;
/// an extra u64 value is included here to return the remaining full length of data
/// starting from offset, without considering `length` parameter
pub trait DataSource<I: Clone + PartialEq> {
fn load_data(&self, id: &I, offset: u64, length: u64) -> Result<(Bytes, u64), Error>;
fn load_data(&self, id: &I, offset: u64, length: u64) -> Option<(Bytes, u64)>;
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -70,7 +70,7 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
if address % PAGE_SIZE != 0 {
return Err(Error::MemPageUnalignedAccess);
}
let (data, _) = self.data_source().load_data(id, *offset, *length)?;
let (data, _) = self.load_data(id, *offset, *length)?;
if data.len() as u64 % PAGE_SIZE != 0 {
return Err(Error::MemPageUnalignedAccess);
}
Expand Down Expand Up @@ -100,12 +100,10 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
Ok(())
}

pub fn data_source(&self) -> &D {
&self.data_source
}

pub fn data_source_mut(&mut self) -> &mut D {
&mut self.data_source
pub fn load_data(&mut self, id: &I, offset: u64, length: u64) -> Result<(Bytes, u64), Error> {
self.data_source
.load_data(id, offset, length)
.ok_or(Error::SnapshotDataLoadError)
}

/// Similar to Memory::store_bytes, but this method also tracks memory
Expand All @@ -120,7 +118,7 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
offset: u64,
length: u64,
) -> Result<(u64, u64), Error> {
let (data, full_length) = self.data_source.load_data(id, offset, length)?;
let (data, full_length) = self.load_data(id, offset, length)?;
self.untrack_pages(machine, addr, data.len() as u64)?;
machine.memory_mut().store_bytes(addr, &data)?;
self.track_pages(machine, addr, data.len() as u64, id, offset)?;
Expand Down
12 changes: 3 additions & 9 deletions tests/test_resume2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ const DATA_ID: u64 = 0x2000;
struct TestSource(HashMap<u64, Bytes>);

impl DataSource<u64> for TestSource {
fn load_data(&self, id: &u64, offset: u64, length: u64) -> Result<(Bytes, u64), Error> {
fn load_data(&self, id: &u64, offset: u64, length: u64) -> Option<(Bytes, u64)> {
match self.0.get(id) {
Some(data) => {
let end = if length > 0 {
Expand All @@ -337,12 +337,9 @@ impl DataSource<u64> for TestSource {
data.len() as u64
};
let full_length = end - offset;
Ok((data.slice(offset as usize..end as usize), full_length))
Some((data.slice(offset as usize..end as usize), full_length))
}
None => Err(Error::Unexpected(format!(
"Id {} is missing in source!",
id
))),
None => None,
}
}
}
Expand Down Expand Up @@ -452,7 +449,6 @@ impl Machine {
let (program, _) = context
.lock()
.unwrap()
.data_source()
.load_data(&PROGRAM_ID, 0, 0)
.unwrap();
let metadata = parse_elf::<u64>(&program, inner.machine.version())?;
Expand All @@ -469,7 +465,6 @@ impl Machine {
let (program, _) = context
.lock()
.unwrap()
.data_source()
.load_data(&PROGRAM_ID, 0, 0)
.unwrap();
let metadata = parse_elf::<u64>(&program, inner.version())?;
Expand All @@ -486,7 +481,6 @@ impl Machine {
let (program, _) = context
.lock()
.unwrap()
.data_source()
.load_data(&PROGRAM_ID, 0, 0)
.unwrap();
let metadata = parse_elf::<u64>(&program, inner.machine.version())?;
Expand Down

0 comments on commit eac5bbf

Please sign in to comment.