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 empty ValidMode and Prepare #725

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/interpreter/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2025 Google LLC
//
zhouwfang marked this conversation as resolved.
Show resolved Hide resolved
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -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::{validate, 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 = validate::<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
32 changes: 28 additions & 4 deletions crates/interpreter/src/valid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -25,23 +25,47 @@ use crate::toctou::*;
use crate::util::*;
use crate::*;

pub trait ValidMode {}

zhouwfang marked this conversation as resolved.
Show resolved Hide resolved
pub struct Prepare;
impl ValidMode for Prepare {}

impl Default for Prepare {
fn default() -> Self {
zhouwfang marked this conversation as resolved.
Show resolved Hide resolved
Self
}
}

/// Checks whether a WASM module in binary format is valid.
pub fn validate(binary: &[u8]) -> Result<Vec<MetadataEntry>, Error> {
pub fn validate<M: ValidMode + Default>(binary: &[u8]) -> Result<Vec<MetadataEntry>, Error> {
Context::default().check_module(&mut Parser::new(binary))
}

type Parser<'m> = parser::Parser<'m, Check>;
type CheckResult = MResult<(), Check>;

#[derive(Default)]
struct Context<'m> {
zhouwfang marked this conversation as resolved.
Show resolved Hide resolved
struct Context<'m, M: ValidMode = Prepare> {
types: Vec<FuncType<'m>>,
zhouwfang marked this conversation as resolved.
Show resolved Hide resolved
funcs: Vec<TypeIdx>,
tables: Vec<TableType>,
mems: Vec<MemType>,
globals: Vec<GlobalType>,
elems: Vec<RefType>,
datas: Option<usize>,
#[allow(dead_code)]
mode: M,
}
ia0 marked this conversation as resolved.
Show resolved Hide resolved

impl<'m, M: ValidMode + Default> Context<'m, M> {
fn new(mode: M) -> Self {
Self { mode, ..Default::default() }
}
}

impl<'m, M: ValidMode + Default> Default for Context<'m, M> {
fn default() -> Self {
Self::new(M::default())
}
}

impl<'m> Context<'m> {
Expand Down
Loading