* Skip memfd creation with precompiled modules This commit updates the memfd support internally to not actually use a memfd if a compiled module originally came from disk via the `wasmtime::Module::deserialize_file` API. In this situation we already have a file descriptor open and there's no need to copy a module's heap image to a new file descriptor. To facilitate a new source of `mmap` the currently-memfd-specific-logic of creating a heap image is generalized to a new form of `MemoryInitialization` which is attempted for all modules at module-compile-time. This means that the serialized artifact to disk will have the memory image in its entirety waiting for us. Furthermore the memory image is ensured to be padded and aligned carefully to the target system's page size, notably meaning that the data section in the final object file is page-aligned and the size of the data section is also page aligned. This means that when a precompiled module is mapped from disk we can reuse the underlying `File` to mmap all initial memory images. This means that the offset-within-the-memory-mapped-file can differ for memfd-vs-not, but that's just another piece of state to track in the memfd implementation. In the limit this waters down the term "memfd" for this technique of quickly initializing memory because we no longer use memfd unconditionally (only when the backing file isn't available). This does however open up an avenue in the future to porting this support to other OSes because while `memfd_create` is Linux-specific both macOS and Windows support mapping a file with copy-on-write. This porting isn't done in this PR and is left for a future refactoring. Closes #3758 * Enable "memfd" support on all unix systems Cordon off the Linux-specific bits and enable the memfd support to compile and run on platforms like macOS which have a Linux-like `mmap`. This only works if a module is mapped from a precompiled module file on disk, but that's better than not supporting it at all! * Fix linux compile * Use `Arc<File>` instead of `MmapVecFileBacking` * Use a named struct instead of mysterious tuples * Comment about unsafety in `Module::deserialize_file` * Fix tests * Fix uffd compile * Always align data segments No need to have conditional alignment since their sizes are all aligned anyway * Update comment in build.rs * Use rustix, not `region` * Fix some confusing logic/names around memory indexes These functions all work with memory indexes, not specifically defined memory indexes.
28 lines
947 B
Rust
28 lines
947 B
Rust
use std::env;
|
|
|
|
fn main() {
|
|
println!("cargo:rerun-if-changed=src/helpers.c");
|
|
cc::Build::new()
|
|
.warnings(true)
|
|
.define(
|
|
&format!("CFG_TARGET_OS_{}", env::var("CARGO_CFG_TARGET_OS").unwrap()),
|
|
None,
|
|
)
|
|
.file("src/helpers.c")
|
|
.compile("wasmtime-helpers");
|
|
|
|
// Check to see if we are on Unix and the `memfd` feature is
|
|
// active. If so, enable the `memfd` rustc cfg so `#[cfg(memfd)]`
|
|
// will work.
|
|
//
|
|
// Note that while this is called memfd it only actually uses the `memfd`
|
|
// crate on Linux and on other Unix platforms this tries to reuse mmap'd
|
|
// `*.cwasm` files.
|
|
let family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
|
|
let is_memfd = env::var("CARGO_FEATURE_MEMFD").is_ok();
|
|
let is_uffd = env::var("CARGO_FEATURE_UFFD").is_ok();
|
|
if &family == "unix" && is_memfd && !is_uffd {
|
|
println!("cargo:rustc-cfg=memfd");
|
|
}
|
|
}
|