Skip to content

Commit 03960dc

Browse files
fix: borrow import names from bytecode instead of allocating them
Signed-off-by: Florian Hartung <florian.hartung@dlr.de>
1 parent 62986e9 commit 03960dc

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/core/reader/types/import.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use alloc::borrow::ToOwned;
2-
use alloc::string::String;
3-
41
use crate::core::indices::TypeIdx;
52
use crate::core::reader::WasmReader;
63
use crate::{ValidationError, ValidationInfo};
@@ -9,16 +6,16 @@ use super::global::GlobalType;
96
use super::{ExternType, MemType, TableType};
107

118
#[derive(Debug, Clone)]
12-
pub struct Import {
13-
pub module_name: String,
14-
pub name: String,
9+
pub struct Import<'wasm> {
10+
pub module_name: &'wasm str,
11+
pub name: &'wasm str,
1512
pub desc: ImportDesc,
1613
}
1714

18-
impl Import {
19-
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
20-
let module_name = wasm.read_name()?.to_owned();
21-
let name = wasm.read_name()?.to_owned();
15+
impl<'wasm> Import<'wasm> {
16+
pub fn read(wasm: &mut WasmReader<'wasm>) -> Result<Self, ValidationError> {
17+
let module_name = wasm.read_name()?;
18+
let name = wasm.read_name()?;
2219
let desc = ImportDesc::read(wasm)?;
2320

2421
Ok(Self {

src/core/reader/types/values.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const CONTINUATION_BIT: u8 = 0b10000000;
1919

2020
const INTEGER_BIT_FLAG: u8 = !CONTINUATION_BIT;
2121

22-
impl WasmReader<'_> {
22+
impl<'wasm> WasmReader<'wasm> {
2323
/// Tries to read one byte and fails if the end of file is reached.
2424
pub fn read_u8(&mut self) -> Result<u8, ValidationError> {
2525
let byte = self.peek_u8()?;
@@ -351,7 +351,7 @@ impl WasmReader<'_> {
351351
}
352352

353353
/// Note: If `Err`, the [WasmReader] object is no longer guaranteed to be in a valid state
354-
pub fn read_name(&mut self) -> Result<&str, ValidationError> {
354+
pub fn read_name(&mut self) -> Result<&'wasm str, ValidationError> {
355355
let len = self.read_var_u32()? as usize;
356356

357357
let utf8_str = &self
@@ -369,7 +369,8 @@ impl WasmReader<'_> {
369369
mut read_element: F,
370370
) -> Result<Vec<T>, ValidationError>
371371
where
372-
F: FnMut(&mut WasmReader, usize) -> Result<T, ValidationError>,
372+
T: 'wasm,
373+
F: FnMut(&mut WasmReader<'wasm>, usize) -> Result<T, ValidationError>,
373374
{
374375
let mut idx = 0;
375376
self.read_vec(|wasm| {
@@ -382,7 +383,8 @@ impl WasmReader<'_> {
382383
/// Note: If `Err`, the [WasmReader] object is no longer guaranteed to be in a valid state
383384
pub fn read_vec<T, F>(&mut self, mut read_element: F) -> Result<Vec<T>, ValidationError>
384385
where
385-
F: FnMut(&mut WasmReader) -> Result<T, ValidationError>,
386+
T: 'wasm,
387+
F: FnMut(&mut WasmReader<'wasm>) -> Result<T, ValidationError>,
386388
{
387389
let len = self.read_var_u32()?;
388390
core::iter::repeat_with(|| read_element(self))

src/execution/linker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::{
2+
borrow::ToOwned,
23
collections::btree_map::{BTreeMap, Entry},
34
string::String,
45
vec::Vec,
@@ -141,7 +142,7 @@ impl Linker {
141142
.imports
142143
.iter()
143144
.map(|import| {
144-
self.get_unchecked(import.module_name.clone(), import.name.clone())
145+
self.get_unchecked(import.module_name.to_owned(), import.name.to_owned())
145146
.ok_or(RuntimeError::UnableToResolveExternLookup)
146147
})
147148
.collect()

src/validation/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) struct ImportsLength {
3939
pub struct ValidationInfo<'bytecode> {
4040
pub(crate) wasm: &'bytecode [u8],
4141
pub(crate) types: Vec<FuncType>,
42-
pub(crate) imports: Vec<Import>,
42+
pub(crate) imports: Vec<Import<'bytecode>>,
4343
pub(crate) functions: Vec<TypeIdx>,
4444
pub(crate) tables: Vec<TableType>,
4545
pub(crate) memories: Vec<MemType>,
@@ -488,12 +488,16 @@ fn read_next_header(
488488
}
489489

490490
#[inline(always)]
491-
fn handle_section<T, F: FnOnce(&mut WasmReader, SectionHeader) -> Result<T, ValidationError>>(
492-
wasm: &mut WasmReader,
491+
fn handle_section<'wasm, T, F>(
492+
wasm: &mut WasmReader<'wasm>,
493493
header: &mut Option<SectionHeader>,
494494
section_ty: SectionTy,
495495
handler: F,
496-
) -> Result<Option<T>, ValidationError> {
496+
) -> Result<Option<T>, ValidationError>
497+
where
498+
T: 'wasm,
499+
F: FnOnce(&mut WasmReader<'wasm>, SectionHeader) -> Result<T, ValidationError>,
500+
{
497501
match &header {
498502
Some(SectionHeader { ty, .. }) if *ty == section_ty => {
499503
let h = header.take().unwrap();

0 commit comments

Comments
 (0)