Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various emscripten fixes #823

Merged
merged 7 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Blocks of changes will separated by version increments.

## **[Unreleased]**

- [#823](https://github.com/wasmerio/wasmer/issues/823) Improved Emscripten / WASI integration
- [#821](https://github.com/wasmerio/wasmer/issues/821) Remove patch version on most deps Cargo manifests. This gives Wasmer library users more control over which versions of the deps they use.
- [#820](https://github.com/wasmerio/wasmer/issues/820) Remove null-pointer checks in `WasmPtr` from runtime-core, re-add them in Emscripten
- [#803](https://github.com/wasmerio/wasmer/issues/803) Add method to `Ctx` to invoke functions by their `TableIndex`
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

# Generate files
generate-spectests:
WASMER_RUNTIME_GENERATE_SPECTESTS=1 cargo build -p wasmer-runtime-core --release
WASMER_RUNTIME_GENERATE_SPECTESTS=1 cargo build -p wasmer-runtime-core --release \
&& echo "formatting" \
&& cargo fmt

generate-emtests:
WASM_EMSCRIPTEN_GENERATE_EMTESTS=1 cargo build -p wasmer-emscripten-tests --release
WASM_EMSCRIPTEN_GENERATE_EMTESTS=1 cargo build -p wasmer-emscripten-tests --release \
&& echo "formatting" \
&& cargo fmt

generate-wasitests: wasitests-setup
WASM_WASI_GENERATE_WASITESTS=1 cargo build -p wasmer-wasi-tests --release -vv \
Expand Down
6 changes: 5 additions & 1 deletion lib/emscripten/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,9 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
"___syscall345" => func!(crate::syscalls::___syscall345),

// Process
"abort" => func!(crate::process::em_abort),
"abort" => func!(crate::process::_abort),
"_abort" => func!(crate::process::_abort),
"_prctl" => func!(crate::process::_prctl),
"abortStackOverflow" => func!(crate::process::abort_stack_overflow),
"_llvm_trap" => func!(crate::process::_llvm_trap),
"_fork" => func!(crate::process::_fork),
Expand Down Expand Up @@ -829,6 +830,9 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
"_gmtime" => func!(crate::time::_gmtime),

// Math
"sqrt" => func!(crate::math::sqrt),
"floor" => func!(crate::math::floor),
"fabs" => func!(crate::math::fabs),
"f64-rem" => func!(crate::math::f64_rem),
"_llvm_copysign_f32" => func!(crate::math::_llvm_copysign_f32),
"_llvm_copysign_f64" => func!(crate::math::_llvm_copysign_f64),
Expand Down
15 changes: 15 additions & 0 deletions lib/emscripten/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ pub fn log(_ctx: &mut Ctx, value: f64) -> f64 {
value.ln()
}

// emscripten: global.Math sqrt
pub fn sqrt(_ctx: &mut Ctx, value: f64) -> f64 {
value.sqrt()
}

// emscripten: global.Math floor
pub fn floor(_ctx: &mut Ctx, value: f64) -> f64 {
value.floor()
}

// emscripten: global.Math fabs
pub fn fabs(_ctx: &mut Ctx, value: f64) -> f64 {
value.abs()
}

// emscripten: asm2wasm.f64-to-int
pub fn f64_to_int(_ctx: &mut Ctx, value: f64) -> i32 {
debug!("emscripten::f64_to_int {}", value);
Expand Down
21 changes: 7 additions & 14 deletions lib/emscripten/src/process.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use libc::{abort, c_char, c_int, exit, EAGAIN};
use libc::{abort, c_int, exit, EAGAIN};

#[cfg(not(target_os = "windows"))]
type PidT = libc::pid_t;
#[cfg(target_os = "windows")]
type PidT = c_int;

use std::ffi::CStr;
use wasmer_runtime_core::vm::Ctx;

pub fn abort_with_message(ctx: &mut Ctx, message: &str) {
Expand All @@ -21,6 +20,12 @@ pub fn _abort(_ctx: &mut Ctx) {
}
}

pub fn _prctl(ctx: &mut Ctx, _a: i32, _b: i32) -> i32 {
debug!("emscripten::_prctl");
abort_with_message(ctx, "missing function: prctl");
-1
}

pub fn _fork(_ctx: &mut Ctx) -> PidT {
debug!("emscripten::_fork");
// unsafe {
Expand All @@ -45,18 +50,6 @@ pub fn _exit(_ctx: &mut Ctx, status: c_int) {
unsafe { exit(status) }
}

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 {
let message = CStr::from_ptr(message_addr)
.to_str()
.unwrap_or("Unexpected abort");

abort_with_message(ctx, message);
}
}

pub fn _kill(_ctx: &mut Ctx, _one: i32, _two: i32) -> i32 {
debug!("emscripten::_kill");
-1
Expand Down
4 changes: 3 additions & 1 deletion lib/emscripten/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub fn is_emscripten_module(module: &Module) -> bool {
.namespace_table
.get(import_name.namespace_index);
let field = module.info().name_table.get(import_name.name_index);
if (field == "_emscripten_memcpy_big" || field == "emscripten_memcpy_big")
if (field == "_emscripten_memcpy_big"
|| field == "emscripten_memcpy_big"
|| field == "__map_file")
&& namespace == "env"
{
return true;
Expand Down
4 changes: 4 additions & 0 deletions lib/runtime-core/src/structures/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ where
self.elems.len()
}

pub fn is_empty(&self) -> bool {
self.elems.is_empty()
}

pub fn push(&mut self, value: V) -> K {
let len = self.len();
self.elems.push(value);
Expand Down
9 changes: 6 additions & 3 deletions lib/wasi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use wasmer_runtime_core::module::Module;

/// Check if a provided module is compiled with WASI support
pub fn is_wasi_module(module: &Module) -> bool {
if module.info().imported_functions.is_empty() {
return false;
}
for (_, import_name) in &module.info().imported_functions {
let namespace = module
.info()
.namespace_table
.get(import_name.namespace_index);
if namespace == "wasi_unstable" {
return true;
if namespace != "wasi_unstable" {
return false;
}
}
false
true
}
8 changes: 4 additions & 4 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
import_object.allow_missing_functions = true; // Import initialization might be left to the loader.
let instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
.map_err(|e| format!("Can't instantiate loader module: {:?}", e))?;

let args: Vec<Value> = options
.args
Expand Down Expand Up @@ -551,7 +551,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
let import_object = wasmer_emscripten::generate_emscripten_env(&mut emscripten_globals);
let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
.map_err(|e| format!("Can't instantiate emscripten module: {:?}", e))?;

wasmer_emscripten::run_emscripten_instance(
&module,
Expand Down Expand Up @@ -591,7 +591,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
#[allow(unused_mut)] // mut used in feature
let mut instance = module
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate module: {:?}", e))?;
.map_err(|e| format!("Can't instantiate WASI module: {:?}", e))?;

let start: Func<(), ()> = instance.func("_start").map_err(|e| format!("{:?}", e))?;

Expand Down Expand Up @@ -752,7 +752,7 @@ fn run(options: Run) {
match execute_wasm(&options) {
Ok(()) => {}
Err(message) => {
eprintln!("execute_wasm: {:?}", message);
eprintln!("Error: {}", message);
exit(1);
}
}
Expand Down