Skip to content

Commit

Permalink
Change Tunables::static_memory_bound to bytes (#8616)
Browse files Browse the repository at this point in the history
* Change `Tunables::static_memory_bound` to bytes

This commit changes the wasm-page-sized `static_memory_bound` field to
instead being a byte-defined unit rather than a page-defined unit. To
accomplish this the field is renamed to `static_memory_reservation` and
all references are updated. This builds on the support from #8608 to
remove another page-based variable from the internals of Wasmtime.

* Fix tests
  • Loading branch information
alexcrichton authored May 14, 2024
1 parent aade0c8 commit 010c20f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
12 changes: 4 additions & 8 deletions crates/environ/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,16 @@ pub enum MemoryStyle {
impl MemoryStyle {
/// Decide on an implementation style for the given `Memory`.
pub fn for_memory(memory: Memory, tunables: &Tunables) -> (Self, u64) {
let static_memory_bound = tunables
.static_memory_bound
.checked_mul(u64::from(WASM_PAGE_SIZE))
.unwrap();

let is_static = match memory.maximum_byte_size() {
Ok(mut maximum) => {
if tunables.static_memory_bound_is_maximum {
maximum = maximum.min(static_memory_bound);
maximum = maximum.min(tunables.static_memory_reservation);
}

// Ensure the minimum is less than the maximum; the minimum might exceed the maximum
// when the memory is artificially bounded via `static_memory_bound_is_maximum` above
memory.minimum_byte_size().unwrap() <= maximum && maximum <= static_memory_bound
memory.minimum_byte_size().unwrap() <= maximum
&& maximum <= tunables.static_memory_reservation
}

// If the maximum size of this memory is not representable with
Expand All @@ -55,7 +51,7 @@ impl MemoryStyle {
if is_static {
return (
Self::Static {
byte_reservation: static_memory_bound,
byte_reservation: tunables.static_memory_reservation,
},
tunables.static_memory_offset_guard_size,
);
Expand Down
12 changes: 6 additions & 6 deletions crates/environ/src/tunables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use target_lexicon::{PointerWidth, Triple};
/// Tunable parameters for WebAssembly compilation.
#[derive(Clone, Hash, Serialize, Deserialize, Debug)]
pub struct Tunables {
/// For static heaps, the size in wasm pages of the heap protected by bounds
/// checking.
pub static_memory_bound: u64,
/// For static heaps, the size in bytes of virtual memory reservation for
/// the heap.
pub static_memory_reservation: u64,

/// The size in bytes of the offset guard for static heaps.
pub static_memory_offset_guard_size: u64,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Tunables {
Tunables {
// No virtual memory tricks are available on miri so make these
// limits quite conservative.
static_memory_bound: (1 << 20) / crate::WASM_PAGE_SIZE as u64,
static_memory_reservation: 1 << 20,
static_memory_offset_guard_size: 0,
dynamic_memory_offset_guard_size: 0,
dynamic_memory_growth_reserve: 0,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Tunables {
// For 32-bit we scale way down to 10MB of reserved memory. This
// impacts performance severely but allows us to have more than a
// few instances running around.
static_memory_bound: (10 * (1 << 20)) / crate::WASM_PAGE_SIZE as u64,
static_memory_reservation: 10 * (1 << 20),
static_memory_offset_guard_size: 0x1_0000,
dynamic_memory_offset_guard_size: 0x1_0000,
dynamic_memory_growth_reserve: 1 << 20, // 1MB
Expand All @@ -154,7 +154,7 @@ impl Tunables {
//
// Coupled with a 2 GiB address space guard it lets us translate
// wasm offsets into x86 offsets as aggressively as we can.
static_memory_bound: 0x1_0000,
static_memory_reservation: 1 << 32,
static_memory_offset_guard_size: 0x8000_0000,

// Size in bytes of the offset guard for dynamic memories.
Expand Down
14 changes: 5 additions & 9 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub struct Config {

#[derive(Default, Clone)]
struct ConfigTunables {
static_memory_bound: Option<u64>,
static_memory_reservation: Option<u64>,
static_memory_offset_guard_size: Option<u64>,
dynamic_memory_offset_guard_size: Option<u64>,
dynamic_memory_growth_reserve: Option<u64>,
Expand Down Expand Up @@ -1368,8 +1368,7 @@ impl Config {
/// for pooling allocation by using memory protection; see
/// `PoolingAllocatorConfig::memory_protection_keys` for details.
pub fn static_memory_maximum_size(&mut self, max_size: u64) -> &mut Self {
let max_pages = max_size / u64::from(wasmtime_environ::WASM_PAGE_SIZE);
self.tunables.static_memory_bound = Some(max_pages);
self.tunables.static_memory_reservation = Some(max_size);
self
}

Expand Down Expand Up @@ -1814,7 +1813,7 @@ impl Config {
}

set_fields! {
static_memory_bound
static_memory_reservation
static_memory_offset_guard_size
dynamic_memory_offset_guard_size
dynamic_memory_growth_reserve
Expand Down Expand Up @@ -2178,11 +2177,8 @@ impl fmt::Debug for Config {
if let Some(enable) = self.tunables.parse_wasm_debuginfo {
f.field("parse_wasm_debuginfo", &enable);
}
if let Some(size) = self.tunables.static_memory_bound {
f.field(
"static_memory_maximum_size",
&(u64::from(size) * u64::from(wasmtime_environ::WASM_PAGE_SIZE)),
);
if let Some(size) = self.tunables.static_memory_reservation {
f.field("static_memory_maximum_reservation", &size);
}
if let Some(size) = self.tunables.static_memory_offset_guard_size {
f.field("static_memory_guard_size", &size);
Expand Down
8 changes: 4 additions & 4 deletions crates/wasmtime/src/engine/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl Metadata<'_> {

fn check_tunables(&mut self, other: &Tunables) -> Result<()> {
let Tunables {
static_memory_bound,
static_memory_reservation,
static_memory_offset_guard_size,
dynamic_memory_offset_guard_size,
generate_native_debuginfo,
Expand Down Expand Up @@ -376,9 +376,9 @@ impl Metadata<'_> {
} = self.tunables;

Self::check_int(
static_memory_bound,
other.static_memory_bound,
"static memory bound",
static_memory_reservation,
other.static_memory_reservation,
"static memory reservation",
)?;
Self::check_int(
static_memory_offset_guard_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ mod test {
PoolingInstanceAllocator::new(
&config,
&Tunables {
static_memory_bound: 1,
static_memory_reservation: 65536,
..Tunables::default_host()
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ impl SlabConstraints {
// most bounds checks. `MemoryPool` must respect this bound, though not
// explicitly: if we can achieve the same effect via MPK-protected
// stripes, the slot size can be lower than the `static_memory_bound`.
let expected_slot_bytes = tunables.static_memory_bound * u64::from(WASM_PAGE_SIZE);
let expected_slot_bytes = tunables.static_memory_reservation;

let constraints = SlabConstraints {
max_memory_bytes: max_memory_bytes
Expand Down Expand Up @@ -773,7 +773,7 @@ mod tests {
..Default::default()
},
&Tunables {
static_memory_bound: 1,
static_memory_reservation: 65536,
static_memory_offset_guard_size: 0,
..Tunables::default_host()
},
Expand Down Expand Up @@ -807,7 +807,7 @@ mod tests {
let pool = PoolingInstanceAllocator::new(
&config,
&Tunables {
static_memory_bound: 1,
static_memory_reservation: 65536,
static_memory_offset_guard_size: 0,
..Tunables::default_host()
},
Expand Down

0 comments on commit 010c20f

Please sign in to comment.