Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions crates/c-api/include/wasmtime/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,24 @@ WASMTIME_CONFIG_PROP(void, cranelift_opt_level, wasmtime_opt_level_t)
WASMTIME_CONFIG_PROP(void, profiler, wasmtime_profiling_strategy_t)

/**
* \brief Configures the “static” style of memory to always be used.
* \brief Configures whether `memory_reservation` is the maximal size of linear
* memory.
*
* This setting is `false` by default.
*
* For more information see the Rust documentation at
* https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Config.html#method.static_memory_forced.
* https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Config.html#method.memory_may_move.
*/
WASMTIME_CONFIG_PROP(void, static_memory_forced, bool)
WASMTIME_CONFIG_PROP(void, memory_may_move, bool)

/**
* \brief Configures the maximum size for memory to be considered "static"
* \brief Configures the size, in bytes, of initial memory reservation size for
* linear memories.
*
* For more information see the Rust documentation at
* https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Config.html#method.static_memory_maximum_size.
* https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Config.html#method.memory_reservation.
*/
WASMTIME_CONFIG_PROP(void, static_memory_maximum_size, uint64_t)
WASMTIME_CONFIG_PROP(void, memory_reservation, uint64_t)

/**
* \brief Configures the guard region size for linear memory.
Expand All @@ -343,12 +345,12 @@ WASMTIME_CONFIG_PROP(void, memory_guard_size, uint64_t)

/**
* \brief Configures the size, in bytes, of the extra virtual memory space
* reserved after a “dynamic” memory for growing into.
* reserved for memories to grow into after being relocated.
*
* For more information see the Rust documentation at
* https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.dynamic_memory_reserved_for_growth
* https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.memory_reservation_for_growth
*/
WASMTIME_CONFIG_PROP(void, dynamic_memory_reserved_for_growth, uint64_t)
WASMTIME_CONFIG_PROP(void, memory_reservation_for_growth, uint64_t)

/**
* \brief Configures whether to generate native unwind information (e.g.
Expand Down
12 changes: 6 additions & 6 deletions crates/c-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ pub unsafe extern "C" fn wasmtime_config_cache_config_load(
}

#[no_mangle]
pub extern "C" fn wasmtime_config_static_memory_forced_set(c: &mut wasm_config_t, enable: bool) {
c.config.static_memory_forced(enable);
pub extern "C" fn wasmtime_config_memory_may_move_set(c: &mut wasm_config_t, enable: bool) {
c.config.memory_may_move(enable);
}

#[no_mangle]
pub extern "C" fn wasmtime_config_static_memory_maximum_size_set(c: &mut wasm_config_t, size: u64) {
c.config.static_memory_maximum_size(size);
pub extern "C" fn wasmtime_config_memory_reservation_set(c: &mut wasm_config_t, size: u64) {
c.config.memory_reservation(size);
}

#[no_mangle]
Expand All @@ -237,11 +237,11 @@ pub extern "C" fn wasmtime_config_memory_guard_size_set(c: &mut wasm_config_t, s
}

#[no_mangle]
pub extern "C" fn wasmtime_config_dynamic_memory_reserved_for_growth_set(
pub extern "C" fn wasmtime_config_memory_reservation_reserved_for_growth_set(
c: &mut wasm_config_t,
size: u64,
) {
c.config.dynamic_memory_reserved_for_growth(size);
c.config.memory_reservation_for_growth(size);
}

#[no_mangle]
Expand Down
47 changes: 33 additions & 14 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@ wasmtime_option_group! {
/// Optimization level of generated code (0-2, s; default: 2)
pub opt_level: Option<wasmtime::OptLevel>,

/// Force using a "static" style for all wasm memories
pub static_memory_forced: Option<bool>,
/// Do not allow Wasm linear memories to move in the host process's
/// address space.
pub memory_may_move: Option<bool>,

/// Maximum size in bytes of wasm memory before it becomes dynamically
/// relocatable instead of up-front-reserved.
pub static_memory_maximum_size: Option<u64>,
/// Initial virtual memory allocation size for memories.
pub memory_reservation: Option<u64>,

/// Bytes to reserve at the end of linear memory for growth into.
pub memory_reservation_for_growth: Option<u64>,

/// Size, in bytes, of guard pages for linear memories.
pub memory_guard_size: Option<u64>,

/// Bytes to reserve at the end of linear memory for growth for dynamic
/// memories.
pub dynamic_memory_reserved_for_growth: Option<u64>,

/// Indicates whether an unmapped region of memory is placed before all
/// linear memories.
pub guard_before_linear_memory: Option<bool>,
Expand Down Expand Up @@ -170,6 +169,15 @@ wasmtime_option_group! {

/// DEPRECATED: Use `-Cmemory-guard-size=N` instead.
pub static_memory_guard_size: Option<u64>,

/// DEPRECATED: Use `-Cmemory-may-move` instead.
pub static_memory_forced: Option<bool>,

/// DEPRECATED: Use `-Cmemory-reservation=N` instead.
pub static_memory_maximum_size: Option<u64>,

/// DEPRECATED: Use `-Cmemory-reservation-for-growth=N` instead.
pub dynamic_memory_reserved_for_growth: Option<u64>,
}

enum Optimize {
Expand Down Expand Up @@ -631,12 +639,19 @@ impl CommonOptions {
true => err,
}

if let Some(max) = self.opts.static_memory_maximum_size {
config.static_memory_maximum_size(max);
if let Some(max) = self
.opts
.memory_reservation
.or(self.opts.static_memory_maximum_size)
{
config.memory_reservation(max);
}

if let Some(enable) = self.opts.static_memory_forced {
config.static_memory_forced(enable);
config.memory_may_move(!enable);
}
if let Some(enable) = self.opts.memory_may_move {
config.memory_may_move(enable);
}

if let Some(size) = self
Expand All @@ -648,8 +663,12 @@ impl CommonOptions {
config.memory_guard_size(size);
}

if let Some(size) = self.opts.dynamic_memory_reserved_for_growth {
config.dynamic_memory_reserved_for_growth(size);
if let Some(size) = self
.opts
.memory_reservation_for_growth
.or(self.opts.dynamic_memory_reserved_for_growth)
{
config.memory_reservation_for_growth(size);
}
if let Some(enable) = self.opts.guard_before_linear_memory {
config.guard_before_linear_memory(enable);
Expand Down
25 changes: 13 additions & 12 deletions crates/environ/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,29 @@ impl MemoryStyle {
&& tunables.signals_based_traps
&& match memory.maximum_byte_size() {
Ok(mut maximum) => {
if tunables.static_memory_bound_is_maximum {
maximum = maximum.min(tunables.static_memory_reservation);
if !tunables.memory_may_move {
maximum = maximum.min(tunables.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
// Ensure the minimum is less than the maximum; the minimum
// might exceed the maximum when the memory is artificially
// bounded via `memory_may_move` above
memory.minimum_byte_size().unwrap() <= maximum
&& maximum <= tunables.static_memory_reservation
&& maximum <= tunables.memory_reservation
}

// If the maximum size of this memory is not representable with
// `u64` then use the `static_memory_bound_is_maximum` to indicate
// whether it's a static memory or not. It should be ok to discard
// the linear memory's maximum size here as growth to the maximum is
// always fallible and never guaranteed.
Err(_) => tunables.static_memory_bound_is_maximum,
// `u64` then use the `memory_may_move` to indicate whether
// it's a static memory or not. It should be ok to discard the
// linear memory's maximum size here as growth to the maximum
// is always fallible and never guaranteed.
Err(_) => !tunables.memory_may_move,
};

if is_static {
return (
Self::Static {
byte_reservation: tunables.static_memory_reservation,
byte_reservation: tunables.memory_reservation,
},
tunables.memory_guard_size,
);
Expand All @@ -65,7 +66,7 @@ impl MemoryStyle {
// Otherwise, make it dynamic.
(
Self::Dynamic {
reserve: tunables.dynamic_memory_growth_reserve,
reserve: tunables.memory_reservation_for_growth,
},
tunables.memory_guard_size,
)
Expand Down
33 changes: 15 additions & 18 deletions crates/environ/src/tunables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ define_tunables! {
/// GC objects and barriers that must be emitted in Wasm code.
pub collector: Option<Collector>,

/// For static heaps, the size in bytes of virtual memory reservation for
/// the heap.
pub static_memory_reservation: u64,
/// Initial size, in bytes, to be allocated for linear memories.
pub memory_reservation: u64,

/// The size, in bytes, of the guard page region for linear memories.
pub memory_guard_size: u64,

/// The size, in bytes, of reserved memory at the end of a "dynamic" memory,
/// before the guard page, that memory can grow into. This is intended to
/// amortize the cost of `memory.grow` in the same manner that `Vec<T>` has
/// space not in use to grow into.
pub dynamic_memory_growth_reserve: u64,
/// The size, in bytes, to allocate at the end of a relocated linear
/// memory for growth.
pub memory_reservation_for_growth: u64,

/// Whether or not to generate native DWARF debug information.
pub generate_native_debuginfo: bool,
Expand All @@ -88,9 +85,9 @@ define_tunables! {
/// Whether or not we use epoch-based interruption.
pub epoch_interruption: bool,

/// Whether or not to treat the static memory bound as the maximum for
/// unbounded heaps.
pub static_memory_bound_is_maximum: bool,
/// Whether or not linear memories are allowed to be reallocated after
/// initial allocation at runtime.
pub memory_may_move: bool,

/// Whether or not linear memory allocations will have a guard region at the
/// beginning of the allocation in addition to the end.
Expand Down Expand Up @@ -160,17 +157,17 @@ impl Tunables {

// No virtual memory tricks are available on miri so make these
// limits quite conservative.
static_memory_reservation: 1 << 20,
memory_reservation: 1 << 20,
memory_guard_size: 0,
dynamic_memory_growth_reserve: 0,
memory_reservation_for_growth: 0,

// General options which have the same defaults regardless of
// architecture.
generate_native_debuginfo: false,
parse_wasm_debuginfo: true,
consume_fuel: false,
epoch_interruption: false,
static_memory_bound_is_maximum: false,
memory_may_move: true,
guard_before_linear_memory: true,
table_lazy_init: true,
generate_address_map: true,
Expand All @@ -187,9 +184,9 @@ 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_reservation: 10 * (1 << 20),
memory_reservation: 10 * (1 << 20),
memory_guard_size: 0x1_0000,
dynamic_memory_growth_reserve: 1 << 20, // 1MB
memory_reservation_for_growth: 1 << 20, // 1MB

..Tunables::default_miri()
}
Expand All @@ -204,14 +201,14 @@ 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_reservation: 1 << 32,
memory_reservation: 1 << 32,
memory_guard_size: 0x8000_0000,

// We've got lots of address space on 64-bit so use a larger
// grow-into-this area, but on 32-bit we aren't as lucky. Miri is
// not exactly fast so reduce memory consumption instead of trying
// to avoid memory movement.
dynamic_memory_growth_reserve: 2 << 30, // 2GB
memory_reservation_for_growth: 2 << 30, // 2GB

..Tunables::default_miri()
}
Expand Down
16 changes: 8 additions & 8 deletions crates/fuzzing/src/generators/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Config {
pooling.core_instance_size = 1_000_000;

if let MemoryConfig::Normal(cfg) = &mut self.wasmtime.memory_config {
match &mut cfg.static_memory_maximum_size {
match &mut cfg.memory_reservation {
Some(size) => *size = (*size).max(pooling.max_memory_size as u64),
other @ None => *other = Some(pooling.max_memory_size as u64),
}
Expand Down Expand Up @@ -266,9 +266,9 @@ impl Config {
// supported when bounds checks are elided.
let memory_config = if pcc {
MemoryConfig::Normal(NormalMemoryConfig {
static_memory_maximum_size: Some(4 << 30), // 4 GiB
memory_guard_size: Some(2 << 30), // 2 GiB
dynamic_memory_reserved_for_growth: Some(0),
memory_reservation: Some(4 << 30), // 4 GiB
memory_guard_size: Some(2 << 30), // 2 GiB
memory_reservation_for_growth: Some(0),
guard_before_linear_memory: false,
memory_init_cow: true,
// Doesn't matter, only using virtual memory.
Expand All @@ -284,9 +284,9 @@ impl Config {
}
MemoryConfig::CustomUnaligned => {
cfg.with_host_memory(Arc::new(UnalignedMemoryCreator))
.static_memory_maximum_size(0)
.memory_reservation(0)
.memory_guard_size(0)
.dynamic_memory_reserved_for_growth(0)
.memory_reservation_for_growth(0)
.guard_before_linear_memory(false)
.memory_init_cow(false);
}
Expand Down Expand Up @@ -519,7 +519,7 @@ impl WasmtimeConfig {
.min(config.max_memory64_bytes.try_into().unwrap_or(u64::MAX));
let mut min = min_bytes.min(pooling.max_memory_size as u64);
if let MemoryConfig::Normal(cfg) = &self.memory_config {
min = min.min(cfg.static_memory_maximum_size.unwrap_or(0));
min = min.min(cfg.memory_reservation.unwrap_or(0));
}
pooling.max_memory_size = min as usize;
config.max_memory32_bytes = min;
Expand All @@ -534,7 +534,7 @@ impl WasmtimeConfig {
config.max_memory32_bytes = 1 << 16;
config.max_memory64_bytes = 1 << 16;
if let MemoryConfig::Normal(cfg) = &mut self.memory_config {
match &mut cfg.static_memory_maximum_size {
match &mut cfg.memory_reservation {
Some(size) => *size = (*size).max(pooling.max_memory_size as u64),
size @ None => *size = Some(pooling.max_memory_size as u64),
}
Expand Down
14 changes: 6 additions & 8 deletions crates/fuzzing/src/generators/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ pub enum MemoryConfig {
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[allow(missing_docs)]
pub struct NormalMemoryConfig {
pub static_memory_maximum_size: Option<u64>,
pub memory_reservation: Option<u64>,
pub memory_guard_size: Option<u64>,
pub dynamic_memory_reserved_for_growth: Option<u64>,
pub memory_reservation_for_growth: Option<u64>,
pub guard_before_linear_memory: bool,
pub cranelift_enable_heap_access_spectre_mitigations: Option<bool>,
pub memory_init_cow: bool,
Expand All @@ -148,9 +148,9 @@ impl<'a> Arbitrary<'a> for NormalMemoryConfig {
// This attempts to limit memory and guard sizes to 32-bit ranges so
// we don't exhaust a 64-bit address space easily.
Ok(Self {
static_memory_maximum_size: <Option<u32> as Arbitrary>::arbitrary(u)?.map(Into::into),
memory_reservation: <Option<u32> as Arbitrary>::arbitrary(u)?.map(Into::into),
memory_guard_size: <Option<u32> as Arbitrary>::arbitrary(u)?.map(Into::into),
dynamic_memory_reserved_for_growth: <Option<u32> as Arbitrary>::arbitrary(u)?
memory_reservation_for_growth: <Option<u32> as Arbitrary>::arbitrary(u)?
.map(Into::into),
guard_before_linear_memory: u.arbitrary()?,
cranelift_enable_heap_access_spectre_mitigations: u.arbitrary()?,
Expand All @@ -163,11 +163,9 @@ impl NormalMemoryConfig {
/// Apply this memory configuration to the given `wasmtime::Config`.
pub fn apply_to(&self, config: &mut wasmtime::Config) {
config
.static_memory_maximum_size(self.static_memory_maximum_size.unwrap_or(0))
.memory_reservation(self.memory_reservation.unwrap_or(0))
.memory_guard_size(self.memory_guard_size.unwrap_or(0))
.dynamic_memory_reserved_for_growth(
self.dynamic_memory_reserved_for_growth.unwrap_or(0),
)
.memory_reservation_for_growth(self.memory_reservation_for_growth.unwrap_or(0))
.guard_before_linear_memory(self.guard_before_linear_memory)
.memory_init_cow(self.memory_init_cow);

Expand Down
2 changes: 1 addition & 1 deletion crates/misc/component-test-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn config() -> Config {
// component model tests create a disproportionate number of instances so
// try to cut down on virtual memory usage by avoiding 4G reservations.
if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
config.static_memory_maximum_size(0);
config.memory_reservation(0);
config.memory_guard_size(0);
}
config
Expand Down
Loading