Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix an addition overflow when configuring the pooling allocator (#14864)

Alex Crichton5d0c7b4d2ae4
diff --git a/crates/wasmtime/src/runtime/vm/instance/allocator/pooling.rs b/crates/wasmtime/src/runtime/vm/instance/allocator/pooling.rs
index 207146838e..42d4c498be 100644
--- a/crates/wasmtime/src/runtime/vm/instance/allocator/pooling.rs
+++ b/crates/wasmtime/src/runtime/vm/instance/allocator/pooling.rs
@@ -142,12 +142,6 @@ use wasmtime_environ::{
component::{Component, VMComponentOffsets},
};
-fn round_up_to_pow2(n: usize, to: usize) -> usize {
- debug_assert!(to > 0);
- debug_assert!(to.is_power_of_two());
- (n + to - 1) & !(to - 1)
-}
-
impl PoolingAllocationConfig {
/// Tests whether [`Self::pagemap_scan`] is available or not on the host
/// system.
@@ -313,11 +307,11 @@ impl PoolingInstanceAllocator {
})
}
- fn core_instance_size(&self) -> usize {
- round_up_to_pow2(
- self.config.limits.core_instance_size,
- mem::align_of::<Instance>(),
- )
+ fn max_core_instance_size(&self) -> Option<usize> {
+ self.config
+ .limits
+ .core_instance_size
+ .checked_next_multiple_of(mem::align_of::<Instance>())
}
fn validate_table_plans(&self, module: &Module) -> Result<()> {
@@ -330,7 +324,11 @@ impl PoolingInstanceAllocator {
fn validate_core_instance_size(&self, offsets: &VMOffsets<HostPtr>) -> Result<()> {
let layout = Instance::alloc_layout(offsets);
- if layout.size() <= self.core_instance_size() {
+ let Some(max) = self.max_core_instance_size() else {
+ return Ok(());
+ };
+
+ if layout.size() <= max {
return Ok(());
}
@@ -344,9 +342,8 @@ impl PoolingInstanceAllocator {
let mut message = format!(
"instance allocation for this module \
requires {} bytes which exceeds the configured maximum \
- of {} bytes; breakdown of allocation requirement:\n\n",
+ of {max} bytes; breakdown of allocation requirement:\n\n",
layout.size(),
- self.core_instance_size(),
);
let mut remaining = layout.size();
diff --git a/tests/all/pooling_allocator.rs b/tests/all/pooling_allocator.rs
index 3e4c393f9e..9848e052da 100644
--- a/tests/all/pooling_allocator.rs
+++ b/tests/all/pooling_allocator.rs
@@ -666,6 +666,14 @@ fn instance_too_large() -> Result<()> {
}
}
+ // A maximum near `usize::MAX` shouldn't overflow when rounded up.
+ let mut pool = crate::small_pool_config();
+ pool.max_core_instance_size(usize::MAX);
+ let mut config = Config::new();
+ config.allocation_strategy(pool);
+ let engine = Engine::new(&config)?;
+ Module::new(&engine, "(module)")?;
+
Ok(())
}