wasmer/lib/emscripten/src/memory.rs

125 lines
3.8 KiB
Rust
Raw Normal View History

2018-11-24 15:55:21 +01:00
use super::process::abort_with_message;
2018-12-18 09:43:59 -08:00
use libc::{c_int, c_void, memcpy, size_t};
use wasmer_runtime_core::{
2019-04-08 16:17:34 -07:00
units::{Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE},
vm::Ctx,
};
2018-11-20 20:11:58 +01:00
/// emscripten: _emscripten_memcpy_big
pub fn _emscripten_memcpy_big(ctx: &mut Ctx, dest: u32, src: u32, len: u32) -> u32 {
debug!(
"emscripten::_emscripten_memcpy_big {}, {}, {}",
dest, src, len
);
2019-01-24 13:04:12 -08: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-21 20:59:23 -08:00
unsafe {
memcpy(dest_addr, src_addr, len as size_t);
}
2018-11-20 20:11:58 +01:00
dest
}
/// emscripten: _emscripten_get_heap_size
pub fn _emscripten_get_heap_size(ctx: &mut Ctx) -> u32 {
2019-05-03 17:34:57 -07:00
debug!("emscripten::_emscripten_get_heap_size");
let result = ctx.memory(0).size().bytes().0 as u32;
debug!("=> {}", result);
result
}
2019-04-08 16:17:34 -07:00
// From emscripten implementation
fn align_up(mut val: usize, multiple: usize) -> usize {
if val % multiple > 0 {
val += multiple - val % multiple;
}
val
}
/// emscripten: _emscripten_resize_heap
/// 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 16:17:34 -07: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 16:17:34 -07:00
}
2019-04-08 16:17:34 -07: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
}
}
2018-11-20 20:11:58 +01:00
/// emscripten: getTotalMemory
pub fn get_total_memory(_ctx: &mut Ctx) -> u32 {
2018-11-20 19:24:23 -08:00
debug!("emscripten::get_total_memory");
2018-11-26 16:06:27 -05:00
// instance.memories[0].current_pages()
2019-01-23 01:27:13 -06:00
// TODO: Fix implementation
2019-04-04 13:58:05 -07:00
_ctx.memory(0).size().bytes().0 as u32
2018-11-20 20:11:58 +01:00
}
/// emscripten: enlargeMemory
pub fn enlarge_memory(_ctx: &mut Ctx) -> u32 {
2018-11-20 19:24:23 -08:00
debug!("emscripten::enlarge_memory");
// instance.memories[0].grow(100);
2019-01-23 01:27:13 -06:00
// TODO: Fix implementation
2019-01-23 10:54:03 -08:00
0
2018-11-20 20:11:58 +01:00
}
2018-11-24 15:55:21 +01:00
/// emscripten: abortOnCannotGrowMemory
fix(emscripten) Various warning fixes and cleanups (#266) * fix(emscripten) Remove unused imports. This patch removes unused imports reported by `rustc` as warnings. * fix(emscripten) Allow unreachable patterns in `_clock_gettime`. The compiler thinks `CLOCK_MONOTONIC_COARSE` is unreachable, which is not always the case. Add an attribute to allow unreachable patterns to remove the warning. * fix(emscripten) Rename unused variables. This patch renames various unused variables by appending an underscore to them. * fix(emscripten) Declare `table` as immutable. The `table` variable in `EmscriptenGlobals::new` was declared as mutable, but it's never mutated. * fix(emscripten) Remove an unnecessary `unsafe` block. * fix(emscripten) Remove duplicate definition of `SO_NOSIGPIPE`. The `SO_NOSIGPIPE` constant is defined in `syscalls/mod.rs` and `syscalls/unix.rs`. It's never used in the first case. We can safely remove it in this file, and keep it in `unix.rs`. * fix(emscripten) `read_string_from_wasm` is used only on Windows. Mark `read_string_from_wasm` as possible deadcode, since it's used only on Windows. * fix(emscripten) Remove `DYNAMICTOP_PTR_DIFF`, `stacktop`, `stack_max`, `dynamic_base` and `dynamic_ptr`. Four functions and one constant are used together but never used inside or outside this file. They are deadcode. * fix(emscripten) Remove `infinity` and `nan` fields of `EmscriptenGlobalsData`. Those fields are never used. * fix(emscripten) Allow non snake case in `emscripten_target.rs`. Many functions in this file don't follow the snake case style for Rust function names. The reason is that we want the names to match the emscripten symbol names; even if a mapping is done in `lib.rs`, it's easier to get the same names. * fix(emscripten) Rename `STATIC_TOP` to `static_top`. This variable is not a constant.
2019-03-12 22:00:33 +01:00
pub fn abort_on_cannot_grow_memory(ctx: &mut Ctx, _requested_size: u32) -> u32 {
debug!(
"emscripten::abort_on_cannot_grow_memory {}",
_requested_size
);
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 15:55:21 +01:00
debug!("emscripten::abort_on_cannot_grow_memory");
abort_with_message(ctx, "Cannot enlarge memory arrays!");
2019-01-23 10:54:03 -08:00
0
2018-11-24 15:55:21 +01: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");
}
/// emscripten: ___map_file
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
}