From 76f6d3c33c39208aba5e9ca0b1a3caaef1c88dc0 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 11 Jan 2019 17:10:21 +0100 Subject: [PATCH 01/17] Fix emscripten imports --- Cargo.lock | 21 +- lib/clif-backend/Cargo.toml | 2 +- lib/emscripten/Cargo.toml | 3 +- lib/emscripten/src/lib.rs | 1891 +++++++++++++++++++---------------- lib/runtime/Cargo.toml | 2 +- lib/runtime/src/lib.rs | 2 +- lib/runtime/src/module.rs | 2 +- 7 files changed, 1026 insertions(+), 897 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5559398d..14e6fff66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1097,16 +1097,16 @@ dependencies = [ "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmer-clif-backend 0.1.0", - "wasmer-emscripten 0.1.1", - "wasmer-runtime 0.1.0", + "wasmer-clif-backend 0.1.4", + "wasmer-emscripten 0.1.4", + "wasmer-runtime 0.1.4", "wasmparser 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasmer-clif-backend" -version = "0.1.0" +version = "0.1.4" dependencies = [ "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "cranelift-codegen 0.26.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1115,26 +1115,27 @@ dependencies = [ "cranelift-wasm 0.26.0 (registry+https://github.com/rust-lang/crates.io-index)", "hashbrown 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "target-lexicon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmer-runtime 0.1.0", + "wasmer-runtime 0.1.4", "wasmparser 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "wasmer-emscripten" -version = "0.1.1" +version = "0.1.4" dependencies = [ "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hashbrown 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.44 (git+https://github.com/rust-lang/libc)", "time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmer-clif-backend 0.1.0", - "wasmer-runtime 0.1.0", + "wasmer-clif-backend 0.1.4", + "wasmer-runtime 0.1.4", ] [[package]] name = "wasmer-runtime" -version = "0.1.0" +version = "0.1.4" dependencies = [ "errno 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "field-offset 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1143,7 +1144,7 @@ dependencies = [ "nix 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "page_size 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", - "wasmer-clif-backend 0.1.0", + "wasmer-clif-backend 0.1.4", ] [[package]] diff --git a/lib/clif-backend/Cargo.toml b/lib/clif-backend/Cargo.toml index 8f126868b..0698f9971 100644 --- a/lib/clif-backend/Cargo.toml +++ b/lib/clif-backend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasmer-clif-backend" -version = "0.1.0" +version = "0.1.4" authors = [ "Lachlan Sneff ", "Steve Akinyemi ", diff --git a/lib/emscripten/Cargo.toml b/lib/emscripten/Cargo.toml index 982eea73b..6f0e502c4 100644 --- a/lib/emscripten/Cargo.toml +++ b/lib/emscripten/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasmer-emscripten" -version = "0.1.1" +version = "0.1.4" authors = [ "Lachlan Sneff ", "Steve Akinyemi ", @@ -12,6 +12,7 @@ edition = "2018" build = "build/mod.rs" [dependencies] +hashbrown = "0.1" wasmer-runtime = { path = "../runtime" } libc = { git = "https://github.com/rust-lang/libc" } byteorder = "1" diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 6d8a7fe67..167fa7844 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -1,14 +1,22 @@ #[macro_use] extern crate wasmer_runtime; -use wasmer_runtime::LinearMemory; -use wasmer_runtime::types::{FuncSig, Type, Value}; -use wasmer_runtime::{Import, Imports, Instance, FuncRef}; -/// NOTE: TODO: These emscripten api implementation only support wasm32 for now because they assume offsets are u32 +use wasmer_runtime::{ + instance::{FuncRef}, + import::{Imports}, + export::{Export, Context}, + types::{ + FuncSig, Type, Value, + GlobalDesc, + }, + vm::{self, LocalGlobal}, + memory::LinearMemory, +}; use byteorder::{ByteOrder, LittleEndian}; use libc::c_int; use std::cell::UnsafeCell; use std::mem; +use hashbrown::{hash_map::Entry, HashMap}; // EMSCRIPTEN APIS mod env; @@ -54,14 +62,14 @@ fn dynamictop_ptr(static_bump: u32) -> u32 { static_bump + DYNAMICTOP_PTR_DIFF } -pub struct EmscriptenData { - pub malloc: extern "C" fn(i32, &Instance) -> u32, - pub free: extern "C" fn(i32, &mut Instance), - pub memalign: extern "C" fn(u32, u32, &mut Instance) -> u32, - pub memset: extern "C" fn(u32, i32, u32, &mut Instance) -> u32, - pub stack_alloc: extern "C" fn(u32, &Instance) -> u32, - pub jumps: Vec>, -} +//pub struct EmscriptenData { +// pub malloc: extern "C" fn(i32, &Instance) -> u32, +// pub free: extern "C" fn(i32, &mut Instance), +// pub memalign: extern "C" fn(u32, u32, &mut Instance) -> u32, +// pub memset: extern "C" fn(u32, i32, u32, &mut Instance) -> u32, +// pub stack_alloc: extern "C" fn(u32, &Instance) -> u32, +// pub jumps: Vec>, +//} pub fn emscripten_set_up_memory(memory: &mut LinearMemory) { let dynamictop_ptr = dynamictop_ptr(STATIC_BUMP) as usize; @@ -85,7 +93,7 @@ pub fn emscripten_set_up_memory(memory: &mut LinearMemory) { macro_rules! mock_external { ($import:ident, $name:ident) => {{ use wasmer_runtime::types::{FuncSig, Type}; - use wasmer_runtime::Import; + use wasmer_runtime::import::Import; extern "C" fn _mocked_fn() -> i32 { debug!("emscripten::{} ", stringify!($name)); -1 @@ -104,1116 +112,1235 @@ macro_rules! mock_external { }}; } +struct EmscriptenGlobals { + pub data: Vec<(String, LocalGlobal, GlobalDesc)>, +} + +impl EmscriptenGlobals { + fn new() -> Self { + let mut data = Vec::new(); + + data.push(( + "STACKTOP".into(), + LocalGlobal { data: stacktop(STATIC_BUMP) as _ }, + GlobalDesc { mutable: false, ty: Type::I32 }), + ); + + data.push(( + "DYNAMICTOP_PTR", + LocalGlobal { data: dynamictop_ptr(STATIC_BUMP) as _ }, + GlobalDesc { mutable: false, ty: Type::I32 }), + ); + + data.push(( + "Infinity", + LocalGlobal { data: std::f64::INFINITY.to_bits() }, + GlobalDesc { mutable: false, ty: Type::F64 }, + )); + + data.push(( + "NaN", + LocalGlobal { data: std::f64::NAN.to_bits() }, + GlobalDesc { mutable: false, ty: Type::F64 }, + )); + + data.push(( + "tableBase", + LocalGlobal { data: 0 }, + GlobalDesc { mutable: false, ty: Type::I32 }, + )); + + Self { + data, + } + } +} + + pub fn generate_emscripten_env() -> Imports { - let mut import_object = Imports::new(); + use Type::*; - // import_object.add( - // "spectest".to_string(), - // "print_i32".to_string(), - // Import::Func( - // print_i32 as _, - // FuncSig { - // params: vec![Type::I32], - // returns: vec![], - // }, - // ), - // ); - // - // import_object.add( - // "spectest".to_string(), - // "global_i32".to_string(), - // Import::Global(Value::I64(GLOBAL_I32 as _)), - // ); - // Globals - import_object.add( - "env".to_string(), - "STACKTOP".to_string(), - Import::Global(Value::I64(stacktop(STATIC_BUMP) as _)), - ); - import_object.add( - "env".to_string(), - "STACK_MAX".to_string(), - Import::Global(Value::I64(stack_max(STATIC_BUMP) as _)), - ); - import_object.add( - "env".to_string(), - "DYNAMICTOP_PTR".to_string(), - Import::Global(Value::I64(dynamictop_ptr(STATIC_BUMP) as _)), - ); - import_object.add( - "global".to_string(), - "Infinity".to_string(), - Import::Global(Value::I64(std::f64::INFINITY.to_bits() as _)), - ); - import_object.add( - "global".to_string(), - "NaN".to_string(), - Import::Global(Value::I64(std::f64::NAN.to_bits() as _)), - ); - import_object.add( - "env".to_string(), - "tableBase".to_string(), - Import::Global(Value::I64(0)), - ); - // // Print functions + let mut imports = Imports::new(); + let mut env_imports= HashMap::new(); + let em_globals = EmscriptenGlobals::new(); - import_object.add( - "env".to_string(), - "printf".to_string(), - Import::Func( - unsafe { FuncRef::new(io::printf as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], - }, - ), + // Add globals. + for (name, ref global, desc) in em_globals.data { + let export = Export::Global { + local: global as *mut _, + global: desc, + }; + + imports.register_export("env", name, export); + } + + // Print functions + imports.register_export( + "env", + "printf", + Export::Function { + func: unsafe { FuncRef::new(io::printf as *const _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], + } + }, ); - import_object.add( - "env".to_string(), - "putchar".to_string(), - Import::Func( - unsafe { FuncRef::new(io::putchar as _) }, - FuncSig { - params: vec![Type::I32], + + imports.register_export( + "env", + "printf", + Export::Function { + func: unsafe { FuncRef::new(io::printf as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], + }, + }, + ); + + imports.register_export( + "env", + "putchar", + Export::Function { + func: unsafe { FuncRef::new(io::putchar as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - // // Lock - import_object.add( - "env".to_string(), - "___lock".to_string(), - Import::Func( - unsafe { FuncRef::new(lock::___lock as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], + // Lock + imports.register_export( + "env", + "___lock", + Export::Function { + func: unsafe { FuncRef::new(lock::___lock as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___unlock".to_string(), - Import::Func( - unsafe { FuncRef::new(lock::___unlock as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], + imports.register_export( + "env", + "___unlock", + Export::Function { + func: unsafe { FuncRef::new(lock::___unlock as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___wait".to_string(), - Import::Func( - unsafe { FuncRef::new(lock::___wait as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], + imports.register_export( + "env", + "___wait", + Export::Function { + func: unsafe { FuncRef::new(lock::___wait as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], returns: vec![], }, - ), + }, ); - // // Env - import_object.add( - "env".to_string(), - "_getenv".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_getenv as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + // Env + imports.register_export( + "env", + "_getenv", + Export::Function { + func: unsafe { FuncRef::new(env::_getenv as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_setenv".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_setenv as _) }, - FuncSig { - params: vec![Type::I32, Type::I32, Type::I32], + imports.register_export( + "env", + "_setenv", + Export::Function { + func: unsafe { FuncRef::new(env::_setenv as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32, I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_putenv".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_putenv as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "_putenv", + Export::Function { + func: unsafe { FuncRef::new(env::_putenv as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_unsetenv".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_unsetenv as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "_unsetenv", + Export::Function { + func: unsafe { FuncRef::new(env::_unsetenv as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_getpwnam".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_getpwnam as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_getpwnam", + Export::Function { + func: unsafe { FuncRef::new(env::_getpwnam as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_getgrnam".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_getgrnam as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_getgrnam", + Export::Function { + func: unsafe { FuncRef::new(env::_getgrnam as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___buildEnvironment".to_string(), - Import::Func( - unsafe { FuncRef::new(env::___build_environment as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "___buildEnvironment", + Export::Function { + func: unsafe { FuncRef::new(env::___build_environment as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - // // Errno - import_object.add( - "env".to_string(), - "___setErrNo".to_string(), - Import::Func( - unsafe { FuncRef::new(errno::___seterrno as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + // Errno + imports.register_export( + "env", + "___setErrNo", + Export::Function { + func: unsafe { FuncRef::new(errno::___seterrno as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - // // Syscalls - import_object.add( - "env".to_string(), - "___syscall1".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall1 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], + // Syscalls + imports.register_export( + "env", + "___syscall1", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall1 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall3".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall3 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall3", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall3 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall4".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall4 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall4", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall4 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall5".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall5 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall5", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall5 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall6".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall6 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall6", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall6 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall12".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall12 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall12", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall12 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall20".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall20 as _) }, - FuncSig { + imports.register_export( + "env", + "___syscall20", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall20 as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall39".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall39 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall39", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall39 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall40".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall40 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall40", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall40 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall54".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall54 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall54", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall54 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall57".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall57 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall57", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall57 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall63".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall63 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall63", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall63 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall64".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall64 as _) }, - FuncSig { + imports.register_export( + "env", + "___syscall64", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall64 as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall102".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall102 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall102", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall102 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall114".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall114 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall114", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall114 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall122".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall122 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall122", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall122 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall140".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall140 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall140", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall140 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall142".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall142 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall142", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall142 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall145".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall145 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall145", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall145 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall146".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall146 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall146", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall146 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall180".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall180 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall180", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall180 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall181".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall181 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall181", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall181 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall192".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall192 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall192", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall192 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall195".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall195 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall195", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall195 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall197".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall197 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall197", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall197 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall201".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall201 as _) }, - FuncSig { + imports.register_export( + "env", + "___syscall201", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall201 as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall202".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall202 as _) }, - FuncSig { + imports.register_export( + "env", + "___syscall202", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall202 as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall212".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall212 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall212", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall212 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall221".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall221 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall221", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall221 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall330".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall330 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall330", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall330 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___syscall340".to_string(), - Import::Func( - unsafe { FuncRef::new(syscalls::___syscall340 as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___syscall340", + Export::Function { + func: unsafe { FuncRef::new(syscalls::___syscall340 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - // // Process - import_object.add( - "env".to_string(), - "abort".to_string(), - Import::Func( - unsafe { FuncRef::new(process::em_abort as _) }, - FuncSig { - params: vec![Type::I32], + // Process + imports.register_export( + "env", + "abort", + Export::Function { + func: unsafe { FuncRef::new(process::em_abort as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_abort".to_string(), - Import::Func( - unsafe { FuncRef::new(process::_abort as _) }, - FuncSig { + imports.register_export( + "env", + "_abort", + Export::Function { + func: unsafe { FuncRef::new(process::_abort as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "abortStackOverflow".to_string(), - Import::Func( - unsafe { FuncRef::new(process::abort_stack_overflow as _) }, - FuncSig { + imports.register_export( + "env", + "abortStackOverflow", + Export::Function { + func: unsafe { FuncRef::new(process::abort_stack_overflow as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_llvm_trap".to_string(), - Import::Func( - unsafe { FuncRef::new(process::_llvm_trap as _) }, - FuncSig { + imports.register_export( + "env", + "_llvm_trap", + Export::Function { + func: unsafe { FuncRef::new(process::_llvm_trap as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_fork".to_string(), - Import::Func( - unsafe { FuncRef::new(process::_fork as _) }, - FuncSig { + imports.register_export( + "env", + "_fork", + Export::Function { + func: unsafe { FuncRef::new(process::_fork as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_exit".to_string(), - Import::Func( - unsafe { FuncRef::new(process::_exit as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "_exit", + Export::Function { + func: unsafe { FuncRef::new(process::_exit as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_system".to_string(), - Import::Func( - unsafe { FuncRef::new(process::_system as _) }, - FuncSig { + imports.register_export( + "env", + "_system", + Export::Function { + func: unsafe { FuncRef::new(process::_system as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_popen".to_string(), - Import::Func( - unsafe { FuncRef::new(process::_popen as _) }, - FuncSig { + imports.register_export( + "env", + "_popen", + Export::Function { + func: unsafe { FuncRef::new(process::_popen as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - // // Signal - import_object.add( - "env".to_string(), - "_sigemptyset".to_string(), - Import::Func( - unsafe { FuncRef::new(signal::_sigemptyset as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + // Signal + imports.register_export( + "env", + "_sigemptyset", + Export::Function { + func: unsafe { FuncRef::new(signal::_sigemptyset as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_sigaddset".to_string(), - Import::Func( - unsafe { FuncRef::new(signal::_sigaddset as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_sigaddset", + Export::Function { + func: unsafe { FuncRef::new(signal::_sigaddset as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_sigprocmask".to_string(), - Import::Func( - unsafe { FuncRef::new(signal::_sigprocmask as _) }, - FuncSig { + imports.register_export( + "env", + "_sigprocmask", + Export::Function { + func: unsafe { FuncRef::new(signal::_sigprocmask as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_sigaction".to_string(), - Import::Func( - unsafe { FuncRef::new(signal::_sigaction as _) }, - FuncSig { - params: vec![Type::I32, Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_sigaction", + Export::Function { + func: unsafe { FuncRef::new(signal::_sigaction as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_signal".to_string(), - Import::Func( - unsafe { FuncRef::new(signal::_signal as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_signal", + Export::Function { + func: unsafe { FuncRef::new(signal::_signal as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - // // Memory - import_object.add( - "env".to_string(), - "abortOnCannotGrowMemory".to_string(), - Import::Func( - unsafe { FuncRef::new(memory::abort_on_cannot_grow_memory as _) }, - FuncSig { + // Memory + imports.register_export( + "env", + "abortOnCannotGrowMemory", + Export::Function { + func: unsafe { FuncRef::new(memory::abort_on_cannot_grow_memory as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_emscripten_memcpy_big".to_string(), - Import::Func( - unsafe { FuncRef::new(memory::_emscripten_memcpy_big as _) }, - FuncSig { - params: vec![Type::I32, Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_emscripten_memcpy_big", + Export::Function { + func: unsafe { FuncRef::new(memory::_emscripten_memcpy_big as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "enlargeMemory".to_string(), - Import::Func( - unsafe { FuncRef::new(memory::enlarge_memory as _) }, - FuncSig { + imports.register_export( + "env", + "enlargeMemory", + Export::Function { + func: unsafe { FuncRef::new(memory::enlarge_memory as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "getTotalMemory".to_string(), - Import::Func( - unsafe { FuncRef::new(memory::get_total_memory as _) }, - FuncSig { + imports.register_export( + "env", + "getTotalMemory", + Export::Function { + func: unsafe { FuncRef::new(memory::get_total_memory as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___map_file".to_string(), - Import::Func( - unsafe { FuncRef::new(memory::___map_file as _) }, - FuncSig { + imports.register_export( + "env", + "___map_file", + Export::Function { + func: unsafe { FuncRef::new(memory::___map_file as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - // // Exception - import_object.add( - "env".to_string(), - "___cxa_allocate_exception".to_string(), - Import::Func( - unsafe { FuncRef::new(exception::___cxa_allocate_exception as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + // Exception + imports.register_export( + "env", + "___cxa_allocate_exception", + Export::Function { + func: unsafe { FuncRef::new(exception::___cxa_allocate_exception as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___cxa_allocate_exception".to_string(), - Import::Func( - unsafe { FuncRef::new(exception::___cxa_throw as _) }, - FuncSig { - params: vec![Type::I32, Type::I32, Type::I32], + imports.register_export( + "env", + "___cxa_allocate_exception", + Export::Function { + func: unsafe { FuncRef::new(exception::___cxa_throw as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32, I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___cxa_throw".to_string(), - Import::Func( - unsafe { FuncRef::new(exception::___cxa_throw as _) }, - FuncSig { - params: vec![Type::I32, Type::I32, Type::I32], + imports.register_export( + "env", + "___cxa_throw", + Export::Function { + func: unsafe { FuncRef::new(exception::___cxa_throw as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32, I32], returns: vec![], }, - ), + }, ); - // // NullFuncs - import_object.add( - "env".to_string(), - "nullFunc_ii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_ii as _) }, - FuncSig { - params: vec![Type::I32], + // NullFuncs + imports.register_export( + "env", + "nullFunc_ii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_ii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_iii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_iii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_iii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_iii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_iiii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_iiii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_iiii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_iiii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_iiiii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_iiiii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_iiiii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_iiiii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_iiiiii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_iiiiii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_iiiiii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_iiiiii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_v".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_v as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_v", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_v as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_vi".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_vi as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_vi", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_vi as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_vii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_vii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_vii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_vii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_viii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_viii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_viii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_viii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_viiii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_viiii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_viiii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_viiii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_viiiii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_viiiii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_viiiii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_viiiii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - import_object.add( - "env".to_string(), - "nullFunc_viiiiii".to_string(), - Import::Func( - unsafe { FuncRef::new(nullfunc::nullfunc_viiiiii as _) }, - FuncSig { - params: vec![Type::I32], + imports.register_export( + "env", + "nullFunc_viiiiii", + Export::Function { + func: unsafe { FuncRef::new(nullfunc::nullfunc_viiiiii as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], returns: vec![], }, - ), + }, ); - // // Time - import_object.add( - "env".to_string(), - "_gettimeofday".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_gettimeofday as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + // Time + imports.register_export( + "env", + "_gettimeofday", + Export::Function { + func: unsafe { FuncRef::new(time::_gettimeofday as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_clock_gettime".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_clock_gettime as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_clock_gettime", + Export::Function { + func: unsafe { FuncRef::new(time::_clock_gettime as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "___clock_gettime".to_string(), - Import::Func( - unsafe { FuncRef::new(time::___clock_gettime as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "___clock_gettime", + Export::Function { + func: unsafe { FuncRef::new(time::___clock_gettime as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_clock".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_clock as _) }, - FuncSig { + imports.register_export( + "env", + "_clock", + Export::Function { + func: unsafe { FuncRef::new(time::_clock as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_difftime".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_difftime as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_difftime", + Export::Function { + func: unsafe { FuncRef::new(time::_difftime as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_asctime".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_asctime as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_asctime", + Export::Function { + func: unsafe { FuncRef::new(time::_asctime as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_asctime_r".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_asctime_r as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_asctime_r", + Export::Function { + func: unsafe { FuncRef::new(time::_asctime_r as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_localtime".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_localtime as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_localtime", + Export::Function { + func: unsafe { FuncRef::new(time::_localtime as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_time".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_time as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_time", + Export::Function { + func: unsafe { FuncRef::new(time::_time as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_strftime".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_strftime as _) }, - FuncSig { - params: vec![Type::I32, Type::I32, Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_strftime", + Export::Function { + func: unsafe { FuncRef::new(time::_strftime as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32, I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_localtime_r".to_string(), - Import::Func( - unsafe { FuncRef::new(time::_localtime_r as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_localtime_r", + Export::Function { + func: unsafe { FuncRef::new(time::_localtime_r as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_getpagesize".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_getpagesize as _) }, - FuncSig { + imports.register_export( + "env", + "_getpagesize", + Export::Function { + func: unsafe { FuncRef::new(env::_getpagesize as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_sysconf".to_string(), - Import::Func( - unsafe { FuncRef::new(env::_sysconf as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "_sysconf", + Export::Function { + func: unsafe { FuncRef::new(env::_sysconf as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - // // Math - import_object.add( - "env".to_string(), - "_llvm_log10_f64".to_string(), - Import::Func( - unsafe { FuncRef::new(math::_llvm_log10_f64 as _) }, - FuncSig { - params: vec![Type::F64], - returns: vec![Type::F64], + // Math + imports.register_export( + "env", + "_llvm_log10_f64", + Export::Function { + func: unsafe { FuncRef::new(math::_llvm_log10_f64 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64], + returns: vec![F64], }, - ), + }, ); - import_object.add( - "env".to_string(), - "_llvm_log2_f64".to_string(), - Import::Func( - unsafe { FuncRef::new( math::_llvm_log2_f64 as _) }, - FuncSig { - params: vec![Type::F64], - returns: vec![Type::F64], + imports.register_export( + "env", + "_llvm_log2_f64", + Export::Function { + func: unsafe { FuncRef::new( math::_llvm_log2_f64 as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64], + returns: vec![F64], }, - ), + }, ); - import_object.add( - "asm2wasm".to_string(), - "f64-rem".to_string(), - Import::Func( - unsafe { FuncRef::new(math::f64_rem as _) }, - FuncSig { - params: vec![Type::F64, Type::F64], - returns: vec![Type::F64], + imports.register_export( + "asm2wasm", + "f64-rem", + Export::Function { + func: unsafe { FuncRef::new(math::f64_rem as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64, F64], + returns: vec![F64], }, - ), + }, ); // - import_object.add( - "env".to_string(), - "__setjmp".to_string(), - Import::Func( - unsafe { FuncRef::new(jmp::__setjmp as _) }, - FuncSig { - params: vec![Type::I32], - returns: vec![Type::I32], + imports.register_export( + "env", + "__setjmp", + Export::Function { + func: unsafe { FuncRef::new(jmp::__setjmp as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32], + returns: vec![I32], }, - ), + }, ); - import_object.add( - "env".to_string(), - "__longjmp".to_string(), - Import::Func( - unsafe { FuncRef::new(jmp::__longjmp as _) }, - FuncSig { - params: vec![Type::I32, Type::I32], + imports.register_export( + "env", + "__longjmp", + Export::Function { + func: unsafe { FuncRef::new(jmp::__longjmp as _) }, + ctx: Context::Internal, + signature: FuncSig { + params: vec![I32, I32], returns: vec![], }, - ), + }, ); mock_external!(import_object, _waitpid); diff --git a/lib/runtime/Cargo.toml b/lib/runtime/Cargo.toml index 0bca28007..5b3d4ccff 100644 --- a/lib/runtime/Cargo.toml +++ b/lib/runtime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasmer-runtime" -version = "0.1.0" +version = "0.1.4" authors = [ "Lachlan Sneff ", "Steve Akinyemi ", diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 7a48de2dd..42d812201 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -9,7 +9,7 @@ pub mod backend; mod backing; pub mod export; pub mod import; -mod instance; +pub mod instance; pub mod memory; mod mmap; pub mod module; diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index 7c40c7ef0..e575689b9 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -39,7 +39,7 @@ pub struct ModuleInner { pub sig_registry: SigRegistry, } -pub struct Module(Rc); +pub struct Module(pub Rc); impl Module { pub(crate) fn new(inner: Rc) -> Self { From 82cb5dd906f6b5a0dfddbc88ae067ef9b06f37d1 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 11 Jan 2019 17:10:36 +0100 Subject: [PATCH 02/17] Ignore intellij settings --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6727387a5..dd8330f3e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/*.rs.bk /artifacts .DS_Store +.idea From ba6828c3d4d947a4ee01981fd013d55995444126 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 11 Jan 2019 17:10:59 +0100 Subject: [PATCH 03/17] Add memory offset addr impl --- lib/runtime/src/instance.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index e9b23a7bd..b102832ab 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -344,7 +344,24 @@ impl Namespace for Instance { // TODO Remove this later, only needed for compilation till emscripten is updated impl Instance { - pub fn memory_offset_addr(&self, _index: usize, _offset: usize) -> *const usize { - unimplemented!("TODO replace this emscripten stub") + pub fn memory_offset_addr(&self, index: usize, offset: usize) -> *const u8 { + // Get vm context. + let vmctx = &self.vmctx; + + // Check if the index specified refers to an imported memory. + if index < self.module.0.imported_memories.len() { + // Get imported memory at specified index. + let memory = unsafe { vmctx.imported_memories.add(index) }; + + // Get the actual address of the specified offset. + return unsafe { (*(*memory).memory).base.add(offset) } + } + + // Index points to a locally-defined memory + // Get local memory at specified index. + let memory = unsafe { vmctx.memories.add(index) }; + + // Get the actual address of the specified offset. + unsafe { (*memory).base.add(offset) } } } From 927f6e41553b6ea0e1196b115e1fb58033a8c288 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 11 Jan 2019 20:47:41 +0100 Subject: [PATCH 04/17] Fix other issues with emscripten integration --- lib/emscripten/src/env.rs | 2 +- lib/emscripten/src/lib.rs | 212 ++++++++++++++++++------------------ lib/emscripten/src/utils.rs | 2 +- src/bin/wasmer.rs | 7 +- src/webassembly/mod.rs | 9 +- 5 files changed, 117 insertions(+), 115 deletions(-) diff --git a/lib/emscripten/src/env.rs b/lib/emscripten/src/env.rs index bd4c4e79f..0900be8d9 100644 --- a/lib/emscripten/src/env.rs +++ b/lib/emscripten/src/env.rs @@ -8,8 +8,8 @@ use std::mem; use std::os::raw::c_char; use super::utils::{allocate_on_stack, copy_cstr_into_wasm, copy_terminated_array_of_cstrs}; -use super::EmscriptenData; use wasmer_runtime::{types::Value, Instance}; +//use super::EmscriptenData; //impl Instance { // pub fn memory_offset_addr(&self, index: usize, offset: usize) -> *const usize { diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 167fa7844..af73fe234 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -6,7 +6,7 @@ use wasmer_runtime::{ import::{Imports}, export::{Export, Context}, types::{ - FuncSig, Type, Value, + FuncSig, Type::*, Value, GlobalDesc, }, vm::{self, LocalGlobal}, @@ -91,23 +91,23 @@ pub fn emscripten_set_up_memory(memory: &mut LinearMemory) { } macro_rules! mock_external { - ($import:ident, $name:ident) => {{ - use wasmer_runtime::types::{FuncSig, Type}; - use wasmer_runtime::import::Import; + ($imports:ident, $name:ident) => {{ extern "C" fn _mocked_fn() -> i32 { debug!("emscripten::{} ", stringify!($name)); -1 } - $import.add( - "env".to_string(), - stringify!($name).to_string(), - Import::Func( - unsafe { FuncRef::new(_mocked_fn as _) }, - FuncSig { + + $imports.register_export( + "env", + stringify!($name), + Export::Function { + func: unsafe { FuncRef::new(_mocked_fn as _) }, + ctx: Context::Internal, + signature: FuncSig { params: vec![], - returns: vec![Type::I32], + returns: vec![I32], }, - ), + }, ); }}; } @@ -123,31 +123,31 @@ impl EmscriptenGlobals { data.push(( "STACKTOP".into(), LocalGlobal { data: stacktop(STATIC_BUMP) as _ }, - GlobalDesc { mutable: false, ty: Type::I32 }), + GlobalDesc { mutable: false, ty: I32 }), ); data.push(( - "DYNAMICTOP_PTR", + "DYNAMICTOP_PTR".into(), LocalGlobal { data: dynamictop_ptr(STATIC_BUMP) as _ }, - GlobalDesc { mutable: false, ty: Type::I32 }), + GlobalDesc { mutable: false, ty: I32 }), ); data.push(( - "Infinity", + "Infinity".into(), LocalGlobal { data: std::f64::INFINITY.to_bits() }, - GlobalDesc { mutable: false, ty: Type::F64 }, + GlobalDesc { mutable: false, ty: F64 }, )); data.push(( - "NaN", + "NaN".into(), LocalGlobal { data: std::f64::NAN.to_bits() }, - GlobalDesc { mutable: false, ty: Type::F64 }, + GlobalDesc { mutable: false, ty: F64 }, )); data.push(( - "tableBase", + "tableBase".into(), LocalGlobal { data: 0 }, - GlobalDesc { mutable: false, ty: Type::I32 }, + GlobalDesc { mutable: false, ty: I32 }, )); Self { @@ -158,21 +158,17 @@ impl EmscriptenGlobals { pub fn generate_emscripten_env() -> Imports { - use Type::*; - - let mut imports = Imports::new(); - let mut env_imports= HashMap::new(); let em_globals = EmscriptenGlobals::new(); // Add globals. - for (name, ref global, desc) in em_globals.data { + for (ref name, ref global, ref desc) in em_globals.data { let export = Export::Global { - local: global as *mut _, - global: desc, + local: unsafe { std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) }, + global: desc.clone(), }; - imports.register_export("env", name, export); + imports.register_export("env", name.clone(), export); } // Print functions @@ -1343,84 +1339,84 @@ pub fn generate_emscripten_env() -> Imports { }, ); - mock_external!(import_object, _waitpid); - mock_external!(import_object, _utimes); - mock_external!(import_object, _usleep); - // mock_external!(import_object, _time); - // mock_external!(import_object, _sysconf); - // mock_external!(import_object, _strftime); - mock_external!(import_object, _sigsuspend); - // mock_external!(import_object, _sigprocmask); - // mock_external!(import_object, _sigemptyset); - // mock_external!(import_object, _sigaddset); - // mock_external!(import_object, _sigaction); - mock_external!(import_object, _setitimer); - mock_external!(import_object, _setgroups); - mock_external!(import_object, _setgrent); - mock_external!(import_object, _sem_wait); - mock_external!(import_object, _sem_post); - mock_external!(import_object, _sem_init); - mock_external!(import_object, _sched_yield); - mock_external!(import_object, _raise); - mock_external!(import_object, _mktime); - // mock_external!(import_object, _localtime_r); - // mock_external!(import_object, _localtime); - mock_external!(import_object, _llvm_stacksave); - mock_external!(import_object, _llvm_stackrestore); - mock_external!(import_object, _kill); - mock_external!(import_object, _gmtime_r); - // mock_external!(import_object, _gettimeofday); - // mock_external!(import_object, _getpagesize); - mock_external!(import_object, _getgrent); - mock_external!(import_object, _getaddrinfo); - // mock_external!(import_object, _fork); - // mock_external!(import_object, _exit); - mock_external!(import_object, _execve); - mock_external!(import_object, _endgrent); - // mock_external!(import_object, _clock_gettime); - mock_external!(import_object, ___syscall97); - mock_external!(import_object, ___syscall91); - mock_external!(import_object, ___syscall85); - mock_external!(import_object, ___syscall75); - mock_external!(import_object, ___syscall66); - // mock_external!(import_object, ___syscall64); - // mock_external!(import_object, ___syscall63); - // mock_external!(import_object, ___syscall60); - // mock_external!(import_object, ___syscall54); - // mock_external!(import_object, ___syscall39); - mock_external!(import_object, ___syscall38); - // mock_external!(import_object, ___syscall340); - mock_external!(import_object, ___syscall334); - mock_external!(import_object, ___syscall300); - mock_external!(import_object, ___syscall295); - mock_external!(import_object, ___syscall272); - mock_external!(import_object, ___syscall268); - // mock_external!(import_object, ___syscall221); - mock_external!(import_object, ___syscall220); - // mock_external!(import_object, ___syscall212); - // mock_external!(import_object, ___syscall201); - mock_external!(import_object, ___syscall199); - // mock_external!(import_object, ___syscall197); - mock_external!(import_object, ___syscall196); - // mock_external!(import_object, ___syscall195); - mock_external!(import_object, ___syscall194); - mock_external!(import_object, ___syscall191); - // mock_external!(import_object, ___syscall181); - // mock_external!(import_object, ___syscall180); - mock_external!(import_object, ___syscall168); - // mock_external!(import_object, ___syscall146); - // mock_external!(import_object, ___syscall145); - // mock_external!(import_object, ___syscall142); - mock_external!(import_object, ___syscall140); - // mock_external!(import_object, ___syscall122); - // mock_external!(import_object, ___syscall102); - // mock_external!(import_object, ___syscall20); - mock_external!(import_object, ___syscall15); - mock_external!(import_object, ___syscall10); - mock_external!(import_object, _dlopen); - mock_external!(import_object, _dlclose); - mock_external!(import_object, _dlsym); - mock_external!(import_object, _dlerror); + mock_external!(imports, _waitpid); + mock_external!(imports, _utimes); + mock_external!(imports, _usleep); + // mock_external!(imports, _time); + // mock_external!(imports, _sysconf); + // mock_external!(imports, _strftime); + mock_external!(imports, _sigsuspend); + // mock_external!(imports, _sigprocmask); + // mock_external!(imports, _sigemptyset); + // mock_external!(imports, _sigaddset); + // mock_external!(imports, _sigaction); + mock_external!(imports, _setitimer); + mock_external!(imports, _setgroups); + mock_external!(imports, _setgrent); + mock_external!(imports, _sem_wait); + mock_external!(imports, _sem_post); + mock_external!(imports, _sem_init); + mock_external!(imports, _sched_yield); + mock_external!(imports, _raise); + mock_external!(imports, _mktime); + // mock_external!(imports, _localtime_r); + // mock_external!(imports, _localtime); + mock_external!(imports, _llvm_stacksave); + mock_external!(imports, _llvm_stackrestore); + mock_external!(imports, _kill); + mock_external!(imports, _gmtime_r); + // mock_external!(imports, _gettimeofday); + // mock_external!(imports, _getpagesize); + mock_external!(imports, _getgrent); + mock_external!(imports, _getaddrinfo); + // mock_external!(imports, _fork); + // mock_external!(imports, _exit); + mock_external!(imports, _execve); + mock_external!(imports, _endgrent); + // mock_external!(imports, _clock_gettime); + mock_external!(imports, ___syscall97); + mock_external!(imports, ___syscall91); + mock_external!(imports, ___syscall85); + mock_external!(imports, ___syscall75); + mock_external!(imports, ___syscall66); + // mock_external!(imports, ___syscall64); + // mock_external!(imports, ___syscall63); + // mock_external!(imports, ___syscall60); + // mock_external!(imports, ___syscall54); + // mock_external!(imports, ___syscall39); + mock_external!(imports, ___syscall38); + // mock_external!(imports, ___syscall340); + mock_external!(imports, ___syscall334); + mock_external!(imports, ___syscall300); + mock_external!(imports, ___syscall295); + mock_external!(imports, ___syscall272); + mock_external!(imports, ___syscall268); + // mock_external!(imports, ___syscall221); + mock_external!(imports, ___syscall220); + // mock_external!(imports, ___syscall212); + // mock_external!(imports, ___syscall201); + mock_external!(imports, ___syscall199); + // mock_external!(imports, ___syscall197); + mock_external!(imports, ___syscall196); + // mock_external!(imports, ___syscall195); + mock_external!(imports, ___syscall194); + mock_external!(imports, ___syscall191); + // mock_external!(imports, ___syscall181); + // mock_external!(imports, ___syscall180); + mock_external!(imports, ___syscall168); + // mock_external!(imports, ___syscall146); + // mock_external!(imports, ___syscall145); + // mock_external!(imports, ___syscall142); + mock_external!(imports, ___syscall140); + // mock_external!(imports, ___syscall122); + // mock_external!(imports, ___syscall102); + // mock_external!(imports, ___syscall20); + mock_external!(imports, ___syscall15); + mock_external!(imports, ___syscall10); + mock_external!(imports, _dlopen); + mock_external!(imports, _dlclose); + mock_external!(imports, _dlsym); + mock_external!(imports, _dlerror); - import_object + imports } diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 2feffa79a..dae8f7b65 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -9,7 +9,7 @@ use std::slice; /// We check if a provided module is an Emscripten generated one pub fn is_emscripten_module(module: &Module) -> bool { for (_, import_name) in &module.imported_functions { - if import_name.name == "_emscripten_memcpy_big" && import_name.module == "env" { + if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { return true; } } diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 52ba9cba9..cb96db87a 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -7,6 +7,7 @@ use std::io::Read; use std::path::PathBuf; use std::process::exit; use std::sync::Arc; +use std::rc::Rc; use structopt::StructOpt; @@ -83,9 +84,11 @@ fn execute_wasm(options: &Run) -> Result<(), String> { let import_object = if abi == webassembly::InstanceABI::Emscripten { wasmer_emscripten::generate_emscripten_env() } else { - wasmer_runtime::Imports::new() + wasmer_runtime::import::Imports::new() }; + let import_object = Rc::new(import_object); + let instance_options = webassembly::InstanceOptions { mock_missing_imports: true, mock_missing_globals: true, @@ -97,7 +100,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> { debug!("webassembly - creating instance"); - let mut instance = module.instantiate(&import_object) + let mut instance = module.instantiate(import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?; webassembly::start_instance( diff --git a/src/webassembly/mod.rs b/src/webassembly/mod.rs index 50c40d82e..34173a9d3 100644 --- a/src/webassembly/mod.rs +++ b/src/webassembly/mod.rs @@ -4,9 +4,12 @@ pub mod relocation; pub mod utils; use wasmer_clif_backend::CraneliftCompiler; -use wasmer_runtime::{backend::Compiler, module::Module}; -use wasmer_runtime; -use wasmer_runtime::{Import, Imports, Instance}; +use wasmer_runtime::{ + backend::Compiler, + module::Module, + import::Imports, + instance::Instance, +}; use cranelift_codegen::{ isa, settings::{self, Configurable}, From fdb0841783f6600c9307e1225a759c33829cca83 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 11 Jan 2019 21:17:28 +0100 Subject: [PATCH 05/17] Fix emscripten globals --- lib/emscripten/src/lib.rs | 10 ++++------ src/bin/wasmer.rs | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index af73fe234..b83fce0cd 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -112,12 +112,12 @@ macro_rules! mock_external { }}; } -struct EmscriptenGlobals { +pub struct EmscriptenGlobals { pub data: Vec<(String, LocalGlobal, GlobalDesc)>, } impl EmscriptenGlobals { - fn new() -> Self { + pub fn new() -> Self { let mut data = Vec::new(); data.push(( @@ -156,13 +156,11 @@ impl EmscriptenGlobals { } } - -pub fn generate_emscripten_env() -> Imports { +pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { let mut imports = Imports::new(); - let em_globals = EmscriptenGlobals::new(); // Add globals. - for (ref name, ref global, ref desc) in em_globals.data { + for (name, global, desc) in &globals.data { let export = Export::Global { local: unsafe { std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) }, global: desc.clone(), diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index cb96db87a..1028dafc5 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -81,8 +81,10 @@ fn execute_wasm(options: &Run) -> Result<(), String> { webassembly::InstanceABI::None }; + let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(); + let import_object = if abi == webassembly::InstanceABI::Emscripten { - wasmer_emscripten::generate_emscripten_env() + wasmer_emscripten::generate_emscripten_env(&emscripten_globals) } else { wasmer_runtime::import::Imports::new() }; From e26590d3704d633c648a593d7a3fd463ffce51a1 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Wed, 16 Jan 2019 00:22:27 +0100 Subject: [PATCH 06/17] Refactor emscripten globals --- lib/emscripten/src/lib.rs | 55 ++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index b83fce0cd..625d4de04 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -118,38 +118,29 @@ pub struct EmscriptenGlobals { impl EmscriptenGlobals { pub fn new() -> Self { - let mut data = Vec::new(); - - data.push(( - "STACKTOP".into(), - LocalGlobal { data: stacktop(STATIC_BUMP) as _ }, - GlobalDesc { mutable: false, ty: I32 }), - ); - - data.push(( - "DYNAMICTOP_PTR".into(), - LocalGlobal { data: dynamictop_ptr(STATIC_BUMP) as _ }, - GlobalDesc { mutable: false, ty: I32 }), - ); - - data.push(( - "Infinity".into(), - LocalGlobal { data: std::f64::INFINITY.to_bits() }, - GlobalDesc { mutable: false, ty: F64 }, - )); - - data.push(( - "NaN".into(), - LocalGlobal { data: std::f64::NAN.to_bits() }, - GlobalDesc { mutable: false, ty: F64 }, - )); - - data.push(( - "tableBase".into(), - LocalGlobal { data: 0 }, - GlobalDesc { mutable: false, ty: I32 }, - )); - + let mut data = vec![ + ( + "STACKTOP".into(), + LocalGlobal { data: stacktop(STATIC_BUMP) as _ }, + GlobalDesc { mutable: false, ty: I32 } + ), + ( + "Infinity".into(), + LocalGlobal { data: std::f64::INFINITY.to_bits() }, + GlobalDesc { mutable: false, ty: F64 }, + ), + ( + "NaN".into(), + LocalGlobal { data: std::f64::NAN.to_bits() }, + GlobalDesc { mutable: false, ty: F64 }, + ), + ( + "tableBase".into(), + LocalGlobal { data: 0 }, + GlobalDesc { mutable: false, ty: I32 }, + ), + ]; + Self { data, } From aa01fd50e95ddf1be9acd1ade6c7eb3b27189beb Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Wed, 16 Jan 2019 12:41:06 +0100 Subject: [PATCH 07/17] Remove memory_offset_addr impl --- lib/runtime/src/instance.rs | 19 +------------------ lib/runtime/src/module.rs | 2 +- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index b102832ab..d7dc60071 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -345,23 +345,6 @@ impl Namespace for Instance { // TODO Remove this later, only needed for compilation till emscripten is updated impl Instance { pub fn memory_offset_addr(&self, index: usize, offset: usize) -> *const u8 { - // Get vm context. - let vmctx = &self.vmctx; - - // Check if the index specified refers to an imported memory. - if index < self.module.0.imported_memories.len() { - // Get imported memory at specified index. - let memory = unsafe { vmctx.imported_memories.add(index) }; - - // Get the actual address of the specified offset. - return unsafe { (*(*memory).memory).base.add(offset) } - } - - // Index points to a locally-defined memory - // Get local memory at specified index. - let memory = unsafe { vmctx.memories.add(index) }; - - // Get the actual address of the specified offset. - unsafe { (*memory).base.add(offset) } + unimplemented!() } } diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index e575689b9..7c40c7ef0 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -39,7 +39,7 @@ pub struct ModuleInner { pub sig_registry: SigRegistry, } -pub struct Module(pub Rc); +pub struct Module(Rc); impl Module { pub(crate) fn new(inner: Rc) -> Self { From 16a67c996a4aefc91f351a806a88a880a3a2761d Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Thu, 17 Jan 2019 15:43:33 +0100 Subject: [PATCH 08/17] FIx socket syacall for Linux --- lib/emscripten/src/syscalls.rs | 58 ++++++++++++---------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/lib/emscripten/src/syscalls.rs b/lib/emscripten/src/syscalls.rs index 0b20c97f8..2010169d8 100644 --- a/lib/emscripten/src/syscalls.rs +++ b/lib/emscripten/src/syscalls.rs @@ -68,6 +68,7 @@ use libc::{ F_GETFD, F_SETFD, SOL_SOCKET, + SO_REUSEADDR, TIOCGWINSZ, }; @@ -336,18 +337,17 @@ pub extern "C" fn ___syscall102( unsafe { ioctl(fd, FIOCLEX); }; - if cfg!(target_os = "darwin") { - type T = u32; - let payload = 1 as *const T as *const c_void; - unsafe { - setsockopt( - fd, - SOL_SOCKET, - SO_NOSIGPIPE, - payload, - mem::size_of::() as socklen_t, - ); - }; + + type T = u32; + let payload = 1 as *const T as *const c_void; + unsafe { + setsockopt( + fd, + SOL_SOCKET, + SO_NOSIGPIPE, + payload, + mem::size_of::() as socklen_t, + ); }; debug!( @@ -364,12 +364,7 @@ pub extern "C" fn ___syscall102( let address: u32 = socket_varargs.get(instance); let address_len: u32 = socket_varargs.get(instance); let address = instance.memory_offset_addr(0, address as usize) as *mut sockaddr; - // unsafe { - // debug!( - // "=> address.sin_family: {:?}, address.sin_port: {:?}, address.sin_addr.s_addr: {:?}", - // (*address).sin_family, (*address).sin_port, (*address).sin_addr.s_addr - // ); - // } + // we convert address as a sockaddr (even if this is incorrect), to bypass the type // issue with libc bind @@ -437,25 +432,13 @@ pub extern "C" fn ___syscall102( (*address_linux).sa_family = (*address).sa_family as u16; (*address_linux).sa_data = (*address).sa_data; }; - // // Debug received address - // unsafe { - // let proper_address = address as *const GuestSockaddrIn; - // debug!( - // "=> address.sin_family: {:?}, address.sin_port: {:?}, address.sin_addr.s_addr: {:?}", - // (*proper_address).sin_family, (*proper_address).sin_port, (*proper_address).sin_addr.s_addr - // ); - // debug!( - // "=> address.sa_family: {:?}", - // (*address).sa_family - // ); - // } + // set_cloexec unsafe { ioctl(fd, FIOCLEX); }; debug!("fd: {}", fd); - // nix::unistd::write(fd, "Hello, World!".as_bytes()).unwrap(); - // nix::unistd::fsync(fd).unwrap(); + fd } 6 => { @@ -515,20 +498,19 @@ pub extern "C" fn ___syscall102( // name: Em passes SO_ACCEPTCONN, but Nginx complains about REUSEADDR // https://github.com/openbsd/src/blob/master/sys/sys/socket.h#L156 // setsockopt (socket: c_int, level: c_int, name: c_int, value: *const c_void, option_len: socklen_t) -> c_int + let socket: i32 = socket_varargs.get(instance); - // SOL_SOCKET = 0xffff in BSD - let level: i32 = 0xffff; + // SOL_SOCKET = 0xffff (BSD, Linux) + let level: i32 = SOL_SOCKET; let _: u32 = socket_varargs.get(instance); - // SO_ACCEPTCONN = 0x4 - let name: i32 = 0x4; + // SO_REUSEADDR = 0x4 (BSD, Linux) + let name: i32 = SO_REUSEADDR; let _: u32 = socket_varargs.get(instance); let value: u32 = socket_varargs.get(instance); let option_len: u32 = socket_varargs.get(instance); let value_addr = instance.memory_offset_addr(0, value as usize) as *mut c_void; // Endian problem let ret = unsafe { setsockopt(socket, level, name, value_addr, option_len) }; - // debug!("option_value = {:?}", unsafe { *(value_addr as *const u32) }); - debug!("=> socketfd: {}, level: {} (SOL_SOCKET/0xffff), name: {} (SO_REUSEADDR/4), value_addr: {:?}, option_len: {} = status: {}", socket, level, name, value_addr, option_len, ret); ret } From 0bf6ce49f6e2835157368ff68e2359f392f08a4c Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Thu, 17 Jan 2019 19:55:25 +0100 Subject: [PATCH 09/17] Update emscripten to latest changes --- lib/clif-backend/src/module.rs | 2 +- lib/emscripten/src/lib.rs | 861 ++++++++++++++++----------------- lib/emscripten/src/utils.rs | 16 +- lib/runtime/src/instance.rs | 2 +- lib/runtime/src/lib.rs | 2 +- lib/runtime/src/macros.rs | 1 + lib/runtime/src/module.rs | 2 +- src/bin/wasmer.rs | 7 +- src/webassembly/mod.rs | 9 +- 9 files changed, 445 insertions(+), 457 deletions(-) diff --git a/lib/clif-backend/src/module.rs b/lib/clif-backend/src/module.rs index 2e5f01dca..98ce1dfa6 100644 --- a/lib/clif-backend/src/module.rs +++ b/lib/clif-backend/src/module.rs @@ -32,7 +32,7 @@ impl FuncResolver for PlaceholderFuncResolver { /// This contains all of the items in a `ModuleInner` except the `func_resolver`. pub struct Module { - module: ModuleInner, + pub module: ModuleInner, } impl Module { diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 625d4de04..a7f734624 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -1,15 +1,17 @@ #[macro_use] extern crate wasmer_runtime; +#[macro_use] +use wasmer_runtime::macros; + use wasmer_runtime::{ - instance::{FuncRef}, - import::{Imports}, - export::{Export, Context}, + import::{Imports, NamespaceMap}, + export::{Export, Context, GlobalPointer, FuncPointer}, types::{ - FuncSig, Type::*, Value, + FuncSig, Type::{self, *}, Value, GlobalDesc, }, - vm::{self, LocalGlobal}, + vm::{self, LocalGlobal, Func}, memory::LinearMemory, }; use byteorder::{ByteOrder, LittleEndian}; @@ -91,17 +93,16 @@ pub fn emscripten_set_up_memory(memory: &mut LinearMemory) { } macro_rules! mock_external { - ($imports:ident, $name:ident) => {{ + ($namespace:ident, $name:ident) => {{ extern "C" fn _mocked_fn() -> i32 { debug!("emscripten::{} ", stringify!($name)); -1 } - $imports.register_export( - "env", + $namespace.insert( stringify!($name), Export::Function { - func: unsafe { FuncRef::new(_mocked_fn as _) }, + func: unsafe { FuncPointer::new(_mocked_fn as _) }, ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -112,35 +113,32 @@ macro_rules! mock_external { }}; } -pub struct EmscriptenGlobals { - pub data: Vec<(String, LocalGlobal, GlobalDesc)>, +macro_rules! func { + ($namespace:ident, $function:ident) => {{ + unsafe { FuncPointer::new($namespace::$function as _) } + }}; } -impl EmscriptenGlobals { +pub struct EmscriptenGlobals<'a> { + pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // > +} + +impl <'a> EmscriptenGlobals<'a> { pub fn new() -> Self { - let mut data = vec![ - ( - "STACKTOP".into(), - LocalGlobal { data: stacktop(STATIC_BUMP) as _ }, - GlobalDesc { mutable: false, ty: I32 } - ), - ( - "Infinity".into(), - LocalGlobal { data: std::f64::INFINITY.to_bits() }, - GlobalDesc { mutable: false, ty: F64 }, - ), - ( - "NaN".into(), - LocalGlobal { data: std::f64::NAN.to_bits() }, - GlobalDesc { mutable: false, ty: F64 }, - ), - ( - "tableBase".into(), - LocalGlobal { data: 0 }, - GlobalDesc { mutable: false, ty: I32 }, - ), - ]; - + let mut data = HashMap::new(); + let mut env_namepace = HashMap::new(); + let mut global_namepace = HashMap::new(); + + env_namepace.insert("STACKTOP", (stacktop(STATIC_BUMP) as _, I32)); + env_namepace.insert("STACK_MAX", (stack_max(STATIC_BUMP) as _, I32)); + env_namepace.insert("DYNAMICTOP_PTR", (dynamictop_ptr(STATIC_BUMP) as _, I32)); + env_namepace.insert("tableBase", (0, I32)); + global_namepace.insert("Infinity", (std::f64::INFINITY.to_bits() as _, F64)); + global_namepace.insert("NaN", (std::f64::NAN.to_bits() as _, F64)); + + data.insert("env", env_namepace); + data.insert("global", global_namepace); + Self { data, } @@ -149,36 +147,32 @@ impl EmscriptenGlobals { pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { let mut imports = Imports::new(); + let mut env_namespace = NamespaceMap::new(); + let mut asm_namespace = NamespaceMap::new(); // Add globals. - for (name, global, desc) in &globals.data { - let export = Export::Global { - local: unsafe { std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) }, - global: desc.clone(), - }; +// for () - imports.register_export("env", name.clone(), export); - } + // for (name, global, desc) in &globals.data { + // let global_ptr = unsafe { + // GlobalPointer::new( + // std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) + // ) + // }; - // Print functions - imports.register_export( - "env", + // let export = Export::Global { + // local: global_ptr, + // global: desc.clone(), + // }; + + // imports.register_export("env", name.clone(), export); + // } + + // Print function + env_namespace.insert( "printf", Export::Function { - func: unsafe { FuncRef::new(io::printf as *const _) }, - ctx: Context::Internal, - signature: FuncSig { - params: vec![I32, I32], - returns: vec![I32], - } - }, - ); - - imports.register_export( - "env", - "printf", - Export::Function { - func: unsafe { FuncRef::new(io::printf as _) }, + func: func!(io, printf), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -187,11 +181,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); - imports.register_export( - "env", + + env_namespace.insert( "putchar", Export::Function { - func: unsafe { FuncRef::new(io::putchar as _) }, + func: func!(io, putchar), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -200,11 +194,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Lock - imports.register_export( - "env", + env_namespace.insert( "___lock", Export::Function { - func: unsafe { FuncRef::new(lock::___lock as _) }, + func: func!(lock, ___lock), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -212,11 +205,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___unlock", Export::Function { - func: unsafe { FuncRef::new(lock::___unlock as _) }, + func: func!(lock, ___unlock), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -224,11 +217,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___wait", Export::Function { - func: unsafe { FuncRef::new(lock::___wait as _) }, + func: func!(lock, ___wait), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -237,11 +230,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Env - imports.register_export( - "env", + env_namespace.insert( "_getenv", Export::Function { - func: unsafe { FuncRef::new(env::_getenv as _) }, + func: func!(env, _getenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -249,11 +241,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_setenv", Export::Function { - func: unsafe { FuncRef::new(env::_setenv as _) }, + func: func!(env, _setenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -261,11 +253,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_putenv", Export::Function { - func: unsafe { FuncRef::new(env::_putenv as _) }, + func: func!(env, _putenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -273,11 +265,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_unsetenv", Export::Function { - func: unsafe { FuncRef::new(env::_unsetenv as _) }, + func: func!(env, _unsetenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -285,11 +277,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_getpwnam", Export::Function { - func: unsafe { FuncRef::new(env::_getpwnam as _) }, + func: func!(env, _getpwnam), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -297,11 +289,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_getgrnam", Export::Function { - func: unsafe { FuncRef::new(env::_getgrnam as _) }, + func: func!(env, _getgrnam), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -309,11 +301,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___buildEnvironment", Export::Function { - func: unsafe { FuncRef::new(env::___build_environment as _) }, + func: func!(env, ___build_environment), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -322,11 +314,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Errno - imports.register_export( - "env", + env_namespace.insert( "___setErrNo", Export::Function { - func: unsafe { FuncRef::new(errno::___seterrno as _) }, + func: func!(errno, ___seterrno), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -335,11 +326,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Syscalls - imports.register_export( - "env", + env_namespace.insert( "___syscall1", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall1 as _) }, + func: func!(syscalls, ___syscall1), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -347,11 +337,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall3", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall3 as _) }, + func: func!(syscalls, ___syscall3), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -359,11 +349,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall4", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall4 as _) }, + func: func!(syscalls, ___syscall4), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -371,11 +361,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall5", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall5 as _) }, + func: func!(syscalls, ___syscall5), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -383,11 +373,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall6", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall6 as _) }, + func: func!(syscalls, ___syscall6), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -395,11 +385,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall12", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall12 as _) }, + func: func!(syscalls, ___syscall12), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -407,11 +397,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall20", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall20 as _) }, + func: func!(syscalls, ___syscall20), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -419,11 +409,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall39", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall39 as _) }, + func: func!(syscalls, ___syscall39), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -431,11 +421,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall40", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall40 as _) }, + func: func!(syscalls, ___syscall40), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -443,11 +433,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall54", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall54 as _) }, + func: func!(syscalls, ___syscall54), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -455,11 +445,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall57", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall57 as _) }, + func: func!(syscalls, ___syscall57), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -467,11 +457,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall63", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall63 as _) }, + func: func!(syscalls, ___syscall63), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -479,11 +469,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall64", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall64 as _) }, + func: func!(syscalls, ___syscall64), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -491,11 +481,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall102", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall102 as _) }, + func: func!(syscalls, ___syscall102), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -503,11 +493,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall114", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall114 as _) }, + func: func!(syscalls, ___syscall114), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -515,11 +505,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall122", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall122 as _) }, + func: func!(syscalls, ___syscall122), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -527,11 +517,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall140", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall140 as _) }, + func: func!(syscalls, ___syscall140), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -539,11 +529,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall142", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall142 as _) }, + func: func!(syscalls, ___syscall142), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -551,11 +541,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall145", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall145 as _) }, + func: func!(syscalls, ___syscall145), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -563,11 +553,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall146", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall146 as _) }, + func: func!(syscalls, ___syscall146), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -575,11 +565,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall180", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall180 as _) }, + func: func!(syscalls, ___syscall180), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -587,11 +577,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall181", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall181 as _) }, + func: func!(syscalls, ___syscall181), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -599,11 +589,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall192", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall192 as _) }, + func: func!(syscalls, ___syscall192), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -611,11 +601,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall195", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall195 as _) }, + func: func!(syscalls, ___syscall195), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -623,11 +613,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall197", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall197 as _) }, + func: func!(syscalls, ___syscall197), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -635,11 +625,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall201", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall201 as _) }, + func: func!(syscalls, ___syscall201), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -647,11 +637,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall202", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall202 as _) }, + func: func!(syscalls, ___syscall202), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -659,11 +649,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall212", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall212 as _) }, + func: func!(syscalls, ___syscall212), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -671,11 +661,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall221", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall221 as _) }, + func: func!(syscalls, ___syscall221), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -683,11 +673,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall330", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall330 as _) }, + func: func!(syscalls, ___syscall330), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -695,11 +685,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall340", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall340 as _) }, + func: func!(syscalls, ___syscall340), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -708,11 +698,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Process - imports.register_export( - "env", + env_namespace.insert( "abort", Export::Function { - func: unsafe { FuncRef::new(process::em_abort as _) }, + func: func!(process, em_abort), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -720,11 +709,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_abort", Export::Function { - func: unsafe { FuncRef::new(process::_abort as _) }, + func: func!(process, _abort), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -732,11 +721,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "abortStackOverflow", Export::Function { - func: unsafe { FuncRef::new(process::abort_stack_overflow as _) }, + func: func!(process, abort_stack_overflow), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -744,11 +733,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_llvm_trap", Export::Function { - func: unsafe { FuncRef::new(process::_llvm_trap as _) }, + func: func!(process, _llvm_trap), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -756,11 +745,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_fork", Export::Function { - func: unsafe { FuncRef::new(process::_fork as _) }, + func: func!(process, _fork), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -768,11 +757,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_exit", Export::Function { - func: unsafe { FuncRef::new(process::_exit as _) }, + func: func!(process, _exit), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -780,11 +769,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_system", Export::Function { - func: unsafe { FuncRef::new(process::_system as _) }, + func: func!(process, _system), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -792,11 +781,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_popen", Export::Function { - func: unsafe { FuncRef::new(process::_popen as _) }, + func: func!(process, _popen), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -805,11 +794,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Signal - imports.register_export( - "env", + env_namespace.insert( "_sigemptyset", Export::Function { - func: unsafe { FuncRef::new(signal::_sigemptyset as _) }, + func: func!(signal, _sigemptyset), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -817,11 +805,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sigaddset", Export::Function { - func: unsafe { FuncRef::new(signal::_sigaddset as _) }, + func: func!(signal, _sigaddset), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -829,11 +817,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sigprocmask", Export::Function { - func: unsafe { FuncRef::new(signal::_sigprocmask as _) }, + func: func!(signal, _sigprocmask), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -841,11 +829,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sigaction", Export::Function { - func: unsafe { FuncRef::new(signal::_sigaction as _) }, + func: func!(signal, _sigaction), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -853,11 +841,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_signal", Export::Function { - func: unsafe { FuncRef::new(signal::_signal as _) }, + func: func!(signal, _signal), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -866,11 +854,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Memory - imports.register_export( - "env", + env_namespace.insert( "abortOnCannotGrowMemory", Export::Function { - func: unsafe { FuncRef::new(memory::abort_on_cannot_grow_memory as _) }, + func: func!(memory, abort_on_cannot_grow_memory), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -878,11 +865,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_emscripten_memcpy_big", Export::Function { - func: unsafe { FuncRef::new(memory::_emscripten_memcpy_big as _) }, + func: func!(memory, _emscripten_memcpy_big), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -890,11 +877,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "enlargeMemory", Export::Function { - func: unsafe { FuncRef::new(memory::enlarge_memory as _) }, + func: func!(memory, enlarge_memory), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -902,11 +889,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "getTotalMemory", Export::Function { - func: unsafe { FuncRef::new(memory::get_total_memory as _) }, + func: func!(memory, get_total_memory), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -914,11 +901,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___map_file", Export::Function { - func: unsafe { FuncRef::new(memory::___map_file as _) }, + func: func!(memory, ___map_file), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -927,11 +914,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Exception - imports.register_export( - "env", + env_namespace.insert( "___cxa_allocate_exception", Export::Function { - func: unsafe { FuncRef::new(exception::___cxa_allocate_exception as _) }, + func: func!(exception, ___cxa_allocate_exception), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -939,11 +925,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___cxa_allocate_exception", Export::Function { - func: unsafe { FuncRef::new(exception::___cxa_throw as _) }, + func: func!(exception, ___cxa_throw), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -951,11 +937,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___cxa_throw", Export::Function { - func: unsafe { FuncRef::new(exception::___cxa_throw as _) }, + func: func!(exception, ___cxa_throw), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -964,11 +950,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // NullFuncs - imports.register_export( - "env", + env_namespace.insert( "nullFunc_ii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_ii as _) }, + func: func!(nullfunc, nullfunc_ii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -976,11 +961,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iii as _) }, + func: func!(nullfunc, nullfunc_iii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -988,11 +973,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iiii as _) }, + func: func!(nullfunc, nullfunc_iiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1000,11 +985,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iiiii as _) }, + func: func!(nullfunc, nullfunc_iiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1012,11 +997,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iiiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iiiiii as _) }, + func: func!(nullfunc, nullfunc_iiiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1024,11 +1009,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_v", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_v as _) }, + func: func!(nullfunc, nullfunc_v), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1036,11 +1021,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_vi", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_vi as _) }, + func: func!(nullfunc, nullfunc_vi), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1048,11 +1033,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_vii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_vii as _) }, + func: func!(nullfunc, nullfunc_vii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1060,11 +1045,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viii as _) }, + func: func!(nullfunc, nullfunc_viii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1072,11 +1057,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viiii as _) }, + func: func!(nullfunc, nullfunc_viiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1084,11 +1069,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viiiii as _) }, + func: func!(nullfunc, nullfunc_viiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1096,11 +1081,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viiiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viiiiii as _) }, + func: func!(nullfunc, nullfunc_viiiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1109,11 +1094,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Time - imports.register_export( - "env", + env_namespace.insert( "_gettimeofday", Export::Function { - func: unsafe { FuncRef::new(time::_gettimeofday as _) }, + func: func!(time, _gettimeofday), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1121,11 +1105,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_clock_gettime", Export::Function { - func: unsafe { FuncRef::new(time::_clock_gettime as _) }, + func: func!(time, _clock_gettime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1133,11 +1117,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___clock_gettime", Export::Function { - func: unsafe { FuncRef::new(time::___clock_gettime as _) }, + func: func!(time, ___clock_gettime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1145,11 +1129,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_clock", Export::Function { - func: unsafe { FuncRef::new(time::_clock as _) }, + func: func!(time, _clock), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -1157,11 +1141,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_difftime", Export::Function { - func: unsafe { FuncRef::new(time::_difftime as _) }, + func: func!(time, _difftime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1169,11 +1153,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_asctime", Export::Function { - func: unsafe { FuncRef::new(time::_asctime as _) }, + func: func!(time, _asctime), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1181,11 +1165,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_asctime_r", Export::Function { - func: unsafe { FuncRef::new(time::_asctime_r as _) }, + func: func!(time, _asctime_r), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1193,11 +1177,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_localtime", Export::Function { - func: unsafe { FuncRef::new(time::_localtime as _) }, + func: func!(time, _localtime), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1205,11 +1189,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_time", Export::Function { - func: unsafe { FuncRef::new(time::_time as _) }, + func: func!(time, _time), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1217,11 +1201,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_strftime", Export::Function { - func: unsafe { FuncRef::new(time::_strftime as _) }, + func: func!(time, _strftime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32, I32], @@ -1229,11 +1213,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_localtime_r", Export::Function { - func: unsafe { FuncRef::new(time::_localtime_r as _) }, + func: func!(time, _localtime_r), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1241,11 +1225,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_getpagesize", Export::Function { - func: unsafe { FuncRef::new(env::_getpagesize as _) }, + func: func!(env, _getpagesize), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -1253,11 +1237,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sysconf", Export::Function { - func: unsafe { FuncRef::new(env::_sysconf as _) }, + func: func!(env, _sysconf), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1265,36 +1249,12 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); + // Math - imports.register_export( - "env", - "_llvm_log10_f64", - Export::Function { - func: unsafe { FuncRef::new(math::_llvm_log10_f64 as _) }, - ctx: Context::Internal, - signature: FuncSig { - params: vec![F64], - returns: vec![F64], - }, - }, - ); - imports.register_export( - "env", - "_llvm_log2_f64", - Export::Function { - func: unsafe { FuncRef::new( math::_llvm_log2_f64 as _) }, - ctx: Context::Internal, - signature: FuncSig { - params: vec![F64], - returns: vec![F64], - }, - }, - ); - imports.register_export( - "asm2wasm", + asm_namespace.insert( "f64-rem", Export::Function { - func: unsafe { FuncRef::new(math::f64_rem as _) }, + func: func!(math, f64_rem), ctx: Context::Internal, signature: FuncSig { params: vec![F64, F64], @@ -1302,12 +1262,36 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); + + env_namespace.insert( + "_llvm_log10_f64", + Export::Function { + func: func!(math, _llvm_log10_f64), + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64], + returns: vec![F64], + }, + }, + ); + + env_namespace.insert( + "_llvm_log2_f64", + Export::Function { + func: func!(math, _llvm_log2_f64), + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64], + returns: vec![F64], + }, + }, + ); + // - imports.register_export( - "env", + env_namespace.insert( "__setjmp", Export::Function { - func: unsafe { FuncRef::new(jmp::__setjmp as _) }, + func: func!(jmp, __setjmp), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1315,11 +1299,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "__longjmp", Export::Function { - func: unsafe { FuncRef::new(jmp::__longjmp as _) }, + func: func!(jmp, __longjmp), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1328,84 +1312,87 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); - mock_external!(imports, _waitpid); - mock_external!(imports, _utimes); - mock_external!(imports, _usleep); - // mock_external!(imports, _time); - // mock_external!(imports, _sysconf); - // mock_external!(imports, _strftime); - mock_external!(imports, _sigsuspend); - // mock_external!(imports, _sigprocmask); - // mock_external!(imports, _sigemptyset); - // mock_external!(imports, _sigaddset); - // mock_external!(imports, _sigaction); - mock_external!(imports, _setitimer); - mock_external!(imports, _setgroups); - mock_external!(imports, _setgrent); - mock_external!(imports, _sem_wait); - mock_external!(imports, _sem_post); - mock_external!(imports, _sem_init); - mock_external!(imports, _sched_yield); - mock_external!(imports, _raise); - mock_external!(imports, _mktime); - // mock_external!(imports, _localtime_r); - // mock_external!(imports, _localtime); - mock_external!(imports, _llvm_stacksave); - mock_external!(imports, _llvm_stackrestore); - mock_external!(imports, _kill); - mock_external!(imports, _gmtime_r); - // mock_external!(imports, _gettimeofday); - // mock_external!(imports, _getpagesize); - mock_external!(imports, _getgrent); - mock_external!(imports, _getaddrinfo); - // mock_external!(imports, _fork); - // mock_external!(imports, _exit); - mock_external!(imports, _execve); - mock_external!(imports, _endgrent); - // mock_external!(imports, _clock_gettime); - mock_external!(imports, ___syscall97); - mock_external!(imports, ___syscall91); - mock_external!(imports, ___syscall85); - mock_external!(imports, ___syscall75); - mock_external!(imports, ___syscall66); - // mock_external!(imports, ___syscall64); - // mock_external!(imports, ___syscall63); - // mock_external!(imports, ___syscall60); - // mock_external!(imports, ___syscall54); - // mock_external!(imports, ___syscall39); - mock_external!(imports, ___syscall38); - // mock_external!(imports, ___syscall340); - mock_external!(imports, ___syscall334); - mock_external!(imports, ___syscall300); - mock_external!(imports, ___syscall295); - mock_external!(imports, ___syscall272); - mock_external!(imports, ___syscall268); - // mock_external!(imports, ___syscall221); - mock_external!(imports, ___syscall220); - // mock_external!(imports, ___syscall212); - // mock_external!(imports, ___syscall201); - mock_external!(imports, ___syscall199); - // mock_external!(imports, ___syscall197); - mock_external!(imports, ___syscall196); - // mock_external!(imports, ___syscall195); - mock_external!(imports, ___syscall194); - mock_external!(imports, ___syscall191); - // mock_external!(imports, ___syscall181); - // mock_external!(imports, ___syscall180); - mock_external!(imports, ___syscall168); - // mock_external!(imports, ___syscall146); - // mock_external!(imports, ___syscall145); - // mock_external!(imports, ___syscall142); - mock_external!(imports, ___syscall140); - // mock_external!(imports, ___syscall122); - // mock_external!(imports, ___syscall102); - // mock_external!(imports, ___syscall20); - mock_external!(imports, ___syscall15); - mock_external!(imports, ___syscall10); - mock_external!(imports, _dlopen); - mock_external!(imports, _dlclose); - mock_external!(imports, _dlsym); - mock_external!(imports, _dlerror); + mock_external!(env_namespace, _waitpid); + mock_external!(env_namespace, _utimes); + mock_external!(env_namespace, _usleep); + // mock_external!(env_namespace, _time); + // mock_external!(env_namespace, _sysconf); + // mock_external!(env_namespace, _strftime); + mock_external!(env_namespace, _sigsuspend); + // mock_external!(env_namespace, _sigprocmask); + // mock_external!(env_namespace, _sigemptyset); + // mock_external!(env_namespace, _sigaddset); + // mock_external!(env_namespace, _sigaction); + mock_external!(env_namespace, _setitimer); + mock_external!(env_namespace, _setgroups); + mock_external!(env_namespace, _setgrent); + mock_external!(env_namespace, _sem_wait); + mock_external!(env_namespace, _sem_post); + mock_external!(env_namespace, _sem_init); + mock_external!(env_namespace, _sched_yield); + mock_external!(env_namespace, _raise); + mock_external!(env_namespace, _mktime); + // mock_external!(env_namespace, _localtime_r); + // mock_external!(env_namespace, _localtime); + mock_external!(env_namespace, _llvm_stacksave); + mock_external!(env_namespace, _llvm_stackrestore); + mock_external!(env_namespace, _kill); + mock_external!(env_namespace, _gmtime_r); + // mock_external!(env_namespace, _gettimeofday); + // mock_external!(env_namespace, _getpagesize); + mock_external!(env_namespace, _getgrent); + mock_external!(env_namespace, _getaddrinfo); + // mock_external!(env_namespace, _fork); + // mock_external!(env_namespace, _exit); + mock_external!(env_namespace, _execve); + mock_external!(env_namespace, _endgrent); + // mock_external!(env_namespace, _clock_gettime); + mock_external!(env_namespace, ___syscall97); + mock_external!(env_namespace, ___syscall91); + mock_external!(env_namespace, ___syscall85); + mock_external!(env_namespace, ___syscall75); + mock_external!(env_namespace, ___syscall66); + // mock_external!(env_namespace, ___syscall64); + // mock_external!(env_namespace, ___syscall63); + // mock_external!(env_namespace, ___syscall60); + // mock_external!(env_namespace, ___syscall54); + // mock_external!(env_namespace, ___syscall39); + mock_external!(env_namespace, ___syscall38); + // mock_external!(env_namespace, ___syscall340); + mock_external!(env_namespace, ___syscall334); + mock_external!(env_namespace, ___syscall300); + mock_external!(env_namespace, ___syscall295); + mock_external!(env_namespace, ___syscall272); + mock_external!(env_namespace, ___syscall268); + // mock_external!(env_namespace, ___syscall221); + mock_external!(env_namespace, ___syscall220); + // mock_external!(env_namespace, ___syscall212); + // mock_external!(env_namespace, ___syscall201); + mock_external!(env_namespace, ___syscall199); + // mock_external!(env_namespace, ___syscall197); + mock_external!(env_namespace, ___syscall196); + // mock_external!(env_namespace, ___syscall195); + mock_external!(env_namespace, ___syscall194); + mock_external!(env_namespace, ___syscall191); + // mock_external!(env_namespace, ___syscall181); + // mock_external!(env_namespace, ___syscall180); + mock_external!(env_namespace, ___syscall168); + // mock_external!(env_namespace, ___syscall146); + // mock_external!(env_namespace, ___syscall145); + // mock_external!(env_namespace, ___syscall142); + mock_external!(env_namespace, ___syscall140); + // mock_external!(env_namespace, ___syscall122); + // mock_external!(env_namespace, ___syscall102); + // mock_external!(env_namespace, ___syscall20); + mock_external!(env_namespace, ___syscall15); + mock_external!(env_namespace, ___syscall10); + mock_external!(env_namespace, _dlopen); + mock_external!(env_namespace, _dlclose); + mock_external!(env_namespace, _dlsym); + mock_external!(env_namespace, _dlerror); + + imports.register("env", env_namespace); + imports.register("asm2wasm", asm_namespace); imports } diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index dae8f7b65..84e4feb8a 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -6,14 +6,16 @@ use std::ffi::CStr; use std::mem::size_of; use std::os::raw::c_char; use std::slice; +use std::sync::Arc; /// We check if a provided module is an Emscripten generated one -pub fn is_emscripten_module(module: &Module) -> bool { - for (_, import_name) in &module.imported_functions { - if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { - return true; - } - } - false +pub fn is_emscripten_module(module: &Arc) -> bool { + // for (_, import_name) in &module.imported_functions { + // if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { + // return true; + // } + // } + // false + true } pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance: &Instance) -> u32 { diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index d7dc60071..f4d64712f 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -24,7 +24,7 @@ pub(crate) struct InstanceInner { } pub struct Instance { - pub(crate) module: Rc, + pub module: Rc, inner: Box, #[allow(dead_code)] imports: Box, diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 42d812201..37d650262 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -3,7 +3,7 @@ extern crate field_offset; #[macro_use] -mod macros; +pub mod macros; #[doc(hidden)] pub mod backend; mod backing; diff --git a/lib/runtime/src/macros.rs b/lib/runtime/src/macros.rs index fe2bd1a7f..c27b1e8e0 100644 --- a/lib/runtime/src/macros.rs +++ b/lib/runtime/src/macros.rs @@ -1,3 +1,4 @@ +#[macro_export] macro_rules! debug { ($fmt:expr) => (if cfg!(any(debug_assertions, feature="debug")) { println!(concat!("wasmer-runtime(:{})::", $fmt), line!()) }); ($fmt:expr, $($arg:tt)*) => (if cfg!(any(debug_assertions, feature="debug")) { println!(concat!("wasmer-runtime(:{})::", $fmt, "\n"), line!(), $($arg)*) }); diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index 7c40c7ef0..e575689b9 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -39,7 +39,7 @@ pub struct ModuleInner { pub sig_registry: SigRegistry, } -pub struct Module(Rc); +pub struct Module(pub Rc); impl Module { pub(crate) fn new(inner: Rc) -> Self { diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 1028dafc5..220eae9bd 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -83,26 +83,23 @@ fn execute_wasm(options: &Run) -> Result<(), String> { let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(); - let import_object = if abi == webassembly::InstanceABI::Emscripten { + let mut import_object = if abi == webassembly::InstanceABI::Emscripten { wasmer_emscripten::generate_emscripten_env(&emscripten_globals) } else { wasmer_runtime::import::Imports::new() }; - let import_object = Rc::new(import_object); - let instance_options = webassembly::InstanceOptions { mock_missing_imports: true, mock_missing_globals: true, mock_missing_tables: true, abi: abi, show_progressbar: true, -// isa: isa, }; debug!("webassembly - creating instance"); - let mut instance = module.instantiate(import_object) + let mut instance = module.instantiate(&mut import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?; webassembly::start_instance( diff --git a/src/webassembly/mod.rs b/src/webassembly/mod.rs index 34173a9d3..05c516f7a 100644 --- a/src/webassembly/mod.rs +++ b/src/webassembly/mod.rs @@ -6,7 +6,7 @@ pub mod utils; use wasmer_clif_backend::CraneliftCompiler; use wasmer_runtime::{ backend::Compiler, - module::Module, + module::{Module, ModuleInner}, import::Imports, instance::Instance, }; @@ -17,6 +17,7 @@ use cranelift_codegen::{ use std::panic; use std::str::FromStr; use std::sync::Arc; +use std::rc::Rc; use target_lexicon; use wasmparser; use wasmparser::WasmDecoder; @@ -122,11 +123,11 @@ pub fn instantiate_streaming( /// webassembly::CompileError. pub fn compile(buffer_source: &[u8]) -> Result, ErrorKind> { let compiler = &CraneliftCompiler {}; - let module = compiler + let module_inner = compiler .compile(buffer_source) .map_err(|e| ErrorKind::CompileError(e))?; - Ok(Arc::new(module)) + Ok(Arc::new(Module(Rc::new(module_inner)))) } /// The webassembly::validate() function validates a given typed @@ -230,7 +231,7 @@ pub fn start_instance( path: &str, args: Vec<&str>, ) -> Result<(), String> { - let main_name = if is_emscripten_module(&instance.module) { + let main_name = if is_emscripten_module(&module) { "_main" } else { "main" From 57fbfd58d3287a89b68b54382a0c3cde30428dd3 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Thu, 17 Jan 2019 23:10:29 +0100 Subject: [PATCH 10/17] Fix globals --- lib/emscripten/src/lib.rs | 98 ++++++++++++++++++++++++++++++++----- lib/emscripten/src/utils.rs | 13 +++-- 2 files changed, 91 insertions(+), 20 deletions(-) diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index a7f734624..886d602ca 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -119,6 +119,17 @@ macro_rules! func { }}; } +macro_rules! global { + ($value:ident) => {{ + unsafe { + GlobalPointer::new( + // NOTE: Taking a shortcut here. LocalGlobal is a struct containing just u64. + std::mem::transmute::<&u64, *mut LocalGlobal>($value) + ) + } + }}; +} + pub struct EmscriptenGlobals<'a> { pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // > } @@ -149,24 +160,84 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { let mut imports = Imports::new(); let mut env_namespace = NamespaceMap::new(); let mut asm_namespace = NamespaceMap::new(); + let mut global_namespace = NamespaceMap::new(); // Add globals. -// for () + // NOTE: There is really no need for checks, these globals should always be available. + let env_globals = globals.data.get("env").unwrap(); + let global_globals = globals.data.get("global").unwrap(); - // for (name, global, desc) in &globals.data { - // let global_ptr = unsafe { - // GlobalPointer::new( - // std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) - // ) - // }; + let (value, ty) = env_globals.get("STACKTOP").unwrap(); + env_namespace.insert( + "STACKTOP".to_string(), + Export::Global { + local: global!(value), + global: GlobalDesc { + mutable: false, + ty: ty.clone(), + } + }, + ); - // let export = Export::Global { - // local: global_ptr, - // global: desc.clone(), - // }; + let (value, ty) = env_globals.get("STACK_MAX").unwrap(); + env_namespace.insert( + "STACK_MAX".to_string(), + Export::Global { + local: global!(value), + global: GlobalDesc { + mutable: false, + ty: ty.clone(), + } + }, + ); - // imports.register_export("env", name.clone(), export); - // } + let (value, ty) = env_globals.get("DYNAMICTOP_PTR").unwrap(); + env_namespace.insert( + "DYNAMICTOP_PTR".to_string(), + Export::Global { + local: global!(value), + global: GlobalDesc { + mutable: false, + ty: ty.clone(), + } + }, + ); + + let (value, ty) = env_globals.get("tableBase").unwrap(); + env_namespace.insert( + "tableBase".to_string(), + Export::Global { + local: global!(value), + global: GlobalDesc { + mutable: false, + ty: ty.clone(), + } + }, + ); + + let (value, ty) = global_globals.get("Infinity").unwrap(); + global_namespace.insert( + "Infinity".to_string(), + Export::Global { + local: global!(value), + global: GlobalDesc { + mutable: false, + ty: ty.clone(), + } + }, + ); + + let (value, ty) = global_globals.get("NaN").unwrap(); + global_namespace.insert( + "NaN".to_string(), + Export::Global { + local: global!(value), + global: GlobalDesc { + mutable: false, + ty: ty.clone(), + } + }, + ); // Print function env_namespace.insert( @@ -193,6 +264,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); + // Lock env_namespace.insert( "___lock", diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 84e4feb8a..04e1c17ce 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -9,13 +9,12 @@ use std::slice; use std::sync::Arc; /// We check if a provided module is an Emscripten generated one pub fn is_emscripten_module(module: &Arc) -> bool { - // for (_, import_name) in &module.imported_functions { - // if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { - // return true; - // } - // } - // false - true + for (_, import_name) in &module.0.imported_functions { + if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { + return true; + } + } + false } pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance: &Instance) -> u32 { From a17d8c08773593d84d8e6dd0959248af4a26fb4d Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Thu, 17 Jan 2019 23:23:21 +0100 Subject: [PATCH 11/17] Add rebase fix --- src/bin/wasmer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 220eae9bd..9f1e5dbb7 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -99,7 +99,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> { debug!("webassembly - creating instance"); - let mut instance = module.instantiate(&mut import_object) + let mut instance = module.instantiate(import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?; webassembly::start_instance( From 456a76a5e5e146ef8e15c35fdbb67cf668f2c730 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 18 Jan 2019 01:25:05 +0100 Subject: [PATCH 12/17] Fix emtest compilation issues --- Cargo.lock | 1 + lib/emscripten/src/utils.rs | 11 ++++- lib/emscripten/tests/emtests/_common.rs | 56 ++++++++++++++++--------- src/common/mod.rs | 4 +- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14e6fff66..45e99f1f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1129,6 +1129,7 @@ dependencies = [ "libc 0.2.44 (git+https://github.com/rust-lang/libc)", "time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "wasmer 0.1.4", "wasmer-clif-backend 0.1.4", "wasmer-runtime 0.1.4", ] diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 04e1c17ce..6255d046e 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -143,13 +143,19 @@ pub unsafe fn copy_stat_into_wasm(instance: &mut Instance, buf: u32, stat: &stat mod tests { use super::is_emscripten_module; use wasmer_clif_backend::CraneliftCompiler; + use wasmer_runtime::{ + compile, + module::Module, + }; use wabt::wat2wasm; + use std::sync::Arc; #[test] fn should_detect_emscripten_files() { const wast_bytes: &[u8] = include_bytes!("tests/is_emscripten_true.wast"); let wasm_binary = wat2wasm(wast_bytes.to_vec()).expect("Can't convert to wasm"); - let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); + let module = compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); + let module = Arc::new(module); assert!(is_emscripten_module(&module)); } @@ -157,7 +163,8 @@ mod tests { fn should_detect_non_emscripten_files() { const wast_bytes: &[u8] = include_bytes!("tests/is_emscripten_false.wast"); let wasm_binary = wat2wasm(wast_bytes.to_vec()).expect("Can't convert to wasm"); - let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); + let module = compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); + let module = Arc::new(module); assert!(!is_emscripten_module(&module)); } } diff --git a/lib/emscripten/tests/emtests/_common.rs b/lib/emscripten/tests/emtests/_common.rs index 594280047..6ecc52ce9 100644 --- a/lib/emscripten/tests/emtests/_common.rs +++ b/lib/emscripten/tests/emtests/_common.rs @@ -1,29 +1,47 @@ macro_rules! assert_emscripten_output { ($file:expr, $name:expr, $args:expr, $expected:expr) => {{ - use wasmer_emscripten::generate_emscripten_env; - // use wasmer::common::stdio::StdioCapturer; - use wasmer_runtime::{Import, Imports, FuncRef}; - use wasmer_runtime::table::TableBacking; - use wasmer_runtime::{Instance, module::Module}; + // TODO: Cyclic Dep! + use wasmer::{ + webassembly::{ + compile, + start_instance, + }, + common::stdio::StdioCapturer, + }; + + use wasmer_runtime::{ + instance::Instance, + module::Module, + table::TableBacking + }; + use wasmer_clif_backend::CraneliftCompiler; + use wasmer_emscripten::{ + EmscriptenGlobals, + generate_emscripten_env, + }; use std::sync::Arc; let wasm_bytes = include_bytes!($file); - let import_object = generate_emscripten_env(); -// let options = Some(InstanceOptions { -// mock_missing_imports: true, -// mock_missing_globals: true, -// mock_missing_tables: true, -// abi: InstanceABI::Emscripten, -// show_progressbar: false, -// // isa: get_isa(), -// }); -// let mut result_object = instantiate(&wasm_bytes.to_vec(), &import_object, options) -// .expect("Not compiled properly"); - let module = wasmer_runtime::compile(&wasm_bytes[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); - let instance = module.instantiate(&import_object).expect("WASM can't be instantiated"); + let module = compile(&wasm_bytes[..]) + .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? + + let emscripten_globals = EmscriptenGlobals::new(); + let mut import_object = generate_emscripten_env(&emscripten_globals); + + let mut instance = module.instantiate(import_object) + .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? + + start_instance( + Arc::clone(&module), + &mut instance, + $name, + $args, + ); + + assert!(false, "Emscripten tests are mocked"); // let capturer = StdioCapturer::new(); // start_instance( @@ -35,7 +53,7 @@ macro_rules! assert_emscripten_output { // .unwrap(); // let output = capturer.end().unwrap().0; // let expected_output = include_str!($expected); - assert!(false, "Emscripten tests are mocked"); + // assert!(false, "Emscripten tests are mocked"); // assert!( // output.contains(expected_output), // "Output: `{}` does not contain expected output: `{}`", diff --git a/src/common/mod.rs b/src/common/mod.rs index 63a6bd39f..34d8a0b66 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -2,5 +2,5 @@ pub mod mmap; pub mod slice; mod file_descriptor; -#[cfg(test)] -pub mod stdio; +//#[cfg(test)] +pub mod stdio; \ No newline at end of file From 1025a0d73020ec3bfab66e26714bb30ec6856a8c Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Thu, 17 Jan 2019 23:55:44 -0600 Subject: [PATCH 13/17] Emscripten tests compiling again --- .../emscripten/src}/file_descriptor.rs | 0 lib/emscripten/src/lib.rs | 48 ++++++------ {src/common => lib/emscripten/src}/stdio.rs | 0 lib/emscripten/tests/emtests/_common.rs | 75 ++++++++----------- src/common/mod.rs | 4 - 5 files changed, 58 insertions(+), 69 deletions(-) rename {src/common => lib/emscripten/src}/file_descriptor.rs (100%) rename {src/common => lib/emscripten/src}/stdio.rs (100%) diff --git a/src/common/file_descriptor.rs b/lib/emscripten/src/file_descriptor.rs similarity index 100% rename from src/common/file_descriptor.rs rename to lib/emscripten/src/file_descriptor.rs diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 886d602ca..c94425af5 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -4,21 +4,26 @@ extern crate wasmer_runtime; #[macro_use] use wasmer_runtime::macros; -use wasmer_runtime::{ - import::{Imports, NamespaceMap}, - export::{Export, Context, GlobalPointer, FuncPointer}, - types::{ - FuncSig, Type::{self, *}, Value, - GlobalDesc, - }, - vm::{self, LocalGlobal, Func}, - memory::LinearMemory, -}; use byteorder::{ByteOrder, LittleEndian}; +use hashbrown::{hash_map::Entry, HashMap}; use libc::c_int; use std::cell::UnsafeCell; use std::mem; -use hashbrown::{hash_map::Entry, HashMap}; +use wasmer_runtime::{ + export::{Context, Export, FuncPointer, GlobalPointer}, + import::{Imports, NamespaceMap}, + memory::LinearMemory, + types::{ + FuncSig, GlobalDesc, + Type::{self, *}, + Value, + }, + vm::{self, Func, LocalGlobal}, +}; + +//#[cfg(test)] +mod file_descriptor; +pub mod stdio; // EMSCRIPTEN APIS mod env; @@ -124,7 +129,7 @@ macro_rules! global { unsafe { GlobalPointer::new( // NOTE: Taking a shortcut here. LocalGlobal is a struct containing just u64. - std::mem::transmute::<&u64, *mut LocalGlobal>($value) + std::mem::transmute::<&u64, *mut LocalGlobal>($value), ) } }}; @@ -134,7 +139,7 @@ pub struct EmscriptenGlobals<'a> { pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // > } -impl <'a> EmscriptenGlobals<'a> { +impl<'a> EmscriptenGlobals<'a> { pub fn new() -> Self { let mut data = HashMap::new(); let mut env_namepace = HashMap::new(); @@ -150,9 +155,7 @@ impl <'a> EmscriptenGlobals<'a> { data.insert("env", env_namepace); data.insert("global", global_namepace); - Self { - data, - } + Self { data } } } @@ -175,7 +178,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -187,7 +190,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -199,7 +202,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -211,7 +214,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -223,7 +226,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -235,7 +238,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -252,7 +255,6 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); - env_namespace.insert( "putchar", Export::Function { diff --git a/src/common/stdio.rs b/lib/emscripten/src/stdio.rs similarity index 100% rename from src/common/stdio.rs rename to lib/emscripten/src/stdio.rs diff --git a/lib/emscripten/tests/emtests/_common.rs b/lib/emscripten/tests/emtests/_common.rs index 6ecc52ce9..526ae891c 100644 --- a/lib/emscripten/tests/emtests/_common.rs +++ b/lib/emscripten/tests/emtests/_common.rs @@ -1,64 +1,55 @@ macro_rules! assert_emscripten_output { ($file:expr, $name:expr, $args:expr, $expected:expr) => {{ - // TODO: Cyclic Dep! - use wasmer::{ - webassembly::{ - compile, - start_instance, - }, - common::stdio::StdioCapturer, - }; - - use wasmer_runtime::{ - instance::Instance, - module::Module, - table::TableBacking - }; use wasmer_clif_backend::CraneliftCompiler; use wasmer_emscripten::{ EmscriptenGlobals, generate_emscripten_env, + stdio::StdioCapturer }; - use std::sync::Arc; - let wasm_bytes = include_bytes!($file); - let module = compile(&wasm_bytes[..]) - .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? + let module = wasmer_runtime::compile(&wasm_bytes[..], &CraneliftCompiler::new()) + .expect("WASM can't be compiled"); + +// let module = compile(&wasm_bytes[..]) +// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? let emscripten_globals = EmscriptenGlobals::new(); - let mut import_object = generate_emscripten_env(&emscripten_globals); + let import_object = generate_emscripten_env(&emscripten_globals); let mut instance = module.instantiate(import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? - start_instance( - Arc::clone(&module), - &mut instance, - $name, - $args, - ); +// start_instance( +// Arc::clone(&module), +// &mut instance, +// $name, +// $args, +// ); assert!(false, "Emscripten tests are mocked"); - // let capturer = StdioCapturer::new(); - // start_instance( - // Arc::clone(&result_object.module), - // &mut result_object.instance, - // $name, - // $args, - // ) - // .unwrap(); - // let output = capturer.end().unwrap().0; - // let expected_output = include_str!($expected); - // assert!(false, "Emscripten tests are mocked"); - // assert!( - // output.contains(expected_output), - // "Output: `{}` does not contain expected output: `{}`", - // output, - // expected_output - // ); + let capturer = StdioCapturer::new(); + + instance.call("_main", &[]).map(|_o| ()).unwrap(); + // TODO handle start instance logic +// start_instance( +// Arc::clone(&result_object.module), +// &mut result_object.instance, +// $name, +// $args, +// ) +// .unwrap(); + let output = capturer.end().unwrap().0; + let expected_output = include_str!($expected); + assert!(false, "Emscripten tests are mocked"); + assert!( + output.contains(expected_output), + "Output: `{}` does not contain expected output: `{}`", + output, + expected_output + ); }}; } diff --git a/src/common/mod.rs b/src/common/mod.rs index 34d8a0b66..2e4a760a8 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,6 +1,2 @@ pub mod mmap; pub mod slice; - -mod file_descriptor; -//#[cfg(test)] -pub mod stdio; \ No newline at end of file From a6df28e147aeaf0eb338070e603b1d0e66cee760 Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Fri, 18 Jan 2019 00:01:27 -0600 Subject: [PATCH 14/17] Exclude emscripten tests using Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8d2af451b..37f8ee9d8 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ precommit: lint test test: # We use one thread so the emscripten stdouts doesn't collide - cargo test --all -- --test-threads=1 $(runargs) + cargo test --all --exclude wasmer-emscripten -- --test-threads=1 $(runargs) release: # If you are in OS-X, you will need mingw-w64 for cross compiling to windows From e43c3cb2ebae77c7cbf5443e1f1ab182fae7ed51 Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Fri, 18 Jan 2019 00:18:13 -0600 Subject: [PATCH 15/17] cargo fmt --- lib/emscripten/build/mod.rs | 2 +- lib/emscripten/src/exception.rs | 2 +- lib/emscripten/src/jmp.rs | 48 ++++++++++++++++----------------- lib/emscripten/src/lock.rs | 2 +- lib/emscripten/src/memory.rs | 2 +- lib/emscripten/src/process.rs | 2 +- lib/emscripten/src/syscalls.rs | 2 +- lib/emscripten/src/utils.rs | 45 +++++++++++++++---------------- lib/emscripten/src/varargs.rs | 2 +- src/bin/wasmer.rs | 7 ++--- src/webassembly/mod.rs | 4 +-- 11 files changed, 59 insertions(+), 59 deletions(-) diff --git a/lib/emscripten/build/mod.rs b/lib/emscripten/build/mod.rs index 06d11ae6b..54b8714a1 100644 --- a/lib/emscripten/build/mod.rs +++ b/lib/emscripten/build/mod.rs @@ -8,4 +8,4 @@ fn main() { if env::var(EMTESTS_ENV_VAR).unwrap_or("0".to_string()) == "1" { emtests::build(); } -} \ No newline at end of file +} diff --git a/lib/emscripten/src/exception.rs b/lib/emscripten/src/exception.rs index 4244c3005..a7b0e9e63 100644 --- a/lib/emscripten/src/exception.rs +++ b/lib/emscripten/src/exception.rs @@ -1,5 +1,5 @@ -use super::process::_abort; use super::env; +use super::process::_abort; use wasmer_runtime::Instance; /// emscripten: ___cxa_allocate_exception diff --git a/lib/emscripten/src/jmp.rs b/lib/emscripten/src/jmp.rs index c5e77132c..d32e6ce74 100644 --- a/lib/emscripten/src/jmp.rs +++ b/lib/emscripten/src/jmp.rs @@ -1,40 +1,40 @@ -use wasmer_runtime::Instance; use libc::{c_int, c_void}; use std::cell::UnsafeCell; +use wasmer_runtime::Instance; /// setjmp pub extern "C" fn __setjmp(env_addr: u32, instance: &mut Instance) -> c_int { debug!("emscripten::__setjmp (setjmp)"); unimplemented!() -// unsafe { -// // Rather than using the env as the holder of the jump buffer pointer, -// // we use the environment address to store the index relative to jumps -// // so the address of the jump it's outside the wasm memory itself. -// let jump_index = instance.memory_offset_addr(0, env_addr as usize) as *mut i8; -// // We create the jump buffer outside of the wasm memory -// let jump_buf: UnsafeCell<[c_int; 27]> = UnsafeCell::new([0; 27]); -// let mut jumps = &mut instance.emscripten_data().as_mut().unwrap().jumps; -// let result = setjmp(jump_buf.get() as _); -// // We set the jump index to be the last value of jumps -// *jump_index = jumps.len() as _; -// // We hold the reference of the jump buffer -// jumps.push(jump_buf); -// result -// } + // unsafe { + // // Rather than using the env as the holder of the jump buffer pointer, + // // we use the environment address to store the index relative to jumps + // // so the address of the jump it's outside the wasm memory itself. + // let jump_index = instance.memory_offset_addr(0, env_addr as usize) as *mut i8; + // // We create the jump buffer outside of the wasm memory + // let jump_buf: UnsafeCell<[c_int; 27]> = UnsafeCell::new([0; 27]); + // let mut jumps = &mut instance.emscripten_data().as_mut().unwrap().jumps; + // let result = setjmp(jump_buf.get() as _); + // // We set the jump index to be the last value of jumps + // *jump_index = jumps.len() as _; + // // We hold the reference of the jump buffer + // jumps.push(jump_buf); + // result + // } } /// longjmp pub extern "C" fn __longjmp(env_addr: u32, val: c_int, instance: &mut Instance) -> ! { debug!("emscripten::__longjmp (longjmp) {}", val); unimplemented!() -// unsafe { -// // We retrieve the jump index from the env address -// let jump_index = instance.memory_offset_addr(0, env_addr as usize) as *mut i8; -// let mut jumps = &mut instance.emscripten_data().as_mut().unwrap().jumps; -// // We get the real jump buffer from the jumps vector, using the retrieved index -// let mut jump_buf = &jumps[*jump_index as usize]; -// longjmp(jump_buf.get() as _, val) -// }; + // unsafe { + // // We retrieve the jump index from the env address + // let jump_index = instance.memory_offset_addr(0, env_addr as usize) as *mut i8; + // let mut jumps = &mut instance.emscripten_data().as_mut().unwrap().jumps; + // // We get the real jump buffer from the jumps vector, using the retrieved index + // let mut jump_buf = &jumps[*jump_index as usize]; + // longjmp(jump_buf.get() as _, val) + // }; } extern "C" { diff --git a/lib/emscripten/src/lock.rs b/lib/emscripten/src/lock.rs index 5b2473c05..384eefb82 100644 --- a/lib/emscripten/src/lock.rs +++ b/lib/emscripten/src/lock.rs @@ -1,5 +1,5 @@ -use wasmer_runtime::Instance; use libc::c_int; +use wasmer_runtime::Instance; // NOTE: Not implemented by Emscripten pub extern "C" fn ___lock(which: c_int, varargs: c_int, _instance: &mut Instance) { diff --git a/lib/emscripten/src/memory.rs b/lib/emscripten/src/memory.rs index f1af076d7..fea9967cd 100644 --- a/lib/emscripten/src/memory.rs +++ b/lib/emscripten/src/memory.rs @@ -1,6 +1,6 @@ use super::process::abort_with_message; -use wasmer_runtime::Instance; use libc::{c_int, c_void, memcpy, size_t}; +use wasmer_runtime::Instance; /// emscripten: _emscripten_memcpy_big pub extern "C" fn _emscripten_memcpy_big( diff --git a/lib/emscripten/src/process.rs b/lib/emscripten/src/process.rs index 8fdabdbb8..10a7b73b8 100644 --- a/lib/emscripten/src/process.rs +++ b/lib/emscripten/src/process.rs @@ -1,7 +1,7 @@ use libc::{abort, c_char, c_int, exit, pid_t, EAGAIN}; -use wasmer_runtime::Instance; use std::ffi::CStr; +use wasmer_runtime::Instance; pub extern "C" fn abort_with_message(message: &str) { debug!("emscripten::abort_with_message"); diff --git a/lib/emscripten/src/syscalls.rs b/lib/emscripten/src/syscalls.rs index 2010169d8..4ea39e00d 100644 --- a/lib/emscripten/src/syscalls.rs +++ b/lib/emscripten/src/syscalls.rs @@ -1,6 +1,5 @@ use super::utils::copy_stat_into_wasm; use super::varargs::VarArgs; -use wasmer_runtime::Instance; use byteorder::{ByteOrder, LittleEndian}; /// NOTE: TODO: These syscalls only support wasm_32 for now because they assume offsets are u32 /// Syscall list: https://www.cs.utexas.edu/~bismith/test/syscalls/syscalls32.html @@ -71,6 +70,7 @@ use libc::{ SO_REUSEADDR, TIOCGWINSZ, }; +use wasmer_runtime::Instance; use super::env; use std::mem; diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 6255d046e..44957970a 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -1,4 +1,4 @@ -use wasmer_runtime::{Instance, module::Module}; +use wasmer_runtime::{module::Module, Instance}; //use wasmer_runtime::Instance; use super::env; use libc::stat; @@ -9,12 +9,12 @@ use std::slice; use std::sync::Arc; /// We check if a provided module is an Emscripten generated one pub fn is_emscripten_module(module: &Arc) -> bool { - for (_, import_name) in &module.0.imported_functions { - if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { - return true; - } - } - false + for (_, import_name) in &module.0.imported_functions { + if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { + return true; + } + } + false } pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance: &Instance) -> u32 { @@ -51,14 +51,14 @@ pub unsafe fn allocate_on_stack<'a, T: Copy>( instance: &'a Instance, ) -> (u32, &'a mut [T]) { unimplemented!("allocate_on_stack not implemented") -// let offset = (instance.emscripten_data().as_ref().unwrap().stack_alloc)( -// count * (size_of::() as u32), -// instance, -// ); -// let addr = instance.memory_offset_addr(0, offset as _) as *mut T; -// let slice = slice::from_raw_parts_mut(addr, count as usize); -// -// (offset, slice) + // let offset = (instance.emscripten_data().as_ref().unwrap().stack_alloc)( + // count * (size_of::() as u32), + // instance, + // ); + // let addr = instance.memory_offset_addr(0, offset as _) as *mut T; + // let slice = slice::from_raw_parts_mut(addr, count as usize); + // + // (offset, slice) } pub unsafe fn allocate_cstr_on_stack<'a>(s: &str, instance: &'a Instance) -> (u32, &'a [u8]) { @@ -142,19 +142,17 @@ pub unsafe fn copy_stat_into_wasm(instance: &mut Instance, buf: u32, stat: &stat #[cfg(test)] mod tests { use super::is_emscripten_module; - use wasmer_clif_backend::CraneliftCompiler; - use wasmer_runtime::{ - compile, - module::Module, - }; - use wabt::wat2wasm; use std::sync::Arc; + use wabt::wat2wasm; + use wasmer_clif_backend::CraneliftCompiler; + use wasmer_runtime::{compile, module::Module}; #[test] fn should_detect_emscripten_files() { const wast_bytes: &[u8] = include_bytes!("tests/is_emscripten_true.wast"); let wasm_binary = wat2wasm(wast_bytes.to_vec()).expect("Can't convert to wasm"); - let module = compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); + let module = + compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); let module = Arc::new(module); assert!(is_emscripten_module(&module)); } @@ -163,7 +161,8 @@ mod tests { fn should_detect_non_emscripten_files() { const wast_bytes: &[u8] = include_bytes!("tests/is_emscripten_false.wast"); let wasm_binary = wat2wasm(wast_bytes.to_vec()).expect("Can't convert to wasm"); - let module = compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); + let module = + compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled"); let module = Arc::new(module); assert!(!is_emscripten_module(&module)); } diff --git a/lib/emscripten/src/varargs.rs b/lib/emscripten/src/varargs.rs index 3caef65a7..ce0bc9e96 100644 --- a/lib/emscripten/src/varargs.rs +++ b/lib/emscripten/src/varargs.rs @@ -1,5 +1,5 @@ -use wasmer_runtime::Instance; use std::mem; +use wasmer_runtime::Instance; #[repr(transparent)] pub struct VarArgs { diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 9f1e5dbb7..906555114 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -6,14 +6,14 @@ use std::io; use std::io::Read; use std::path::PathBuf; use std::process::exit; -use std::sync::Arc; use std::rc::Rc; +use std::sync::Arc; use structopt::StructOpt; use wasmer::*; -use wasmer_runtime; use wasmer_emscripten; +use wasmer_runtime; #[derive(Debug, StructOpt)] #[structopt(name = "wasmer", about = "WASM execution runtime.")] @@ -99,7 +99,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> { debug!("webassembly - creating instance"); - let mut instance = module.instantiate(import_object) + let mut instance = module + .instantiate(import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?; webassembly::start_instance( diff --git a/src/webassembly/mod.rs b/src/webassembly/mod.rs index cbf5205bd..e17b439de 100644 --- a/src/webassembly/mod.rs +++ b/src/webassembly/mod.rs @@ -6,9 +6,9 @@ pub mod utils; use wasmer_clif_backend::CraneliftCompiler; use wasmer_runtime::{ backend::Compiler, - module::{Module, ModuleInner}, import::Imports, instance::Instance, + module::{Module, ModuleInner}, }; use cranelift_codegen::{ @@ -16,9 +16,9 @@ use cranelift_codegen::{ settings::{self, Configurable}, }; use std::panic; +use std::rc::Rc; use std::str::FromStr; use std::sync::Arc; -use std::rc::Rc; use target_lexicon; use wasmparser; use wasmparser::WasmDecoder; From 47f46be69c6e01cc2b0f8af0942923042d54bb1e Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Fri, 18 Jan 2019 00:33:46 -0600 Subject: [PATCH 16/17] Ignore clippy error level --- lib/emscripten/src/env.rs | 3 +++ lib/emscripten/src/signal.rs | 2 ++ lib/emscripten/src/syscalls.rs | 5 +++++ lib/emscripten/src/time.rs | 6 ++++++ lib/emscripten/src/utils.rs | 1 + lib/runtime/src/backing.rs | 1 + lib/runtime/src/vm.rs | 9 +++++++++ 7 files changed, 27 insertions(+) diff --git a/lib/emscripten/src/env.rs b/lib/emscripten/src/env.rs index 0900be8d9..ac03c420b 100644 --- a/lib/emscripten/src/env.rs +++ b/lib/emscripten/src/env.rs @@ -73,6 +73,7 @@ pub extern "C" fn _unsetenv(name: c_int, instance: &mut Instance) { unsafe { unsetenv(name_addr) }; } +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _getpwnam(name_ptr: c_int, instance: &mut Instance) -> c_int { debug!("emscripten::_getpwnam {}", name_ptr); @@ -110,6 +111,7 @@ pub extern "C" fn _getpwnam(name_ptr: c_int, instance: &mut Instance) -> c_int { } } +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _getgrnam(name_ptr: c_int, instance: &mut Instance) -> c_int { debug!("emscripten::_getgrnam {}", name_ptr); @@ -189,6 +191,7 @@ pub extern "C" fn _getpagesize() -> u32 { 16384 } +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn ___build_environment(environ: c_int, instance: &mut Instance) { debug!("emscripten::___build_environment {}", environ); const MAX_ENV_VALUES: u32 = 64; diff --git a/lib/emscripten/src/signal.rs b/lib/emscripten/src/signal.rs index 852f4f4ae..7f256d6bf 100644 --- a/lib/emscripten/src/signal.rs +++ b/lib/emscripten/src/signal.rs @@ -1,6 +1,7 @@ // use super::varargs::VarArgs; use wasmer_runtime::Instance; +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _sigemptyset(set: u32, instance: &mut Instance) -> i32 { debug!("emscripten::_sigemptyset"); let set_addr = instance.memory_offset_addr(0, set as _) as *mut u32; @@ -15,6 +16,7 @@ pub extern "C" fn _sigaction(signum: u32, act: u32, oldact: u32, _instance: &mut 0 } +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _sigaddset(set: u32, signum: u32, instance: &mut Instance) -> i32 { debug!("emscripten::_sigaddset {}, {}", set, signum); let set_addr = instance.memory_offset_addr(0, set as _) as *mut u32; diff --git a/lib/emscripten/src/syscalls.rs b/lib/emscripten/src/syscalls.rs index 4ea39e00d..366d93c7d 100644 --- a/lib/emscripten/src/syscalls.rs +++ b/lib/emscripten/src/syscalls.rs @@ -295,6 +295,7 @@ pub extern "C" fn ___syscall64() -> pid_t { } // socketcall +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn ___syscall102( which: c_int, mut varargs: VarArgs, @@ -553,6 +554,7 @@ pub extern "C" fn ___syscall102( } /// wait4 +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn ___syscall114( _which: c_int, mut varargs: VarArgs, @@ -588,6 +590,7 @@ pub extern "C" fn ___syscall122( } // select +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn ___syscall142( which: c_int, mut varargs: VarArgs, @@ -655,6 +658,7 @@ pub extern "C" fn ___syscall140( } /// readv +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn ___syscall145( which: c_int, mut varargs: VarArgs, @@ -700,6 +704,7 @@ pub extern "C" fn ___syscall145( } // writev +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn ___syscall146( which: c_int, mut varargs: VarArgs, diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index 14fb8bbf3..ec3f47f33 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -25,6 +25,7 @@ const CLOCK_MONOTONIC: libc::clockid_t = 1; const CLOCK_MONOTONIC_COARSE: libc::clockid_t = 6; /// emscripten: _gettimeofday +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _gettimeofday(tp: c_int, tz: c_int, instance: &mut Instance) -> c_int { debug!("emscripten::_gettimeofday {} {}", tp, tz); #[repr(C)] @@ -49,6 +50,7 @@ pub extern "C" fn _gettimeofday(tp: c_int, tz: c_int, instance: &mut Instance) - } /// emscripten: _clock_gettime +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _clock_gettime( clk_id: libc::clockid_t, tp: c_int, @@ -124,6 +126,7 @@ pub extern "C" fn _tvset() { } /// formats time as a C string +#[allow(clippy::cast_ptr_alignment)] unsafe extern "C" fn fmt_time(time: u32, instance: &Instance) -> *const c_char { let date = &*(instance.memory_offset_addr(0, time as _) as *mut guest_tm); @@ -181,6 +184,7 @@ pub extern "C" fn _asctime_r(time: u32, buf: u32, instance: &mut Instance) -> u3 } /// emscripten: _localtime +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> c_int { debug!("emscripten::_localtime {}", time_p); // NOTE: emscripten seems to want tzset() called in this function @@ -217,6 +221,7 @@ pub extern "C" fn _localtime(time_p: u32, instance: &mut Instance) -> c_int { } } /// emscripten: _localtime_r +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _localtime_r(time_p: u32, result: u32, instance: &mut Instance) -> c_int { debug!("emscripten::_localtime_r {}", time_p); @@ -253,6 +258,7 @@ pub extern "C" fn _localtime_r(time_p: u32, result: u32, instance: &mut Instance } /// emscripten: _time +#[allow(clippy::cast_ptr_alignment)] pub extern "C" fn _time(time_p: u32, instance: &mut Instance) -> time_t { debug!("emscripten::_time {}", time_p); diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 44957970a..c590801f8 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -112,6 +112,7 @@ pub struct GuestStat { st_ino: u64, } +#[allow(clippy::cast_ptr_alignment)] pub unsafe fn copy_stat_into_wasm(instance: &mut Instance, buf: u32, stat: &stat) { let stat_ptr = instance.memory_offset_addr(0, buf as _) as *mut GuestStat; (*stat_ptr).st_dev = stat.st_dev as _; diff --git a/lib/runtime/src/backing.rs b/lib/runtime/src/backing.rs index f84670d3d..72c5c908e 100644 --- a/lib/runtime/src/backing.rs +++ b/lib/runtime/src/backing.rs @@ -142,6 +142,7 @@ impl LocalBacking { tables.into_boxed_map() } + #[allow(clippy::cast_ptr_alignment)] fn finalize_tables( module: &ModuleInner, imports: &ImportBacking, diff --git a/lib/runtime/src/vm.rs b/lib/runtime/src/vm.rs index dad4f2ad3..75e453168 100644 --- a/lib/runtime/src/vm.rs +++ b/lib/runtime/src/vm.rs @@ -84,6 +84,7 @@ impl Ctx { } } + #[allow(clippy::erasing_op)] // TODO pub fn offset_memories() -> u8 { 0 * (mem::size_of::() as u8) } @@ -131,6 +132,7 @@ pub struct ImportedFunc { } impl ImportedFunc { + #[allow(clippy::erasing_op)] // TODO pub fn offset_func() -> u8 { 0 * (mem::size_of::() as u8) } @@ -157,6 +159,7 @@ pub struct LocalTable { } impl LocalTable { + #[allow(clippy::erasing_op)] // TODO pub fn offset_base() -> u8 { 0 * (mem::size_of::() as u8) } @@ -180,6 +183,7 @@ pub struct ImportedTable { } impl ImportedTable { + #[allow(clippy::erasing_op)] // TODO pub fn offset_table() -> u8 { 0 * (mem::size_of::() as u8) } @@ -206,6 +210,7 @@ pub struct LocalMemory { } impl LocalMemory { + #[allow(clippy::erasing_op)] // TODO pub fn offset_base() -> u8 { 0 * (mem::size_of::() as u8) } @@ -228,6 +233,7 @@ pub struct ImportedMemory { } impl ImportedMemory { + #[allow(clippy::erasing_op)] // TODO pub fn offset_memory() -> u8 { 0 * (mem::size_of::() as u8) } @@ -249,6 +255,7 @@ pub struct LocalGlobal { } impl LocalGlobal { + #[allow(clippy::erasing_op)] // TODO pub fn offset_data() -> u8 { 0 * (mem::size_of::() as u8) } @@ -269,6 +276,7 @@ pub struct ImportedGlobal { } impl ImportedGlobal { + #[allow(clippy::erasing_op)] // TODO pub fn offset_global() -> u8 { 0 * (mem::size_of::() as u8) } @@ -301,6 +309,7 @@ impl Anyfunc { } } + #[allow(clippy::erasing_op)] // TODO pub fn offset_func() -> u8 { 0 * (mem::size_of::() as u8) } From ac994d0c5184cf639aff801d3c3f4714be45af9d Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Fri, 18 Jan 2019 01:01:45 -0600 Subject: [PATCH 17/17] Remove unused import --- src/bin/wasmer.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 906555114..90605b1bd 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -6,7 +6,6 @@ use std::io; use std::io::Read; use std::path::PathBuf; use std::process::exit; -use std::rc::Rc; use std::sync::Arc; use structopt::StructOpt;