From c94cdc7730bc2cf5e97c21678a0e393860dc092d Mon Sep 17 00:00:00 2001 From: Sergei Pepyakin Date: Tue, 18 Feb 2020 15:33:57 +0100 Subject: [PATCH] Treat undeclared maximum as 4GiB (#944) * Treat undeclared maximum as 4GiB * Review fixes. --- crates/environ/src/module.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs index 127e3eb874..f6389c23d2 100644 --- a/crates/environ/src/module.rs +++ b/crates/environ/src/module.rs @@ -2,6 +2,7 @@ use crate::module_environ::FunctionBodyData; use crate::tunables::Tunables; +use crate::WASM_MAX_PAGES; use cranelift_codegen::ir; use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_wasm::{ @@ -55,18 +56,19 @@ pub enum MemoryStyle { impl MemoryStyle { /// Decide on an implementation style for the given `Memory`. pub fn for_memory(memory: Memory, tunables: &Tunables) -> (Self, u64) { - if let Some(maximum) = memory.maximum { - if maximum <= tunables.static_memory_bound { - // A heap with a declared maximum can be immovable, so make - // it static. - assert_ge!(tunables.static_memory_bound, memory.minimum); - return ( - Self::Static { - bound: tunables.static_memory_bound, - }, - tunables.static_memory_offset_guard_size, - ); - } + // A heap with a maximum that doesn't exceed the static memory bound specified by the + // tunables make it static. + // + // If the module doesn't declare an explicit maximum treat it as 4GiB. + let maximum = memory.maximum.unwrap_or(WASM_MAX_PAGES); + if maximum <= tunables.static_memory_bound { + assert_ge!(tunables.static_memory_bound, memory.minimum); + return ( + Self::Static { + bound: tunables.static_memory_bound, + }, + tunables.static_memory_offset_guard_size, + ); } // Otherwise, make it dynamic.