Skip to content

Commit

Permalink
Implement allocating fiber stacks for an instance allocator.
Browse files Browse the repository at this point in the history
This commit implements allocating fiber stacks in an instance allocator.

The on-demand instance allocator doesn't support custom stacks, so the
implementation will use the allocation from `wasmtime-fiber` for the fiber
stacks.

In the future, the pooling instance allocator will return custom stacks to use
on Linux and macOS.

On Windows, the native fiber implementation will always be used.
  • Loading branch information
peterhuene committed Feb 5, 2021
1 parent 84e3bcd commit 3107349
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 50 deletions.
4 changes: 2 additions & 2 deletions crates/c-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub extern "C" fn wasmtime_config_interruptable_set(c: &mut wasm_config_t, enabl
}

#[no_mangle]
pub extern "C" fn wasmtime_config_max_wasm_stack_set(c: &mut wasm_config_t, size: usize) {
c.config.max_wasm_stack(size);
pub extern "C" fn wasmtime_config_max_wasm_stack_set(c: &mut wasm_config_t, size: usize) -> bool {
c.config.max_wasm_stack(size).is_ok()
}

#[no_mangle]
Expand Down
21 changes: 21 additions & 0 deletions crates/fiber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ impl<'a, Resume, Yield, Return> Fiber<'a, Resume, Yield, Return> {
})
}

/// Creates a new fiber with existing stack space that will execute `func`.
///
/// This function returns a `Fiber` which, when resumed, will execute `func`
/// to completion. When desired the `func` can suspend itself via
/// `Fiber::suspend`.
///
/// # Safety
///
/// The caller must properly allocate the stack space with a guard page and
/// make the pages accessible for correct behavior.
pub unsafe fn new_with_stack(
top_of_stack: *mut u8,
func: impl FnOnce(Resume, &Suspend<Resume, Yield, Return>) -> Return + 'a,
) -> io::Result<Fiber<'a, Resume, Yield, Return>> {
Ok(Fiber {
inner: imp::Fiber::new_with_stack(top_of_stack, func),
done: Cell::new(false),
_phantom: PhantomData,
})
}

/// Resumes execution of this fiber.
///
/// This function will transfer execution to the fiber and resume from where
Expand Down
61 changes: 40 additions & 21 deletions crates/fiber/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ use std::io;
use std::ptr;

pub struct Fiber {
// Description of the mmap region we own. This should be abstracted
// eventually so we aren't personally mmap-ing this region.
mmap: *mut libc::c_void,
mmap_len: usize,
// The top of the stack; for stacks allocated by the fiber implementation itself,
// the base address of the allocation will be `top_of_stack.sub(alloc_len.unwrap())`
top_of_stack: *mut u8,
alloc_len: Option<usize>,
}

pub struct Suspend {
Expand Down Expand Up @@ -66,21 +66,40 @@ where
}

impl Fiber {
pub fn new<F, A, B, C>(stack_size: usize, func: F) -> io::Result<Fiber>
pub fn new<F, A, B, C>(stack_size: usize, func: F) -> io::Result<Self>
where
F: FnOnce(A, &super::Suspend<A, B, C>) -> C,
{
let fiber = Self::alloc_with_stack(stack_size)?;
fiber.init(func);
Ok(fiber)
}

pub fn new_with_stack<F, A, B, C>(top_of_stack: *mut u8, func: F) -> Self
where
F: FnOnce(A, &super::Suspend<A, B, C>) -> C,
{
let fiber = Self {
top_of_stack,
alloc_len: None,
};

fiber.init(func);

fiber
}

fn init<F, A, B, C>(&self, func: F)
where
F: FnOnce(A, &super::Suspend<A, B, C>) -> C,
{
let fiber = Fiber::alloc_with_stack(stack_size)?;
unsafe {
// Initialize the top of the stack to be resumed from
let top_of_stack = fiber.top_of_stack();
let data = Box::into_raw(Box::new(func)).cast();
wasmtime_fiber_init(top_of_stack, fiber_start::<F, A, B, C>, data);
Ok(fiber)
wasmtime_fiber_init(self.top_of_stack, fiber_start::<F, A, B, C>, data);
}
}

fn alloc_with_stack(stack_size: usize) -> io::Result<Fiber> {
fn alloc_with_stack(stack_size: usize) -> io::Result<Self> {
unsafe {
// Round up our stack size request to the nearest multiple of the
// page size.
Expand All @@ -104,7 +123,10 @@ impl Fiber {
if mmap == libc::MAP_FAILED {
return Err(io::Error::last_os_error());
}
let ret = Fiber { mmap, mmap_len };
let ret = Self {
top_of_stack: mmap.cast::<u8>().add(mmap_len),
alloc_len: Some(mmap_len),
};
let res = libc::mprotect(
mmap.cast::<u8>().add(page_size).cast(),
stack_size,
Expand All @@ -124,27 +146,24 @@ impl Fiber {
// stack, otherwise known as our reserved slot for this information.
//
// In the diagram above this is updating address 0xAff8
let top_of_stack = self.top_of_stack();
let addr = top_of_stack.cast::<usize>().offset(-1);
let addr = self.top_of_stack.cast::<usize>().offset(-1);
addr.write(result as *const _ as usize);

wasmtime_fiber_switch(top_of_stack);
wasmtime_fiber_switch(self.top_of_stack);

// null this out to help catch use-after-free
addr.write(0);
}
}

unsafe fn top_of_stack(&self) -> *mut u8 {
self.mmap.cast::<u8>().add(self.mmap_len)
}
}

impl Drop for Fiber {
fn drop(&mut self) {
unsafe {
let ret = libc::munmap(self.mmap, self.mmap_len);
debug_assert!(ret == 0);
if let Some(alloc_len) = self.alloc_len {
let ret = libc::munmap(self.top_of_stack.sub(alloc_len) as _, alloc_len);
debug_assert!(ret == 0);
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/fiber/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
}

impl Fiber {
pub fn new<F, A, B, C>(stack_size: usize, func: F) -> io::Result<Fiber>
pub fn new<F, A, B, C>(stack_size: usize, func: F) -> io::Result<Self>
where
F: FnOnce(A, &super::Suspend<A, B, C>) -> C,
{
Expand All @@ -61,11 +61,19 @@ impl Fiber {
drop(Box::from_raw(state.initial_closure.get().cast::<F>()));
Err(io::Error::last_os_error())
} else {
Ok(Fiber { fiber, state })
Ok(Self { fiber, state })
}
}
}

pub fn new_with_stack<F, A, B, C>(_top_of_stack: *mut u8, _func: F) -> Self
where
F: FnOnce(A, &super::Suspend<A, B, C>) -> C,
{
// Windows fibers have no support for custom stacks
unimplemented!()
}

pub(crate) fn resume<A, B, C>(&self, result: &Cell<RunResult<A, B, C>>) {
unsafe {
let is_fiber = IsThreadAFiber() != 0;
Expand Down
6 changes: 6 additions & 0 deletions crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ cc = "1.0"

[badges]
maintenance = { status = "actively-developed" }

[features]
default = ["async"]

# Enables support for "async" fiber stacks in the instance allocator
async = []
41 changes: 41 additions & 0 deletions crates/runtime/src/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ pub enum InstantiationError {
#[error("Trap occurred during instantiation")]
Trap(Trap),
}
/// An error while creating a fiber stack.
#[cfg(feature = "async")]
#[derive(Error, Debug)]
pub enum FiberStackError {
/// An error for when the allocator doesn't support custom fiber stacks.
#[error("Custom fiber stacks are not supported by the allocator")]
NotSupported,
/// A limit on how many fibers are supported has been reached.
#[error("Limit of {0} concurrent fibers has been reached")]
Limit(u32),
}

/// Represents a runtime instance allocator.
///
Expand Down Expand Up @@ -127,6 +138,24 @@ pub unsafe trait InstanceAllocator: Send + Sync {
///
/// Use extreme care when deallocating an instance so that there are no dangling instance pointers.
unsafe fn deallocate(&self, handle: &InstanceHandle);

/// Allocates a fiber stack for calling async functions on.
///
/// Returns the top of the fiber stack if successfully allocated.
#[cfg(feature = "async")]
fn allocate_fiber_stack(&self) -> Result<*mut u8, FiberStackError>;

/// Deallocates a fiber stack that was previously allocated.
///
/// # Safety
///
/// This function is unsafe because there are no guarantees that the given stack
/// is no longer in use.
///
/// Additionally, passing a stack pointer that was not returned from `allocate_fiber_stack`
/// will lead to undefined behavior.
#[cfg(feature = "async")]
unsafe fn deallocate_fiber_stack(&self, stack: *mut u8);
}

unsafe fn initialize_vmcontext(
Expand Down Expand Up @@ -544,4 +573,16 @@ unsafe impl InstanceAllocator for OnDemandInstanceAllocator {
ptr::drop_in_place(instance as *const Instance as *mut Instance);
alloc::dealloc(instance as *const Instance as *mut _, layout);
}

#[cfg(feature = "async")]
fn allocate_fiber_stack(&self) -> Result<*mut u8, FiberStackError> {
// The on-demand allocator does not support allocating fiber stacks
Err(FiberStackError::NotSupported)
}

#[cfg(feature = "async")]
unsafe fn deallocate_fiber_stack(&self, _stack: *mut u8) {
// This should never be called as `allocate_fiber_stack` never returns success
unreachable!()
}
}
3 changes: 3 additions & 0 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ pub use crate::vmcontext::{
VMSharedSignatureIndex, VMTableDefinition, VMTableImport, VMTrampoline,
};

#[cfg(feature = "async")]
pub use crate::instance::FiberStackError;

/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ experimental_x64 = ["wasmtime-jit/experimental_x64"]

# Enables support for "async stores" as well as defining host functions as
# `async fn` and calling functions asynchronously.
async = ["wasmtime-fiber"]
async = ["wasmtime-fiber", "wasmtime-runtime/async"]
59 changes: 49 additions & 10 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub struct Config {
pub(crate) max_instances: usize,
pub(crate) max_tables: usize,
pub(crate) max_memories: usize,
#[cfg(feature = "async")]
pub(crate) async_stack_size: usize,
}

impl Config {
Expand Down Expand Up @@ -108,6 +110,8 @@ impl Config {
max_instances: 10_000,
max_tables: 10_000,
max_memories: 10_000,
#[cfg(feature = "async")]
async_stack_size: 2 << 20,
};
ret.wasm_backtrace_details(WasmBacktraceDetails::Environment);
return ret;
Expand Down Expand Up @@ -182,23 +186,58 @@ impl Config {
self
}

/// Configures the maximum amount of native stack space available to
/// Configures the maximum amount of stack space available for
/// executing WebAssembly code.
///
/// WebAssembly code currently executes on the native call stack for its own
/// call frames. WebAssembly, however, also has well-defined semantics on
/// stack overflow. This is intended to be a knob which can help configure
/// how much native stack space a wasm module is allowed to consume. Note
/// that the number here is not super-precise, but rather wasm will take at
/// most "pretty close to this much" stack space.
/// WebAssembly has well-defined semantics on stack overflow. This is
/// intended to be a knob which can help configure how much stack space
/// wasm execution is allowed to consume. Note that the number here is not
/// super-precise, but rather wasm will take at most "pretty close to this
/// much" stack space.
///
/// If a wasm call (or series of nested wasm calls) take more stack space
/// than the `size` specified then a stack overflow trap will be raised.
///
/// By default this option is 1 MB.
pub fn max_wasm_stack(&mut self, size: usize) -> &mut Self {
/// When the `async` feature is enabled, this value cannot exceed the
/// `async_stack_size` option. Be careful not to set this value too close
/// to `async_stack_size` as doing so may limit how much stack space
/// is available for host functions. Unlike wasm functions that trap
/// on stack overflow, a host function that overflows the stack will
/// abort the process.
///
/// By default this option is 1 MiB.
pub fn max_wasm_stack(&mut self, size: usize) -> Result<&mut Self> {
#[cfg(feature = "async")]
if size > self.async_stack_size {
bail!("wasm stack size cannot exceed the async stack size");
}

if size == 0 {
bail!("wasm stack size cannot be zero");
}

self.max_wasm_stack = size;
self
Ok(self)
}

/// Configures the size of the stacks used for asynchronous execution.
///
/// This setting configures the size of the stacks that are allocated for
/// asynchronous execution. The value cannot be less than `max_wasm_stack`.
///
/// The amount of stack space guaranteed for host functions is
/// `async_stack_size - max_wasm_stack`, so take care not to set these two values
/// close to one another; doing so may cause host functions to overflow the
/// stack and abort the process.
///
/// By default this option is 2 MiB.
#[cfg(feature = "async")]
pub fn async_stack_size(&mut self, size: usize) -> Result<&mut Self> {
if size < self.max_wasm_stack {
bail!("async stack size cannot be less than the maximum wasm stack size");
}
self.async_stack_size = size;
Ok(self)
}

/// Configures whether the WebAssembly threads proposal will be enabled for
Expand Down
Loading

0 comments on commit 3107349

Please sign in to comment.