Skip to content

Commit 8d2a4d3

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 4957cc6 commit 8d2a4d3

File tree

3 files changed

+19
-43
lines changed

3 files changed

+19
-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,
@@ -350,10 +349,6 @@ impl Module {
350349
}
351350
}
352351

353-
pub(crate) fn interpreted_module(&self) -> Option<&InterpretedModule> {
354-
self.state().interpreted_module.as_ref()
355-
}
356-
357352
pub(crate) fn blob(&self) -> &ProgramBlob {
358353
&self.state().blob
359354
}
@@ -614,16 +609,10 @@ impl Module {
614609
}}
615610
};
616611

617-
let interpreted_module = if engine.interpreter_enabled {
618-
Some(InterpretedModule::new(init)?)
619-
} else {
620-
None
621-
};
622-
623612
let compiled_module = compiled_module.unwrap_or(CompiledModuleKind::Unavailable);
624613
log::trace!("Processing finished!");
625614

626-
assert!(compiled_module.is_some() || interpreted_module.is_some());
615+
assert!(compiled_module.is_some() || engine.interpreter_enabled);
627616
if compiled_module.is_some() {
628617
log::debug!("Backend used: 'compiled'");
629618
} else {
@@ -670,7 +659,6 @@ impl Module {
670659

671660
blob,
672661
compiled_module,
673-
interpreted_module,
674662
memory_map,
675663
gas_metering: config.gas_metering,
676664
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
};

crates/polkavm/src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use alloc::vec::Vec;
22

3+
#[allow(dead_code)]
34
#[derive(Copy, Clone, Default)]
45
pub struct GuestInit<'a> {
56
pub page_size: u32,

0 commit comments

Comments
 (0)