Skip to content

Commit

Permalink
Avoid dependency on Wasmer's Bytes to Pages conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Dec 14, 2020
1 parent ec8501d commit 33e5b3b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/vm/src/wasm_backend/store.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use std::convert::TryInto;
#[cfg(feature = "cranelift")]
use wasmer::Cranelift;
#[cfg(not(feature = "cranelift"))]
use wasmer::Singlepass;
use wasmer::{
Bytes, Engine, Pages, Store, Target,
Engine, Pages, Store, Target,
Tunables as ReferenceTunables, /* See https://github.com/wasmerio/wasmer/issues/1872 */
JIT,
JIT, WASM_PAGE_SIZE,
};

use crate::size::Size;

use super::limiting_tunables::LimitingTunables;

/// WebAssembly linear memory objects have sizes measured in pages. Each page
/// is 65536 (2^16) bytes. In WebAssembly version 1, a linear memory can have at
/// most 65536 pages, for a total of 2^32 bytes (4 gibibytes).
/// https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md
const MAX_WASM_MEMORY: usize = 4 * 1024 * 1024 * 1024;

/// Created a store with the default compiler and the given memory limit (in bytes)
/// If memory_limit is None, no limit is applied.
pub fn make_store(memory_limit: Option<Size>) -> Store {
Expand Down Expand Up @@ -42,9 +49,13 @@ pub fn make_store_headless(memory_limit: Option<Size>) -> Store {
fn make_store_with_engine(engine: &dyn Engine, memory_limit: Option<Size>) -> Store {
match memory_limit {
Some(limit) => {
let pages = Pages::from(Bytes(limit.0));
let capped = std::cmp::max(limit.0, MAX_WASM_MEMORY);
// round down to ensure the limit is less than or equal to the config
let pages: u32 = (capped / WASM_PAGE_SIZE)
.try_into()
.expect("Value must be <= 4 GiB/64KiB, i.e. fit in uint32. This is a bug.");
let base = ReferenceTunables::for_target(&Target::default());
let tunables = LimitingTunables::new(base, pages);
let tunables = LimitingTunables::new(base, Pages(pages));
Store::new_with_tunables(engine, tunables)
}
None => Store::new(engine),
Expand Down

0 comments on commit 33e5b3b

Please sign in to comment.