wasmer/lib/emscripten/src/lib.rs

510 lines
19 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate wasmer_runtime_core;
2019-01-11 05:37:59 +00:00
use lazy_static::lazy_static;
use std::cell::UnsafeCell;
2019-02-09 21:31:28 +00:00
use std::{f64, ffi::c_void};
use wasmer_runtime_core::{
2019-01-31 01:19:53 +00:00
error::CallResult,
2019-02-09 21:31:28 +00:00
export::Export,
2019-01-30 02:00:08 +00:00
func,
global::Global,
2019-02-09 21:31:28 +00:00
import::ImportObject,
2019-01-30 02:00:08 +00:00
imports,
2019-02-05 18:35:15 +00:00
memory::Memory,
module::ImportName,
2019-01-30 02:00:08 +00:00
table::Table,
types::{ElementType, FuncSig, MemoryDescriptor, TableDescriptor, Type, Value},
units::Pages,
2019-01-31 01:19:53 +00:00
vm::Ctx,
Func, Instance, IsExport, Module,
2019-01-11 16:10:21 +00:00
};
2019-01-18 05:55:44 +00:00
2019-01-22 00:42:20 +00:00
#[macro_use]
mod macros;
2019-01-18 05:55:44 +00:00
//#[cfg(test)]
mod file_descriptor;
pub mod stdio;
2018-11-20 19:11:58 +00:00
// EMSCRIPTEN APIS
2018-11-21 23:10:03 +00:00
mod env;
2018-11-27 04:29:26 +00:00
mod errno;
2018-12-18 17:43:59 +00:00
mod exception;
2018-11-22 04:59:23 +00:00
mod io;
2018-12-19 01:21:12 +00:00
mod jmp;
2019-01-26 23:02:51 +00:00
mod linking;
2018-11-27 04:29:26 +00:00
mod lock;
mod math;
2018-12-18 17:43:59 +00:00
mod memory;
2018-11-27 04:29:26 +00:00
mod nullfunc;
2018-11-20 19:11:58 +00:00
mod process;
2018-11-28 21:25:56 +00:00
mod signal;
2018-11-27 04:29:26 +00:00
mod storage;
2018-11-21 23:10:03 +00:00
mod syscalls;
2018-11-27 04:29:26 +00:00
mod time;
2018-11-22 04:59:23 +00:00
mod utils;
2018-11-23 05:13:01 +00:00
mod varargs;
2018-11-20 19:11:58 +00:00
pub use self::storage::{align_memory, static_alloc};
pub use self::utils::{
allocate_cstr_on_stack, allocate_on_stack, get_emscripten_memory_size,
get_emscripten_table_size, is_emscripten_module,
};
// TODO: Magic number - how is this calculated?
const TOTAL_STACK: u32 = 5_242_880;
2018-12-07 02:10:26 +00:00
// TODO: Magic number - how is this calculated?
const DYNAMICTOP_PTR_DIFF: u32 = 1088;
2018-12-07 02:10:26 +00:00
// TODO: make this variable
const STATIC_BUMP: u32 = 215_536;
lazy_static! {
static ref OLD_ABORT_ON_CANNOT_GROW_MEMORY_SIG: FuncSig =
{ FuncSig::new(vec![], vec![Type::I32]) };
}
2019-01-23 20:43:41 +00:00
// The address globals begin at. Very low in memory, for code size and optimization opportunities.
// Above 0 is static memory, starting with globals.
// Then the stack.
// Then 'dynamic' memory for sbrk.
const GLOBAL_BASE: u32 = 1024;
const STATIC_BASE: u32 = GLOBAL_BASE;
2019-01-23 20:43:41 +00:00
fn stacktop(static_bump: u32) -> u32 {
align_memory(dynamictop_ptr(static_bump) + 4)
}
fn stack_max(static_bump: u32) -> u32 {
stacktop(static_bump) + TOTAL_STACK
}
fn dynamic_base(static_bump: u32) -> u32 {
align_memory(stack_max(static_bump))
}
fn dynamictop_ptr(static_bump: u32) -> u32 {
static_bump + DYNAMICTOP_PTR_DIFF
}
pub struct EmscriptenData<'a> {
pub malloc: Func<'a, u32, u32>,
pub free: Func<'a, u32>,
pub memalign: Option<Func<'a, (u32, u32), u32>>,
pub memset: Func<'a, (u32, u32, u32), u32>,
pub stack_alloc: Func<'a, u32, u32>,
2019-02-07 18:45:48 +00:00
pub jumps: Vec<UnsafeCell<[u32; 27]>>,
}
impl<'a> EmscriptenData<'a> {
pub fn new(instance: &'a mut Instance) -> EmscriptenData<'a> {
let malloc = instance.func("_malloc").unwrap();
let free = instance.func("_free").unwrap();
let memalign = if let Ok(func) = instance.func("_memalign") {
Some(func)
} else {
None
};
let memset = instance.func("_memset").unwrap();
let stack_alloc = instance.func("stackAlloc").unwrap();
EmscriptenData {
malloc,
free,
memalign,
memset,
stack_alloc,
jumps: Vec::new(),
}
}
}
2019-01-06 21:21:06 +00:00
pub fn run_emscripten_instance(
2019-01-24 06:00:38 +00:00
_module: &Module,
instance: &mut Instance,
2019-01-26 20:17:17 +00:00
path: &str,
args: Vec<&str>,
) -> CallResult<()> {
let mut data = EmscriptenData::new(instance);
let data_ptr = &mut data as *mut _ as *mut c_void;
instance.context_mut().data = data_ptr;
if let Ok(_func) = instance.dyn_func("___emscripten_environ_constructor") {
instance.call("___emscripten_environ_constructor", &[])?;
}
// println!("running emscripten instance");
2019-02-07 18:45:48 +00:00
let main_func = instance.dyn_func("_main")?;
let num_params = main_func.signature().params().len();
2019-01-28 01:06:03 +00:00
let _result = match num_params {
2019-01-26 20:17:17 +00:00
2 => {
let (argc, argv) = store_module_arguments(instance.context_mut(), path, args);
2019-01-26 20:17:17 +00:00
instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?;
}
0 => {
instance.call("_main", &[])?;
}
_ => panic!(
"The emscripten main function has received an incorrect number of params {}",
num_params
),
};
// TODO atinit and atexit for emscripten
// println!("{:?}", data);
Ok(())
}
fn store_module_arguments(ctx: &mut Ctx, path: &str, args: Vec<&str>) -> (u32, u32) {
2019-01-27 18:18:58 +00:00
let argc = args.len() + 1;
let mut args_slice = vec![0; argc];
args_slice[0] = unsafe { allocate_cstr_on_stack(ctx, path).0 };
2019-01-27 18:18:58 +00:00
for (slot, arg) in args_slice[1..argc].iter_mut().zip(args.iter()) {
*slot = unsafe { allocate_cstr_on_stack(ctx, &arg).0 };
2019-01-27 18:18:58 +00:00
}
let (argv_offset, argv_slice): (_, &mut [u32]) =
unsafe { allocate_on_stack(ctx, ((argc + 1) * 4) as u32) };
2019-01-27 18:18:58 +00:00
assert!(!argv_slice.is_empty());
for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) {
*slot = *arg
}
argv_slice[argc] = 0;
(argc as u32, argv_offset)
}
2019-01-26 20:17:17 +00:00
pub fn emscripten_set_up_memory(memory: &Memory, globals: &EmscriptenGlobalsData) {
let dynamictop_ptr = globals.dynamictop_ptr;
let stack_max = globals.stack_max;
let dynamic_base = align_memory(stack_max);
memory.view::<u32>()[(dynamictop_ptr / 4) as usize].set(dynamic_base);
2019-01-17 22:10:29 +00:00
}
pub struct EmscriptenGlobalsData {
2019-01-25 05:58:54 +00:00
abort: u64,
// Env namespace
stacktop: u32,
stack_max: u32,
dynamictop_ptr: u32,
memory_base: u32,
table_base: u32,
temp_double_ptr: u32,
use_old_abort_on_cannot_grow_memory: bool,
// Global namespace
infinity: f64,
nan: f64,
}
pub struct EmscriptenGlobals {
// The emscripten data
pub data: EmscriptenGlobalsData,
2019-01-23 20:25:56 +00:00
// The emscripten memory
pub memory: Memory,
pub table: Table,
pub memory_min: Pages,
pub memory_max: Option<Pages>,
2019-01-11 16:10:21 +00:00
}
2019-01-25 00:46:16 +00:00
impl EmscriptenGlobals {
pub fn new(module: &Module /*, static_bump: u32 */) -> Self {
let mut use_old_abort_on_cannot_grow_memory = false;
for (
index,
ImportName {
namespace_index,
name_index,
},
) in &module.0.info.imported_functions
{
let namespace = module.0.info.namespace_table.get(*namespace_index);
let name = module.0.info.name_table.get(*name_index);
if name == "abortOnCannotGrowMemory" && namespace == "env" {
let sig_index = module.0.info.func_assoc[index.convert_up(&module.0)];
let expected_sig = &module.0.info.signatures[sig_index];
if **expected_sig == *OLD_ABORT_ON_CANNOT_GROW_MEMORY_SIG {
use_old_abort_on_cannot_grow_memory = true;
}
break;
}
}
let (table_min, table_max) = get_emscripten_table_size(&module);
let (memory_min, memory_max) = get_emscripten_memory_size(&module);
2019-01-23 20:34:56 +00:00
// Memory initialization
let memory_type = MemoryDescriptor {
minimum: memory_min,
maximum: memory_max,
2019-01-23 20:25:56 +00:00
shared: false,
};
let memory = Memory::new(memory_type).unwrap();
2019-01-23 20:25:56 +00:00
let table_type = TableDescriptor {
element: ElementType::Anyfunc,
minimum: table_min,
maximum: table_max,
2019-01-23 20:34:56 +00:00
};
let mut table = Table::new(table_type).unwrap();
2019-01-23 20:34:56 +00:00
let data = {
let static_bump = STATIC_BUMP;
let mut STATIC_TOP = STATIC_BASE + static_bump;
let memory_base = STATIC_BASE;
let table_base = 0;
let temp_double_ptr = STATIC_TOP;
STATIC_TOP += 16;
let dynamictop_ptr = static_alloc(&mut STATIC_TOP, 4);
let stacktop = align_memory(STATIC_TOP);
let stack_max = stacktop + TOTAL_STACK;
EmscriptenGlobalsData {
abort: 0,
stacktop,
stack_max,
dynamictop_ptr,
memory_base,
table_base,
temp_double_ptr,
use_old_abort_on_cannot_grow_memory,
infinity: std::f64::INFINITY,
nan: std::f64::NAN,
}
};
2019-01-17 18:55:25 +00:00
emscripten_set_up_memory(&memory, &data);
2019-01-31 01:19:53 +00:00
Self {
data,
memory,
table,
memory_min,
memory_max,
}
2019-01-11 16:10:21 +00:00
}
}
2019-01-23 20:25:56 +00:00
pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject {
let abort_on_cannot_grow_memory_export = if globals.data.use_old_abort_on_cannot_grow_memory {
func!(crate::memory::abort_on_cannot_grow_memory_old).to_export()
} else {
func!(crate::memory::abort_on_cannot_grow_memory).to_export()
};
imports! {
"env" => {
2019-02-05 18:35:15 +00:00
"memory" => Export::Memory(globals.memory.clone()),
"table" => Export::Table(globals.table.clone()),
// Globals
"STACKTOP" => Global::new(Value::I32(globals.data.stacktop as i32)),
"STACK_MAX" => Global::new(Value::I32(globals.data.stack_max as i32)),
"DYNAMICTOP_PTR" => Global::new(Value::I32(globals.data.dynamictop_ptr as i32)),
"tableBase" => Global::new(Value::I32(globals.data.table_base as i32)),
"__table_base" => Global::new(Value::I32(globals.data.table_base as i32)),
"ABORT" => Global::new(Value::I32(globals.data.abort as i32)),
"memoryBase" => Global::new(Value::I32(globals.data.memory_base as i32)),
"__memory_base" => Global::new(Value::I32(globals.data.memory_base as i32)),
"tempDoublePtr" => Global::new(Value::I32(globals.data.temp_double_ptr as i32)),
// IO
"printf" => func!(crate::io::printf),
"putchar" => func!(crate::io::putchar),
"___lock" => func!(crate::lock::___lock),
"___unlock" => func!(crate::lock::___unlock),
"___wait" => func!(crate::lock::___wait),
// Env
"___assert_fail" => func!(crate::env::___assert_fail),
"_getenv" => func!(crate::env::_getenv),
"_setenv" => func!(crate::env::_setenv),
"_putenv" => func!(crate::env::_putenv),
"_unsetenv" => func!(crate::env::_unsetenv),
"_getpwnam" => func!(crate::env::_getpwnam),
"_getgrnam" => func!(crate::env::_getgrnam),
"___buildEnvironment" => func!(crate::env::___build_environment),
"___setErrNo" => func!(crate::errno::___seterrno),
"_getpagesize" => func!(crate::env::_getpagesize),
"_sysconf" => func!(crate::env::_sysconf),
"_getaddrinfo" => func!(crate::env::_getaddrinfo),
// Null func
"nullFunc_i" => func!(crate::nullfunc::nullfunc_i),
"nullFunc_ii" => func!(crate::nullfunc::nullfunc_ii),
"nullFunc_iii" => func!(crate::nullfunc::nullfunc_iii),
"nullFunc_iiii" => func!(crate::nullfunc::nullfunc_iiii),
"nullFunc_iiiii" => func!(crate::nullfunc::nullfunc_iiiii),
"nullFunc_iiiiii" => func!(crate::nullfunc::nullfunc_iiiiii),
"nullFunc_v" => func!(crate::nullfunc::nullfunc_v),
"nullFunc_vi" => func!(crate::nullfunc::nullfunc_vi),
"nullFunc_vii" => func!(crate::nullfunc::nullfunc_vii),
"nullFunc_viii" => func!(crate::nullfunc::nullfunc_viii),
"nullFunc_viiii" => func!(crate::nullfunc::nullfunc_viiii),
"nullFunc_viiiii" => func!(crate::nullfunc::nullfunc_viiiii),
"nullFunc_viiiiii" => func!(crate::nullfunc::nullfunc_viiiiii),
// Syscalls
"___syscall1" => func!(crate::syscalls::___syscall1),
"___syscall3" => func!(crate::syscalls::___syscall3),
"___syscall4" => func!(crate::syscalls::___syscall4),
"___syscall5" => func!(crate::syscalls::___syscall5),
"___syscall6" => func!(crate::syscalls::___syscall6),
"___syscall10" => func!(crate::syscalls::___syscall10),
"___syscall12" => func!(crate::syscalls::___syscall12),
"___syscall15" => func!(crate::syscalls::___syscall15),
"___syscall20" => func!(crate::syscalls::___syscall20),
"___syscall39" => func!(crate::syscalls::___syscall39),
"___syscall38" => func!(crate::syscalls::___syscall38),
"___syscall40" => func!(crate::syscalls::___syscall40),
"___syscall54" => func!(crate::syscalls::___syscall54),
"___syscall57" => func!(crate::syscalls::___syscall57),
"___syscall60" => func!(crate::syscalls::___syscall60),
"___syscall63" => func!(crate::syscalls::___syscall63),
"___syscall64" => func!(crate::syscalls::___syscall64),
"___syscall66" => func!(crate::syscalls::___syscall66),
"___syscall75" => func!(crate::syscalls::___syscall75),
"___syscall85" => func!(crate::syscalls::___syscall85),
"___syscall91" => func!(crate::syscalls::___syscall191),
"___syscall97" => func!(crate::syscalls::___syscall97),
"___syscall102" => func!(crate::syscalls::___syscall102),
"___syscall110" => func!(crate::syscalls::___syscall110),
"___syscall114" => func!(crate::syscalls::___syscall114),
"___syscall122" => func!(crate::syscalls::___syscall122),
"___syscall140" => func!(crate::syscalls::___syscall140),
"___syscall142" => func!(crate::syscalls::___syscall142),
"___syscall145" => func!(crate::syscalls::___syscall145),
"___syscall146" => func!(crate::syscalls::___syscall146),
"___syscall168" => func!(crate::syscalls::___syscall168),
"___syscall180" => func!(crate::syscalls::___syscall180),
"___syscall181" => func!(crate::syscalls::___syscall181),
"___syscall191" => func!(crate::syscalls::___syscall191),
"___syscall192" => func!(crate::syscalls::___syscall192),
"___syscall194" => func!(crate::syscalls::___syscall194),
"___syscall195" => func!(crate::syscalls::___syscall195),
"___syscall196" => func!(crate::syscalls::___syscall196),
"___syscall197" => func!(crate::syscalls::___syscall197),
"___syscall199" => func!(crate::syscalls::___syscall199),
"___syscall201" => func!(crate::syscalls::___syscall201),
"___syscall202" => func!(crate::syscalls::___syscall202),
"___syscall212" => func!(crate::syscalls::___syscall212),
"___syscall220" => func!(crate::syscalls::___syscall220),
"___syscall221" => func!(crate::syscalls::___syscall221),
"___syscall268" => func!(crate::syscalls::___syscall268),
"___syscall272" => func!(crate::syscalls::___syscall272),
"___syscall295" => func!(crate::syscalls::___syscall295),
"___syscall300" => func!(crate::syscalls::___syscall300),
"___syscall330" => func!(crate::syscalls::___syscall330),
"___syscall334" => func!(crate::syscalls::___syscall334),
"___syscall340" => func!(crate::syscalls::___syscall340),
// Process
"abort" => func!(crate::process::em_abort),
"_abort" => func!(crate::process::_abort),
"abortStackOverflow" => func!(crate::process::abort_stack_overflow),
"_llvm_trap" => func!(crate::process::_llvm_trap),
"_fork" => func!(crate::process::_fork),
"_exit" => func!(crate::process::_exit),
"_system" => func!(crate::process::_system),
"_popen" => func!(crate::process::_popen),
"_endgrent" => func!(crate::process::_endgrent),
"_execve" => func!(crate::process::_execve),
"_kill" => func!(crate::process::_kill),
"_llvm_stackrestore" => func!(crate::process::_llvm_stackrestore),
"_llvm_stacksave" => func!(crate::process::_llvm_stacksave),
"_raise" => func!(crate::process::_raise),
"_sem_init" => func!(crate::process::_sem_init),
"_sem_post" => func!(crate::process::_sem_post),
"_sem_wait" => func!(crate::process::_sem_wait),
"_getgrent" => func!(crate::process::_getgrent),
"_sched_yield" => func!(crate::process::_sched_yield),
"_setgrent" => func!(crate::process::_setgrent),
"_setgroups" => func!(crate::process::_setgroups),
"_setitimer" => func!(crate::process::_setitimer),
"_usleep" => func!(crate::process::_usleep),
"_utimes" => func!(crate::process::_utimes),
"_waitpid" => func!(crate::process::_waitpid),
// Signal
"_sigemptyset" => func!(crate::signal::_sigemptyset),
"_sigaddset" => func!(crate::signal::_sigaddset),
"_sigprocmask" => func!(crate::signal::_sigprocmask),
"_sigaction" => func!(crate::signal::_sigaction),
"_signal" => func!(crate::signal::_signal),
"_sigsuspend" => func!(crate::signal::_sigsuspend),
// Memory
"abortOnCannotGrowMemory" => abort_on_cannot_grow_memory_export,
"_emscripten_memcpy_big" => func!(crate::memory::_emscripten_memcpy_big),
"_emscripten_get_heap_size" => func!(crate::memory::_emscripten_get_heap_size),
"_emscripten_resize_heap" => func!(crate::memory::_emscripten_resize_heap),
"enlargeMemory" => func!(crate::memory::enlarge_memory),
"getTotalMemory" => func!(crate::memory::get_total_memory),
"___map_file" => func!(crate::memory::___map_file),
// Exception
"___cxa_allocate_exception" => func!(crate::exception::___cxa_allocate_exception),
"___cxa_throw" => func!(crate::exception::___cxa_throw),
// Time
"_gettimeofday" => func!(crate::time::_gettimeofday),
"_clock_gettime" => func!(crate::time::_clock_gettime),
"___clock_gettime" => func!(crate::time::_clock_gettime),
"_clock" => func!(crate::time::_clock),
"_difftime" => func!(crate::time::_difftime),
"_asctime" => func!(crate::time::_asctime),
"_asctime_r" => func!(crate::time::_asctime_r),
"_localtime" => func!(crate::time::_localtime),
"_time" => func!(crate::time::_time),
"_strftime" => func!(crate::time::_strftime),
"_localtime_r" => func!(crate::time::_localtime_r),
"_gmtime_r" => func!(crate::time::_gmtime_r),
"_mktime" => func!(crate::time::_mktime),
"_gmtime" => func!(crate::time::_gmtime),
// Math
"f64-rem" => func!(crate::math::f64_rem),
"_llvm_log10_f64" => func!(crate::math::_llvm_log10_f64),
"_llvm_log2_f64" => func!(crate::math::_llvm_log2_f64),
"_llvm_log10_f32" => func!(crate::math::_llvm_log10_f32),
"_llvm_log2_f32" => func!(crate::math::_llvm_log2_f64),
"_emscripten_random" => func!(crate::math::_emscripten_random),
// Jump
"__setjmp" => func!(crate::jmp::__setjmp),
"__longjmp" => func!(crate::jmp::__longjmp),
// Linking
"_dlclose" => func!(crate::linking::_dlclose),
2019-02-06 05:39:12 +00:00
"_dlerror" => func!(crate::linking::_dlerror),
"_dlopen" => func!(crate::linking::_dlopen),
"_dlsym" => func!(crate::linking::_dlsym),
},
"global" => {
"NaN" => Global::new(Value::F64(f64::NAN)),
"Infinity" => Global::new(Value::F64(f64::INFINITY)),
},
"global.Math" => {
"pow" => func!(crate::math::pow),
},
2019-02-06 05:39:12 +00:00
"asm2wasm" => {
"f64-rem" => func!(crate::math::f64_rem),
},
}
}
/// The current version of this crate
pub const VERSION: &str = env!("CARGO_PKG_VERSION");