diff --git a/crates/environ/src/compile/module_environ.rs b/crates/environ/src/compile/module_environ.rs index f8edcc8455a7..7c90f15960e1 100644 --- a/crates/environ/src/compile/module_environ.rs +++ b/crates/environ/src/compile/module_environ.rs @@ -996,9 +996,20 @@ impl ModuleTranslation<'_> { // Validate that the memory information collected is indeed valid for // static memory initialization. - for info in info.values().filter(|i| i.data_size > 0) { + for (i, info) in info.iter().filter(|(_, info)| info.data_size > 0) { let image_size = info.max_addr - info.min_addr; + // Simplify things for now by bailing out entirely if any memory has + // a page size smaller than the host's page size. This fixes a case + // where currently initializers are created in host-page-size units + // of length which means that a larger-than-the-entire-memory + // initializer can be created. This can be handled technically but + // would require some more changes to help fix the assert elsewhere + // that this protects against. + if self.module.memory_plans[i].memory.page_size() < page_size { + return; + } + // If the range of memory being initialized is less than twice the // total size of the data itself then it's assumed that static // initialization is ok. This means we'll at most double memory diff --git a/crates/wasmtime/src/runtime/vm/instance/allocator.rs b/crates/wasmtime/src/runtime/vm/instance/allocator.rs index 766293eab0fe..ee39886cb884 100644 --- a/crates/wasmtime/src/runtime/vm/instance/allocator.rs +++ b/crates/wasmtime/src/runtime/vm/instance/allocator.rs @@ -732,7 +732,11 @@ fn initialize_memories(instance: &mut Instance, module: &Module) -> Result<()> { unsafe { let src = self.instance.wasm_data(init.data.clone()); - let dst = memory.base.add(usize::try_from(init.offset).unwrap()); + let offset = usize::try_from(init.offset).unwrap(); + let dst = memory.base.add(offset); + + assert!(offset + src.len() <= memory.current_length()); + // FIXME audit whether this is safe in the presence of shared // memory // (https://github.com/bytecodealliance/wasmtime/issues/4203). diff --git a/tests/wast.rs b/tests/wast.rs index 789ab1cb70af..71527d4e32a2 100644 --- a/tests/wast.rs +++ b/tests/wast.rs @@ -294,8 +294,10 @@ fn run_wast(wast: &Path, strategy: Strategy, pooling: bool) -> anyhow::Result<() cfg.static_memory_maximum_size(0); } cfg.dynamic_memory_reserved_for_growth(0); - cfg.static_memory_guard_size(0); - cfg.dynamic_memory_guard_size(0); + + let small_guard = 64 * 1024; + cfg.static_memory_guard_size(small_guard); + cfg.dynamic_memory_guard_size(small_guard); } let _pooling_lock = if pooling {