Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

runtime: eliminate the shared global code registry altogether in no-signals-based-traps builds. (#14690)

Chris Fallin2b85e305af5e
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index d7c0e83c0c..4496c5af0c 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -1199,7 +1199,7 @@ jobs:
working-directory: ./examples/min-platform
env:
WASMTIME_SIGNALS_BASED_TRAPS: 1
- MIN_PLATFORM_TEST_DISABLE_WASI: 1
+ MIN_PLATFORM_EXAMPLE_DISABLE_WASI: 1
# Add the `wasmtime-platform.h` file as a release artifact
- uses: actions/upload-artifact@v6
diff --git a/crates/wasmtime/src/runtime/module/registry.rs b/crates/wasmtime/src/runtime/module/registry.rs
index f056a8d1de..79f565b030 100644
--- a/crates/wasmtime/src/runtime/module/registry.rs
+++ b/crates/wasmtime/src/runtime/module/registry.rs
@@ -4,9 +4,8 @@ use crate::code::{EngineCode, EngineCodePC, ModuleWithCode, StoreCode, StoreCode
#[cfg(feature = "component-model")]
use crate::component::Component;
use crate::runtime::vm::VMWasmCallFunction;
-use crate::sync::{OnceLock, RwLock};
use crate::vm::CompiledModuleId;
-use crate::{Engine, FrameInfo, Module, code_memory::CodeMemory, prelude::*};
+use crate::{Engine, FrameInfo, Module, prelude::*};
use alloc::sync::Arc;
#[cfg(not(feature = "debug"))]
use core::marker::PhantomData;
@@ -385,64 +384,109 @@ impl ModuleRegistry {
}
}
-// This is the global code registry that stores information for all loaded code
-// objects that are currently in use by any `Store` in the current process.
-//
-// The purpose of this map is to be called from signal handlers to determine
-// whether a program counter is a wasm trap or not. Specifically macOS has
-// no contextual information about the thread available, hence the necessity
-// for global state rather than using thread local state.
-//
-// This is similar to `ModuleRegistry` except that it has less information and
-// supports removal. Any time anything is registered with a `ModuleRegistry`
-// it is also automatically registered with the singleton global module
-// registry. When a `ModuleRegistry` is destroyed then all of its entries
-// are removed from the global registry.
-fn global_code() -> &'static RwLock<GlobalRegistry> {
- static GLOBAL_CODE: OnceLock<RwLock<GlobalRegistry>> = OnceLock::new();
- GLOBAL_CODE.get_or_init(Default::default)
-}
-
-type GlobalRegistry = TryBTreeMap<usize, (usize, Arc<CodeMemory>)>;
-
-/// Find which registered region of code contains the given program counter, and
-/// what offset that PC is within that module's code.
-pub fn lookup_code(pc: usize) -> Option<(Arc<CodeMemory>, usize)> {
- let all_modules = global_code().read();
- let (_end, (start, module)) = all_modules.range(pc..).next()?;
- let text_offset = pc.checked_sub(*start)?;
- Some((module.clone(), text_offset))
-}
+#[cfg(has_native_signals)]
+pub use global_registry::*;
+#[cfg(not(has_native_signals))]
+pub use no_global_registry::*;
-/// Registers a new region of code.
+/// This is the global code registry that stores information for all loaded
+/// code objects that are currently in use by any `Store` in the current
+/// process.
+///
+/// The purpose of this map is to be called from signal handlers to determine
+/// whether a program counter is a wasm trap or not. Specifically macOS has no
+/// contextual information about the thread available, hence the necessity for
+/// global state rather than using thread local state.
///
-/// Must not have been previously registered and must be `unregister`'d to
-/// prevent leaking memory.
+/// This is similar to `ModuleRegistry` except that it has less information
+/// and supports removal. Any time anything is registered with a
+/// `ModuleRegistry` it is also automatically registered with the singleton
+/// global module registry. When a `ModuleRegistry` is destroyed then all of
+/// its entries are removed from the global registry.
///
-/// This is required to enable traps to work correctly since the signal handler
-/// will lookup in the `GLOBAL_CODE` list to determine which a particular pc
-/// is a trap or not.
-pub fn register_code(image: &Arc<CodeMemory>, address: Range<usize>) -> Result<(), OutOfMemory> {
- if address.is_empty() {
- return Ok(());
+/// Note that this global registry only exists when native signals are used to
+/// implement traps (`cfg(has_native_signals)`); see `no_global_registry`
+/// below for the alternative.
+#[cfg(has_native_signals)]
+mod global_registry {
+ use crate::code_memory::CodeMemory;
+ use crate::prelude::*;
+ use crate::sync::{OnceLock, RwLock};
+ use alloc::sync::Arc;
+ use core::ops::Range;
+
+ fn global_code() -> &'static RwLock<GlobalRegistry> {
+ static GLOBAL_CODE: OnceLock<RwLock<GlobalRegistry>> = OnceLock::new();
+ GLOBAL_CODE.get_or_init(Default::default)
+ }
+
+ type GlobalRegistry = TryBTreeMap<usize, (usize, Arc<CodeMemory>)>;
+
+ /// Find which registered region of code contains the given program
+ /// counter, and what offset that PC is within that module's code.
+ pub fn lookup_code(pc: usize) -> Option<(Arc<CodeMemory>, usize)> {
+ let all_modules = global_code().read();
+ let (_end, (start, module)) = all_modules.range(pc..).next()?;
+ let text_offset = pc.checked_sub(*start)?;
+ Some((module.clone(), text_offset))
+ }
+
+ /// Registers a new region of code.
+ ///
+ /// Must not have been previously registered and must be `unregister`'d to
+ /// prevent leaking memory.
+ ///
+ /// This is required to enable traps to work correctly since the signal
+ /// handler will lookup in the `GLOBAL_CODE` list to determine which a
+ /// particular pc is a trap or not.
+ pub fn register_code(
+ image: &Arc<CodeMemory>,
+ address: Range<usize>,
+ ) -> Result<(), OutOfMemory> {
+ if address.is_empty() {
+ return Ok(());
+ }
+ let start = address.start;
+ let end = address.end - 1;
+ let prev = global_code().write().insert(end, (start, image.clone()))?;
+ assert!(prev.is_none());
+ Ok(())
+ }
+
+ /// Unregisters a code mmap from the global map.
+ ///
+ /// Must have been previously registered with `register`.
+ pub fn unregister_code(address: Range<usize>) {
+ if address.is_empty() {
+ return;
+ }
+ let end = address.end - 1;
+ let code = global_code().write().remove(end);
+ assert!(code.is_some());
}
- let start = address.start;
- let end = address.end - 1;
- let prev = global_code().write().insert(end, (start, image.clone()))?;
- assert!(prev.is_none());
- Ok(())
}
-/// Unregisters a code mmap from the global map.
-///
-/// Must have been previously registered with `register`.
-pub fn unregister_code(address: Range<usize>) {
- if address.is_empty() {
- return;
+/// Without native signals there is no global registry: traps are only ever
+/// raised synchronously from contexts that have access to a `Store`'s own
+/// `ModuleRegistry`, so trap lookups happen there instead. This means that
+/// loading and unloading modules performs no cross-thread synchronization.
+#[cfg(not(has_native_signals))]
+mod no_global_registry {
+ use crate::code_memory::CodeMemory;
+ use crate::prelude::*;
+ use alloc::sync::Arc;
+ use core::ops::Range;
+
+ /// No-op: there is no global registry in this configuration.
+ pub fn register_code(
+ _image: &Arc<CodeMemory>,
+ _address: Range<usize>,
+ ) -> Result<(), OutOfMemory> {
+ Ok(())
}
- let end = address.end - 1;
- let code = global_code().write().remove(end);
- assert!(code.is_some());
+
+ /// No-op: there is no global registry in this configuration.
+ pub fn unregister_code(_address: Range<usize>) {}
}
#[test]
diff --git a/crates/wasmtime/src/runtime/vm/interpreter.rs b/crates/wasmtime/src/runtime/vm/interpreter.rs
index a32c26bb1c..1376faf43e 100644
--- a/crates/wasmtime/src/runtime/vm/interpreter.rs
+++ b/crates/wasmtime/src/runtime/vm/interpreter.rs
@@ -1,7 +1,7 @@
use crate::error::OutOfMemory;
use crate::runtime::vm::vmcontext::VMArrayCallNative;
use crate::runtime::vm::{
- StoreBox, TrapRegisters, TrapTest, VMContext, VMOpaqueContext, f32x4, f64x2, i8x16, tls,
+ Instance, StoreBox, TrapRegisters, VMContext, VMOpaqueContext, f32x4, f64x2, i8x16, tls,
};
use crate::{Engine, ValRaw};
use core::marker;
@@ -224,7 +224,12 @@ impl InterpreterRef<'_> {
}
// If the VM trapped then process that here and return `false`.
DoneReason::Trap { pc, kind } => {
- bytecode = self.trap(pc, kind);
+ // SAFETY: `caller` is the `VMContext` this
+ // function was invoked with, which is valid per
+ // this function's contract, and Wasm is currently
+ // executing so no other borrows of its store are
+ // active.
+ bytecode = unsafe { self.trap(pc, kind, caller) };
vm = self.vm();
}
}
@@ -452,46 +457,64 @@ impl InterpreterRef<'_> {
}
/// Handles an interpreter trap. This will initialize the trap state stored
- /// in TLS via the `test_if_trap` helper below by reading the pc/fp of the
- /// interpreter and seeing if that's a valid opcode to trap at.
- fn trap(&mut self, pc: NonNull<u8>, kind: Option<TrapKind>) -> NonNull<u8> {
+ /// in TLS by reading the pc/fp of the interpreter and seeing if that's a
+ /// valid opcode to trap at.
+ ///
+ /// Trap codes are looked up in the module registry of the store that
+ /// `caller` belongs to, rather than in any process-global state.
+ ///
+ /// # Safety
+ ///
+ /// `caller` must be the valid `VMContext` passed to the `call` that is
+ /// currently executing, and there must be no other active borrows of its
+ /// store (as is the case when wasm traps), since it is used to enter the
+ /// host via `Instance::enter_host_from_wasm`.
+ unsafe fn trap(
+ &mut self,
+ pc: NonNull<u8>,
+ kind: Option<TrapKind>,
+ caller: NonNull<VMContext>,
+ ) -> NonNull<u8> {
let regs = TrapRegisters {
pc: pc.as_ptr() as usize,
fp: self.vm().fp() as usize,
};
- let handler = tls::with(|s| {
- let s = s.unwrap();
- match kind {
- Some(kind) => {
- let trap = match kind {
- TrapKind::IntegerOverflow => Trap::IntegerOverflow.into(),
- TrapKind::DivideByZero => Trap::IntegerDivisionByZero.into(),
- TrapKind::BadConversionToInteger => Trap::BadConversionToInteger.into(),
- TrapKind::MemoryOutOfBounds => Trap::MemoryOutOfBounds.into(),
- TrapKind::DisabledOpcode => Trap::DisabledOpcode.into(),
- TrapKind::StackOverflow => Trap::StackOverflow.into(),
- };
- s.set_jit_trap(regs, None, trap);
- s.entry_trap_handler()
+ let trap = match kind {
+ Some(kind) => match kind {
+ TrapKind::IntegerOverflow => Trap::IntegerOverflow.into(),
+ TrapKind::DivideByZero => Trap::IntegerDivisionByZero.into(),
+ TrapKind::BadConversionToInteger => Trap::BadConversionToInteger.into(),
+ TrapKind::MemoryOutOfBounds => Trap::MemoryOutOfBounds.into(),
+ TrapKind::DisabledOpcode => Trap::DisabledOpcode.into(),
+ TrapKind::StackOverflow => Trap::StackOverflow.into(),
+ },
+ None => {
+ let mut trap = None;
+ // SAFETY: `caller` is valid and its store is not otherwise
+ // borrowed, per this function's contract.
+ unsafe {
+ Instance::enter_host_from_wasm(caller, |store, _instance| {
+ trap = store
+ .store_opaque()
+ .modules()
+ .store_code_by_pc(regs.pc)
+ .and_then(|(code, text_offset)| {
+ code.code_memory().lookup_trap_code(text_offset)
+ });
+ });
}
- None => {
- match s.test_if_trap(regs, None, |_| false) {
- // This shouldn't be possible, so this is a fatal error
- // if it happens.
- TrapTest::NotWasm => {
- panic!("pulley trap at {pc:?} without trap code registered")
- }
-
- // Not possible with our closure above returning `false`.
- #[cfg(has_native_signals)]
- TrapTest::HandledByEmbedder => unreachable!(),
-
- // Trap was handled, yay! Configure interpreter state
- // to resume at the exception handler.
- TrapTest::Trap(handler) => handler,
- }
+ match trap {
+ Some(trap) => trap,
+ // This shouldn't be possible, so this is a fatal error if
+ // it happens.
+ None => panic!("pulley trap at {pc:?} without trap code registered"),
}
}
+ };
+ let handler = tls::with(|s| {
+ let s = s.unwrap();
+ s.set_jit_trap(regs, None, trap);
+ s.entry_trap_handler()
});
unsafe {
self.resume_to_exception_handler(&handler, 0, 0);
diff --git a/crates/wasmtime/src/runtime/vm/traphandlers.rs b/crates/wasmtime/src/runtime/vm/traphandlers.rs
index 64b6d8b026..95a3520d24 100644
--- a/crates/wasmtime/src/runtime/vm/traphandlers.rs
+++ b/crates/wasmtime/src/runtime/vm/traphandlers.rs
@@ -22,6 +22,7 @@ pub use self::signals::*;
#[cfg(feature = "gc")]
use crate::ThrownException;
+#[cfg(has_native_signals)]
use crate::runtime::module::lookup_code;
use crate::runtime::store::{ExecutorRef, StoreOpaque};
use crate::runtime::vm::sys::traphandlers;
@@ -52,6 +53,7 @@ pub(crate) struct TrapRegisters {
}
/// Return value from `test_if_trap`.
+#[cfg(has_native_signals)]
pub(crate) enum TrapTest {
/// Not a wasm trap, need to delegate to whatever process handler is next.
NotWasm,
@@ -1043,6 +1045,7 @@ impl CallThreadState {
/// this signal, nothing else to do.
/// * `TrapTest::Trap` - this is a wasm trap an the stack needs to be
/// unwound now.
+ #[cfg(has_native_signals)]
pub(crate) fn test_if_trap(
&self,
regs: TrapRegisters,
diff --git a/examples/min-platform/build.sh b/examples/min-platform/build.sh
index b78a36b2c4..fe7e614d86 100755
--- a/examples/min-platform/build.sh
+++ b/examples/min-platform/build.sh
@@ -50,7 +50,7 @@ fi
# which ensures that Rust & C agree on types and such.
cbindgen "$REPO_DIR/crates/wasmtime/src/runtime/vm/sys/custom/capi.rs" \
--config "$EMBEDDING_DIR/cbindgen.toml" > "$EMBEDDING_DIR/wasmtime-platform.h"
-clang -shared -O2 -o "$HOST_DIR/libwasmtime-platform.so" "$EMBEDDING_DIR/wasmtime-platform.c" \
+clang -shared -fPIC -O2 -o "$HOST_DIR/libwasmtime-platform.so" "$EMBEDDING_DIR/wasmtime-platform.c" \
-D_GNU_SOURCE $cflags
# Next the embedding itself is built.
diff --git a/examples/min-platform/embedding/src/allocator.rs b/examples/min-platform/embedding/src/allocator.rs
index 5084fd99e4..4216d73757 100644
--- a/examples/min-platform/embedding/src/allocator.rs
+++ b/examples/min-platform/embedding/src/allocator.rs
@@ -35,47 +35,52 @@ struct MyAllocator;
unsafe impl GlobalAlloc for MyGlobalDmalloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
- unsafe {
- self.dlmalloc
- .try_lock()
- .unwrap()
- .malloc(layout.size(), layout.align())
- }
+ let size = alloc_size(layout.size(), layout.align());
+ unsafe { self.dlmalloc.lock().malloc(size, layout.align()) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
- unsafe {
- self.dlmalloc
- .try_lock()
- .unwrap()
- .calloc(layout.size(), layout.align())
- }
+ let size = alloc_size(layout.size(), layout.align());
+ unsafe { self.dlmalloc.lock().calloc(size, layout.align()) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
+ let old_size = alloc_size(layout.size(), layout.align());
+ let new_size = alloc_size(new_size, layout.align());
unsafe {
self.dlmalloc
- .try_lock()
- .unwrap()
- .realloc(ptr, layout.size(), layout.align(), new_size)
+ .lock()
+ .realloc(ptr, old_size, layout.align(), new_size)
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
- unsafe {
- self.dlmalloc
- .try_lock()
- .unwrap()
- .free(ptr, layout.size(), layout.align())
- }
+ let size = alloc_size(layout.size(), layout.align());
+ unsafe { self.dlmalloc.lock().free(ptr, size, layout.align()) }
+ }
+}
+
+pub const PAGE_SIZE: usize = 4096;
+
+/// Returns the size to actually allocate for a request of `size` bytes with
+/// alignment `align`.
+///
+/// Page-aligned allocations are rounded up to a whole number of pages so that
+/// they never share a page with any other allocation. This is relied upon by
+/// the custom code memory in `lib.rs`: Wasmtime allocates code images with
+/// page alignment, and the embedding changes the protection of the pages
+/// containing code, which must not affect unrelated allocations.
+fn alloc_size(size: usize, align: usize) -> usize {
+ if align >= PAGE_SIZE {
+ size.next_multiple_of(align)
+ } else {
+ size
}
}
-#[cfg(not(feature = "wasi"))]
-const INITIAL_HEAP_SIZE: usize = 64 * 1024;
-// The wasi component requires a larger heap than the module tests
-#[cfg(feature = "wasi")]
-const INITIAL_HEAP_SIZE: usize = 4 * 1024 * 1024;
+// Note that this is large enough to accommodate the wasi component as well as
+// multiple threads concurrently loading and running modules.
+const INITIAL_HEAP_SIZE: usize = 16 * 1024 * 1024;
static mut INITIAL_HEAP: [u8; INITIAL_HEAP_SIZE] = [0; INITIAL_HEAP_SIZE];
static mut INITIAL_HEAP_ALLOCATED: bool = false;
@@ -113,12 +118,13 @@ unsafe impl dlmalloc::Allocator for MyAllocator {
}
fn page_size(&self) -> usize {
- 4096
+ PAGE_SIZE
}
}
-// Simple mutex which only supports `try_lock` at this time. This would probably
-// be replaced with a "real" mutex in a "real" embedding.
+// Simple spin lock, which is sufficient for this example's allocator as it may
+// be used concurrently from multiple threads. This would probably be replaced
+// with a "real" mutex in a "real" embedding.
struct Mutex<T> {
data: UnsafeCell<T>,
locked: AtomicBool,
@@ -135,12 +141,11 @@ impl<T> Mutex<T> {
}
}
- fn try_lock(&self) -> Option<impl DerefMut<Target = T> + '_> {
- if self.locked.swap(true, Acquire) {
- None
- } else {
- Some(MutexGuard { lock: self })
+ fn lock(&self) -> impl DerefMut<Target = T> + '_ {
+ while self.locked.swap(true, Acquire) {
+ core::hint::spin_loop();
}
+ MutexGuard { lock: self }
}
}
diff --git a/examples/min-platform/embedding/src/lib.rs b/examples/min-platform/embedding/src/lib.rs
index 6876af0514..1d67e5045a 100644
--- a/examples/min-platform/embedding/src/lib.rs
+++ b/examples/min-platform/embedding/src/lib.rs
@@ -3,9 +3,10 @@
#[macro_use]
extern crate alloc;
-use alloc::string::ToString;
-use core::ptr;
-use wasmtime::{Config, Engine, Instance, Linker, Module, Result, Store, ensure};
+use alloc::sync::Arc;
+use wasmtime::{
+ Config, CustomCodeMemory, Engine, Instance, Linker, Module, Result, Store, Trap, ensure,
+};
mod allocator;
mod panic;
@@ -50,6 +51,66 @@ pub unsafe extern "C" fn run(
}
}
+/// Entrypoint for a test which is run concurrently on multiple threads.
+///
+/// Each invocation creates its own `Engine` and then repeatedly loads,
+/// instantiates, runs, and drops the given precompiled `module`, including
+/// executing some traps. The intent is to verify that when Wasmtime is not
+/// using native signals for traps, separate engines on separate threads do not
+/// contend on any process-global state (such as a global registry of loaded
+/// code), since the default `no_std` synchronization primitives in Wasmtime
+/// panic on contention.
+///
+/// This is only expected to work without the `custom` feature (i.e. without
+/// signals-based traps).
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn run_concurrent_module_load_test(
+ error_buf: *mut u8,
+ error_size: usize,
+ module: *const u8,
+ module_size: usize,
+ iterations: usize,
+) -> usize {
+ unsafe {
+ let buf = core::slice::from_raw_parts_mut(error_buf, error_size);
+ let module = core::slice::from_raw_parts(module, module_size);
+ match run_concurrent_module_load_test_result(module, iterations) {
+ Ok(()) => 0,
+ Err(e) => {
+ let msg = format!("{e:?}");
+ let len = buf.len().min(msg.len());
+ buf[..len].copy_from_slice(&msg.as_bytes()[..len]);
+ len
+ }
+ }
+ }
+}
+
+fn run_concurrent_module_load_test_result(module: &[u8], iterations: usize) -> Result<()> {
+ let engine = Engine::new(&config())?;
+
+ for i in 0..iterations {
+ // Load a fresh copy of the module each iteration so that its code is
+ // registered and unregistered every time.
+ let module = deserialize(&engine, module)?;
+ let mut store = Store::new(&engine, ());
+ let instance = Linker::new(&engine).instantiate(&mut store, &module)?;
+
+ let add = instance.get_typed_func::<(u32, u32), u32>(&mut store, "add")?;
+ ensure!(add.call(&mut store, (i as u32, 3))? == (i as u32).wrapping_add(3));
+
+ let unreachable = instance.get_typed_func::<(), ()>(&mut store, "unreachable")?;
+ let err = unreachable.call(&mut store, ()).unwrap_err();
+ ensure!(err.downcast_ref::<Trap>() == Some(&Trap::UnreachableCodeReached));
+
+ let load = instance.get_typed_func::<u32, u32>(&mut store, "load")?;
+ ensure!(load.call(&mut store, 0)? == 0);
+ let err = load.call(&mut store, 0x10000).unwrap_err();
+ ensure!(err.downcast_ref::<Trap>() == Some(&Trap::MemoryOutOfBounds));
+ }
+ Ok(())
+}
+
fn run_result(
smoke_module: &[u8],
simple_add_module: &[u8],
@@ -101,25 +162,59 @@ fn config() -> Config {
}
}
+ // Without the `custom` feature Wasmtime has no virtual memory support, so
+ // it can't make loaded native code executable itself. Instead the embedding
+ // provides that capability through a custom code memory.
+ if !cfg!(feature = "custom") {
+ config.with_custom_code_memory(Some(Arc::new(CodeMemory)));
+ }
+
config
}
+/// A `CustomCodeMemory` implementation which delegates to the
+/// `embedding_{publish,unpublish}_executable` functions provided by this
+/// embedding's platform (see `wasmtime-platform.c`).
+///
+/// Note that the code images given to this are allocated from this embedding's
+/// heap, so the page-granular protection changes made here rely on the
+/// allocator not sharing pages of code images with other allocations (see
+/// `allocator.rs`).
+struct CodeMemory;
+
+unsafe extern "C" {
+ fn embedding_publish_executable(ptr: *const u8, len: usize) -> i32;
+ fn embedding_unpublish_executable(ptr: *const u8, len: usize) -> i32;
+}
+
+impl CustomCodeMemory for CodeMemory {
+ fn required_alignment(&self) -> usize {
+ allocator::PAGE_SIZE
+ }
+
+ fn publish_executable(&self, ptr: *const u8, len: usize) -> Result<()> {
+ let rc = unsafe { embedding_publish_executable(ptr, len) };
+ ensure!(rc == 0, "failed to publish executable code memory");
+ Ok(())
+ }
+
+ fn unpublish_executable(&self, ptr: *const u8, len: usize) -> Result<()> {
+ let rc = unsafe { embedding_unpublish_executable(ptr, len) };
+ ensure!(rc == 0, "failed to unpublish executable code memory");
+ Ok(())
+ }
+}
+
fn smoke(module: &[u8]) -> Result<()> {
let engine = Engine::new(&config())?;
- let module = match deserialize(&engine, module)? {
- Some(module) => module,
- None => return Ok(()),
- };
+ let module = deserialize(&engine, module)?;
Instance::new(&mut Store::new(&engine, ()), &module, &[])?;
Ok(())
}
fn simple_add(module: &[u8]) -> Result<()> {
let engine = Engine::new(&config())?;
- let module = match deserialize(&engine, module)? {
- Some(module) => module,
- None => return Ok(()),
- };
+ let module = deserialize(&engine, module)?;
let mut store = Store::new(&engine, ());
let instance = Linker::new(&engine).instantiate(&mut store, &module)?;
let func = instance.get_typed_func::<(u32, u32), u32>(&mut store, "add")?;
@@ -129,10 +224,7 @@ fn simple_add(module: &[u8]) -> Result<()> {
fn simple_host_fn(module: &[u8]) -> Result<()> {
let engine = Engine::new(&config())?;
- let module = match deserialize(&engine, module)? {
- Some(module) => module,
- None => return Ok(()),
- };
+ let module = deserialize(&engine, module)?;
let mut linker = Linker::<()>::new(&engine);
linker.func_wrap("host", "multiply", |a: u32, b: u32| a.saturating_mul(b))?;
let mut store = Store::new(&engine, ());
@@ -144,10 +236,7 @@ fn simple_host_fn(module: &[u8]) -> Result<()> {
fn simple_floats(module: &[u8]) -> Result<()> {
let engine = Engine::new(&config())?;
- let module = match deserialize(&engine, module)? {
- Some(module) => module,
- None => return Ok(()),
- };
+ let module = deserialize(&engine, module)?;
let mut store = Store::new(&engine, ());
let instance = Linker::new(&engine).instantiate(&mut store, &module)?;
let func = instance.get_typed_func::<(f32, f32), f32>(&mut store, "frob")?;
@@ -155,38 +244,9 @@ fn simple_floats(module: &[u8]) -> Result<()> {
Ok(())
}
-fn deserialize(engine: &Engine, module: &[u8]) -> Result<Option<Module>> {
- let result = if cfg!(feature = "custom") {
- // If a custom virtual memory system is in use use the raw `deserialize`
- // API to let Wasmtime handle publishing the executable and such.
- unsafe { Module::deserialize(engine, module) }
- } else {
- // NOTE: deserialize_raw avoids creating a copy of the module code. See
- // the safety notes before using in your embedding.
- //
- // Also note that this will only work for native code with a custom code
- // publisher which isn't configured in this example. Such custom code
- // publisher will need to handle making this executable for example.
- let memory_ptr = ptr::slice_from_raw_parts(module.as_ptr(), module.len());
- let module_memory = ptr::NonNull::new(memory_ptr.cast_mut()).unwrap();
- unsafe { Module::deserialize_raw(engine, module_memory) }
- };
- match result {
- Ok(module) => Ok(Some(module)),
- Err(e) => {
- // Currently if custom signals/virtual memory are disabled then this
- // example is expected to fail to load since loading native code
- // requires virtual memory. In the future this will go away as when
- // signals-based-traps is disabled then that means that the
- // interpreter should be used which should work here.
- if !cfg!(feature = "custom")
- && e.to_string()
- .contains("requires virtual memory to be enabled")
- {
- Ok(None)
- } else {
- Err(e)
- }
- }
- }
+fn deserialize(engine: &Engine, module: &[u8]) -> Result<Module> {
+ // Note that this copies `module` into memory owned by Wasmtime and aligned
+ // per `CustomCodeMemory::required_alignment`, if configured, which is then
+ // made executable.
+ unsafe { Module::deserialize(engine, module) }
}
diff --git a/examples/min-platform/embedding/src/panic.rs b/examples/min-platform/embedding/src/panic.rs
index 81e24c9a53..e23559f889 100644
--- a/examples/min-platform/embedding/src/panic.rs
+++ b/examples/min-platform/embedding/src/panic.rs
@@ -8,5 +8,13 @@ fn handler(_info: &PanicInfo) -> ! {
// out. A more complete embedding would likely turn `info` into a
// stack-allocated string and then pass that as a message to the outer
// system to get printed and trigger a failure.
+ //
+ // Here an illegal instruction is executed, where possible, to abort the
+ // process so that failures don't manifest as hangs.
+ #[cfg(target_arch = "x86_64")]
+ unsafe {
+ core::arch::asm!("ud2", options(noreturn));
+ }
+ #[cfg(not(target_arch = "x86_64"))]
loop {}
}
diff --git a/examples/min-platform/embedding/wasmtime-platform.c b/examples/min-platform/embedding/wasmtime-platform.c
index 1a18a8c7d0..2e4d6ecfff 100644
--- a/examples/min-platform/embedding/wasmtime-platform.c
+++ b/examples/min-platform/embedding/wasmtime-platform.c
@@ -155,8 +155,11 @@ void wasmtime_tls_set(size_t slot, uint8_t *val) {
#else
-// Single-threaded TLS using a static variable
-static uint8_t *WASMTIME_TLS[2] = {NULL, NULL};
+// Thread-local storage via the compiler's native support, which doesn't
+// require any synchronization. Note that this is still per-thread state as
+// separate threads may each be using Wasmtime (for example with a separate
+// `Engine` per thread) even without custom synchronization primitives.
+static _Thread_local uint8_t *WASMTIME_TLS[2] = {NULL, NULL};
uint8_t *wasmtime_tls_get(size_t slot) { return WASMTIME_TLS[slot]; }
@@ -256,3 +259,18 @@ void wasmtime_sync_rwlock_write_release(uintptr_t *lock) {
}
#endif // WASMTIME_CUSTOM_SYNC
+
+// The following are not part of Wasmtime's platform API but are instead used
+// by this example embedding's `CustomCodeMemory` implementation to make loaded
+// code executable when Wasmtime itself doesn't use virtual memory (i.e. without
+// `WASMTIME_VIRTUAL_MEMORY`). `ptr` is page-aligned, and the embedding's
+// allocator ensures that the pages spanned by `len` are not shared with any
+// other allocation.
+
+int embedding_publish_executable(const uint8_t *ptr, size_t len) {
+ return mprotect((void *)ptr, len, PROT_READ | PROT_EXEC);
+}
+
+int embedding_unpublish_executable(const uint8_t *ptr, size_t len) {
+ return mprotect((void *)ptr, len, PROT_READ | PROT_WRITE);
+}
diff --git a/examples/min-platform/src/main.rs b/examples/min-platform/src/main.rs
index 798549eb5c..6f778c5111 100644
--- a/examples/min-platform/src/main.rs
+++ b/examples/min-platform/src/main.rs
@@ -49,6 +49,9 @@ fn main() -> Result<()> {
match sym.name()? {
"memmove" | "memset" | "memcmp" | "memcpy" | "bcmp" | "__tls_get_addr" => {}
s if s.starts_with("wasmtime_") => {}
+ // Hooks used by the embedding's `CustomCodeMemory`, also provided by
+ // `wasmtime-platform.c`.
+ "embedding_publish_executable" | "embedding_unpublish_executable" => {}
other => {
panic!("unexpected dependency on symbol `{other}`")
}
@@ -187,6 +190,61 @@ fn main() -> Result<()> {
bail!("{}", String::from_utf8_lossy(&error_buf));
}
+ // Without signals-based traps Wasmtime should not touch any
+ // process-global synchronized state when loading, running, and
+ // unloading modules (and trapping). The embedding is built without
+ // custom sync primitives, meaning that Wasmtime's internal locks panic
+ // on contention, so this tests that guarantee by running separate
+ // engines in separate threads concurrently.
+ if !cfg!(feature = "custom") {
+ let concurrent = engine.precompile_module(
+ br#"
+ (module
+ (memory 1 1)
+ (func (export "add") (param i32 i32) (result i32)
+ (i32.add (local.get 0) (local.get 1)))
+ (func (export "unreachable")
+ unreachable)
+ (func (export "load") (param i32) (result i32)
+ (i32.load (local.get 0)))
+ )
+ "#,
+ )?;
+
+ let run_concurrent_module_load_test: Symbol<
+ extern "C" fn(*mut u8, usize, *const u8, usize, usize) -> usize,
+ > = lib.get(b"run_concurrent_module_load_test").context(
+ "failed to find the `run_concurrent_module_load_test` symbol in the library",
+ )?;
+
+ const THREADS: usize = 2;
+ const ITERATIONS: usize = 2000;
+ let barrier = std::sync::Barrier::new(THREADS);
+ std::thread::scope(|s| {
+ let threads = (0..THREADS)
+ .map(|_| {
+ s.spawn(|| {
+ barrier.wait();
+ let mut error_buf = Vec::with_capacity(1024);
+ let len = run_concurrent_module_load_test(
+ error_buf.as_mut_ptr(),
+ error_buf.capacity(),
+ concurrent.as_ptr(),
+ concurrent.len(),
+ ITERATIONS,
+ );
+ error_buf.set_len(len);
+ if len > 0 {
+ bail!("{}", String::from_utf8_lossy(&error_buf));
+ }
+ Ok(())
+ })
+ })
+ .collect::<Vec<_>>();
+ threads.into_iter().try_for_each(|t| t.join().unwrap())
+ })?;
+ }
+
#[cfg(feature = "wasi")]
{
let wasi_component_path = args