Add a configuration option to force "static" memories (#3503)
* Add a configuration option to force "static" memories In poking around at some things earlier today I realized that one configuration option for memories we haven't exposed from embeddings like the CLI is to forcibly limit the size of memory growth and force using a static memory style. This means that the CLI, for example, can't limit memory growth by default and memories are only limited in size by what the OS can give and the wasm's own memory type. This configuration option means that the CLI can artificially limit the size of wasm linear memories. Additionally another motivation for this is for testing out various codegen ramifications of static/dynamic memories. This is the only way to force a static memory, by default, for wasm64 memories with no maximum size listed for example. * Review feedback
This commit is contained in:
@@ -90,7 +90,7 @@ pub struct MmapMemory {
|
|||||||
|
|
||||||
impl MmapMemory {
|
impl MmapMemory {
|
||||||
/// Create a new linear memory instance with specified minimum and maximum number of wasm pages.
|
/// Create a new linear memory instance with specified minimum and maximum number of wasm pages.
|
||||||
pub fn new(plan: &MemoryPlan, minimum: usize, maximum: Option<usize>) -> Result<Self> {
|
pub fn new(plan: &MemoryPlan, minimum: usize, mut maximum: Option<usize>) -> Result<Self> {
|
||||||
// It's a programmer error for these two configuration values to exceed
|
// It's a programmer error for these two configuration values to exceed
|
||||||
// the host available address space, so panic if such a configuration is
|
// the host available address space, so panic if such a configuration is
|
||||||
// found (mostly an issue for hypothetical 32-bit hosts).
|
// found (mostly an issue for hypothetical 32-bit hosts).
|
||||||
@@ -98,13 +98,21 @@ impl MmapMemory {
|
|||||||
let pre_guard_bytes = usize::try_from(plan.pre_guard_size).unwrap();
|
let pre_guard_bytes = usize::try_from(plan.pre_guard_size).unwrap();
|
||||||
|
|
||||||
let (alloc_bytes, extra_to_reserve_on_growth) = match plan.style {
|
let (alloc_bytes, extra_to_reserve_on_growth) = match plan.style {
|
||||||
|
// Dynamic memories start with the minimum size plus the `reserve`
|
||||||
|
// amount specified to grow into.
|
||||||
MemoryStyle::Dynamic { reserve } => (minimum, usize::try_from(reserve).unwrap()),
|
MemoryStyle::Dynamic { reserve } => (minimum, usize::try_from(reserve).unwrap()),
|
||||||
|
|
||||||
|
// Static memories will never move in memory and consequently get
|
||||||
|
// their entire allocation up-front with no extra room to grow into.
|
||||||
|
// Note that the `maximum` is adjusted here to whatever the smaller
|
||||||
|
// of the two is, the `maximum` given or the `bound` specified for
|
||||||
|
// this memory.
|
||||||
MemoryStyle::Static { bound } => {
|
MemoryStyle::Static { bound } => {
|
||||||
assert_ge!(bound, plan.memory.minimum);
|
assert_ge!(bound, plan.memory.minimum);
|
||||||
(
|
let bound_bytes =
|
||||||
usize::try_from(bound.checked_mul(WASM_PAGE_SIZE_U64).unwrap()).unwrap(),
|
usize::try_from(bound.checked_mul(WASM_PAGE_SIZE_U64).unwrap()).unwrap();
|
||||||
0,
|
maximum = Some(bound_bytes.min(maximum.unwrap_or(usize::MAX)));
|
||||||
)
|
(bound_bytes, 0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let request_bytes = pre_guard_bytes
|
let request_bytes = pre_guard_bytes
|
||||||
|
|||||||
@@ -1119,6 +1119,22 @@ impl Config {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Indicates that the "static" style of memory should always be used.
|
||||||
|
///
|
||||||
|
/// This configuration option enables selecting the "static" option for all
|
||||||
|
/// linear memories created within this `Config`. This means that all
|
||||||
|
/// memories will be allocated up-front and will never move. Additionally
|
||||||
|
/// this means that all memories are synthetically limited by the
|
||||||
|
/// [`Config::static_memory_maximum_size`] option, irregardless of what the
|
||||||
|
/// actual maximum size is on the memory's original type.
|
||||||
|
///
|
||||||
|
/// For the difference between static and dynamic memories, see the
|
||||||
|
/// [`Config::static_memory_maximum_size`].
|
||||||
|
pub fn static_memory_forced(&mut self, force: bool) -> &mut Self {
|
||||||
|
self.tunables.static_memory_bound_is_maximum = force;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Configures the size, in bytes, of the guard region used at the end of a
|
/// Configures the size, in bytes, of the guard region used at the end of a
|
||||||
/// static memory's address space reservation.
|
/// static memory's address space reservation.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -244,6 +244,10 @@ struct CommonOptions {
|
|||||||
#[structopt(long, value_name = "MAXIMUM")]
|
#[structopt(long, value_name = "MAXIMUM")]
|
||||||
static_memory_maximum_size: Option<u64>,
|
static_memory_maximum_size: Option<u64>,
|
||||||
|
|
||||||
|
/// Force using a "static" style for all wasm memories.
|
||||||
|
#[structopt(long)]
|
||||||
|
static_memory_forced: bool,
|
||||||
|
|
||||||
/// Byte size of the guard region after static memories are allocated.
|
/// Byte size of the guard region after static memories are allocated.
|
||||||
#[structopt(long, value_name = "SIZE")]
|
#[structopt(long, value_name = "SIZE")]
|
||||||
static_memory_guard_size: Option<u64>,
|
static_memory_guard_size: Option<u64>,
|
||||||
@@ -319,6 +323,8 @@ impl CommonOptions {
|
|||||||
config.static_memory_maximum_size(max);
|
config.static_memory_maximum_size(max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.static_memory_forced(self.static_memory_forced);
|
||||||
|
|
||||||
if let Some(size) = self.static_memory_guard_size {
|
if let Some(size) = self.static_memory_guard_size {
|
||||||
config.static_memory_guard_size(size);
|
config.static_memory_guard_size(size);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -362,3 +362,17 @@ fn tiny_static_heap() -> Result<()> {
|
|||||||
f.call(&mut store, ())?;
|
f.call(&mut store, ())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn static_forced_max() -> Result<()> {
|
||||||
|
let mut config = Config::new();
|
||||||
|
config.static_memory_maximum_size(5 * 65536);
|
||||||
|
config.static_memory_forced(true);
|
||||||
|
let engine = Engine::new(&config)?;
|
||||||
|
let mut store = Store::new(&engine, ());
|
||||||
|
|
||||||
|
let mem = Memory::new(&mut store, MemoryType::new(0, None))?;
|
||||||
|
mem.grow(&mut store, 5).unwrap();
|
||||||
|
assert!(mem.grow(&mut store, 1).is_err());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user