Skip to content

Commit

Permalink
Only treat a memory as static when the minimum is also within bounds.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
peterhuene committed Jan 22, 2021
1 parent ae9b4c4 commit af4b3d4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/environ/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit af4b3d4

Please sign in to comment.