2018-10-11 19:29:36 +00:00
|
|
|
pub mod errors;
|
|
|
|
pub mod utils;
|
2018-10-14 20:10:53 +00:00
|
|
|
pub mod module;
|
2018-10-14 11:59:11 +00:00
|
|
|
pub mod memory;
|
|
|
|
pub mod instance;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
2018-10-13 17:22:57 +00:00
|
|
|
use std::str::FromStr;
|
2018-10-13 13:31:56 +00:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use std::panic;
|
2018-10-14 20:23:48 +00:00
|
|
|
use std::ptr;
|
2018-10-11 19:29:36 +00:00
|
|
|
use cranelift_native;
|
2018-10-13 17:22:57 +00:00
|
|
|
use target_lexicon::{self, Triple};
|
2018-10-14 20:23:48 +00:00
|
|
|
use wasmparser;
|
2018-10-13 17:22:57 +00:00
|
|
|
use cranelift_codegen::isa;
|
2018-10-14 20:23:48 +00:00
|
|
|
// use cranelift_codegen::print_errors::pretty_verifier_error;
|
|
|
|
// use cranelift_codegen::verifier;
|
2018-10-13 17:22:57 +00:00
|
|
|
|
2018-10-14 20:10:53 +00:00
|
|
|
pub use self::module::Module;
|
2018-10-14 11:59:11 +00:00
|
|
|
pub use self::instance::Instance;
|
2018-10-11 19:29:36 +00:00
|
|
|
pub use self::errors::{Error, ErrorKind};
|
2018-10-14 20:23:48 +00:00
|
|
|
pub use self::memory::LinearMemory;
|
|
|
|
|
|
|
|
pub struct ResultObject {
|
|
|
|
/// A webassembly::Module object representing the compiled WebAssembly module.
|
|
|
|
/// This Module can be instantiated again
|
|
|
|
pub module: Module,
|
|
|
|
/// A webassembly::Instance object that contains all the Exported WebAssembly
|
|
|
|
/// functions.
|
|
|
|
pub instance: Instance,
|
|
|
|
}
|
2018-10-11 19:29:36 +00:00
|
|
|
|
|
|
|
pub struct ImportObject {
|
|
|
|
}
|
|
|
|
|
2018-10-14 20:10:53 +00:00
|
|
|
/// The webassembly::instantiate() function allows you to compile and
|
2018-10-11 19:29:36 +00:00
|
|
|
/// instantiate WebAssembly code
|
|
|
|
|
|
|
|
/// Params:
|
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
|
|
|
|
/// * `import_object`: An object containing the values to be imported
|
|
|
|
/// into the newly-created Instance, such as functions or
|
2018-10-14 20:10:53 +00:00
|
|
|
/// webassembly::Memory objects. There must be one matching property
|
2018-10-11 19:29:36 +00:00
|
|
|
/// for each declared import of the compiled module or else a
|
2018-10-14 20:10:53 +00:00
|
|
|
/// webassembly::LinkError is thrown.
|
2018-10-11 19:29:36 +00:00
|
|
|
|
|
|
|
/// Errors:
|
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 20:10:53 +00:00
|
|
|
/// webassembly::CompileError, webassembly::LinkError, or
|
|
|
|
/// webassembly::RuntimeError, depending on the cause of the failure.
|
2018-10-14 20:23:48 +00:00
|
|
|
pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>) -> Result<ResultObject, ErrorKind> {
|
|
|
|
let module = compile(buffer_source)?;
|
|
|
|
let instance = Instance::new(&module, ptr::null(), &vec![]);
|
|
|
|
Ok(ResultObject{
|
|
|
|
module,
|
|
|
|
instance
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The webassembly::compile() function compiles a webassembly::Module
|
|
|
|
/// from WebAssembly binary code. This function is useful if it
|
|
|
|
/// is necessary to a compile a module before it can be instantiated
|
|
|
|
/// (otherwise, the webassembly::instantiate() function should be used).
|
|
|
|
|
|
|
|
/// Params:
|
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
|
|
|
|
/// Errors:
|
|
|
|
/// If the operation fails, the Result rejects with a
|
|
|
|
/// webassembly::CompileError.
|
|
|
|
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
|
2018-10-14 19:41:59 +00:00
|
|
|
// TODO: This should be automatically validated when creating the Module
|
2018-10-14 18:37:42 +00:00
|
|
|
if !validate(&buffer_source) {
|
|
|
|
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
|
|
|
}
|
2018-10-14 19:13:19 +00:00
|
|
|
|
2018-10-14 20:23:48 +00:00
|
|
|
let module = Module::from_bytes(buffer_source, triple!("riscv64"), None)?;
|
2018-10-14 19:13:19 +00:00
|
|
|
|
|
|
|
// 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)
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 20:10:53 +00:00
|
|
|
/// The webassembly::validate() function validates a given typed
|
2018-10-11 19:29:36 +00:00
|
|
|
/// array of WebAssembly binary code, returning whether the bytes
|
|
|
|
/// form a valid wasm module (true) or not (false).
|
|
|
|
|
|
|
|
/// Params:
|
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
pub fn validate(buffer_source: &Vec<u8>) -> bool {
|
|
|
|
wasmparser::validate(buffer_source, None)
|
|
|
|
}
|