Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Verify mode #726

Merged
merged 17 commits into from
Feb 7, 2025
2 changes: 1 addition & 1 deletion crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ pub use module::Module;
pub use syntax::{
FuncType, GlobalType, ImportDesc, Limits, Mut, RefType, ResultType, TableType, ValType,
};
pub use valid::validate;
pub use valid::prepare;
4 changes: 2 additions & 2 deletions crates/interpreter/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::parser::{SkipData, SkipElem};
use crate::side_table::*;
use crate::syntax::*;
use crate::toctou::*;
use crate::valid::validate;
use crate::valid::prepare;
use crate::*;

/// Valid module.
Expand Down Expand Up @@ -52,7 +52,7 @@ impl ImportDesc {
impl<'m> Module<'m> {
/// Validates a WASM module in binary format.
pub fn new(binary: &'m [u8]) -> Result<Self, Error> {
let side_table = validate(binary)?;
let side_table = prepare(binary)?;
let mut module = unsafe { Self::new_unchecked(binary) };
// TODO(dev/fast-interp): We should take a buffer as argument to write to.
module.side_table = Box::leak(Box::new(side_table));
Expand Down
4 changes: 4 additions & 0 deletions crates/interpreter/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl<'m, M: Mode> Parser<'m, M> {
Ok(val)
}

pub fn parse_u16(&mut self) -> MResult<u16, M> {
Ok(self.parse_leb128(false, 16)? as u16)
}

pub fn parse_u32(&mut self) -> MResult<u32, M> {
Ok(self.parse_leb128(false, 32)? as u32)
}
Expand Down
26 changes: 14 additions & 12 deletions crates/interpreter/src/side_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,41 @@ use core::ops::Range;
use crate::error::*;
use crate::module::Parser;

#[allow(dead_code)]
pub struct SideTable<'m> {
indices: &'m [u16], // including 0 and the length of metadata_array
metadata: &'m [u16],
pub struct SideTableView<'m> {
pub func_idx: usize,
pub indices: &'m [u16], // including 0 and the length of metadata_array
pub metadata: &'m [u16],
}

#[allow(dead_code)]
impl<'m> SideTable<'m> {
fn metadata(&self, func_idx: usize) -> Metadata<'m> {
impl<'m> SideTableView<'m> {
pub fn metadata(&self, func_idx: usize) -> Metadata<'m> {
Metadata(
&self.metadata[self.indices[func_idx] as usize .. self.indices[func_idx + 1] as usize],
)
}
}

#[allow(dead_code)]
#[derive(Copy, Clone)]
struct Metadata<'m>(&'m [u16]);
#[derive(Default, Copy, Clone)]
pub struct Metadata<'m>(&'m [u16]);

#[allow(dead_code)]
impl<'m> Metadata<'m> {
pub fn type_idx(&self) -> usize {
self.0[0] as usize
}

#[allow(dead_code)]
pub fn parser(&self, module: &'m [u8]) -> Parser<'m> {
unsafe { Parser::new(&module[self.read_u32(1) .. self.read_u32(3)]) }
unsafe { Parser::new(&module[self.parser_range().start .. self.parser_range().end]) }
}

pub fn branch_table(&self) -> &[BranchTableEntry] {
bytemuck::cast_slice(&self.0[5 ..])
}

pub fn parser_range(&self) -> Range<usize> {
self.read_u32(1) .. self.read_u32(3)
}

fn read_u32(&self, idx: usize) -> usize {
bytemuck::pod_read_unaligned::<u32>(bytemuck::cast_slice(&self.0[idx .. idx + 2])) as usize
}
Expand Down
Loading
Loading