Skip to content

Commit 7f9049b

Browse files
authored
Replace signals-based-traps with auto-detection (bytecodealliance#9941)
* Replace `signals-based-traps` with auto-detection This commit refactors the platform support of the `wasmtime` crate itself to remove the previously added `signals-based-traps` feature in favor of auto-detecting whether it's there or not. The `build.rs` script for the `wasmtime` crate will now detect the target platform and auto-enable this feature as necessary. The `signals-based-traps` cargo feature is removed and split into two custom `#[cfg]` directives that the build script sets: * `has_virtual_memory` - this is used to gate mmap implementations for example. This is enabled on `unix || windows` and will be off for `no_std` targets for example. This is split out of "signals-based-traps" to better handle platforms like iOS which have virtual memory but don't execute native code (removing the need for native signals). * `has_native_signals` - gates signal handlers on Unix for example. This is disabled on MIRI but otherwise enabled for `unix || windows`. This is intended to in the future get disabled for iOS by default for example since it's not necessary when using Pulley. This is additionally off-by-default for `no_std` platforms. Two new crate features were added for `no_std` or "custom" platforms to opt-in to the `wasmtime-platform.h` C APIs for implementing virtual memory and signals. These are used in the `min-platform` embedding example. This commit additionally updates some various documentation here and there to be more up-to-date. * Update CI configuration * Fix compile warnings * Fix test on miri * Fix more tests on miri * Fix some warnings * Another round of miri/CI attempts/fixes prtest:full
1 parent 73c4ff2 commit 7f9049b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+290
-291
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,6 @@ jobs:
371371
-p wasmtime --no-default-features --features threads
372372
-p wasmtime --no-default-features --features runtime,threads
373373
-p wasmtime --no-default-features --features cranelift,threads
374-
-p wasmtime --no-default-features --features runtime,signals-based-traps
375-
-p wasmtime --no-default-features --features runtime,gc,component-model,signals-based-traps
376374
-p wasmtime --features incremental-cache
377375
-p wasmtime --all-features
378376
@@ -541,8 +539,6 @@ jobs:
541539
test: >
542540
cargo check -p wasmtime --no-default-features --features runtime,component-model &&
543541
cargo check -p wasmtime --no-default-features --features runtime,gc,component-model &&
544-
cargo check -p wasmtime --no-default-features --features runtime,gc,component-model,signals-based-traps &&
545-
cargo check -p wasmtime --no-default-features --features runtime,gc,component-model,signals-based-traps &&
546542
cargo check -p cranelift-control --no-default-features &&
547543
cargo check -p pulley-interpreter --features encode,decode,disas,interp
548544
# Use `cross` for illumos to have a C compiler/linker available.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ rustix = { workspace = true, features = ["mm", "param", "process"] }
8686

8787
[dev-dependencies]
8888
# depend again on wasmtime to activate its default features for tests
89-
wasmtime = { workspace = true, features = ['default', 'winch', 'pulley', 'all-arch', 'call-hook', 'memory-protection-keys', 'signals-based-traps'] }
89+
wasmtime = { workspace = true, features = ['default', 'winch', 'pulley', 'all-arch', 'call-hook', 'memory-protection-keys'] }
9090
env_logger = { workspace = true }
9191
log = { workspace = true }
9292
filecheck = { workspace = true }
@@ -416,7 +416,6 @@ default = [
416416
"addr2line",
417417
"debug-builtins",
418418
"component-model",
419-
"signals-based-traps",
420419
"threads",
421420
"gc",
422421
"gc-drc",
@@ -475,7 +474,6 @@ threads = ["wasmtime-cli-flags/threads"]
475474
gc = ["wasmtime-cli-flags/gc", "wasmtime/gc"]
476475
gc-drc = ["gc", "wasmtime/gc-drc", "wasmtime-cli-flags/gc-drc"]
477476
gc-null = ["gc", "wasmtime/gc-null", "wasmtime-cli-flags/gc-null"]
478-
signals-based-traps = ["wasmtime/signals-based-traps", "wasmtime-cli-flags/signals-based-traps"]
479477
pulley = ["wasmtime-cli-flags/pulley"]
480478

481479
# CLI subcommands for the `wasmtime` executable. See `wasmtime $cmd --help`

crates/cli-flags/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ gc-drc = ["gc", "wasmtime/gc-drc"]
3535
gc-null = ["gc", "wasmtime/gc-null"]
3636
threads = ["wasmtime/threads"]
3737
memory-protection-keys = ["wasmtime/memory-protection-keys"]
38-
signals-based-traps = ["wasmtime/signals-based-traps"]
3938
pulley = ["wasmtime/pulley"]

crates/cli-flags/src/lib.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -661,47 +661,35 @@ impl CommonOptions {
661661
.opts
662662
.memory_reservation
663663
.or(self.opts.static_memory_maximum_size);
664-
match_feature! {
665-
["signals-based-traps" : memory_reservation]
666-
size => config.memory_reservation(size),
667-
_ => err,
664+
if let Some(size) = memory_reservation {
665+
config.memory_reservation(size);
668666
}
669667

670-
match_feature! {
671-
["signals-based-traps" : self.opts.static_memory_forced]
672-
enable => config.memory_may_move(!enable),
673-
_ => err,
668+
if let Some(enable) = self.opts.static_memory_forced {
669+
config.memory_may_move(!enable);
674670
}
675-
match_feature! {
676-
["signals-based-traps" : self.opts.memory_may_move]
677-
enable => config.memory_may_move(enable),
678-
_ => err,
671+
if let Some(enable) = self.opts.memory_may_move {
672+
config.memory_may_move(enable);
679673
}
680674

681675
let memory_guard_size = self
682676
.opts
683677
.static_memory_guard_size
684678
.or(self.opts.dynamic_memory_guard_size)
685679
.or(self.opts.memory_guard_size);
686-
match_feature! {
687-
["signals-based-traps" : memory_guard_size]
688-
size => config.memory_guard_size(size),
689-
_ => err,
680+
if let Some(size) = memory_guard_size {
681+
config.memory_guard_size(size);
690682
}
691683

692684
let mem_for_growth = self
693685
.opts
694686
.memory_reservation_for_growth
695687
.or(self.opts.dynamic_memory_reserved_for_growth);
696-
match_feature! {
697-
["signals-based-traps" : mem_for_growth]
698-
size => config.memory_reservation_for_growth(size),
699-
_ => err,
688+
if let Some(size) = mem_for_growth {
689+
config.memory_reservation_for_growth(size);
700690
}
701-
match_feature! {
702-
["signals-based-traps" : self.opts.guard_before_linear_memory]
703-
enable => config.guard_before_linear_memory(enable),
704-
_ => err,
691+
if let Some(enable) = self.opts.guard_before_linear_memory {
692+
config.guard_before_linear_memory(enable);
705693
}
706694
if let Some(enable) = self.opts.table_lazy_init {
707695
config.table_lazy_init(enable);
@@ -718,15 +706,11 @@ impl CommonOptions {
718706
if let Some(enable) = self.debug.address_map {
719707
config.generate_address_map(enable);
720708
}
721-
match_feature! {
722-
["signals-based-traps" : self.opts.memory_init_cow]
723-
enable => config.memory_init_cow(enable),
724-
_ => err,
709+
if let Some(enable) = self.opts.memory_init_cow {
710+
config.memory_init_cow(enable);
725711
}
726-
match_feature! {
727-
["signals-based-traps" : self.opts.signals_based_traps]
728-
enable => config.signals_based_traps(enable),
729-
_ => err,
712+
if let Some(enable) = self.opts.signals_based_traps {
713+
config.signals_based_traps(enable);
730714
}
731715
if let Some(enable) = self.codegen.native_unwind_info {
732716
config.native_unwind_info(enable);

crates/fuzzing/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ target-lexicon = { workspace = true }
2626
tempfile = "3.3.0"
2727
wasmparser = { workspace = true }
2828
wasmprinter = { workspace = true }
29-
wasmtime = { workspace = true, features = ['default', 'winch', 'gc', 'memory-protection-keys', 'signals-based-traps', 'pulley'] }
29+
wasmtime = { workspace = true, features = ['default', 'winch', 'gc', 'memory-protection-keys', 'pulley'] }
3030
wasmtime-wast = { workspace = true, features = ['component-model'] }
3131
wasm-encoder = { workspace = true }
3232
wasm-smith = { workspace = true }

crates/test-macros/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ fn expand(test_config: &TestConfig, func: Fn) -> Result<TokenStream> {
210210

211211
for strategy in &test_config.strategies {
212212
let strategy_name = format!("{strategy:?}");
213-
// Winch currently only offers support for x64.
213+
// Winch currently only offers support for x64, and it requires
214+
// signals-based-traps which MIRI disables so disable winch tests on MIRI
214215
let target = if *strategy == Compiler::Winch {
215-
quote! { #[cfg(target_arch = "x86_64")] }
216+
quote! { #[cfg(all(target_arch = "x86_64", not(miri)))] }
216217
} else {
217218
quote! {}
218219
};

crates/wasi-nn/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ cap-std = { workspace = true }
6161
libtest-mimic = { workspace = true }
6262
test-programs-artifacts = { workspace = true }
6363
wasmtime-wasi = { workspace = true, features = ["preview1"] }
64-
wasmtime = { workspace = true, features = ["cranelift", 'signals-based-traps'] }
64+
wasmtime = { workspace = true, features = ["cranelift"] }
6565
tracing-subscriber = { workspace = true }
6666

6767
[features]

crates/wasi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test-log = { workspace = true }
4242
tracing-subscriber = { workspace = true }
4343
test-programs-artifacts = { workspace = true }
4444
tempfile = { workspace = true }
45-
wasmtime = { workspace = true, features = ['cranelift', 'incremental-cache', 'signals-based-traps'] }
45+
wasmtime = { workspace = true, features = ['cranelift', 'incremental-cache'] }
4646

4747
[target.'cfg(unix)'.dependencies]
4848
rustix = { workspace = true, features = ["event", "fs", "net"] }

crates/wasmtime/Cargo.toml

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ default = [
138138
'component-model',
139139
'threads',
140140
'std',
141-
'signals-based-traps',
142141
]
143142

144143
# An on-by-default feature enabling runtime compilation of WebAssembly modules
@@ -193,7 +192,6 @@ async = [
193192
pooling-allocator = [
194193
"runtime",
195194
"std", # not ported to no_std yet
196-
"signals-based-traps", # pooling allocation always uses mmap at this time
197195
]
198196

199197
# Enables support for all architectures in Cranelift, allowing
@@ -250,6 +248,7 @@ runtime = [
250248
"dep:rustix",
251249
"rustix/mm",
252250
"pulley-interpreter?/interp",
251+
"dep:wasmtime-jit-icache-coherence",
253252
]
254253

255254
# Enable support for garbage collection-related things.
@@ -273,7 +272,6 @@ gc = [
273272
"wasmtime-environ/gc",
274273
"wasmtime-cranelift?/gc",
275274
"wasmtime-winch?/gc",
276-
"signals-based-traps", # not ported to non-mmap schemes yet
277275
]
278276

279277
# Enable the deferred reference counting garbage collector.
@@ -297,7 +295,6 @@ threads = [
297295
"wasmtime-cranelift?/threads",
298296
"wasmtime-winch?/threads",
299297
"std",
300-
"signals-based-traps",
301298
]
302299

303300
# Controls whether backtraces will attempt to parse DWARF information in
@@ -319,13 +316,6 @@ std = [
319316
'wasmtime-fiber?/std',
320317
'pulley-interpreter?/std',
321318
'wasmtime-math/std',
322-
# technically this isn't necessary but once you have the standard library you
323-
# probably want things to go fast in which case you've probably got signal
324-
# handlers and such so implicitly enable this. This also helps reduce the
325-
# verbosity of others depending on `wasmtime` with `default-features = false`
326-
# where frequently `std` is enabled and this feature will typically want to be
327-
# enabled by default as well.
328-
'signals-based-traps',
329319
]
330320

331321
# Enables support for the `Store::call_hook` API which enables injecting custom
@@ -357,14 +347,17 @@ reexport-wasmparser = []
357347
# provides a human-readable text format for component values.
358348
wave = ["dep:wasm-wave"]
359349

360-
# Gates compile-time support for host signals-based-traps.
350+
# For platforms that Wasmtime does not have support for Wasmtime will disable
351+
# the use of virtual memory by default, for example allocating linear memories
352+
# with `malloc` instead. This feature can be used, for these platforms, to
353+
# instead use a C API defined in `wasmtime-platform.h` instead.
361354
#
362-
# Traps based on signals, such as SIGSEGV, are useful for accelerating
363-
# WebAssembly by removing explicit checks and letting the hardware deliver
364-
# signals instead. This feature is enabled by default and gates a number
365-
# of implementations within Wasmtime that may rely on virtual memory, for
366-
# example. Embedded systems or smaller systems may wish to disable this feature
367-
# to reduce the runtime requirements of Wasmtime.
368-
signals-based-traps = [
369-
"dep:wasmtime-jit-icache-coherence",
370-
]
355+
# For some more information see
356+
# https://docs.wasmtime.dev/stability-platform-support.html#support-for-no_std
357+
#
358+
# This feature is not necessary for supported platforms like Unix and Windows as
359+
# virtual memory is always enabled there.
360+
custom-virtual-memory = []
361+
362+
# Same as `custom-virtual-memory` above, but for custom signal-handling APIs.
363+
custom-native-signals = []

crates/wasmtime/build.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
11
fn main() {
22
println!("cargo:rerun-if-changed=build.rs");
33

4+
// NB: duplicating a workaround in the wasmtime-fiber build script.
5+
println!("cargo:rustc-check-cfg=cfg(asan)");
6+
if cfg_is("sanitize", "address") {
7+
println!("cargo:rustc-cfg=asan");
8+
}
9+
10+
let unix = cfg("unix");
11+
let windows = cfg("windows");
12+
let miri = cfg("miri");
13+
let supported_platform = unix || windows;
14+
15+
let has_native_signals =
16+
!miri && (supported_platform || cfg!(feature = "custom-native-signals"));
17+
let has_virtual_memory = supported_platform || cfg!(feature = "custom-virtual-memory");
18+
19+
println!("cargo:rustc-check-cfg=cfg(has_native_signals, has_virtual_memory)");
20+
if has_native_signals {
21+
println!("cargo:rustc-cfg=has_native_signals");
22+
}
23+
if has_virtual_memory {
24+
println!("cargo:rustc-cfg=has_virtual_memory");
25+
}
26+
427
#[cfg(feature = "runtime")]
528
build_c_helpers();
629
}
730

31+
fn cfg(key: &str) -> bool {
32+
std::env::var(&format!("CARGO_CFG_{}", key.to_uppercase())).is_ok()
33+
}
34+
35+
fn cfg_is(key: &str, val: &str) -> bool {
36+
std::env::var(&format!("CARGO_CFG_{}", key.to_uppercase()))
37+
.ok()
38+
.as_deref()
39+
== Some(val)
40+
}
41+
842
#[cfg(feature = "runtime")]
943
fn build_c_helpers() {
1044
use wasmtime_versioned_export_macros::versioned_suffix;
1145

12-
// NB: duplicating a workaround in the wasmtime-fiber build script.
13-
println!("cargo:rustc-check-cfg=cfg(asan)");
14-
match std::env::var("CARGO_CFG_SANITIZE") {
15-
Ok(s) if s == "address" => {
16-
println!("cargo:rustc-cfg=asan");
17-
}
18-
_ => {}
19-
}
20-
2146
// If this platform is neither unix nor windows then there's no default need
2247
// for a C helper library since `helpers.c` is tailored for just these
2348
// platforms currently.
24-
if std::env::var("CARGO_CFG_UNIX").is_err() && std::env::var("CARGO_CFG_WINDOWS").is_err() {
49+
if !cfg("unix") && !cfg("windows") {
2550
return;
2651
}
2752

@@ -38,9 +63,7 @@ fn build_c_helpers() {
3863

3964
// On MinGW targets work around a bug in the MinGW compiler described at
4065
// https://github.com/bytecodealliance/wasmtime/pull/9688#issuecomment-2573367719
41-
if std::env::var("CARGO_CFG_WINDOWS").is_ok()
42-
&& std::env::var("CARGO_CFG_TARGET_ENV").ok().as_deref() == Some("gnu")
43-
{
66+
if cfg("windows") && cfg_is("target_env", "gnu") {
4467
build.define("__USE_MINGW_SETJMP_NON_SEH", None);
4568
}
4669

0 commit comments

Comments
 (0)