From 0ff8f6ab2020c721b0d8065cd503422e4d0dbf10 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 31 Jan 2022 16:54:04 -0800 Subject: [PATCH] Make build-config magic use memfd by default. --- .github/workflows/main.yml | 3 --- Cargo.toml | 3 +-- crates/runtime/Cargo.toml | 4 +--- crates/runtime/build.rs | 11 +++++++++++ crates/runtime/src/instance/allocator/pooling.rs | 2 +- crates/runtime/src/lib.rs | 10 +++++----- crates/runtime/src/memfd_disabled.rs | 2 -- crates/runtime/src/traphandlers/unix.rs | 9 +-------- crates/wasmtime/Cargo.toml | 4 ++-- 9 files changed, 22 insertions(+), 26 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5e5e0c64d9..8516d4ec0a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -136,7 +136,6 @@ jobs: - run: cargo check -p wasmtime --no-default-features --features async - run: cargo check -p wasmtime --no-default-features --features uffd - run: cargo check -p wasmtime --no-default-features --features pooling-allocator - - run: cargo check -p wasmtime --no-default-features --features memfd-allocator - run: cargo check -p wasmtime --no-default-features --features cranelift - run: cargo check -p wasmtime --no-default-features --features cranelift,wat,async,cache @@ -316,8 +315,6 @@ jobs: cargo test --features uffd -p wasmtime-runtime instance::allocator::pooling cargo test --features uffd -p wasmtime-cli pooling_allocator cargo test --features uffd -p wasmtime-cli wast::Cranelift - cargo test --features memfd-allocator -p wasmtime-cli pooling_allocator - cargo test --features memfd-allocator -p wasmtime-cli wast::Cranelift if: matrix.os == 'ubuntu-latest' && matrix.target == '' env: RUST_BACKTRACE: 1 diff --git a/Cargo.toml b/Cargo.toml index 51c4843fcc..bc385ece76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ path = "src/bin/wasmtime.rs" doc = false [dependencies] -wasmtime = { path = "crates/wasmtime", version = "0.33.0", default-features = false, features = ['cache', 'cranelift'] } +wasmtime = { path = "crates/wasmtime", version = "0.33.0", default-features = false, features = ['cache', 'cranelift', 'pooling-allocator', 'memfd'] } wasmtime-cache = { path = "crates/cache", version = "=0.33.0" } wasmtime-cranelift = { path = "crates/cranelift", version = "=0.33.0" } wasmtime-environ = { path = "crates/environ", version = "=0.33.0" } @@ -96,7 +96,6 @@ wasi-crypto = ["wasmtime-wasi-crypto"] wasi-nn = ["wasmtime-wasi-nn"] uffd = ["wasmtime/uffd"] pooling-allocator = ["wasmtime/pooling-allocator"] -memfd-allocator = ["pooling-allocator", "wasmtime/memfd-allocator"] all-arch = ["wasmtime/all-arch"] posix-signals-on-macos = ["wasmtime/posix-signals-on-macos"] diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml index aaef30f677..a4b717fee6 100644 --- a/crates/runtime/Cargo.toml +++ b/crates/runtime/Cargo.toml @@ -25,6 +25,7 @@ backtrace = "0.3.61" lazy_static = "1.3.0" rand = "0.8.3" anyhow = "1.0.38" +memfd = { version = "0.4.1", optional = true } [target.'cfg(target_os = "macos")'.dependencies] mach = "0.3.2" @@ -37,7 +38,6 @@ winapi = { version = "0.3.7", features = ["winbase", "memoryapi", "errhandlingap [target.'cfg(target_os = "linux")'.dependencies] userfaultfd = { version = "0.4.1", optional = true } -memfd = { version = "0.4.1", optional = true } [build-dependencies] cc = "1.0" @@ -60,5 +60,3 @@ uffd = ["userfaultfd", "pooling-allocator"] # It is useful for applications that do not bind their own exception ports and # need portable signal handling. posix-signals-on-macos = [] - -memfd-allocator = ["pooling-allocator", "memfd"] diff --git a/crates/runtime/build.rs b/crates/runtime/build.rs index 6f112f25c9..9c2741714a 100644 --- a/crates/runtime/build.rs +++ b/crates/runtime/build.rs @@ -10,4 +10,15 @@ fn main() { ) .file("src/helpers.c") .compile("wasmtime-helpers"); + + // Check to see if we are on Linux and the `memfd` feature is + // active. If so, enable the `memfd` rustc cfg so `#[cfg(memfd)]` + // will work. + let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let is_memfd = env::var("CARGO_FEATURE_MEMFD").is_ok(); + let is_pooling = env::var("CARGO_FEATURE_POOLING_ALLOCATOR").is_ok(); + let is_uffd = env::var("CARGO_FEATURE_UFFD").is_ok(); + if &os == "linux" && is_memfd && is_pooling && !is_uffd { + println!("cargo:rustc-cfg=memfd"); + } } diff --git a/crates/runtime/src/instance/allocator/pooling.rs b/crates/runtime/src/instance/allocator/pooling.rs index abf2683c45..a633bbac29 100644 --- a/crates/runtime/src/instance/allocator/pooling.rs +++ b/crates/runtime/src/instance/allocator/pooling.rs @@ -703,7 +703,7 @@ impl MemoryPool { let mapping = Mmap::accessible_reserved(0, allocation_size) .context("failed to create memory pool mapping")?; - let num_memfd_slots = if cfg!(feature = "memfd-allocator") { + let num_memfd_slots = if cfg!(memfd) { max_instances * max_memories } else { 0 diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 822970727c..fb1ffee621 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -19,7 +19,7 @@ clippy::use_self ) )] -#![cfg_attr(feature = "memfd-allocator", allow(dead_code))] +#![cfg_attr(memfd, allow(dead_code))] use std::sync::atomic::AtomicU64; @@ -67,14 +67,14 @@ pub use crate::vmcontext::{ mod module_id; pub use module_id::{CompiledModuleId, CompiledModuleIdAllocator}; -#[cfg(feature = "memfd-allocator")] +#[cfg(memfd)] mod memfd; -#[cfg(feature = "memfd-allocator")] +#[cfg(memfd)] pub use crate::memfd::{MemFdSlot, MemoryMemFd, ModuleMemFds}; -#[cfg(not(feature = "memfd-allocator"))] +#[cfg(not(memfd))] mod memfd_disabled; -#[cfg(not(feature = "memfd-allocator"))] +#[cfg(not(memfd))] pub use crate::memfd_disabled::{MemFdSlot, MemoryMemFd, ModuleMemFds}; /// Version number of this crate. diff --git a/crates/runtime/src/memfd_disabled.rs b/crates/runtime/src/memfd_disabled.rs index 30dfb5fa8f..a0adf10d2d 100644 --- a/crates/runtime/src/memfd_disabled.rs +++ b/crates/runtime/src/memfd_disabled.rs @@ -37,11 +37,9 @@ impl ModuleMemFds { /// To allow MemFdSlot to be unconditionally passed around in various /// places (e.g. a `Memory`), we define a zero-sized type when memfd is /// not included in the build. -#[cfg(not(feature = "memfd-allocator"))] #[derive(Debug)] pub struct MemFdSlot; -#[cfg(not(feature = "memfd-allocator"))] #[allow(dead_code)] impl MemFdSlot { pub(crate) fn create(_: *mut libc::c_void, _: usize) -> Self { diff --git a/crates/runtime/src/traphandlers/unix.rs b/crates/runtime/src/traphandlers/unix.rs index fd16bfcdd1..30545914d8 100644 --- a/crates/runtime/src/traphandlers/unix.rs +++ b/crates/runtime/src/traphandlers/unix.rs @@ -54,14 +54,7 @@ pub unsafe fn platform_init() { // Sometimes we need to handle SIGBUS too: // - On ARM, handle Unaligned Accesses. // - On Darwin, guard page accesses are raised as SIGBUS. - // - With the MemFD allocator, heap growth is controlled by - // ftruncate'ing an mmap'd file, and so out-of-bounds accesses - // are raised as SIGBUS. - if cfg!(target_arch = "arm") - || cfg!(target_os = "macos") - || cfg!(target_os = "freebsd") - || cfg!(feature = "memfd-allocator") - { + if cfg!(target_arch = "arm") || cfg!(target_os = "macos") || cfg!(target_os = "freebsd") { register(&mut PREV_SIGBUS, libc::SIGBUS); } } diff --git a/crates/wasmtime/Cargo.toml b/crates/wasmtime/Cargo.toml index c7b0037d0e..3f2930b872 100644 --- a/crates/wasmtime/Cargo.toml +++ b/crates/wasmtime/Cargo.toml @@ -50,7 +50,7 @@ wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" } maintenance = { status = "actively-developed" } [features] -default = ['async', 'cache', 'wat', 'jitdump', 'parallel-compilation', 'cranelift', 'pooling-allocator'] +default = ['async', 'cache', 'wat', 'jitdump', 'parallel-compilation', 'cranelift', 'pooling-allocator', 'memfd'] # An on-by-default feature enabling runtime compilation of WebAssembly modules # with the Cranelift compiler. Cranelift is the default compilation backend of @@ -90,4 +90,4 @@ all-arch = ["wasmtime-cranelift/all-arch"] # need portable signal handling. posix-signals-on-macos = ["wasmtime-runtime/posix-signals-on-macos"] -memfd-allocator = ["wasmtime-runtime/memfd-allocator", "pooling-allocator"] \ No newline at end of file +memfd = ["wasmtime-runtime/memfd", "pooling-allocator"]