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.
This commit is contained in:
Peter Huene
2020-12-16 13:00:35 -08:00
parent 8457261cfe
commit 3bb145f65c

View File

@@ -6,7 +6,6 @@ use cranelift_codegen::ir;
use cranelift_entity::{EntityRef, PrimaryMap}; use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_wasm::*; use cranelift_wasm::*;
use indexmap::IndexMap; use indexmap::IndexMap;
use more_asserts::assert_ge;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; 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 // 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. // requested to use the static memory bound itself as the maximum.
let maximum = memory let maximum = std::cmp::min(
.maximum memory.maximum.unwrap_or(WASM_MAX_PAGES),
.unwrap_or(if tunables.static_memory_bound_is_maximum { if tunables.static_memory_bound_is_maximum {
tunables.static_memory_bound tunables.static_memory_bound
} else { } else {
WASM_MAX_PAGES WASM_MAX_PAGES
}); },
);
if maximum <= tunables.static_memory_bound { // Ensure the minimum is less than the maximum; the minimum might exceed the maximum
assert_ge!(tunables.static_memory_bound, memory.minimum); // when the memory is artificially bounded via `static_memory_bound_is_maximum` above
if memory.minimum <= maximum && maximum <= tunables.static_memory_bound {
return ( return (
Self::Static { Self::Static {
bound: tunables.static_memory_bound, bound: tunables.static_memory_bound,