From 6578838acd7bf00c7d22f53673310f8c5c216190 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Mon, 8 Apr 2019 14:44:40 -0700 Subject: [PATCH 1/2] allow resize_heap to grow heap in emscripten --- lib/emscripten/src/memory.rs | 36 +++++++++++++++++++++++++++++++---- lib/runtime-core/src/units.rs | 4 ++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/emscripten/src/memory.rs b/lib/emscripten/src/memory.rs index 9fccbdde4e1..f35dbbb8e2c 100644 --- a/lib/emscripten/src/memory.rs +++ b/lib/emscripten/src/memory.rs @@ -1,6 +1,9 @@ use super::process::abort_with_message; use libc::{c_int, c_void, memcpy, size_t}; -use wasmer_runtime_core::vm::Ctx; +use wasmer_runtime_core::{ + units::{Pages, WASM_MAX_PAGES, WASM_PAGE_SIZE}, + vm::Ctx, +}; /// emscripten: _emscripten_memcpy_big pub fn _emscripten_memcpy_big(ctx: &mut Ctx, dest: u32, src: u32, len: u32) -> u32 { @@ -23,9 +26,34 @@ pub fn _emscripten_get_heap_size(ctx: &mut Ctx) -> u32 { } /// emscripten: _emscripten_resize_heap -pub fn _emscripten_resize_heap(_ctx: &mut Ctx, _requested_size: u32) -> u32 { - debug!("emscripten::_emscripten_resize_heap {}", _requested_size); - // TODO: Fix implementation +/// 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; + + if requested_size > current_memory { + let remainder = (requested_size - current_memory) as usize % WASM_PAGE_SIZE; + let delta = { + let delta = (requested_size - current_memory) as usize / WASM_PAGE_SIZE; + + if remainder != 0 { + delta + 1 + } else { + delta + } + }; + + if current_memory_pages.0 as usize + delta > WASM_MAX_PAGES { + // TODO: handle this? + debug!("Can't exceed max pages"); + return 0; + } + + if let Ok(v) = ctx.memory(0).grow(Pages(delta as u32)) { + return v.0; + } + } 0 } diff --git a/lib/runtime-core/src/units.rs b/lib/runtime-core/src/units.rs index 56d5014384c..982baefcbe6 100644 --- a/lib/runtime-core/src/units.rs +++ b/lib/runtime-core/src/units.rs @@ -4,8 +4,8 @@ use std::{ ops::{Add, Sub}, }; -const WASM_PAGE_SIZE: usize = 65_536; -const WASM_MAX_PAGES: usize = 65_536; +pub const WASM_PAGE_SIZE: usize = 65_536; +pub const WASM_MAX_PAGES: usize = 65_536; /// Units of WebAssembly pages (as specified to be 65,536 bytes). #[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] From 50a22bcae27a7f947ab77966b55b32477f97f4f5 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Mon, 8 Apr 2019 16:17:34 -0700 Subject: [PATCH 2/2] implement emscripten resize_heap --- lib/emscripten/src/memory.rs | 47 +++++++++++++++++++---------------- lib/runtime-core/src/units.rs | 2 ++ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/emscripten/src/memory.rs b/lib/emscripten/src/memory.rs index f35dbbb8e2c..9a087401577 100644 --- a/lib/emscripten/src/memory.rs +++ b/lib/emscripten/src/memory.rs @@ -1,7 +1,7 @@ use super::process::abort_with_message; use libc::{c_int, c_void, memcpy, size_t}; use wasmer_runtime_core::{ - units::{Pages, WASM_MAX_PAGES, WASM_PAGE_SIZE}, + units::{Pages, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE}, vm::Ctx, }; @@ -25,6 +25,14 @@ pub fn _emscripten_get_heap_size(ctx: &mut Ctx) -> u32 { ctx.memory(0).size().bytes().0 as u32 } +// 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 { @@ -32,29 +40,26 @@ pub fn _emscripten_resize_heap(ctx: &mut Ctx, requested_size: u32) -> u32 { let current_memory_pages = ctx.memory(0).size(); let current_memory = current_memory_pages.bytes().0 as u32; - if requested_size > current_memory { - let remainder = (requested_size - current_memory) as usize % WASM_PAGE_SIZE; - let delta = { - let delta = (requested_size - current_memory) as usize / WASM_PAGE_SIZE; - - if remainder != 0 { - delta + 1 - } else { - delta - } - }; - - if current_memory_pages.0 as usize + delta > WASM_MAX_PAGES { - // TODO: handle this? - debug!("Can't exceed max pages"); - return 0; + // 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, + ); } + } - if let Ok(v) = ctx.memory(0).grow(Pages(delta as u32)) { - return v.0; - } + 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 } - 0 } /// emscripten: getTotalMemory diff --git a/lib/runtime-core/src/units.rs b/lib/runtime-core/src/units.rs index 982baefcbe6..e8232efc8aa 100644 --- a/lib/runtime-core/src/units.rs +++ b/lib/runtime-core/src/units.rs @@ -6,6 +6,8 @@ use std::{ pub const WASM_PAGE_SIZE: usize = 65_536; pub const WASM_MAX_PAGES: usize = 65_536; +// From emscripten resize_heap implementation +pub const WASM_MIN_PAGES: usize = 256; /// Units of WebAssembly pages (as specified to be 65,536 bytes). #[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]