Skip to content
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
16 changes: 2 additions & 14 deletions crates/polkavm/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if_compiler_is_supported! {
use crate::config::{BackendKind, Config, GasMeteringKind, ModuleConfig, SandboxKind};
use crate::error::{bail, bail_static, Error};
use crate::gas::{CostModel, CostModelRef, GasVisitor};
use crate::interpreter::{InterpretedInstance, InterpretedModule};
use crate::interpreter::InterpretedInstance;
use crate::utils::{GuestInit, InterruptKind};
use crate::{Gas, ProgramCounter};

Expand Down Expand Up @@ -286,7 +286,6 @@ pub(crate) struct ModulePrivate {

blob: ProgramBlob,
compiled_module: CompiledModuleKind,
interpreted_module: Option<InterpretedModule>,
memory_map: MemoryMap,
gas_metering: Option<GasMeteringKind>,
is_strict: bool,
Expand Down Expand Up @@ -350,10 +349,6 @@ impl Module {
}
}

pub(crate) fn interpreted_module(&self) -> Option<&InterpretedModule> {
self.state().interpreted_module.as_ref()
}

pub(crate) fn blob(&self) -> &ProgramBlob {
&self.state().blob
}
Expand Down Expand Up @@ -614,16 +609,10 @@ impl Module {
}}
};

let interpreted_module = if engine.interpreter_enabled {
Some(InterpretedModule::new(init)?)
} else {
None
};

let compiled_module = compiled_module.unwrap_or(CompiledModuleKind::Unavailable);
log::trace!("Processing finished!");

assert!(compiled_module.is_some() || interpreted_module.is_some());
assert!(compiled_module.is_some() || engine.interpreter_enabled);
if compiled_module.is_some() {
log::debug!("Backend used: 'compiled'");
} else {
Expand Down Expand Up @@ -670,7 +659,6 @@ impl Module {

blob,
compiled_module,
interpreted_module,
memory_map,
gas_metering: config.gas_metering,
is_strict: config.is_strict,
Expand Down
45 changes: 16 additions & 29 deletions crates/polkavm/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::api::{MemoryAccessError, Module, RegValue, SetCacheSizeLimitArgs};
use crate::error::Error;
use crate::gas::{GasVisitor, GasVisitorT};
use crate::utils::{FlatMap, GuestInit, InterruptKind, Segfault};
use crate::utils::{FlatMap, InterruptKind, Segfault};
use crate::{Gas, GasMeteringKind, ProgramCounter};
use alloc::boxed::Box;
use alloc::collections::btree_map::Entry;
Expand Down Expand Up @@ -62,25 +62,8 @@ impl IntoRegImm for u32 {
}
}

pub(crate) struct InterpretedModule {
ro_data: Vec<u8>,
rw_data: Vec<u8>,
}

impl InterpretedModule {
pub fn new(init: GuestInit) -> Result<Self, Error> {
let memory_map = init.memory_map().map_err(Error::from_static_str)?;
let mut ro_data: Vec<_> = init.ro_data.into();
ro_data.resize(cast(memory_map.ro_data_size()).to_usize(), 0);

Ok(InterpretedModule {
ro_data,
rw_data: init.rw_data.into(),
})
}
}

pub(crate) struct BasicMemory {
ro_data: Vec<u8>,
rw_data: Vec<u8>,
stack: Vec<u8>,
aux: Vec<u8>,
Expand All @@ -92,6 +75,7 @@ pub(crate) struct BasicMemory {
impl BasicMemory {
fn new() -> Self {
Self {
ro_data: Vec::new(),
rw_data: Vec::new(),
stack: Vec::new(),
aux: Vec::new(),
Expand All @@ -116,22 +100,26 @@ impl BasicMemory {
}

fn force_reset(&mut self, module: &Module) {
self.ro_data.clear();
self.rw_data.clear();
self.stack.clear();
self.aux.clear();
self.heap_size = 0;
self.is_memory_dirty = false;
self.accessible_aux_size = 0;

if let Some(interpreted_module) = module.interpreted_module().as_ref() {
self.rw_data.extend_from_slice(&interpreted_module.rw_data);
self.rw_data.resize(cast(module.memory_map().rw_data_size()).to_usize(), 0);
self.stack.resize(cast(module.memory_map().stack_size()).to_usize(), 0);
let blob = module.blob();
self.rw_data.extend_from_slice(blob.rw_data());
self.rw_data.resize(cast(module.memory_map().rw_data_size()).to_usize(), 0);

// TODO: Do this lazily?
self.aux.resize(cast(module.memory_map().aux_data_size()).to_usize(), 0);
self.accessible_aux_size = cast(module.memory_map().aux_data_size()).to_usize();
}
self.ro_data.extend_from_slice(blob.ro_data());
self.ro_data.resize(cast(module.memory_map().ro_data_size()).to_usize(), 0);

self.stack.resize(cast(module.memory_map().stack_size()).to_usize(), 0);

// TODO: Do this lazily?
self.aux.resize(cast(module.memory_map().aux_data_size()).to_usize(), 0);
self.accessible_aux_size = cast(module.memory_map().aux_data_size()).to_usize();
}

fn accessible_aux_size(&self) -> u32 {
Expand All @@ -152,8 +140,7 @@ impl BasicMemory {
} else if address >= memory_map.rw_data_address() {
(memory_map.rw_data_address(), &self.rw_data[..])
} else if address >= memory_map.ro_data_address() {
let module = module.interpreted_module().unwrap();
(memory_map.ro_data_address(), &module.ro_data[..])
(memory_map.ro_data_address(), &self.ro_data[..])
} else {
return None;
};
Expand Down
1 change: 1 addition & 0 deletions crates/polkavm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::vec::Vec;

#[allow(dead_code)]
#[derive(Copy, Clone, Default)]
pub struct GuestInit<'a> {
pub page_size: u32,
Expand Down