diff --git a/Cargo.lock b/Cargo.lock index 281f9494467..8a0937c2c2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2985,6 +2985,7 @@ dependencies = [ "log", "time", "wasmer", + "wasmer-types", ] [[package]] @@ -3258,6 +3259,7 @@ dependencies = [ "wasmer-compiler-cranelift", "wasmer-compiler-llvm", "wasmer-compiler-singlepass", + "wasmer-emscripten", "wasmer-engine", "wasmer-engine-dummy", "wasmer-engine-dylib", diff --git a/Cargo.toml b/Cargo.toml index a8153f2179a..a7b2468e9d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ wasmer-compiler = { version = "=2.2.1", path = "lib/compiler" } wasmer-compiler-cranelift = { version = "=2.2.1", path = "lib/compiler-cranelift", optional = true } wasmer-compiler-singlepass = { version = "=2.2.1", path = "lib/compiler-singlepass", optional = true } wasmer-compiler-llvm = { version = "=2.2.1", path = "lib/compiler-llvm", optional = true } -#wasmer-emscripten = { version = "=2.2.1", path = "lib/emscripten", optional = true } +wasmer-emscripten = { version = "=2.2.1", path = "lib/emscripten", optional = true } wasmer-engine = { version = "=2.2.1", path = "lib/engine" } wasmer-engine-universal = { version = "=2.2.1", path = "lib/engine-universal", optional = true } wasmer-engine-dylib = { version = "=2.2.1", path = "lib/engine-dylib", optional = true } @@ -95,7 +95,7 @@ default = [ "staticlib", #"cache", "wasi", - #"emscripten", + "emscripten", #"middlewares", ] engine = [] @@ -105,7 +105,7 @@ staticlib = ["wasmer-engine-staticlib", "engine"] #cache = ["wasmer-cache"] wast = ["wasmer-wast"] wasi = ["wasmer-wasi"] -#emscripten = ["wasmer-emscripten"] +emscripten = ["wasmer-emscripten"] wat = ["wasmer/wat"] compiler = [ "wasmer/compiler", diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 6f82382b922..a928e37581a 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -7,9 +7,11 @@ use crate::warning; use anyhow::{anyhow, Context, Result}; use std::path::PathBuf; use std::str::FromStr; +use wasmer::Context as WasmerContext; use wasmer::*; #[cfg(feature = "cache")] use wasmer_cache::{Cache, FileSystemCache, Hash}; +use wasmer_types::Type as ValueType; use structopt::StructOpt; @@ -95,6 +97,41 @@ impl Run { }) } + fn inner_run(&self, mut ctx: WasmerContext, instance: Instance) -> Result<()> { + let module = self.get_module()?; + // If this module exports an _initialize function, run that first. + if let Ok(initialize) = instance.exports.get_function("_initialize") { + initialize + .call(&mut ctx, &[]) + .with_context(|| "failed to run _initialize function")?; + } + + // Do we want to invoke a function? + if let Some(ref invoke) = self.invoke { + let imports = imports! {}; + let instance = Instance::new(&mut ctx, &module, &imports)?; + let result = + self.invoke_function(&mut ctx.as_context_mut(), &instance, &invoke, &self.args)?; + println!( + "{}", + result + .iter() + .map(|val| val.to_string()) + .collect::>() + .join(" ") + ); + } else { + let start: Function = self.try_find_function(&instance, "_start", &[])?; + let result = start.call(&mut ctx, &[]); + #[cfg(feature = "wasi")] + self.wasi.handle_result(result)?; + #[cfg(not(feature = "wasi"))] + result?; + } + + Ok(()) + } + fn inner_execute(&self) -> Result<()> { let module = self.get_module()?; #[cfg(feature = "emscripten")] @@ -108,12 +145,19 @@ impl Run { if self.invoke.is_some() { bail!("--invoke is not supported with emscripten modules"); } - let mut emscripten_globals = EmscriptenGlobals::new(module.store(), &module) + // create an EmEnv with default global + let mut ctx = WasmerContext::new(module.store(), EmEnv::new()); + let mut emscripten_globals = EmscriptenGlobals::new(ctx.as_context_mut(), &module) .map_err(|e| anyhow!("{}", e))?; - let mut em_env = EmEnv::new(&emscripten_globals.data, Default::default()); + ctx.data_mut() + .set_data(&emscripten_globals.data, Default::default()); let import_object = - generate_emscripten_env(module.store(), &mut emscripten_globals, &mut em_env); - let mut instance = match Instance::new(&module, &import_object) { + generate_emscripten_env(&mut ctx.as_context_mut(), &mut emscripten_globals); + let mut instance = match Instance::new( + &mut ctx.as_context_mut(), + &module, + &import_object, + ) { Ok(instance) => instance, Err(e) => { let err: Result<(), _> = Err(e); @@ -129,7 +173,7 @@ impl Run { run_emscripten_instance( &mut instance, - &mut em_env, + ctx.as_context_mut(), &mut emscripten_globals, if let Some(cn) = &self.command_name { cn @@ -145,7 +189,7 @@ impl Run { // If WASI is enabled, try to execute it with it #[cfg(feature = "wasi")] - let instance = { + let ret = { use std::collections::BTreeSet; use wasmer_wasi::WasiVersion; @@ -178,47 +222,28 @@ impl Run { .map(|f| f.to_string_lossy().to_string()) }) .unwrap_or_default(); - self.wasi + let (ctx, instance) = self + .wasi .instantiate(&module, program_name, self.args.clone()) - .with_context(|| "failed to instantiate WASI module")? + .with_context(|| "failed to instantiate WASI module")?; + self.inner_run(ctx, instance) } // not WASI - _ => Instance::new(&module, &imports! {})?, + _ => { + let mut ctx = WasmerContext::new(module.store(), ()); + let instance = Instance::new(&mut ctx, &module, &imports! {})?; + self.inner_run(ctx, instance) + } } }; #[cfg(not(feature = "wasi"))] - let instance = Instance::new(&module, &imports! {})?; - - // If this module exports an _initialize function, run that first. - if let Ok(initialize) = instance.exports.get_function("_initialize") { - initialize - .call(&[]) - .with_context(|| "failed to run _initialize function")?; - } - - // Do we want to invoke a function? - if let Some(ref invoke) = self.invoke { - let imports = imports! {}; - let instance = Instance::new(&module, &imports)?; - let result = self.invoke_function(&instance, &invoke, &self.args)?; - println!( - "{}", - result - .iter() - .map(|val| val.to_string()) - .collect::>() - .join(" ") - ); - } else { - let start: Function = self.try_find_function(&instance, "_start", &[])?; - let result = start.call(&[]); - #[cfg(feature = "wasi")] - self.wasi.handle_result(result)?; - #[cfg(not(feature = "wasi"))] - result?; - } + let ret = { + let mut ctx = WasmerContext::new(module.store(), ()); + let instance = Instance::new(&mut ctx, &module, &imports! {})?; + self.inner_run(ctx, instance) + }; - Ok(()) + ret } fn get_module(&self) -> Result { @@ -387,12 +412,13 @@ impl Run { fn invoke_function( &self, + ctx: &mut impl AsContextMut, instance: &Instance, invoke: &str, args: &[String], - ) -> Result> { + ) -> Result> { let func: Function = self.try_find_function(&instance, invoke, args)?; - let func_ty = func.ty(); + let func_ty = func.ty(ctx); let required_arguments = func_ty.params().len(); let provided_arguments = args.len(); if required_arguments != provided_arguments { @@ -407,23 +433,23 @@ impl Run { .iter() .zip(func_ty.params().iter()) .map(|(arg, param_type)| match param_type { - ValType::I32 => { - Ok(Val::I32(arg.parse().map_err(|_| { + ValueType::I32 => { + Ok(Value::I32(arg.parse().map_err(|_| { anyhow!("Can't convert `{}` into a i32", arg) })?)) } - ValType::I64 => { - Ok(Val::I64(arg.parse().map_err(|_| { + ValueType::I64 => { + Ok(Value::I64(arg.parse().map_err(|_| { anyhow!("Can't convert `{}` into a i64", arg) })?)) } - ValType::F32 => { - Ok(Val::F32(arg.parse().map_err(|_| { + ValueType::F32 => { + Ok(Value::F32(arg.parse().map_err(|_| { anyhow!("Can't convert `{}` into a f32", arg) })?)) } - ValType::F64 => { - Ok(Val::F64(arg.parse().map_err(|_| { + ValueType::F64 => { + Ok(Value::F64(arg.parse().map_err(|_| { anyhow!("Can't convert `{}` into a f64", arg) })?)) } @@ -434,7 +460,7 @@ impl Run { )), }) .collect::>>()?; - Ok(func.call(&invoke_args)?) + Ok(func.call(ctx, &invoke_args)?) } /// Create Run instance for arguments/env, diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index dca9c9d8943..5a17aac7e3c 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -2,8 +2,11 @@ use crate::utils::{parse_envvar, parse_mapdir}; use anyhow::Result; use std::collections::BTreeSet; use std::path::PathBuf; -use wasmer::{Instance, Module, RuntimeError, Val}; -use wasmer_wasi::{get_wasi_versions, WasiError, WasiState, WasiVersion}; +use wasmer::{AsContextMut, Context, Instance, Module, RuntimeError, Value}; +use wasmer_wasi::{ + get_wasi_versions, import_object_for_all_wasi_versions, WasiEnv, WasiError, WasiState, + WasiVersion, +}; use structopt::StructOpt; @@ -75,7 +78,7 @@ impl Wasi { module: &Module, program_name: String, args: Vec, - ) -> Result { + ) -> Result<(Context, Instance)> { let args = args.iter().cloned().map(|arg| arg.into_bytes()); let mut wasi_state_builder = WasiState::new(program_name); @@ -93,14 +96,17 @@ impl Wasi { } } - let mut wasi_env = wasi_state_builder.finalize()?; - let import_object = wasi_env.import_object_for_all_wasi_versions(&module)?; - let instance = Instance::new(&module, &import_object)?; - Ok(instance) + let wasi_env = wasi_state_builder.finalize()?; + let mut ctx = Context::new(module.store(), wasi_env.clone()); + let import_object = import_object_for_all_wasi_versions(&mut ctx.as_context_mut()); + let instance = Instance::new(&mut ctx, &module, &import_object)?; + let memory = instance.exports.get_memory("memory")?; + ctx.data_mut().set_memory(memory.clone()); + Ok((ctx, instance)) } /// Helper function for handling the result of a Wasi _start function. - pub fn handle_result(&self, result: Result, RuntimeError>) -> Result<()> { + pub fn handle_result(&self, result: Result, RuntimeError>) -> Result<()> { match result { Ok(_) => Ok(()), Err(err) => { diff --git a/lib/cli/src/commands/wast.rs b/lib/cli/src/commands/wast.rs index 2837069f21f..44262e769a8 100644 --- a/lib/cli/src/commands/wast.rs +++ b/lib/cli/src/commands/wast.rs @@ -3,6 +3,7 @@ use crate::store::StoreOptions; use anyhow::{Context, Result}; use std::path::PathBuf; use structopt::StructOpt; +use wasmer::Context as WasmerContext; use wasmer_wast::Wast as WastSpectest; #[derive(Debug, StructOpt)] @@ -28,7 +29,8 @@ impl Wast { } fn inner_execute(&self) -> Result<()> { let (store, _engine_name, _compiler_name) = self.store.get_store()?; - let mut wast = WastSpectest::new_with_spectest(store); + let ctx = WasmerContext::new(&store, ()); + let mut wast = WastSpectest::new_with_spectest(ctx); wast.fail_fast = self.fail_fast; wast.run_file(&self.path).with_context(|| "tests failed")?; eprintln!("Wast tests succeeded for `{}`.", self.path.display()); diff --git a/lib/emscripten/Cargo.toml b/lib/emscripten/Cargo.toml index 182f782ba7c..9da5d2da1e1 100644 --- a/lib/emscripten/Cargo.toml +++ b/lib/emscripten/Cargo.toml @@ -17,6 +17,7 @@ libc = "^0.2" log = "0.4" time = { version = "0.2", features = ["std"] } wasmer = { path = "../api", version = "=2.2.1", default-features = false, features = ["sys"] } +wasmer-types = { path = "../types", version = "=2.2.1" } [target.'cfg(windows)'.dependencies] getrandom = "0.2" diff --git a/lib/emscripten/src/bitwise.rs b/lib/emscripten/src/bitwise.rs index 5343dfe1c49..be6ee40be01 100644 --- a/lib/emscripten/src/bitwise.rs +++ b/lib/emscripten/src/bitwise.rs @@ -1,8 +1,9 @@ use crate::emscripten_target; use crate::EmEnv; +use wasmer::ContextMut; ///emscripten: _llvm_bswap_i64 -pub fn _llvm_bswap_i64(ctx: &EmEnv, _low: i32, high: i32) -> i32 { +pub fn _llvm_bswap_i64(ctx: ContextMut<'_, EmEnv>, _low: i32, high: i32) -> i32 { debug!("emscripten::_llvm_bswap_i64"); emscripten_target::setTempRet0(ctx, _low.swap_bytes()); high.swap_bytes() diff --git a/lib/emscripten/src/emscripten_target.rs b/lib/emscripten/src/emscripten_target.rs index 7b1a4336207..a444b24f360 100644 --- a/lib/emscripten/src/emscripten_target.rs +++ b/lib/emscripten/src/emscripten_target.rs @@ -1,35 +1,36 @@ #![allow(non_snake_case)] -use crate::env::get_emscripten_data; +use crate::env::{get_emscripten_data, get_emscripten_funcs}; use crate::EmEnv; #[cfg(target_os = "linux")] use libc::getdtablesize; +use wasmer::{AsContextMut, ContextMut}; -pub fn asm_const_i(_ctx: &EmEnv, _val: i32) -> i32 { +pub fn asm_const_i(_ctx: ContextMut<'_, EmEnv>, _val: i32) -> i32 { debug!("emscripten::asm_const_i: {}", _val); 0 } -pub fn exit_with_live_runtime(_ctx: &EmEnv) { +pub fn exit_with_live_runtime(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::exit_with_live_runtime"); } -pub fn setTempRet0(ctx: &EmEnv, val: i32) { +pub fn setTempRet0(ctx: ContextMut<'_, EmEnv>, val: i32) { trace!("emscripten::setTempRet0: {}", val); - get_emscripten_data(ctx).temp_ret_0 = val; + get_emscripten_data(&ctx).as_mut().unwrap().temp_ret_0 = val; } -pub fn getTempRet0(ctx: &EmEnv) -> i32 { +pub fn getTempRet0(ctx: ContextMut<'_, EmEnv>) -> i32 { trace!("emscripten::getTempRet0"); - get_emscripten_data(ctx).temp_ret_0 + get_emscripten_data(&ctx).as_ref().unwrap().temp_ret_0 } -pub fn _alarm(_ctx: &EmEnv, _seconds: u32) -> i32 { +pub fn _alarm(_ctx: ContextMut<'_, EmEnv>, _seconds: u32) -> i32 { debug!("emscripten::_alarm({})", _seconds); 0 } -pub fn _atexit(_ctx: &EmEnv, _func: i32) -> i32 { +pub fn _atexit(_ctx: ContextMut<'_, EmEnv>, _func: i32) -> i32 { debug!("emscripten::_atexit"); // TODO: implement atexit properly // __ATEXIT__.unshift({ @@ -38,38 +39,38 @@ pub fn _atexit(_ctx: &EmEnv, _func: i32) -> i32 { // }); 0 } -pub fn __Unwind_Backtrace(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn __Unwind_Backtrace(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::__Unwind_Backtrace"); 0 } -pub fn __Unwind_FindEnclosingFunction(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn __Unwind_FindEnclosingFunction(_ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { debug!("emscripten::__Unwind_FindEnclosingFunction"); 0 } -pub fn __Unwind_GetIPInfo(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn __Unwind_GetIPInfo(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::__Unwind_GetIPInfo"); 0 } -pub fn ___cxa_find_matching_catch_2(_ctx: &EmEnv) -> i32 { +pub fn ___cxa_find_matching_catch_2(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::___cxa_find_matching_catch_2"); 0 } -pub fn ___cxa_find_matching_catch_3(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn ___cxa_find_matching_catch_3(_ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { debug!("emscripten::___cxa_find_matching_catch_3"); 0 } -pub fn ___cxa_free_exception(_ctx: &EmEnv, _a: i32) { +pub fn ___cxa_free_exception(_ctx: ContextMut<'_, EmEnv>, _a: i32) { debug!("emscripten::___cxa_free_exception"); } -pub fn ___resumeException(_ctx: &EmEnv, _a: i32) { +pub fn ___resumeException(_ctx: ContextMut<'_, EmEnv>, _a: i32) { debug!("emscripten::___resumeException"); } -pub fn _dladdr(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _dladdr(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::_dladdr"); 0 } pub fn ___gxx_personality_v0( - _ctx: &EmEnv, + _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32, _c: i32, @@ -82,25 +83,25 @@ pub fn ___gxx_personality_v0( } #[cfg(target_os = "linux")] -pub fn _getdtablesize(_ctx: &EmEnv) -> i32 { +pub fn _getdtablesize(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::getdtablesize"); unsafe { getdtablesize() } } #[cfg(not(target_os = "linux"))] -pub fn _getdtablesize(_ctx: &EmEnv) -> i32 { +pub fn _getdtablesize(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::getdtablesize"); -1 } -pub fn _gethostbyaddr(_ctx: &EmEnv, _addr: i32, _addrlen: i32, _atype: i32) -> i32 { +pub fn _gethostbyaddr(_ctx: ContextMut<'_, EmEnv>, _addr: i32, _addrlen: i32, _atype: i32) -> i32 { debug!("emscripten::gethostbyaddr"); 0 } -pub fn _gethostbyname(_ctx: &EmEnv, _name: i32) -> i32 { +pub fn _gethostbyname(_ctx: ContextMut<'_, EmEnv>, _name: i32) -> i32 { debug!("emscripten::gethostbyname_r"); 0 } pub fn _gethostbyname_r( - _ctx: &EmEnv, + _ctx: ContextMut<'_, EmEnv>, _name: i32, _ret: i32, _buf: i32, @@ -112,12 +113,12 @@ pub fn _gethostbyname_r( 0 } // NOTE: php.js has proper impl; libc has proper impl for linux -pub fn _getloadavg(_ctx: &EmEnv, _loadavg: i32, _nelem: i32) -> i32 { +pub fn _getloadavg(_ctx: ContextMut<'_, EmEnv>, _loadavg: i32, _nelem: i32) -> i32 { debug!("emscripten::getloadavg"); 0 } pub fn _getnameinfo( - _ctx: &EmEnv, + _ctx: ContextMut<'_, EmEnv>, _addr: i32, _addrlen: i32, _host: i32, @@ -139,15 +140,18 @@ pub fn _getnameinfo( // Macro definitions macro_rules! invoke { ($ctx: ident, $name:ident, $name_ref:ident, $( $arg:ident ),*) => {{ - let sp = get_emscripten_data($ctx).stack_save_ref().expect("stack_save is None").call().expect("stack_save call failed"); - let call = get_emscripten_data($ctx).$name_ref().expect(concat!("Dynamic call is None: ", stringify!($name))).clone(); - match call.call($($arg),*) { + let funcs = get_emscripten_funcs(&$ctx).clone(); + let sp = funcs.stack_save_ref().expect("stack_save is None").call(&mut $ctx.as_context_mut()).expect("stack_save call failed"); + let call = funcs.$name_ref().expect(concat!("Dynamic call is None: ", stringify!($name))).clone(); + match call.call(&mut $ctx, $($arg),*) { Ok(v) => v, Err(_e) => { - get_emscripten_data($ctx).stack_restore_ref().expect("stack_restore is None").call(sp).expect("stack_restore call failed"); + let stack = funcs.stack_restore_ref().expect("stack_restore is None"); + stack.call(&mut $ctx, sp).expect("stack_restore call failed"); // TODO: We should check if _e != "longjmp" and if that's the case, re-throw the error // JS version is: if (e !== e+0 && e !== 'longjmp') throw e; - get_emscripten_data($ctx).set_threw_ref().expect("set_threw is None").call(1, 0).expect("set_threw call failed"); + let threw = funcs.set_threw_ref().expect("set_threw is None"); + threw.call(&mut $ctx, 1, 0).expect("set_threw call failed"); 0 as _ } } @@ -155,15 +159,19 @@ macro_rules! invoke { } macro_rules! invoke_no_return { ($ctx: ident, $name:ident, $name_ref:ident, $( $arg:ident ),*) => {{ - let sp = get_emscripten_data($ctx).stack_save_ref().expect("stack_save is None").call().expect("stack_save call failed"); - let call = get_emscripten_data($ctx).$name_ref().expect(concat!("Dynamic call is None: ", stringify!($name))).clone(); - match call.call($($arg),*) { + let funcs = get_emscripten_funcs(&$ctx).clone(); + let stack = funcs.stack_save_ref().expect("stack_save is None"); + let sp = stack.call(&mut $ctx).expect("stack_save call failed"); + let call = funcs.$name_ref().expect(concat!("Dynamic call is None: ", stringify!($name))).clone(); + match call.call(&mut $ctx, $($arg),*) { Ok(v) => v, Err(_e) => { - get_emscripten_data($ctx).stack_restore_ref().expect("stack_restore is None").call(sp).expect("stack_restore call failed"); + let stack = funcs.stack_restore_ref().expect("stack_restore is None"); + stack.call(&mut $ctx, sp).expect("stack_restore call failed"); // TODO: We should check if _e != "longjmp" and if that's the case, re-throw the error // JS version is: if (e !== e+0 && e !== 'longjmp') throw e; - get_emscripten_data($ctx).set_threw_ref().expect("set_threw is None").call(1, 0).expect("set_threw call failed"); + let threw = funcs.set_threw_ref().expect("set_threw is None"); + threw.call(&mut $ctx, 1, 0).expect("set_threw call failed"); } } }}; @@ -171,51 +179,59 @@ macro_rules! invoke_no_return { // The invoke_j functions do not save the stack macro_rules! invoke_no_stack_save { ($ctx: ident, $name:ident, $name_ref:ident, $( $arg:ident ),*) => {{ - let call = get_emscripten_data($ctx).$name_ref().expect(concat!(stringify!($name), " is set to None")).clone(); + let funcs = get_emscripten_funcs(&$ctx).clone(); + let call = funcs.$name_ref().expect(concat!(stringify!($name), " is set to None")).clone(); - call.call($($arg),*).unwrap() + call.call(&mut $ctx.as_context_mut(), $($arg),*).unwrap() }} } // Invoke functions -pub fn invoke_i(ctx: &EmEnv, index: i32) -> i32 { +pub fn invoke_i(mut ctx: ContextMut<'_, EmEnv>, index: i32) -> i32 { debug!("emscripten::invoke_i"); invoke!(ctx, dyn_call_i, dyn_call_i_ref, index) } -pub fn invoke_ii(ctx: &EmEnv, index: i32, a1: i32) -> i32 { +pub fn invoke_ii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32) -> i32 { debug!("emscripten::invoke_ii"); invoke!(ctx, dyn_call_ii, dyn_call_ii_ref, index, a1) } -pub fn invoke_iii(ctx: &EmEnv, index: i32, a1: i32, a2: i32) -> i32 { +pub fn invoke_iii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32) -> i32 { debug!("emscripten::invoke_iii"); invoke!(ctx, dyn_call_iii, dyn_call_iii_ref, index, a1, a2) } -pub fn invoke_iiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { +pub fn invoke_iiii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { debug!("emscripten::invoke_iiii"); invoke!(ctx, dyn_call_iiii, dyn_call_iiii_ref, index, a1, a2, a3) } -pub fn invoke_iifi(ctx: &EmEnv, index: i32, a1: i32, a2: f64, a3: i32) -> i32 { +pub fn invoke_iifi(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: f64, a3: i32) -> i32 { debug!("emscripten::invoke_iifi"); invoke!(ctx, dyn_call_iifi, dyn_call_iifi_ref, index, a1, a2, a3) } -pub fn invoke_v(ctx: &EmEnv, index: i32) { +pub fn invoke_v(mut ctx: ContextMut<'_, EmEnv>, index: i32) { debug!("emscripten::invoke_v"); invoke_no_return!(ctx, dyn_call_v, dyn_call_v_ref, index); } -pub fn invoke_vi(ctx: &EmEnv, index: i32, a1: i32) { +pub fn invoke_vi(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32) { debug!("emscripten::invoke_vi"); invoke_no_return!(ctx, dyn_call_vi, dyn_call_vi_ref, index, a1); } -pub fn invoke_vii(ctx: &EmEnv, index: i32, a1: i32, a2: i32) { +pub fn invoke_vii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32) { debug!("emscripten::invoke_vii"); invoke_no_return!(ctx, dyn_call_vii, dyn_call_vii_ref, index, a1, a2); } -pub fn invoke_viii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32) { +pub fn invoke_viii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32) { debug!("emscripten::invoke_viii"); invoke_no_return!(ctx, dyn_call_viii, dyn_call_viii_ref, index, a1, a2, a3); } -pub fn invoke_viiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) { +pub fn invoke_viiii( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, +) { debug!("emscripten::invoke_viiii"); invoke_no_return!( ctx, @@ -228,11 +244,18 @@ pub fn invoke_viiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) a4 ); } -pub fn invoke_dii(ctx: &EmEnv, index: i32, a1: i32, a2: i32) -> f64 { +pub fn invoke_dii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32) -> f64 { debug!("emscripten::invoke_dii"); invoke!(ctx, dyn_call_dii, dyn_call_dii_ref, index, a1, a2) } -pub fn invoke_diiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) -> f64 { +pub fn invoke_diiii( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, +) -> f64 { debug!("emscripten::invoke_diiii"); invoke!( ctx, @@ -245,7 +268,14 @@ pub fn invoke_diiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) a4 ) } -pub fn invoke_iiiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) -> i32 { +pub fn invoke_iiiii( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, +) -> i32 { debug!("emscripten::invoke_iiiii"); invoke!( ctx, @@ -258,7 +288,15 @@ pub fn invoke_iiiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) a4 ) } -pub fn invoke_iiiiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) -> i32 { +pub fn invoke_iiiiii( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, +) -> i32 { debug!("emscripten::invoke_iiiiii"); invoke!( ctx, @@ -273,7 +311,7 @@ pub fn invoke_iiiiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32 ) } pub fn invoke_iiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -297,7 +335,7 @@ pub fn invoke_iiiiiii( ) } pub fn invoke_iiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -323,7 +361,7 @@ pub fn invoke_iiiiiiii( ) } pub fn invoke_iiiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -351,7 +389,7 @@ pub fn invoke_iiiiiiiii( ) } pub fn invoke_iiiiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -381,7 +419,7 @@ pub fn invoke_iiiiiiiiii( ) } pub fn invoke_iiiiiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -412,11 +450,19 @@ pub fn invoke_iiiiiiiiiii( a10 ) } -pub fn invoke_vd(ctx: &EmEnv, index: i32, a1: f64) { +pub fn invoke_vd(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: f64) { debug!("emscripten::invoke_vd"); invoke_no_return!(ctx, dyn_call_vd, dyn_call_vd_ref, index, a1) } -pub fn invoke_viiiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) { +pub fn invoke_viiiii( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, +) { debug!("emscripten::invoke_viiiii"); invoke_no_return!( ctx, @@ -431,7 +477,7 @@ pub fn invoke_viiiii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32 ) } pub fn invoke_viiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -455,7 +501,7 @@ pub fn invoke_viiiiii( ) } pub fn invoke_viiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -481,7 +527,7 @@ pub fn invoke_viiiiiii( ) } pub fn invoke_viiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -509,7 +555,7 @@ pub fn invoke_viiiiiiii( ) } pub fn invoke_viiiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -539,7 +585,7 @@ pub fn invoke_viiiiiiiii( ) } pub fn invoke_viiiiiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -571,23 +617,30 @@ pub fn invoke_viiiiiiiiii( ) } -pub fn invoke_iij(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { +pub fn invoke_iij(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { debug!("emscripten::invoke_iij"); invoke!(ctx, dyn_call_iij, dyn_call_iij_ref, index, a1, a2, a3) } -pub fn invoke_iji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { +pub fn invoke_iji(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { debug!("emscripten::invoke_iji"); invoke!(ctx, dyn_call_iji, dyn_call_iji_ref, index, a1, a2, a3) } -pub fn invoke_iiji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) -> i32 { +pub fn invoke_iiji( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, +) -> i32 { debug!("emscripten::invoke_iiji"); invoke!(ctx, dyn_call_iiji, dyn_call_iiji_ref, index, a1, a2, a3, a4) } pub fn invoke_iiijj( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -610,28 +663,43 @@ pub fn invoke_iiijj( a6 ) } -pub fn invoke_j(ctx: &EmEnv, index: i32) -> i32 { +pub fn invoke_j(mut ctx: ContextMut<'_, EmEnv>, index: i32) -> i32 { debug!("emscripten::invoke_j"); invoke_no_stack_save!(ctx, dyn_call_j, dyn_call_j_ref, index) } -pub fn invoke_ji(ctx: &EmEnv, index: i32, a1: i32) -> i32 { +pub fn invoke_ji(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32) -> i32 { debug!("emscripten::invoke_ji"); invoke_no_stack_save!(ctx, dyn_call_ji, dyn_call_ji_ref, index, a1) } -pub fn invoke_jii(ctx: &EmEnv, index: i32, a1: i32, a2: i32) -> i32 { +pub fn invoke_jii(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32) -> i32 { debug!("emscripten::invoke_jii"); invoke_no_stack_save!(ctx, dyn_call_jii, dyn_call_jii_ref, index, a1, a2) } -pub fn invoke_jij(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { +pub fn invoke_jij(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32) -> i32 { debug!("emscripten::invoke_jij"); invoke_no_stack_save!(ctx, dyn_call_jij, dyn_call_jij_ref, index, a1, a2, a3) } -pub fn invoke_jjj(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) -> i32 { +pub fn invoke_jjj( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, +) -> i32 { debug!("emscripten::invoke_jjj"); invoke_no_stack_save!(ctx, dyn_call_jjj, dyn_call_jjj_ref, index, a1, a2, a3, a4) } -pub fn invoke_viiij(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) { +pub fn invoke_viiij( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, +) { debug!("emscripten::invoke_viiij"); invoke_no_stack_save!( ctx, @@ -646,7 +714,7 @@ pub fn invoke_viiij(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, ) } pub fn invoke_viiijiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -676,7 +744,7 @@ pub fn invoke_viiijiiii( ) } pub fn invoke_viiijiiiiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -709,11 +777,19 @@ pub fn invoke_viiijiiiiii( a11 ) } -pub fn invoke_viij(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) { +pub fn invoke_viij(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) { debug!("emscripten::invoke_viij"); invoke_no_stack_save!(ctx, dyn_call_viij, dyn_call_viij_ref, index, a1, a2, a3, a4) } -pub fn invoke_viiji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) { +pub fn invoke_viiji( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, +) { debug!("emscripten::invoke_viiji"); invoke_no_stack_save!( ctx, @@ -728,7 +804,7 @@ pub fn invoke_viiji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, ) } pub fn invoke_viijiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -753,7 +829,16 @@ pub fn invoke_viijiii( a7 ) } -pub fn invoke_viijj(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32, a6: i32) { +pub fn invoke_viijj( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, + a6: i32, +) { debug!("emscripten::invoke_viijj"); invoke_no_stack_save!( ctx, @@ -768,11 +853,19 @@ pub fn invoke_viijj(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a6 ) } -pub fn invoke_vj(ctx: &EmEnv, index: i32, a1: i32, a2: i32) { +pub fn invoke_vj(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32) { debug!("emscripten::invoke_vj"); invoke_no_stack_save!(ctx, dyn_call_vj, dyn_call_vj_ref, index, a1, a2) } -pub fn invoke_vjji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) { +pub fn invoke_vjji( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, +) { debug!("emscripten::invoke_vjji"); invoke_no_return!( ctx, @@ -786,16 +879,16 @@ pub fn invoke_vjji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5 ) } -pub fn invoke_vij(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32) { +pub fn invoke_vij(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32) { debug!("emscripten::invoke_vij"); invoke_no_stack_save!(ctx, dyn_call_vij, dyn_call_vij_ref, index, a1, a2, a3) } -pub fn invoke_viji(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) { +pub fn invoke_viji(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: i32, a4: i32) { debug!("emscripten::invoke_viji"); invoke_no_stack_save!(ctx, dyn_call_viji, dyn_call_viji_ref, index, a1, a2, a3, a4) } pub fn invoke_vijiii( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, @@ -818,7 +911,15 @@ pub fn invoke_vijiii( a6 ) } -pub fn invoke_vijj(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) { +pub fn invoke_vijj( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: i32, + a4: i32, + a5: i32, +) { debug!("emscripten::invoke_vijj"); invoke_no_stack_save!( ctx, @@ -832,15 +933,23 @@ pub fn invoke_vijj(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5 ) } -pub fn invoke_vidd(ctx: &EmEnv, index: i32, a1: i32, a2: f64, a3: f64) { +pub fn invoke_vidd(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: f64, a3: f64) { debug!("emscripten::invoke_viid"); invoke_no_return!(ctx, dyn_call_vidd, dyn_call_vidd_ref, index, a1, a2, a3); } -pub fn invoke_viid(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: f64) { +pub fn invoke_viid(mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, a3: f64) { debug!("emscripten::invoke_viid"); invoke_no_return!(ctx, dyn_call_viid, dyn_call_viid_ref, index, a1, a2, a3); } -pub fn invoke_viidii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: f64, a4: i32, a5: i32) { +pub fn invoke_viidii( + mut ctx: ContextMut<'_, EmEnv>, + index: i32, + a1: i32, + a2: i32, + a3: f64, + a4: i32, + a5: i32, +) { debug!("emscripten::invoke_viidii"); invoke_no_return!( ctx, @@ -855,7 +964,7 @@ pub fn invoke_viidii(ctx: &EmEnv, index: i32, a1: i32, a2: i32, a3: f64, a4: i32 ); } pub fn invoke_viidddddddd( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, index: i32, a1: i32, a2: i32, diff --git a/lib/emscripten/src/env/mod.rs b/lib/emscripten/src/env/mod.rs index 71e4011ca9a..fa7c1ab1edc 100644 --- a/lib/emscripten/src/env/mod.rs +++ b/lib/emscripten/src/env/mod.rs @@ -12,54 +12,55 @@ pub use self::windows::*; use libc::c_char; -use crate::{allocate_on_stack, EmscriptenData}; +use crate::{allocate_on_stack, EmscriptenData, EmscriptenFunctions}; use std::os::raw::c_int; use std::sync::MutexGuard; use crate::EmEnv; use wasmer::ValueType; -use wasmer::WasmPtr; +use wasmer::{AsContextMut, ContextMut, WasmPtr}; -pub fn call_malloc(ctx: &EmEnv, size: u32) -> u32 { - get_emscripten_data(ctx) - .malloc_ref() - .unwrap() - .call(size) - .unwrap() +pub fn call_malloc(mut ctx: ContextMut<'_, EmEnv>, size: u32) -> u32 { + let malloc_ref = get_emscripten_funcs(&ctx).malloc_ref().unwrap().clone(); + malloc_ref.call(&mut ctx.as_context_mut(), size).unwrap() } #[warn(dead_code)] -pub fn call_malloc_with_cast(ctx: &EmEnv, size: u32) -> WasmPtr { +pub fn call_malloc_with_cast(ctx: ContextMut<'_, EmEnv>, size: u32) -> WasmPtr { WasmPtr::new(call_malloc(ctx, size)) } -pub fn call_memalign(ctx: &EmEnv, alignment: u32, size: u32) -> u32 { - if let Some(memalign) = &get_emscripten_data(ctx).memalign_ref() { - memalign.call(alignment, size).unwrap() - } else { - panic!("Memalign is set to None"); - } +pub fn call_memalign(mut ctx: ContextMut<'_, EmEnv>, alignment: u32, size: u32) -> u32 { + let memalign_ref = get_emscripten_funcs(&ctx).memalign_ref().unwrap().clone(); + memalign_ref.call(&mut ctx, alignment, size).unwrap() } -pub fn call_memset(ctx: &EmEnv, pointer: u32, value: u32, size: u32) -> u32 { - get_emscripten_data(ctx) - .memset_ref() - .unwrap() - .call(pointer, value, size) +pub fn call_memset(mut ctx: ContextMut<'_, EmEnv>, pointer: u32, value: u32, size: u32) -> u32 { + let memset_ref = get_emscripten_funcs(&ctx).memset_ref().unwrap().clone(); + memset_ref + .call(&mut ctx.as_context_mut(), pointer, value, size) .unwrap() } -pub(crate) fn get_emscripten_data(ctx: &EmEnv) -> MutexGuard { - ctx.data.lock().unwrap() +pub(crate) fn get_emscripten_data<'a>( + ctx: &'a ContextMut<'_, EmEnv>, +) -> MutexGuard<'a, Option> { + ctx.data().data.lock().unwrap() +} + +pub(crate) fn get_emscripten_funcs<'a>( + ctx: &'a ContextMut<'_, EmEnv>, +) -> MutexGuard<'a, EmscriptenFunctions> { + ctx.data().funcs.lock().unwrap() } -pub fn _getpagesize(_ctx: &EmEnv) -> u32 { +pub fn _getpagesize(_ctx: ContextMut<'_, EmEnv>) -> u32 { debug!("emscripten::_getpagesize"); 16384 } -pub fn _times(ctx: &EmEnv, buffer: u32) -> u32 { +pub fn _times(ctx: ContextMut<'_, EmEnv>, buffer: u32) -> u32 { if buffer != 0 { call_memset(ctx, buffer, 0, 16); } @@ -67,18 +68,20 @@ pub fn _times(ctx: &EmEnv, buffer: u32) -> u32 { } #[allow(clippy::cast_ptr_alignment)] -pub fn ___build_environment(ctx: &EmEnv, environ: c_int) { +pub fn ___build_environment(mut ctx: ContextMut<'_, EmEnv>, environ: c_int) { debug!("emscripten::___build_environment {}", environ); const MAX_ENV_VALUES: u32 = 64; const TOTAL_ENV_SIZE: u32 = 1024; - let environment = emscripten_memory_pointer!(ctx.memory(0), environ) as *mut c_int; + let environment = emscripten_memory_pointer!(ctx, ctx.data().memory(0), environ) as *mut c_int; let (mut pool_offset, env_ptr, mut pool_ptr) = unsafe { let (pool_offset, _pool_slice): (u32, &mut [u8]) = - allocate_on_stack(ctx, TOTAL_ENV_SIZE as u32); + allocate_on_stack(&mut ctx.as_context_mut(), TOTAL_ENV_SIZE as u32); let (env_offset, _env_slice): (u32, &mut [u8]) = - allocate_on_stack(ctx, (MAX_ENV_VALUES * 4) as u32); - let env_ptr = emscripten_memory_pointer!(ctx.memory(0), env_offset) as *mut c_int; - let pool_ptr = emscripten_memory_pointer!(ctx.memory(0), pool_offset) as *mut u8; + allocate_on_stack(&mut ctx.as_context_mut(), (MAX_ENV_VALUES * 4) as u32); + let env_ptr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), env_offset) as *mut c_int; + let pool_ptr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), pool_offset) as *mut u8; *env_ptr = pool_offset as i32; *environment = env_offset as i32; @@ -119,18 +122,18 @@ pub fn ___build_environment(ctx: &EmEnv, environ: c_int) { } } -pub fn ___assert_fail(_ctx: &EmEnv, _a: c_int, _b: c_int, _c: c_int, _d: c_int) { +pub fn ___assert_fail(_ctx: ContextMut<'_, EmEnv>, _a: c_int, _b: c_int, _c: c_int, _d: c_int) { debug!("emscripten::___assert_fail {} {} {} {}", _a, _b, _c, _d); // TODO: Implement like emscripten expects regarding memory/page size // TODO raise an error } -pub fn _pathconf(ctx: &EmEnv, path_addr: c_int, name: c_int) -> c_int { +pub fn _pathconf(ctx: ContextMut<'_, EmEnv>, path_addr: c_int, name: c_int) -> c_int { debug!( "emscripten::_pathconf {} {} - UNIMPLEMENTED", path_addr, name ); - let _path = emscripten_memory_pointer!(ctx.memory(0), path_addr) as *const c_char; + let _path = emscripten_memory_pointer!(ctx, ctx.data().memory(0), path_addr) as *const c_char; match name { 0 => 32000, 1 | 2 | 3 => 255, @@ -146,7 +149,7 @@ pub fn _pathconf(ctx: &EmEnv, path_addr: c_int, name: c_int) -> c_int { } } -pub fn _fpathconf(_ctx: &EmEnv, _fildes: c_int, name: c_int) -> c_int { +pub fn _fpathconf(_ctx: ContextMut<'_, EmEnv>, _fildes: c_int, name: c_int) -> c_int { debug!("emscripten::_fpathconf {} {}", _fildes, name); match name { 0 => 32000, diff --git a/lib/emscripten/src/env/unix/mod.rs b/lib/emscripten/src/env/unix/mod.rs index c41521f1240..9ef7e59a024 100644 --- a/lib/emscripten/src/env/unix/mod.rs +++ b/lib/emscripten/src/env/unix/mod.rs @@ -10,14 +10,14 @@ use std::os::raw::c_char; use crate::env::{call_malloc, call_malloc_with_cast, EmAddrInfo, EmSockAddr}; use crate::utils::{copy_cstr_into_wasm, copy_terminated_array_of_cstrs}; use crate::EmEnv; -use wasmer::WasmPtr; +use wasmer::{AsContextMut, ContextMut, WasmPtr}; // #[no_mangle] /// emscripten: _getenv // (name: *const char) -> *const c_char; -pub fn _getenv(ctx: &EmEnv, name: i32) -> u32 { +pub fn _getenv(ctx: ContextMut<'_, EmEnv>, name: i32) -> u32 { debug!("emscripten::_getenv"); - let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; + let name_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), name) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); @@ -30,11 +30,11 @@ pub fn _getenv(ctx: &EmEnv, name: i32) -> u32 { } /// emscripten: _setenv // (name: *const char, name: *const value, overwrite: int); -pub fn _setenv(ctx: &EmEnv, name: c_int, value: c_int, overwrite: c_int) -> c_int { +pub fn _setenv(ctx: ContextMut<'_, EmEnv>, name: c_int, value: c_int, overwrite: c_int) -> c_int { debug!("emscripten::_setenv"); - let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; - let value_addr = emscripten_memory_pointer!(ctx.memory(0), value) as *const c_char; + let name_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), name) as *const c_char; + let value_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), value) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); debug!("=> value({:?})", unsafe { CStr::from_ptr(value_addr) }); @@ -43,10 +43,10 @@ pub fn _setenv(ctx: &EmEnv, name: c_int, value: c_int, overwrite: c_int) -> c_in } /// emscripten: _putenv // (name: *const char); -pub fn _putenv(ctx: &EmEnv, name: c_int) -> c_int { +pub fn _putenv(ctx: ContextMut<'_, EmEnv>, name: c_int) -> c_int { debug!("emscripten::_putenv"); - let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; + let name_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), name) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); @@ -54,10 +54,10 @@ pub fn _putenv(ctx: &EmEnv, name: c_int) -> c_int { } /// emscripten: _unsetenv // (name: *const char); -pub fn _unsetenv(ctx: &EmEnv, name: c_int) -> c_int { +pub fn _unsetenv(ctx: ContextMut<'_, EmEnv>, name: c_int) -> c_int { debug!("emscripten::_unsetenv"); - let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; + let name_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), name) as *const c_char; debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); @@ -65,7 +65,7 @@ pub fn _unsetenv(ctx: &EmEnv, name: c_int) -> c_int { } #[allow(clippy::cast_ptr_alignment)] -pub fn _getpwnam(ctx: &EmEnv, name_ptr: c_int) -> c_int { +pub fn _getpwnam(mut ctx: ContextMut<'_, EmEnv>, name_ptr: c_int) -> c_int { debug!("emscripten::_getpwnam {}", name_ptr); #[cfg(feature = "debug")] let _ = name_ptr; @@ -82,21 +82,25 @@ pub fn _getpwnam(ctx: &EmEnv, name_ptr: c_int) -> c_int { } let name = unsafe { - let memory_name_ptr = emscripten_memory_pointer!(ctx.memory(0), name_ptr) as *const c_char; + let memory = ctx.data().memory(0); + let memory_name_ptr = emscripten_memory_pointer!(ctx, memory, name_ptr) as *const c_char; CStr::from_ptr(memory_name_ptr) }; unsafe { let passwd = &*libc_getpwnam(name.as_ptr()); - let passwd_struct_offset = call_malloc(ctx, mem::size_of::() as _); + let passwd_struct_offset = + call_malloc(ctx.as_context_mut(), mem::size_of::() as _); + let memory = ctx.data().memory(0); let passwd_struct_ptr = - emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd; - (*passwd_struct_ptr).pw_name = copy_cstr_into_wasm(ctx, passwd.pw_name); - (*passwd_struct_ptr).pw_passwd = copy_cstr_into_wasm(ctx, passwd.pw_passwd); - (*passwd_struct_ptr).pw_gecos = copy_cstr_into_wasm(ctx, passwd.pw_gecos); - (*passwd_struct_ptr).pw_dir = copy_cstr_into_wasm(ctx, passwd.pw_dir); - (*passwd_struct_ptr).pw_shell = copy_cstr_into_wasm(ctx, passwd.pw_shell); + emscripten_memory_pointer!(ctx, memory, passwd_struct_offset) as *mut GuestPasswd; + (*passwd_struct_ptr).pw_name = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_name); + (*passwd_struct_ptr).pw_passwd = + copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_passwd); + (*passwd_struct_ptr).pw_gecos = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_gecos); + (*passwd_struct_ptr).pw_dir = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_dir); + (*passwd_struct_ptr).pw_shell = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_shell); (*passwd_struct_ptr).pw_uid = passwd.pw_uid; (*passwd_struct_ptr).pw_gid = passwd.pw_gid; @@ -105,7 +109,7 @@ pub fn _getpwnam(ctx: &EmEnv, name_ptr: c_int) -> c_int { } #[allow(clippy::cast_ptr_alignment)] -pub fn _getgrnam(ctx: &EmEnv, name_ptr: c_int) -> c_int { +pub fn _getgrnam(mut ctx: ContextMut<'_, EmEnv>, name_ptr: c_int) -> c_int { debug!("emscripten::_getgrnam {}", name_ptr); #[repr(C)] @@ -117,18 +121,21 @@ pub fn _getgrnam(ctx: &EmEnv, name_ptr: c_int) -> c_int { } let name = unsafe { - let memory_name_ptr = emscripten_memory_pointer!(ctx.memory(0), name_ptr) as *const c_char; + let memory_name_ptr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), name_ptr) as *const c_char; CStr::from_ptr(memory_name_ptr) }; unsafe { let group = &*libc_getgrnam(name.as_ptr()); - let group_struct_offset = call_malloc(ctx, mem::size_of::() as _); + let group_struct_offset = + call_malloc(ctx.as_context_mut(), mem::size_of::() as _); let group_struct_ptr = - emscripten_memory_pointer!(ctx.memory(0), group_struct_offset) as *mut GuestGroup; - (*group_struct_ptr).gr_name = copy_cstr_into_wasm(ctx, group.gr_name); - (*group_struct_ptr).gr_passwd = copy_cstr_into_wasm(ctx, group.gr_passwd); + emscripten_memory_pointer!(ctx, ctx.data().memory(0), group_struct_offset) + as *mut GuestGroup; + (*group_struct_ptr).gr_name = copy_cstr_into_wasm(ctx.as_context_mut(), group.gr_name); + (*group_struct_ptr).gr_passwd = copy_cstr_into_wasm(ctx.as_context_mut(), group.gr_passwd); (*group_struct_ptr).gr_gid = group.gr_gid; (*group_struct_ptr).gr_mem = copy_terminated_array_of_cstrs(ctx, group.gr_mem); @@ -136,22 +143,25 @@ pub fn _getgrnam(ctx: &EmEnv, name_ptr: c_int) -> c_int { } } -pub fn _sysconf(_ctx: &EmEnv, name: c_int) -> i32 { +pub fn _sysconf(_ctx: ContextMut<'_, EmEnv>, name: c_int) -> i32 { debug!("emscripten::_sysconf {}", name); // TODO: Implement like emscripten expects regarding memory/page size unsafe { sysconf(name) as i32 } // TODO review i64 } // this may be a memory leak, probably not though because emscripten does the same thing -pub fn _gai_strerror(ctx: &EmEnv, ecode: i32) -> i32 { +pub fn _gai_strerror(mut ctx: ContextMut<'_, EmEnv>, ecode: i32) -> i32 { debug!("emscripten::_gai_strerror({})", ecode); let cstr = unsafe { std::ffi::CStr::from_ptr(libc::gai_strerror(ecode)) }; let bytes = cstr.to_bytes_with_nul(); - let string_on_guest: WasmPtr = call_malloc_with_cast(ctx, bytes.len() as _); - let memory = ctx.memory(0); + let string_on_guest: WasmPtr = + call_malloc_with_cast(ctx.as_context_mut(), bytes.len() as _); + let memory = ctx.data().memory(0); - let writer = string_on_guest.slice(&memory, bytes.len() as _).unwrap(); + let writer = string_on_guest + .slice(&ctx, &memory, bytes.len() as _) + .unwrap(); for (i, byte) in bytes.iter().enumerate() { writer.index(i as u64).write(*byte as _).unwrap(); } @@ -160,7 +170,7 @@ pub fn _gai_strerror(ctx: &EmEnv, ecode: i32) -> i32 { } pub fn _getaddrinfo( - ctx: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, node_ptr: WasmPtr, service_str_ptr: WasmPtr, hints_ptr: WasmPtr, @@ -168,7 +178,7 @@ pub fn _getaddrinfo( ) -> i32 { use libc::{addrinfo, freeaddrinfo}; debug!("emscripten::_getaddrinfo"); - let memory = ctx.memory(0); + let memory = ctx.data().memory(0); debug!(" => node = {}", { if node_ptr.is_null() { std::borrow::Cow::Borrowed("null") @@ -187,7 +197,7 @@ pub fn _getaddrinfo( let hints = if hints_ptr.is_null() { None } else { - let hints_guest = hints_ptr.deref(&memory).read().unwrap(); + let hints_guest = hints_ptr.deref(&ctx, &memory).read().unwrap(); Some(addrinfo { ai_flags: hints_guest.ai_flags, ai_family: hints_guest.ai_family, @@ -234,14 +244,14 @@ pub fn _getaddrinfo( while !current_host_node.is_null() { let current_guest_node_ptr: WasmPtr = - call_malloc_with_cast(ctx, std::mem::size_of::() as _); + call_malloc_with_cast(ctx.as_context_mut(), std::mem::size_of::() as _); if head_of_list.is_none() { head_of_list = Some(current_guest_node_ptr); } // connect list if let Some(prev_guest) = previous_guest_node { - let derefed_prev_guest = prev_guest.deref(&memory); + let derefed_prev_guest = prev_guest.deref(&ctx, &memory); let mut pg = derefed_prev_guest.read().unwrap(); pg.ai_next = current_guest_node_ptr; derefed_prev_guest.write(pg).unwrap(); @@ -254,9 +264,9 @@ pub fn _getaddrinfo( let guest_sockaddr_ptr = { let host_sockaddr_ptr = (*current_host_node).ai_addr; let guest_sockaddr_ptr: WasmPtr = - call_malloc_with_cast(ctx, host_addrlen as _); + call_malloc_with_cast(ctx.as_context_mut(), host_addrlen as _); - let derefed_guest_sockaddr = guest_sockaddr_ptr.deref(&memory); + let derefed_guest_sockaddr = guest_sockaddr_ptr.deref(&ctx, &memory); let mut gs = derefed_guest_sockaddr.read().unwrap(); gs.sa_family = (*host_sockaddr_ptr).sa_family as i16; gs.sa_data = (*host_sockaddr_ptr).sa_data; @@ -273,10 +283,10 @@ pub fn _getaddrinfo( let canonname_bytes = canonname_cstr.to_bytes_with_nul(); let str_size = canonname_bytes.len(); let guest_canonname: WasmPtr = - call_malloc_with_cast(ctx, str_size as _); + call_malloc_with_cast(ctx.as_context_mut(), str_size as _); let guest_canonname_writer = - guest_canonname.slice(&memory, str_size as _).unwrap(); + guest_canonname.slice(&ctx, &memory, str_size as _).unwrap(); for (i, b) in canonname_bytes.iter().enumerate() { guest_canonname_writer .index(i as u64) @@ -290,7 +300,7 @@ pub fn _getaddrinfo( } }; - let derefed_current_guest_node = current_guest_node_ptr.deref(&memory); + let derefed_current_guest_node = current_guest_node_ptr.deref(&ctx, &memory); let mut cgn = derefed_current_guest_node.read().unwrap(); cgn.ai_flags = (*current_host_node).ai_flags; cgn.ai_family = (*current_host_node).ai_family; @@ -310,7 +320,10 @@ pub fn _getaddrinfo( head_of_list.unwrap_or_else(|| WasmPtr::new(0)) }; - res_val_ptr.deref(&memory).write(head_of_list).unwrap(); + res_val_ptr + .deref(&ctx, &memory) + .write(head_of_list) + .unwrap(); 0 } diff --git a/lib/emscripten/src/errno.rs b/lib/emscripten/src/errno.rs index fcbbe22dfee..fb5681de7c8 100644 --- a/lib/emscripten/src/errno.rs +++ b/lib/emscripten/src/errno.rs @@ -1,7 +1,8 @@ // use std::collections::HashMap; use crate::EmEnv; +use wasmer::ContextMut; -pub fn ___seterrno(_ctx: &EmEnv, _value: i32) { +pub fn ___seterrno(mut _ctx: ContextMut<'_, EmEnv>, _value: i32) { debug!("emscripten::___seterrno {}", _value); // TODO: Incomplete impl eprintln!("failed to set errno!"); diff --git a/lib/emscripten/src/exception.rs b/lib/emscripten/src/exception.rs index 1f781dd652d..c260f1a51d3 100644 --- a/lib/emscripten/src/exception.rs +++ b/lib/emscripten/src/exception.rs @@ -1,56 +1,57 @@ use super::env; use super::process::_abort; use crate::EmEnv; +use wasmer::ContextMut; /// emscripten: ___cxa_allocate_exception -pub fn ___cxa_allocate_exception(ctx: &EmEnv, size: u32) -> u32 { +pub fn ___cxa_allocate_exception(ctx: ContextMut<'_, EmEnv>, size: u32) -> u32 { debug!("emscripten::___cxa_allocate_exception"); env::call_malloc(ctx, size as _) } -pub fn ___cxa_current_primary_exception(_ctx: &EmEnv) -> u32 { +pub fn ___cxa_current_primary_exception(_ctx: ContextMut<'_, EmEnv>) -> u32 { debug!("emscripten::___cxa_current_primary_exception"); unimplemented!("emscripten::___cxa_current_primary_exception") } -pub fn ___cxa_decrement_exception_refcount(_ctx: &EmEnv, _a: u32) { +pub fn ___cxa_decrement_exception_refcount(_ctx: ContextMut<'_, EmEnv>, _a: u32) { debug!("emscripten::___cxa_decrement_exception_refcount({})", _a); unimplemented!("emscripten::___cxa_decrement_exception_refcount({})", _a) } -pub fn ___cxa_increment_exception_refcount(_ctx: &EmEnv, _a: u32) { +pub fn ___cxa_increment_exception_refcount(_ctx: ContextMut<'_, EmEnv>, _a: u32) { debug!("emscripten::___cxa_increment_exception_refcount({})", _a); unimplemented!("emscripten::___cxa_increment_exception_refcount({})", _a) } -pub fn ___cxa_rethrow_primary_exception(_ctx: &EmEnv, _a: u32) { +pub fn ___cxa_rethrow_primary_exception(_ctx: ContextMut<'_, EmEnv>, _a: u32) { debug!("emscripten::___cxa_rethrow_primary_exception({})", _a); unimplemented!("emscripten::___cxa_rethrow_primary_exception({})", _a) } /// emscripten: ___cxa_throw /// TODO: We don't have support for exceptions yet -pub fn ___cxa_throw(ctx: &EmEnv, _ptr: u32, _ty: u32, _destructor: u32) { +pub fn ___cxa_throw(ctx: ContextMut<'_, EmEnv>, _ptr: u32, _ty: u32, _destructor: u32) { debug!("emscripten::___cxa_throw"); eprintln!("Throwing exceptions not yet implemented: aborting!"); _abort(ctx); } -pub fn ___cxa_begin_catch(_ctx: &EmEnv, _exception_object_ptr: u32) -> i32 { +pub fn ___cxa_begin_catch(_ctx: ContextMut<'_, EmEnv>, _exception_object_ptr: u32) -> i32 { debug!("emscripten::___cxa_begin_catch"); -1 } -pub fn ___cxa_end_catch(_ctx: &EmEnv) { +pub fn ___cxa_end_catch(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::___cxa_end_catch"); } -pub fn ___cxa_uncaught_exception(_ctx: &EmEnv) -> i32 { +pub fn ___cxa_uncaught_exception(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::___cxa_uncaught_exception"); -1 } -pub fn ___cxa_pure_virtual(_ctx: &EmEnv) { +pub fn ___cxa_pure_virtual(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::___cxa_pure_virtual"); // ABORT = true panic!("Pure virtual function called!"); diff --git a/lib/emscripten/src/exec.rs b/lib/emscripten/src/exec.rs index 12afe965963..0fa5902d947 100644 --- a/lib/emscripten/src/exec.rs +++ b/lib/emscripten/src/exec.rs @@ -2,27 +2,27 @@ use crate::varargs::VarArgs; use crate::EmEnv; use libc::execvp as libc_execvp; use std::ffi::CString; -use wasmer::WasmPtr; +use wasmer::{ContextMut, WasmPtr}; -pub fn execvp(ctx: &EmEnv, command_name_offset: u32, argv_offset: u32) -> i32 { +pub fn execvp(ctx: ContextMut<'_, EmEnv>, command_name_offset: u32, argv_offset: u32) -> i32 { // a single reference to re-use - let emscripten_memory = ctx.memory(0); + let emscripten_memory = ctx.data().memory(0); // read command name as string let command_name_string_vec = WasmPtr::::new(command_name_offset) - .read_until(&emscripten_memory, |&byte| byte == 0) + .read_until(&ctx, &emscripten_memory, |&byte| byte == 0) .unwrap(); let command_name_string = CString::new(command_name_string_vec).unwrap(); // get the array of args let argv = WasmPtr::>::new(argv_offset) - .read_until(&emscripten_memory, |&ptr| ptr.is_null()) + .read_until(&ctx, &emscripten_memory, |&ptr| ptr.is_null()) .unwrap(); let arg_strings: Vec = argv .into_iter() .map(|ptr| { let vec = ptr - .read_until(&emscripten_memory, |&byte| byte == 0) + .read_until(&ctx, &emscripten_memory, |&byte| byte == 0) .unwrap(); CString::new(vec).unwrap() }) @@ -39,13 +39,23 @@ pub fn execvp(ctx: &EmEnv, command_name_offset: u32, argv_offset: u32) -> i32 { } /// execl -pub fn execl(_ctx: &EmEnv, _path_ptr: i32, _arg0_ptr: i32, _varargs: VarArgs) -> i32 { +pub fn execl( + _ctx: ContextMut<'_, EmEnv>, + _path_ptr: i32, + _arg0_ptr: i32, + _varargs: VarArgs, +) -> i32 { debug!("emscripten::execl"); -1 } /// execle -pub fn execle(_ctx: &EmEnv, _path_ptr: i32, _arg0_ptr: i32, _varargs: VarArgs) -> i32 { +pub fn execle( + _ctx: ContextMut<'_, EmEnv>, + _path_ptr: i32, + _arg0_ptr: i32, + _varargs: VarArgs, +) -> i32 { debug!("emscripten::execle"); -1 } diff --git a/lib/emscripten/src/exit.rs b/lib/emscripten/src/exit.rs index 9518d256712..8ef28cceb56 100644 --- a/lib/emscripten/src/exit.rs +++ b/lib/emscripten/src/exit.rs @@ -1,7 +1,8 @@ use crate::EmEnv; +use wasmer::ContextMut; // __exit -pub fn exit(_ctx: &EmEnv, value: i32) { +pub fn exit(mut _ctx: ContextMut<'_, EmEnv>, value: i32) { debug!("emscripten::exit {}", value); ::std::process::exit(value); } diff --git a/lib/emscripten/src/inet.rs b/lib/emscripten/src/inet.rs index ff0267d88a7..b1a59a0feaf 100644 --- a/lib/emscripten/src/inet.rs +++ b/lib/emscripten/src/inet.rs @@ -1,6 +1,7 @@ use crate::EmEnv; +use wasmer::ContextMut; -pub fn addr(_ctx: &EmEnv, _cp: i32) -> i32 { +pub fn addr(mut _ctx: ContextMut<'_, EmEnv>, _cp: i32) -> i32 { debug!("inet::addr({})", _cp); 0 } diff --git a/lib/emscripten/src/io/mod.rs b/lib/emscripten/src/io/mod.rs index 3c0a9d63b66..0971c10d716 100644 --- a/lib/emscripten/src/io/mod.rs +++ b/lib/emscripten/src/io/mod.rs @@ -11,25 +11,26 @@ pub use self::unix::*; pub use self::windows::*; use crate::EmEnv; +use wasmer::ContextMut; /// getprotobyname -pub fn getprotobyname(_ctx: &EmEnv, _name_ptr: i32) -> i32 { +pub fn getprotobyname(_ctx: ContextMut<'_, EmEnv>, _name_ptr: i32) -> i32 { debug!("emscripten::getprotobyname"); unimplemented!("emscripten::getprotobyname") } /// getprotobynumber -pub fn getprotobynumber(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn getprotobynumber(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::getprotobynumber"); unimplemented!("emscripten::getprotobynumber") } /// sigdelset -pub fn sigdelset(ctx: &EmEnv, set: i32, signum: i32) -> i32 { +pub fn sigdelset(ctx: ContextMut<'_, EmEnv>, set: i32, signum: i32) -> i32 { debug!("emscripten::sigdelset"); - let memory = ctx.memory(0); + let memory = ctx.data().memory(0); #[allow(clippy::cast_ptr_alignment)] - let ptr = emscripten_memory_pointer!(memory, set) as *mut i32; + let ptr = emscripten_memory_pointer!(ctx, memory, set) as *mut i32; unsafe { *ptr &= !(1 << (signum - 1)) } @@ -37,11 +38,11 @@ pub fn sigdelset(ctx: &EmEnv, set: i32, signum: i32) -> i32 { } /// sigfillset -pub fn sigfillset(ctx: &EmEnv, set: i32) -> i32 { +pub fn sigfillset(ctx: ContextMut<'_, EmEnv>, set: i32) -> i32 { debug!("emscripten::sigfillset"); - let memory = ctx.memory(0); + let memory = ctx.data().memory(0); #[allow(clippy::cast_ptr_alignment)] - let ptr = emscripten_memory_pointer!(memory, set) as *mut i32; + let ptr = emscripten_memory_pointer!(ctx, memory, set) as *mut i32; unsafe { *ptr = -1; @@ -51,13 +52,13 @@ pub fn sigfillset(ctx: &EmEnv, set: i32) -> i32 { } /// tzset -pub fn tzset(_ctx: &EmEnv) { +pub fn tzset(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::tzset - stub"); //unimplemented!("emscripten::tzset - stub") } /// strptime -pub fn strptime(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn strptime(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::strptime"); unimplemented!("emscripten::strptime") } diff --git a/lib/emscripten/src/io/unix.rs b/lib/emscripten/src/io/unix.rs index 62190653fbf..0e01580778b 100644 --- a/lib/emscripten/src/io/unix.rs +++ b/lib/emscripten/src/io/unix.rs @@ -4,31 +4,32 @@ use libc::{chroot as _chroot, getpwuid as _getpwuid, printf as _printf}; use std::mem; use crate::EmEnv; +use wasmer::{AsContextMut, ContextMut}; /// putchar -pub fn putchar(_ctx: &EmEnv, chr: i32) { +pub fn putchar(_ctx: ContextMut<'_, EmEnv>, chr: i32) { unsafe { libc::putchar(chr) }; } /// printf -pub fn printf(ctx: &EmEnv, memory_offset: i32, extra: i32) -> i32 { +pub fn printf(ctx: ContextMut<'_, EmEnv>, memory_offset: i32, extra: i32) -> i32 { debug!("emscripten::printf {}, {}", memory_offset, extra); unsafe { - let addr = emscripten_memory_pointer!(ctx.memory(0), memory_offset) as _; + let addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), memory_offset) as _; _printf(addr, extra) } } /// chroot -pub fn chroot(ctx: &EmEnv, name_ptr: i32) -> i32 { +pub fn chroot(ctx: ContextMut<'_, EmEnv>, name_ptr: i32) -> i32 { debug!("emscripten::chroot"); - let name = emscripten_memory_pointer!(ctx.memory(0), name_ptr) as *const i8; + let name = emscripten_memory_pointer!(ctx, ctx.data().memory(0), name_ptr) as *const i8; unsafe { _chroot(name as *const _) } } /// getpwuid #[allow(clippy::cast_ptr_alignment)] -pub fn getpwuid(ctx: &EmEnv, uid: i32) -> i32 { +pub fn getpwuid(mut ctx: ContextMut<'_, EmEnv>, uid: i32) -> i32 { debug!("emscripten::getpwuid {}", uid); #[repr(C)] @@ -44,18 +45,21 @@ pub fn getpwuid(ctx: &EmEnv, uid: i32) -> i32 { unsafe { let passwd = &*_getpwuid(uid as _); - let passwd_struct_offset = call_malloc(ctx, mem::size_of::() as _); + let passwd_struct_offset = + call_malloc(ctx.as_context_mut(), mem::size_of::() as _); let passwd_struct_ptr = - emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd; + emscripten_memory_pointer!(ctx, ctx.data().memory(0), passwd_struct_offset) + as *mut GuestPasswd; assert_eq!( passwd_struct_ptr as usize % std::mem::align_of::(), 0 ); - (*passwd_struct_ptr).pw_name = copy_cstr_into_wasm(ctx, passwd.pw_name); - (*passwd_struct_ptr).pw_passwd = copy_cstr_into_wasm(ctx, passwd.pw_passwd); - (*passwd_struct_ptr).pw_gecos = copy_cstr_into_wasm(ctx, passwd.pw_gecos); - (*passwd_struct_ptr).pw_dir = copy_cstr_into_wasm(ctx, passwd.pw_dir); - (*passwd_struct_ptr).pw_shell = copy_cstr_into_wasm(ctx, passwd.pw_shell); + (*passwd_struct_ptr).pw_name = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_name); + (*passwd_struct_ptr).pw_passwd = + copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_passwd); + (*passwd_struct_ptr).pw_gecos = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_gecos); + (*passwd_struct_ptr).pw_dir = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_dir); + (*passwd_struct_ptr).pw_shell = copy_cstr_into_wasm(ctx.as_context_mut(), passwd.pw_shell); (*passwd_struct_ptr).pw_uid = passwd.pw_uid; (*passwd_struct_ptr).pw_gid = passwd.pw_gid; diff --git a/lib/emscripten/src/jmp.rs b/lib/emscripten/src/jmp.rs index 93651b1016a..9ac824577cc 100644 --- a/lib/emscripten/src/jmp.rs +++ b/lib/emscripten/src/jmp.rs @@ -1,13 +1,14 @@ -use super::env::get_emscripten_data; +use super::env::get_emscripten_funcs; use super::process::abort_with_message; use libc::c_int; // use std::cell::UnsafeCell; use crate::EmEnv; use std::error::Error; use std::fmt; +use wasmer::{AsContextMut, ContextMut}; /// setjmp -pub fn __setjmp(ctx: &EmEnv, _env_addr: u32) -> c_int { +pub fn __setjmp(ctx: ContextMut<'_, EmEnv>, _env_addr: u32) -> c_int { debug!("emscripten::__setjmp (setjmp)"); abort_with_message(ctx, "missing function: _setjmp"); unreachable!() @@ -18,7 +19,7 @@ pub fn __setjmp(ctx: &EmEnv, _env_addr: u32) -> c_int { // let jump_index = emscripten_memory_pointer!(ctx.memory(0), env_addr) as *mut i8; // // We create the jump buffer outside of the wasm memory // let jump_buf: UnsafeCell<[u32; 27]> = UnsafeCell::new([0; 27]); - // let jumps = &mut get_emscripten_data(ctx).jumps; + // let jumps = &mut get_emscripten_data(&ctx).jumps; // let result = setjmp(jump_buf.get() as _); // // We set the jump index to be the last 3value of jumps // *jump_index = jumps.len() as _; @@ -30,13 +31,13 @@ pub fn __setjmp(ctx: &EmEnv, _env_addr: u32) -> c_int { /// longjmp #[allow(unreachable_code)] -pub fn __longjmp(ctx: &EmEnv, _env_addr: u32, _val: c_int) { +pub fn __longjmp(ctx: ContextMut<'_, EmEnv>, _env_addr: u32, _val: c_int) { debug!("emscripten::__longjmp (longmp)"); abort_with_message(ctx, "missing function: _longjmp"); // unsafe { // // We retrieve the jump index from the env address // let jump_index = emscripten_memory_pointer!(ctx.memory(0), env_addr) as *mut i8; - // let jumps = &mut get_emscripten_data(ctx).jumps; + // let jumps = &mut get_emscripten_data(&ctx).jumps; // // We get the real jump buffer from the jumps vector, using the retrieved index // let jump_buf = &jumps[*jump_index as usize]; // longjmp(jump_buf.get() as _, val) @@ -57,12 +58,18 @@ impl Error for LongJumpRet {} /// _longjmp // This function differs from the js implementation, it should return Result<(), &'static str> #[allow(unreachable_code)] -pub fn _longjmp(ctx: &EmEnv, env_addr: i32, val: c_int) -> Result<(), LongJumpRet> { +pub fn _longjmp( + mut ctx: ContextMut<'_, EmEnv>, + env_addr: i32, + val: c_int, +) -> Result<(), LongJumpRet> { let val = if val == 0 { 1 } else { val }; - get_emscripten_data(ctx) + let threw = get_emscripten_funcs(&ctx) .set_threw_ref() .expect("set_threw is None") - .call(env_addr, val) + .clone(); + threw + .call(&mut ctx.as_context_mut(), env_addr, val) .expect("set_threw failed to call"); Err(LongJumpRet) } diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 0b8cb464e6d..1bbe534d652 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -19,10 +19,11 @@ use std::f64; use std::path::PathBuf; use std::sync::{Arc, Mutex, RwLock}; use wasmer::{ - imports, namespace, Exports, Function, FunctionType, Global, Imports, Instance, LazyInit, - Memory, MemoryType, Module, NativeFunc, Pages, RuntimeError, Store, Table, TableType, Val, - ValType, WasmPtr, WasmerEnv, + imports, namespace, AsContextMut, ContextMut, Exports, Function, FunctionType, Global, Imports, + Instance, Memory, MemoryType, Module, NativeFunc, Pages, RuntimeError, Table, TableType, Value, + WasmPtr, }; +use wasmer_types::Type as ValType; #[cfg(unix)] use ::libc::DIR as LibcDir; @@ -71,22 +72,17 @@ pub use self::utils::{ /// The environment provided to the Emscripten imports. pub struct EmEnv { memory: Arc>>, - data: Arc>, -} - -impl WasmerEnv for EmEnv { - fn init_with_instance(&mut self, instance: &Instance) -> Result<(), wasmer::HostEnvInitError> { - let mut ed = self.data.lock().unwrap(); - ed.init_with_instance(instance)?; - Ok(()) - } + data: Arc>>, + funcs: Arc>, } impl EmEnv { - pub fn new(data: &EmscriptenGlobalsData, mapped_dirs: HashMap) -> Self { + /// Create a new EmEnv, with default value to be set later (set_memory, set_functions and set_data) + pub fn new() -> Self { Self { memory: Arc::new(RwLock::new(None)), - data: Arc::new(Mutex::new(EmscriptenData::new(data.clone(), mapped_dirs))), + data: Arc::new(Mutex::new(None)), + funcs: Arc::new(Mutex::new(EmscriptenFunctions::new())), } } @@ -99,6 +95,19 @@ impl EmEnv { pub fn memory(&self, _mem_idx: u32) -> Memory { (&*self.memory.read().unwrap()).as_ref().cloned().unwrap() } + + pub fn set_functions(&mut self, funcs: EmscriptenFunctions) { + self.funcs = Arc::new(Mutex::new(funcs)); + } + + pub fn set_data( + &mut self, + data: &EmscriptenGlobalsData, + mapped_dirs: HashMap, + ) { + let mut w = self.data.lock().unwrap(); + *w = Some(EmscriptenData::new(data.clone(), mapped_dirs)); + } } #[derive(Debug, Clone)] @@ -132,145 +141,90 @@ lazy_static! { const GLOBAL_BASE: u32 = 1024; const STATIC_BASE: u32 = GLOBAL_BASE; -#[derive(WasmerEnv, Clone, Default)] -pub struct EmscriptenData { - pub globals: EmscriptenGlobalsData, - - #[wasmer(export(alias = "_malloc", optional = true))] - pub malloc: LazyInit>, - #[wasmer(export(alias = "_free", optional = true))] - pub free: LazyInit>, - #[wasmer(export(alias = "_memalign", optional = true))] - pub memalign: LazyInit>, - #[wasmer(export(alias = "_memset", optional = true))] - pub memset: LazyInit>, - #[wasmer(export(name = "stackAlloc", optional = true))] - pub stack_alloc: LazyInit>, - pub jumps: Arc>>, - pub opened_dirs: HashMap>, - - #[wasmer(export(name = "dynCall_i", optional = true))] - pub dyn_call_i: LazyInit>, - #[wasmer(export(name = "dynCall_ii", optional = true))] - pub dyn_call_ii: LazyInit>, - #[wasmer(export(name = "dynCall_iii", optional = true))] - pub dyn_call_iii: LazyInit>, - #[wasmer(export(name = "dynCall_iiii", optional = true))] - pub dyn_call_iiii: LazyInit>, - #[wasmer(export(name = "dynCall_iifi", optional = true))] - pub dyn_call_iifi: LazyInit>, - #[wasmer(export(name = "dynCall_v", optional = true))] - pub dyn_call_v: LazyInit>, - #[wasmer(export(name = "dynCall_vi", optional = true))] - pub dyn_call_vi: LazyInit>, - #[wasmer(export(name = "dynCall_vii", optional = true))] - pub dyn_call_vii: LazyInit>, - #[wasmer(export(name = "dynCall_viii", optional = true))] - pub dyn_call_viii: LazyInit>, - #[wasmer(export(name = "dynCall_viiii", optional = true))] - pub dyn_call_viiii: LazyInit>, +#[derive(Clone, Default)] +pub struct EmscriptenFunctions { + pub malloc: Option>, + pub free: Option>, + pub memalign: Option>, + pub memset: Option>, + pub stack_alloc: Option>, + + pub dyn_call_i: Option>, + pub dyn_call_ii: Option>, + pub dyn_call_iii: Option>, + pub dyn_call_iiii: Option>, + pub dyn_call_iifi: Option>, + pub dyn_call_v: Option>, + pub dyn_call_vi: Option>, + pub dyn_call_vii: Option>, + pub dyn_call_viii: Option>, + pub dyn_call_viiii: Option>, // round 2 - #[wasmer(export(name = "dynCall_dii", optional = true))] - pub dyn_call_dii: LazyInit>, - #[wasmer(export(name = "dynCall_diiii", optional = true))] - pub dyn_call_diiii: LazyInit>, - #[wasmer(export(name = "dynCall_iiiii", optional = true))] - pub dyn_call_iiiii: LazyInit>, - #[wasmer(export(name = "dynCall_iiiiii", optional = true))] - pub dyn_call_iiiiii: LazyInit>, - #[wasmer(export(name = "dynCall_iiiiiii", optional = true))] - pub dyn_call_iiiiiii: LazyInit>, - #[wasmer(export(name = "dynCall_iiiiiiii", optional = true))] - pub dyn_call_iiiiiiii: LazyInit>, - #[wasmer(export(name = "dynCall_iiiiiiiii", optional = true))] - pub dyn_call_iiiiiiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_iiiiiiiiii", optional = true))] + pub dyn_call_dii: Option>, + pub dyn_call_diiii: Option>, + pub dyn_call_iiiii: Option>, + pub dyn_call_iiiiii: Option>, + pub dyn_call_iiiiiii: Option>, + pub dyn_call_iiiiiiii: Option>, + pub dyn_call_iiiiiiiii: Option>, pub dyn_call_iiiiiiiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_iiiiiiiiiii", optional = true))] + Option>, pub dyn_call_iiiiiiiiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_vd", optional = true))] - pub dyn_call_vd: LazyInit>, - #[wasmer(export(name = "dynCall_viiiii", optional = true))] - pub dyn_call_viiiii: LazyInit>, - #[wasmer(export(name = "dynCall_viiiiii", optional = true))] - pub dyn_call_viiiiii: LazyInit>, - #[wasmer(export(name = "dynCall_viiiiiii", optional = true))] - pub dyn_call_viiiiiii: LazyInit>, - #[wasmer(export(name = "dynCall_viiiiiiii", optional = true))] - pub dyn_call_viiiiiiii: LazyInit>, - #[wasmer(export(name = "dynCall_viiiiiiiii", optional = true))] + Option>, + pub dyn_call_vd: Option>, + pub dyn_call_viiiii: Option>, + pub dyn_call_viiiiii: Option>, + pub dyn_call_viiiiiii: Option>, + pub dyn_call_viiiiiiii: Option>, pub dyn_call_viiiiiiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_viiiiiiiiii", optional = true))] + Option>, pub dyn_call_viiiiiiiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_iij", optional = true))] - pub dyn_call_iij: LazyInit>, - #[wasmer(export(name = "dynCall_iji", optional = true))] - pub dyn_call_iji: LazyInit>, - #[wasmer(export(name = "dynCall_iiji", optional = true))] - pub dyn_call_iiji: LazyInit>, - #[wasmer(export(name = "dynCall_iiijj", optional = true))] - pub dyn_call_iiijj: LazyInit>, - #[wasmer(export(name = "dynCall_j", optional = true))] - pub dyn_call_j: LazyInit>, - #[wasmer(export(name = "dynCall_ji", optional = true))] - pub dyn_call_ji: LazyInit>, - #[wasmer(export(name = "dynCall_jii", optional = true))] - pub dyn_call_jii: LazyInit>, - #[wasmer(export(name = "dynCall_jij", optional = true))] - pub dyn_call_jij: LazyInit>, - #[wasmer(export(name = "dynCall_jjj", optional = true))] - pub dyn_call_jjj: LazyInit>, - #[wasmer(export(name = "dynCall_viiij", optional = true))] - pub dyn_call_viiij: LazyInit>, - #[wasmer(export(name = "dynCall_viiijiiii", optional = true))] + Option>, + pub dyn_call_iij: Option>, + pub dyn_call_iji: Option>, + pub dyn_call_iiji: Option>, + pub dyn_call_iiijj: Option>, + pub dyn_call_j: Option>, + pub dyn_call_ji: Option>, + pub dyn_call_jii: Option>, + pub dyn_call_jij: Option>, + pub dyn_call_jjj: Option>, + pub dyn_call_viiij: Option>, pub dyn_call_viiijiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_viiijiiiiii", optional = true))] + Option>, pub dyn_call_viiijiiiiii: - LazyInit>, - #[wasmer(export(name = "dynCall_viij", optional = true))] - pub dyn_call_viij: LazyInit>, - #[wasmer(export(name = "dynCall_viiji", optional = true))] - pub dyn_call_viiji: LazyInit>, - #[wasmer(export(name = "dynCall_viijiii", optional = true))] - pub dyn_call_viijiii: LazyInit>, - #[wasmer(export(name = "dynCall_viijj", optional = true))] - pub dyn_call_viijj: LazyInit>, - #[wasmer(export(name = "dynCall_vj", optional = true))] - pub dyn_call_vj: LazyInit>, - #[wasmer(export(name = "dynCall_vjji", optional = true))] - pub dyn_call_vjji: LazyInit>, - #[wasmer(export(name = "dynCall_vij", optional = true))] - pub dyn_call_vij: LazyInit>, - #[wasmer(export(name = "dynCall_viji", optional = true))] - pub dyn_call_viji: LazyInit>, - #[wasmer(export(name = "dynCall_vijiii", optional = true))] - pub dyn_call_vijiii: LazyInit>, - #[wasmer(export(name = "dynCall_vijj", optional = true))] - pub dyn_call_vijj: LazyInit>, - #[wasmer(export(name = "dynCall_viid", optional = true))] - pub dyn_call_viid: LazyInit>, - #[wasmer(export(name = "dynCall_vidd", optional = true))] - pub dyn_call_vidd: LazyInit>, - #[wasmer(export(name = "dynCall_viidii", optional = true))] - pub dyn_call_viidii: LazyInit>, - #[wasmer(export(name = "dynCall_viidddddddd", optional = true))] + Option>, + pub dyn_call_viij: Option>, + pub dyn_call_viiji: Option>, + pub dyn_call_viijiii: Option>, + pub dyn_call_viijj: Option>, + pub dyn_call_vj: Option>, + pub dyn_call_vjji: Option>, + pub dyn_call_vij: Option>, + pub dyn_call_viji: Option>, + pub dyn_call_vijiii: Option>, + pub dyn_call_vijj: Option>, + pub dyn_call_viid: Option>, + pub dyn_call_vidd: Option>, + pub dyn_call_viidii: Option>, pub dyn_call_viidddddddd: - LazyInit>, + Option>, + + pub stack_save: Option>, + pub stack_restore: Option>, + pub set_threw: Option>, +} + +#[derive(Clone, Default)] +pub struct EmscriptenData { + pub globals: EmscriptenGlobalsData, + + pub jumps: Arc>>, + pub opened_dirs: HashMap>, + pub temp_ret_0: i32, - #[wasmer(export(name = "stackSave", optional = true))] - pub stack_save: LazyInit>, - #[wasmer(export(name = "stackRestore", optional = true))] - pub stack_restore: LazyInit>, - #[wasmer(export(name = "setThrew", alias = "_setThrew", optional = true))] - pub set_threw: LazyInit>, pub mapped_dirs: HashMap, } @@ -288,23 +242,250 @@ impl EmscriptenData { } } +impl EmscriptenFunctions { + pub fn new() -> EmscriptenFunctions { + EmscriptenFunctions { + ..Default::default() + } + } + pub fn malloc_ref(&self) -> Option<&NativeFunc> { + self.malloc.as_ref() + } + pub fn free_ref(&self) -> Option<&NativeFunc> { + self.free.as_ref() + } + pub fn memalign_ref(&self) -> Option<&NativeFunc<(u32, u32), u32>> { + self.memalign.as_ref() + } + pub fn memset_ref(&self) -> Option<&NativeFunc<(u32, u32, u32), u32>> { + self.memset.as_ref() + } + pub fn stack_alloc_ref(&self) -> Option<&NativeFunc> { + self.stack_alloc.as_ref() + } + + pub fn dyn_call_i_ref(&self) -> Option<&NativeFunc> { + self.dyn_call_i.as_ref() + } + pub fn dyn_call_ii_ref(&self) -> Option<&NativeFunc<(i32, i32), i32>> { + self.dyn_call_ii.as_ref() + } + pub fn dyn_call_iii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32), i32>> { + self.dyn_call_iii.as_ref() + } + pub fn dyn_call_iiii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32), i32>> { + self.dyn_call_iiii.as_ref() + } + pub fn dyn_call_iifi_ref(&self) -> Option<&NativeFunc<(i32, i32, f64, i32), i32>> { + self.dyn_call_iifi.as_ref() + } + pub fn dyn_call_v_ref(&self) -> Option<&NativeFunc> { + self.dyn_call_v.as_ref() + } + pub fn dyn_call_vi_ref(&self) -> Option<&NativeFunc<(i32, i32), ()>> { + self.dyn_call_vi.as_ref() + } + pub fn dyn_call_vii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32), ()>> { + self.dyn_call_vii.as_ref() + } + pub fn dyn_call_viii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32), ()>> { + self.dyn_call_viii.as_ref() + } + pub fn dyn_call_viiii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiii.as_ref() + } + pub fn dyn_call_dii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32), f64>> { + self.dyn_call_dii.as_ref() + } + pub fn dyn_call_diiii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), f64>> { + self.dyn_call_diiii.as_ref() + } + pub fn dyn_call_iiiii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiii.as_ref() + } + pub fn dyn_call_iiiiii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiiii.as_ref() + } + pub fn dyn_call_iiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiiiii.as_ref() + } + pub fn dyn_call_iiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiiiiii.as_ref() + } + pub fn dyn_call_iiiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiiiiiii.as_ref() + } + pub fn dyn_call_iiiiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiiiiiiii.as_ref() + } + pub fn dyn_call_iiiiiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiiiiiiiiii.as_ref() + } + pub fn dyn_call_vd_ref(&self) -> Option<&NativeFunc<(i32, f64), ()>> { + self.dyn_call_vd.as_ref() + } + pub fn dyn_call_viiiii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiiii.as_ref() + } + pub fn dyn_call_viiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiiiii.as_ref() + } + pub fn dyn_call_viiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiiiiii.as_ref() + } + pub fn dyn_call_viiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiiiiiii.as_ref() + } + pub fn dyn_call_viiiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiiiiiiii.as_ref() + } + pub fn dyn_call_viiiiiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiiiiiiiii.as_ref() + } + pub fn dyn_call_iij_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32), i32>> { + self.dyn_call_iij.as_ref() + } + pub fn dyn_call_iji_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32), i32>> { + self.dyn_call_iji.as_ref() + } + pub fn dyn_call_iiji_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiji.as_ref() + } + pub fn dyn_call_iiijj_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32), i32>> { + self.dyn_call_iiijj.as_ref() + } + pub fn dyn_call_j_ref(&self) -> Option<&NativeFunc> { + self.dyn_call_j.as_ref() + } + pub fn dyn_call_ji_ref(&self) -> Option<&NativeFunc<(i32, i32), i32>> { + self.dyn_call_ji.as_ref() + } + pub fn dyn_call_jii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32), i32>> { + self.dyn_call_jii.as_ref() + } + pub fn dyn_call_jij_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32), i32>> { + self.dyn_call_jij.as_ref() + } + pub fn dyn_call_jjj_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), i32>> { + self.dyn_call_jjj.as_ref() + } + pub fn dyn_call_viiij_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiij.as_ref() + } + pub fn dyn_call_viiijiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiijiiii.as_ref() + } + pub fn dyn_call_viiijiiiiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiijiiiiii.as_ref() + } + pub fn dyn_call_viij_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viij.as_ref() + } + pub fn dyn_call_viiji_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viiji.as_ref() + } + pub fn dyn_call_viijiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viijiii.as_ref() + } + pub fn dyn_call_viijj_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viijj.as_ref() + } + pub fn dyn_call_vj_ref(&self) -> Option<&NativeFunc<(i32, i32, i32), ()>> { + self.dyn_call_vj.as_ref() + } + pub fn dyn_call_vjji_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_vjji.as_ref() + } + pub fn dyn_call_vij_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32), ()>> { + self.dyn_call_vij.as_ref() + } + pub fn dyn_call_viji_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32), ()>> { + self.dyn_call_viji.as_ref() + } + pub fn dyn_call_vijiii_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_vijiii.as_ref() + } + pub fn dyn_call_vijj_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, i32, i32, i32), ()>> { + self.dyn_call_vijj.as_ref() + } + pub fn dyn_call_viid_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, f64), ()>> { + self.dyn_call_viid.as_ref() + } + pub fn dyn_call_vidd_ref(&self) -> Option<&NativeFunc<(i32, i32, f64, f64), ()>> { + self.dyn_call_vidd.as_ref() + } + pub fn dyn_call_viidii_ref(&self) -> Option<&NativeFunc<(i32, i32, i32, f64, i32, i32), ()>> { + self.dyn_call_viidii.as_ref() + } + pub fn dyn_call_viidddddddd_ref( + &self, + ) -> Option<&NativeFunc<(i32, i32, i32, f64, f64, f64, f64, f64, f64, f64, f64), ()>> { + self.dyn_call_viidddddddd.as_ref() + } + + pub fn stack_save_ref(&self) -> Option<&NativeFunc<(), i32>> { + self.stack_save.as_ref() + } + pub fn stack_restore_ref(&self) -> Option<&NativeFunc> { + self.stack_restore.as_ref() + } + pub fn set_threw_ref(&self) -> Option<&NativeFunc<(i32, i32), ()>> { + self.set_threw.as_ref() + } +} + /// Call the global constructors for C++ and set up the emscripten environment. /// /// Note that this function does not completely set up Emscripten to be called. /// before calling this function, please initialize `Ctx::data` with a pointer /// to [`EmscriptenData`]. -pub fn set_up_emscripten(instance: &mut Instance) -> Result<(), RuntimeError> { +pub fn set_up_emscripten( + ctx: &mut ContextMut<'_, EmEnv>, + instance: &mut Instance, +) -> Result<(), RuntimeError> { // ATINIT // (used by C++) - if let Ok(func) = instance.exports.get::("globalCtors") { - func.call(&[])?; + if let Ok(func) = instance.exports.get_function("globalCtors") { + func.call(&mut ctx.as_context_mut(), &[])?; } if let Ok(func) = instance .exports - .get::("___emscripten_environ_constructor") + .get_function("___emscripten_environ_constructor") { - func.call(&[])?; + func.call(&mut ctx.as_context_mut(), &[])?; } Ok(()) } @@ -315,36 +496,39 @@ pub fn set_up_emscripten(instance: &mut Instance) -> Result<(), RuntimeError> { /// If you don't want to set it up yourself, consider using [`run_emscripten_instance`]. pub fn emscripten_call_main( instance: &mut Instance, - env: &EmEnv, + mut ctx: ContextMut<'_, EmEnv>, path: &str, args: &[&str], ) -> Result<(), RuntimeError> { - let (function_name, main_func) = match instance.exports.get::("_main") { + let (function_name, main_func) = match instance.exports.get_function("_main") { Ok(func) => Ok(("_main", func)), Err(_e) => instance .exports - .get::("main") + .get_function("main") .map(|func| ("main", func)), } .map_err(|e| RuntimeError::new(e.to_string()))?; - let num_params = main_func.ty().params().len(); + let num_params = main_func.ty(&ctx).params().len(); let _result = match num_params { 2 => { let mut new_args = vec![path]; new_args.extend(args); - let (argc, argv) = store_module_arguments(env, new_args); + let (argc, argv) = store_module_arguments(ctx.as_context_mut(), new_args); let func: &Function = instance .exports .get(function_name) .map_err(|e| RuntimeError::new(e.to_string()))?; - func.call(&[Val::I32(argc as i32), Val::I32(argv as i32)])?; + func.call( + &mut ctx, + &[Value::I32(argc as i32), Value::I32(argv as i32)], + )?; } 0 => { let func: &Function = instance .exports .get(function_name) .map_err(|e| RuntimeError::new(e.to_string()))?; - func.call(&[])?; + func.call(&mut ctx, &[])?; } _ => { todo!("Update error type to be able to express this"); @@ -360,28 +544,263 @@ pub fn emscripten_call_main( /// Top level function to execute emscripten pub fn run_emscripten_instance( instance: &mut Instance, - env: &mut EmEnv, + mut ctx: ContextMut<'_, EmEnv>, globals: &mut EmscriptenGlobals, path: &str, args: Vec<&str>, entrypoint: Option, ) -> Result<(), RuntimeError> { + let env = &mut ctx.data_mut(); env.set_memory(globals.memory.clone()); - set_up_emscripten(instance)?; + // get emscripten export + let mut emfuncs = EmscriptenFunctions::new(); + if let Ok(func) = instance.exports.get_native_function(&ctx, "malloc") { + emfuncs.malloc = Some(func); + } else if let Ok(func) = instance.exports.get_native_function(&ctx, "_malloc") { + emfuncs.malloc = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "free") { + emfuncs.free = Some(func); + } else if let Ok(func) = instance.exports.get_native_function(&ctx, "_free") { + emfuncs.free = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "memalign") { + emfuncs.memalign = Some(func); + } else if let Ok(func) = instance.exports.get_native_function(&ctx, "_memalign") { + emfuncs.memalign = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "memset") { + emfuncs.memset = Some(func); + } else if let Ok(func) = instance.exports.get_native_function(&ctx, "_memset") { + emfuncs.memset = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "stackAlloc") { + emfuncs.stack_alloc = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_i") { + emfuncs.dyn_call_i = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_ii") { + emfuncs.dyn_call_ii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iii") { + emfuncs.dyn_call_iii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iiii") { + emfuncs.dyn_call_iiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iifi") { + emfuncs.dyn_call_iifi = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_v") { + emfuncs.dyn_call_v = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vi") { + emfuncs.dyn_call_vi = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vii") { + emfuncs.dyn_call_vii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viii") { + emfuncs.dyn_call_viii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viiii") { + emfuncs.dyn_call_viiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_dii") { + emfuncs.dyn_call_dii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_diiii") { + emfuncs.dyn_call_diiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iiiii") { + emfuncs.dyn_call_iiiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iiiiii") { + emfuncs.dyn_call_iiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_iiiiiii") + { + emfuncs.dyn_call_iiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_iiiiiiii") + { + emfuncs.dyn_call_iiiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_iiiiiiiii") + { + emfuncs.dyn_call_iiiiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_iiiiiiiiii") + { + emfuncs.dyn_call_iiiiiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_iiiiiiiiiii") + { + emfuncs.dyn_call_iiiiiiiiiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vd") { + emfuncs.dyn_call_vd = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viiiii") { + emfuncs.dyn_call_viiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiiiii") + { + emfuncs.dyn_call_viiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiiiiii") + { + emfuncs.dyn_call_viiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiiiiiii") + { + emfuncs.dyn_call_viiiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiiiiiiii") + { + emfuncs.dyn_call_viiiiiiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiiiiiiiii") + { + emfuncs.dyn_call_viiiiiiiiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iij") { + emfuncs.dyn_call_iij = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iji") { + emfuncs.dyn_call_iji = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iiji") { + emfuncs.dyn_call_iiji = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_iiijj") { + emfuncs.dyn_call_iiijj = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_j") { + emfuncs.dyn_call_j = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_ji") { + emfuncs.dyn_call_ji = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_jii") { + emfuncs.dyn_call_jii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_jij") { + emfuncs.dyn_call_jij = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_jjj") { + emfuncs.dyn_call_jjj = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viiij") { + emfuncs.dyn_call_viiij = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiijiiii") + { + emfuncs.dyn_call_viiijiiii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viiijiiiiii") + { + emfuncs.dyn_call_viiijiiiiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viij") { + emfuncs.dyn_call_viij = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viiji") { + emfuncs.dyn_call_viiji = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viijiii") + { + emfuncs.dyn_call_viijiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viijj") { + emfuncs.dyn_call_viijj = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vj") { + emfuncs.dyn_call_vj = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vjji") { + emfuncs.dyn_call_vjji = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vij") { + emfuncs.dyn_call_vij = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viji") { + emfuncs.dyn_call_viji = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vijiii") { + emfuncs.dyn_call_vijiii = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vijj") { + emfuncs.dyn_call_vijj = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viid") { + emfuncs.dyn_call_viid = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_vidd") { + emfuncs.dyn_call_vidd = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "dynCall_viidii") { + emfuncs.dyn_call_viidii = Some(func); + } + if let Ok(func) = instance + .exports + .get_native_function(&ctx, "dynCall_viidddddddd") + { + emfuncs.dyn_call_viidddddddd = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "stackSave") { + emfuncs.stack_save = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "stackRestore") { + emfuncs.stack_restore = Some(func); + } + if let Ok(func) = instance.exports.get_native_function(&ctx, "setThrew") { + emfuncs.set_threw = Some(func); + } + ctx.data_mut().set_functions(emfuncs); + + set_up_emscripten(&mut ctx.as_context_mut(), instance)?; // println!("running emscripten instance"); if let Some(ep) = entrypoint { debug!("Running entry point: {}", &ep); - let arg = unsafe { allocate_cstr_on_stack(env, args[0]).0 }; + let arg = unsafe { allocate_cstr_on_stack(&mut ctx.as_context_mut(), args[0]).0 }; //let (argc, argv) = store_module_arguments(instance.context_mut(), args); let func: &Function = instance .exports .get(&ep) .map_err(|e| RuntimeError::new(e.to_string()))?; - func.call(&[Val::I32(arg as i32)])?; + func.call(&mut ctx, &[Value::I32(arg as i32)])?; } else { - emscripten_call_main(instance, env, path, &args)?; + emscripten_call_main(instance, ctx, path, &args)?; } // TODO atexit for emscripten @@ -389,16 +808,16 @@ pub fn run_emscripten_instance( Ok(()) } -fn store_module_arguments(ctx: &EmEnv, args: Vec<&str>) -> (u32, u32) { +fn store_module_arguments(mut ctx: ContextMut<'_, EmEnv>, args: Vec<&str>) -> (u32, u32) { let argc = args.len() + 1; let mut args_slice = vec![0; argc]; for (slot, arg) in args_slice[0..argc].iter_mut().zip(args.iter()) { - *slot = unsafe { allocate_cstr_on_stack(ctx, &arg).0 }; + *slot = unsafe { allocate_cstr_on_stack(&mut ctx.as_context_mut(), &arg).0 }; } let (argv_offset, argv_slice): (_, &mut [u32]) = - unsafe { allocate_on_stack(ctx, ((argc) * 4) as u32) }; + unsafe { allocate_on_stack(&mut ctx, ((argc) * 4) as u32) }; assert!(!argv_slice.is_empty()); for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) { *slot = *arg @@ -409,13 +828,15 @@ fn store_module_arguments(ctx: &EmEnv, args: Vec<&str>) -> (u32, u32) { } pub fn emscripten_set_up_memory( + mut ctx: ContextMut<'_, EmEnv>, memory: &Memory, globals: &EmscriptenGlobalsData, ) -> Result<(), String> { - let dynamictop_ptr = WasmPtr::::new(globals.dynamictop_ptr).deref(memory); + ctx.data_mut().set_memory(memory.clone()); + let dynamictop_ptr = WasmPtr::::new(globals.dynamictop_ptr).deref(&ctx, memory); let dynamic_base = globals.dynamic_base; - if dynamictop_ptr.offset() >= memory.data_size() { + if dynamictop_ptr.offset() >= memory.data_size(&ctx) { return Err("dynamictop_ptr beyond memory len".to_string()); } dynamictop_ptr.write(dynamic_base as i32).unwrap(); @@ -449,7 +870,7 @@ pub struct EmscriptenGlobals { impl EmscriptenGlobals { pub fn new( - store: &Store, + mut ctx: ContextMut<'_, EmEnv>, module: &Module, /*, static_bump: u32 */ ) -> Result { let mut use_old_abort_on_cannot_grow_memory = false; @@ -467,14 +888,14 @@ impl EmscriptenGlobals { // Memory initialization let memory_type = MemoryType::new(memory_min, memory_max, shared); - let memory = Memory::new(store, memory_type).unwrap(); + let memory = Memory::new(&mut ctx, memory_type).unwrap(); let table_type = TableType { ty: ValType::FuncRef, minimum: table_min, maximum: table_max, }; - let table = Table::new(store, table_type, Val::FuncRef(None)).unwrap(); + let table = Table::new(&mut ctx, table_type, Value::FuncRef(None)).unwrap(); let data = { let static_bump = STATIC_BUMP; @@ -512,7 +933,7 @@ impl EmscriptenGlobals { } }; - emscripten_set_up_memory(&memory, &data)?; + emscripten_set_up_memory(ctx, &memory, &data)?; let mut null_function_names = vec![]; for import in module.imports().functions() { @@ -536,22 +957,13 @@ impl EmscriptenGlobals { } pub fn generate_emscripten_env( - store: &Store, + ctx: &mut ContextMut<'_, EmEnv>, globals: &mut EmscriptenGlobals, - env: &EmEnv, ) -> Imports { let abort_on_cannot_grow_memory_export = if globals.data.use_old_abort_on_cannot_grow_memory { - Function::new_native_with_env( - store, - env.clone(), - crate::memory::abort_on_cannot_grow_memory_old, - ) + Function::new_native(ctx, crate::memory::abort_on_cannot_grow_memory_old) } else { - Function::new_native_with_env( - store, - env.clone(), - crate::memory::abort_on_cannot_grow_memory, - ) + Function::new_native(ctx, crate::memory::abort_on_cannot_grow_memory) }; let mut env_ns: Exports = namespace! { @@ -559,440 +971,440 @@ pub fn generate_emscripten_env( "table" => globals.table.clone(), // Globals - "STACKTOP" => Global::new(store, Val::I32(globals.data.stacktop as i32)), - "STACK_MAX" => Global::new(store, Val::I32(globals.data.stack_max as i32)), - "DYNAMICTOP_PTR" => Global::new(store, Val::I32(globals.data.dynamictop_ptr as i32)), - "fb" => Global::new(store, Val::I32(globals.data.table_base as i32)), - "tableBase" => Global::new(store, Val::I32(globals.data.table_base as i32)), - "__table_base" => Global::new(store, Val::I32(globals.data.table_base as i32)), - "ABORT" => Global::new(store, Val::I32(globals.data.abort as i32)), - "gb" => Global::new(store, Val::I32(globals.data.memory_base as i32)), - "memoryBase" => Global::new(store, Val::I32(globals.data.memory_base as i32)), - "__memory_base" => Global::new(store, Val::I32(globals.data.memory_base as i32)), - "tempDoublePtr" => Global::new(store, Val::I32(globals.data.temp_double_ptr as i32)), + "STACKTOP" => Global::new(ctx, Value::I32(globals.data.stacktop as i32)), + "STACK_MAX" => Global::new(ctx, Value::I32(globals.data.stack_max as i32)), + "DYNAMICTOP_PTR" => Global::new(ctx, Value::I32(globals.data.dynamictop_ptr as i32)), + "fb" => Global::new(ctx, Value::I32(globals.data.table_base as i32)), + "tableBase" => Global::new(ctx, Value::I32(globals.data.table_base as i32)), + "__table_base" => Global::new(ctx, Value::I32(globals.data.table_base as i32)), + "ABORT" => Global::new(ctx, Value::I32(globals.data.abort as i32)), + "gb" => Global::new(ctx, Value::I32(globals.data.memory_base as i32)), + "memoryBase" => Global::new(ctx, Value::I32(globals.data.memory_base as i32)), + "__memory_base" => Global::new(ctx, Value::I32(globals.data.memory_base as i32)), + "tempDoublePtr" => Global::new(ctx, Value::I32(globals.data.temp_double_ptr as i32)), // inet - "_inet_addr" => Function::new_native_with_env(store, env.clone(), crate::inet::addr), + "_inet_addr" => Function::new_native(ctx, crate::inet::addr), // IO - "printf" => Function::new_native_with_env(store, env.clone(), crate::io::printf), - "putchar" => Function::new_native_with_env(store, env.clone(), crate::io::putchar), - "___lock" => Function::new_native_with_env(store, env.clone(), crate::lock::___lock), - "___unlock" => Function::new_native_with_env(store, env.clone(), crate::lock::___unlock), - "___wait" => Function::new_native_with_env(store, env.clone(), crate::lock::___wait), - "_flock" => Function::new_native_with_env(store, env.clone(), crate::lock::_flock), - "_chroot" => Function::new_native_with_env(store, env.clone(), crate::io::chroot), - "_getprotobyname" => Function::new_native_with_env(store, env.clone(), crate::io::getprotobyname), - "_getprotobynumber" => Function::new_native_with_env(store, env.clone(), crate::io::getprotobynumber), - "_getpwuid" => Function::new_native_with_env(store, env.clone(), crate::io::getpwuid), - "_sigdelset" => Function::new_native_with_env(store, env.clone(), crate::io::sigdelset), - "_sigfillset" => Function::new_native_with_env(store, env.clone(), crate::io::sigfillset), - "_tzset" => Function::new_native_with_env(store, env.clone(), crate::io::tzset), - "_strptime" => Function::new_native_with_env(store, env.clone(), crate::io::strptime), + "printf" => Function::new_native(ctx, crate::io::printf), + "putchar" => Function::new_native(ctx, crate::io::putchar), + "___lock" => Function::new_native(ctx, crate::lock::___lock), + "___unlock" => Function::new_native(ctx, crate::lock::___unlock), + "___wait" => Function::new_native(ctx, crate::lock::___wait), + "_flock" => Function::new_native(ctx, crate::lock::_flock), + "_chroot" => Function::new_native(ctx, crate::io::chroot), + "_getprotobyname" => Function::new_native(ctx, crate::io::getprotobyname), + "_getprotobynumber" => Function::new_native(ctx, crate::io::getprotobynumber), + "_getpwuid" => Function::new_native(ctx, crate::io::getpwuid), + "_sigdelset" => Function::new_native(ctx, crate::io::sigdelset), + "_sigfillset" => Function::new_native(ctx, crate::io::sigfillset), + "_tzset" => Function::new_native(ctx, crate::io::tzset), + "_strptime" => Function::new_native(ctx, crate::io::strptime), // exec - "_execvp" => Function::new_native_with_env(store, env.clone(), crate::exec::execvp), - "_execl" => Function::new_native_with_env(store, env.clone(), crate::exec::execl), - "_execle" => Function::new_native_with_env(store, env.clone(), crate::exec::execle), + "_execvp" => Function::new_native(ctx, crate::exec::execvp), + "_execl" => Function::new_native(ctx, crate::exec::execl), + "_execle" => Function::new_native(ctx, crate::exec::execle), // exit - "__exit" => Function::new_native_with_env(store, env.clone(), crate::exit::exit), + "__exit" => Function::new_native(ctx, crate::exit::exit), // Env - "___assert_fail" => Function::new_native_with_env(store, env.clone(), crate::env::___assert_fail), - "_getenv" => Function::new_native_with_env(store, env.clone(), crate::env::_getenv), - "_setenv" => Function::new_native_with_env(store, env.clone(), crate::env::_setenv), - "_putenv" => Function::new_native_with_env(store, env.clone(), crate::env::_putenv), - "_unsetenv" => Function::new_native_with_env(store, env.clone(), crate::env::_unsetenv), - "_getpwnam" => Function::new_native_with_env(store, env.clone(), crate::env::_getpwnam), - "_getgrnam" => Function::new_native_with_env(store, env.clone(), crate::env::_getgrnam), - "___buildEnvironment" => Function::new_native_with_env(store, env.clone(), crate::env::___build_environment), - "___setErrNo" => Function::new_native_with_env(store, env.clone(), crate::errno::___seterrno), - "_getpagesize" => Function::new_native_with_env(store, env.clone(), crate::env::_getpagesize), - "_sysconf" => Function::new_native_with_env(store, env.clone(), crate::env::_sysconf), - "_getaddrinfo" => Function::new_native_with_env(store, env.clone(), crate::env::_getaddrinfo), - "_times" => Function::new_native_with_env(store, env.clone(), crate::env::_times), - "_pathconf" => Function::new_native_with_env(store, env.clone(), crate::env::_pathconf), - "_fpathconf" => Function::new_native_with_env(store, env.clone(), crate::env::_fpathconf), + "___assert_fail" => Function::new_native(ctx, crate::env::___assert_fail), + "_getenv" => Function::new_native(ctx, crate::env::_getenv), + "_setenv" => Function::new_native(ctx, crate::env::_setenv), + "_putenv" => Function::new_native(ctx, crate::env::_putenv), + "_unsetenv" => Function::new_native(ctx, crate::env::_unsetenv), + "_getpwnam" => Function::new_native(ctx, crate::env::_getpwnam), + "_getgrnam" => Function::new_native(ctx, crate::env::_getgrnam), + "___buildEnvironment" => Function::new_native(ctx, crate::env::___build_environment), + "___setErrNo" => Function::new_native(ctx, crate::errno::___seterrno), + "_getpagesize" => Function::new_native(ctx, crate::env::_getpagesize), + "_sysconf" => Function::new_native(ctx, crate::env::_sysconf), + "_getaddrinfo" => Function::new_native(ctx, crate::env::_getaddrinfo), + "_times" => Function::new_native(ctx, crate::env::_times), + "_pathconf" => Function::new_native(ctx, crate::env::_pathconf), + "_fpathconf" => Function::new_native(ctx, crate::env::_fpathconf), // Syscalls - "___syscall1" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall1), - "___syscall3" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall3), - "___syscall4" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall4), - "___syscall5" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall5), - "___syscall6" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall6), - "___syscall9" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall9), - "___syscall10" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall10), - "___syscall12" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall12), - "___syscall14" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall14), - "___syscall15" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall15), - "___syscall20" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall20), - "___syscall21" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall21), - "___syscall25" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall25), - "___syscall29" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall29), - "___syscall32" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall32), - "___syscall33" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall33), - "___syscall34" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall34), - "___syscall36" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall36), - "___syscall39" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall39), - "___syscall38" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall38), - "___syscall40" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall40), - "___syscall41" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall41), - "___syscall42" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall42), - "___syscall51" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall51), - "___syscall52" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall52), - "___syscall53" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall53), - "___syscall54" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall54), - "___syscall57" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall57), - "___syscall60" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall60), - "___syscall63" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall63), - "___syscall64" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall64), - "___syscall66" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall66), - "___syscall75" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall75), - "___syscall77" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall77), - "___syscall83" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall83), - "___syscall85" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall85), - "___syscall91" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall91), - "___syscall94" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall94), - "___syscall96" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall96), - "___syscall97" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall97), - "___syscall102" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall102), - "___syscall110" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall110), - "___syscall114" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall114), - "___syscall118" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall118), - "___syscall121" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall121), - "___syscall122" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall122), - "___syscall125" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall125), - "___syscall132" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall132), - "___syscall133" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall133), - "___syscall140" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall140), - "___syscall142" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall142), - "___syscall144" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall144), - "___syscall145" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall145), - "___syscall146" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall146), - "___syscall147" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall147), - "___syscall148" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall148), - "___syscall150" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall150), - "___syscall151" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall151), - "___syscall152" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall152), - "___syscall153" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall153), - "___syscall163" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall163), - "___syscall168" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall168), - "___syscall180" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall180), - "___syscall181" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall181), - "___syscall183" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall183), - "___syscall191" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall191), - "___syscall192" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall192), - "___syscall193" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall193), - "___syscall194" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall194), - "___syscall195" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall195), - "___syscall196" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall196), - "___syscall197" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall197), - "___syscall198" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall198), - "___syscall199" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall199), - "___syscall200" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall200), - "___syscall201" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall201), - "___syscall202" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall202), - "___syscall205" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall205), - "___syscall207" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall207), - "___syscall209" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall209), - "___syscall211" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall211), - "___syscall212" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall212), - "___syscall218" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall218), - "___syscall219" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall219), - "___syscall220" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall220), - "___syscall221" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall221), - "___syscall268" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall268), - "___syscall269" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall269), - "___syscall272" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall272), - "___syscall295" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall295), - "___syscall296" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall296), - "___syscall297" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall297), - "___syscall298" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall298), - "___syscall300" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall300), - "___syscall301" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall301), - "___syscall302" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall302), - "___syscall303" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall303), - "___syscall304" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall304), - "___syscall305" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall305), - "___syscall306" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall306), - "___syscall307" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall307), - "___syscall308" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall308), - "___syscall320" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall320), - "___syscall324" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall324), - "___syscall330" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall330), - "___syscall331" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall331), - "___syscall333" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall333), - "___syscall334" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall334), - "___syscall337" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall337), - "___syscall340" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall340), - "___syscall345" => Function::new_native_with_env(store, env.clone(), crate::syscalls::___syscall345), + "___syscall1" => Function::new_native(ctx, crate::syscalls::___syscall1), + "___syscall3" => Function::new_native(ctx, crate::syscalls::___syscall3), + "___syscall4" => Function::new_native(ctx, crate::syscalls::___syscall4), + "___syscall5" => Function::new_native(ctx, crate::syscalls::___syscall5), + "___syscall6" => Function::new_native(ctx, crate::syscalls::___syscall6), + "___syscall9" => Function::new_native(ctx, crate::syscalls::___syscall9), + "___syscall10" => Function::new_native(ctx, crate::syscalls::___syscall10), + "___syscall12" => Function::new_native(ctx, crate::syscalls::___syscall12), + "___syscall14" => Function::new_native(ctx, crate::syscalls::___syscall14), + "___syscall15" => Function::new_native(ctx, crate::syscalls::___syscall15), + "___syscall20" => Function::new_native(ctx, crate::syscalls::___syscall20), + "___syscall21" => Function::new_native(ctx, crate::syscalls::___syscall21), + "___syscall25" => Function::new_native(ctx, crate::syscalls::___syscall25), + "___syscall29" => Function::new_native(ctx, crate::syscalls::___syscall29), + "___syscall32" => Function::new_native(ctx, crate::syscalls::___syscall32), + "___syscall33" => Function::new_native(ctx, crate::syscalls::___syscall33), + "___syscall34" => Function::new_native(ctx, crate::syscalls::___syscall34), + "___syscall36" => Function::new_native(ctx, crate::syscalls::___syscall36), + "___syscall39" => Function::new_native(ctx, crate::syscalls::___syscall39), + "___syscall38" => Function::new_native(ctx, crate::syscalls::___syscall38), + "___syscall40" => Function::new_native(ctx, crate::syscalls::___syscall40), + "___syscall41" => Function::new_native(ctx, crate::syscalls::___syscall41), + "___syscall42" => Function::new_native(ctx, crate::syscalls::___syscall42), + "___syscall51" => Function::new_native(ctx, crate::syscalls::___syscall51), + "___syscall52" => Function::new_native(ctx, crate::syscalls::___syscall52), + "___syscall53" => Function::new_native(ctx, crate::syscalls::___syscall53), + "___syscall54" => Function::new_native(ctx, crate::syscalls::___syscall54), + "___syscall57" => Function::new_native(ctx, crate::syscalls::___syscall57), + "___syscall60" => Function::new_native(ctx, crate::syscalls::___syscall60), + "___syscall63" => Function::new_native(ctx, crate::syscalls::___syscall63), + "___syscall64" => Function::new_native(ctx, crate::syscalls::___syscall64), + "___syscall66" => Function::new_native(ctx, crate::syscalls::___syscall66), + "___syscall75" => Function::new_native(ctx, crate::syscalls::___syscall75), + "___syscall77" => Function::new_native(ctx, crate::syscalls::___syscall77), + "___syscall83" => Function::new_native(ctx, crate::syscalls::___syscall83), + "___syscall85" => Function::new_native(ctx, crate::syscalls::___syscall85), + "___syscall91" => Function::new_native(ctx, crate::syscalls::___syscall91), + "___syscall94" => Function::new_native(ctx, crate::syscalls::___syscall94), + "___syscall96" => Function::new_native(ctx, crate::syscalls::___syscall96), + "___syscall97" => Function::new_native(ctx, crate::syscalls::___syscall97), + "___syscall102" => Function::new_native(ctx, crate::syscalls::___syscall102), + "___syscall110" => Function::new_native(ctx, crate::syscalls::___syscall110), + "___syscall114" => Function::new_native(ctx, crate::syscalls::___syscall114), + "___syscall118" => Function::new_native(ctx, crate::syscalls::___syscall118), + "___syscall121" => Function::new_native(ctx, crate::syscalls::___syscall121), + "___syscall122" => Function::new_native(ctx, crate::syscalls::___syscall122), + "___syscall125" => Function::new_native(ctx, crate::syscalls::___syscall125), + "___syscall132" => Function::new_native(ctx, crate::syscalls::___syscall132), + "___syscall133" => Function::new_native(ctx, crate::syscalls::___syscall133), + "___syscall140" => Function::new_native(ctx, crate::syscalls::___syscall140), + "___syscall142" => Function::new_native(ctx, crate::syscalls::___syscall142), + "___syscall144" => Function::new_native(ctx, crate::syscalls::___syscall144), + "___syscall145" => Function::new_native(ctx, crate::syscalls::___syscall145), + "___syscall146" => Function::new_native(ctx, crate::syscalls::___syscall146), + "___syscall147" => Function::new_native(ctx, crate::syscalls::___syscall147), + "___syscall148" => Function::new_native(ctx, crate::syscalls::___syscall148), + "___syscall150" => Function::new_native(ctx, crate::syscalls::___syscall150), + "___syscall151" => Function::new_native(ctx, crate::syscalls::___syscall151), + "___syscall152" => Function::new_native(ctx, crate::syscalls::___syscall152), + "___syscall153" => Function::new_native(ctx, crate::syscalls::___syscall153), + "___syscall163" => Function::new_native(ctx, crate::syscalls::___syscall163), + "___syscall168" => Function::new_native(ctx, crate::syscalls::___syscall168), + "___syscall180" => Function::new_native(ctx, crate::syscalls::___syscall180), + "___syscall181" => Function::new_native(ctx, crate::syscalls::___syscall181), + "___syscall183" => Function::new_native(ctx, crate::syscalls::___syscall183), + "___syscall191" => Function::new_native(ctx, crate::syscalls::___syscall191), + "___syscall192" => Function::new_native(ctx, crate::syscalls::___syscall192), + "___syscall193" => Function::new_native(ctx, crate::syscalls::___syscall193), + "___syscall194" => Function::new_native(ctx, crate::syscalls::___syscall194), + "___syscall195" => Function::new_native(ctx, crate::syscalls::___syscall195), + "___syscall196" => Function::new_native(ctx, crate::syscalls::___syscall196), + "___syscall197" => Function::new_native(ctx, crate::syscalls::___syscall197), + "___syscall198" => Function::new_native(ctx, crate::syscalls::___syscall198), + "___syscall199" => Function::new_native(ctx, crate::syscalls::___syscall199), + "___syscall200" => Function::new_native(ctx, crate::syscalls::___syscall200), + "___syscall201" => Function::new_native(ctx, crate::syscalls::___syscall201), + "___syscall202" => Function::new_native(ctx, crate::syscalls::___syscall202), + "___syscall205" => Function::new_native(ctx, crate::syscalls::___syscall205), + "___syscall207" => Function::new_native(ctx, crate::syscalls::___syscall207), + "___syscall209" => Function::new_native(ctx, crate::syscalls::___syscall209), + "___syscall211" => Function::new_native(ctx, crate::syscalls::___syscall211), + "___syscall212" => Function::new_native(ctx, crate::syscalls::___syscall212), + "___syscall218" => Function::new_native(ctx, crate::syscalls::___syscall218), + "___syscall219" => Function::new_native(ctx, crate::syscalls::___syscall219), + "___syscall220" => Function::new_native(ctx, crate::syscalls::___syscall220), + "___syscall221" => Function::new_native(ctx, crate::syscalls::___syscall221), + "___syscall268" => Function::new_native(ctx, crate::syscalls::___syscall268), + "___syscall269" => Function::new_native(ctx, crate::syscalls::___syscall269), + "___syscall272" => Function::new_native(ctx, crate::syscalls::___syscall272), + "___syscall295" => Function::new_native(ctx, crate::syscalls::___syscall295), + "___syscall296" => Function::new_native(ctx, crate::syscalls::___syscall296), + "___syscall297" => Function::new_native(ctx, crate::syscalls::___syscall297), + "___syscall298" => Function::new_native(ctx, crate::syscalls::___syscall298), + "___syscall300" => Function::new_native(ctx, crate::syscalls::___syscall300), + "___syscall301" => Function::new_native(ctx, crate::syscalls::___syscall301), + "___syscall302" => Function::new_native(ctx, crate::syscalls::___syscall302), + "___syscall303" => Function::new_native(ctx, crate::syscalls::___syscall303), + "___syscall304" => Function::new_native(ctx, crate::syscalls::___syscall304), + "___syscall305" => Function::new_native(ctx, crate::syscalls::___syscall305), + "___syscall306" => Function::new_native(ctx, crate::syscalls::___syscall306), + "___syscall307" => Function::new_native(ctx, crate::syscalls::___syscall307), + "___syscall308" => Function::new_native(ctx, crate::syscalls::___syscall308), + "___syscall320" => Function::new_native(ctx, crate::syscalls::___syscall320), + "___syscall324" => Function::new_native(ctx, crate::syscalls::___syscall324), + "___syscall330" => Function::new_native(ctx, crate::syscalls::___syscall330), + "___syscall331" => Function::new_native(ctx, crate::syscalls::___syscall331), + "___syscall333" => Function::new_native(ctx, crate::syscalls::___syscall333), + "___syscall334" => Function::new_native(ctx, crate::syscalls::___syscall334), + "___syscall337" => Function::new_native(ctx, crate::syscalls::___syscall337), + "___syscall340" => Function::new_native(ctx, crate::syscalls::___syscall340), + "___syscall345" => Function::new_native(ctx, crate::syscalls::___syscall345), // Process - "abort" => Function::new_native_with_env(store, env.clone(), crate::process::em_abort), - "_abort" => Function::new_native_with_env(store, env.clone(), crate::process::_abort), - "_prctl" => Function::new_native_with_env(store, env.clone(), crate::process::_prctl), - "abortStackOverflow" => Function::new_native_with_env(store, env.clone(), crate::process::abort_stack_overflow), - "_llvm_trap" => Function::new_native_with_env(store, env.clone(), crate::process::_llvm_trap), - "_fork" => Function::new_native_with_env(store, env.clone(), crate::process::_fork), - "_exit" => Function::new_native_with_env(store, env.clone(), crate::process::_exit), - "_system" => Function::new_native_with_env(store, env.clone(), crate::process::_system), - "_popen" => Function::new_native_with_env(store, env.clone(), crate::process::_popen), - "_endgrent" => Function::new_native_with_env(store, env.clone(), crate::process::_endgrent), - "_execve" => Function::new_native_with_env(store, env.clone(), crate::process::_execve), - "_kill" => Function::new_native_with_env(store, env.clone(), crate::process::_kill), - "_llvm_stackrestore" => Function::new_native_with_env(store, env.clone(), crate::process::_llvm_stackrestore), - "_llvm_stacksave" => Function::new_native_with_env(store, env.clone(), crate::process::_llvm_stacksave), - "_llvm_eh_typeid_for" => Function::new_native_with_env(store, env.clone(), crate::process::_llvm_eh_typeid_for), - "_raise" => Function::new_native_with_env(store, env.clone(), crate::process::_raise), - "_sem_init" => Function::new_native_with_env(store, env.clone(), crate::process::_sem_init), - "_sem_destroy" => Function::new_native_with_env(store, env.clone(), crate::process::_sem_destroy), - "_sem_post" => Function::new_native_with_env(store, env.clone(), crate::process::_sem_post), - "_sem_wait" => Function::new_native_with_env(store, env.clone(), crate::process::_sem_wait), - "_getgrent" => Function::new_native_with_env(store, env.clone(), crate::process::_getgrent), - "_sched_yield" => Function::new_native_with_env(store, env.clone(), crate::process::_sched_yield), - "_setgrent" => Function::new_native_with_env(store, env.clone(), crate::process::_setgrent), - "_setgroups" => Function::new_native_with_env(store, env.clone(), crate::process::_setgroups), - "_setitimer" => Function::new_native_with_env(store, env.clone(), crate::process::_setitimer), - "_usleep" => Function::new_native_with_env(store, env.clone(), crate::process::_usleep), - "_nanosleep" => Function::new_native_with_env(store, env.clone(), crate::process::_nanosleep), - "_utime" => Function::new_native_with_env(store, env.clone(), crate::process::_utime), - "_utimes" => Function::new_native_with_env(store, env.clone(), crate::process::_utimes), - "_wait" => Function::new_native_with_env(store, env.clone(), crate::process::_wait), - "_wait3" => Function::new_native_with_env(store, env.clone(), crate::process::_wait3), - "_wait4" => Function::new_native_with_env(store, env.clone(), crate::process::_wait4), - "_waitid" => Function::new_native_with_env(store, env.clone(), crate::process::_waitid), - "_waitpid" => Function::new_native_with_env(store, env.clone(), crate::process::_waitpid), + "abort" => Function::new_native(ctx, crate::process::em_abort), + "_abort" => Function::new_native(ctx, crate::process::_abort), + "_prctl" => Function::new_native(ctx, crate::process::_prctl), + "abortStackOverflow" => Function::new_native(ctx, crate::process::abort_stack_overflow), + "_llvm_trap" => Function::new_native(ctx, crate::process::_llvm_trap), + "_fork" => Function::new_native(ctx, crate::process::_fork), + "_exit" => Function::new_native(ctx, crate::process::_exit), + "_system" => Function::new_native(ctx, crate::process::_system), + "_popen" => Function::new_native(ctx, crate::process::_popen), + "_endgrent" => Function::new_native(ctx, crate::process::_endgrent), + "_execve" => Function::new_native(ctx, crate::process::_execve), + "_kill" => Function::new_native(ctx, crate::process::_kill), + "_llvm_stackrestore" => Function::new_native(ctx, crate::process::_llvm_stackrestore), + "_llvm_stacksave" => Function::new_native(ctx, crate::process::_llvm_stacksave), + "_llvm_eh_typeid_for" => Function::new_native(ctx, crate::process::_llvm_eh_typeid_for), + "_raise" => Function::new_native(ctx, crate::process::_raise), + "_sem_init" => Function::new_native(ctx, crate::process::_sem_init), + "_sem_destroy" => Function::new_native(ctx, crate::process::_sem_destroy), + "_sem_post" => Function::new_native(ctx, crate::process::_sem_post), + "_sem_wait" => Function::new_native(ctx, crate::process::_sem_wait), + "_getgrent" => Function::new_native(ctx, crate::process::_getgrent), + "_sched_yield" => Function::new_native(ctx, crate::process::_sched_yield), + "_setgrent" => Function::new_native(ctx, crate::process::_setgrent), + "_setgroups" => Function::new_native(ctx, crate::process::_setgroups), + "_setitimer" => Function::new_native(ctx, crate::process::_setitimer), + "_usleep" => Function::new_native(ctx, crate::process::_usleep), + "_nanosleep" => Function::new_native(ctx, crate::process::_nanosleep), + "_utime" => Function::new_native(ctx, crate::process::_utime), + "_utimes" => Function::new_native(ctx, crate::process::_utimes), + "_wait" => Function::new_native(ctx, crate::process::_wait), + "_wait3" => Function::new_native(ctx, crate::process::_wait3), + "_wait4" => Function::new_native(ctx, crate::process::_wait4), + "_waitid" => Function::new_native(ctx, crate::process::_waitid), + "_waitpid" => Function::new_native(ctx, crate::process::_waitpid), // Emscripten - "_emscripten_asm_const_i" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::asm_const_i), - "_emscripten_exit_with_live_runtime" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::exit_with_live_runtime), + "_emscripten_asm_const_i" => Function::new_native(ctx, crate::emscripten_target::asm_const_i), + "_emscripten_exit_with_live_runtime" => Function::new_native(ctx, crate::emscripten_target::exit_with_live_runtime), // Signal - "_sigemptyset" => Function::new_native_with_env(store, env.clone(), crate::signal::_sigemptyset), - "_sigaddset" => Function::new_native_with_env(store, env.clone(), crate::signal::_sigaddset), - "_sigprocmask" => Function::new_native_with_env(store, env.clone(), crate::signal::_sigprocmask), - "_sigaction" => Function::new_native_with_env(store, env.clone(), crate::signal::_sigaction), - "_signal" => Function::new_native_with_env(store, env.clone(), crate::signal::_signal), - "_sigsuspend" => Function::new_native_with_env(store, env.clone(), crate::signal::_sigsuspend), + "_sigemptyset" => Function::new_native(ctx, crate::signal::_sigemptyset), + "_sigaddset" => Function::new_native(ctx, crate::signal::_sigaddset), + "_sigprocmask" => Function::new_native(ctx, crate::signal::_sigprocmask), + "_sigaction" => Function::new_native(ctx, crate::signal::_sigaction), + "_signal" => Function::new_native(ctx, crate::signal::_signal), + "_sigsuspend" => Function::new_native(ctx, crate::signal::_sigsuspend), // Memory "abortOnCannotGrowMemory" => abort_on_cannot_grow_memory_export, - "_emscripten_memcpy_big" => Function::new_native_with_env(store, env.clone(), crate::memory::_emscripten_memcpy_big), - "_emscripten_get_heap_size" => Function::new_native_with_env(store, env.clone(), crate::memory::_emscripten_get_heap_size), - "_emscripten_resize_heap" => Function::new_native_with_env(store, env.clone(), crate::memory::_emscripten_resize_heap), - "enlargeMemory" => Function::new_native_with_env(store, env.clone(), crate::memory::enlarge_memory), - "segfault" => Function::new_native_with_env(store, env.clone(), crate::memory::segfault), - "alignfault" => Function::new_native_with_env(store, env.clone(), crate::memory::alignfault), - "ftfault" => Function::new_native_with_env(store, env.clone(), crate::memory::ftfault), - "getTotalMemory" => Function::new_native_with_env(store, env.clone(), crate::memory::get_total_memory), - "_sbrk" => Function::new_native_with_env(store, env.clone(), crate::memory::sbrk), - "___map_file" => Function::new_native_with_env(store, env.clone(), crate::memory::___map_file), + "_emscripten_memcpy_big" => Function::new_native(ctx, crate::memory::_emscripten_memcpy_big), + "_emscripten_get_heap_size" => Function::new_native(ctx, crate::memory::_emscripten_get_heap_size), + "_emscripten_resize_heap" => Function::new_native(ctx, crate::memory::_emscripten_resize_heap), + "enlargeMemory" => Function::new_native(ctx, crate::memory::enlarge_memory), + "segfault" => Function::new_native(ctx, crate::memory::segfault), + "alignfault" => Function::new_native(ctx, crate::memory::alignfault), + "ftfault" => Function::new_native(ctx, crate::memory::ftfault), + "getTotalMemory" => Function::new_native(ctx, crate::memory::get_total_memory), + "_sbrk" => Function::new_native(ctx, crate::memory::sbrk), + "___map_file" => Function::new_native(ctx, crate::memory::___map_file), // Exception - "___cxa_allocate_exception" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_allocate_exception), - "___cxa_current_primary_exception" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_current_primary_exception), - "___cxa_decrement_exception_refcount" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_decrement_exception_refcount), - "___cxa_increment_exception_refcount" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_increment_exception_refcount), - "___cxa_rethrow_primary_exception" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_rethrow_primary_exception), - "___cxa_throw" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_throw), - "___cxa_begin_catch" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_begin_catch), - "___cxa_end_catch" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_end_catch), - "___cxa_uncaught_exception" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_uncaught_exception), - "___cxa_pure_virtual" => Function::new_native_with_env(store, env.clone(), crate::exception::___cxa_pure_virtual), + "___cxa_allocate_exception" => Function::new_native(ctx, crate::exception::___cxa_allocate_exception), + "___cxa_current_primary_exception" => Function::new_native(ctx, crate::exception::___cxa_current_primary_exception), + "___cxa_decrement_exception_refcount" => Function::new_native(ctx, crate::exception::___cxa_decrement_exception_refcount), + "___cxa_increment_exception_refcount" => Function::new_native(ctx, crate::exception::___cxa_increment_exception_refcount), + "___cxa_rethrow_primary_exception" => Function::new_native(ctx, crate::exception::___cxa_rethrow_primary_exception), + "___cxa_throw" => Function::new_native(ctx, crate::exception::___cxa_throw), + "___cxa_begin_catch" => Function::new_native(ctx, crate::exception::___cxa_begin_catch), + "___cxa_end_catch" => Function::new_native(ctx, crate::exception::___cxa_end_catch), + "___cxa_uncaught_exception" => Function::new_native(ctx, crate::exception::___cxa_uncaught_exception), + "___cxa_pure_virtual" => Function::new_native(ctx, crate::exception::___cxa_pure_virtual), // Time - "_gettimeofday" => Function::new_native_with_env(store, env.clone(), crate::time::_gettimeofday), - "_clock_getres" => Function::new_native_with_env(store, env.clone(), crate::time::_clock_getres), - "_clock_gettime" => Function::new_native_with_env(store, env.clone(), crate::time::_clock_gettime), - "_clock_settime" => Function::new_native_with_env(store, env.clone(), crate::time::_clock_settime), - "___clock_gettime" => Function::new_native_with_env(store, env.clone(), crate::time::_clock_gettime), - "_clock" => Function::new_native_with_env(store, env.clone(), crate::time::_clock), - "_difftime" => Function::new_native_with_env(store, env.clone(), crate::time::_difftime), - "_asctime" => Function::new_native_with_env(store, env.clone(), crate::time::_asctime), - "_asctime_r" => Function::new_native_with_env(store, env.clone(), crate::time::_asctime_r), - "_localtime" => Function::new_native_with_env(store, env.clone(), crate::time::_localtime), - "_time" => Function::new_native_with_env(store, env.clone(), crate::time::_time), - "_timegm" => Function::new_native_with_env(store, env.clone(), crate::time::_timegm), - "_strftime" => Function::new_native_with_env(store, env.clone(), crate::time::_strftime), - "_strftime_l" => Function::new_native_with_env(store, env.clone(), crate::time::_strftime_l), - "_localtime_r" => Function::new_native_with_env(store, env.clone(), crate::time::_localtime_r), - "_gmtime_r" => Function::new_native_with_env(store, env.clone(), crate::time::_gmtime_r), - "_ctime" => Function::new_native_with_env(store, env.clone(), crate::time::_ctime), - "_ctime_r" => Function::new_native_with_env(store, env.clone(), crate::time::_ctime_r), - "_mktime" => Function::new_native_with_env(store, env.clone(), crate::time::_mktime), - "_gmtime" => Function::new_native_with_env(store, env.clone(), crate::time::_gmtime), + "_gettimeofday" => Function::new_native(ctx, crate::time::_gettimeofday), + "_clock_getres" => Function::new_native(ctx, crate::time::_clock_getres), + "_clock_gettime" => Function::new_native(ctx, crate::time::_clock_gettime), + "_clock_settime" => Function::new_native(ctx, crate::time::_clock_settime), + "___clock_gettime" => Function::new_native(ctx, crate::time::_clock_gettime), + "_clock" => Function::new_native(ctx, crate::time::_clock), + "_difftime" => Function::new_native(ctx, crate::time::_difftime), + "_asctime" => Function::new_native(ctx, crate::time::_asctime), + "_asctime_r" => Function::new_native(ctx, crate::time::_asctime_r), + "_localtime" => Function::new_native(ctx, crate::time::_localtime), + "_time" => Function::new_native(ctx, crate::time::_time), + "_timegm" => Function::new_native(ctx, crate::time::_timegm), + "_strftime" => Function::new_native(ctx, crate::time::_strftime), + "_strftime_l" => Function::new_native(ctx, crate::time::_strftime_l), + "_localtime_r" => Function::new_native(ctx, crate::time::_localtime_r), + "_gmtime_r" => Function::new_native(ctx, crate::time::_gmtime_r), + "_ctime" => Function::new_native(ctx, crate::time::_ctime), + "_ctime_r" => Function::new_native(ctx, crate::time::_ctime_r), + "_mktime" => Function::new_native(ctx, crate::time::_mktime), + "_gmtime" => Function::new_native(ctx, crate::time::_gmtime), // Math - "sqrt" => Function::new_native(store, crate::math::sqrt), - "floor" => Function::new_native(store, crate::math::floor), - "fabs" => Function::new_native(store, crate::math::fabs), - "f64-rem" => Function::new_native(store, crate::math::f64_rem), - "_llvm_copysign_f32" => Function::new_native(store, crate::math::_llvm_copysign_f32), - "_llvm_copysign_f64" => Function::new_native(store, crate::math::_llvm_copysign_f64), - "_llvm_log10_f64" => Function::new_native(store, crate::math::_llvm_log10_f64), - "_llvm_log2_f64" => Function::new_native(store, crate::math::_llvm_log2_f64), - "_llvm_log10_f32" => Function::new_native(store, crate::math::_llvm_log10_f32), - "_llvm_log2_f32" => Function::new_native(store, crate::math::_llvm_log2_f64), - "_llvm_sin_f64" => Function::new_native(store, crate::math::_llvm_sin_f64), - "_llvm_cos_f64" => Function::new_native(store, crate::math::_llvm_cos_f64), - "_llvm_exp2_f32" => Function::new_native(store, crate::math::_llvm_exp2_f32), - "_llvm_exp2_f64" => Function::new_native(store, crate::math::_llvm_exp2_f64), - "_llvm_trunc_f64" => Function::new_native(store, crate::math::_llvm_trunc_f64), - "_llvm_fma_f64" => Function::new_native(store, crate::math::_llvm_fma_f64), - "_emscripten_random" => Function::new_native_with_env(store, env.clone(), crate::math::_emscripten_random), + "sqrt" => Function::new_native(ctx, crate::math::sqrt), + "floor" => Function::new_native(ctx, crate::math::floor), + "fabs" => Function::new_native(ctx, crate::math::fabs), + "f64-rem" => Function::new_native(ctx, crate::math::f64_rem), + "_llvm_copysign_f32" => Function::new_native(ctx, crate::math::_llvm_copysign_f32), + "_llvm_copysign_f64" => Function::new_native(ctx, crate::math::_llvm_copysign_f64), + "_llvm_log10_f64" => Function::new_native(ctx, crate::math::_llvm_log10_f64), + "_llvm_log2_f64" => Function::new_native(ctx, crate::math::_llvm_log2_f64), + "_llvm_log10_f32" => Function::new_native(ctx, crate::math::_llvm_log10_f32), + "_llvm_log2_f32" => Function::new_native(ctx, crate::math::_llvm_log2_f64), + "_llvm_sin_f64" => Function::new_native(ctx, crate::math::_llvm_sin_f64), + "_llvm_cos_f64" => Function::new_native(ctx, crate::math::_llvm_cos_f64), + "_llvm_exp2_f32" => Function::new_native(ctx, crate::math::_llvm_exp2_f32), + "_llvm_exp2_f64" => Function::new_native(ctx, crate::math::_llvm_exp2_f64), + "_llvm_trunc_f64" => Function::new_native(ctx, crate::math::_llvm_trunc_f64), + "_llvm_fma_f64" => Function::new_native(ctx, crate::math::_llvm_fma_f64), + "_emscripten_random" => Function::new_native(ctx, crate::math::_emscripten_random), // Jump - "__setjmp" => Function::new_native_with_env(store, env.clone(), crate::jmp::__setjmp), - "__longjmp" => Function::new_native_with_env(store, env.clone(), crate::jmp::__longjmp), - "_longjmp" => Function::new_native_with_env(store, env.clone(), crate::jmp::_longjmp), - "_emscripten_longjmp" => Function::new_native_with_env(store, env.clone(), crate::jmp::_longjmp), + "__setjmp" => Function::new_native(ctx, crate::jmp::__setjmp), + "__longjmp" => Function::new_native(ctx, crate::jmp::__longjmp), + "_longjmp" => Function::new_native(ctx, crate::jmp::_longjmp), + "_emscripten_longjmp" => Function::new_native(ctx, crate::jmp::_longjmp), // Bitwise - "_llvm_bswap_i64" => Function::new_native_with_env(store, env.clone(), crate::bitwise::_llvm_bswap_i64), + "_llvm_bswap_i64" => Function::new_native(ctx, crate::bitwise::_llvm_bswap_i64), // libc - "_execv" => Function::new_native(store, crate::libc::execv), - "_endpwent" => Function::new_native(store, crate::libc::endpwent), - "_fexecve" => Function::new_native(store, crate::libc::fexecve), - "_fpathconf" => Function::new_native(store, crate::libc::fpathconf), - "_getitimer" => Function::new_native(store, crate::libc::getitimer), - "_getpwent" => Function::new_native(store, crate::libc::getpwent), - "_killpg" => Function::new_native(store, crate::libc::killpg), - "_pathconf" => Function::new_native_with_env(store, env.clone(), crate::libc::pathconf), - "_siginterrupt" => Function::new_native_with_env(store, env.clone(), crate::signal::_siginterrupt), - "_setpwent" => Function::new_native(store, crate::libc::setpwent), - "_sigismember" => Function::new_native(store, crate::libc::sigismember), - "_sigpending" => Function::new_native(store, crate::libc::sigpending), - "___libc_current_sigrtmax" => Function::new_native(store, crate::libc::current_sigrtmax), - "___libc_current_sigrtmin" => Function::new_native(store, crate::libc::current_sigrtmin), + "_execv" => Function::new_native(ctx, crate::libc::execv), + "_endpwent" => Function::new_native(ctx, crate::libc::endpwent), + "_fexecve" => Function::new_native(ctx, crate::libc::fexecve), + "_fpathconf" => Function::new_native(ctx, crate::libc::fpathconf), + "_getitimer" => Function::new_native(ctx, crate::libc::getitimer), + "_getpwent" => Function::new_native(ctx, crate::libc::getpwent), + "_killpg" => Function::new_native(ctx, crate::libc::killpg), + "_pathconf" => Function::new_native(ctx, crate::libc::pathconf), + "_siginterrupt" => Function::new_native(ctx, crate::signal::_siginterrupt), + "_setpwent" => Function::new_native(ctx, crate::libc::setpwent), + "_sigismember" => Function::new_native(ctx, crate::libc::sigismember), + "_sigpending" => Function::new_native(ctx, crate::libc::sigpending), + "___libc_current_sigrtmax" => Function::new_native(ctx, crate::libc::current_sigrtmax), + "___libc_current_sigrtmin" => Function::new_native(ctx, crate::libc::current_sigrtmin), // Linking - "_dlclose" => Function::new_native_with_env(store, env.clone(), crate::linking::_dlclose), - "_dlerror" => Function::new_native_with_env(store, env.clone(), crate::linking::_dlerror), - "_dlopen" => Function::new_native_with_env(store, env.clone(), crate::linking::_dlopen), - "_dlsym" => Function::new_native_with_env(store, env.clone(), crate::linking::_dlsym), + "_dlclose" => Function::new_native(ctx, crate::linking::_dlclose), + "_dlerror" => Function::new_native(ctx, crate::linking::_dlerror), + "_dlopen" => Function::new_native(ctx, crate::linking::_dlopen), + "_dlsym" => Function::new_native(ctx, crate::linking::_dlsym), // wasm32-unknown-emscripten - "_alarm" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_alarm), - "_atexit" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_atexit), - "setTempRet0" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::setTempRet0), - "getTempRet0" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::getTempRet0), - "invoke_i" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_i), - "invoke_ii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_ii), - "invoke_iii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iii), - "invoke_iiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiii), - "invoke_iifi" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iifi), - "invoke_v" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_v), - "invoke_vi" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vi), - "invoke_vj" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vj), - "invoke_vjji" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vjji), - "invoke_vii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vii), - "invoke_viii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viii), - "invoke_viiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiii), - "__Unwind_Backtrace" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::__Unwind_Backtrace), - "__Unwind_FindEnclosingFunction" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::__Unwind_FindEnclosingFunction), - "__Unwind_GetIPInfo" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::__Unwind_GetIPInfo), - "___cxa_find_matching_catch_2" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::___cxa_find_matching_catch_2), - "___cxa_find_matching_catch_3" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::___cxa_find_matching_catch_3), - "___cxa_free_exception" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::___cxa_free_exception), - "___resumeException" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::___resumeException), - "_dladdr" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_dladdr), - "_pthread_attr_destroy" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_attr_destroy), - "_pthread_attr_getstack" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_attr_getstack), - "_pthread_attr_init" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_attr_init), - "_pthread_attr_setstacksize" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_attr_setstacksize), - "_pthread_cleanup_pop" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cleanup_pop), - "_pthread_cleanup_push" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cleanup_push), - "_pthread_cond_destroy" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cond_destroy), - "_pthread_cond_init" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cond_init), - "_pthread_cond_signal" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cond_signal), - "_pthread_cond_timedwait" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cond_timedwait), - "_pthread_cond_wait" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_cond_wait), - "_pthread_condattr_destroy" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_condattr_destroy), - "_pthread_condattr_init" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_condattr_init), - "_pthread_condattr_setclock" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_condattr_setclock), - "_pthread_create" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_create), - "_pthread_detach" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_detach), - "_pthread_equal" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_equal), - "_pthread_exit" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_exit), - "_pthread_self" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_self), - "_pthread_getattr_np" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_getattr_np), - "_pthread_getspecific" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_getspecific), - "_pthread_join" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_join), - "_pthread_key_create" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_key_create), - "_pthread_mutex_destroy" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_mutex_destroy), - "_pthread_mutex_init" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_mutex_init), - "_pthread_mutexattr_destroy" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_mutexattr_destroy), - "_pthread_mutexattr_init" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_mutexattr_init), - "_pthread_mutexattr_settype" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_mutexattr_settype), - "_pthread_once" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_once), - "_pthread_rwlock_destroy" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_rwlock_destroy), - "_pthread_rwlock_init" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_rwlock_init), - "_pthread_rwlock_rdlock" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_rwlock_rdlock), - "_pthread_rwlock_unlock" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_rwlock_unlock), - "_pthread_rwlock_wrlock" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_rwlock_wrlock), - "_pthread_setcancelstate" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_setcancelstate), - "_pthread_setspecific" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_setspecific), - "_pthread_sigmask" => Function::new_native_with_env(store, env.clone(), crate::pthread::_pthread_sigmask), - "___gxx_personality_v0" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::___gxx_personality_v0), - "_gai_strerror" => Function::new_native_with_env(store, env.clone(), crate::env::_gai_strerror), - "_getdtablesize" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_getdtablesize), - "_gethostbyaddr" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_gethostbyaddr), - "_gethostbyname" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_gethostbyname), - "_gethostbyname_r" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_gethostbyname_r), - "_getloadavg" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_getloadavg), - "_getnameinfo" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::_getnameinfo), - "invoke_dii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_dii), - "invoke_diiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_diiii), - "invoke_iiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiii), - "invoke_iiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiiii), - "invoke_iiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiiiii), - "invoke_iiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiiiiii), - "invoke_iiiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiiiiiii), - "invoke_iiiiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiiiiiiii), - "invoke_iiiiiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiiiiiiiiii), - "invoke_vd" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vd), - "invoke_viiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiii), - "invoke_viiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiiii), - "invoke_viiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiiiii), - "invoke_viiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiiiiii), - "invoke_viiiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiiiiiii), - "invoke_viiiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiiiiiii), - "invoke_viiiiiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiiiiiiiii), - "invoke_iij" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iij), - "invoke_iji" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iji), - "invoke_iiji" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiji), - "invoke_iiijj" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_iiijj), - "invoke_j" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_j), - "invoke_ji" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_ji), - "invoke_jii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_jii), - "invoke_jij" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_jij), - "invoke_jjj" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_jjj), - "invoke_viiij" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiij), - "invoke_viiijiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiijiiii), - "invoke_viiijiiiiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiijiiiiii), - "invoke_viij" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viij), - "invoke_viiji" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viiji), - "invoke_viijiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viijiii), - "invoke_viijj" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viijj), - "invoke_vij" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vij), - "invoke_viji" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viji), - "invoke_vijiii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vijiii), - "invoke_vijj" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vijj), - "invoke_vidd" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_vidd), - "invoke_viid" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viid), - "invoke_viidii" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viidii), - "invoke_viidddddddd" => Function::new_native_with_env(store, env.clone(), crate::emscripten_target::invoke_viidddddddd), + "_alarm" => Function::new_native(ctx, crate::emscripten_target::_alarm), + "_atexit" => Function::new_native(ctx, crate::emscripten_target::_atexit), + "setTempRet0" => Function::new_native(ctx, crate::emscripten_target::setTempRet0), + "getTempRet0" => Function::new_native(ctx, crate::emscripten_target::getTempRet0), + "invoke_i" => Function::new_native(ctx, crate::emscripten_target::invoke_i), + "invoke_ii" => Function::new_native(ctx, crate::emscripten_target::invoke_ii), + "invoke_iii" => Function::new_native(ctx, crate::emscripten_target::invoke_iii), + "invoke_iiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiii), + "invoke_iifi" => Function::new_native(ctx, crate::emscripten_target::invoke_iifi), + "invoke_v" => Function::new_native(ctx, crate::emscripten_target::invoke_v), + "invoke_vi" => Function::new_native(ctx, crate::emscripten_target::invoke_vi), + "invoke_vj" => Function::new_native(ctx, crate::emscripten_target::invoke_vj), + "invoke_vjji" => Function::new_native(ctx, crate::emscripten_target::invoke_vjji), + "invoke_vii" => Function::new_native(ctx, crate::emscripten_target::invoke_vii), + "invoke_viii" => Function::new_native(ctx, crate::emscripten_target::invoke_viii), + "invoke_viiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiii), + "__Unwind_Backtrace" => Function::new_native(ctx, crate::emscripten_target::__Unwind_Backtrace), + "__Unwind_FindEnclosingFunction" => Function::new_native(ctx, crate::emscripten_target::__Unwind_FindEnclosingFunction), + "__Unwind_GetIPInfo" => Function::new_native(ctx, crate::emscripten_target::__Unwind_GetIPInfo), + "___cxa_find_matching_catch_2" => Function::new_native(ctx, crate::emscripten_target::___cxa_find_matching_catch_2), + "___cxa_find_matching_catch_3" => Function::new_native(ctx, crate::emscripten_target::___cxa_find_matching_catch_3), + "___cxa_free_exception" => Function::new_native(ctx, crate::emscripten_target::___cxa_free_exception), + "___resumeException" => Function::new_native(ctx, crate::emscripten_target::___resumeException), + "_dladdr" => Function::new_native(ctx, crate::emscripten_target::_dladdr), + "_pthread_attr_destroy" => Function::new_native(ctx, crate::pthread::_pthread_attr_destroy), + "_pthread_attr_getstack" => Function::new_native(ctx, crate::pthread::_pthread_attr_getstack), + "_pthread_attr_init" => Function::new_native(ctx, crate::pthread::_pthread_attr_init), + "_pthread_attr_setstacksize" => Function::new_native(ctx, crate::pthread::_pthread_attr_setstacksize), + "_pthread_cleanup_pop" => Function::new_native(ctx, crate::pthread::_pthread_cleanup_pop), + "_pthread_cleanup_push" => Function::new_native(ctx, crate::pthread::_pthread_cleanup_push), + "_pthread_cond_destroy" => Function::new_native(ctx, crate::pthread::_pthread_cond_destroy), + "_pthread_cond_init" => Function::new_native(ctx, crate::pthread::_pthread_cond_init), + "_pthread_cond_signal" => Function::new_native(ctx, crate::pthread::_pthread_cond_signal), + "_pthread_cond_timedwait" => Function::new_native(ctx, crate::pthread::_pthread_cond_timedwait), + "_pthread_cond_wait" => Function::new_native(ctx, crate::pthread::_pthread_cond_wait), + "_pthread_condattr_destroy" => Function::new_native(ctx, crate::pthread::_pthread_condattr_destroy), + "_pthread_condattr_init" => Function::new_native(ctx, crate::pthread::_pthread_condattr_init), + "_pthread_condattr_setclock" => Function::new_native(ctx, crate::pthread::_pthread_condattr_setclock), + "_pthread_create" => Function::new_native(ctx, crate::pthread::_pthread_create), + "_pthread_detach" => Function::new_native(ctx, crate::pthread::_pthread_detach), + "_pthread_equal" => Function::new_native(ctx, crate::pthread::_pthread_equal), + "_pthread_exit" => Function::new_native(ctx, crate::pthread::_pthread_exit), + "_pthread_self" => Function::new_native(ctx, crate::pthread::_pthread_self), + "_pthread_getattr_np" => Function::new_native(ctx, crate::pthread::_pthread_getattr_np), + "_pthread_getspecific" => Function::new_native(ctx, crate::pthread::_pthread_getspecific), + "_pthread_join" => Function::new_native(ctx, crate::pthread::_pthread_join), + "_pthread_key_create" => Function::new_native(ctx, crate::pthread::_pthread_key_create), + "_pthread_mutex_destroy" => Function::new_native(ctx, crate::pthread::_pthread_mutex_destroy), + "_pthread_mutex_init" => Function::new_native(ctx, crate::pthread::_pthread_mutex_init), + "_pthread_mutexattr_destroy" => Function::new_native(ctx, crate::pthread::_pthread_mutexattr_destroy), + "_pthread_mutexattr_init" => Function::new_native(ctx, crate::pthread::_pthread_mutexattr_init), + "_pthread_mutexattr_settype" => Function::new_native(ctx, crate::pthread::_pthread_mutexattr_settype), + "_pthread_once" => Function::new_native(ctx, crate::pthread::_pthread_once), + "_pthread_rwlock_destroy" => Function::new_native(ctx, crate::pthread::_pthread_rwlock_destroy), + "_pthread_rwlock_init" => Function::new_native(ctx, crate::pthread::_pthread_rwlock_init), + "_pthread_rwlock_rdlock" => Function::new_native(ctx, crate::pthread::_pthread_rwlock_rdlock), + "_pthread_rwlock_unlock" => Function::new_native(ctx, crate::pthread::_pthread_rwlock_unlock), + "_pthread_rwlock_wrlock" => Function::new_native(ctx, crate::pthread::_pthread_rwlock_wrlock), + "_pthread_setcancelstate" => Function::new_native(ctx, crate::pthread::_pthread_setcancelstate), + "_pthread_setspecific" => Function::new_native(ctx, crate::pthread::_pthread_setspecific), + "_pthread_sigmask" => Function::new_native(ctx, crate::pthread::_pthread_sigmask), + "___gxx_personality_v0" => Function::new_native(ctx, crate::emscripten_target::___gxx_personality_v0), + "_gai_strerror" => Function::new_native(ctx, crate::env::_gai_strerror), + "_getdtablesize" => Function::new_native(ctx, crate::emscripten_target::_getdtablesize), + "_gethostbyaddr" => Function::new_native(ctx, crate::emscripten_target::_gethostbyaddr), + "_gethostbyname" => Function::new_native(ctx, crate::emscripten_target::_gethostbyname), + "_gethostbyname_r" => Function::new_native(ctx, crate::emscripten_target::_gethostbyname_r), + "_getloadavg" => Function::new_native(ctx, crate::emscripten_target::_getloadavg), + "_getnameinfo" => Function::new_native(ctx, crate::emscripten_target::_getnameinfo), + "invoke_dii" => Function::new_native(ctx, crate::emscripten_target::invoke_dii), + "invoke_diiii" => Function::new_native(ctx, crate::emscripten_target::invoke_diiii), + "invoke_iiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiii), + "invoke_iiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiiii), + "invoke_iiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiiiii), + "invoke_iiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiiiiii), + "invoke_iiiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiiiiiii), + "invoke_iiiiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiiiiiiii), + "invoke_iiiiiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_iiiiiiiiiii), + "invoke_vd" => Function::new_native(ctx, crate::emscripten_target::invoke_vd), + "invoke_viiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiii), + "invoke_viiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiiii), + "invoke_viiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiiiii), + "invoke_viiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiiiiii), + "invoke_viiiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiiiiiii), + "invoke_viiiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiiiiiii), + "invoke_viiiiiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiiiiiiiii), + "invoke_iij" => Function::new_native(ctx, crate::emscripten_target::invoke_iij), + "invoke_iji" => Function::new_native(ctx, crate::emscripten_target::invoke_iji), + "invoke_iiji" => Function::new_native(ctx, crate::emscripten_target::invoke_iiji), + "invoke_iiijj" => Function::new_native(ctx, crate::emscripten_target::invoke_iiijj), + "invoke_j" => Function::new_native(ctx, crate::emscripten_target::invoke_j), + "invoke_ji" => Function::new_native(ctx, crate::emscripten_target::invoke_ji), + "invoke_jii" => Function::new_native(ctx, crate::emscripten_target::invoke_jii), + "invoke_jij" => Function::new_native(ctx, crate::emscripten_target::invoke_jij), + "invoke_jjj" => Function::new_native(ctx, crate::emscripten_target::invoke_jjj), + "invoke_viiij" => Function::new_native(ctx, crate::emscripten_target::invoke_viiij), + "invoke_viiijiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiijiiii), + "invoke_viiijiiiiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viiijiiiiii), + "invoke_viij" => Function::new_native(ctx, crate::emscripten_target::invoke_viij), + "invoke_viiji" => Function::new_native(ctx, crate::emscripten_target::invoke_viiji), + "invoke_viijiii" => Function::new_native(ctx, crate::emscripten_target::invoke_viijiii), + "invoke_viijj" => Function::new_native(ctx, crate::emscripten_target::invoke_viijj), + "invoke_vij" => Function::new_native(ctx, crate::emscripten_target::invoke_vij), + "invoke_viji" => Function::new_native(ctx, crate::emscripten_target::invoke_viji), + "invoke_vijiii" => Function::new_native(ctx, crate::emscripten_target::invoke_vijiii), + "invoke_vijj" => Function::new_native(ctx, crate::emscripten_target::invoke_vijj), + "invoke_vidd" => Function::new_native(ctx, crate::emscripten_target::invoke_vidd), + "invoke_viid" => Function::new_native(ctx, crate::emscripten_target::invoke_viid), + "invoke_viidii" => Function::new_native(ctx, crate::emscripten_target::invoke_viidii), + "invoke_viidddddddd" => Function::new_native(ctx, crate::emscripten_target::invoke_viidddddddd), // ucontext - "_getcontext" => Function::new_native_with_env(store, env.clone(), crate::ucontext::_getcontext), - "_makecontext" => Function::new_native_with_env(store, env.clone(), crate::ucontext::_makecontext), - "_setcontext" => Function::new_native_with_env(store, env.clone(), crate::ucontext::_setcontext), - "_swapcontext" => Function::new_native_with_env(store, env.clone(), crate::ucontext::_swapcontext), + "_getcontext" => Function::new_native(ctx, crate::ucontext::_getcontext), + "_makecontext" => Function::new_native(ctx, crate::ucontext::_makecontext), + "_setcontext" => Function::new_native(ctx, crate::ucontext::_setcontext), + "_swapcontext" => Function::new_native(ctx, crate::ucontext::_swapcontext), // unistd - "_confstr" => Function::new_native_with_env(store, env.clone(), crate::unistd::confstr), + "_confstr" => Function::new_native(ctx, crate::unistd::confstr), }; // Compatibility with newer versions of Emscripten @@ -1012,31 +1424,31 @@ pub fn generate_emscripten_env( for null_function_name in globals.null_function_names.iter() { env_ns.insert( null_function_name.as_str(), - Function::new_native_with_env(store, env.clone(), nullfunc), + Function::new_native(ctx, nullfunc), ); } let import_object: Imports = imports! { "env" => env_ns, "global" => { - "NaN" => Global::new(store, Val::F64(f64::NAN)), - "Infinity" => Global::new(store, Val::F64(f64::INFINITY)), + "NaN" => Global::new(ctx, Value::F64(f64::NAN)), + "Infinity" => Global::new(ctx, Value::F64(f64::INFINITY)), }, "global.Math" => { - "pow" => Function::new_native(store, crate::math::pow), - "exp" => Function::new_native(store, crate::math::exp), - "log" => Function::new_native(store, crate::math::log), + "pow" => Function::new_native(ctx, crate::math::pow), + "exp" => Function::new_native(ctx, crate::math::exp), + "log" => Function::new_native(ctx, crate::math::log), }, "asm2wasm" => { - "f64-rem" => Function::new_native(store, crate::math::f64_rem), - "f64-to-int" => Function::new_native(store, crate::math::f64_to_int), + "f64-rem" => Function::new_native(ctx, crate::math::f64_rem), + "f64-to-int" => Function::new_native(ctx, crate::math::f64_to_int), }, }; import_object } -pub fn nullfunc(ctx: &EmEnv, _x: u32) { +pub fn nullfunc(ctx: ContextMut<'_, EmEnv>, _x: u32) { use crate::process::abort_with_message; debug!("emscripten::nullfunc_i {}", _x); abort_with_message( diff --git a/lib/emscripten/src/libc.rs b/lib/emscripten/src/libc.rs index 81d67971da2..3916a553b48 100644 --- a/lib/emscripten/src/libc.rs +++ b/lib/emscripten/src/libc.rs @@ -1,76 +1,77 @@ extern crate libc; use crate::EmEnv; +use wasmer::ContextMut; #[cfg(unix)] use std::convert::TryInto; -pub fn current_sigrtmax() -> i32 { +pub fn current_sigrtmax(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::current_sigrtmax"); 0 } -pub fn current_sigrtmin() -> i32 { +pub fn current_sigrtmin(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::current_sigrtmin"); 0 } -pub fn endpwent() { +pub fn endpwent(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::endpwent"); } -pub fn execv(_a: i32, _b: i32) -> i32 { +pub fn execv(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::execv"); 0 } -pub fn fexecve(_a: i32, _b: i32, _c: i32) -> i32 { +pub fn fexecve(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32, _c: i32) -> i32 { debug!("emscripten::fexecve"); 0 } -pub fn fpathconf(_a: i32, _b: i32) -> i32 { +pub fn fpathconf(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::fpathconf"); 0 } -pub fn getitimer(_a: i32, _b: i32) -> i32 { +pub fn getitimer(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::getitimer"); 0 } -pub fn getpwent() -> i32 { +pub fn getpwent(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::getpwent"); 0 } -pub fn killpg(_a: i32, _b: i32) -> i32 { +pub fn killpg(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::killpg"); 0 } #[cfg(unix)] -pub fn pathconf(ctx: &EmEnv, path_ptr: i32, name: i32) -> i32 { +pub fn pathconf(ctx: ContextMut<'_, EmEnv>, path_ptr: i32, name: i32) -> i32 { debug!("emscripten::pathconf"); - let path = emscripten_memory_pointer!(ctx.memory(0), path_ptr) as *const i8; + let path = emscripten_memory_pointer!(ctx, ctx.data().memory(0), path_ptr) as *const i8; unsafe { libc::pathconf(path as *const _, name).try_into().unwrap() } } #[cfg(not(unix))] -pub fn pathconf(_ctx: &EmEnv, _path_ptr: i32, _name: i32) -> i32 { +pub fn pathconf(_ctx: ContextMut<'_, EmEnv>, _path_ptr: i32, _name: i32) -> i32 { debug!("emscripten::pathconf"); 0 } -pub fn setpwent() { +pub fn setpwent(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::setpwent"); } -pub fn sigismember(_a: i32, _b: i32) -> i32 { +pub fn sigismember(_ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::sigismember"); 0 } -pub fn sigpending(_a: i32) -> i32 { +pub fn sigpending(_ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { debug!("emscripten::sigpending"); 0 } diff --git a/lib/emscripten/src/linking.rs b/lib/emscripten/src/linking.rs index 5ca49e85e13..585c2e46c8a 100644 --- a/lib/emscripten/src/linking.rs +++ b/lib/emscripten/src/linking.rs @@ -1,27 +1,28 @@ use crate::EmEnv; +use wasmer::ContextMut; // TODO: Need to implement. /// emscripten: dlopen(filename: *const c_char, flag: c_int) -> *mut c_void -pub fn _dlopen(_ctx: &EmEnv, _filename: u32, _flag: u32) -> i32 { +pub fn _dlopen(mut _ctx: ContextMut<'_, EmEnv>, _filename: u32, _flag: u32) -> i32 { debug!("emscripten::_dlopen"); -1 } /// emscripten: dlclose(handle: *mut c_void) -> c_int -pub fn _dlclose(_ctx: &EmEnv, _filename: u32) -> i32 { +pub fn _dlclose(mut _ctx: ContextMut<'_, EmEnv>, _filename: u32) -> i32 { debug!("emscripten::_dlclose"); -1 } /// emscripten: dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void -pub fn _dlsym(_ctx: &EmEnv, _filepath: u32, _symbol: u32) -> i32 { +pub fn _dlsym(mut _ctx: ContextMut<'_, EmEnv>, _filepath: u32, _symbol: u32) -> i32 { debug!("emscripten::_dlsym"); -1 } /// emscripten: dlerror() -> *mut c_char -pub fn _dlerror(_ctx: &EmEnv) -> i32 { +pub fn _dlerror(mut _ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::_dlerror"); -1 } diff --git a/lib/emscripten/src/lock.rs b/lib/emscripten/src/lock.rs index f83b12e38bd..118ff9d5a28 100644 --- a/lib/emscripten/src/lock.rs +++ b/lib/emscripten/src/lock.rs @@ -1,22 +1,29 @@ use crate::EmEnv; use libc::c_int; +use wasmer::ContextMut; // NOTE: Not implemented by Emscripten -pub fn ___lock(_ctx: &EmEnv, _what: c_int) { +pub fn ___lock(mut _ctx: ContextMut<'_, EmEnv>, _what: c_int) { debug!("emscripten::___lock {}", _what); } // NOTE: Not implemented by Emscripten -pub fn ___unlock(_ctx: &EmEnv, _what: c_int) { +pub fn ___unlock(mut _ctx: ContextMut<'_, EmEnv>, _what: c_int) { debug!("emscripten::___unlock {}", _what); } // NOTE: Not implemented by Emscripten -pub fn ___wait(_ctx: &EmEnv, _which: u32, _varargs: u32, _three: u32, _four: u32) { +pub fn ___wait( + mut _ctx: ContextMut<'_, EmEnv>, + _which: u32, + _varargs: u32, + _three: u32, + _four: u32, +) { debug!("emscripten::___wait"); } -pub fn _flock(_ctx: &EmEnv, _fd: u32, _op: u32) -> u32 { +pub fn _flock(mut _ctx: ContextMut<'_, EmEnv>, _fd: u32, _op: u32) -> u32 { debug!("emscripten::_flock"); 0 } diff --git a/lib/emscripten/src/macros.rs b/lib/emscripten/src/macros.rs index 4cd2550b17b..904721fbb19 100644 --- a/lib/emscripten/src/macros.rs +++ b/lib/emscripten/src/macros.rs @@ -1,5 +1,5 @@ macro_rules! emscripten_memory_pointer { - ($memory:expr, $pointer:expr) => { - $memory.data_ptr().wrapping_add($pointer as usize) + ($ctx:expr, $memory:expr, $pointer:expr) => { + $memory.data_ptr(&$ctx).wrapping_add($pointer as usize) }; } diff --git a/lib/emscripten/src/math.rs b/lib/emscripten/src/math.rs index 6328e1853be..896bc3d38f0 100644 --- a/lib/emscripten/src/math.rs +++ b/lib/emscripten/src/math.rs @@ -1,110 +1,111 @@ use crate::EmEnv; +use wasmer::ContextMut; -pub fn _llvm_copysign_f32(x: f64, y: f64) -> f64 { +pub fn _llvm_copysign_f32(mut _ctx: ContextMut<'_, EmEnv>, x: f64, y: f64) -> f64 { x.copysign(y) } -pub fn _llvm_copysign_f64(x: f64, y: f64) -> f64 { +pub fn _llvm_copysign_f64(mut _ctx: ContextMut<'_, EmEnv>, x: f64, y: f64) -> f64 { x.copysign(y) } /// emscripten: _llvm_log10_f64 -pub fn _llvm_log10_f64(value: f64) -> f64 { +pub fn _llvm_log10_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { debug!("emscripten::_llvm_log10_f64"); value.log10() } /// emscripten: _llvm_log2_f64 -pub fn _llvm_log2_f64(value: f64) -> f64 { +pub fn _llvm_log2_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { debug!("emscripten::_llvm_log2_f64"); value.log2() } /// emscripten: _llvm_sin_f64 -pub fn _llvm_sin_f64(value: f64) -> f64 { +pub fn _llvm_sin_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { debug!("emscripten::_llvm_sin_f64"); value.sin() } /// emscripten: _llvm_cos_f64 -pub fn _llvm_cos_f64(value: f64) -> f64 { +pub fn _llvm_cos_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { debug!("emscripten::_llvm_cos_f64"); value.cos() } -pub fn _llvm_log10_f32(_value: f64) -> f64 { +pub fn _llvm_log10_f32(mut _ctx: ContextMut<'_, EmEnv>, _value: f64) -> f64 { debug!("emscripten::_llvm_log10_f32"); -1.0 } -pub fn _llvm_log2_f32(_value: f64) -> f64 { +pub fn _llvm_log2_f32(mut _ctx: ContextMut<'_, EmEnv>, _value: f64) -> f64 { debug!("emscripten::_llvm_log10_f32"); -1.0 } -pub fn _llvm_exp2_f32(value: f32) -> f32 { +pub fn _llvm_exp2_f32(mut _ctx: ContextMut<'_, EmEnv>, value: f32) -> f32 { debug!("emscripten::_llvm_exp2_f32"); 2f32.powf(value) } -pub fn _llvm_exp2_f64(value: f64) -> f64 { +pub fn _llvm_exp2_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { debug!("emscripten::_llvm_exp2_f64"); 2f64.powf(value) } -pub fn _llvm_trunc_f64(value: f64) -> f64 { +pub fn _llvm_trunc_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { debug!("emscripten::_llvm_trunc_f64"); value.trunc() } -pub fn _llvm_fma_f64(value: f64, a: f64, b: f64) -> f64 { +pub fn _llvm_fma_f64(mut _ctx: ContextMut<'_, EmEnv>, value: f64, a: f64, b: f64) -> f64 { debug!("emscripten::_llvm_fma_f64"); value.mul_add(a, b) } -pub fn _emscripten_random(_ctx: &EmEnv) -> f64 { +pub fn _emscripten_random(mut _ctx: ContextMut<'_, EmEnv>) -> f64 { debug!("emscripten::_emscripten_random"); -1.0 } // emscripten: asm2wasm.f64-rem -pub fn f64_rem(x: f64, y: f64) -> f64 { +pub fn f64_rem(mut _ctx: ContextMut<'_, EmEnv>, x: f64, y: f64) -> f64 { debug!("emscripten::f64-rem"); x % y } // emscripten: global.Math pow -pub fn pow(x: f64, y: f64) -> f64 { +pub fn pow(mut _ctx: ContextMut<'_, EmEnv>, x: f64, y: f64) -> f64 { x.powf(y) } // emscripten: global.Math exp -pub fn exp(value: f64) -> f64 { +pub fn exp(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { value.exp() } // emscripten: global.Math log -pub fn log(value: f64) -> f64 { +pub fn log(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { value.ln() } // emscripten: global.Math sqrt -pub fn sqrt(value: f64) -> f64 { +pub fn sqrt(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { value.sqrt() } // emscripten: global.Math floor -pub fn floor(value: f64) -> f64 { +pub fn floor(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { value.floor() } // emscripten: global.Math fabs -pub fn fabs(value: f64) -> f64 { +pub fn fabs(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> f64 { value.abs() } // emscripten: asm2wasm.f64-to-int -pub fn f64_to_int(value: f64) -> i32 { +pub fn f64_to_int(mut _ctx: ContextMut<'_, EmEnv>, value: f64) -> i32 { debug!("emscripten::f64_to_int {}", value); value as i32 } diff --git a/lib/emscripten/src/memory.rs b/lib/emscripten/src/memory.rs index aec81dd1511..065aaaec429 100644 --- a/lib/emscripten/src/memory.rs +++ b/lib/emscripten/src/memory.rs @@ -3,26 +3,32 @@ use super::process::abort_with_message; use crate::EmEnv; use libc::{c_int, c_void, memcpy, size_t}; // TODO: investigate max pages etc. probably in Wasm Common, maybe reexport -use wasmer::{Pages, WasmPtr, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE}; +use wasmer::{ + AsContextMut, ContextMut, Pages, WasmPtr, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE, +}; /// emscripten: _emscripten_memcpy_big -pub fn _emscripten_memcpy_big(ctx: &EmEnv, dest: u32, src: u32, len: u32) -> u32 { +pub fn _emscripten_memcpy_big(ctx: ContextMut<'_, EmEnv>, dest: u32, src: u32, len: u32) -> u32 { debug!( "emscripten::_emscripten_memcpy_big {}, {}, {}", dest, src, len ); - let dest_addr = emscripten_memory_pointer!(ctx.memory(0), dest) as *mut c_void; - let src_addr = emscripten_memory_pointer!(ctx.memory(0), src) as *mut c_void; + let dest_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), dest) as *mut c_void; + let src_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), src) as *mut c_void; unsafe { memcpy(dest_addr, src_addr, len as size_t); } dest } +fn get_heap_size(ctx: &ContextMut<'_, EmEnv>) -> u32 { + ctx.data().memory(0).size(&ctx).bytes().0 as u32 +} + /// emscripten: _emscripten_get_heap_size -pub fn _emscripten_get_heap_size(ctx: &EmEnv) -> u32 { +pub fn _emscripten_get_heap_size(ctx: ContextMut<'_, EmEnv>) -> u32 { trace!("emscripten::_emscripten_get_heap_size"); - let result = ctx.memory(0).size().bytes().0 as u32; + let result = get_heap_size(&ctx); trace!("=> {}", result); result @@ -36,11 +42,9 @@ fn align_up(mut val: usize, multiple: usize) -> usize { val } -/// emscripten: _emscripten_resize_heap -/// Note: this function only allows growing the size of heap -pub fn _emscripten_resize_heap(ctx: &EmEnv, requested_size: u32) -> u32 { +fn resize_heap(ctx: &mut ContextMut<'_, EmEnv>, requested_size: u32) -> u32 { debug!("emscripten::_emscripten_resize_heap {}", requested_size); - let current_memory_pages = ctx.memory(0).size(); + let current_memory_pages = ctx.data().memory(0).size(&ctx); let current_memory = current_memory_pages.bytes().0 as u32; // implementation from emscripten @@ -60,7 +64,11 @@ pub fn _emscripten_resize_heap(ctx: &EmEnv, requested_size: u32) -> u32 { } let amount_to_grow = (new_size - current_memory as usize) / WASM_PAGE_SIZE; - if let Ok(_pages_allocated) = ctx.memory(0).grow(Pages(amount_to_grow as u32)) { + if let Ok(_pages_allocated) = ctx + .data() + .memory(0) + .grow(&mut ctx.as_context_mut(), Pages(amount_to_grow as u32)) + { debug!("{} pages allocated", _pages_allocated.0); 1 } else { @@ -68,19 +76,27 @@ pub fn _emscripten_resize_heap(ctx: &EmEnv, requested_size: u32) -> u32 { } } +/// emscripten: _emscripten_resize_heap +/// Note: this function only allows growing the size of heap +pub fn _emscripten_resize_heap(mut ctx: ContextMut<'_, EmEnv>, requested_size: u32) -> u32 { + resize_heap(&mut ctx, requested_size) +} + /// emscripten: sbrk -pub fn sbrk(ctx: &EmEnv, increment: i32) -> i32 { +pub fn sbrk(mut ctx: ContextMut<'_, EmEnv>, increment: i32) -> i32 { debug!("emscripten::sbrk"); // let old_dynamic_top = 0; // let new_dynamic_top = 0; - let memory = ctx.memory(0); - let dynamictop_ptr = { - let globals = &get_emscripten_data(ctx).globals; - WasmPtr::::new(globals.dynamictop_ptr).deref(&memory) - }; + let memory = ctx.data().memory(0); + let top_ptr = get_emscripten_data(&ctx) + .as_ref() + .unwrap() + .globals + .dynamictop_ptr; + let dynamictop_ptr = WasmPtr::::new(top_ptr).deref(&ctx, &memory); let old_dynamic_top = dynamictop_ptr.read().unwrap(); let new_dynamic_top: i32 = old_dynamic_top + increment; - let total_memory = _emscripten_get_heap_size(ctx) as i32; + let total_memory = get_heap_size(&ctx) as i32; debug!( " => PTR {}, old: {}, new: {}, increment: {}, total: {}", dynamictop_ptr.offset(), @@ -94,25 +110,27 @@ pub fn sbrk(ctx: &EmEnv, increment: i32) -> i32 { return -1; } if new_dynamic_top > total_memory { - let resized = _emscripten_resize_heap(ctx, new_dynamic_top as u32); + let resized = resize_heap(&mut ctx, new_dynamic_top as u32); if resized == 0 { return -1; } } + // re-borrow the top ptr + let dynamictop_ptr = WasmPtr::::new(top_ptr).deref(&ctx, &memory); dynamictop_ptr.write(new_dynamic_top).unwrap(); old_dynamic_top as _ } /// emscripten: getTotalMemory -pub fn get_total_memory(_ctx: &EmEnv) -> u32 { +pub fn get_total_memory(ctx: ContextMut<'_, EmEnv>) -> u32 { debug!("emscripten::get_total_memory"); // instance.memories[0].current_pages() // TODO: Fix implementation - _ctx.memory(0).size().bytes().0 as u32 + ctx.data().memory(0).size(&ctx).bytes().0 as u32 } /// emscripten: enlargeMemory -pub fn enlarge_memory(_ctx: &EmEnv) -> u32 { +pub fn enlarge_memory(_ctx: ContextMut<'_, EmEnv>) -> u32 { debug!("emscripten::enlarge_memory"); // instance.memories[0].grow(100); // TODO: Fix implementation @@ -120,7 +138,7 @@ pub fn enlarge_memory(_ctx: &EmEnv) -> u32 { } /// emscripten: abortOnCannotGrowMemory -pub fn abort_on_cannot_grow_memory(ctx: &EmEnv, _requested_size: u32) -> u32 { +pub fn abort_on_cannot_grow_memory(ctx: ContextMut<'_, EmEnv>, _requested_size: u32) -> u32 { debug!( "emscripten::abort_on_cannot_grow_memory {}", _requested_size @@ -130,32 +148,32 @@ pub fn abort_on_cannot_grow_memory(ctx: &EmEnv, _requested_size: u32) -> u32 { } /// emscripten: abortOnCannotGrowMemory -pub fn abort_on_cannot_grow_memory_old(ctx: &EmEnv) -> u32 { +pub fn abort_on_cannot_grow_memory_old(ctx: ContextMut<'_, EmEnv>) -> u32 { debug!("emscripten::abort_on_cannot_grow_memory"); abort_with_message(ctx, "Cannot enlarge memory arrays!"); 0 } /// emscripten: segfault -pub fn segfault(ctx: &EmEnv) { +pub fn segfault(ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::segfault"); abort_with_message(ctx, "segmentation fault"); } /// emscripten: alignfault -pub fn alignfault(ctx: &EmEnv) { +pub fn alignfault(ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::alignfault"); abort_with_message(ctx, "alignment fault"); } /// emscripten: ftfault -pub fn ftfault(ctx: &EmEnv) { +pub fn ftfault(ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::ftfault"); abort_with_message(ctx, "Function table mask error"); } /// emscripten: ___map_file -pub fn ___map_file(_ctx: &EmEnv, _one: u32, _two: u32) -> c_int { +pub fn ___map_file(_ctx: ContextMut<'_, EmEnv>, _one: u32, _two: u32) -> c_int { debug!("emscripten::___map_file"); // NOTE: TODO: Em returns -1 here as well. May need to implement properly -1 diff --git a/lib/emscripten/src/process.rs b/lib/emscripten/src/process.rs index f5fe4eb07d9..665a5fb9b10 100644 --- a/lib/emscripten/src/process.rs +++ b/lib/emscripten/src/process.rs @@ -6,34 +6,35 @@ type PidT = libc::pid_t; type PidT = c_int; use crate::EmEnv; +use wasmer::ContextMut; -pub fn abort_with_message(ctx: &EmEnv, message: &str) { +pub fn abort_with_message(ctx: ContextMut<'_, EmEnv>, message: &str) { debug!("emscripten::abort_with_message"); println!("{}", message); _abort(ctx); } /// The name of this call is `abort` but we want to avoid conflicts with libc::abort -pub fn em_abort(ctx: &EmEnv, arg: u32) { +pub fn em_abort(ctx: ContextMut<'_, EmEnv>, arg: u32) { debug!("emscripten::abort"); eprintln!("Program aborted with value {}", arg); _abort(ctx); } -pub fn _abort(_ctx: &EmEnv) { +pub fn _abort(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::_abort"); unsafe { abort(); } } -pub fn _prctl(ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _prctl(ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { debug!("emscripten::_prctl"); abort_with_message(ctx, "missing function: prctl"); -1 } -pub fn _fork(_ctx: &EmEnv) -> PidT { +pub fn _fork(_ctx: ContextMut<'_, EmEnv>) -> PidT { debug!("emscripten::_fork"); // unsafe { // fork() @@ -41,132 +42,132 @@ pub fn _fork(_ctx: &EmEnv) -> PidT { -1 } -pub fn _endgrent(_ctx: &EmEnv) { +pub fn _endgrent(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::_endgrent"); } -pub fn _execve(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn _execve(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_execve"); -1 } #[allow(unreachable_code)] -pub fn _exit(_ctx: &EmEnv, status: c_int) { +pub fn _exit(_ctx: ContextMut<'_, EmEnv>, status: c_int) { // -> ! debug!("emscripten::_exit {}", status); unsafe { exit(status) } } -pub fn _kill(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn _kill(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::_kill"); -1 } -pub fn _sched_yield(_ctx: &EmEnv) -> i32 { +pub fn _sched_yield(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::_sched_yield"); -1 } -pub fn _llvm_stacksave(_ctx: &EmEnv) -> i32 { +pub fn _llvm_stacksave(_ctx: ContextMut<'_, EmEnv>) -> i32 { debug!("emscripten::_llvm_stacksave"); -1 } -pub fn _llvm_stackrestore(_ctx: &EmEnv, _one: i32) { +pub fn _llvm_stackrestore(_ctx: ContextMut<'_, EmEnv>, _one: i32) { debug!("emscripten::_llvm_stackrestore"); } -pub fn _raise(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _raise(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_raise"); -1 } -pub fn _sem_init(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn _sem_init(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_sem_init: {}, {}, {}", _one, _two, _three); 0 } -pub fn _sem_destroy(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _sem_destroy(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_sem_destroy"); 0 } -pub fn _sem_post(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _sem_post(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_sem_post"); -1 } -pub fn _sem_wait(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _sem_wait(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_sem_post"); -1 } #[allow(clippy::cast_ptr_alignment)] -pub fn _getgrent(_ctx: &EmEnv) -> c_int { +pub fn _getgrent(_ctx: ContextMut<'_, EmEnv>) -> c_int { debug!("emscripten::_getgrent"); -1 } -pub fn _setgrent(_ctx: &EmEnv) { +pub fn _setgrent(_ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::_setgrent"); } -pub fn _setgroups(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn _setgroups(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::_setgroups"); -1 } -pub fn _setitimer(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn _setitimer(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_setitimer"); -1 } -pub fn _usleep(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _usleep(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_usleep"); -1 } -pub fn _nanosleep(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn _nanosleep(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::_nanosleep"); -1 } -pub fn _utime(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn _utime(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::_utime"); -1 } -pub fn _utimes(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn _utimes(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::_utimes"); -1 } -pub fn _wait(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _wait(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_wait"); -1 } -pub fn _wait3(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn _wait3(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_wait3"); -1 } -pub fn _wait4(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32, _d: i32) -> i32 { +pub fn _wait4(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32, _d: i32) -> i32 { debug!("emscripten::_wait4"); -1 } -pub fn _waitid(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32, _d: i32) -> i32 { +pub fn _waitid(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32, _d: i32) -> i32 { debug!("emscripten::_waitid"); -1 } -pub fn _waitpid(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn _waitpid(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_waitpid"); -1 } -pub fn abort_stack_overflow(ctx: &EmEnv, _what: c_int) { +pub fn abort_stack_overflow(ctx: ContextMut<'_, EmEnv>, _what: c_int) { debug!("emscripten::abort_stack_overflow"); // TODO: Message incomplete. Need to finish em runtime data first abort_with_message( @@ -175,24 +176,24 @@ pub fn abort_stack_overflow(ctx: &EmEnv, _what: c_int) { ); } -pub fn _llvm_trap(ctx: &EmEnv) { +pub fn _llvm_trap(ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::_llvm_trap"); abort_with_message(ctx, "abort!"); } -pub fn _llvm_eh_typeid_for(_ctx: &EmEnv, _type_info_addr: u32) -> i32 { +pub fn _llvm_eh_typeid_for(_ctx: ContextMut<'_, EmEnv>, _type_info_addr: u32) -> i32 { debug!("emscripten::_llvm_eh_typeid_for"); -1 } -pub fn _system(_ctx: &EmEnv, _one: i32) -> c_int { +pub fn _system(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> c_int { debug!("emscripten::_system"); // TODO: May need to change this Em impl to a working version eprintln!("Can't call external programs"); EAGAIN } -pub fn _popen(_ctx: &EmEnv, _one: i32, _two: i32) -> c_int { +pub fn _popen(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> c_int { debug!("emscripten::_popen"); // TODO: May need to change this Em impl to a working version eprintln!("Missing function: popen"); diff --git a/lib/emscripten/src/pthread.rs b/lib/emscripten/src/pthread.rs index c4ab3d6b137..2ffd17c97a0 100644 --- a/lib/emscripten/src/pthread.rs +++ b/lib/emscripten/src/pthread.rs @@ -1,11 +1,17 @@ use crate::EmEnv; +use wasmer::ContextMut; -pub fn _pthread_attr_destroy(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_attr_destroy(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_attr_destroy"); 0 } -pub fn _pthread_attr_getstack(_ctx: &EmEnv, _stackaddr: i32, _stacksize: i32, _other: i32) -> i32 { +pub fn _pthread_attr_getstack( + mut _ctx: ContextMut<'_, EmEnv>, + _stackaddr: i32, + _stacksize: i32, + _other: i32, +) -> i32 { trace!( "emscripten::_pthread_attr_getstack({}, {}, {})", _stackaddr, @@ -18,175 +24,175 @@ pub fn _pthread_attr_getstack(_ctx: &EmEnv, _stackaddr: i32, _stacksize: i32, _o 0 } -pub fn _pthread_attr_init(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_attr_init(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_attr_init({})", _a); 0 } -pub fn _pthread_attr_setstacksize(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_attr_setstacksize(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_attr_setstacksize"); 0 } -pub fn _pthread_cleanup_pop(_ctx: &EmEnv, _a: i32) { +pub fn _pthread_cleanup_pop(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) { trace!("emscripten::_pthread_cleanup_pop"); } -pub fn _pthread_cleanup_push(_ctx: &EmEnv, _a: i32, _b: i32) { +pub fn _pthread_cleanup_push(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) { trace!("emscripten::_pthread_cleanup_push"); } -pub fn _pthread_cond_destroy(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_cond_destroy(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_cond_destroy"); 0 } -pub fn _pthread_cond_init(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_cond_init(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_cond_init"); 0 } -pub fn _pthread_cond_signal(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_cond_signal(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_cond_signal"); 0 } -pub fn _pthread_cond_timedwait(_ctx: &EmEnv, _a: i32, _b: i32, _c: i32) -> i32 { +pub fn _pthread_cond_timedwait(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32, _c: i32) -> i32 { trace!("emscripten::_pthread_cond_timedwait"); 0 } -pub fn _pthread_cond_wait(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_cond_wait(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_cond_wait"); 0 } -pub fn _pthread_condattr_destroy(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_condattr_destroy(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_condattr_destroy"); 0 } -pub fn _pthread_condattr_init(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_condattr_init(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_condattr_init"); 0 } -pub fn _pthread_condattr_setclock(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_condattr_setclock(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_condattr_setclock"); 0 } -pub fn _pthread_create(_ctx: &EmEnv, _a: i32, _b: i32, _c: i32, _d: i32) -> i32 { +pub fn _pthread_create(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32, _c: i32, _d: i32) -> i32 { trace!("emscripten::_pthread_create"); // 11 seems to mean "no" 11 } -pub fn _pthread_detach(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_detach(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_detach"); 0 } -pub fn _pthread_equal(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_equal(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_equal"); 0 } -pub fn _pthread_exit(_ctx: &EmEnv, _a: i32) { +pub fn _pthread_exit(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) { trace!("emscripten::_pthread_exit"); } -pub fn _pthread_getattr_np(_ctx: &EmEnv, _thread: i32, _attr: i32) -> i32 { +pub fn _pthread_getattr_np(mut _ctx: ContextMut<'_, EmEnv>, _thread: i32, _attr: i32) -> i32 { trace!("emscripten::_pthread_getattr_np({}, {})", _thread, _attr); 0 } -pub fn _pthread_getspecific(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_getspecific(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_getspecific"); 0 } -pub fn _pthread_join(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_join(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_join"); 0 } -pub fn _pthread_self(_ctx: &EmEnv) -> i32 { +pub fn _pthread_self(mut _ctx: ContextMut<'_, EmEnv>) -> i32 { trace!("emscripten::_pthread_self"); 0 } -pub fn _pthread_key_create(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_key_create(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_key_create"); 0 } -pub fn _pthread_mutex_destroy(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_mutex_destroy(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_mutex_destroy"); 0 } -pub fn _pthread_mutex_init(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_mutex_init(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_mutex_init"); 0 } -pub fn _pthread_mutexattr_destroy(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_mutexattr_destroy(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_mutexattr_destroy"); 0 } -pub fn _pthread_mutexattr_init(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_mutexattr_init(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_mutexattr_init"); 0 } -pub fn _pthread_mutexattr_settype(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_mutexattr_settype(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_mutexattr_settype"); 0 } -pub fn _pthread_once(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_once(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_once"); 0 } -pub fn _pthread_rwlock_destroy(_ctx: &EmEnv, _rwlock: i32) -> i32 { +pub fn _pthread_rwlock_destroy(mut _ctx: ContextMut<'_, EmEnv>, _rwlock: i32) -> i32 { trace!("emscripten::_pthread_rwlock_destroy({})", _rwlock); 0 } -pub fn _pthread_rwlock_init(_ctx: &EmEnv, _rwlock: i32, _attr: i32) -> i32 { +pub fn _pthread_rwlock_init(mut _ctx: ContextMut<'_, EmEnv>, _rwlock: i32, _attr: i32) -> i32 { trace!("emscripten::_pthread_rwlock_init({}, {})", _rwlock, _attr); 0 } -pub fn _pthread_rwlock_rdlock(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_rwlock_rdlock(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_rwlock_rdlock"); 0 } -pub fn _pthread_rwlock_unlock(_ctx: &EmEnv, _a: i32) -> i32 { +pub fn _pthread_rwlock_unlock(mut _ctx: ContextMut<'_, EmEnv>, _a: i32) -> i32 { trace!("emscripten::_pthread_rwlock_unlock"); 0 } -pub fn _pthread_rwlock_wrlock(_ctx: &EmEnv, _rwlock: i32) -> i32 { +pub fn _pthread_rwlock_wrlock(mut _ctx: ContextMut<'_, EmEnv>, _rwlock: i32) -> i32 { trace!("emscripten::_pthread_rwlock_wrlock({})", _rwlock); 0 } -pub fn _pthread_setcancelstate(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_setcancelstate(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_setcancelstate"); 0 } -pub fn _pthread_setspecific(_ctx: &EmEnv, _a: i32, _b: i32) -> i32 { +pub fn _pthread_setspecific(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32) -> i32 { trace!("emscripten::_pthread_setspecific"); 0 } -pub fn _pthread_sigmask(_ctx: &EmEnv, _a: i32, _b: i32, _c: i32) -> i32 { +pub fn _pthread_sigmask(mut _ctx: ContextMut<'_, EmEnv>, _a: i32, _b: i32, _c: i32) -> i32 { trace!("emscripten::_pthread_sigmask"); 0 } diff --git a/lib/emscripten/src/signal.rs b/lib/emscripten/src/signal.rs index 34fd22416b2..1e0c55f9b5a 100644 --- a/lib/emscripten/src/signal.rs +++ b/lib/emscripten/src/signal.rs @@ -1,47 +1,48 @@ // use super::varargs::VarArgs; use crate::EmEnv; +use wasmer::ContextMut; #[allow(clippy::cast_ptr_alignment)] -pub fn _sigemptyset(ctx: &EmEnv, set: u32) -> i32 { +pub fn _sigemptyset(ctx: ContextMut<'_, EmEnv>, set: u32) -> i32 { debug!("emscripten::_sigemptyset"); - let set_addr = emscripten_memory_pointer!(ctx.memory(0), set) as *mut u32; + let set_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), set) as *mut u32; unsafe { *set_addr = 0; } 0 } -pub fn _sigaction(_ctx: &EmEnv, _signum: u32, _act: u32, _oldact: u32) -> i32 { +pub fn _sigaction(_ctx: ContextMut<'_, EmEnv>, _signum: u32, _act: u32, _oldact: u32) -> i32 { debug!("emscripten::_sigaction {}, {}, {}", _signum, _act, _oldact); 0 } -pub fn _siginterrupt(_ctx: &EmEnv, _a: u32, _b: u32) -> i32 { +pub fn _siginterrupt(_ctx: ContextMut<'_, EmEnv>, _a: u32, _b: u32) -> i32 { debug!("emscripten::_siginterrupt {}, {}", _a, _b); 0 } #[allow(clippy::cast_ptr_alignment)] -pub fn _sigaddset(ctx: &EmEnv, set: u32, signum: u32) -> i32 { +pub fn _sigaddset(ctx: ContextMut<'_, EmEnv>, set: u32, signum: u32) -> i32 { debug!("emscripten::_sigaddset {}, {}", set, signum); - let set_addr = emscripten_memory_pointer!(ctx.memory(0), set) as *mut u32; + let set_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), set) as *mut u32; unsafe { *set_addr |= 1 << (signum - 1); } 0 } -pub fn _sigsuspend(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _sigsuspend(_ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_sigsuspend"); -1 } -pub fn _sigprocmask(_ctx: &EmEnv, _one: i32, _two: i32, _three: i32) -> i32 { +pub fn _sigprocmask(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_sigprocmask"); 0 } -pub fn _signal(_ctx: &EmEnv, _sig: u32, _two: i32) -> i32 { +pub fn _signal(_ctx: ContextMut<'_, EmEnv>, _sig: u32, _two: i32) -> i32 { debug!("emscripten::_signal ({})", _sig); 0 } diff --git a/lib/emscripten/src/syscalls/mod.rs b/lib/emscripten/src/syscalls/mod.rs index b050b1b1f92..6b8dc0ffd47 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -48,54 +48,54 @@ use super::env; #[allow(unused_imports)] use std::io::Error; use std::slice; -use wasmer::WasmPtr; +use wasmer::{AsContextMut, ContextMut, WasmPtr}; /// exit -pub fn ___syscall1(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) { +pub fn ___syscall1(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) { debug!("emscripten::___syscall1 (exit) {}", _which); - let status: i32 = varargs.get(ctx); + let status: i32 = varargs.get(&ctx); unsafe { exit(status); } } /// read -pub fn ___syscall3(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { +pub fn ___syscall3(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { // -> ssize_t debug!("emscripten::___syscall3 (read) {}", _which); - let fd: i32 = varargs.get(ctx); - let buf: u32 = varargs.get(ctx); - let count: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let buf: u32 = varargs.get(&ctx); + let count: i32 = varargs.get(&ctx); debug!("=> fd: {}, buf_offset: {}, count: {}", fd, buf, count); - let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut c_void; + let buf_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as *mut c_void; let ret = unsafe { read(fd, buf_addr, count as _) }; debug!("=> ret: {}", ret); ret as _ } /// write -pub fn ___syscall4(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall4(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall4 (write) {}", _which); - let fd: i32 = varargs.get(ctx); - let buf: i32 = varargs.get(ctx); - let count: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let buf: i32 = varargs.get(&ctx); + let count: i32 = varargs.get(&ctx); debug!("=> fd: {}, buf: {}, count: {}", fd, buf, count); - let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *const c_void; + let buf_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as *const c_void; unsafe { write(fd, buf_addr, count as _) as i32 } } /// close -pub fn ___syscall6(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall6(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall6 (close) {}", _which); - let fd: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); debug!("fd: {}", fd); unsafe { close(fd) } } // chdir -pub fn ___syscall12(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall12(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall12 (chdir) {}", _which); - let path_ptr = varargs.get_str(ctx); + let path_ptr = varargs.get_str(&ctx); let real_path_owned = get_cstr_path(ctx, path_ptr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() @@ -111,63 +111,63 @@ pub fn ___syscall12(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { ret } -pub fn ___syscall10(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall10(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall10"); -1 } -pub fn ___syscall14(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall14(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall14"); -1 } -pub fn ___syscall15(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall15(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall15"); -1 } // getpid -pub fn ___syscall20(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall20(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall20 (getpid)"); unsafe { getpid() } } -pub fn ___syscall21(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall21(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall21"); -1 } -pub fn ___syscall25(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall25(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall25"); -1 } -pub fn ___syscall29(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall29(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall29"); -1 } -pub fn ___syscall32(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall32(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall32"); -1 } -pub fn ___syscall33(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall33(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall33"); -1 } -pub fn ___syscall36(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall36(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall36"); -1 } // rename -pub fn ___syscall38(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { +pub fn ___syscall38(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall38 (rename)"); - let old_path = varargs.get_str(ctx); - let new_path = varargs.get_str(ctx); - let real_old_path_owned = get_cstr_path(ctx, old_path as *const _); + let old_path = varargs.get_str(&ctx); + let new_path = varargs.get_str(&ctx); + let real_old_path_owned = get_cstr_path(ctx.as_context_mut(), old_path as *const _); let real_old_path = if let Some(ref rp) = real_old_path_owned { rp.as_c_str().as_ptr() } else { @@ -190,9 +190,9 @@ pub fn ___syscall38(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { } // rmdir -pub fn ___syscall40(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall40(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall40 (rmdir)"); - let pathname_addr = varargs.get_str(ctx); + let pathname_addr = varargs.get_str(&ctx); let real_path_owned = get_cstr_path(ctx, pathname_addr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() @@ -203,16 +203,16 @@ pub fn ___syscall40(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { } // pipe -pub fn ___syscall42(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall42(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall42 (pipe)"); // offset to a file descriptor, which contains a read end and write end, 2 integers - let fd_offset: u32 = varargs.get(ctx); + let fd_offset: u32 = varargs.get(&ctx); - let emscripten_memory = ctx.memory(0); + let emscripten_memory = ctx.data().memory(0); // convert the file descriptor into a vec with two slots let mut fd_vec: [c_int; 2] = WasmPtr::<[c_int; 2]>::new(fd_offset) - .deref(&emscripten_memory) + .deref(&ctx, &emscripten_memory) .read() .unwrap(); @@ -230,133 +230,133 @@ pub fn ___syscall42(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { result } -pub fn ___syscall51(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall51(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall51"); -1 } -pub fn ___syscall52(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall52(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall52"); -1 } -pub fn ___syscall53(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall53(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall53"); -1 } -pub fn ___syscall60(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall60(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall60"); -1 } // dup2 -pub fn ___syscall63(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall63(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall63 (dup2) {}", _which); - let src: i32 = varargs.get(ctx); - let dst: i32 = varargs.get(ctx); + let src: i32 = varargs.get(&ctx); + let dst: i32 = varargs.get(&ctx); unsafe { dup2(src, dst) } } // getppid -pub fn ___syscall64(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall64(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall64 (getppid)"); unsafe { getpid() } } -pub fn ___syscall66(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall66(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall66"); -1 } -pub fn ___syscall75(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall75(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall75"); -1 } -pub fn ___syscall91(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall91(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall91 - stub"); 0 } -pub fn ___syscall96(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall96(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall96"); -1 } -pub fn ___syscall97(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall97(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall97"); -1 } -pub fn ___syscall110(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall110(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall110"); -1 } -pub fn ___syscall121(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall121(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall121"); -1 } -pub fn ___syscall125(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall125(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall125"); -1 } -pub fn ___syscall133(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall133(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall133"); -1 } -pub fn ___syscall144(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall144(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall144"); -1 } -pub fn ___syscall147(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall147(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall147"); -1 } -pub fn ___syscall150(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall150(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall150"); -1 } -pub fn ___syscall151(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall151(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall151"); -1 } -pub fn ___syscall152(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall152(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall152"); -1 } -pub fn ___syscall153(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall153(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall153"); -1 } -pub fn ___syscall163(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall163(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall163"); -1 } // getcwd -pub fn ___syscall183(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { +pub fn ___syscall183(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall183"); - let buf_offset: WasmPtr = varargs.get(ctx); - let _size: c_int = varargs.get(ctx); - let path = get_current_directory(ctx); + let buf_offset: WasmPtr = varargs.get(&ctx); + let _size: c_int = varargs.get(&ctx); + let path = get_current_directory(ctx.as_context_mut()); let path_string = path.unwrap().display().to_string(); let len = path_string.len(); - let memory = ctx.memory(0); + let memory = ctx.data().memory(0); - let buf_writer = buf_offset.slice(&memory, len as u32 + 1).unwrap(); + let buf_writer = buf_offset.slice(&ctx, &memory, len as u32 + 1).unwrap(); for (i, byte) in path_string.bytes().enumerate() { buf_writer.index(i as u64).write(byte as _).unwrap(); } @@ -365,26 +365,26 @@ pub fn ___syscall183(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { } // mmap2 -pub fn ___syscall192(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall192(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall192 (mmap2) {}", _which); - let _addr: i32 = varargs.get(ctx); - let len: u32 = varargs.get(ctx); - let _prot: i32 = varargs.get(ctx); - let _flags: i32 = varargs.get(ctx); - let fd: i32 = varargs.get(ctx); - let _off: i32 = varargs.get(ctx); + let _addr: i32 = varargs.get(&ctx); + let len: u32 = varargs.get(&ctx); + let _prot: i32 = varargs.get(&ctx); + let _flags: i32 = varargs.get(&ctx); + let fd: i32 = varargs.get(&ctx); + let _off: i32 = varargs.get(&ctx); debug!( "=> addr: {}, len: {}, prot: {}, flags: {}, fd: {}, off: {}", _addr, len, _prot, _flags, fd, _off ); if fd == -1 { - let ptr = env::call_memalign(ctx, 16384, len); + let ptr = env::call_memalign(ctx.as_context_mut(), 16384, len); if ptr == 0 { // ENOMEM return -12; } - let real_ptr = emscripten_memory_pointer!(ctx.memory(0), ptr) as *const u8; + let real_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), ptr) as *const u8; env::call_memset(ctx, ptr, 0, len); for i in 0..(len as usize) { unsafe { @@ -400,19 +400,19 @@ pub fn ___syscall192(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } /// lseek -pub fn ___syscall140(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { +pub fn ___syscall140(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { // -> c_int debug!("emscripten::___syscall140 (lseek) {}", _which); - let fd: i32 = varargs.get(ctx); - let _offset_high: u32 = varargs.get(ctx); // We don't use the offset high as emscripten skips it - let offset_low: u32 = varargs.get(ctx); - let result_ptr_value: WasmPtr = varargs.get(ctx); - let whence: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let _offset_high: u32 = varargs.get(&ctx); // We don't use the offset high as emscripten skips it + let offset_low: u32 = varargs.get(&ctx); + let result_ptr_value: WasmPtr = varargs.get(&ctx); + let whence: i32 = varargs.get(&ctx); let offset = offset_low; let ret = unsafe { lseek(fd, offset as _, whence) as i64 }; - let memory = ctx.memory(0); + let memory = ctx.data().memory(0); - let result_ptr = result_ptr_value.deref(&memory); + let result_ptr = result_ptr_value.deref(&ctx, &memory); result_ptr.write(ret).unwrap(); debug!( @@ -429,13 +429,13 @@ pub fn ___syscall140(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { /// readv #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall145(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { +pub fn ___syscall145(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> i32 { // -> ssize_t debug!("emscripten::___syscall145 (readv) {}", _which); - let fd: i32 = varargs.get(ctx); - let iov: i32 = varargs.get(ctx); - let iovcnt: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let iov: i32 = varargs.get(&ctx); + let iovcnt: i32 = varargs.get(&ctx); #[repr(C)] struct GuestIovec { @@ -448,9 +448,11 @@ pub fn ___syscall145(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { unsafe { for i in 0..iovcnt { let guest_iov_addr = - emscripten_memory_pointer!(ctx.memory(0), (iov + i * 8)) as *mut GuestIovec; - let iov_base = emscripten_memory_pointer!(ctx.memory(0), (*guest_iov_addr).iov_base) - as *mut c_void; + emscripten_memory_pointer!(ctx, ctx.data().memory(0), (iov + i * 8)) + as *mut GuestIovec; + let iov_base = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), (*guest_iov_addr).iov_base) + as *mut c_void; let iov_len = (*guest_iov_addr).iov_len as _; // debug!("=> iov_addr: {:?}, {:?}", iov_base, iov_len); let curr = read(fd, iov_base, iov_len); @@ -466,12 +468,12 @@ pub fn ___syscall145(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { // writev #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall146(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { +pub fn ___syscall146(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { // -> ssize_t debug!("emscripten::___syscall146 (writev) {}", _which); - let fd: i32 = varargs.get(ctx); - let iov: i32 = varargs.get(ctx); - let iovcnt: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let iov: i32 = varargs.get(&ctx); + let iovcnt: i32 = varargs.get(&ctx); #[repr(C)] struct GuestIovec { @@ -484,9 +486,11 @@ pub fn ___syscall146(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { for i in 0..iovcnt { unsafe { let guest_iov_addr = - emscripten_memory_pointer!(ctx.memory(0), (iov + i * 8)) as *mut GuestIovec; - let iov_base = emscripten_memory_pointer!(ctx.memory(0), (*guest_iov_addr).iov_base) - as *const c_void; + emscripten_memory_pointer!(ctx, ctx.data().memory(0), (iov + i * 8)) + as *mut GuestIovec; + let iov_base = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), (*guest_iov_addr).iov_base) + as *const c_void; let iov_len = (*guest_iov_addr).iov_len as _; // debug!("=> iov_addr: {:?}, {:?}", iov_base, iov_len); let curr = write(fd, iov_base, iov_len); @@ -507,14 +511,14 @@ pub fn ___syscall146(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { ret as _ } -pub fn ___syscall191(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { - let _resource: i32 = varargs.get(ctx); +pub fn ___syscall191(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { + let _resource: i32 = varargs.get(&ctx); debug!( "emscripten::___syscall191 - mostly stub, resource: {}", _resource ); - let rlim_emptr: i32 = varargs.get(ctx); - let rlim_ptr = emscripten_memory_pointer!(ctx.memory(0), rlim_emptr) as *mut u8; + let rlim_emptr: i32 = varargs.get(&ctx); + let rlim_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), rlim_emptr) as *mut u8; let rlim = unsafe { slice::from_raw_parts_mut(rlim_ptr, 16) }; // set all to RLIM_INIFINTY @@ -524,18 +528,18 @@ pub fn ___syscall191(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { 0 } -pub fn ___syscall193(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall193(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall193"); -1 } // stat64 -pub fn ___syscall195(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall195(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall195 (stat64) {}", _which); - let pathname_addr = varargs.get_str(ctx); - let buf: u32 = varargs.get(ctx); + let pathname_addr = varargs.get_str(&ctx); + let buf: u32 = varargs.get(&ctx); - let real_path_owned = get_cstr_path(ctx, pathname_addr as *const _); + let real_path_owned = get_cstr_path(ctx.as_context_mut(), pathname_addr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { @@ -561,11 +565,11 @@ pub fn ___syscall195(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } // fstat64 -pub fn ___syscall197(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall197(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall197 (fstat64) {}", _which); - let fd: c_int = varargs.get(ctx); - let buf: u32 = varargs.get(ctx); + let fd: c_int = varargs.get(&ctx); + let buf: u32 = varargs.get(&ctx); unsafe { let mut stat = std::mem::zeroed(); @@ -580,135 +584,135 @@ pub fn ___syscall197(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 0 } -pub fn ___syscall209(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall209(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall209"); -1 } -pub fn ___syscall211(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall211(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall211"); -1 } -pub fn ___syscall218(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall218(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall218"); -1 } -pub fn ___syscall268(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall268(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall268"); -1 } -pub fn ___syscall269(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall269(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall269"); -1 } -pub fn ___syscall272(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall272(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall272"); -1 } -pub fn ___syscall295(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall295(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall295"); -1 } -pub fn ___syscall296(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall296(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall296"); -1 } -pub fn ___syscall297(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall297(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall297"); -1 } -pub fn ___syscall298(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall298(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall298"); -1 } -pub fn ___syscall300(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall300(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall300"); -1 } -pub fn ___syscall301(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall301(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall301"); -1 } -pub fn ___syscall302(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall302(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall302"); -1 } -pub fn ___syscall303(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall303(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall303"); -1 } -pub fn ___syscall304(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall304(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall304"); -1 } -pub fn ___syscall305(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall305(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall305"); -1 } -pub fn ___syscall306(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall306(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall306"); -1 } -pub fn ___syscall307(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall307(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall307"); -1 } -pub fn ___syscall308(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall308(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall308"); -1 } // utimensat -pub fn ___syscall320(_ctx: &EmEnv, _which: c_int, mut _varargs: VarArgs) -> c_int { +pub fn ___syscall320(_ctx: ContextMut<'_, EmEnv>, _which: c_int, mut _varargs: VarArgs) -> c_int { debug!("emscripten::___syscall320 (utimensat), {}", _which); 0 } -pub fn ___syscall331(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall331(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall331"); -1 } -pub fn ___syscall333(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall333(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall333"); -1 } -pub fn ___syscall334(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall334(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall334"); -1 } -pub fn ___syscall337(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall337(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall337"); -1 } // prlimit64 -pub fn ___syscall340(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall340(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall340 (prlimit64), {}", _which); // NOTE: Doesn't really matter. Wasm modules cannot exceed WASM_PAGE_SIZE anyway. - let _pid: i32 = varargs.get(ctx); - let resource: i32 = varargs.get(ctx); - let _new_limit: u32 = varargs.get(ctx); - let old_limit: u32 = varargs.get(ctx); + let _pid: i32 = varargs.get(&ctx); + let resource: i32 = varargs.get(&ctx); + let _new_limit: u32 = varargs.get(&ctx); + let old_limit: u32 = varargs.get(&ctx); let val = match resource { // RLIMIT_NOFILE @@ -718,7 +722,7 @@ pub fn ___syscall340(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int if old_limit != 0 { // just report no limits - let buf_ptr = emscripten_memory_pointer!(ctx.memory(0), old_limit) as *mut u8; + let buf_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), old_limit) as *mut u8; let buf = unsafe { slice::from_raw_parts_mut(buf_ptr, 16) }; LittleEndian::write_i64(&mut *buf, val); @@ -728,7 +732,7 @@ pub fn ___syscall340(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 0 } -pub fn ___syscall345(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall345(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall345"); -1 } diff --git a/lib/emscripten/src/syscalls/unix.rs b/lib/emscripten/src/syscalls/unix.rs index c0c1855e12d..0808ddae4e7 100644 --- a/lib/emscripten/src/syscalls/unix.rs +++ b/lib/emscripten/src/syscalls/unix.rs @@ -81,7 +81,7 @@ use libc::{ // TCGETS, // TCSETSW, }; -use wasmer::{ValueType, WasmPtr}; +use wasmer::{AsContextMut, ContextMut, ValueType, WasmPtr}; // They are not exposed in in Rust libc in macOS const TCGETS: u64 = 0x5401; @@ -157,12 +157,12 @@ use libc::SO_NOSIGPIPE; const SO_NOSIGPIPE: c_int = 0; /// open -pub fn ___syscall5(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall5(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall5 (open) {}", _which); - let pathname_addr = varargs.get_str(ctx); - let flags: i32 = varargs.get(ctx); - let mode: u32 = varargs.get(ctx); - let real_path_owned = utils::get_cstr_path(ctx, pathname_addr as *const _); + let pathname_addr = varargs.get_str(&ctx); + let flags: i32 = varargs.get(&ctx); + let mode: u32 = varargs.get(&ctx); + let real_path_owned = utils::get_cstr_path(ctx.as_context_mut(), pathname_addr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { @@ -181,11 +181,11 @@ pub fn ___syscall5(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { } /// link -pub fn ___syscall9(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall9(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall9 (link) {}", _which); - let oldname_ptr = varargs.get_str(ctx); - let newname_ptr = varargs.get_str(ctx); + let oldname_ptr = varargs.get_str(&ctx); + let newname_ptr = varargs.get_str(&ctx); let result = unsafe { link(oldname_ptr, newname_ptr) }; debug!( "=> oldname: {}, newname: {}, result: {}", @@ -197,30 +197,30 @@ pub fn ___syscall9(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { } /// getrusage -pub fn ___syscall77(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall77(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall77 (getrusage) {}", _which); - let resource: c_int = varargs.get(ctx); - let rusage_ptr: c_int = varargs.get(ctx); + let resource: c_int = varargs.get(&ctx); + let rusage_ptr: c_int = varargs.get(&ctx); #[allow(clippy::cast_ptr_alignment)] - let rusage = emscripten_memory_pointer!(ctx.memory(0), rusage_ptr) as *mut rusage; + let rusage = emscripten_memory_pointer!(ctx, ctx.data().memory(0), rusage_ptr) as *mut rusage; assert_eq!(8, mem::align_of_val(&rusage)); unsafe { getrusage(resource, rusage) } } /// symlink -pub fn ___syscall83(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall83(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall83 (symlink) {}", _which); - let path1 = varargs.get_str(ctx); - let path2 = varargs.get_str(ctx); - let real_path1_owned = utils::get_cstr_path(ctx, path1 as *const _); + let path1 = varargs.get_str(&ctx); + let path2 = varargs.get_str(&ctx); + let real_path1_owned = utils::get_cstr_path(ctx.as_context_mut(), path1 as *const _); let real_path1 = if let Some(ref rp) = real_path1_owned { rp.as_c_str().as_ptr() } else { path1 }; - let real_path2_owned = utils::get_cstr_path(ctx, path2 as *const _); + let real_path2_owned = utils::get_cstr_path(ctx.as_context_mut(), path2 as *const _); let real_path2 = if let Some(ref rp) = real_path2_owned { rp.as_c_str().as_ptr() } else { @@ -237,13 +237,13 @@ pub fn ___syscall83(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { } /// readlink -pub fn ___syscall85(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { +pub fn ___syscall85(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall85 (readlink)"); - let pathname_addr = varargs.get_str(ctx); - let buf = varargs.get_str(ctx); - // let buf_addr: i32 = varargs.get(ctx); - let buf_size: i32 = varargs.get(ctx); - let real_path_owned = get_cstr_path(ctx, pathname_addr as *const _); + let pathname_addr = varargs.get_str(&ctx); + let buf = varargs.get_str(&ctx); + // let buf_addr: i32 = varargs.get(&ctx); + let buf_size: i32 = varargs.get(&ctx); + let real_path_owned = get_cstr_path(ctx.as_context_mut(), pathname_addr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { @@ -266,10 +266,10 @@ pub fn ___syscall85(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> i32 { } /// ftruncate64 -pub fn ___syscall194(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall194(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall194 (ftruncate64) {}", _which); - let _fd: c_int = varargs.get(ctx); - let _length: i64 = varargs.get(ctx); + let _fd: c_int = varargs.get(&ctx); + let _length: i64 = varargs.get(&ctx); #[cfg(not(any(target_os = "freebsd", target_vendor = "apple")))] unsafe { ftruncate64(_fd, _length) @@ -283,17 +283,17 @@ pub fn ___syscall194(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } /// lchown -pub fn ___syscall198(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall198(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall198 (lchown) {}", _which); - let path_ptr = varargs.get_str(ctx); - let real_path_owned = utils::get_cstr_path(ctx, path_ptr as *const _); + let path_ptr = varargs.get_str(&ctx); + let real_path_owned = utils::get_cstr_path(ctx.as_context_mut(), path_ptr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { path_ptr }; - let uid: uid_t = varargs.get(ctx); - let gid: gid_t = varargs.get(ctx); + let uid: uid_t = varargs.get(&ctx); + let gid: gid_t = varargs.get(&ctx); let result = unsafe { lchown(real_path, uid, gid) }; debug!( "=> path: {}, uid: {}, gid: {}, result: {}", @@ -306,13 +306,13 @@ pub fn ___syscall198(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } /// getgroups -pub fn ___syscall205(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall205(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall205 (getgroups) {}", _which); - let ngroups_max: c_int = varargs.get(ctx); - let groups: c_int = varargs.get(ctx); + let ngroups_max: c_int = varargs.get(&ctx); + let groups: c_int = varargs.get(&ctx); #[allow(clippy::cast_ptr_alignment)] - let gid_ptr = emscripten_memory_pointer!(ctx.memory(0), groups) as *mut gid_t; + let gid_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), groups) as *mut gid_t; assert_eq!(4, mem::align_of_val(&gid_ptr)); let result = unsafe { getgroups(ngroups_max, gid_ptr) }; debug!( @@ -323,46 +323,46 @@ pub fn ___syscall205(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } // chown -pub fn ___syscall212(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall212(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall212 (chown) {}", _which); - let pathname_addr = varargs.get_str(ctx); - let real_path_owned = utils::get_cstr_path(ctx, pathname_addr as *const _); + let pathname_addr = varargs.get_str(&ctx); + let real_path_owned = utils::get_cstr_path(ctx.as_context_mut(), pathname_addr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { pathname_addr }; - let owner: u32 = varargs.get(ctx); - let group: u32 = varargs.get(ctx); + let owner: u32 = varargs.get(&ctx); + let group: u32 = varargs.get(&ctx); unsafe { chown(real_path, owner, group) } } /// madvise -pub fn ___syscall219(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall219(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall212 (chown) {}", _which); - let addr_ptr: c_int = varargs.get(ctx); - let len: usize = varargs.get(ctx); - let advice: c_int = varargs.get(ctx); + let addr_ptr: c_int = varargs.get(&ctx); + let len: usize = varargs.get(&ctx); + let advice: c_int = varargs.get(&ctx); - let addr = emscripten_memory_pointer!(ctx.memory(0), addr_ptr) as *mut c_void; + let addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), addr_ptr) as *mut c_void; unsafe { madvise(addr, len, advice) } } /// access -pub fn ___syscall33(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall33(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall33 (access) {}", _which); - let path = varargs.get_str(ctx); - let real_path_owned = utils::get_cstr_path(ctx, path as *const _); + let path = varargs.get_str(&ctx); + let real_path_owned = utils::get_cstr_path(ctx.as_context_mut(), path as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { path }; - let amode: c_int = varargs.get(ctx); + let amode: c_int = varargs.get(&ctx); let result = unsafe { access(real_path, amode) }; debug!( "=> path: {}, amode: {}, result: {}", @@ -374,41 +374,41 @@ pub fn ___syscall33(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { } /// nice -pub fn ___syscall34(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall34(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall34 (nice) {}", _which); - let inc_r: c_int = varargs.get(ctx); + let inc_r: c_int = varargs.get(&ctx); unsafe { nice(inc_r) } } // mkdir -pub fn ___syscall39(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall39(mut ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall39 (mkdir) {}", _which); - let pathname_addr = varargs.get_str(ctx); - let real_path_owned = utils::get_cstr_path(ctx, pathname_addr as *const _); + let pathname_addr = varargs.get_str(&ctx); + let real_path_owned = utils::get_cstr_path(ctx.as_context_mut(), pathname_addr as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { pathname_addr }; - let mode: u32 = varargs.get(ctx); + let mode: u32 = varargs.get(&ctx); unsafe { mkdir(real_path, mode as _) } } /// dup -pub fn ___syscall41(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall41(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall41 (dup) {}", _which); - let fd: c_int = varargs.get(ctx); + let fd: c_int = varargs.get(&ctx); unsafe { dup(fd) } } /// getgid32 -pub fn ___syscall200(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall200(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall200 (getgid32)"); unsafe { getgid() as i32 } } // geteuid32 -pub fn ___syscall201(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall201(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall201 (geteuid32)"); unsafe { // Maybe fix: Emscripten returns 0 always @@ -417,7 +417,7 @@ pub fn ___syscall201(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { } // getegid32 -pub fn ___syscall202(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall202(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { // gid_t debug!("emscripten::___syscall202 (getegid32)"); unsafe { @@ -427,21 +427,21 @@ pub fn ___syscall202(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { } /// fchown -pub fn ___syscall207(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall207(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall207 (fchown) {}", _which); - let fd: c_int = varargs.get(ctx); - let owner: uid_t = varargs.get(ctx); - let group: gid_t = varargs.get(ctx); + let fd: c_int = varargs.get(&ctx); + let owner: uid_t = varargs.get(&ctx); + let group: gid_t = varargs.get(&ctx); unsafe { fchown(fd, owner, group) } } /// dup3 -pub fn ___syscall330(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> pid_t { +pub fn ___syscall330(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> pid_t { // Implementation based on description at https://linux.die.net/man/2/dup3 debug!("emscripten::___syscall330 (dup3)"); - let oldfd: c_int = varargs.get(ctx); - let newfd: c_int = varargs.get(ctx); - let flags: c_int = varargs.get(ctx); + let oldfd: c_int = varargs.get(&ctx); + let newfd: c_int = varargs.get(&ctx); + let flags: c_int = varargs.get(&ctx); if oldfd == newfd { return EINVAL; @@ -470,19 +470,20 @@ pub fn ___syscall330(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> pid_t } /// ioctl -pub fn ___syscall54(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall54(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall54 (ioctl) {}", _which); - let fd: i32 = varargs.get(ctx); - let request: u32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let request: u32 = varargs.get(&ctx); debug!("=> fd: {}, op: {}", fd, request); // Got the equivalents here: https://code.woboq.org/linux/linux/include/uapi/asm-generic/ioctls.h.html match request { WASM_FIOCLEX | WASM_FIONBIO | WASM_TIOCGWINSZ | WASM_TIOCSPGRP | WASM_TCGETS | WASM_TCSETSW => { - let argp: u32 = varargs.get(ctx); - let argp_ptr = emscripten_memory_pointer!(ctx.memory(0), argp) as *mut c_void; + let argp: u32 = varargs.get(&ctx); + let argp_ptr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), argp) as *mut c_void; let translated_request = translate_ioctl(request); let ret = unsafe { ioctl(fd, translated_request as _, argp_ptr) }; debug!( @@ -512,11 +513,11 @@ const SOCK_CLOEXC: i32 = 0x80000; // socketcall #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall102(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall102 (socketcall) {}", _which); - let call: u32 = varargs.get(ctx); - let mut socket_varargs: VarArgs = varargs.get(ctx); - let memory = ctx.memory(0); + let call: u32 = varargs.get(&ctx); + let mut socket_varargs: VarArgs = varargs.get(&ctx); + let memory = ctx.data().memory(0); // migrating to EmSockAddr, port being separate here is nice, should update that too #[repr(C)] @@ -537,9 +538,9 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 1 => { debug!("socket: socket"); // socket (domain: c_int, ty: c_int, protocol: c_int) -> c_int - let domain: i32 = socket_varargs.get(ctx); - let ty_and_flags: i32 = socket_varargs.get(ctx); - let protocol: i32 = socket_varargs.get(ctx); + let domain: i32 = socket_varargs.get(&ctx); + let ty_and_flags: i32 = socket_varargs.get(&ctx); + let protocol: i32 = socket_varargs.get(&ctx); let ty = ty_and_flags & (!SOCK_NON_BLOCK) & (!SOCK_CLOEXC); let fd = unsafe { socket(domain, ty, protocol) }; @@ -578,10 +579,10 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int debug!("socket: bind"); // bind (socket: c_int, address: *const sockaddr, address_len: socklen_t) -> c_int // TODO: Emscripten has a different signature. - let socket = socket_varargs.get(ctx); - let address: u32 = socket_varargs.get(ctx); - let address_len = socket_varargs.get(ctx); - let address = emscripten_memory_pointer!(&memory, address) as *mut sockaddr; + let socket = socket_varargs.get(&ctx); + let address: u32 = socket_varargs.get(&ctx); + let address_len = socket_varargs.get(&ctx); + let address = emscripten_memory_pointer!(ctx, &memory, address) as *mut sockaddr; // Debug received address let _proper_address = address as *const GuestSockaddrIn; @@ -603,17 +604,17 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int debug!("socket: connect"); // connect (socket: c_int, address: *const sockaddr, len: socklen_t) -> c_int // TODO: Emscripten has a different signature. - let socket = socket_varargs.get(ctx); - let address: u32 = socket_varargs.get(ctx); - let address_len = socket_varargs.get(ctx); - let address = emscripten_memory_pointer!(&memory, address) as *mut sockaddr; + let socket = socket_varargs.get(&ctx); + let address: u32 = socket_varargs.get(&ctx); + let address_len = socket_varargs.get(&ctx); + let address = emscripten_memory_pointer!(ctx, &memory, address) as *mut sockaddr; unsafe { connect(socket, address, address_len) } } 4 => { debug!("socket: listen"); // listen (socket: c_int, backlog: c_int) -> c_int - let socket = socket_varargs.get(ctx); - let backlog: i32 = socket_varargs.get(ctx); + let socket = socket_varargs.get(&ctx); + let backlog: i32 = socket_varargs.get(&ctx); let status = unsafe { listen(socket, backlog) }; debug!( "=> socketfd: {}, backlog: {} = status: {}", @@ -624,17 +625,17 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 5 => { debug!("socket: accept"); // accept (socket: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int - let socket: i32 = socket_varargs.get(ctx); - let address: WasmPtr = socket_varargs.get(ctx); - let address_len: WasmPtr = socket_varargs.get(ctx); + let socket: i32 = socket_varargs.get(&ctx); + let address: WasmPtr = socket_varargs.get(&ctx); + let address_len: WasmPtr = socket_varargs.get(&ctx); debug!( "=> socket: {}, address: {:?}, address_len: {}", socket, - address.deref(&memory).read().unwrap(), - address_len.deref(&memory).read().unwrap() + address.deref(&ctx, &memory).read().unwrap(), + address_len.deref(&ctx, &memory).read().unwrap() ); - let mut address_len_addr = address_len.deref(&memory).read().unwrap(); + let mut address_len_addr = address_len.deref(&ctx, &memory).read().unwrap(); // let mut address_len_addr: socklen_t = 0; let mut host_address: sockaddr = sockaddr { @@ -644,7 +645,7 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int sa_len: Default::default(), }; let fd = unsafe { accept(socket, &mut host_address, &mut address_len_addr) }; - let mut address_addr = address.deref(&memory).read().unwrap(); + let mut address_addr = address.deref(&ctx, &memory).read().unwrap(); address_addr.sa_family = host_address.sa_family as _; address_addr.sa_data = host_address.sa_data; @@ -665,10 +666,10 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 6 => { debug!("socket: getsockname"); // getsockname (socket: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int - let socket: i32 = socket_varargs.get(ctx); - let address: WasmPtr = socket_varargs.get(ctx); - let address_len: WasmPtr = socket_varargs.get(ctx); - let address_len_addr = address_len.deref(&memory).read().unwrap(); + let socket: i32 = socket_varargs.get(&ctx); + let address: WasmPtr = socket_varargs.get(&ctx); + let address_len: WasmPtr = socket_varargs.get(&ctx); + let address_len_addr = address_len.deref(&ctx, &memory).read().unwrap(); let mut sock_addr_host: sockaddr = sockaddr { sa_family: Default::default(), @@ -684,7 +685,7 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int ) }; // translate from host data into emscripten data - let mut address_mut = address.deref(&memory).read().unwrap(); + let mut address_mut = address.deref(&ctx, &memory).read().unwrap(); address_mut.sa_family = sock_addr_host.sa_family as _; address_mut.sa_data = sock_addr_host.sa_data; @@ -698,40 +699,40 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 7 => { debug!("socket: getpeername"); // getpeername (socket: c_int, address: *mut sockaddr, address_len: *mut socklen_t) -> c_int - let socket = socket_varargs.get(ctx); - let address: u32 = socket_varargs.get(ctx); - let address_len: u32 = socket_varargs.get(ctx); - let address = emscripten_memory_pointer!(memory, address) as *mut sockaddr; + let socket = socket_varargs.get(&ctx); + let address: u32 = socket_varargs.get(&ctx); + let address_len: u32 = socket_varargs.get(&ctx); + let address = emscripten_memory_pointer!(ctx, memory, address) as *mut sockaddr; let address_len_addr = - emscripten_memory_pointer!(memory, address_len) as *mut socklen_t; + emscripten_memory_pointer!(ctx, memory, address_len) as *mut socklen_t; unsafe { getpeername(socket, address, address_len_addr) } } 11 => { debug!("socket: sendto"); // sendto (socket: c_int, buf: *const c_void, len: size_t, flags: c_int, addr: *const sockaddr, addrlen: socklen_t) -> ssize_t - let socket = socket_varargs.get(ctx); - let buf: u32 = socket_varargs.get(ctx); - let flags = socket_varargs.get(ctx); - let len: i32 = socket_varargs.get(ctx); - let address: u32 = socket_varargs.get(ctx); - let address_len = socket_varargs.get(ctx); - let buf_addr = emscripten_memory_pointer!(memory, buf) as _; - let address = emscripten_memory_pointer!(memory, address) as *mut sockaddr; + let socket = socket_varargs.get(&ctx); + let buf: u32 = socket_varargs.get(&ctx); + let flags = socket_varargs.get(&ctx); + let len: i32 = socket_varargs.get(&ctx); + let address: u32 = socket_varargs.get(&ctx); + let address_len = socket_varargs.get(&ctx); + let buf_addr = emscripten_memory_pointer!(ctx, memory, buf) as _; + let address = emscripten_memory_pointer!(ctx, memory, address) as *mut sockaddr; unsafe { sendto(socket, buf_addr, flags, len, address, address_len) as i32 } } 12 => { debug!("socket: recvfrom"); // recvfrom (socket: c_int, buf: *const c_void, len: size_t, flags: c_int, addr: *const sockaddr, addrlen: socklen_t) -> ssize_t - let socket = socket_varargs.get(ctx); - let buf: u32 = socket_varargs.get(ctx); - let len: i32 = socket_varargs.get(ctx); - let flags: i32 = socket_varargs.get(ctx); - let address: u32 = socket_varargs.get(ctx); - let address_len: u32 = socket_varargs.get(ctx); - let buf_addr = emscripten_memory_pointer!(memory, buf) as _; - let address = emscripten_memory_pointer!(memory, address) as *mut sockaddr; + let socket = socket_varargs.get(&ctx); + let buf: u32 = socket_varargs.get(&ctx); + let len: i32 = socket_varargs.get(&ctx); + let flags: i32 = socket_varargs.get(&ctx); + let address: u32 = socket_varargs.get(&ctx); + let address_len: u32 = socket_varargs.get(&ctx); + let buf_addr = emscripten_memory_pointer!(ctx, memory, buf) as _; + let address = emscripten_memory_pointer!(ctx, memory, address) as *mut sockaddr; let address_len_addr = - emscripten_memory_pointer!(memory, address_len) as *mut socklen_t; + emscripten_memory_pointer!(ctx, memory, address_len) as *mut socklen_t; unsafe { recvfrom( socket, @@ -749,13 +750,13 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int // 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 = socket_varargs.get(ctx); - let level: i32 = socket_varargs.get(ctx); + let socket = socket_varargs.get(&ctx); + let level: i32 = socket_varargs.get(&ctx); let level = if level == 1 { SOL_SOCKET } else { level }; - let untranslated_name: i32 = socket_varargs.get(ctx); - let value: u32 = socket_varargs.get(ctx); - let option_len: u32 = socket_varargs.get(ctx); - let value_addr = emscripten_memory_pointer!(memory, value) as *const libc::c_void; + let untranslated_name: i32 = socket_varargs.get(&ctx); + let value: u32 = socket_varargs.get(&ctx); + let option_len: u32 = socket_varargs.get(&ctx); + let value_addr = emscripten_memory_pointer!(ctx, memory, value) as *const libc::c_void; let name: i32 = translate_socket_name_flag(untranslated_name); let ret = unsafe { setsockopt(socket, level, name, value_addr, option_len) }; @@ -766,33 +767,34 @@ pub fn ___syscall102(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int 15 => { debug!("socket: getsockopt"); // getsockopt (sockfd: c_int, level: c_int, optname: c_int, optval: *mut c_void, optlen: *mut socklen_t) -> c_int - let socket = socket_varargs.get(ctx); - let level: i32 = socket_varargs.get(ctx); + let socket = socket_varargs.get(&ctx); + let level: i32 = socket_varargs.get(&ctx); let level = if level == 1 { SOL_SOCKET } else { level }; - let untranslated_name: i32 = socket_varargs.get(ctx); + let untranslated_name: i32 = socket_varargs.get(&ctx); let name: i32 = translate_socket_name_flag(untranslated_name); - let value: u32 = socket_varargs.get(ctx); - let option_len: u32 = socket_varargs.get(ctx); - let value_addr = emscripten_memory_pointer!(memory, value) as _; - let option_len_addr = emscripten_memory_pointer!(memory, option_len) as *mut socklen_t; + let value: u32 = socket_varargs.get(&ctx); + let option_len: u32 = socket_varargs.get(&ctx); + let value_addr = emscripten_memory_pointer!(ctx, memory, value) as _; + let option_len_addr = + emscripten_memory_pointer!(ctx, memory, option_len) as *mut socklen_t; unsafe { getsockopt(socket, level, name, value_addr, option_len_addr) } } 16 => { debug!("socket: sendmsg"); // sendmsg (fd: c_int, msg: *const msghdr, flags: c_int) -> ssize_t - let socket: i32 = socket_varargs.get(ctx); - let msg: u32 = socket_varargs.get(ctx); - let flags: i32 = socket_varargs.get(ctx); - let msg_addr = emscripten_memory_pointer!(memory, msg) as *const msghdr; + let socket: i32 = socket_varargs.get(&ctx); + let msg: u32 = socket_varargs.get(&ctx); + let flags: i32 = socket_varargs.get(&ctx); + let msg_addr = emscripten_memory_pointer!(ctx, memory, msg) as *const msghdr; unsafe { sendmsg(socket, msg_addr, flags) as i32 } } 17 => { debug!("socket: recvmsg"); // recvmsg (fd: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t - let socket: i32 = socket_varargs.get(ctx); - let msg: u32 = socket_varargs.get(ctx); - let flags: i32 = socket_varargs.get(ctx); - let msg_addr = emscripten_memory_pointer!(memory, msg) as *mut msghdr; + let socket: i32 = socket_varargs.get(&ctx); + let msg: u32 = socket_varargs.get(&ctx); + let flags: i32 = socket_varargs.get(&ctx); + let msg_addr = emscripten_memory_pointer!(ctx, memory, msg) as *mut msghdr; unsafe { recvmsg(socket, msg_addr, flags) as i32 } } _ => { @@ -827,10 +829,10 @@ fn translate_socket_name_flag(name: i32) -> i32 { } /// getpgid -pub fn ___syscall132(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall132(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall132 (getpgid)"); - let pid: pid_t = varargs.get(ctx); + let pid: pid_t = varargs.get(&ctx); let ret = unsafe { getpgid(pid) }; debug!("=> pid: {} = {}", pid, ret); @@ -849,14 +851,14 @@ pub struct EmPollFd { } /// poll -pub fn ___syscall168(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { +pub fn ___syscall168(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall168(poll)"); - let fds: WasmPtr = varargs.get(ctx); - let nfds: u32 = varargs.get(ctx); - let timeout: i32 = varargs.get(ctx); - let memory = ctx.memory(0); + let fds: WasmPtr = varargs.get(&ctx); + let nfds: u32 = varargs.get(&ctx); + let timeout: i32 = varargs.get(&ctx); + let memory = ctx.data().memory(0); - let mut fds_mut = fds.deref(&memory).read().unwrap(); + let mut fds_mut = fds.deref(&ctx, &memory).read().unwrap(); unsafe { libc::poll( @@ -868,35 +870,35 @@ pub fn ___syscall168(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { } // pread -pub fn ___syscall180(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall180(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall180 (pread) {}", _which); - let fd: i32 = varargs.get(ctx); - let buf: u32 = varargs.get(ctx); - let count: u32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let buf: u32 = varargs.get(&ctx); + let count: u32 = varargs.get(&ctx); { - let zero: u32 = varargs.get(ctx); + let zero: u32 = varargs.get(&ctx); assert_eq!(zero, 0); } - let offset: i64 = varargs.get(ctx); + let offset: i64 = varargs.get(&ctx); - let buf_ptr = emscripten_memory_pointer!(ctx.memory(0), buf) as _; + let buf_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as _; unsafe { pread(fd, buf_ptr, count as _, offset) as _ } } // pwrite -pub fn ___syscall181(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall181(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall181 (pwrite) {}", _which); - let fd: i32 = varargs.get(ctx); - let buf: u32 = varargs.get(ctx); - let count: u32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let buf: u32 = varargs.get(&ctx); + let count: u32 = varargs.get(&ctx); { - let zero: u32 = varargs.get(ctx); + let zero: u32 = varargs.get(&ctx); assert_eq!(zero, 0); } - let offset: i64 = varargs.get(ctx); + let offset: i64 = varargs.get(&ctx); - let buf_ptr = emscripten_memory_pointer!(ctx.memory(0), buf) as _; + let buf_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as _; let status = unsafe { pwrite(fd, buf_ptr, count as _, offset) as _ }; debug!( "=> fd: {}, buf: {}, count: {}, offset: {} = status:{}", @@ -906,24 +908,24 @@ pub fn ___syscall181(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } /// fchmod -pub fn ___syscall94(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall94(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall118 (fchmod) {}", _which); - let fd: c_int = varargs.get(ctx); - let mode: mode_t = varargs.get(ctx); + let fd: c_int = varargs.get(&ctx); + let mode: mode_t = varargs.get(&ctx); unsafe { fchmod(fd, mode) } } /// wait4 #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall114(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> pid_t { +pub fn ___syscall114(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> pid_t { debug!("emscripten::___syscall114 (wait4)"); - let pid: pid_t = varargs.get(ctx); - let status: u32 = varargs.get(ctx); - let options: c_int = varargs.get(ctx); - let rusage: u32 = varargs.get(ctx); - let status_addr = emscripten_memory_pointer!(ctx.memory(0), status) as *mut c_int; + let pid: pid_t = varargs.get(&ctx); + let status: u32 = varargs.get(&ctx); + let options: c_int = varargs.get(&ctx); + let rusage: u32 = varargs.get(&ctx); + let status_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), status) as *mut c_int; - let rusage_addr = emscripten_memory_pointer!(ctx.memory(0), rusage) as *mut rusage; + let rusage_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), rusage) as *mut rusage; let res = unsafe { wait4(pid, status_addr, options, rusage_addr) }; debug!( "=> pid: {}, status: {:?}, options: {}, rusage: {:?} = pid: {}", @@ -933,22 +935,22 @@ pub fn ___syscall114(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> pid_t } /// fsync -pub fn ___syscall118(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall118(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall118 (fsync) {}", _which); - let fd: c_int = varargs.get(ctx); + let fd: c_int = varargs.get(&ctx); unsafe { fsync(fd) } } // select #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall142(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall142(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall142 (newselect) {}", _which); - let nfds: i32 = varargs.get(ctx); - let readfds: u32 = varargs.get(ctx); - let writefds: u32 = varargs.get(ctx); - let exceptfds: u32 = varargs.get(ctx); - let _timeout: i32 = varargs.get(ctx); + let nfds: i32 = varargs.get(&ctx); + let readfds: u32 = varargs.get(&ctx); + let writefds: u32 = varargs.get(&ctx); + let exceptfds: u32 = varargs.get(&ctx); + let _timeout: i32 = varargs.get(&ctx); if nfds > 1024 { // EINVAL @@ -956,27 +958,27 @@ pub fn ___syscall142(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } assert!(exceptfds == 0, "`exceptfds` is not supporrted"); - let readfds_ptr = emscripten_memory_pointer!(ctx.memory(0), readfds) as _; - let writefds_ptr = emscripten_memory_pointer!(ctx.memory(0), writefds) as _; + let readfds_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), readfds) as _; + let writefds_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), writefds) as _; unsafe { select(nfds, readfds_ptr, writefds_ptr, 0 as _, 0 as _) } } /// fdatasync -pub fn ___syscall148(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall148(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall148 (fdatasync) {}", _which); - let fd: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); unsafe { fdatasync(fd) } } // setpgid -pub fn ___syscall57(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall57(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall57 (setpgid) {}", _which); - let pid: i32 = varargs.get(ctx); - let pgid: i32 = varargs.get(ctx); + let pid: i32 = varargs.get(&ctx); + let pgid: i32 = varargs.get(&ctx); let ret = unsafe { setpgid(pid, pgid) }; debug!("=> pid: {}, pgid: {} = {}", pid, pgid, ret); @@ -988,25 +990,25 @@ pub fn ___syscall57(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { /// uname // NOTE: Wondering if we should return custom utsname, like Emscripten. -pub fn ___syscall122(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall122(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall122 (uname) {}", _which); - let buf: u32 = varargs.get(ctx); + let buf: u32 = varargs.get(&ctx); debug!("=> buf: {}", buf); - let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut utsname; + let buf_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as *mut utsname; unsafe { uname(buf_addr) } } /// lstat64 -pub fn ___syscall196(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { +pub fn ___syscall196(mut ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { debug!("emscripten::___syscall196 (lstat64) {}", _which); - let path = varargs.get_str(ctx); - let real_path_owned = utils::get_cstr_path(ctx, path as *const _); + let path = varargs.get_str(&ctx); + let real_path_owned = utils::get_cstr_path(ctx.as_context_mut(), path as *const _); let real_path = if let Some(ref rp) = real_path_owned { rp.as_c_str().as_ptr() } else { path }; - let buf_ptr: u32 = varargs.get(ctx); + let buf_ptr: u32 = varargs.get(&ctx); unsafe { let mut stat: stat = std::mem::zeroed(); @@ -1030,7 +1032,7 @@ pub fn ___syscall196(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { } // getuid -pub fn ___syscall199(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn ___syscall199(_ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall199 (getuid)"); let uid = unsafe { getuid() as _ }; debug!(" => {}", uid); @@ -1040,20 +1042,21 @@ pub fn ___syscall199(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { // getdents // dirent structure is // i64, i64, u16 (280), i8, [i8; 256] -pub fn ___syscall220(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { +pub fn ___syscall220(ctx: ContextMut<'_, EmEnv>, _which: i32, mut varargs: VarArgs) -> i32 { use super::super::env::get_emscripten_data; - let fd: i32 = varargs.get(ctx); - let dirp_addr: i32 = varargs.get(ctx); - let count: u32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let dirp_addr: i32 = varargs.get(&ctx); + let count: u32 = varargs.get(&ctx); debug!( "emscripten::___syscall220 (getdents) {} {} {}", fd, dirp_addr, count ); - let dirp = emscripten_memory_pointer!(ctx.memory(0), dirp_addr) as *mut u8; + let dirp = emscripten_memory_pointer!(ctx, ctx.data().memory(0), dirp_addr) as *mut u8; - let opened_dirs = &mut get_emscripten_data(ctx).opened_dirs; + let data = &mut get_emscripten_data(&ctx); + let opened_dirs = &mut data.as_mut().unwrap().opened_dirs; // need to persist stream across calls? // let dir: *mut libc::DIR = unsafe { libc::fdopendir(fd) }; @@ -1104,11 +1107,11 @@ pub fn ___syscall220(ctx: &EmEnv, _which: i32, mut varargs: VarArgs) -> i32 { } // fcntl64 -pub fn ___syscall221(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall221(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall221 (fcntl64) {}", _which); - let fd: i32 = varargs.get(ctx); - let cmd: i32 = varargs.get(ctx); - let arg: i32 = varargs.get(ctx); + let fd: i32 = varargs.get(&ctx); + let cmd: i32 = varargs.get(&ctx); + let arg: i32 = varargs.get(&ctx); // (FAPPEND - 0x08 // |FASYNC - 0x40 // |FFSYNC - 0x80 @@ -1122,12 +1125,12 @@ pub fn ___syscall221(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int } /// fallocate -pub fn ___syscall324(ctx: &EmEnv, _which: c_int, mut varargs: VarArgs) -> c_int { +pub fn ___syscall324(ctx: ContextMut<'_, EmEnv>, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall324 (fallocate) {}", _which); - let _fd: c_int = varargs.get(ctx); - let _mode: c_int = varargs.get(ctx); - let _offset: off_t = varargs.get(ctx); - let _len: off_t = varargs.get(ctx); + let _fd: c_int = varargs.get(&ctx); + let _mode: c_int = varargs.get(&ctx); + let _offset: off_t = varargs.get(&ctx); + let _len: off_t = varargs.get(&ctx); #[cfg(not(any(target_os = "freebsd", target_vendor = "apple", target_os = "android")))] unsafe { fallocate(_fd, _mode, _offset, _len) diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index afb1049694f..fc33318681c 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -13,6 +13,8 @@ use std::ffi::CString; #[cfg(target_os = "windows")] use libc::time_t; +use wasmer::{AsContextMut, ContextMut}; + #[cfg(target_os = "windows")] #[allow(non_camel_case_types)] type clockid_t = c_int; @@ -50,7 +52,7 @@ const CLOCK_MONOTONIC_COARSE: clockid_t = 6; /// emscripten: _gettimeofday #[allow(clippy::cast_ptr_alignment)] -pub fn _gettimeofday(ctx: &EmEnv, tp: c_int, tz: c_int) -> c_int { +pub fn _gettimeofday(ctx: ContextMut<'_, EmEnv>, tp: c_int, tz: c_int) -> c_int { debug!("emscripten::_gettimeofday {} {}", tp, tz); #[repr(C)] struct GuestTimeVal { @@ -65,7 +67,8 @@ pub fn _gettimeofday(ctx: &EmEnv, tp: c_int, tz: c_int) -> c_int { unsafe { let now = SystemTime::now(); let since_epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap(); - let timeval_struct_ptr = emscripten_memory_pointer!(ctx.memory(0), tp) as *mut GuestTimeVal; + let timeval_struct_ptr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), tp) as *mut GuestTimeVal; (*timeval_struct_ptr).tv_sec = since_epoch.as_secs() as _; (*timeval_struct_ptr).tv_usec = since_epoch.subsec_nanos() as _; @@ -73,7 +76,7 @@ pub fn _gettimeofday(ctx: &EmEnv, tp: c_int, tz: c_int) -> c_int { 0 } -pub fn _clock_getres(_ctx: &EmEnv, _clk_id: i32, _tp: i32) -> i32 { +pub fn _clock_getres(mut _ctx: ContextMut<'_, EmEnv>, _clk_id: i32, _tp: i32) -> i32 { debug!("emscripten::_clock_getres"); // clock_getres(clk_id, tp) 0 @@ -81,7 +84,7 @@ pub fn _clock_getres(_ctx: &EmEnv, _clk_id: i32, _tp: i32) -> i32 { /// emscripten: _clock_gettime #[allow(clippy::cast_ptr_alignment)] -pub fn _clock_gettime(ctx: &EmEnv, clk_id: clockid_t, tp: c_int) -> c_int { +pub fn _clock_gettime(ctx: ContextMut<'_, EmEnv>, clk_id: clockid_t, tp: c_int) -> c_int { debug!("emscripten::_clock_gettime {} {}", clk_id, tp); // debug!("Memory {:?}", ctx.memory(0)[..]); #[repr(C)] @@ -106,48 +109,48 @@ pub fn _clock_gettime(ctx: &EmEnv, clk_id: clockid_t, tp: c_int) -> c_int { unsafe { let timespec_struct_ptr = - emscripten_memory_pointer!(ctx.memory(0), tp) as *mut GuestTimeSpec; + emscripten_memory_pointer!(ctx, ctx.data().memory(0), tp) as *mut GuestTimeSpec; (*timespec_struct_ptr).tv_sec = (duration / 1_000_000_000) as _; (*timespec_struct_ptr).tv_nsec = (duration % 1_000_000_000) as _; } 0 } -pub fn _clock_settime(_ctx: &EmEnv, _clk_id: i32, _tp: i32) -> i32 { +pub fn _clock_settime(mut _ctx: ContextMut<'_, EmEnv>, _clk_id: i32, _tp: i32) -> i32 { debug!("emscripten::_clock_settime"); // clock_settime(clk_id, tp) 0 } /// emscripten: ___clock_gettime -pub fn ___clock_gettime(ctx: &EmEnv, clk_id: clockid_t, tp: c_int) -> c_int { +pub fn ___clock_gettime(ctx: ContextMut<'_, EmEnv>, clk_id: clockid_t, tp: c_int) -> c_int { debug!("emscripten::___clock_gettime {} {}", clk_id, tp); _clock_gettime(ctx, clk_id, tp) } /// emscripten: _clock -pub fn _clock(_ctx: &EmEnv) -> c_int { +pub fn _clock(mut _ctx: ContextMut<'_, EmEnv>) -> c_int { debug!("emscripten::_clock"); 0 // TODO: unimplemented } /// emscripten: _difftime -pub fn _difftime(_ctx: &EmEnv, t0: u32, t1: u32) -> f64 { +pub fn _difftime(mut _ctx: ContextMut<'_, EmEnv>, t0: u32, t1: u32) -> f64 { debug!("emscripten::_difftime"); (t0 - t1) as _ } -pub fn _gmtime_r(_ctx: &EmEnv, _one: i32, _two: i32) -> i32 { +pub fn _gmtime_r(mut _ctx: ContextMut<'_, EmEnv>, _one: i32, _two: i32) -> i32 { debug!("emscripten::_gmtime_r"); -1 } -pub fn _mktime(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _mktime(mut _ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_mktime"); -1 } -pub fn _gmtime(_ctx: &EmEnv, _one: i32) -> i32 { +pub fn _gmtime(mut _ctx: ContextMut<'_, EmEnv>, _one: i32) -> i32 { debug!("emscripten::_gmtime"); -1 } @@ -168,14 +171,14 @@ struct guest_tm { } /// emscripten: _tvset -pub fn _tvset(_ctx: &EmEnv) { +pub fn _tvset(mut _ctx: ContextMut<'_, EmEnv>) { debug!("emscripten::_tvset UNIMPLEMENTED"); } /// formats time as a C string #[allow(clippy::cast_ptr_alignment)] -unsafe fn fmt_time(ctx: &EmEnv, time: u32) -> *const c_char { - let date = &*(emscripten_memory_pointer!(ctx.memory(0), time) as *mut guest_tm); +unsafe fn fmt_time(ctx: ContextMut<'_, EmEnv>, time: u32) -> *const c_char { + let date = &*(emscripten_memory_pointer!(ctx, ctx.data().memory(0), time) as *mut guest_tm); let days = vec!["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; let months = vec![ @@ -199,21 +202,21 @@ unsafe fn fmt_time(ctx: &EmEnv, time: u32) -> *const c_char { } /// emscripten: _asctime -pub fn _asctime(ctx: &EmEnv, time: u32) -> u32 { +pub fn _asctime(mut ctx: ContextMut<'_, EmEnv>, time: u32) -> u32 { debug!("emscripten::_asctime {}", time); unsafe { - let time_str_ptr = fmt_time(ctx, time); + let time_str_ptr = fmt_time(ctx.as_context_mut(), time); copy_cstr_into_wasm(ctx, time_str_ptr) - // let c_str = emscripten_memory_pointer!(ctx.memory(0), res) as *mut i8; + // let c_str = emscripten_memory_pointer!(ctx, ctx.data().memory(0), res) as *mut i8; // use std::ffi::CStr; // debug!("#### cstr = {:?}", CStr::from_ptr(c_str)); } } /// emscripten: _asctime_r -pub fn _asctime_r(ctx: &EmEnv, time: u32, buf: u32) -> u32 { +pub fn _asctime_r(mut ctx: ContextMut<'_, EmEnv>, time: u32, buf: u32) -> u32 { debug!("emscripten::_asctime_r {}, {}", time, buf); unsafe { @@ -221,10 +224,10 @@ pub fn _asctime_r(ctx: &EmEnv, time: u32, buf: u32) -> u32 { // to write out more than 26 bytes (including the null terminator). // See http://pubs.opengroup.org/onlinepubs/9699919799/functions/asctime.html // Our undefined behavior is to truncate the write to at most 26 bytes, including null terminator. - let time_str_ptr = fmt_time(ctx, time); + let time_str_ptr = fmt_time(ctx.as_context_mut(), time); write_to_buf(ctx, time_str_ptr, buf, 26) - // let c_str = emscripten_memory_pointer!(ctx.memory(0), res) as *mut i8; + // let c_str = emscripten_memory_pointer!(ctx, ctx.data().memory(0), res) as *mut i8; // use std::ffi::CStr; // debug!("#### cstr = {:?}", CStr::from_ptr(c_str)); } @@ -232,21 +235,22 @@ pub fn _asctime_r(ctx: &EmEnv, time: u32, buf: u32) -> u32 { /// emscripten: _localtime #[allow(clippy::cast_ptr_alignment)] -pub fn _localtime(ctx: &EmEnv, time_p: u32) -> c_int { +pub fn _localtime(mut ctx: ContextMut<'_, EmEnv>, time_p: u32) -> c_int { debug!("emscripten::_localtime {}", time_p); // NOTE: emscripten seems to want tzset() called in this function // https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r let timespec = unsafe { - let time_p_addr = emscripten_memory_pointer!(ctx.memory(0), time_p) as *mut i64; + let time_p_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), time_p) as *mut i64; let seconds = *time_p_addr; time::OffsetDateTime::from_unix_timestamp(seconds) }; unsafe { - let tm_struct_offset = env::call_malloc(ctx, mem::size_of::() as _); - let tm_struct_ptr = - emscripten_memory_pointer!(ctx.memory(0), tm_struct_offset) as *mut guest_tm; + let tm_struct_offset = + env::call_malloc(ctx.as_context_mut(), mem::size_of::() as _); + let tm_struct_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), tm_struct_offset) + as *mut guest_tm; // debug!( // ">>>>>>> time = {}, {}, {}, {}, {}, {}, {}, {}", // result_tm.tm_sec, result_tm.tm_min, result_tm.tm_hour, result_tm.tm_mday, @@ -269,14 +273,14 @@ pub fn _localtime(ctx: &EmEnv, time_p: u32) -> c_int { } /// emscripten: _localtime_r #[allow(clippy::cast_ptr_alignment)] -pub fn _localtime_r(ctx: &EmEnv, time_p: u32, result: u32) -> c_int { +pub fn _localtime_r(ctx: ContextMut<'_, EmEnv>, time_p: u32, result: u32) -> c_int { debug!("emscripten::_localtime_r {}", time_p); // NOTE: emscripten seems to want tzset() called in this function // https://stackoverflow.com/questions/19170721/real-time-awareness-of-timezone-change-in-localtime-vs-localtime-r unsafe { - let seconds = emscripten_memory_pointer!(ctx.memory(0), time_p) as *const i32; + let seconds = emscripten_memory_pointer!(ctx, ctx.data().memory(0), time_p) as *const i32; let timespec = time::OffsetDateTime::from_unix_timestamp_nanos(*seconds as _); // debug!( @@ -285,7 +289,8 @@ pub fn _localtime_r(ctx: &EmEnv, time_p: u32, result: u32) -> c_int { // result_tm.tm_mon, result_tm.tm_year, result_tm.tm_wday, result_tm.tm_yday, // ); - let result_addr = emscripten_memory_pointer!(ctx.memory(0), result) as *mut guest_tm; + let result_addr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), result) as *mut guest_tm; (*result_addr).tm_sec = timespec.second() as _; (*result_addr).tm_min = timespec.minute() as _; @@ -305,27 +310,28 @@ pub fn _localtime_r(ctx: &EmEnv, time_p: u32, result: u32) -> c_int { /// emscripten: _time #[allow(clippy::cast_ptr_alignment)] -pub fn _time(ctx: &EmEnv, time_p: u32) -> i32 { +pub fn _time(ctx: ContextMut<'_, EmEnv>, time_p: u32) -> i32 { debug!("emscripten::_time {}", time_p); unsafe { - let time_p_addr = emscripten_memory_pointer!(ctx.memory(0), time_p) as *mut i64; + let time_p_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), time_p) as *mut i64; libc_time(time_p_addr) as i32 // TODO review i64 } } -pub fn _ctime_r(ctx: &EmEnv, time_p: u32, buf: u32) -> u32 { +pub fn _ctime_r(mut ctx: ContextMut<'_, EmEnv>, time_p: u32, buf: u32) -> u32 { debug!("emscripten::_ctime_r {} {}", time_p, buf); // var stack = stackSave(); - let (result_offset, _result_slice): (u32, &mut [u8]) = unsafe { allocate_on_stack(ctx, 44) }; - let time = _localtime_r(ctx, time_p, result_offset) as u32; + let (result_offset, _result_slice): (u32, &mut [u8]) = + unsafe { allocate_on_stack(&mut ctx.as_context_mut(), 44) }; + let time = _localtime_r(ctx.as_context_mut(), time_p, result_offset) as u32; let rv = _asctime_r(ctx, time, buf); // stackRestore(stack); rv } -pub fn _ctime(ctx: &EmEnv, time_p: u32) -> u32 { +pub fn _ctime(ctx: ContextMut<'_, EmEnv>, time_p: u32) -> u32 { debug!("emscripten::_ctime {}", time_p); let tm_current = 2414544; _ctime_r(ctx, time_p, tm_current) @@ -334,11 +340,12 @@ pub fn _ctime(ctx: &EmEnv, time_p: u32) -> u32 { /// emscripten: _timegm #[cfg(not(target_os = "windows"))] #[allow(clippy::cast_ptr_alignment)] -pub fn _timegm(ctx: &EmEnv, time_ptr: u32) -> i32 { +pub fn _timegm(ctx: ContextMut<'_, EmEnv>, time_ptr: u32) -> i32 { debug!("emscripten::_timegm {}", time_ptr); unsafe { - let time_p_addr = emscripten_memory_pointer!(ctx.memory(0), time_ptr) as *mut guest_tm; + let time_p_addr = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), time_ptr) as *mut guest_tm; let x: *mut c_char = CString::new("").expect("CString::new failed").into_raw(); let mut rust_tm = libc_tm { @@ -374,7 +381,7 @@ pub fn _timegm(ctx: &EmEnv, time_ptr: u32) -> i32 { } #[cfg(target_os = "windows")] -pub fn _timegm(_ctx: &EmEnv, _time_ptr: c_int) -> i32 { +pub fn _timegm(mut _ctx: ContextMut<'_, EmEnv>, _time_ptr: c_int) -> i32 { debug!( "emscripten::_timegm - UNIMPLEMENTED IN WINDOWS {}", _time_ptr @@ -383,18 +390,24 @@ pub fn _timegm(_ctx: &EmEnv, _time_ptr: c_int) -> i32 { } /// emscripten: _strftime -pub fn _strftime(ctx: &EmEnv, s_ptr: c_int, maxsize: u32, format_ptr: c_int, tm_ptr: c_int) -> i32 { +pub fn _strftime( + ctx: ContextMut<'_, EmEnv>, + s_ptr: c_int, + maxsize: u32, + format_ptr: c_int, + tm_ptr: c_int, +) -> i32 { debug!( "emscripten::_strftime {} {} {} {}", s_ptr, maxsize, format_ptr, tm_ptr ); #[allow(clippy::cast_ptr_alignment)] - let s = emscripten_memory_pointer!(ctx.memory(0), s_ptr) as *mut c_char; + let s = emscripten_memory_pointer!(ctx, ctx.data().memory(0), s_ptr) as *mut c_char; #[allow(clippy::cast_ptr_alignment)] - let format = emscripten_memory_pointer!(ctx.memory(0), format_ptr) as *const c_char; + let format = emscripten_memory_pointer!(ctx, ctx.data().memory(0), format_ptr) as *const c_char; #[allow(clippy::cast_ptr_alignment)] - let tm = emscripten_memory_pointer!(ctx.memory(0), tm_ptr) as *const guest_tm; + let tm = emscripten_memory_pointer!(ctx, ctx.data().memory(0), tm_ptr) as *const guest_tm; let format_string = unsafe { std::ffi::CStr::from_ptr(format).to_str().unwrap() }; @@ -431,7 +444,7 @@ pub fn _strftime(ctx: &EmEnv, s_ptr: c_int, maxsize: u32, format_ptr: c_int, tm_ /// emscripten: _strftime_l pub fn _strftime_l( - ctx: &EmEnv, + ctx: ContextMut<'_, EmEnv>, s_ptr: c_int, maxsize: u32, format_ptr: c_int, diff --git a/lib/emscripten/src/ucontext.rs b/lib/emscripten/src/ucontext.rs index 7fda8656ea0..d987cb0ba1c 100644 --- a/lib/emscripten/src/ucontext.rs +++ b/lib/emscripten/src/ucontext.rs @@ -1,20 +1,27 @@ use crate::EmEnv; +use wasmer::ContextMut; -pub fn _getcontext(_ctx: &EmEnv, _ucp: i32) -> i32 { +pub fn _getcontext(mut _ctx: ContextMut<'_, EmEnv>, _ucp: i32) -> i32 { debug!("emscripten::_getcontext({})", _ucp); 0 } -pub fn _makecontext(_ctx: &EmEnv, _ucp: i32, _func: i32, _argc: i32, _argv: i32) { +pub fn _makecontext( + mut _ctx: ContextMut<'_, EmEnv>, + _ucp: i32, + _func: i32, + _argc: i32, + _argv: i32, +) { debug!( "emscripten::_makecontext({}, {}, {}, {})", _ucp, _func, _argc, _argv ); } -pub fn _setcontext(_ctx: &EmEnv, _ucp: i32) -> i32 { +pub fn _setcontext(mut _ctx: ContextMut<'_, EmEnv>, _ucp: i32) -> i32 { debug!("emscripten::_setcontext({})", _ucp); 0 } -pub fn _swapcontext(_ctx: &EmEnv, _oucp: i32, _ucp: i32) -> i32 { +pub fn _swapcontext(mut _ctx: ContextMut<'_, EmEnv>, _oucp: i32, _ucp: i32) -> i32 { debug!("emscripten::_swapcontext({}, {})", _oucp, _ucp); 0 } diff --git a/lib/emscripten/src/unistd.rs b/lib/emscripten/src/unistd.rs index ecb08a371f6..7a6090199bd 100644 --- a/lib/emscripten/src/unistd.rs +++ b/lib/emscripten/src/unistd.rs @@ -1,6 +1,7 @@ use crate::EmEnv; +use wasmer::ContextMut; -pub fn confstr(_ctx: &EmEnv, _name: i32, _buf_pointer: i32, _len: i32) -> i32 { +pub fn confstr(mut _ctx: ContextMut<'_, EmEnv>, _name: i32, _buf_pointer: i32, _len: i32) -> i32 { debug!("unistd::confstr({}, {}, {})", _name, _buf_pointer, _len); 0 } diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 5ea2e99ad37..cf7a1422c22 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -1,5 +1,5 @@ use super::env; -use super::env::get_emscripten_data; +use super::env::{get_emscripten_data, get_emscripten_funcs}; use crate::storage::align_memory; use crate::EmEnv; use libc::stat; @@ -8,7 +8,7 @@ use std::mem::size_of; use std::os::raw::c_char; use std::path::PathBuf; use std::slice; -use wasmer::{GlobalInit, Memory, Module, Pages, WasmPtr}; +use wasmer::{AsContextMut, ContextMut, GlobalInit, Memory, Module, Pages, WasmPtr}; /// We check if a provided module is an Emscripten generated one pub fn is_emscripten_module(module: &Module) -> bool { @@ -93,8 +93,13 @@ pub fn get_emscripten_metadata(module: &Module) -> Result, St } } -pub unsafe fn write_to_buf(ctx: &EmEnv, string: *const c_char, buf: u32, max: u32) -> u32 { - let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut c_char; +pub unsafe fn write_to_buf( + ctx: ContextMut<'_, EmEnv>, + string: *const c_char, + buf: u32, + max: u32, +) -> u32 { + let buf_addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as *mut c_char; for i in 0..max { *buf_addr.add(i as _) = *string.add(i as _); @@ -104,11 +109,12 @@ pub unsafe fn write_to_buf(ctx: &EmEnv, string: *const c_char, buf: u32, max: u3 } /// This function expects nullbyte to be appended. -pub unsafe fn copy_cstr_into_wasm(ctx: &EmEnv, cstr: *const c_char) -> u32 { +pub unsafe fn copy_cstr_into_wasm(mut ctx: ContextMut<'_, EmEnv>, cstr: *const c_char) -> u32 { let s = CStr::from_ptr(cstr).to_str().unwrap(); let cstr_len = s.len(); - let space_offset = env::call_malloc(ctx, (cstr_len as u32) + 1); - let raw_memory = emscripten_memory_pointer!(ctx.memory(0), space_offset) as *mut c_char; + let space_offset = env::call_malloc(ctx.as_context_mut(), (cstr_len as u32) + 1); + let raw_memory = + emscripten_memory_pointer!(ctx, ctx.data().memory(0), space_offset) as *mut c_char; let slice = slice::from_raw_parts_mut(raw_memory, cstr_len); for (byte, loc) in s.bytes().zip(slice.iter_mut()) { @@ -122,20 +128,25 @@ pub unsafe fn copy_cstr_into_wasm(ctx: &EmEnv, cstr: *const c_char) -> u32 { space_offset } -pub unsafe fn allocate_on_stack<'a, T: Copy>(ctx: &'a EmEnv, count: u32) -> (u32, &'a mut [T]) { - let offset = get_emscripten_data(ctx) - .stack_alloc_ref() - .unwrap() - .call(count * (size_of::() as u32)) +pub unsafe fn allocate_on_stack<'a, T: Copy>( + ctx: &mut ContextMut<'a, EmEnv>, + count: u32, +) -> (u32, &'a mut [T]) { + let stack_alloc_ref = get_emscripten_funcs(ctx).stack_alloc_ref().unwrap().clone(); + let offset = stack_alloc_ref + .call(&mut ctx.as_context_mut(), count * (size_of::() as u32)) .unwrap(); - let addr = emscripten_memory_pointer!(ctx.memory(0), offset) as *mut T; + let addr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), offset) as *mut T; let slice = slice::from_raw_parts_mut(addr, count as usize); (offset, slice) } -pub unsafe fn allocate_cstr_on_stack<'a>(ctx: &'a EmEnv, s: &str) -> (u32, &'a [u8]) { +pub unsafe fn allocate_cstr_on_stack<'a>( + ctx: &'a mut ContextMut<'a, EmEnv>, + s: &str, +) -> (u32, &'a [u8]) { let (offset, slice) = allocate_on_stack(ctx, (s.len() + 1) as u32); use std::iter; @@ -147,7 +158,10 @@ pub unsafe fn allocate_cstr_on_stack<'a>(ctx: &'a EmEnv, s: &str) -> (u32, &'a [ } #[cfg(not(target_os = "windows"))] -pub unsafe fn copy_terminated_array_of_cstrs(_ctx: &EmEnv, cstrs: *mut *mut c_char) -> u32 { +pub unsafe fn copy_terminated_array_of_cstrs( + mut _ctx: ContextMut<'_, EmEnv>, + cstrs: *mut *mut c_char, +) -> u32 { let _total_num = { let mut ptr = cstrs; let mut counter = 0; @@ -185,8 +199,8 @@ pub struct GuestStat { } #[allow(clippy::cast_ptr_alignment)] -pub unsafe fn copy_stat_into_wasm(ctx: &EmEnv, buf: u32, stat: &stat) { - let stat_ptr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut GuestStat; +pub unsafe fn copy_stat_into_wasm(ctx: ContextMut<'_, EmEnv>, buf: u32, stat: &stat) { + let stat_ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), buf) as *mut GuestStat; (*stat_ptr).st_dev = stat.st_dev as _; (*stat_ptr).__st_dev_padding = 0; (*stat_ptr).__st_ino_truncated = stat.st_ino as _; @@ -213,20 +227,20 @@ pub unsafe fn copy_stat_into_wasm(ctx: &EmEnv, buf: u32, stat: &stat) { } #[allow(dead_code)] // it's used in `env/windows/mod.rs`. -pub fn read_string_from_wasm(memory: &Memory, offset: u32) -> String { +pub fn read_string_from_wasm(ctx: ContextMut<'_, EmEnv>, memory: &Memory, offset: u32) -> String { WasmPtr::::new(offset) - .read_utf8_string_with_nul(memory) + .read_utf8_string_with_nul(&ctx, memory) .unwrap() } /// This function trys to find an entry in mapdir /// translating paths into their correct value -pub fn get_cstr_path(ctx: &EmEnv, path: *const i8) -> Option { +pub fn get_cstr_path(ctx: ContextMut<'_, EmEnv>, path: *const i8) -> Option { use std::collections::VecDeque; let path_str = unsafe { std::ffi::CStr::from_ptr(path as *const _).to_str().unwrap() }.to_string(); - let data = get_emscripten_data(ctx); + let data = get_emscripten_data(&ctx); let path = PathBuf::from(path_str); let mut prefix_added = false; let mut components = path.components().collect::>(); @@ -240,6 +254,8 @@ pub fn get_cstr_path(ctx: &EmEnv, path: *const i8) -> Option for c in components.into_iter() { cumulative_path.push(c); if let Some(val) = data + .as_ref() + .unwrap() .mapped_dirs .get(&cumulative_path.to_string_lossy().to_string()) { @@ -257,13 +273,20 @@ pub fn get_cstr_path(ctx: &EmEnv, path: *const i8) -> Option /// gets the current directory /// handles mapdir logic -pub fn get_current_directory(ctx: &EmEnv) -> Option { - if let Some(val) = get_emscripten_data(ctx).mapped_dirs.get(".") { +pub fn get_current_directory(ctx: ContextMut<'_, EmEnv>) -> Option { + if let Some(val) = get_emscripten_data(&ctx) + .as_ref() + .unwrap() + .mapped_dirs + .get(".") + { return Some(val.clone()); } std::env::current_dir() .map(|cwd| { - if let Some(val) = get_emscripten_data(ctx) + if let Some(val) = get_emscripten_data(&ctx) + .as_ref() + .unwrap() .mapped_dirs .get(&cwd.to_string_lossy().to_string()) { diff --git a/lib/emscripten/src/varargs.rs b/lib/emscripten/src/varargs.rs index 4a9c886b046..826dc15cf9b 100644 --- a/lib/emscripten/src/varargs.rs +++ b/lib/emscripten/src/varargs.rs @@ -3,6 +3,7 @@ use std::mem; use wasmer::FromToNativeWasmType; // use std::ffi::CStr; use std::os::raw::c_char; +use wasmer::ContextMut; #[repr(transparent)] #[derive(Copy, Clone)] @@ -11,16 +12,16 @@ pub struct VarArgs { } impl VarArgs { - pub fn get(&mut self, ctx: &EmEnv) -> T { - let ptr = emscripten_memory_pointer!(ctx.memory(0), self.pointer); + pub fn get(&mut self, ctx: &ContextMut<'_, EmEnv>) -> T { + let ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), self.pointer); self.pointer += mem::size_of::() as u32; unsafe { (ptr as *const T).read() } } // pub fn getStr<'a>(&mut self, ctx: &mut Ctx) -> &'a CStr { - pub fn get_str(&mut self, ctx: &EmEnv) -> *const c_char { + pub fn get_str(&mut self, ctx: &ContextMut<'_, EmEnv>) -> *const c_char { let ptr_addr: u32 = self.get(ctx); - let ptr = emscripten_memory_pointer!(ctx.memory(0), ptr_addr) as *const c_char; + let ptr = emscripten_memory_pointer!(ctx, ctx.data().memory(0), ptr_addr) as *const c_char; ptr // unsafe { CStr::from_ptr(ptr) } } diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 6d98cbf2571..ab8f35e0471 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -55,7 +55,7 @@ pub use wasmer_vfs::VirtualFile as WasiFile; pub use wasmer_vfs::{FsError, VirtualFile}; use thiserror::Error; -use wasmer::{imports, Function, Imports, Memory, MemoryAccessError}; +use wasmer::{imports, namespace, Exports, Function, Imports, Memory, MemoryAccessError}; use std::sync::{Arc, Mutex, MutexGuard}; @@ -121,109 +121,129 @@ pub fn generate_import_object_from_ctx( } } +fn wasi_unstable_exports(ctx: &mut ContextMut<'_, WasiEnv>) -> Exports { + let namespace = namespace! { + "args_get" => Function::new_native(ctx, args_get), + "args_sizes_get" => Function::new_native(ctx, args_sizes_get), + "clock_res_get" => Function::new_native(ctx, clock_res_get), + "clock_time_get" => Function::new_native(ctx, clock_time_get), + "environ_get" => Function::new_native(ctx, environ_get), + "environ_sizes_get" => Function::new_native(ctx, environ_sizes_get), + "fd_advise" => Function::new_native(ctx, fd_advise), + "fd_allocate" => Function::new_native(ctx, fd_allocate), + "fd_close" => Function::new_native(ctx, fd_close), + "fd_datasync" => Function::new_native(ctx, fd_datasync), + "fd_fdstat_get" => Function::new_native(ctx, fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_native(ctx, fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_native(ctx, fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_native(ctx, legacy::snapshot0::fd_filestat_get), + "fd_filestat_set_size" => Function::new_native(ctx, fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_native(ctx, fd_filestat_set_times), + "fd_pread" => Function::new_native(ctx, fd_pread), + "fd_prestat_get" => Function::new_native(ctx, fd_prestat_get), + "fd_prestat_dir_name" => Function::new_native(ctx, fd_prestat_dir_name), + "fd_pwrite" => Function::new_native(ctx, fd_pwrite), + "fd_read" => Function::new_native(ctx, fd_read), + "fd_readdir" => Function::new_native(ctx, fd_readdir), + "fd_renumber" => Function::new_native(ctx, fd_renumber), + "fd_seek" => Function::new_native(ctx, legacy::snapshot0::fd_seek), + "fd_sync" => Function::new_native(ctx, fd_sync), + "fd_tell" => Function::new_native(ctx, fd_tell), + "fd_write" => Function::new_native(ctx, fd_write), + "path_create_directory" => Function::new_native(ctx, path_create_directory), + "path_filestat_get" => Function::new_native(ctx, legacy::snapshot0::path_filestat_get), + "path_filestat_set_times" => Function::new_native(ctx, path_filestat_set_times), + "path_link" => Function::new_native(ctx, path_link), + "path_open" => Function::new_native(ctx, path_open), + "path_readlink" => Function::new_native(ctx, path_readlink), + "path_remove_directory" => Function::new_native(ctx, path_remove_directory), + "path_rename" => Function::new_native(ctx, path_rename), + "path_symlink" => Function::new_native(ctx, path_symlink), + "path_unlink_file" => Function::new_native(ctx, path_unlink_file), + "poll_oneoff" => Function::new_native(ctx, legacy::snapshot0::poll_oneoff), + "proc_exit" => Function::new_native(ctx, proc_exit), + "proc_raise" => Function::new_native(ctx, proc_raise), + "random_get" => Function::new_native(ctx, random_get), + "sched_yield" => Function::new_native(ctx, sched_yield), + "sock_recv" => Function::new_native(ctx, sock_recv), + "sock_send" => Function::new_native(ctx, sock_send), + "sock_shutdown" => Function::new_native(ctx, sock_shutdown), + }; + namespace +} + +fn wasi_snapshot_preview1_exports(ctx: &mut ContextMut<'_, WasiEnv>) -> Exports { + let namespace = namespace! { + "args_get" => Function::new_native(ctx, args_get), + "args_sizes_get" => Function::new_native(ctx, args_sizes_get), + "clock_res_get" => Function::new_native(ctx, clock_res_get), + "clock_time_get" => Function::new_native(ctx, clock_time_get), + "environ_get" => Function::new_native(ctx, environ_get), + "environ_sizes_get" => Function::new_native(ctx, environ_sizes_get), + "fd_advise" => Function::new_native(ctx, fd_advise), + "fd_allocate" => Function::new_native(ctx, fd_allocate), + "fd_close" => Function::new_native(ctx, fd_close), + "fd_datasync" => Function::new_native(ctx, fd_datasync), + "fd_fdstat_get" => Function::new_native(ctx, fd_fdstat_get), + "fd_fdstat_set_flags" => Function::new_native(ctx, fd_fdstat_set_flags), + "fd_fdstat_set_rights" => Function::new_native(ctx, fd_fdstat_set_rights), + "fd_filestat_get" => Function::new_native(ctx, fd_filestat_get), + "fd_filestat_set_size" => Function::new_native(ctx, fd_filestat_set_size), + "fd_filestat_set_times" => Function::new_native(ctx, fd_filestat_set_times), + "fd_pread" => Function::new_native(ctx, fd_pread), + "fd_prestat_get" => Function::new_native(ctx, fd_prestat_get), + "fd_prestat_dir_name" => Function::new_native(ctx, fd_prestat_dir_name), + "fd_pwrite" => Function::new_native(ctx, fd_pwrite), + "fd_read" => Function::new_native(ctx, fd_read), + "fd_readdir" => Function::new_native(ctx, fd_readdir), + "fd_renumber" => Function::new_native(ctx, fd_renumber), + "fd_seek" => Function::new_native(ctx, fd_seek), + "fd_sync" => Function::new_native(ctx, fd_sync), + "fd_tell" => Function::new_native(ctx, fd_tell), + "fd_write" => Function::new_native(ctx, fd_write), + "path_create_directory" => Function::new_native(ctx, path_create_directory), + "path_filestat_get" => Function::new_native(ctx, path_filestat_get), + "path_filestat_set_times" => Function::new_native(ctx, path_filestat_set_times), + "path_link" => Function::new_native(ctx, path_link), + "path_open" => Function::new_native(ctx, path_open), + "path_readlink" => Function::new_native(ctx, path_readlink), + "path_remove_directory" => Function::new_native(ctx, path_remove_directory), + "path_rename" => Function::new_native(ctx, path_rename), + "path_symlink" => Function::new_native(ctx, path_symlink), + "path_unlink_file" => Function::new_native(ctx, path_unlink_file), + "poll_oneoff" => Function::new_native(ctx, poll_oneoff), + "proc_exit" => Function::new_native(ctx, proc_exit), + "proc_raise" => Function::new_native(ctx, proc_raise), + "random_get" => Function::new_native(ctx, random_get), + "sched_yield" => Function::new_native(ctx, sched_yield), + "sock_recv" => Function::new_native(ctx, sock_recv), + "sock_send" => Function::new_native(ctx, sock_send), + "sock_shutdown" => Function::new_native(ctx, sock_shutdown), + }; + namespace +} +pub fn import_object_for_all_wasi_versions(ctx: &mut ContextMut<'_, WasiEnv>) -> Imports { + let wasi_unstable_exports = wasi_unstable_exports(ctx); + let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(ctx); + imports! { + "wasi_unstable" => wasi_unstable_exports, + "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports, + } +} + /// Combines a state generating function with the import list for legacy WASI fn generate_import_object_snapshot0(ctx: &mut ContextMut<'_, WasiEnv>) -> Imports { + let wasi_unstable_exports = wasi_unstable_exports(ctx); imports! { - "wasi_unstable" => { - "args_get" => Function::new_native(ctx, args_get), - "args_sizes_get" => Function::new_native(ctx, args_sizes_get), - "clock_res_get" => Function::new_native(ctx, clock_res_get), - "clock_time_get" => Function::new_native(ctx, clock_time_get), - "environ_get" => Function::new_native(ctx, environ_get), - "environ_sizes_get" => Function::new_native(ctx, environ_sizes_get), - "fd_advise" => Function::new_native(ctx, fd_advise), - "fd_allocate" => Function::new_native(ctx, fd_allocate), - "fd_close" => Function::new_native(ctx, fd_close), - "fd_datasync" => Function::new_native(ctx, fd_datasync), - "fd_fdstat_get" => Function::new_native(ctx, fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native(ctx, fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native(ctx, fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native(ctx, legacy::snapshot0::fd_filestat_get), - "fd_filestat_set_size" => Function::new_native(ctx, fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native(ctx, fd_filestat_set_times), - "fd_pread" => Function::new_native(ctx, fd_pread), - "fd_prestat_get" => Function::new_native(ctx, fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native(ctx, fd_prestat_dir_name), - "fd_pwrite" => Function::new_native(ctx, fd_pwrite), - "fd_read" => Function::new_native(ctx, fd_read), - "fd_readdir" => Function::new_native(ctx, fd_readdir), - "fd_renumber" => Function::new_native(ctx, fd_renumber), - "fd_seek" => Function::new_native(ctx, legacy::snapshot0::fd_seek), - "fd_sync" => Function::new_native(ctx, fd_sync), - "fd_tell" => Function::new_native(ctx, fd_tell), - "fd_write" => Function::new_native(ctx, fd_write), - "path_create_directory" => Function::new_native(ctx, path_create_directory), - "path_filestat_get" => Function::new_native(ctx, legacy::snapshot0::path_filestat_get), - "path_filestat_set_times" => Function::new_native(ctx, path_filestat_set_times), - "path_link" => Function::new_native(ctx, path_link), - "path_open" => Function::new_native(ctx, path_open), - "path_readlink" => Function::new_native(ctx, path_readlink), - "path_remove_directory" => Function::new_native(ctx, path_remove_directory), - "path_rename" => Function::new_native(ctx, path_rename), - "path_symlink" => Function::new_native(ctx, path_symlink), - "path_unlink_file" => Function::new_native(ctx, path_unlink_file), - "poll_oneoff" => Function::new_native(ctx, legacy::snapshot0::poll_oneoff), - "proc_exit" => Function::new_native(ctx, proc_exit), - "proc_raise" => Function::new_native(ctx, proc_raise), - "random_get" => Function::new_native(ctx, random_get), - "sched_yield" => Function::new_native(ctx, sched_yield), - "sock_recv" => Function::new_native(ctx, sock_recv), - "sock_send" => Function::new_native(ctx, sock_send), - "sock_shutdown" => Function::new_native(ctx, sock_shutdown), - }, + "wasi_unstable" => wasi_unstable_exports } } /// Combines a state generating function with the import list for snapshot 1 fn generate_import_object_snapshot1(ctx: &mut ContextMut<'_, WasiEnv>) -> Imports { + let wasi_snapshot_preview1_exports = wasi_snapshot_preview1_exports(ctx); imports! { - "wasi_snapshot_preview1" => { - "args_get" => Function::new_native(ctx, args_get), - "args_sizes_get" => Function::new_native(ctx, args_sizes_get), - "clock_res_get" => Function::new_native(ctx, clock_res_get), - "clock_time_get" => Function::new_native(ctx, clock_time_get), - "environ_get" => Function::new_native(ctx, environ_get), - "environ_sizes_get" => Function::new_native(ctx, environ_sizes_get), - "fd_advise" => Function::new_native(ctx, fd_advise), - "fd_allocate" => Function::new_native(ctx, fd_allocate), - "fd_close" => Function::new_native(ctx, fd_close), - "fd_datasync" => Function::new_native(ctx, fd_datasync), - "fd_fdstat_get" => Function::new_native(ctx, fd_fdstat_get), - "fd_fdstat_set_flags" => Function::new_native(ctx, fd_fdstat_set_flags), - "fd_fdstat_set_rights" => Function::new_native(ctx, fd_fdstat_set_rights), - "fd_filestat_get" => Function::new_native(ctx, fd_filestat_get), - "fd_filestat_set_size" => Function::new_native(ctx, fd_filestat_set_size), - "fd_filestat_set_times" => Function::new_native(ctx, fd_filestat_set_times), - "fd_pread" => Function::new_native(ctx, fd_pread), - "fd_prestat_get" => Function::new_native(ctx, fd_prestat_get), - "fd_prestat_dir_name" => Function::new_native(ctx, fd_prestat_dir_name), - "fd_pwrite" => Function::new_native(ctx, fd_pwrite), - "fd_read" => Function::new_native(ctx, fd_read), - "fd_readdir" => Function::new_native(ctx, fd_readdir), - "fd_renumber" => Function::new_native(ctx, fd_renumber), - "fd_seek" => Function::new_native(ctx, fd_seek), - "fd_sync" => Function::new_native(ctx, fd_sync), - "fd_tell" => Function::new_native(ctx, fd_tell), - "fd_write" => Function::new_native(ctx, fd_write), - "path_create_directory" => Function::new_native(ctx, path_create_directory), - "path_filestat_get" => Function::new_native(ctx, path_filestat_get), - "path_filestat_set_times" => Function::new_native(ctx, path_filestat_set_times), - "path_link" => Function::new_native(ctx, path_link), - "path_open" => Function::new_native(ctx, path_open), - "path_readlink" => Function::new_native(ctx, path_readlink), - "path_remove_directory" => Function::new_native(ctx, path_remove_directory), - "path_rename" => Function::new_native(ctx, path_rename), - "path_symlink" => Function::new_native(ctx, path_symlink), - "path_unlink_file" => Function::new_native(ctx, path_unlink_file), - "poll_oneoff" => Function::new_native(ctx, poll_oneoff), - "proc_exit" => Function::new_native(ctx, proc_exit), - "proc_raise" => Function::new_native(ctx, proc_raise), - "random_get" => Function::new_native(ctx, random_get), - "sched_yield" => Function::new_native(ctx, sched_yield), - "sock_recv" => Function::new_native(ctx, sock_recv), - "sock_send" => Function::new_native(ctx, sock_send), - "sock_shutdown" => Function::new_native(ctx, sock_shutdown), - } + "wasi_snapshot_preview1" => wasi_snapshot_preview1_exports } }