Skip to content

Commit e16faf8

Browse files
zhouwfangia0
andauthored
Add the Verify mode (#726)
#46 --------- Co-authored-by: Zhou Fang <[email protected]> Co-authored-by: Julien Cretin <[email protected]>
1 parent 8ff8d8e commit e16faf8

File tree

4 files changed

+292
-131
lines changed

4 files changed

+292
-131
lines changed

crates/interpreter/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,4 @@ pub use module::Module;
156156
pub use syntax::{
157157
FuncType, GlobalType, ImportDesc, Limits, Mut, RefType, ResultType, TableType, ValType,
158158
};
159-
pub use valid::validate;
159+
pub use valid::prepare;

crates/interpreter/src/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::parser::{SkipData, SkipElem};
2020
use crate::side_table::*;
2121
use crate::syntax::*;
2222
use crate::toctou::*;
23-
use crate::valid::validate;
23+
use crate::valid::prepare;
2424
use crate::*;
2525

2626
/// Valid module.
@@ -52,7 +52,7 @@ impl ImportDesc {
5252
impl<'m> Module<'m> {
5353
/// Validates a WASM module in binary format.
5454
pub fn new(binary: &'m [u8]) -> Result<Self, Error> {
55-
let side_table = validate(binary)?;
55+
let side_table = prepare(binary)?;
5656
let mut module = unsafe { Self::new_unchecked(binary) };
5757
// TODO(dev/fast-interp): We should take a buffer as argument to write to.
5858
module.side_table = Box::leak(Box::new(side_table));

crates/interpreter/src/side_table.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,62 @@ use core::ops::Range;
1818
use crate::error::*;
1919
use crate::module::Parser;
2020

21-
#[allow(dead_code)]
22-
pub struct SideTable<'m> {
23-
indices: &'m [u16], // including 0 and the length of metadata_array
24-
metadata: &'m [u16],
21+
pub struct SideTableView<'m> {
22+
pub func_idx: usize,
23+
pub indices: &'m [u16], // including 0 and the length of metadata_array
24+
pub metadata: &'m [u16],
25+
pub branch_table_view: Metadata<'m>,
2526
}
2627

27-
#[allow(dead_code)]
28-
impl<'m> SideTable<'m> {
29-
fn metadata(&self, func_idx: usize) -> Metadata<'m> {
28+
impl<'m> SideTableView<'m> {
29+
pub fn new(parser: &mut crate::valid::Parser<'m>) -> Result<Self, Error> {
30+
Ok(SideTableView {
31+
func_idx: 0,
32+
indices: parse_side_table_field(parser)?,
33+
metadata: parse_side_table_field(parser)?,
34+
branch_table_view: Default::default(),
35+
})
36+
}
37+
38+
pub fn metadata(&self, func_idx: usize) -> Metadata<'m> {
3039
Metadata(
3140
&self.metadata[self.indices[func_idx] as usize .. self.indices[func_idx + 1] as usize],
3241
)
3342
}
3443
}
3544

36-
#[allow(dead_code)]
37-
#[derive(Copy, Clone)]
38-
struct Metadata<'m>(&'m [u16]);
45+
fn parse_u16(data: &[u8]) -> u16 {
46+
bytemuck::pod_read_unaligned::<u16>(bytemuck::cast_slice(&data[0 .. 2]))
47+
}
48+
49+
fn parse_side_table_field<'m>(parser: &mut crate::valid::Parser<'m>) -> Result<&'m [u16], Error> {
50+
let len = parse_u16(parser.save()) as usize;
51+
let parser = parser.split_at(len)?;
52+
let bytes = parser.save().get(0 .. len * 2).unwrap();
53+
Ok(bytemuck::cast_slice::<_, u16>(bytes))
54+
}
55+
56+
#[derive(Default, Copy, Clone)]
57+
pub struct Metadata<'m>(&'m [u16]);
3958

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

64+
#[allow(dead_code)]
4665
pub fn parser(&self, module: &'m [u8]) -> Parser<'m> {
47-
unsafe { Parser::new(&module[self.read_u32(1) .. self.read_u32(3)]) }
66+
unsafe { Parser::new(&module[self.parser_range()]) }
4867
}
4968

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

73+
pub fn parser_range(&self) -> Range<usize> {
74+
self.read_u32(1) .. self.read_u32(3)
75+
}
76+
5477
fn read_u32(&self, idx: usize) -> usize {
5578
bytemuck::pod_read_unaligned::<u32>(bytemuck::cast_slice(&self.0[idx .. idx + 2])) as usize
5679
}

0 commit comments

Comments
 (0)