Skip to content

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

Validate reservation size in `Config` (#15209)

Alex Crichton0eb6d77091c9
diff --git a/crates/wasmtime/src/config.rs b/crates/wasmtime/src/config.rs
index 92fa6a98cf..098bc8df1f 100644
--- a/crates/wasmtime/src/config.rs
+++ b/crates/wasmtime/src/config.rs
@@ -2919,13 +2919,17 @@ impl Config {
}
// 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.
+ // enough to ever possibly actually get allocated. If this doesn't fit
+ // in the target's address space then there's no hope.
let guard_regions = if tunables.guard_before_linear_memory {
2
} else {
1
};
+ let max_size = match self.compiler_target().pointer_width() {
+ Ok(width) if width.bits() < 64 => (1 << width.bits()) - 1,
+ _ => u64::MAX,
+ };
for (what, reservation, guard_size) in [
(
"memory",
@@ -2941,11 +2945,11 @@ impl Config {
if guard_size
.checked_mul(guard_regions)
.and_then(|g| g.checked_add(reservation))
- .is_none()
+ .is_none_or(|size| size > max_size)
{
bail!(
"`{what}_reservation` ({reservation}) plus `{what}_guard_size` \
- ({guard_size}) overflows"
+ ({guard_size}) overflows the target's address space"
);
}
}
diff --git a/tests/all/pulley.rs b/tests/all/pulley.rs
index 01f7cde4e7..c82a57bd61 100644
--- a/tests/all/pulley.rs
+++ b/tests/all/pulley.rs
@@ -58,6 +58,21 @@ fn pulley_wrong_architecture_is_rejected() -> Result<()> {
Ok(())
}
+#[test]
+fn memory_tunables_must_fit_in_target_address_space() -> Result<()> {
+ let mut config = Config::new();
+ config.target("pulley32")?;
+ config.memory_reservation(1 << 32);
+ assert!(Engine::new(&config).is_err());
+ config.memory_reservation(0).memory_guard_size(1 << 32);
+ assert!(Engine::new(&config).is_err());
+
+ config.target("pulley64")?;
+ config.memory_reservation(1 << 32);
+ Engine::new(&config)?;
+ Ok(())
+}
+
// CLI subcommands should support `--target`
#[test]
#[cfg(not(miri))]