Skip to content

Commit 0eb6d77

Browse files
authored
Validate reservation size in Config (#15209)
This commit fixes some possible `unwrap()`s with the new `MemoryRequest` type where some values were coerced from `u64` to `usize` in a way that could panic. This commit moves validation into `Config` to make these conversions always work and seems like a reasonable place to get an error anyway.
1 parent 5619a5d commit 0eb6d77

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎crates/wasmtime/src/config.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,13 +2919,17 @@ impl Config {
29192919
}
29202920

29212921
// Validate that the configuration of reservation/guards is sensible
2922-
// enough to ever possibly actually get allocated. If this overflows a
2923-
// u64 then there's no hope.
2922+
// enough to ever possibly actually get allocated. If this doesn't fit
2923+
// in the target's address space then there's no hope.
29242924
let guard_regions = if tunables.guard_before_linear_memory {
29252925
2
29262926
} else {
29272927
1
29282928
};
2929+
let max_size = match self.compiler_target().pointer_width() {
2930+
Ok(width) if width.bits() < 64 => (1 << width.bits()) - 1,
2931+
_ => u64::MAX,
2932+
};
29292933
for (what, reservation, guard_size) in [
29302934
(
29312935
"memory",
@@ -2941,11 +2945,11 @@ impl Config {
29412945
if guard_size
29422946
.checked_mul(guard_regions)
29432947
.and_then(|g| g.checked_add(reservation))
2944-
.is_none()
2948+
.is_none_or(|size| size > max_size)
29452949
{
29462950
bail!(
29472951
"`{what}_reservation` ({reservation}) plus `{what}_guard_size` \
2948-
({guard_size}) overflows"
2952+
({guard_size}) overflows the target's address space"
29492953
);
29502954
}
29512955
}

‎tests/all/pulley.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ fn pulley_wrong_architecture_is_rejected() -> Result<()> {
5858
Ok(())
5959
}
6060

61+
#[test]
62+
fn memory_tunables_must_fit_in_target_address_space() -> Result<()> {
63+
let mut config = Config::new();
64+
config.target("pulley32")?;
65+
config.memory_reservation(1 << 32);
66+
assert!(Engine::new(&config).is_err());
67+
config.memory_reservation(0).memory_guard_size(1 << 32);
68+
assert!(Engine::new(&config).is_err());
69+
70+
config.target("pulley64")?;
71+
config.memory_reservation(1 << 32);
72+
Engine::new(&config)?;
73+
Ok(())
74+
}
75+
6176
// CLI subcommands should support `--target`
6277
#[test]
6378
#[cfg(not(miri))]

0 commit comments

Comments
 (0)