Skip to content

Commit 2ec0f61

Browse files
committed
interpreter: get rid of InterpretedModule
...since ro_data and rw_data are already part of the ProgramBlob. Signed-off-by: Aman <[email protected]>
1 parent 9d1f9bc commit 2ec0f61

File tree

2 files changed

+18
-43
lines changed

2 files changed

+18
-43
lines changed

crates/polkavm/src/api.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if_compiler_is_supported! {
1919
use crate::config::{BackendKind, Config, GasMeteringKind, ModuleConfig, SandboxKind};
2020
use crate::error::{bail, bail_static, Error};
2121
use crate::gas::{CostModel, CostModelRef, GasVisitor};
22-
use crate::interpreter::{InterpretedInstance, InterpretedModule};
22+
use crate::interpreter::InterpretedInstance;
2323
use crate::utils::{GuestInit, InterruptKind};
2424
use crate::{Gas, ProgramCounter};
2525

@@ -286,7 +286,6 @@ pub(crate) struct ModulePrivate {
286286

287287
blob: ProgramBlob,
288288
compiled_module: CompiledModuleKind,
289-
interpreted_module: Option<InterpretedModule>,
290289
memory_map: MemoryMap,
291290
gas_metering: Option<GasMeteringKind>,
292291
is_strict: bool,
@@ -344,10 +343,6 @@ impl Module {
344343
}
345344
}
346345

347-
pub(crate) fn interpreted_module(&self) -> Option<&InterpretedModule> {
348-
self.state().interpreted_module.as_ref()
349-
}
350-
351346
pub(crate) fn blob(&self) -> &ProgramBlob {
352347
&self.state().blob
353348
}
@@ -601,16 +596,10 @@ impl Module {
601596
}}
602597
};
603598

604-
let interpreted_module = if engine.interpreter_enabled {
605-
Some(InterpretedModule::new(init)?)
606-
} else {
607-
None
608-
};
609-
610599
let compiled_module = compiled_module.unwrap_or(CompiledModuleKind::Unavailable);
611600
log::trace!("Processing finished!");
612601

613-
assert!(compiled_module.is_some() || interpreted_module.is_some());
602+
assert!(compiled_module.is_some() || engine.interpreter_enabled);
614603
if compiled_module.is_some() {
615604
log::debug!("Backend used: 'compiled'");
616605
} else {
@@ -657,7 +646,6 @@ impl Module {
657646

658647
blob,
659648
compiled_module,
660-
interpreted_module,
661649
memory_map,
662650
gas_metering: config.gas_metering,
663651
is_strict: config.is_strict,

crates/polkavm/src/interpreter.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::api::{MemoryAccessError, Module, RegValue, SetCacheSizeLimitArgs};
55
use crate::error::Error;
66
use crate::gas::{GasVisitor, GasVisitorT};
7-
use crate::utils::{FlatMap, GuestInit, InterruptKind, Segfault};
7+
use crate::utils::{FlatMap, InterruptKind, Segfault};
88
use crate::{Gas, GasMeteringKind, ProgramCounter};
99
use alloc::boxed::Box;
1010
use alloc::collections::btree_map::Entry;
@@ -62,25 +62,8 @@ impl IntoRegImm for u32 {
6262
}
6363
}
6464

65-
pub(crate) struct InterpretedModule {
66-
ro_data: Vec<u8>,
67-
rw_data: Vec<u8>,
68-
}
69-
70-
impl InterpretedModule {
71-
pub fn new(init: GuestInit) -> Result<Self, Error> {
72-
let memory_map = init.memory_map().map_err(Error::from_static_str)?;
73-
let mut ro_data: Vec<_> = init.ro_data.into();
74-
ro_data.resize(cast(memory_map.ro_data_size()).to_usize(), 0);
75-
76-
Ok(InterpretedModule {
77-
ro_data,
78-
rw_data: init.rw_data.into(),
79-
})
80-
}
81-
}
82-
8365
pub(crate) struct BasicMemory {
66+
ro_data: Vec<u8>,
8467
rw_data: Vec<u8>,
8568
stack: Vec<u8>,
8669
aux: Vec<u8>,
@@ -92,6 +75,7 @@ pub(crate) struct BasicMemory {
9275
impl BasicMemory {
9376
fn new() -> Self {
9477
Self {
78+
ro_data: Vec::new(),
9579
rw_data: Vec::new(),
9680
stack: Vec::new(),
9781
aux: Vec::new(),
@@ -116,22 +100,26 @@ impl BasicMemory {
116100
}
117101

118102
fn force_reset(&mut self, module: &Module) {
103+
self.ro_data.clear();
119104
self.rw_data.clear();
120105
self.stack.clear();
121106
self.aux.clear();
122107
self.heap_size = 0;
123108
self.is_memory_dirty = false;
124109
self.accessible_aux_size = 0;
125110

126-
if let Some(interpreted_module) = module.interpreted_module().as_ref() {
127-
self.rw_data.extend_from_slice(&interpreted_module.rw_data);
128-
self.rw_data.resize(cast(module.memory_map().rw_data_size()).to_usize(), 0);
129-
self.stack.resize(cast(module.memory_map().stack_size()).to_usize(), 0);
111+
let blob = module.blob();
112+
self.rw_data.extend_from_slice(blob.rw_data());
113+
self.rw_data.resize(cast(module.memory_map().rw_data_size()).to_usize(), 0);
130114

131-
// TODO: Do this lazily?
132-
self.aux.resize(cast(module.memory_map().aux_data_size()).to_usize(), 0);
133-
self.accessible_aux_size = cast(module.memory_map().aux_data_size()).to_usize();
134-
}
115+
self.ro_data.extend_from_slice(blob.ro_data());
116+
self.ro_data.resize(cast(module.memory_map().ro_data_size()).to_usize(), 0);
117+
118+
self.stack.resize(cast(module.memory_map().stack_size()).to_usize(), 0);
119+
120+
// TODO: Do this lazily?
121+
self.aux.resize(cast(module.memory_map().aux_data_size()).to_usize(), 0);
122+
self.accessible_aux_size = cast(module.memory_map().aux_data_size()).to_usize();
135123
}
136124

137125
fn accessible_aux_size(&self) -> u32 {
@@ -152,8 +140,7 @@ impl BasicMemory {
152140
} else if address >= memory_map.rw_data_address() {
153141
(memory_map.rw_data_address(), &self.rw_data[..])
154142
} else if address >= memory_map.ro_data_address() {
155-
let module = module.interpreted_module().unwrap();
156-
(memory_map.ro_data_address(), &module.ro_data[..])
143+
(memory_map.ro_data_address(), &self.ro_data[..])
157144
} else {
158145
return None;
159146
};

0 commit comments

Comments
 (0)