A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix some add-with-overflow with memory tunables (#14590)
Alex Crichton8a0192c2f0ec
diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rsindex 14d4d2ae0e..3bfe294c9d 100644--- a/crates/wasmtime/src/config.rs+++ b/crates/wasmtime/src/config.rs@@ -2801,6 +2801,38 @@ impl Config {tunables.gc_heap_may_move = tunables.memory_may_move;}+ // Validate that the configuration of reservation/guards is sensible+ // enough to ever possibly actually get allocated. If this overflows a+ // u64 then there's no hope.+ let guard_regions = if tunables.guard_before_linear_memory {+ 2+ } else {+ 1+ };+ for (what, reservation, guard_size) in [+ (+ "memory",+ tunables.memory_reservation,+ tunables.memory_guard_size,+ ),+ (+ "gc_heap",+ tunables.gc_heap_reservation,+ tunables.gc_heap_guard_size,+ ),+ ] {+ if guard_size+ .checked_mul(guard_regions)+ .and_then(|g| g.checked_add(reservation))+ .is_none()+ {+ bail!(+ "`{what}_reservation` ({reservation}) plus `{what}_guard_size` \+ ({guard_size}) overflows"+ );+ }+ }+// If we're going to compile with winch, we must use the winch calling convention.#[cfg(any(feature = "cranelift", feature = "winch"))]{diff --git a/tests/all/memory.rs b/tests/all/memory.rsindex f321f5d98c..89ed1ec0b8 100644--- a/tests/all/memory.rs+++ b/tests/all/memory.rs@@ -451,6 +451,29 @@ fn static_forced_max() -> Result<()> {Ok(())}+#[test]+fn reservation_plus_guard_size_overflow() {+ let mut config = Config::new();+ config.memory_guard_size(u64::MAX);+ assert!(Engine::new(&config).is_err());++ let mut config = Config::new();+ config.memory_reservation(1 << 63);+ config.memory_guard_size(1 << 63);+ assert!(Engine::new(&config).is_err());++ let mut config = Config::new();+ config.memory_reservation(0);+ config.memory_guard_size(1 << 63);+ config.guard_before_linear_memory(true);+ assert!(Engine::new(&config).is_err());++ let mut config = Config::new();+ config.gc_heap_reservation(1 << 63);+ config.gc_heap_guard_size(1 << 63);+ assert!(Engine::new(&config).is_err());+}+#[wasmtime_test]fn dynamic_extra_growth_unchanged_pointer(config: &mut Config) -> Result<()> {const EXTRA_PAGES: u64 = 5;