2018-10-11 19:29:36 +00:00
|
|
|
pub mod errors;
|
2018-10-17 14:08:31 +00:00
|
|
|
pub mod import_object;
|
2018-10-14 11:59:11 +00:00
|
|
|
pub mod instance;
|
2018-12-13 20:49:30 +00:00
|
|
|
pub mod libcalls;
|
2018-10-14 21:48:59 +00:00
|
|
|
pub mod memory;
|
|
|
|
pub mod module;
|
2018-10-15 00:48:59 +00:00
|
|
|
pub mod relocation;
|
2018-10-15 01:03:00 +00:00
|
|
|
pub mod utils;
|
2018-12-18 02:30:27 +00:00
|
|
|
pub mod vmcontext;
|
|
|
|
pub mod vmoffsets;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
2018-11-27 04:29:26 +00:00
|
|
|
use cranelift_codegen::{
|
|
|
|
isa,
|
|
|
|
settings::{self, Configurable},
|
|
|
|
};
|
2018-12-16 06:43:10 +00:00
|
|
|
use cranelift_wasm::ModuleEnvironment;
|
2018-12-18 06:23:49 +00:00
|
|
|
use std::io::{self, Write};
|
2018-10-13 13:31:56 +00:00
|
|
|
use std::panic;
|
2018-10-14 21:48:59 +00:00
|
|
|
use std::str::FromStr;
|
2018-11-06 14:51:01 +00:00
|
|
|
use target_lexicon;
|
2018-10-14 20:23:48 +00:00
|
|
|
use wasmparser;
|
2018-11-15 08:50:54 +00:00
|
|
|
use wasmparser::WasmDecoder;
|
2018-10-13 17:22:57 +00:00
|
|
|
|
2018-10-11 19:29:36 +00:00
|
|
|
pub use self::errors::{Error, ErrorKind};
|
2018-11-16 15:55:49 +00:00
|
|
|
pub use self::import_object::{ImportObject, ImportValue};
|
2018-12-15 06:46:11 +00:00
|
|
|
pub use self::instance::{Instance, InstanceABI, InstanceOptions};
|
2018-10-14 20:23:48 +00:00
|
|
|
pub use self::memory::LinearMemory;
|
2018-10-15 11:45:44 +00:00
|
|
|
pub use self::module::{Export, Module, ModuleInfo};
|
2018-10-14 20:23:48 +00:00
|
|
|
|
2018-12-15 06:46:11 +00:00
|
|
|
use crate::apis::emscripten::{allocate_cstr_on_stack, allocate_on_stack, is_emscripten_module};
|
2018-12-11 03:19:46 +00:00
|
|
|
|
2018-10-14 20:23:48 +00:00
|
|
|
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
|
|
|
|
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
|
2018-10-14 21:48:59 +00:00
|
|
|
/// Params:
|
2018-10-11 19:29:36 +00:00
|
|
|
/// * `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:
|
2018-10-14 21:48:59 +00:00
|
|
|
/// 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 21:48:59 +00:00
|
|
|
pub fn instantiate(
|
|
|
|
buffer_source: Vec<u8>,
|
2018-10-17 15:14:35 +00:00
|
|
|
import_object: ImportObject<&str, &str>,
|
2018-12-11 03:19:46 +00:00
|
|
|
options: Option<InstanceOptions>,
|
2018-10-14 21:48:59 +00:00
|
|
|
) -> Result<ResultObject, ErrorKind> {
|
2018-12-11 03:19:46 +00:00
|
|
|
let isa = get_isa();
|
|
|
|
let module = compile(buffer_source)?;
|
2018-11-25 18:51:21 +00:00
|
|
|
|
2018-12-11 05:19:39 +00:00
|
|
|
let abi = if is_emscripten_module(&module) {
|
|
|
|
InstanceABI::Emscripten
|
2018-12-15 06:46:11 +00:00
|
|
|
} else {
|
2018-12-11 05:19:39 +00:00
|
|
|
InstanceABI::None
|
|
|
|
};
|
|
|
|
|
2018-12-11 03:19:46 +00:00
|
|
|
let options = options.unwrap_or_else(|| InstanceOptions {
|
|
|
|
mock_missing_imports: false,
|
|
|
|
mock_missing_globals: false,
|
|
|
|
mock_missing_tables: false,
|
2018-12-15 06:06:47 +00:00
|
|
|
abi,
|
2018-12-11 03:19:46 +00:00
|
|
|
show_progressbar: false,
|
2018-12-15 06:06:47 +00:00
|
|
|
isa,
|
2018-12-11 03:19:46 +00:00
|
|
|
});
|
2018-11-15 07:10:35 +00:00
|
|
|
|
2018-10-15 00:48:59 +00:00
|
|
|
debug!("webassembly - creating instance");
|
2018-12-15 06:46:11 +00:00
|
|
|
let instance = Instance::new(&module, import_object, options)?;
|
2018-10-15 00:48:59 +00:00
|
|
|
debug!("webassembly - instance created");
|
2018-10-14 21:48:59 +00:00
|
|
|
Ok(ResultObject { module, instance })
|
2018-10-14 20:23:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 10:36:43 +00:00
|
|
|
/// The webassembly::instantiate_streaming() function compiles and instantiates
|
2018-10-24 09:56:42 +00:00
|
|
|
/// a WebAssembly module directly from a streamed underlying source.
|
|
|
|
/// This is the most efficient, optimized way to load wasm code.
|
2018-10-24 10:36:43 +00:00
|
|
|
pub fn instantiate_streaming(
|
2018-11-06 14:51:01 +00:00
|
|
|
_buffer_source: Vec<u8>,
|
|
|
|
_import_object: ImportObject<&str, &str>,
|
2018-10-24 09:56:42 +00:00
|
|
|
) -> Result<ResultObject, ErrorKind> {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2018-10-14 20:23:48 +00:00
|
|
|
/// 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).
|
2018-10-14 21:48:59 +00:00
|
|
|
/// Params:
|
2018-10-14 20:23:48 +00:00
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
/// Errors:
|
2018-10-14 21:48:59 +00:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 20:23:48 +00:00
|
|
|
/// 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-11-15 08:50:54 +00:00
|
|
|
debug!("webassembly - validating module");
|
|
|
|
validate_or_error(&buffer_source)?;
|
2018-10-14 21:48:59 +00:00
|
|
|
|
2018-12-11 03:19:46 +00:00
|
|
|
let isa = get_isa();
|
2018-10-14 21:48:59 +00:00
|
|
|
|
2018-10-15 00:48:59 +00:00
|
|
|
debug!("webassembly - creating module");
|
2018-11-15 07:10:35 +00:00
|
|
|
let module = Module::from_bytes(buffer_source, isa.frontend_config())?;
|
2018-10-15 00:48:59 +00:00
|
|
|
debug!("webassembly - module created");
|
2018-10-14 19:13:19 +00:00
|
|
|
|
|
|
|
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).
|
2018-10-14 21:48:59 +00:00
|
|
|
/// Params:
|
2018-11-15 08:50:54 +00:00
|
|
|
/// * `buffer_source`: A `&[u8]` containing the
|
2018-10-11 19:29:36 +00:00
|
|
|
/// binary code of the .wasm module you want to compile.
|
2018-11-15 08:50:54 +00:00
|
|
|
pub fn validate(buffer_source: &[u8]) -> bool {
|
|
|
|
validate_or_error(buffer_source).is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn validate_or_error(bytes: &[u8]) -> Result<(), ErrorKind> {
|
|
|
|
let mut parser = wasmparser::ValidatingParser::new(bytes, None);
|
|
|
|
loop {
|
|
|
|
let state = parser.read();
|
|
|
|
match *state {
|
|
|
|
wasmparser::ParserState::EndWasm => return Ok(()),
|
2018-11-15 21:31:37 +00:00
|
|
|
wasmparser::ParserState::Error(err) => {
|
|
|
|
return Err(ErrorKind::CompileError(format!(
|
|
|
|
"Validation error: {}",
|
|
|
|
err.message
|
2018-12-18 02:30:27 +00:00
|
|
|
)));
|
2018-11-15 21:31:37 +00:00
|
|
|
}
|
2018-11-15 08:50:54 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
2018-12-11 00:23:14 +00:00
|
|
|
|
2018-12-11 03:19:46 +00:00
|
|
|
pub fn get_isa() -> Box<isa::TargetIsa> {
|
|
|
|
let flags = {
|
|
|
|
let mut builder = settings::builder();
|
|
|
|
builder.set("opt_level", "best").unwrap();
|
2018-12-11 00:23:14 +00:00
|
|
|
|
2018-12-20 01:47:51 +00:00
|
|
|
if cfg!(not(test)) {
|
|
|
|
builder.set("enable_verifier", "false").unwrap();
|
|
|
|
}
|
|
|
|
|
2018-12-11 03:19:46 +00:00
|
|
|
let flags = settings::Flags::new(builder);
|
|
|
|
debug_assert_eq!(flags.opt_level(), settings::OptLevel::Best);
|
|
|
|
flags
|
|
|
|
};
|
|
|
|
isa::lookup(triple!("x86_64")).unwrap().finish(flags)
|
|
|
|
}
|
2018-12-11 00:23:14 +00:00
|
|
|
|
2018-12-18 06:23:49 +00:00
|
|
|
fn store_module_arguments(path: &str, args: Vec<&str>, instance: &Instance) -> (u32, u32) {
|
2018-12-11 00:23:14 +00:00
|
|
|
let argc = args.len() + 1;
|
|
|
|
|
2018-12-15 06:46:11 +00:00
|
|
|
let (argv_offset, argv_slice): (_, &mut [u32]) =
|
|
|
|
unsafe { allocate_on_stack(((argc + 1) * 4) as u32, instance) };
|
2018-12-15 06:10:07 +00:00
|
|
|
assert!(!argv_slice.is_empty());
|
2018-12-11 00:23:14 +00:00
|
|
|
|
|
|
|
argv_slice[0] = unsafe { allocate_cstr_on_stack(path, instance).0 };
|
|
|
|
|
|
|
|
for (slot, arg) in argv_slice[1..argc].iter_mut().zip(args.iter()) {
|
|
|
|
*slot = unsafe { allocate_cstr_on_stack(&arg, instance).0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
argv_slice[argc] = 0;
|
|
|
|
|
|
|
|
(argc as u32, argv_offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fn get_module_arguments(options: &Run, instance: &mut webassembly::Instance) -> (u32, u32) {
|
|
|
|
// // Application Arguments
|
|
|
|
// let mut arg_values: Vec<String> = Vec::new();
|
|
|
|
// let mut arg_addrs: Vec<*const u8> = Vec::new();
|
|
|
|
// let arg_length = options.args.len() + 1;
|
|
|
|
|
|
|
|
// arg_values.reserve_exact(arg_length);
|
|
|
|
// arg_addrs.reserve_exact(arg_length);
|
|
|
|
|
|
|
|
// // Push name of wasm file
|
|
|
|
// arg_values.push(format!("{}\0", options.path.to_str().unwrap()));
|
|
|
|
// arg_addrs.push(arg_values[0].as_ptr());
|
|
|
|
|
|
|
|
// // Push additional arguments
|
|
|
|
// for (i, arg) in options.args.iter().enumerate() {
|
|
|
|
// arg_values.push(format!("{}\0", arg));
|
|
|
|
// arg_addrs.push(arg_values[i + 1].as_ptr());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Get argument count and pointer to addresses
|
|
|
|
// let argv = arg_addrs.as_ptr() as *mut *mut i8;
|
|
|
|
// let argc = arg_length as u32;
|
|
|
|
|
|
|
|
// // Copy the the arguments into the wasm memory and get offset
|
|
|
|
// let argv_offset = unsafe {
|
|
|
|
// copy_cstr_array_into_wasm(argc, argv, instance)
|
|
|
|
// };
|
|
|
|
|
|
|
|
// debug!("argc = {:?}", argc);
|
|
|
|
// debug!("argv = {:?}", arg_addrs);
|
|
|
|
|
|
|
|
// (argc, argv_offset)
|
|
|
|
// }
|
|
|
|
|
2018-12-15 06:46:11 +00:00
|
|
|
pub fn start_instance(
|
|
|
|
module: &Module,
|
|
|
|
instance: &mut Instance,
|
|
|
|
path: &str,
|
|
|
|
args: Vec<&str>,
|
|
|
|
) -> Result<(), String> {
|
2018-12-18 06:23:49 +00:00
|
|
|
if let Some(ref emscripten_data) = &instance.emscripten_data {
|
|
|
|
emscripten_data.atinit(module, instance)?;
|
2018-12-11 00:23:14 +00:00
|
|
|
|
|
|
|
let func_index = match module.info.exports.get("_main") {
|
|
|
|
Some(&Export::Function(index)) => index,
|
|
|
|
_ => panic!("_main emscripten function not found"),
|
|
|
|
};
|
|
|
|
|
2018-12-16 06:43:10 +00:00
|
|
|
let sig_index = module.get_func_type(func_index);
|
|
|
|
let signature = module.get_signature(sig_index);
|
|
|
|
let num_params = signature.params.len();
|
2018-12-18 06:23:49 +00:00
|
|
|
let result = match num_params {
|
2018-12-16 06:43:10 +00:00
|
|
|
2 => {
|
|
|
|
let main: extern "C" fn(u32, u32, &Instance) =
|
|
|
|
get_instance_function!(instance, func_index);
|
|
|
|
let (argc, argv) = store_module_arguments(path, args, instance);
|
|
|
|
call_protected!(main(argc, argv, &instance))
|
|
|
|
}
|
|
|
|
0 => {
|
|
|
|
let main: extern "C" fn(&Instance) = get_instance_function!(instance, func_index);
|
|
|
|
call_protected!(main(&instance))
|
|
|
|
}
|
|
|
|
_ => panic!(
|
|
|
|
"The emscripten main function has received an incorrect number of params {}",
|
|
|
|
num_params
|
|
|
|
),
|
|
|
|
}
|
2018-12-18 06:23:49 +00:00
|
|
|
.map_err(|err| format!("{}", err));
|
|
|
|
|
|
|
|
emscripten_data.atexit(module, instance)?;
|
|
|
|
|
|
|
|
result
|
2018-12-11 00:23:14 +00:00
|
|
|
} else {
|
|
|
|
let func_index =
|
|
|
|
instance
|
|
|
|
.start_func
|
|
|
|
.unwrap_or_else(|| match module.info.exports.get("main") {
|
|
|
|
Some(&Export::Function(index)) => index,
|
|
|
|
_ => panic!("Main function not found"),
|
|
|
|
});
|
2018-12-15 07:17:55 +00:00
|
|
|
let main: extern "C" fn(&Instance) = get_instance_function!(instance, func_index);
|
2018-12-15 06:07:14 +00:00
|
|
|
call_protected!(main(&instance)).map_err(|err| format!("{}", err))
|
2018-12-11 00:23:14 +00:00
|
|
|
}
|
|
|
|
}
|