2018-10-17 14:08:31 +00:00
|
|
|
#![feature(test, libc)]
|
2018-10-16 11:27:26 +00:00
|
|
|
|
|
|
|
extern crate test;
|
2018-10-11 19:29:36 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate error_chain;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate structopt;
|
|
|
|
extern crate cranelift_codegen;
|
2018-10-14 21:48:59 +00:00
|
|
|
extern crate cranelift_entity;
|
2018-10-11 19:29:36 +00:00
|
|
|
extern crate cranelift_native;
|
|
|
|
extern crate cranelift_wasm;
|
2018-10-14 21:48:59 +00:00
|
|
|
extern crate wabt;
|
2018-10-13 17:22:57 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate target_lexicon;
|
2018-10-14 11:59:11 +00:00
|
|
|
extern crate spin;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
2018-10-15 00:48:59 +00:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
// #[macro_use] extern crate log;
|
|
|
|
|
2018-10-17 14:08:31 +00:00
|
|
|
use libc;
|
2018-10-14 21:48:59 +00:00
|
|
|
use std::error::Error;
|
2018-10-11 19:29:36 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
2018-10-14 21:48:59 +00:00
|
|
|
use std::path::PathBuf;
|
2018-10-11 19:29:36 +00:00
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
use wabt::wat2wasm;
|
|
|
|
|
2018-10-16 15:01:47 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
2018-10-14 11:59:11 +00:00
|
|
|
pub mod common;
|
2018-10-17 14:08:31 +00:00
|
|
|
pub mod integrations;
|
2018-10-18 23:29:12 +00:00
|
|
|
pub mod spectests;
|
2018-10-14 21:48:59 +00:00
|
|
|
pub mod webassembly;
|
2018-10-11 19:29:36 +00:00
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(name = "wasmer", about = "WASM execution runtime.")]
|
2018-10-14 21:47:35 +00:00
|
|
|
/// The options for the wasmer Command Line Interface
|
|
|
|
enum CLIOptions {
|
|
|
|
/// Run a WebAssembly file. Formats accepted: wasm, wast
|
|
|
|
#[structopt(name = "run")]
|
2018-10-14 21:48:59 +00:00
|
|
|
Run(Run),
|
2018-10-14 21:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct Run {
|
2018-10-11 19:29:36 +00:00
|
|
|
#[structopt(short = "d", long = "debug")]
|
|
|
|
debug: bool,
|
|
|
|
/// Input file
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
path: PathBuf,
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:48:59 +00:00
|
|
|
/// Read the contents of a file
|
2018-10-11 19:29:36 +00:00
|
|
|
fn read_file_contents(path: PathBuf) -> Result<Vec<u8>, io::Error> {
|
|
|
|
let mut buffer: Vec<u8> = Vec::new();
|
|
|
|
let mut file = File::open(path)?;
|
|
|
|
file.read_to_end(&mut buffer)?;
|
|
|
|
Ok(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a WASM/WAT file
|
2018-10-14 21:48:59 +00:00
|
|
|
fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
|
|
|
let mut wasm_binary: Vec<u8> =
|
|
|
|
read_file_contents(wasm_path).map_err(|err| String::from(err.description()))?;
|
2018-10-11 19:29:36 +00:00
|
|
|
if !webassembly::utils::is_wasm_binary(&wasm_binary) {
|
2018-10-14 21:48:59 +00:00
|
|
|
wasm_binary = wat2wasm(wasm_binary).map_err(|err| String::from(err.description()))?;
|
2018-10-11 19:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 14:45:24 +00:00
|
|
|
let import_object = integrations::generate_libc_env();
|
2018-10-22 10:59:21 +00:00
|
|
|
let webassembly::ResultObject { module, mut instance } =
|
2018-10-17 22:09:04 +00:00
|
|
|
webassembly::instantiate(wasm_binary, import_object)
|
|
|
|
.map_err(|err| String::from(err.description()))?;
|
|
|
|
let func_index = instance
|
|
|
|
.start_func
|
|
|
|
.unwrap_or_else(|| match module.info.exports.get("main") {
|
2018-10-17 14:45:24 +00:00
|
|
|
Some(&webassembly::Export::Function(index)) => index,
|
|
|
|
_ => panic!("Main function not found"),
|
2018-10-17 22:09:04 +00:00
|
|
|
});
|
2018-10-22 10:59:21 +00:00
|
|
|
let main: fn(&webassembly::VmCtx) = get_instance_function!(instance, func_index);
|
2018-10-22 23:15:18 +00:00
|
|
|
let mainn_func_index = match module.info.exports.get("mainn") {
|
|
|
|
Some(&webassembly::Export::Function(index)) => index,
|
|
|
|
_ => panic!("Mainn function not found"),
|
|
|
|
};
|
2018-10-22 23:44:19 +00:00
|
|
|
let mainn: fn(*const usize) -> i32 = get_instance_function!(instance, mainn_func_index);
|
2018-10-22 23:15:18 +00:00
|
|
|
let context = &instance.generate_context();
|
2018-10-22 23:44:19 +00:00
|
|
|
let pointer_context = &context as *const _;
|
2018-10-22 23:15:18 +00:00
|
|
|
// println!("Context ptr {:p}", context);
|
2018-10-22 23:44:19 +00:00
|
|
|
println!("Context ptr {:?}", pointer_context);
|
2018-10-22 23:15:18 +00:00
|
|
|
// println!("Context ptr {:?}", &context as *const _);
|
|
|
|
// println!("Memories ptr {:?}", context.memories.as_ptr());
|
2018-10-22 23:44:19 +00:00
|
|
|
let pointer_tables = &context.tables as *const _;
|
|
|
|
println!("Tables ptr {:?}", pointer_tables);
|
|
|
|
println!("Tables ptr {:p}", pointer_tables);
|
|
|
|
let ref tables_ptr_2 = unsafe { (&*pointer_tables as *const _) };
|
|
|
|
println!("Tables ptr {:?}", tables_ptr_2);
|
|
|
|
println!("DIFF {:?}", (pointer_tables as usize-pointer_context as usize));
|
2018-10-22 23:15:18 +00:00
|
|
|
// println!("Tables ptr {:?}", context.tables.as_ptr());
|
|
|
|
// println!("Tables ptr {:?}", &context.tables as *const _);
|
|
|
|
// println!("Tables ptr {:?}", &context.tables as *const _);
|
|
|
|
// println!("User data ptr {:?}", &context.user_data as *const _);
|
|
|
|
// println!("Globals ptr {:?}", &context.globals as *const _);
|
|
|
|
// println!("Memories ptr {:?}", &context.memories as *const _);
|
|
|
|
// println!("Tables ptr {:?}", &context.tables as *const _);
|
|
|
|
// unsafe {
|
|
|
|
// println!("Tables 0 ptr {:p}", &context.tables.get_unchecked(0));
|
|
|
|
// println!("Tables 0 ptr {:p}", &context.tables.get_unchecked(0).get(0));
|
|
|
|
// }
|
|
|
|
let table_ptr = &context.tables as *const _;
|
|
|
|
// let table: &Ta
|
2018-10-22 19:03:43 +00:00
|
|
|
main(&context);
|
2018-10-22 23:15:18 +00:00
|
|
|
println!("-------------NOW MAINN----------");
|
2018-10-22 23:52:03 +00:00
|
|
|
let res = mainn(pointer_context as *const usize);
|
2018-10-22 23:15:18 +00:00
|
|
|
println!("RESULT {:?}", res);
|
2018-10-11 19:29:36 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:47:35 +00:00
|
|
|
fn run(options: Run) {
|
|
|
|
match execute_wasm(options.path.clone()) {
|
2018-10-11 19:29:36 +00:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(message) => {
|
2018-10-14 21:47:35 +00:00
|
|
|
let name = options.path.as_os_str().to_string_lossy();
|
2018-10-11 19:29:36 +00:00
|
|
|
println!("error while executing {}: {}", name, message);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 21:47:35 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let options = CLIOptions::from_args();
|
|
|
|
match options {
|
|
|
|
CLIOptions::Run(options) => run(options),
|
|
|
|
}
|
|
|
|
}
|