From 3bb145f65c4a3b840d02bd7704c2affe51539d1b Mon Sep 17 00:00:00 2001 From: Peter Huene Date: Wed, 16 Dec 2020 13:00:35 -0800 Subject: [PATCH] Only treat a memory as static when the minimum is also within bounds. With the change to artificially limit unbounded memories based on Tunables, it's possible to hit the assert where the minimum might exceed the static memory bound. This commit removes the assert in favor of a check to see if the minimum also fits within the static memory bound. It also corrects the maximum bounding to ensure the minimum between the memory's maximum and the configured maximum is used. If it does not fit, the memory will be treated as dynamic. In the case of the pooling instance allocator, the bounds will be checked again during translation and an appropriate error will be returned as dynamic memories are not supported for that allocator. --- crates/environ/src/module.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs index 7282501e4a..8daefaf079 100644 --- a/crates/environ/src/module.rs +++ b/crates/environ/src/module.rs @@ -6,7 +6,6 @@ use cranelift_codegen::ir; use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_wasm::*; use indexmap::IndexMap; -use more_asserts::assert_ge; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::Arc; @@ -44,16 +43,18 @@ impl MemoryStyle { // // If the module doesn't declare an explicit maximum treat it as 4GiB when not // requested to use the static memory bound itself as the maximum. - let maximum = memory - .maximum - .unwrap_or(if tunables.static_memory_bound_is_maximum { + let maximum = std::cmp::min( + memory.maximum.unwrap_or(WASM_MAX_PAGES), + if tunables.static_memory_bound_is_maximum { tunables.static_memory_bound } else { WASM_MAX_PAGES - }); + }, + ); - if maximum <= tunables.static_memory_bound { - assert_ge!(tunables.static_memory_bound, memory.minimum); + // Ensure the minimum is less than the maximum; the minimum might exceed the maximum + // when the memory is artificially bounded via `static_memory_bound_is_maximum` above + if memory.minimum <= maximum && maximum <= tunables.static_memory_bound { return ( Self::Static { bound: tunables.static_memory_bound,