From 393b7dbdf3936ec9efceb844c5e45cf08a9e78be Mon Sep 17 00:00:00 2001 From: Syrus Date: Sat, 9 Feb 2019 13:33:22 -0800 Subject: [PATCH 1/8] Moved ctx in func macro to be first arg --- lib/runtime-core/src/import.rs | 2 +- lib/runtime-core/src/macros.rs | 2 +- lib/runtime-core/src/typed_func.rs | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/runtime-core/src/import.rs b/lib/runtime-core/src/import.rs index cf833ec6e41..7073a5ac1e0 100644 --- a/lib/runtime-core/src/import.rs +++ b/lib/runtime-core/src/import.rs @@ -32,7 +32,7 @@ impl IsExport for Export { /// }, /// }; /// -/// fn foo(n: i32, _: &mut Ctx) -> i32 { +/// fn foo(_: &mut Ctx, n: i32) -> i32 { /// n /// } /// ``` diff --git a/lib/runtime-core/src/macros.rs b/lib/runtime-core/src/macros.rs index 620f6504646..a7bdff1b0a8 100644 --- a/lib/runtime-core/src/macros.rs +++ b/lib/runtime-core/src/macros.rs @@ -38,7 +38,7 @@ macro_rules! func { /// }, /// }; /// -/// fn foo(n: i32, _: &mut Ctx) -> i32 { +/// fn foo(_: &mut Ctx, n: i32) -> i32 { /// n /// } /// ``` diff --git a/lib/runtime-core/src/typed_func.rs b/lib/runtime-core/src/typed_func.rs index 9cbcdc911d1..9989764c646 100644 --- a/lib/runtime-core/src/typed_func.rs +++ b/lib/runtime-core/src/typed_func.rs @@ -183,16 +183,16 @@ macro_rules! impl_traits { } } - impl< $( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly, FN: Fn( $( $x, )* &mut Ctx) -> Trap> ExternalFunction<($( $x ),*), Rets> for FN { + impl< $( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap> ExternalFunction<($( $x ),*), Rets> for FN { #[allow(non_snake_case)] fn to_raw(&self) -> *const () { assert_eq!(mem::size_of::(), 0, "you cannot use a closure that captures state for `Func`."); - extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly, FN: Fn( $( $x, )* &mut Ctx) -> Trap>( $( $x: $x, )* ctx: &mut Ctx) -> Rets::CStruct { + extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: $x )* ) -> Rets::CStruct { let f: FN = unsafe { mem::transmute_copy(&()) }; let msg = match panic::catch_unwind(panic::AssertUnwindSafe(|| { - f( $( $x, )* ctx).report() + f( ctx $( ,$x )* ).report() })) { Ok(Ok(returns)) => return returns.into_c_struct(), Ok(Err(err)) => err, @@ -271,7 +271,7 @@ mod tests { use super::*; #[test] fn test_call() { - fn foo(a: i32, b: i32, _ctx: &mut Ctx) -> (i32, i32) { + fn foo(_ctx: &mut Ctx, a: i32, b: i32) -> (i32, i32) { (a, b) } @@ -282,7 +282,7 @@ mod tests { fn test_imports() { use crate::{func, imports}; - fn foo(a: i32, _ctx: &mut Ctx) -> i32 { + fn foo(_ctx: &mut Ctx, a: i32) -> i32 { a } From 6c7fd55b875363182045c13d429887d9a55ab1e7 Mon Sep 17 00:00:00 2001 From: Syrus Date: Sat, 9 Feb 2019 13:58:05 -0800 Subject: [PATCH 2/8] Moved ctx to be the first argument in emscripten --- lib/emscripten/src/README.md | 16 ++--- lib/emscripten/src/env/mod.rs | 16 ++--- lib/emscripten/src/env/unix/mod.rs | 18 +++--- lib/emscripten/src/env/windows/mod.rs | 18 +++--- lib/emscripten/src/errno.rs | 2 +- lib/emscripten/src/exception.rs | 6 +- lib/emscripten/src/io/unix.rs | 4 +- lib/emscripten/src/io/windows.rs | 4 +- lib/emscripten/src/jmp.rs | 6 +- lib/emscripten/src/lib.rs | 10 ++-- lib/emscripten/src/linking.rs | 6 +- lib/emscripten/src/lock.rs | 6 +- lib/emscripten/src/math.rs | 12 ++-- lib/emscripten/src/memory.rs | 6 +- lib/emscripten/src/nullfunc.rs | 52 ++++++++-------- lib/emscripten/src/process.rs | 42 ++++++------- lib/emscripten/src/signal.rs | 12 ++-- lib/emscripten/src/syscalls/mod.rs | 82 +++++++++++++------------- lib/emscripten/src/syscalls/unix.rs | 26 ++++---- lib/emscripten/src/syscalls/windows.rs | 26 ++++---- lib/emscripten/src/time.rs | 38 ++++++------ lib/emscripten/src/utils.rs | 10 ++-- 22 files changed, 209 insertions(+), 209 deletions(-) diff --git a/lib/emscripten/src/README.md b/lib/emscripten/src/README.md index 128ef1783f1..54b9f9dc1af 100644 --- a/lib/emscripten/src/README.md +++ b/lib/emscripten/src/README.md @@ -10,7 +10,7 @@ ``` - **abort** ✅ 🔥     [:top:](#host-apis) ```rust - fn abort(message: u32, ctx: &mut Ctx) + fn abort(ctx: &mut Ctx, message: u32, ) ``` - **abort_on_cannot_grow_memory** ✅     [:top:](#host-apis) ```rust @@ -28,11 +28,11 @@ - **\_getenv** ✅     [:top:](#host-apis) ```rust - fn _getenv(name: c_int, ctx: &mut Ctx) + fn _getenv(ctx: &mut Ctx, name: c_int, ) ``` - **\_putenv** ✅     [:top:](#host-apis) ```rust - fn _putenv(name: c_int, ctx: &mut Ctx) + fn _putenv(ctx: &mut Ctx, name: c_int, ) ``` - **\_setenv** ✅     [:top:](#host-apis) ```rust @@ -40,7 +40,7 @@ ``` - **\_unsetenv** ✅     [:top:](#host-apis) ```rust - fn _unsetenv(name: c_int, ctx: &mut Ctx) + fn _unsetenv(ctx: &mut Ctx, name: c_int, ) ``` ###### THREAD @@ -70,7 +70,7 @@ - **\_emscripten_memcpy_big** ✅ 🔥     [:top:](#host-apis) ```rust - fn _emscripten_memcpy_big(dest: u32, src: u32, len: u32, ctx: &mut Ctx) -> u32 + fn _emscripten_memcpy_big(ctx: &mut Ctx, dest: u32, src: u32, len: u32, ) -> u32 ``` - **enlarge_memory** ✅     [:top:](#host-apis) ```rust @@ -78,7 +78,7 @@ ``` - **get_total_memory** ✅     [:top:](#host-apis) ```rust - fn get_total_memory(ctx: &mut Ctx) -> u32 + fn get_total_memory(ctx: &mut Ctx, ) -> u32 ``` ###### TIMING @@ -337,7 +337,7 @@ ``` - **open** (\_\_\_syscall5) ✅ ❗️ 🔥     [:top:](#host-apis) ```rust - fn open(path: u32, flags: c_int, mode: c_int, ctx: &mut Ctx) -> c_int + fn open(ctx: &mut Ctx, path: u32, flags: c_int, mode: c_int, ) -> c_int ``` - **openat** (\_\_\_syscall295)     [:top:](#host-apis) ```rust @@ -385,7 +385,7 @@ ``` - **read** (\_\_\_syscall3) ✅ ❗️     [:top:](#host-apis) ```rust - fn read(fd: c_int, buf: u32, count: size_t, ctx: &mut Ctx) -> ssize_t + fn read(ctx: &mut Ctx, fd: c_int, buf: u32, count: size_t, ) -> ssize_t ``` - **readlink** (\_\_\_syscall85)     [:top:](#host-apis) ```rust diff --git a/lib/emscripten/src/env/mod.rs b/lib/emscripten/src/env/mod.rs index 50b7a48f964..89f55fd0948 100644 --- a/lib/emscripten/src/env/mod.rs +++ b/lib/emscripten/src/env/mod.rs @@ -14,16 +14,16 @@ use crate::{allocate_on_stack, EmscriptenData}; use std::os::raw::c_int; use wasmer_runtime_core::vm::Ctx; -pub fn _getaddrinfo(_one: i32, _two: i32, _three: i32, _four: i32, _ctx: &mut Ctx) -> i32 { +pub fn _getaddrinfo(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32, _four: i32) -> i32 { debug!("emscripten::_getaddrinfo"); -1 } -pub fn call_malloc(size: u32, ctx: &mut Ctx) -> u32 { +pub fn call_malloc(ctx: &mut Ctx, size: u32) -> u32 { get_emscripten_data(ctx).malloc.call(size).unwrap() } -pub fn call_memalign(alignment: u32, size: u32, ctx: &mut Ctx) -> u32 { +pub fn call_memalign(ctx: &mut Ctx, alignment: u32, size: u32) -> u32 { if let Some(memalign) = &get_emscripten_data(ctx).memalign { memalign.call(alignment, size).unwrap() } else { @@ -31,7 +31,7 @@ pub fn call_memalign(alignment: u32, size: u32, ctx: &mut Ctx) -> u32 { } } -pub fn call_memset(pointer: u32, value: u32, size: u32, ctx: &mut Ctx) -> u32 { +pub fn call_memset(ctx: &mut Ctx, pointer: u32, value: u32, size: u32) -> u32 { get_emscripten_data(ctx) .memset .call(pointer, value, size) @@ -48,16 +48,16 @@ pub fn _getpagesize(_ctx: &mut Ctx) -> u32 { } #[allow(clippy::cast_ptr_alignment)] -pub fn ___build_environment(environ: c_int, ctx: &mut Ctx) { +pub fn ___build_environment(ctx: &mut Ctx, 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; unsafe { let (pool_offset, _pool_slice): (u32, &mut [u8]) = - allocate_on_stack(TOTAL_ENV_SIZE as u32, ctx); + allocate_on_stack(ctx, TOTAL_ENV_SIZE as u32); let (env_offset, _env_slice): (u32, &mut [u8]) = - allocate_on_stack((MAX_ENV_VALUES * 4) as u32, ctx); + 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 mut _pool_ptr = emscripten_memory_pointer!(ctx.memory(0), pool_offset) as *mut c_int; *env_ptr = pool_offset as i32; @@ -70,7 +70,7 @@ pub fn ___build_environment(environ: c_int, ctx: &mut Ctx) { // }; } -pub fn ___assert_fail(a: c_int, b: c_int, c: c_int, d: c_int, _ctx: &mut Ctx) { +pub fn ___assert_fail(_ctx: &mut Ctx, 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 diff --git a/lib/emscripten/src/env/unix/mod.rs b/lib/emscripten/src/env/unix/mod.rs index 55fe0c0004d..5c4ef3d3a06 100644 --- a/lib/emscripten/src/env/unix/mod.rs +++ b/lib/emscripten/src/env/unix/mod.rs @@ -14,7 +14,7 @@ use wasmer_runtime_core::vm::Ctx; // #[no_mangle] /// emscripten: _getenv // (name: *const char) -> *const c_char; -pub fn _getenv(name: i32, ctx: &mut Ctx) -> u32 { +pub fn _getenv(ctx: &mut Ctx, name: i32) -> u32 { debug!("emscripten::_getenv"); let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; @@ -30,7 +30,7 @@ pub fn _getenv(name: i32, ctx: &mut Ctx) -> u32 { } /// emscripten: _setenv // (name: *const char, name: *const value, overwrite: int); -pub fn _setenv(name: c_int, value: c_int, overwrite: c_int, ctx: &mut Ctx) -> c_int { +pub fn _setenv(ctx: &mut Ctx, 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; @@ -43,7 +43,7 @@ pub fn _setenv(name: c_int, value: c_int, overwrite: c_int, ctx: &mut Ctx) -> c_ } /// emscripten: _putenv // (name: *const char); -pub fn _putenv(name: c_int, ctx: &mut Ctx) -> c_int { +pub fn _putenv(ctx: &mut Ctx, name: c_int) -> c_int { debug!("emscripten::_putenv"); let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; @@ -54,7 +54,7 @@ pub fn _putenv(name: c_int, ctx: &mut Ctx) -> c_int { } /// emscripten: _unsetenv // (name: *const char); -pub fn _unsetenv(name: c_int, ctx: &mut Ctx) -> c_int { +pub fn _unsetenv(ctx: &mut Ctx, name: c_int) -> c_int { debug!("emscripten::_unsetenv"); let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; @@ -65,7 +65,7 @@ pub fn _unsetenv(name: c_int, ctx: &mut Ctx) -> c_int { } #[allow(clippy::cast_ptr_alignment)] -pub fn _getpwnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { +pub fn _getpwnam(ctx: &mut Ctx, name_ptr: c_int) -> c_int { debug!("emscripten::_getpwnam {}", name_ptr); #[repr(C)] @@ -86,7 +86,7 @@ pub fn _getpwnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { unsafe { let passwd = &*libc_getpwnam(name.as_ptr()); - let passwd_struct_offset = call_malloc(mem::size_of::() as _, ctx); + let passwd_struct_offset = call_malloc(ctx, mem::size_of::() as _); let passwd_struct_ptr = emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd; @@ -103,7 +103,7 @@ pub fn _getpwnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { } #[allow(clippy::cast_ptr_alignment)] -pub fn _getgrnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { +pub fn _getgrnam(ctx: &mut Ctx, name_ptr: c_int) -> c_int { debug!("emscripten::_getgrnam {}", name_ptr); #[repr(C)] @@ -121,7 +121,7 @@ pub fn _getgrnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { unsafe { let group = &*libc_getgrnam(name.as_ptr()); - let group_struct_offset = call_malloc(mem::size_of::() as _, ctx); + let group_struct_offset = call_malloc(ctx, mem::size_of::() as _); let group_struct_ptr = emscripten_memory_pointer!(ctx.memory(0), group_struct_offset) as *mut GuestGroup; @@ -134,7 +134,7 @@ pub fn _getgrnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { } } -pub fn _sysconf(name: c_int, _ctx: &mut Ctx) -> i32 { +pub fn _sysconf(_ctx: &mut Ctx, 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 diff --git a/lib/emscripten/src/env/windows/mod.rs b/lib/emscripten/src/env/windows/mod.rs index b4171e13ccf..b4ee6ecff35 100644 --- a/lib/emscripten/src/env/windows/mod.rs +++ b/lib/emscripten/src/env/windows/mod.rs @@ -16,7 +16,7 @@ extern "C" { // #[no_mangle] /// emscripten: _getenv // (name: *const char) -> *const c_char; -pub fn _getenv(name: u32, ctx: &mut Ctx) -> u32 { +pub fn _getenv(ctx: &mut Ctx, name: u32) -> u32 { debug!("emscripten::_getenv"); let name_string = read_string_from_wasm(ctx.memory(0), name); debug!("=> name({:?})", name_string); @@ -28,7 +28,7 @@ pub fn _getenv(name: u32, ctx: &mut Ctx) -> u32 { } /// emscripten: _setenv // (name: *const char, name: *const value, overwrite: int); -pub fn _setenv(name: u32, value: u32, overwrite: u32, ctx: &mut Ctx) -> c_int { +pub fn _setenv(ctx: &mut Ctx, name: u32, value: u32, overwrite: u32) -> c_int { debug!("emscripten::_setenv"); let name_addr = emscripten_memory_pointer!(ctx.memory(0), name); let value_addr = emscripten_memory_pointer!(ctx.memory(0), value); @@ -44,7 +44,7 @@ pub fn _setenv(name: u32, value: u32, overwrite: u32, ctx: &mut Ctx) -> c_int { } /// emscripten: _putenv // (name: *const char); -pub fn _putenv(name: c_int, ctx: &mut Ctx) -> c_int { +pub fn _putenv(ctx: &mut Ctx, name: c_int) -> c_int { debug!("emscripten::_putenv"); let name_addr = emscripten_memory_pointer!(ctx.memory(0), name) as *const c_char; @@ -54,7 +54,7 @@ pub fn _putenv(name: c_int, ctx: &mut Ctx) -> c_int { } /// emscripten: _unsetenv // (name: *const char); -pub fn _unsetenv(name: u32, ctx: &mut Ctx) -> c_int { +pub fn _unsetenv(ctx: &mut Ctx, name: u32) -> c_int { debug!("emscripten::_unsetenv"); let name_addr = emscripten_memory_pointer!(ctx.memory(0), name); let name = read_string_from_wasm(ctx.memory(0), name); @@ -67,7 +67,7 @@ pub fn _unsetenv(name: u32, ctx: &mut Ctx) -> c_int { } #[allow(clippy::cast_ptr_alignment)] -pub fn _getpwnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { +pub fn _getpwnam(ctx: &mut Ctx, name_ptr: c_int) -> c_int { debug!("emscripten::_getpwnam {}", name_ptr); #[repr(C)] @@ -83,7 +83,7 @@ pub fn _getpwnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { // stub this in windows as it is not valid unsafe { - let passwd_struct_offset = call_malloc(mem::size_of::() as _, ctx); + let passwd_struct_offset = call_malloc(ctx, mem::size_of::() as _); let passwd_struct_ptr = emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd; (*passwd_struct_ptr).pw_name = 0; @@ -99,7 +99,7 @@ pub fn _getpwnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { } #[allow(clippy::cast_ptr_alignment)] -pub fn _getgrnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { +pub fn _getgrnam(ctx: &mut Ctx, name_ptr: c_int) -> c_int { debug!("emscripten::_getgrnam {}", name_ptr); #[repr(C)] @@ -112,7 +112,7 @@ pub fn _getgrnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { // stub the group struct as it is not supported on windows unsafe { - let group_struct_offset = call_malloc(mem::size_of::() as _, ctx); + let group_struct_offset = call_malloc(ctx, 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 = 0; @@ -123,7 +123,7 @@ pub fn _getgrnam(name_ptr: c_int, ctx: &mut Ctx) -> c_int { } } -pub fn _sysconf(name: c_int, _ctx: &mut Ctx) -> c_long { +pub fn _sysconf(_ctx: &mut Ctx, name: c_int) -> c_long { debug!("emscripten::_sysconf {}", name); // stub because sysconf is not valid on windows 0 diff --git a/lib/emscripten/src/errno.rs b/lib/emscripten/src/errno.rs index 64793281047..06267e21018 100644 --- a/lib/emscripten/src/errno.rs +++ b/lib/emscripten/src/errno.rs @@ -1,7 +1,7 @@ // use std::collections::HashMap; use wasmer_runtime_core::vm::Ctx; -pub fn ___seterrno(value: i32, _ctx: &mut Ctx) { +pub fn ___seterrno(_ctx: &mut Ctx, 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 b1814e10758..b0aab78dd93 100644 --- a/lib/emscripten/src/exception.rs +++ b/lib/emscripten/src/exception.rs @@ -3,14 +3,14 @@ use super::process::_abort; use wasmer_runtime_core::vm::Ctx; /// emscripten: ___cxa_allocate_exception -pub fn ___cxa_allocate_exception(size: u32, ctx: &mut Ctx) -> u32 { +pub fn ___cxa_allocate_exception(ctx: &mut Ctx, size: u32) -> u32 { debug!("emscripten::___cxa_allocate_exception"); - env::call_malloc(size as _, ctx) + env::call_malloc(ctx, size as _) } /// emscripten: ___cxa_throw /// TODO: We don't have support for exceptions yet -pub fn ___cxa_throw(_ptr: u32, _ty: u32, _destructor: u32, ctx: &mut Ctx) { +pub fn ___cxa_throw(ctx: &mut Ctx, _ptr: u32, _ty: u32, _destructor: u32) { debug!("emscripten::___cxa_throw"); _abort(ctx); } diff --git a/lib/emscripten/src/io/unix.rs b/lib/emscripten/src/io/unix.rs index a7c26a196d7..38324ac1ecc 100644 --- a/lib/emscripten/src/io/unix.rs +++ b/lib/emscripten/src/io/unix.rs @@ -3,12 +3,12 @@ use libc::printf as _printf; use wasmer_runtime_core::vm::Ctx; /// putchar -pub fn putchar(chr: i32, ctx: &mut Ctx) { +pub fn putchar(ctx: &mut Ctx, chr: i32) { unsafe { libc::putchar(chr) }; } /// printf -pub fn printf(memory_offset: i32, extra: i32, ctx: &mut Ctx) -> i32 { +pub fn printf(ctx: &mut Ctx, memory_offset: i32, extra: i32) -> i32 { debug!("emscripten::printf {}, {}", memory_offset, extra); unsafe { let addr = emscripten_memory_pointer!(ctx.memory(0), memory_offset) as _; diff --git a/lib/emscripten/src/io/windows.rs b/lib/emscripten/src/io/windows.rs index b85d9c54a3e..903d0590018 100644 --- a/lib/emscripten/src/io/windows.rs +++ b/lib/emscripten/src/io/windows.rs @@ -15,12 +15,12 @@ use wasmer_runtime_core::vm::Ctx; //} /// putchar -pub fn putchar(chr: i32, ctx: &mut Ctx) { +pub fn putchar(ctx: &mut Ctx, chr: i32) { unsafe { libc::putchar(chr) }; } /// printf -pub fn printf(memory_offset: i32, extra: i32, ctx: &mut Ctx) -> i32 { +pub fn printf(ctx: &mut Ctx, memory_offset: i32, extra: i32) -> i32 { debug!("emscripten::printf {}, {}", memory_offset, extra); // unsafe { // let addr = emscripten_memory_pointer!(ctx.memory(0), memory_offset) as _; diff --git a/lib/emscripten/src/jmp.rs b/lib/emscripten/src/jmp.rs index 0c63593aa91..c5b54f02b12 100644 --- a/lib/emscripten/src/jmp.rs +++ b/lib/emscripten/src/jmp.rs @@ -4,7 +4,7 @@ use std::cell::UnsafeCell; use wasmer_runtime_core::vm::Ctx; /// setjmp -pub fn __setjmp(env_addr: u32, ctx: &mut Ctx) -> c_int { +pub fn __setjmp(ctx: &mut Ctx, env_addr: u32) -> c_int { debug!("emscripten::__setjmp (setjmp)"); unsafe { // Rather than using the env as the holder of the jump buffer pointer, @@ -15,7 +15,7 @@ pub fn __setjmp(env_addr: u32, ctx: &mut Ctx) -> c_int { let jump_buf: UnsafeCell<[u32; 27]> = UnsafeCell::new([0; 27]); let jumps = &mut get_emscripten_data(ctx).jumps; let result = setjmp(jump_buf.get() as _); - // We set the jump index to be the last value of jumps + // We set the jump index to be the last 3value of jumps *jump_index = jumps.len() as _; // We hold the reference of the jump buffer jumps.push(jump_buf); @@ -25,7 +25,7 @@ pub fn __setjmp(env_addr: u32, ctx: &mut Ctx) -> c_int { /// longjmp #[allow(unreachable_code)] -pub fn __longjmp(env_addr: u32, val: c_int, ctx: &mut Ctx) { +pub fn __longjmp(ctx: &mut Ctx, env_addr: u32, val: c_int) { debug!("emscripten::__longjmp (longmp)"); unsafe { // We retrieve the jump index from the env address diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 0fb07d72c15..999addb2e8f 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -141,7 +141,7 @@ pub fn run_emscripten_instance( let num_params = main_func.signature().params().len(); let _result = match num_params { 2 => { - let (argc, argv) = store_module_arguments(path, args, instance.context_mut()); + let (argc, argv) = store_module_arguments(instance.context_mut(), path, args); instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?; } 0 => { @@ -158,17 +158,17 @@ pub fn run_emscripten_instance( Ok(()) } -fn store_module_arguments(path: &str, args: Vec<&str>, ctx: &mut Ctx) -> (u32, u32) { +fn store_module_arguments(ctx: &mut Ctx, path: &str, args: Vec<&str>) -> (u32, u32) { let argc = args.len() + 1; let mut args_slice = vec![0; argc]; - args_slice[0] = unsafe { allocate_cstr_on_stack(path, ctx).0 }; + args_slice[0] = unsafe { allocate_cstr_on_stack(ctx, path).0 }; for (slot, arg) in args_slice[1..argc].iter_mut().zip(args.iter()) { - *slot = unsafe { allocate_cstr_on_stack(&arg, ctx).0 }; + *slot = unsafe { allocate_cstr_on_stack(ctx, &arg).0 }; } let (argv_offset, argv_slice): (_, &mut [u32]) = - unsafe { allocate_on_stack(((argc + 1) * 4) as u32, ctx) }; + unsafe { allocate_on_stack(ctx, ((argc + 1) * 4) as u32) }; assert!(!argv_slice.is_empty()); for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) { *slot = *arg diff --git a/lib/emscripten/src/linking.rs b/lib/emscripten/src/linking.rs index e61b9d1b1f1..831712e9115 100644 --- a/lib/emscripten/src/linking.rs +++ b/lib/emscripten/src/linking.rs @@ -3,19 +3,19 @@ use wasmer_runtime_core::vm::Ctx; // TODO: Need to implement. /// emscripten: dlopen(filename: *const c_char, flag: c_int) -> *mut c_void -pub fn _dlopen(_filename: u32, _flag: u32, _ctx: &mut Ctx) -> i32 { +pub fn _dlopen(_ctx: &mut Ctx, _filename: u32, _flag: u32) -> i32 { debug!("emscripten::_dlopen"); -1 } /// emscripten: dlclose(handle: *mut c_void) -> c_int -pub fn _dlclose(_filename: u32, _ctx: &mut Ctx) -> i32 { +pub fn _dlclose(_ctx: &mut Ctx, _filename: u32) -> i32 { debug!("emscripten::_dlclose"); -1 } /// emscripten: dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void -pub fn _dlsym(_filepath: u32, _symbol: u32, _ctx: &mut Ctx) -> i32 { +pub fn _dlsym(_ctx: &mut Ctx, _filepath: u32, _symbol: u32) -> i32 { debug!("emscripten::_dlsym"); -1 } diff --git a/lib/emscripten/src/lock.rs b/lib/emscripten/src/lock.rs index 005a1baedd8..45f09581f9e 100644 --- a/lib/emscripten/src/lock.rs +++ b/lib/emscripten/src/lock.rs @@ -2,16 +2,16 @@ use libc::c_int; use wasmer_runtime_core::vm::Ctx; // NOTE: Not implemented by Emscripten -pub fn ___lock(what: c_int, _ctx: &mut Ctx) { +pub fn ___lock(_ctx: &mut Ctx, what: c_int) { debug!("emscripten::___lock {}", what); } // NOTE: Not implemented by Emscripten -pub fn ___unlock(what: c_int, _ctx: &mut Ctx) { +pub fn ___unlock(_ctx: &mut Ctx, what: c_int) { debug!("emscripten::___unlock {}", what); } // NOTE: Not implemented by Emscripten -pub fn ___wait(_which: u32, _varargs: u32, _three: u32, _four: u32, _ctx: &mut Ctx) { +pub fn ___wait(_ctx: &mut Ctx, _which: u32, _varargs: u32, _three: u32, _four: u32) { debug!("emscripten::___wait"); } diff --git a/lib/emscripten/src/math.rs b/lib/emscripten/src/math.rs index 16f3ed419fd..a60966a1c4a 100644 --- a/lib/emscripten/src/math.rs +++ b/lib/emscripten/src/math.rs @@ -1,23 +1,23 @@ use wasmer_runtime_core::vm::Ctx; /// emscripten: _llvm_log10_f64 -pub fn _llvm_log10_f64(value: f64, _ctx: &mut Ctx) -> f64 { +pub fn _llvm_log10_f64(_ctx: &mut Ctx, value: f64) -> f64 { debug!("emscripten::_llvm_log10_f64"); value.log10() } /// emscripten: _llvm_log2_f64 -pub fn _llvm_log2_f64(value: f64, _ctx: &mut Ctx) -> f64 { +pub fn _llvm_log2_f64(_ctx: &mut Ctx, value: f64) -> f64 { debug!("emscripten::_llvm_log2_f64"); value.log2() } -pub fn _llvm_log10_f32(_value: f64, _ctx: &mut Ctx) -> f64 { +pub fn _llvm_log10_f32(_ctx: &mut Ctx, _value: f64) -> f64 { debug!("emscripten::_llvm_log10_f32"); -1.0 } -pub fn _llvm_log2_f32(_value: f64, _ctx: &mut Ctx) -> f64 { +pub fn _llvm_log2_f32(_ctx: &mut Ctx, _value: f64) -> f64 { debug!("emscripten::_llvm_log10_f32"); -1.0 } @@ -28,12 +28,12 @@ pub fn _emscripten_random(_ctx: &mut Ctx) -> f64 { } // emscripten: f64-rem -pub fn f64_rem(x: f64, y: f64, _ctx: &mut Ctx) -> f64 { +pub fn f64_rem(_ctx: &mut Ctx, x: f64, y: f64) -> f64 { debug!("emscripten::f64-rem"); x % y } // emscripten: global.Math pow -pub fn pow(x: f64, y: f64, _ctx: &mut Ctx) -> f64 { +pub fn pow(_ctx: &mut Ctx, x: f64, y: f64) -> f64 { x.powf(y) } diff --git a/lib/emscripten/src/memory.rs b/lib/emscripten/src/memory.rs index e174e023d1d..c4540dff08e 100644 --- a/lib/emscripten/src/memory.rs +++ b/lib/emscripten/src/memory.rs @@ -3,7 +3,7 @@ use libc::{c_int, c_void, memcpy, size_t}; use wasmer_runtime_core::vm::Ctx; /// emscripten: _emscripten_memcpy_big -pub fn _emscripten_memcpy_big(dest: u32, src: u32, len: u32, ctx: &mut Ctx) -> u32 { +pub fn _emscripten_memcpy_big(ctx: &mut Ctx, dest: u32, src: u32, len: u32) -> u32 { debug!( "emscripten::_emscripten_memcpy_big {}, {}, {}", dest, src, len @@ -35,12 +35,12 @@ pub fn enlarge_memory(_ctx: &mut Ctx) -> u32 { /// emscripten: abortOnCannotGrowMemory pub fn abort_on_cannot_grow_memory(ctx: &mut Ctx) -> u32 { debug!("emscripten::abort_on_cannot_grow_memory"); - abort_with_message("Cannot enlarge memory arrays!", ctx); + abort_with_message(ctx, "Cannot enlarge memory arrays!"); 0 } /// emscripten: ___map_file -pub fn ___map_file(_one: u32, _two: u32, _ctx: &mut Ctx) -> c_int { +pub fn ___map_file(_ctx: &mut Ctx, _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/nullfunc.rs b/lib/emscripten/src/nullfunc.rs index 253edf8f6b7..7fb44ab11b6 100644 --- a/lib/emscripten/src/nullfunc.rs +++ b/lib/emscripten/src/nullfunc.rs @@ -1,67 +1,67 @@ use super::process::abort_with_message; use wasmer_runtime_core::vm::Ctx; -pub fn nullfunc_i(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_i(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_i {}", x); - abort_with_message("Invalid function pointer called with signature 'i'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'i'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_ii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_ii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_ii {}", x); - abort_with_message("Invalid function pointer called with signature 'ii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'ii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_iii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_iii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_iii {}", x); - abort_with_message("Invalid function pointer called with signature 'iii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'iii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_iiii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_iiii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_iiii {}", x); - abort_with_message("Invalid function pointer called with signature 'iiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'iiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_iiiii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_iiiii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_iiiii {}", x); - abort_with_message("Invalid function pointer called with signature 'iiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'iiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_iiiiii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_iiiiii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_iiiiii {}", x); - abort_with_message("Invalid function pointer called with signature 'iiiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'iiiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_v(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_v(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_v {}", x); - abort_with_message("Invalid function pointer called with signature 'v'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'v'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_vi(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_vi(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_vi {}", x); - abort_with_message("Invalid function pointer called with signature 'vi'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'vi'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_vii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_vii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_vii {}", x); - abort_with_message("Invalid function pointer called with signature 'vii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'vii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_viii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_viii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_viii {}", x); - abort_with_message("Invalid function pointer called with signature 'viii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'viii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_viiii(x: u32, ctx: &mut Ctx) { +pub fn nullfunc_viiii(ctx: &mut Ctx, x: u32) { debug!("emscripten::nullfunc_viiii {}", x); - abort_with_message("Invalid function pointer called with signature 'viiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'viiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_viiiii(_x: u32, ctx: &mut Ctx) { +pub fn nullfunc_viiiii(ctx: &mut Ctx, _x: u32) { debug!("emscripten::nullfunc_viiiii"); - abort_with_message("Invalid function pointer called with signature 'viiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'viiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } -pub fn nullfunc_viiiiii(_x: u32, ctx: &mut Ctx) { +pub fn nullfunc_viiiiii(ctx: &mut Ctx, _x: u32) { debug!("emscripten::nullfunc_viiiiii"); - abort_with_message("Invalid function pointer called with signature 'viiiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)", ctx); + abort_with_message(ctx, "Invalid function pointer called with signature 'viiiiii'. Perhaps this is an invalid value (e.g. caused by calling a virtual method on a NULL pointer)? Or calling a function with an incorrect type, which will fail? (it is worth building your source files with -Werror (warnings are errors), as warnings can indicate undefined behavior which can cause this)"); } diff --git a/lib/emscripten/src/process.rs b/lib/emscripten/src/process.rs index d6be7a78bb4..8d1dddb4829 100644 --- a/lib/emscripten/src/process.rs +++ b/lib/emscripten/src/process.rs @@ -9,7 +9,7 @@ type pid_t = c_int; use std::ffi::CStr; use wasmer_runtime_core::vm::Ctx; -pub fn abort_with_message(message: &str, ctx: &mut Ctx) { +pub fn abort_with_message(ctx: &mut Ctx, message: &str) { debug!("emscripten::abort_with_message"); println!("{}", message); _abort(ctx); @@ -34,19 +34,19 @@ pub fn _endgrent(_ctx: &mut Ctx) { debug!("emscripten::_endgrent"); } -pub fn _execve(_one: i32, _two: i32, _three: i32, _ctx: &mut Ctx) -> i32 { +pub fn _execve(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_execve"); -1 } #[allow(unreachable_code)] -pub fn _exit(status: c_int, _ctx: &mut Ctx) { +pub fn _exit(_ctx: &mut Ctx, status: c_int) { // -> ! debug!("emscripten::_exit {}", status); unsafe { exit(status) } } -pub fn em_abort(message: u32, ctx: &mut Ctx) { +pub fn em_abort(ctx: &mut Ctx, message: u32) { debug!("emscripten::em_abort {}", message); let message_addr = emscripten_memory_pointer!(ctx.memory(0), message) as *mut c_char; unsafe { @@ -54,11 +54,11 @@ pub fn em_abort(message: u32, ctx: &mut Ctx) { .to_str() .unwrap_or("Unexpected abort"); - abort_with_message(message, ctx); + abort_with_message(ctx, message); } } -pub fn _kill(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn _kill(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::_kill"); -1 } @@ -73,26 +73,26 @@ pub fn _llvm_stacksave(_ctx: &mut Ctx) -> i32 { -1 } -pub fn _llvm_stackrestore(_one: i32, _ctx: &mut Ctx) { +pub fn _llvm_stackrestore(_ctx: &mut Ctx, _one: i32) { debug!("emscripten::_llvm_stackrestore"); } -pub fn _raise(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _raise(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_raise"); -1 } -pub fn _sem_init(_one: i32, _two: i32, _three: i32, _ctx: &mut Ctx) -> i32 { +pub fn _sem_init(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_sem_init"); -1 } -pub fn _sem_post(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _sem_post(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_sem_post"); -1 } -pub fn _sem_wait(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _sem_wait(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_sem_post"); -1 } @@ -107,53 +107,53 @@ pub fn _setgrent(_ctx: &mut Ctx) { debug!("emscripten::_setgrent"); } -pub fn _setgroups(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn _setgroups(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::_setgroups"); -1 } -pub fn _setitimer(_one: i32, _two: i32, _three: i32, _ctx: &mut Ctx) -> i32 { +pub fn _setitimer(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_setitimer"); -1 } -pub fn _usleep(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _usleep(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_usleep"); -1 } -pub fn _utimes(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn _utimes(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::_utimes"); -1 } -pub fn _waitpid(_one: i32, _two: i32, _three: i32, _ctx: &mut Ctx) -> i32 { +pub fn _waitpid(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_waitpid"); -1 } -pub fn abort_stack_overflow(_what: c_int, ctx: &mut Ctx) { +pub fn abort_stack_overflow(ctx: &mut Ctx, _what: c_int) { debug!("emscripten::abort_stack_overflow"); // TODO: Message incomplete. Need to finish em runtime data first abort_with_message( - "Stack overflow! Attempted to allocate some bytes on the stack", ctx, + "Stack overflow! Attempted to allocate some bytes on the stack", ); } pub fn _llvm_trap(ctx: &mut Ctx) { debug!("emscripten::_llvm_trap"); - abort_with_message("abort!", ctx); + abort_with_message(ctx, "abort!"); } -pub fn _system(_one: i32, _ctx: &mut Ctx) -> c_int { +pub fn _system(_ctx: &mut Ctx, _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"); return EAGAIN; } -pub fn _popen(_one: i32, _two: i32, _ctx: &mut Ctx) -> c_int { +pub fn _popen(_ctx: &mut Ctx, _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/signal.rs b/lib/emscripten/src/signal.rs index 1a6d4307828..f7657630b92 100644 --- a/lib/emscripten/src/signal.rs +++ b/lib/emscripten/src/signal.rs @@ -2,7 +2,7 @@ use wasmer_runtime_core::vm::Ctx; #[allow(clippy::cast_ptr_alignment)] -pub fn _sigemptyset(set: u32, ctx: &mut Ctx) -> i32 { +pub fn _sigemptyset(ctx: &mut Ctx, set: u32) -> i32 { debug!("emscripten::_sigemptyset"); let set_addr = emscripten_memory_pointer!(ctx.memory(0), set) as *mut u32; unsafe { @@ -11,13 +11,13 @@ pub fn _sigemptyset(set: u32, ctx: &mut Ctx) -> i32 { 0 } -pub fn _sigaction(signum: u32, act: u32, oldact: u32, _ctx: &mut Ctx) -> i32 { +pub fn _sigaction(_ctx: &mut Ctx, signum: u32, act: u32, oldact: u32) -> i32 { debug!("emscripten::_sigaction {}, {}, {}", signum, act, oldact); 0 } #[allow(clippy::cast_ptr_alignment)] -pub fn _sigaddset(set: u32, signum: u32, ctx: &mut Ctx) -> i32 { +pub fn _sigaddset(ctx: &mut Ctx, set: u32, signum: u32) -> i32 { debug!("emscripten::_sigaddset {}, {}", set, signum); let set_addr = emscripten_memory_pointer!(ctx.memory(0), set) as *mut u32; unsafe { @@ -26,17 +26,17 @@ pub fn _sigaddset(set: u32, signum: u32, ctx: &mut Ctx) -> i32 { 0 } -pub fn _sigsuspend(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _sigsuspend(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_sigsuspend"); -1 } -pub fn _sigprocmask(_one: i32, _two: i32, _three: i32, _ctx: &mut Ctx) -> i32 { +pub fn _sigprocmask(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32) -> i32 { debug!("emscripten::_sigprocmask"); 0 } -pub fn _signal(sig: u32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn _signal(_ctx: &mut Ctx, 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 edae4860ce8..38c4157d1e6 100644 --- a/lib/emscripten/src/syscalls/mod.rs +++ b/lib/emscripten/src/syscalls/mod.rs @@ -70,7 +70,7 @@ use libc::SO_NOSIGPIPE; const SO_NOSIGPIPE: c_int = 0; /// exit -pub fn ___syscall1(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) { +pub fn ___syscall1(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) { debug!("emscripten::___syscall1 (exit) {}", which); let status: i32 = varargs.get(ctx); unsafe { @@ -79,7 +79,7 @@ pub fn ___syscall1(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) { } /// read -pub fn ___syscall3(which: i32, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { +pub fn ___syscall3(ctx: &mut Ctx, which: i32, mut varargs: VarArgs) -> i32 { // -> ssize_t debug!("emscripten::___syscall3 (read) {}", which); let fd: i32 = varargs.get(ctx); @@ -93,7 +93,7 @@ pub fn ___syscall3(which: i32, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { } /// write -pub fn ___syscall4(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall4(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall4 (write) {}", which); let fd: i32 = varargs.get(ctx); let buf: u32 = varargs.get(ctx); @@ -104,7 +104,7 @@ pub fn ___syscall4(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { } /// open -pub fn ___syscall5(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall5(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall5 (open) {}", which); let pathname: u32 = varargs.get(ctx); let flags: i32 = varargs.get(ctx); @@ -120,7 +120,7 @@ pub fn ___syscall5(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { } /// close -pub fn ___syscall6(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall6(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall6 (close) {}", which); let fd: i32 = varargs.get(ctx); debug!("fd: {}", fd); @@ -128,7 +128,7 @@ pub fn ___syscall6(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { } // chdir -pub fn ___syscall12(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall12(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall12 (chdir) {}", which); let path_addr: i32 = varargs.get(ctx); unsafe { @@ -140,42 +140,42 @@ pub fn ___syscall12(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } } -pub fn ___syscall10(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall10(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall10"); -1 } -pub fn ___syscall15(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall15(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall15"); -1 } // getpid -pub fn ___syscall20(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall20(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall20 (getpid)"); unsafe { getpid() } } -pub fn ___syscall38(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall38(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall38"); -1 } // rmdir -pub fn ___syscall40(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall40(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall40 (rmdir)"); let pathname: u32 = varargs.get(ctx); let pathname_addr = emscripten_memory_pointer!(ctx.memory(0), pathname) as *const i8; unsafe { rmdir(pathname_addr) } } -pub fn ___syscall60(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall60(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall60"); -1 } // dup2 -pub fn ___syscall63(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall63(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall63 (dup2) {}", which); let src: i32 = varargs.get(ctx); @@ -185,43 +185,43 @@ pub fn ___syscall63(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // getppid -pub fn ___syscall64(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall64(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall64 (getppid)"); unsafe { getpid() } } -pub fn ___syscall66(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall66(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall66"); -1 } -pub fn ___syscall75(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall75(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall75"); -1 } -pub fn ___syscall85(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall85(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall85"); -1 } -pub fn ___syscall91(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall91(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall91"); -1 } -pub fn ___syscall97(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall97(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall97"); -1 } -pub fn ___syscall110(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall110(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall110"); -1 } // mmap2 -pub fn ___syscall192(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall192(ctx: &mut Ctx, 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); @@ -235,11 +235,11 @@ pub fn ___syscall192(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int ); if fd == -1 { - let ptr = env::call_memalign(16384, len, ctx); + let ptr = env::call_memalign(ctx, 16384, len); if ptr == 0 { return -1; } - env::call_memset(ptr, 0, len, ctx); + env::call_memset(ctx, ptr, 0, len); ptr as _ } else { -1 @@ -247,7 +247,7 @@ pub fn ___syscall192(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } /// lseek -pub fn ___syscall140(which: i32, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { +pub fn ___syscall140(ctx: &mut Ctx, which: i32, mut varargs: VarArgs) -> i32 { // -> c_int debug!("emscripten::___syscall140 (lseek) {}", which); let fd: i32 = varargs.get(ctx); @@ -259,7 +259,7 @@ pub fn ___syscall140(which: i32, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { /// readv #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall145(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { +pub fn ___syscall145(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> i32 { // -> ssize_t debug!("emscripten::___syscall145 (readv) {}", which); // let fd: i32 = varargs.get(ctx); @@ -302,7 +302,7 @@ pub fn ___syscall145(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { // writev #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall146(which: i32, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { +pub fn ___syscall146(ctx: &mut Ctx, which: i32, mut varargs: VarArgs) -> i32 { // -> ssize_t debug!("emscripten::___syscall146 (writev) {}", which); let fd: i32 = varargs.get(ctx); @@ -336,33 +336,33 @@ pub fn ___syscall146(which: i32, mut varargs: VarArgs, ctx: &mut Ctx) -> i32 { } } -pub fn ___syscall168(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall168(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall168"); -1 } -pub fn ___syscall191(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall191(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall191 - stub"); -1 } -pub fn ___syscall194(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall194(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall194 - stub"); -1 } -pub fn ___syscall196(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall196(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall194 - stub"); -1 } -pub fn ___syscall199(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall199(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall199 - stub"); -1 } // stat64 -pub fn ___syscall195(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall195(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall195 (stat64) {}", which); let pathname: u32 = varargs.get(ctx); let buf: u32 = varargs.get(ctx); @@ -382,7 +382,7 @@ pub fn ___syscall195(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // fstat64 -pub fn ___syscall197(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall197(ctx: &mut Ctx, 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); @@ -400,13 +400,13 @@ pub fn ___syscall197(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int 0 } -pub fn ___syscall220(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall220(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall220"); -1 } // fcntl64 -pub fn ___syscall221(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall221(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall221 (fcntl64) {}", which); // fcntl64 let _fd: i32 = varargs.get(ctx); @@ -417,33 +417,33 @@ pub fn ___syscall221(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } } -pub fn ___syscall268(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall268(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall268"); -1 } -pub fn ___syscall272(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall272(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall272"); -1 } -pub fn ___syscall295(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall295(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall295"); -1 } -pub fn ___syscall300(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall300(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall300"); -1 } -pub fn ___syscall334(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall334(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall334"); -1 } // prlimit64 -pub fn ___syscall340(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall340(ctx: &mut Ctx, 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); diff --git a/lib/emscripten/src/syscalls/unix.rs b/lib/emscripten/src/syscalls/unix.rs index 00b3b47e8fd..766f78b92b2 100644 --- a/lib/emscripten/src/syscalls/unix.rs +++ b/lib/emscripten/src/syscalls/unix.rs @@ -94,7 +94,7 @@ use libc::SO_NOSIGPIPE; const SO_NOSIGPIPE: c_int = 0; // chown -pub fn ___syscall212(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall212(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall212 (chown) {}", which); let pathname: u32 = varargs.get(ctx); @@ -107,7 +107,7 @@ pub fn ___syscall212(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // mkdir -pub fn ___syscall39(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall39(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall39 (mkdir) {}", which); let pathname: u32 = varargs.get(ctx); let mode: u32 = varargs.get(ctx); @@ -116,7 +116,7 @@ pub fn ___syscall39(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // getgid -pub fn ___syscall201(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall201(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall201 (getgid)"); unsafe { // Maybe fix: Emscripten returns 0 always @@ -125,7 +125,7 @@ pub fn ___syscall201(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { } // getgid32 -pub fn ___syscall202(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall202(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { // gid_t debug!("emscripten::___syscall202 (getgid32)"); unsafe { @@ -135,7 +135,7 @@ pub fn ___syscall202(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { } /// dup3 -pub fn ___syscall330(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> pid_t { +pub fn ___syscall330(ctx: &mut Ctx, _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); @@ -169,7 +169,7 @@ pub fn ___syscall330(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> pid_ } /// ioctl -pub fn ___syscall54(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall54(ctx: &mut Ctx, 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); @@ -212,7 +212,7 @@ pub fn ___syscall54(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int // socketcall #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall102(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall102(ctx: &mut Ctx, 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); @@ -464,7 +464,7 @@ pub fn ___syscall102(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // pread -pub fn ___syscall180(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall180(ctx: &mut Ctx, 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); @@ -481,7 +481,7 @@ pub fn ___syscall180(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // pwrite -pub fn ___syscall181(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall181(ctx: &mut Ctx, 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); @@ -503,7 +503,7 @@ pub fn ___syscall181(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int /// wait4 #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall114(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> pid_t { +pub fn ___syscall114(ctx: &mut Ctx, _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); @@ -521,7 +521,7 @@ pub fn ___syscall114(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> pid_ // select #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall142(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall142(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall142 (newselect) {}", which); let nfds: i32 = varargs.get(ctx); @@ -540,7 +540,7 @@ pub fn ___syscall142(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // setpgid -pub fn ___syscall57(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall57(ctx: &mut Ctx, 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); @@ -549,7 +549,7 @@ pub fn ___syscall57(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int /// uname // NOTE: Wondering if we should return custom utsname, like Emscripten. -pub fn ___syscall122(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall122(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall122 (uname) {}", which); let buf: u32 = varargs.get(ctx); debug!("=> buf: {}", buf); diff --git a/lib/emscripten/src/syscalls/windows.rs b/lib/emscripten/src/syscalls/windows.rs index 2b577fcac85..41c4cf7ebbc 100644 --- a/lib/emscripten/src/syscalls/windows.rs +++ b/lib/emscripten/src/syscalls/windows.rs @@ -6,13 +6,13 @@ use wasmer_runtime_core::vm::Ctx; type pid_t = c_int; // chown -pub fn ___syscall212(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall212(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall212 (chown) {}", which); -1 } // mkdir -pub fn ___syscall39(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall39(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall39 (mkdir) {}", which); let pathname: u32 = varargs.get(ctx); let mode: u32 = varargs.get(ctx); @@ -21,72 +21,72 @@ pub fn ___syscall39(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int } // getgid -pub fn ___syscall201(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall201(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::___syscall201 (getgid)"); -1 } // getgid32 -pub fn ___syscall202(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn ___syscall202(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { // gid_t debug!("emscripten::___syscall202 (getgid32)"); -1 } /// dup3 -pub fn ___syscall330(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> pid_t { +pub fn ___syscall330(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> pid_t { debug!("emscripten::___syscall330 (dup3)"); -1 } /// ioctl -pub fn ___syscall54(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall54(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall54 (ioctl) {}", which); -1 } // socketcall #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall102(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall102(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall102 (socketcall) {}", which); -1 } // pread -pub fn ___syscall180(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall180(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall180 (pread) {}", which); -1 } // pwrite -pub fn ___syscall181(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall181(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall181 (pwrite) {}", which); -1 } /// wait4 #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall114(_which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> pid_t { +pub fn ___syscall114(ctx: &mut Ctx, _which: c_int, mut varargs: VarArgs) -> pid_t { debug!("emscripten::___syscall114 (wait4)"); -1 } // select #[allow(clippy::cast_ptr_alignment)] -pub fn ___syscall142(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall142(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall142 (newselect) {}", which); -1 } // setpgid -pub fn ___syscall57(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall57(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall57 (setpgid) {}", which); -1 } /// uname // NOTE: Wondering if we should return custom utsname, like Emscripten. -pub fn ___syscall122(which: c_int, mut varargs: VarArgs, ctx: &mut Ctx) -> c_int { +pub fn ___syscall122(ctx: &mut Ctx, which: c_int, mut varargs: VarArgs) -> c_int { debug!("emscripten::___syscall122 (uname) {}", which); -1 } diff --git a/lib/emscripten/src/time.rs b/lib/emscripten/src/time.rs index 2ecdfe6a8da..8386a36aeba 100644 --- a/lib/emscripten/src/time.rs +++ b/lib/emscripten/src/time.rs @@ -38,7 +38,7 @@ const CLOCK_MONOTONIC_COARSE: clockid_t = 6; /// emscripten: _gettimeofday #[allow(clippy::cast_ptr_alignment)] -pub fn _gettimeofday(tp: c_int, tz: c_int, ctx: &mut Ctx) -> c_int { +pub fn _gettimeofday(ctx: &mut Ctx, tp: c_int, tz: c_int) -> c_int { debug!("emscripten::_gettimeofday {} {}", tp, tz); #[repr(C)] struct GuestTimeVal { @@ -63,7 +63,7 @@ pub fn _gettimeofday(tp: c_int, tz: c_int, ctx: &mut Ctx) -> c_int { /// emscripten: _clock_gettime #[allow(clippy::cast_ptr_alignment)] -pub fn _clock_gettime(clk_id: clockid_t, tp: c_int, ctx: &mut Ctx) -> c_int { +pub fn _clock_gettime(ctx: &mut Ctx, clk_id: clockid_t, tp: c_int) -> c_int { debug!("emscripten::_clock_gettime {} {}", clk_id, tp); // debug!("Memory {:?}", ctx.memory(0)[..]); #[repr(C)] @@ -94,9 +94,9 @@ pub fn _clock_gettime(clk_id: clockid_t, tp: c_int, ctx: &mut Ctx) -> c_int { } /// emscripten: ___clock_gettime -pub fn ___clock_gettime(clk_id: clockid_t, tp: c_int, ctx: &mut Ctx) -> c_int { +pub fn ___clock_gettime(ctx: &mut Ctx, clk_id: clockid_t, tp: c_int) -> c_int { debug!("emscripten::___clock_gettime {} {}", clk_id, tp); - _clock_gettime(clk_id, tp, ctx) + _clock_gettime(ctx, clk_id, tp) } /// emscripten: _clock @@ -106,22 +106,22 @@ pub fn _clock(_ctx: &mut Ctx) -> c_int { } /// emscripten: _difftime -pub fn _difftime(t0: u32, t1: u32, _ctx: &mut Ctx) -> f64 { +pub fn _difftime(_ctx: &mut Ctx, t0: u32, t1: u32) -> f64 { debug!("emscripten::_difftime"); (t0 - t1) as _ } -pub fn _gmtime_r(_one: i32, _two: i32, _ctx: &mut Ctx) -> i32 { +pub fn _gmtime_r(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 { debug!("emscripten::_gmtime_r"); -1 } -pub fn _mktime(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _mktime(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_mktime"); -1 } -pub fn _gmtime(_one: i32, _ctx: &mut Ctx) -> i32 { +pub fn _gmtime(_ctx: &mut Ctx, _one: i32) -> i32 { debug!("emscripten::_gmtime"); -1 } @@ -148,7 +148,7 @@ pub fn _tvset(_ctx: &mut Ctx) { /// formats time as a C string #[allow(clippy::cast_ptr_alignment)] -unsafe fn fmt_time(time: u32, ctx: &mut Ctx) -> *const c_char { +unsafe fn fmt_time(ctx: &mut Ctx, time: u32) -> *const c_char { let date = &*(emscripten_memory_pointer!(ctx.memory(0), time) as *mut guest_tm); let days = vec!["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; @@ -173,11 +173,11 @@ unsafe fn fmt_time(time: u32, ctx: &mut Ctx) -> *const c_char { } /// emscripten: _asctime -pub fn _asctime(time: u32, ctx: &mut Ctx) -> u32 { +pub fn _asctime(ctx: &mut Ctx, time: u32) -> u32 { debug!("emscripten::_asctime {}", time); unsafe { - let time_str_ptr = fmt_time(time, ctx); + let time_str_ptr = fmt_time(ctx, time); copy_cstr_into_wasm(ctx, time_str_ptr) // let c_str = emscripten_memory_pointer!(ctx.memory(0), res) as *mut i8; @@ -187,7 +187,7 @@ pub fn _asctime(time: u32, ctx: &mut Ctx) -> u32 { } /// emscripten: _asctime_r -pub fn _asctime_r(time: u32, buf: u32, ctx: &mut Ctx) -> u32 { +pub fn _asctime_r(ctx: &mut Ctx, time: u32, buf: u32) -> u32 { debug!("emscripten::_asctime_r {}, {}", time, buf); unsafe { @@ -195,8 +195,8 @@ pub fn _asctime_r(time: u32, buf: u32, ctx: &mut Ctx) -> 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(time, ctx); - write_to_buf(time_str_ptr, buf, 26, ctx) + let time_str_ptr = fmt_time(ctx, time); + write_to_buf(ctx, time_str_ptr, buf, 26) // let c_str = emscripten_memory_pointer!(ctx.memory(0), res) as *mut i8; // use std::ffi::CStr; @@ -206,7 +206,7 @@ pub fn _asctime_r(time: u32, buf: u32, ctx: &mut Ctx) -> u32 { /// emscripten: _localtime #[allow(clippy::cast_ptr_alignment)] -pub fn _localtime(time_p: u32, ctx: &mut Ctx) -> c_int { +pub fn _localtime(ctx: &mut Ctx, 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 @@ -219,7 +219,7 @@ pub fn _localtime(time_p: u32, ctx: &mut Ctx) -> c_int { let result_tm = time::at(timespec); unsafe { - let tm_struct_offset = env::call_malloc(mem::size_of::() as _, ctx); + 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; // debug!( @@ -244,7 +244,7 @@ pub fn _localtime(time_p: u32, ctx: &mut Ctx) -> c_int { } /// emscripten: _localtime_r #[allow(clippy::cast_ptr_alignment)] -pub fn _localtime_r(time_p: u32, result: u32, ctx: &mut Ctx) -> c_int { +pub fn _localtime_r(ctx: &mut Ctx, time_p: u32, result: u32) -> c_int { debug!("emscripten::_localtime_r {}", time_p); // NOTE: emscripten seems to want tzset() called in this function @@ -281,7 +281,7 @@ pub fn _localtime_r(time_p: u32, result: u32, ctx: &mut Ctx) -> c_int { /// emscripten: _time #[allow(clippy::cast_ptr_alignment)] -pub fn _time(time_p: u32, ctx: &mut Ctx) -> i32 { +pub fn _time(ctx: &mut Ctx, time_p: u32) -> i32 { debug!("emscripten::_time {}", time_p); unsafe { @@ -292,11 +292,11 @@ pub fn _time(time_p: u32, ctx: &mut Ctx) -> i32 { /// emscripten: _strftime pub fn _strftime( + _ctx: &mut Ctx, s_ptr: c_int, maxsize: u32, format_ptr: c_int, tm_ptr: c_int, - _ctx: &mut Ctx, ) -> i32 { debug!( "emscripten::_strftime {} {} {} {}", diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index fe3381d766b..92d51cf25bb 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -42,7 +42,7 @@ pub fn get_emscripten_memory_size(module: &Module) -> (Pages, Option) { (memory.minimum, memory.maximum) } -pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, ctx: &mut Ctx) -> u32 { +pub unsafe fn write_to_buf(ctx: &mut Ctx, string: *const c_char, buf: u32, max: u32) -> u32 { let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut c_char; for i in 0..max { @@ -56,7 +56,7 @@ pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, ctx: &mut pub unsafe fn copy_cstr_into_wasm(ctx: &mut Ctx, 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((cstr_len as u32) + 1, ctx); + 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 slice = slice::from_raw_parts_mut(raw_memory, cstr_len); @@ -71,7 +71,7 @@ pub unsafe fn copy_cstr_into_wasm(ctx: &mut Ctx, cstr: *const c_char) -> u32 { space_offset } -pub unsafe fn allocate_on_stack<'a, T: Copy>(count: u32, ctx: &'a mut Ctx) -> (u32, &'a mut [T]) { +pub unsafe fn allocate_on_stack<'a, T: Copy>(ctx: &'a mut Ctx, count: u32) -> (u32, &'a mut [T]) { let offset = get_emscripten_data(ctx) .stack_alloc .call(count * (size_of::() as u32)) @@ -82,8 +82,8 @@ pub unsafe fn allocate_on_stack<'a, T: Copy>(count: u32, ctx: &'a mut Ctx) -> (u (offset, slice) } -pub unsafe fn allocate_cstr_on_stack<'a>(s: &str, ctx: &'a mut Ctx) -> (u32, &'a [u8]) { - let (offset, slice) = allocate_on_stack((s.len() + 1) as u32, ctx); +pub unsafe fn allocate_cstr_on_stack<'a>(ctx: &'a mut Ctx, s: &str) -> (u32, &'a [u8]) { + let (offset, slice) = allocate_on_stack(ctx, (s.len() + 1) as u32); use std::iter; for (byte, loc) in s.bytes().chain(iter::once(0)).zip(slice.iter_mut()) { From 227d5e24cfe2a8da63e456fc72bcfbd93eab24ed Mon Sep 17 00:00:00 2001 From: Syrus Date: Sat, 9 Feb 2019 14:18:53 -0800 Subject: [PATCH 3/8] Moved ctx to first argument in Cranelift backend --- lib/clif-backend/src/func_env.rs | 10 +++++----- lib/clif-backend/src/module_env.rs | 10 +++++----- lib/clif-backend/src/resolver.rs | 13 +++++++------ lib/clif-backend/src/trampoline.rs | 2 +- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/clif-backend/src/func_env.rs b/lib/clif-backend/src/func_env.rs index 78919526499..cada963d125 100644 --- a/lib/clif-backend/src/func_env.rs +++ b/lib/clif-backend/src/func_env.rs @@ -460,7 +460,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { // and the vmctx parameter. let mut args = Vec::with_capacity(call_args.len() + 1); args.extend(call_args.iter().cloned()); - args.push(vmctx_ptr); + args.insert(0, vmctx_ptr); Ok(pos.ins().call_indirect(sig_ref, func_ptr, &args)) } @@ -486,7 +486,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { let mut args = Vec::with_capacity(call_args.len() + 1); args.extend(call_args.iter().cloned()); - args.push(vmctx); + args.insert(0, vmctx); Ok(pos.ins().call(callee, &args)) } @@ -533,7 +533,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { let mut args = Vec::with_capacity(call_args.len() + 1); args.extend(call_args.iter().cloned()); - args.push(imported_vmctx_addr); + args.insert(0, imported_vmctx_addr); Ok(pos .ins() @@ -604,7 +604,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { let call_inst = pos .ins() - .call(mem_grow_func, &[const_mem_index, by_value, vmctx]); + .call(mem_grow_func, &[vmctx, const_mem_index, by_value]); Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap()) } @@ -664,7 +664,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { .special_param(ir::ArgumentPurpose::VMContext) .expect("missing vmctx parameter"); - let call_inst = pos.ins().call(mem_grow_func, &[const_mem_index, vmctx]); + let call_inst = pos.ins().call(mem_grow_func, &[vmctx, const_mem_index]); Ok(*pos.func.dfg.inst_results(call_inst).first().unwrap()) } diff --git a/lib/clif-backend/src/module_env.rs b/lib/clif-backend/src/module_env.rs index 8a26b273196..6b22b4ce61d 100644 --- a/lib/clif-backend/src/module_env.rs +++ b/lib/clif-backend/src/module_env.rs @@ -536,14 +536,14 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let func_index = pos.ins().iconst(ir::types::I32, func_index.index() as i64); - pos.ins().call(start_debug, &[func_index, vmctx]); + pos.ins().call(start_debug, &[vmctx, func_index]); for param in new_ebb_params.iter().cloned() { match pos.func.dfg.value_type(param) { - ir::types::I32 => pos.ins().call(i32_print, &[param, vmctx]), - ir::types::I64 => pos.ins().call(i64_print, &[param, vmctx]), - ir::types::F32 => pos.ins().call(f32_print, &[param, vmctx]), - ir::types::F64 => pos.ins().call(f64_print, &[param, vmctx]), + ir::types::I32 => pos.ins().call(i32_print, &[vmctx, param]), + ir::types::I64 => pos.ins().call(i64_print, &[vmctx, param]), + ir::types::F32 => pos.ins().call(f32_print, &[vmctx, param]), + ir::types::F64 => pos.ins().call(f64_print, &[vmctx, param]), _ => unimplemented!(), }; } diff --git a/lib/clif-backend/src/resolver.rs b/lib/clif-backend/src/resolver.rs index 1e62c0c20a8..ddc7cd3b957 100644 --- a/lib/clif-backend/src/resolver.rs +++ b/lib/clif-backend/src/resolver.rs @@ -34,6 +34,7 @@ use wasmer_runtime_core::{ types::{FuncSig, LocalFuncIndex, SigIndex}, vm, vmcalls, }; +use wasmer_runtime_core::vm::Ctx; extern "C" { #[cfg(not(target_os = "windows"))] @@ -350,21 +351,21 @@ fn round_up(n: usize, multiple: usize) -> usize { (n + multiple - 1) & !(multiple - 1) } -extern "C" fn i32_print(n: i32) { +extern "C" fn i32_print(_ctx: &mut Ctx, n: i32) { print!(" i32: {},", n); } -extern "C" fn i64_print(n: i64) { +extern "C" fn i64_print(_ctx: &mut Ctx, n: i64) { print!(" i64: {},", n); } -extern "C" fn f32_print(n: f32) { +extern "C" fn f32_print(_ctx: &mut Ctx, n: f32) { print!(" f32: {},", n); } -extern "C" fn f64_print(n: f64) { +extern "C" fn f64_print(_ctx: &mut Ctx, n: f64) { print!(" f64: {},", n); } -extern "C" fn start_debug(func_index: u32) { +extern "C" fn start_debug(_ctx: &mut Ctx, func_index: u32) { print!("func ({}), args: [", func_index); } -extern "C" fn end_debug() { +extern "C" fn end_debug(_ctx: &mut Ctx) { println!(" ]"); } diff --git a/lib/clif-backend/src/trampoline.rs b/lib/clif-backend/src/trampoline.rs index 0751cea142c..a30dfd11448 100644 --- a/lib/clif-backend/src/trampoline.rs +++ b/lib/clif-backend/src/trampoline.rs @@ -169,6 +169,7 @@ fn generate_func(func_sig: &FuncSig) -> ir::Function { let mut pos = FuncCursor::new(&mut func).at_first_insertion_point(entry_ebb); let mut args_vec = Vec::with_capacity(func_sig.params().len() + 1); + args_vec.push(vmctx_ptr); for (index, wasm_ty) in func_sig.params().iter().enumerate() { let mem_flags = ir::MemFlags::trusted(); @@ -180,7 +181,6 @@ fn generate_func(func_sig: &FuncSig) -> ir::Function { ); args_vec.push(val); } - args_vec.push(vmctx_ptr); let call_inst = pos.ins().call_indirect(export_sig_ref, func_ptr, &args_vec); From 4311a37defbdd65d2357e1afe2528b33d6ae2548 Mon Sep 17 00:00:00 2001 From: Syrus Date: Sat, 9 Feb 2019 14:19:04 -0800 Subject: [PATCH 4/8] Fixed simple spectests example compilation --- lib/clif-backend/src/func_env.rs | 6 +++--- lib/clif-backend/src/resolver.rs | 13 ++++++------- lib/runtime-core/src/vmcalls.rs | 20 ++++++++++---------- lib/spectests/examples/simple/main.rs | 2 +- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/clif-backend/src/func_env.rs b/lib/clif-backend/src/func_env.rs index cada963d125..723ab71d90f 100644 --- a/lib/clif-backend/src/func_env.rs +++ b/lib/clif-backend/src/func_env.rs @@ -32,7 +32,7 @@ impl<'env, 'module, 'isa> FuncEnv<'env, 'module, 'isa> { let mut signature = self.env.signatures[Converter(clif_sig_index).into()].clone(); // Add the vmctx parameter type to it - signature.params.push(ir::AbiParam::special( + signature.params.insert(0, ir::AbiParam::special( self.pointer_type(), ir::ArgumentPurpose::VMContext, )); @@ -559,9 +559,9 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ + ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext), ir::AbiParam::new(ir::types::I32), ir::AbiParam::new(ir::types::I32), - ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext), ], returns: vec![ir::AbiParam::new(ir::types::I32)], }); @@ -623,8 +623,8 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ - ir::AbiParam::new(ir::types::I32), ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext), + ir::AbiParam::new(ir::types::I32), ], returns: vec![ir::AbiParam::new(ir::types::I32)], }); diff --git a/lib/clif-backend/src/resolver.rs b/lib/clif-backend/src/resolver.rs index ddc7cd3b957..91e6102efd8 100644 --- a/lib/clif-backend/src/resolver.rs +++ b/lib/clif-backend/src/resolver.rs @@ -34,7 +34,6 @@ use wasmer_runtime_core::{ types::{FuncSig, LocalFuncIndex, SigIndex}, vm, vmcalls, }; -use wasmer_runtime_core::vm::Ctx; extern "C" { #[cfg(not(target_os = "windows"))] @@ -351,21 +350,21 @@ fn round_up(n: usize, multiple: usize) -> usize { (n + multiple - 1) & !(multiple - 1) } -extern "C" fn i32_print(_ctx: &mut Ctx, n: i32) { +extern "C" fn i32_print(_ctx: &mut vm::Ctx, n: i32) { print!(" i32: {},", n); } -extern "C" fn i64_print(_ctx: &mut Ctx, n: i64) { +extern "C" fn i64_print(_ctx: &mut vm::Ctx, n: i64) { print!(" i64: {},", n); } -extern "C" fn f32_print(_ctx: &mut Ctx, n: f32) { +extern "C" fn f32_print(_ctx: &mut vm::Ctx, n: f32) { print!(" f32: {},", n); } -extern "C" fn f64_print(_ctx: &mut Ctx, n: f64) { +extern "C" fn f64_print(_ctx: &mut vm::Ctx, n: f64) { print!(" f64: {},", n); } -extern "C" fn start_debug(_ctx: &mut Ctx, func_index: u32) { +extern "C" fn start_debug(_ctx: &mut vm::Ctx, func_index: u32) { print!("func ({}), args: [", func_index); } -extern "C" fn end_debug(_ctx: &mut Ctx) { +extern "C" fn end_debug(_ctx: &mut vm::Ctx) { println!(" ]"); } diff --git a/lib/runtime-core/src/vmcalls.rs b/lib/runtime-core/src/vmcalls.rs index 8a6e683b57f..576ca34ce91 100644 --- a/lib/runtime-core/src/vmcalls.rs +++ b/lib/runtime-core/src/vmcalls.rs @@ -13,9 +13,9 @@ use crate::{ // +****************************+ pub unsafe extern "C" fn local_static_memory_grow( + ctx: &mut vm::Ctx, memory_index: LocalMemoryIndex, delta: Pages, - ctx: &mut vm::Ctx, ) -> i32 { let local_memory = *ctx.memories.add(memory_index.index()); let memory = (*local_memory).memory as *mut StaticMemory; @@ -28,8 +28,8 @@ pub unsafe extern "C" fn local_static_memory_grow( } pub unsafe extern "C" fn local_static_memory_size( - memory_index: LocalMemoryIndex, ctx: &vm::Ctx, + memory_index: LocalMemoryIndex, ) -> Pages { let local_memory = *ctx.memories.add(memory_index.index()); let memory = (*local_memory).memory as *mut StaticMemory; @@ -38,9 +38,9 @@ pub unsafe extern "C" fn local_static_memory_size( } pub unsafe extern "C" fn local_dynamic_memory_grow( + ctx: &mut vm::Ctx, memory_index: LocalMemoryIndex, delta: Pages, - ctx: &mut vm::Ctx, ) -> i32 { let local_memory = *ctx.memories.add(memory_index.index()); let memory = (*local_memory).memory as *mut DynamicMemory; @@ -53,8 +53,8 @@ pub unsafe extern "C" fn local_dynamic_memory_grow( } pub unsafe extern "C" fn local_dynamic_memory_size( - memory_index: LocalMemoryIndex, ctx: &vm::Ctx, + memory_index: LocalMemoryIndex, ) -> Pages { let local_memory = *ctx.memories.add(memory_index.index()); let memory = (*local_memory).memory as *mut DynamicMemory; @@ -67,9 +67,9 @@ pub unsafe extern "C" fn local_dynamic_memory_size( // +****************************+ pub unsafe extern "C" fn imported_static_memory_grow( + ctx: &mut vm::Ctx, import_memory_index: ImportedMemoryIndex, delta: Pages, - ctx: &mut vm::Ctx, ) -> i32 { let local_memory = *ctx.imported_memories.add(import_memory_index.index()); let memory = (*local_memory).memory as *mut StaticMemory; @@ -82,8 +82,8 @@ pub unsafe extern "C" fn imported_static_memory_grow( } pub unsafe extern "C" fn imported_static_memory_size( - import_memory_index: ImportedMemoryIndex, ctx: &vm::Ctx, + import_memory_index: ImportedMemoryIndex, ) -> Pages { let local_memory = *ctx.imported_memories.add(import_memory_index.index()); let memory = (*local_memory).memory as *mut StaticMemory; @@ -92,9 +92,9 @@ pub unsafe extern "C" fn imported_static_memory_size( } pub unsafe extern "C" fn imported_dynamic_memory_grow( + ctx: &mut vm::Ctx, memory_index: ImportedMemoryIndex, delta: Pages, - ctx: &mut vm::Ctx, ) -> i32 { let local_memory = *ctx.imported_memories.add(memory_index.index()); let memory = (*local_memory).memory as *mut DynamicMemory; @@ -107,8 +107,8 @@ pub unsafe extern "C" fn imported_dynamic_memory_grow( } pub unsafe extern "C" fn imported_dynamic_memory_size( - memory_index: ImportedMemoryIndex, ctx: &vm::Ctx, + memory_index: ImportedMemoryIndex, ) -> Pages { let local_memory = *ctx.imported_memories.add(memory_index.index()); let memory = (*local_memory).memory as *mut DynamicMemory; @@ -121,9 +121,9 @@ pub unsafe extern "C" fn imported_dynamic_memory_size( // +****************************+ pub unsafe extern "C" fn local_table_grow( + ctx: &mut vm::Ctx, table_index: LocalTableIndex, delta: u32, - ctx: &mut vm::Ctx, ) -> i32 { let _ = table_index; let _ = delta; @@ -131,7 +131,7 @@ pub unsafe extern "C" fn local_table_grow( unimplemented!() } -pub unsafe extern "C" fn local_table_size(table_index: LocalTableIndex, ctx: &vm::Ctx) -> u32 { +pub unsafe extern "C" fn local_table_size(ctx: &vm::Ctx, table_index: LocalTableIndex) -> u32 { let _ = table_index; let _ = ctx; unimplemented!() diff --git a/lib/spectests/examples/simple/main.rs b/lib/spectests/examples/simple/main.rs index fb215ee21f1..e418c9abe2f 100644 --- a/lib/spectests/examples/simple/main.rs +++ b/lib/spectests/examples/simple/main.rs @@ -61,7 +61,7 @@ fn main() -> error::Result<()> { Ok(()) } -fn print_num(n: i32, ctx: &mut vm::Ctx) -> Result { +fn print_num(ctx: &mut vm::Ctx, n: i32) -> Result { println!("print_num({})", n); let memory: &Memory = ctx.memory(0); From c40195cf77cdd2fef1147b81c443bf9dcdc96325 Mon Sep 17 00:00:00 2001 From: Syrus Date: Tue, 12 Feb 2019 10:04:11 -0800 Subject: [PATCH 5/8] Fixed spectests --- lib/clif-backend/src/func_env.rs | 8 ++++---- lib/clif-backend/src/trampoline.rs | 31 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/clif-backend/src/func_env.rs b/lib/clif-backend/src/func_env.rs index 723ab71d90f..7f1f8b52f5e 100644 --- a/lib/clif-backend/src/func_env.rs +++ b/lib/clif-backend/src/func_env.rs @@ -32,10 +32,10 @@ impl<'env, 'module, 'isa> FuncEnv<'env, 'module, 'isa> { let mut signature = self.env.signatures[Converter(clif_sig_index).into()].clone(); // Add the vmctx parameter type to it - signature.params.insert(0, ir::AbiParam::special( - self.pointer_type(), - ir::ArgumentPurpose::VMContext, - )); + signature.params.insert( + 0, + ir::AbiParam::special(self.pointer_type(), ir::ArgumentPurpose::VMContext), + ); // Return signature signature diff --git a/lib/clif-backend/src/trampoline.rs b/lib/clif-backend/src/trampoline.rs index a30dfd11448..b9d942ba140 100644 --- a/lib/clif-backend/src/trampoline.rs +++ b/lib/clif-backend/src/trampoline.rs @@ -229,22 +229,21 @@ fn generate_trampoline_signature() -> ir::Signature { fn generate_export_signature(func_sig: &FuncSig) -> ir::Signature { let mut export_clif_sig = ir::Signature::new(isa::CallConv::SystemV); - export_clif_sig.params = func_sig - .params() - .iter() - .map(|wasm_ty| ir::AbiParam { - value_type: wasm_ty_to_clif(*wasm_ty), - purpose: ir::ArgumentPurpose::Normal, - extension: ir::ArgumentExtension::None, - location: ir::ArgumentLoc::Unassigned, - }) - .chain(iter::once(ir::AbiParam { - value_type: ir::types::I64, - purpose: ir::ArgumentPurpose::VMContext, - extension: ir::ArgumentExtension::None, - location: ir::ArgumentLoc::Unassigned, - })) - .collect(); + let func_sig_iter = func_sig.params().iter().map(|wasm_ty| ir::AbiParam { + value_type: wasm_ty_to_clif(*wasm_ty), + purpose: ir::ArgumentPurpose::Normal, + extension: ir::ArgumentExtension::None, + location: ir::ArgumentLoc::Unassigned, + }); + + export_clif_sig.params = iter::once(ir::AbiParam { + value_type: ir::types::I64, + purpose: ir::ArgumentPurpose::VMContext, + extension: ir::ArgumentExtension::None, + location: ir::ArgumentLoc::Unassigned, + }) + .chain(func_sig_iter) + .collect(); export_clif_sig.returns = func_sig .returns() From 44d1a0d1f963b1cb13f4e4dbfc9bf844959846f5 Mon Sep 17 00:00:00 2001 From: Syrus Date: Tue, 12 Feb 2019 10:49:43 -0800 Subject: [PATCH 6/8] Fixed debug function context position --- lib/clif-backend/src/module_env.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/clif-backend/src/module_env.rs b/lib/clif-backend/src/module_env.rs index 6b22b4ce61d..65bd5f83940 100644 --- a/lib/clif-backend/src/module_env.rs +++ b/lib/clif-backend/src/module_env.rs @@ -419,8 +419,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ - ir::AbiParam::new(ir::types::I32), ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext), + ir::AbiParam::new(ir::types::I32), ], returns: vec![], }); @@ -457,8 +457,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ - ir::AbiParam::new(ir::types::I32), ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext), + ir::AbiParam::new(ir::types::I32), ], returns: vec![], }); @@ -476,8 +476,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ - ir::AbiParam::new(ir::types::I64), ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext), + ir::AbiParam::new(ir::types::I64), ], returns: vec![], }); @@ -495,8 +495,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ - ir::AbiParam::new(ir::types::F32), ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext), + ir::AbiParam::new(ir::types::F32), ], returns: vec![], }); @@ -514,8 +514,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa> let signature = pos.func.import_signature(ir::Signature { call_conv: self.target_config().default_call_conv, params: vec![ - ir::AbiParam::new(ir::types::F64), ir::AbiParam::special(ir::types::I64, ir::ArgumentPurpose::VMContext), + ir::AbiParam::new(ir::types::F64), ], returns: vec![], }); From b7c3cc09d122f533415db830ce2c8ca62696e06c Mon Sep 17 00:00:00 2001 From: Syrus Date: Tue, 12 Feb 2019 11:14:20 -0800 Subject: [PATCH 7/8] Fixed typed func implementation --- lib/runtime-core/src/typed_func.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/runtime-core/src/typed_func.rs b/lib/runtime-core/src/typed_func.rs index 9989764c646..e16307d8e12 100644 --- a/lib/runtime-core/src/typed_func.rs +++ b/lib/runtime-core/src/typed_func.rs @@ -138,9 +138,9 @@ impl WasmTypeList for (A,) { } #[allow(non_snake_case)] unsafe fn call(self, f: *const (), ctx: *mut Ctx) -> Rets { - let f: extern "C" fn(A, *mut Ctx) -> Rets = mem::transmute(f); + let f: extern "C" fn(*mut Ctx, A) -> Rets = mem::transmute(f); let (a,) = self; - f(a, ctx) + f(ctx, a) } } @@ -175,10 +175,10 @@ macro_rules! impl_traits { } #[allow(non_snake_case)] unsafe fn call(self, f: *const (), ctx: *mut Ctx) -> Rets { - let f: extern fn( $( $x, )* *mut Ctx) -> Rets::CStruct = mem::transmute(f); + let f: extern fn(*mut Ctx $( ,$x )*) -> Rets::CStruct = mem::transmute(f); #[allow(unused_parens)] let ( $( $x ),* ) = self; - let c_struct = f( $( $x, )* ctx); + let c_struct = f(ctx $( ,$x )*); Rets::from_c_struct(c_struct) } } From b6416068a8dc84039d6c8d96426d892daeffb296 Mon Sep 17 00:00:00 2001 From: Syrus Date: Tue, 12 Feb 2019 11:17:09 -0800 Subject: [PATCH 8/8] Use vec.push instead of .insert for vmctx arg --- lib/clif-backend/src/func_env.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/clif-backend/src/func_env.rs b/lib/clif-backend/src/func_env.rs index 7f1f8b52f5e..8670e0000db 100644 --- a/lib/clif-backend/src/func_env.rs +++ b/lib/clif-backend/src/func_env.rs @@ -459,8 +459,8 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { // Build a value list for the indirect call instruction containing the call_args // and the vmctx parameter. let mut args = Vec::with_capacity(call_args.len() + 1); + args.push(vmctx_ptr); args.extend(call_args.iter().cloned()); - args.insert(0, vmctx_ptr); Ok(pos.ins().call_indirect(sig_ref, func_ptr, &args)) } @@ -485,8 +485,8 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { .expect("missing vmctx parameter"); let mut args = Vec::with_capacity(call_args.len() + 1); + args.push(vmctx); args.extend(call_args.iter().cloned()); - args.insert(0, vmctx); Ok(pos.ins().call(callee, &args)) } @@ -532,8 +532,8 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> { let sig_ref = pos.func.dfg.ext_funcs[callee].signature; let mut args = Vec::with_capacity(call_args.len() + 1); + args.push(imported_vmctx_addr); args.extend(call_args.iter().cloned()); - args.insert(0, imported_vmctx_addr); Ok(pos .ins()