2019-07-07 05:05:45 +00:00
|
|
|
use super::env::get_emscripten_data;
|
2018-11-24 14:55:21 +00:00
|
|
|
use super::process::abort_with_message;
|
2018-12-18 17:43:59 +00:00
|
|
|
use libc::{c_int, c_void, memcpy, size_t};
|
2019-04-08 21:44:40 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-04-08 23:17:34 +00:00
|
|
|
units::{Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE},
|
2019-04-08 21:44:40 +00:00
|
|
|
vm::Ctx,
|
|
|
|
};
|
2018-11-20 19:11:58 +00:00
|
|
|
|
|
|
|
/// emscripten: _emscripten_memcpy_big
|
2019-02-09 21:58:05 +00:00
|
|
|
pub fn _emscripten_memcpy_big(ctx: &mut Ctx, dest: u32, src: u32, len: u32) -> u32 {
|
2018-12-18 05:54:00 +00:00
|
|
|
debug!(
|
|
|
|
"emscripten::_emscripten_memcpy_big {}, {}, {}",
|
|
|
|
dest, src, len
|
|
|
|
);
|
2019-01-24 21:04:12 +00:00
|
|
|
let dest_addr = emscripten_memory_pointer!(ctx.memory(0), dest) as *mut c_void;
|
|
|
|
let src_addr = emscripten_memory_pointer!(ctx.memory(0), src) as *mut c_void;
|
2018-11-22 04:59:23 +00:00
|
|
|
unsafe {
|
|
|
|
memcpy(dest_addr, src_addr, len as size_t);
|
|
|
|
}
|
2018-11-20 19:11:58 +00:00
|
|
|
dest
|
|
|
|
}
|
|
|
|
|
2019-02-10 22:24:04 +00:00
|
|
|
/// emscripten: _emscripten_get_heap_size
|
2019-04-06 00:21:40 +00:00
|
|
|
pub fn _emscripten_get_heap_size(ctx: &mut Ctx) -> u32 {
|
2019-07-01 23:36:30 +00:00
|
|
|
trace!("emscripten::_emscripten_get_heap_size");
|
2019-05-04 00:34:57 +00:00
|
|
|
let result = ctx.memory(0).size().bytes().0 as u32;
|
2019-07-01 23:36:30 +00:00
|
|
|
trace!("=> {}", result);
|
2019-05-04 00:34:57 +00:00
|
|
|
|
|
|
|
result
|
2019-02-10 22:24:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 23:17:34 +00:00
|
|
|
// From emscripten implementation
|
|
|
|
fn align_up(mut val: usize, multiple: usize) -> usize {
|
|
|
|
if val % multiple > 0 {
|
|
|
|
val += multiple - val % multiple;
|
|
|
|
}
|
|
|
|
val
|
|
|
|
}
|
|
|
|
|
2019-02-10 22:24:04 +00:00
|
|
|
/// emscripten: _emscripten_resize_heap
|
2019-04-08 21:44:40 +00:00
|
|
|
/// Note: this function only allows growing the size of heap
|
|
|
|
pub fn _emscripten_resize_heap(ctx: &mut Ctx, requested_size: u32) -> u32 {
|
|
|
|
debug!("emscripten::_emscripten_resize_heap {}", requested_size);
|
|
|
|
let current_memory_pages = ctx.memory(0).size();
|
|
|
|
let current_memory = current_memory_pages.bytes().0 as u32;
|
|
|
|
|
2019-04-08 23:17:34 +00:00
|
|
|
// implementation from emscripten
|
|
|
|
let mut new_size = usize::max(current_memory as usize, WASM_MIN_PAGES * WASM_PAGE_SIZE);
|
|
|
|
while new_size < requested_size as usize {
|
|
|
|
if new_size <= 0x2000_0000 {
|
|
|
|
new_size = align_up(new_size * 2, WASM_PAGE_SIZE);
|
|
|
|
} else {
|
|
|
|
new_size = usize::min(
|
|
|
|
align_up((3 * new_size + 0x8000_0000) / 4, WASM_PAGE_SIZE),
|
|
|
|
WASM_PAGE_SIZE * WASM_MAX_PAGES,
|
|
|
|
);
|
2019-04-08 21:44:40 +00:00
|
|
|
}
|
2019-04-08 23:17:34 +00:00
|
|
|
}
|
2019-04-08 21:44:40 +00:00
|
|
|
|
2019-04-08 23:17:34 +00:00
|
|
|
let amount_to_grow = (new_size - current_memory as usize) / WASM_PAGE_SIZE;
|
|
|
|
if let Ok(_pages_allocated) = ctx.memory(0).grow(Pages(amount_to_grow as u32)) {
|
|
|
|
debug!("{} pages allocated", _pages_allocated.0);
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
0
|
2019-04-08 21:44:40 +00:00
|
|
|
}
|
2019-02-10 22:24:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-07 05:05:45 +00:00
|
|
|
/// emscripten: sbrk
|
|
|
|
pub fn sbrk(ctx: &mut Ctx, increment: i32) -> i32 {
|
|
|
|
debug!("emscripten::sbrk");
|
|
|
|
// let old_dynamic_top = 0;
|
|
|
|
// let new_dynamic_top = 0;
|
2019-07-31 04:21:20 +00:00
|
|
|
let globals = get_emscripten_data(ctx).globals;
|
2019-07-07 05:05:45 +00:00
|
|
|
let dynamictop_ptr = (globals.dynamictop_ptr) as usize;
|
|
|
|
let old_dynamic_top = ctx.memory(0).view::<u32>()[dynamictop_ptr].get() as i32;
|
|
|
|
let new_dynamic_top: i32 = old_dynamic_top + increment;
|
|
|
|
let total_memory = _emscripten_get_heap_size(ctx) as i32;
|
|
|
|
debug!(
|
|
|
|
" => PTR {}, old: {}, new: {}, increment: {}, total: {}",
|
|
|
|
dynamictop_ptr, old_dynamic_top, new_dynamic_top, increment, total_memory
|
|
|
|
);
|
|
|
|
if increment > 0 && new_dynamic_top < old_dynamic_top || new_dynamic_top < 0 {
|
|
|
|
abort_on_cannot_grow_memory_old(ctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if new_dynamic_top > total_memory {
|
|
|
|
let resized = _emscripten_resize_heap(ctx, new_dynamic_top as u32);
|
|
|
|
if resized == 0 {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.memory(0).view::<u32>()[dynamictop_ptr].set(new_dynamic_top as u32);
|
|
|
|
return old_dynamic_top as _;
|
|
|
|
}
|
|
|
|
|
2018-11-20 19:11:58 +00:00
|
|
|
/// emscripten: getTotalMemory
|
2019-02-02 23:28:50 +00:00
|
|
|
pub fn get_total_memory(_ctx: &mut Ctx) -> u32 {
|
2018-11-21 03:24:23 +00:00
|
|
|
debug!("emscripten::get_total_memory");
|
2018-11-26 21:06:27 +00:00
|
|
|
// instance.memories[0].current_pages()
|
2019-01-23 07:27:13 +00:00
|
|
|
// TODO: Fix implementation
|
2019-04-04 20:58:05 +00:00
|
|
|
_ctx.memory(0).size().bytes().0 as u32
|
2018-11-20 19:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: enlargeMemory
|
2019-02-02 23:28:50 +00:00
|
|
|
pub fn enlarge_memory(_ctx: &mut Ctx) -> u32 {
|
2018-11-21 03:24:23 +00:00
|
|
|
debug!("emscripten::enlarge_memory");
|
|
|
|
// instance.memories[0].grow(100);
|
2019-01-23 07:27:13 +00:00
|
|
|
// TODO: Fix implementation
|
2019-01-23 18:54:03 +00:00
|
|
|
0
|
2018-11-20 19:11:58 +00:00
|
|
|
}
|
2018-11-24 14:55:21 +00:00
|
|
|
|
|
|
|
/// emscripten: abortOnCannotGrowMemory
|
2019-03-12 21:00:33 +00:00
|
|
|
pub fn abort_on_cannot_grow_memory(ctx: &mut Ctx, _requested_size: u32) -> u32 {
|
|
|
|
debug!(
|
|
|
|
"emscripten::abort_on_cannot_grow_memory {}",
|
|
|
|
_requested_size
|
|
|
|
);
|
2019-02-10 22:24:04 +00:00
|
|
|
abort_with_message(ctx, "Cannot enlarge memory arrays!");
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: abortOnCannotGrowMemory
|
|
|
|
pub fn abort_on_cannot_grow_memory_old(ctx: &mut Ctx) -> u32 {
|
2018-11-24 14:55:21 +00:00
|
|
|
debug!("emscripten::abort_on_cannot_grow_memory");
|
2019-02-09 21:58:05 +00:00
|
|
|
abort_with_message(ctx, "Cannot enlarge memory arrays!");
|
2019-01-23 18:54:03 +00:00
|
|
|
0
|
2018-11-24 14:55:21 +00:00
|
|
|
}
|
2018-12-13 18:28:30 +00:00
|
|
|
|
2019-04-27 19:53:13 +00:00
|
|
|
/// emscripten: segfault
|
|
|
|
pub fn segfault(ctx: &mut Ctx) {
|
|
|
|
debug!("emscripten::segfault");
|
|
|
|
abort_with_message(ctx, "segmentation fault");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: alignfault
|
|
|
|
pub fn alignfault(ctx: &mut Ctx) {
|
|
|
|
debug!("emscripten::alignfault");
|
|
|
|
abort_with_message(ctx, "alignment fault");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emscripten: ftfault
|
|
|
|
pub fn ftfault(ctx: &mut Ctx) {
|
|
|
|
debug!("emscripten::ftfault");
|
|
|
|
abort_with_message(ctx, "Function table mask error");
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:28:30 +00:00
|
|
|
/// emscripten: ___map_file
|
2019-02-09 21:58:05 +00:00
|
|
|
pub fn ___map_file(_ctx: &mut Ctx, _one: u32, _two: u32) -> c_int {
|
2018-12-13 18:28:30 +00:00
|
|
|
debug!("emscripten::___map_file");
|
|
|
|
// NOTE: TODO: Em returns -1 here as well. May need to implement properly
|
|
|
|
-1
|
|
|
|
}
|