Skip to content

Commit 34504fe

Browse files
authored
Move memory_init_cow option to Tunables (#9632)
This commit moves the configuration option from `Config` to `Tunables` to ensure that the memory allocation backend has access to it and then it's used to specifically avoid choosing a malloc-based memory because CoW isn't compatible with malloc.
1 parent b502cf9 commit 34504fe

File tree

7 files changed

+19
-8
lines changed

7 files changed

+19
-8
lines changed

crates/environ/src/tunables.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ define_tunables! {
117117
/// Whether or not the host will be using native signals (e.g. SIGILL,
118118
/// SIGSEGV, etc) to implement traps.
119119
pub signals_based_traps: bool,
120+
121+
/// Whether CoW images might be used to initialize linear memories.
122+
pub memory_init_cow: bool,
120123
}
121124

122125
pub struct ConfigTunables {
@@ -175,6 +178,7 @@ impl Tunables {
175178
relaxed_simd_deterministic: false,
176179
winch_callable: false,
177180
signals_based_traps: true,
181+
memory_init_cow: true,
178182
}
179183
}
180184

crates/wasmtime/src/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl FunctionIndices {
796796
// can either at runtime be implemented as a single memcpy to
797797
// initialize memory or otherwise enabling virtual-memory-tricks
798798
// such as mmap'ing from a file to get copy-on-write.
799-
if engine.config().memory_init_cow {
799+
if engine.tunables().memory_init_cow {
800800
let align = compiler.page_size_align();
801801
let max_always_allowed = engine.config().memory_guaranteed_dense_image_size;
802802
translation.try_static_init(align, max_always_allowed);

crates/wasmtime/src/config.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ pub struct Config {
144144
pub(crate) async_support: bool,
145145
pub(crate) module_version: ModuleVersionStrategy,
146146
pub(crate) parallel_compilation: bool,
147-
pub(crate) memory_init_cow: bool,
148147
pub(crate) memory_guaranteed_dense_image_size: u64,
149148
pub(crate) force_memory_init_memfd: bool,
150149
pub(crate) wmemcheck: bool,
@@ -246,9 +245,6 @@ impl Config {
246245
async_support: false,
247246
module_version: ModuleVersionStrategy::default(),
248247
parallel_compilation: !cfg!(miri),
249-
// If signals are disabled then virtual memory is probably mostly
250-
// disabled so also disable the use of CoW by default.
251-
memory_init_cow: cfg!(feature = "signals-based-traps"),
252248
memory_guaranteed_dense_image_size: 16 << 20,
253249
force_memory_init_memfd: false,
254250
wmemcheck: false,
@@ -1791,7 +1787,7 @@ impl Config {
17911787
/// [IPI]: https://en.wikipedia.org/wiki/Inter-processor_interrupt
17921788
#[cfg(feature = "signals-based-traps")]
17931789
pub fn memory_init_cow(&mut self, enable: bool) -> &mut Self {
1794-
self.memory_init_cow = enable;
1790+
self.tunables.memory_init_cow = Some(enable);
17951791
self
17961792
}
17971793

@@ -2051,6 +2047,7 @@ impl Config {
20512047
tunables.memory_reservation = 0;
20522048
tunables.memory_guard_size = 0;
20532049
tunables.memory_reservation_for_growth = 1 << 20; // 1MB
2050+
tunables.memory_init_cow = false;
20542051
}
20552052

20562053
self.tunables.configure(&mut tunables);
@@ -2081,7 +2078,7 @@ impl Config {
20812078
// the defaults here.
20822079
if !cfg!(feature = "signals-based-traps") {
20832080
assert!(!tunables.signals_based_traps);
2084-
assert!(!self.memory_init_cow);
2081+
assert!(!tunables.memory_init_cow);
20852082
}
20862083

20872084
Ok((tunables, features))

crates/wasmtime/src/engine/serialization.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ impl Metadata<'_> {
376376
relaxed_simd_deterministic,
377377
winch_callable,
378378
signals_based_traps,
379+
memory_init_cow,
379380
// This doesn't affect compilation, it's just a runtime setting.
380381
memory_reservation_for_growth: _,
381382

@@ -438,6 +439,11 @@ impl Metadata<'_> {
438439
other.signals_based_traps,
439440
"Signals-based traps",
440441
)?;
442+
Self::check_bool(
443+
memory_init_cow,
444+
other.memory_init_cow,
445+
"memory initialization with CoW",
446+
)?;
441447

442448
Ok(())
443449
}

crates/wasmtime/src/runtime/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ fn _assert_send_sync() {
11631163
fn memory_images(engine: &Engine, module: &CompiledModule) -> Result<Option<ModuleMemoryImages>> {
11641164
// If initialization via copy-on-write is explicitly disabled in
11651165
// configuration then this path is skipped entirely.
1166-
if !engine.config().memory_init_cow {
1166+
if !engine.tunables().memory_init_cow {
11671167
return Ok(None);
11681168
}
11691169

crates/wasmtime/src/runtime/vm/memory.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl RuntimeMemoryCreator for DefaultMemoryCreator {
131131
if tunables.signals_based_traps
132132
|| tunables.memory_guard_size > 0
133133
|| tunables.memory_reservation > 0
134+
|| tunables.memory_init_cow
134135
{
135136
return Ok(Box::new(MmapMemory::new(ty, tunables, minimum, maximum)?));
136137
}

crates/wasmtime/src/runtime/vm/memory/malloc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ impl MallocMemory {
3434
if tunables.memory_reservation > 0 {
3535
bail!("malloc memory is only compatible with no ahead-of-time memory reservation");
3636
}
37+
if tunables.memory_init_cow {
38+
bail!("malloc memory cannot be used with CoW images");
39+
}
3740

3841
let byte_size = minimum
3942
.checked_add(

0 commit comments

Comments
 (0)