Added Module.verify function

This commit is contained in:
Syrus Akbary
2018-10-15 00:25:11 +02:00
parent 9bca617892
commit e7788645d7
2 changed files with 18 additions and 50 deletions

View File

@ -4,7 +4,6 @@ pub mod memory;
pub mod module; pub mod module;
pub mod utils; pub mod utils;
use cranelift_codegen::isa;
use cranelift_native; use cranelift_native;
use std::panic; use std::panic;
use std::ptr; use std::ptr;
@ -12,8 +11,6 @@ use std::str::FromStr;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use target_lexicon::{self, Triple}; use target_lexicon::{self, Triple};
use wasmparser; use wasmparser;
// use cranelift_codegen::print_errors::pretty_verifier_error;
// use cranelift_codegen::verifier;
pub use self::errors::{Error, ErrorKind}; pub use self::errors::{Error, ErrorKind};
pub use self::instance::Instance; pub use self::instance::Instance;
@ -33,17 +30,14 @@ pub struct ImportObject {}
/// The webassembly::instantiate() function allows you to compile and /// The webassembly::instantiate() function allows you to compile and
/// instantiate WebAssembly code /// instantiate WebAssembly code
/// Params: /// Params:
/// * `buffer_source`: A `Vec<u8>` containing the /// * `buffer_source`: A `Vec<u8>` containing the
/// binary code of the .wasm module you want to compile. /// binary code of the .wasm module you want to compile.
/// * `import_object`: An object containing the values to be imported /// * `import_object`: An object containing the values to be imported
/// into the newly-created Instance, such as functions or /// into the newly-created Instance, such as functions or
/// webassembly::Memory objects. There must be one matching property /// webassembly::Memory objects. There must be one matching property
/// for each declared import of the compiled module or else a /// for each declared import of the compiled module or else a
/// webassembly::LinkError is thrown. /// webassembly::LinkError is thrown.
/// Errors: /// Errors:
/// If the operation fails, the Result rejects with a /// If the operation fails, the Result rejects with a
/// webassembly::CompileError, webassembly::LinkError, or /// webassembly::CompileError, webassembly::LinkError, or
@ -61,11 +55,9 @@ pub fn instantiate(
/// from WebAssembly binary code. This function is useful if it /// from WebAssembly binary code. This function is useful if it
/// is necessary to a compile a module before it can be instantiated /// is necessary to a compile a module before it can be instantiated
/// (otherwise, the webassembly::instantiate() function should be used). /// (otherwise, the webassembly::instantiate() function should be used).
/// Params: /// Params:
/// * `buffer_source`: A `Vec<u8>` containing the /// * `buffer_source`: A `Vec<u8>` containing the
/// binary code of the .wasm module you want to compile. /// binary code of the .wasm module you want to compile.
/// Errors: /// Errors:
/// If the operation fails, the Result rejects with a /// If the operation fails, the Result rejects with a
/// webassembly::CompileError. /// webassembly::CompileError.
@ -77,23 +69,12 @@ pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
let module = Module::from_bytes(buffer_source, triple!("riscv64"), None)?; let module = Module::from_bytes(buffer_source, triple!("riscv64"), None)?;
// let isa = isa::lookup(module.info.triple)
// .unwrap()
// .finish(module.info.flags);
// for func in module.info.function_bodies.values() {
// verifier::verify_function(func, &*isa)
// .map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
// .unwrap();
// };
Ok(module) Ok(module)
} }
/// The webassembly::validate() function validates a given typed /// The webassembly::validate() function validates a given typed
/// array of WebAssembly binary code, returning whether the bytes /// array of WebAssembly binary code, returning whether the bytes
/// form a valid wasm module (true) or not (false). /// form a valid wasm module (true) or not (false).
/// Params: /// Params:
/// * `buffer_source`: A `Vec<u8>` containing the /// * `buffer_source`: A `Vec<u8>` containing the
/// binary code of the .wasm module you want to compile. /// binary code of the .wasm module you want to compile.

View File

@ -8,7 +8,8 @@ use cranelift_codegen::ir::{
self, AbiParam, ArgumentExtension, ArgumentLoc, ArgumentPurpose, ExtFuncData, ExternalName, self, AbiParam, ArgumentExtension, ArgumentLoc, ArgumentPurpose, ExtFuncData, ExternalName,
FuncRef, Function, InstBuilder, Signature, FuncRef, Function, InstBuilder, Signature,
}; };
use cranelift_codegen::settings; use cranelift_codegen::print_errors::pretty_verifier_error;
use cranelift_codegen::{isa, settings, verifier};
use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_entity::{EntityRef, PrimaryMap};
use super::errors::ErrorKind; use super::errors::ErrorKind;
@ -34,9 +35,6 @@ use std::string::String;
use std::vec::Vec; use std::vec::Vec;
use target_lexicon::{PointerWidth, Triple}; use target_lexicon::{PointerWidth, Triple};
// use alloc::vec::Vec;
// use alloc::string::String;
/// Compute a `ir::ExternalName` for a given wasm function index. /// Compute a `ir::ExternalName` for a given wasm function index.
fn get_func_name(func_index: FuncIndex) -> ir::ExternalName { fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
ir::ExternalName::user(0, func_index.index() as u32) ir::ExternalName::user(0, func_index.index() as u32)
@ -44,7 +42,7 @@ fn get_func_name(func_index: FuncIndex) -> ir::ExternalName {
/// A collection of names under which a given entity is exported. /// A collection of names under which a given entity is exported.
pub struct Exportable<T> { pub struct Exportable<T> {
/// A wasm entity. /// An entity.
pub entity: T, pub entity: T,
/// Names under which the entity is exported. /// Names under which the entity is exported.
@ -91,8 +89,6 @@ pub struct ModuleInfo {
pub tables: Vec<Exportable<Table>>, pub tables: Vec<Exportable<Table>>,
/// WebAssembly table initializers. /// WebAssembly table initializers.
// Should be Vec<TableElements>
// instead of Vec<Exportable<TableElements>> ??
pub table_elements: Vec<TableElements>, pub table_elements: Vec<TableElements>,
/// The base of tables. /// The base of tables.
pub tables_base: Option<ir::GlobalValue>, pub tables_base: Option<ir::GlobalValue>,
@ -109,6 +105,7 @@ pub struct ModuleInfo {
/// The start function. /// The start function.
pub start_func: Option<FuncIndex>, pub start_func: Option<FuncIndex>,
/// The data initializers
pub data_initializers: Vec<DataInitializer>, pub data_initializers: Vec<DataInitializer>,
} }
@ -188,30 +185,7 @@ pub struct Module {
} }
impl Module { impl Module {
/// Allocates the data structures with default flags. /// Instantiate a Module given WASM bytecode
// pub fn with_triple(triple: Triple) -> Self {
// Self::with_triple_flags(
// triple,
// settings::Flags::new(settings::builder()),
// ReturnMode::NormalReturns,
// )
// }
/// Allocates the data structures with the given triple.
// pub fn with_triple_flags(
// triple: Triple,
// flags: settings::Flags,
// return_mode: ReturnMode,
// ) -> Self {
// Self {
// info: ModuleInfo::with_triple_flags(triple, flags),
// trans: FuncTranslator::new(),
// func_bytecode_sizes: Vec::new(),
// return_mode,
// }
// }
pub fn from_bytes( pub fn from_bytes(
buffer_source: Vec<u8>, buffer_source: Vec<u8>,
triple: Triple, triple: Triple,
@ -225,6 +199,7 @@ impl Module {
func_bytecode_sizes: Vec::new(), func_bytecode_sizes: Vec::new(),
// return_mode, // return_mode,
}; };
// We iterate through the source bytes, generating the compiled module // We iterate through the source bytes, generating the compiled module
translate_module(&buffer_source, &mut module) translate_module(&buffer_source, &mut module)
.map_err(|e| ErrorKind::CompileError(e.to_string()))?; .map_err(|e| ErrorKind::CompileError(e.to_string()))?;
@ -258,6 +233,18 @@ impl Module {
)) ))
} }
} }
pub fn verify(&self) {
let isa = isa::lookup(self.info.triple.clone())
.unwrap()
.finish(self.info.flags.clone());
for func in self.info.function_bodies.values() {
verifier::verify_function(func, &*isa)
.map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
.unwrap();
}
}
} }
/// The `FuncEnvironment` implementation for use by the `Module`. /// The `FuncEnvironment` implementation for use by the `Module`.