Remove some custom error types in Wasmtime (#5347)
* Remove some custom error types in Wasmtime These types are mostly cumbersome to work with nowadays that `anyhow` is used everywhere else. This commit removes `InstantiationError` and `SetupError` in favor of using `anyhow::Error` throughout. This can eventually culminate in creation of specific errors for embedders to downcast to but for now this should be general enough. * Fix Windows build
This commit is contained in:
@@ -3,13 +3,12 @@ use crate::instance::{Instance, InstanceHandle, RuntimeMemoryCreator};
|
||||
use crate::memory::{DefaultMemoryCreator, Memory};
|
||||
use crate::table::Table;
|
||||
use crate::{CompiledModuleId, ModuleRuntimeInfo, Store};
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use std::alloc;
|
||||
use std::any::Any;
|
||||
use std::convert::TryFrom;
|
||||
use std::ptr;
|
||||
use std::sync::Arc;
|
||||
use thiserror::Error;
|
||||
use wasmtime_environ::{
|
||||
DefinedMemoryIndex, DefinedTableIndex, HostPtr, InitMemory, MemoryInitialization,
|
||||
MemoryInitializer, Module, PrimaryMap, TableInitialization, TableInitializer, Trap, VMOffsets,
|
||||
@@ -86,46 +85,6 @@ impl StorePtr {
|
||||
}
|
||||
}
|
||||
|
||||
/// An link error while instantiating a module.
|
||||
#[derive(Error, Debug)]
|
||||
#[error("Link error: {0}")]
|
||||
pub struct LinkError(pub String);
|
||||
|
||||
/// An error while instantiating a module.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum InstantiationError {
|
||||
/// Insufficient resources available for execution.
|
||||
#[error("Insufficient resources: {0}")]
|
||||
Resource(anyhow::Error),
|
||||
|
||||
/// A wasm link error occurred.
|
||||
#[error("Failed to link module")]
|
||||
Link(#[from] LinkError),
|
||||
|
||||
/// A trap ocurred during instantiation, after linking.
|
||||
#[error("Trap occurred during instantiation")]
|
||||
Trap(Trap),
|
||||
|
||||
/// A limit on how many instances are supported has been reached.
|
||||
#[error("Limit of {0} concurrent instances has been reached")]
|
||||
Limit(u32),
|
||||
}
|
||||
|
||||
/// An error while creating a fiber stack.
|
||||
#[cfg(feature = "async")]
|
||||
#[derive(Error, Debug)]
|
||||
pub enum FiberStackError {
|
||||
/// Insufficient resources available for the request.
|
||||
#[error("Insufficient resources: {0}")]
|
||||
Resource(anyhow::Error),
|
||||
/// An error for when the allocator doesn't support fiber stacks.
|
||||
#[error("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.
|
||||
///
|
||||
/// # Safety
|
||||
@@ -151,10 +110,7 @@ pub unsafe trait InstanceAllocator: Send + Sync {
|
||||
///
|
||||
/// This method is not inherently unsafe, but care must be made to ensure
|
||||
/// pointers passed in the allocation request outlive the returned instance.
|
||||
unsafe fn allocate(
|
||||
&self,
|
||||
req: InstanceAllocationRequest,
|
||||
) -> Result<InstanceHandle, InstantiationError>;
|
||||
unsafe fn allocate(&self, req: InstanceAllocationRequest) -> Result<InstanceHandle>;
|
||||
|
||||
/// Finishes the instantiation process started by an instance allocator.
|
||||
///
|
||||
@@ -166,7 +122,7 @@ pub unsafe trait InstanceAllocator: Send + Sync {
|
||||
handle: &mut InstanceHandle,
|
||||
module: &Module,
|
||||
is_bulk_memory: bool,
|
||||
) -> Result<(), InstantiationError>;
|
||||
) -> Result<()>;
|
||||
|
||||
/// Deallocates a previously allocated instance.
|
||||
///
|
||||
@@ -180,7 +136,7 @@ pub unsafe trait InstanceAllocator: Send + Sync {
|
||||
|
||||
/// Allocates a fiber stack for calling async functions on.
|
||||
#[cfg(feature = "async")]
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack, FiberStackError>;
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack>;
|
||||
|
||||
/// Deallocates a fiber stack that was previously allocated with `allocate_fiber_stack`.
|
||||
///
|
||||
@@ -198,10 +154,7 @@ pub unsafe trait InstanceAllocator: Send + Sync {
|
||||
fn purge_module(&self, module: CompiledModuleId);
|
||||
}
|
||||
|
||||
fn get_table_init_start(
|
||||
init: &TableInitializer,
|
||||
instance: &Instance,
|
||||
) -> Result<u32, InstantiationError> {
|
||||
fn get_table_init_start(init: &TableInitializer, instance: &Instance) -> Result<u32> {
|
||||
match init.base {
|
||||
Some(base) => {
|
||||
let val = unsafe {
|
||||
@@ -212,20 +165,15 @@ fn get_table_init_start(
|
||||
}
|
||||
};
|
||||
|
||||
init.offset.checked_add(val).ok_or_else(|| {
|
||||
InstantiationError::Link(LinkError(
|
||||
"element segment global base overflows".to_owned(),
|
||||
))
|
||||
})
|
||||
init.offset
|
||||
.checked_add(val)
|
||||
.ok_or_else(|| anyhow!("element segment global base overflows"))
|
||||
}
|
||||
None => Ok(init.offset),
|
||||
}
|
||||
}
|
||||
|
||||
fn check_table_init_bounds(
|
||||
instance: &mut Instance,
|
||||
module: &Module,
|
||||
) -> Result<(), InstantiationError> {
|
||||
fn check_table_init_bounds(instance: &mut Instance, module: &Module) -> Result<()> {
|
||||
match &module.table_initialization {
|
||||
TableInitialization::FuncTable { segments, .. }
|
||||
| TableInitialization::Segments { segments } => {
|
||||
@@ -240,9 +188,7 @@ fn check_table_init_bounds(
|
||||
// Initializer is in bounds
|
||||
}
|
||||
_ => {
|
||||
return Err(InstantiationError::Link(LinkError(
|
||||
"table out of bounds: elements segment does not fit".to_owned(),
|
||||
)))
|
||||
bail!("table out of bounds: elements segment does not fit")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,7 +198,7 @@ fn check_table_init_bounds(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize_tables(instance: &mut Instance, module: &Module) -> Result<(), InstantiationError> {
|
||||
fn initialize_tables(instance: &mut Instance, module: &Module) -> Result<()> {
|
||||
// Note: if the module's table initializer state is in
|
||||
// FuncTable mode, we will lazily initialize tables based on
|
||||
// any statically-precomputed image of FuncIndexes, but there
|
||||
@@ -264,15 +210,13 @@ fn initialize_tables(instance: &mut Instance, module: &Module) -> Result<(), Ins
|
||||
TableInitialization::FuncTable { segments, .. }
|
||||
| TableInitialization::Segments { segments } => {
|
||||
for segment in segments {
|
||||
instance
|
||||
.table_init_segment(
|
||||
segment.table_index,
|
||||
&segment.elements,
|
||||
get_table_init_start(segment, instance)?,
|
||||
0,
|
||||
segment.elements.len() as u32,
|
||||
)
|
||||
.map_err(InstantiationError::Trap)?;
|
||||
instance.table_init_segment(
|
||||
segment.table_index,
|
||||
&segment.elements,
|
||||
get_table_init_start(segment, instance)?,
|
||||
0,
|
||||
segment.elements.len() as u32,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -280,10 +224,7 @@ fn initialize_tables(instance: &mut Instance, module: &Module) -> Result<(), Ins
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_memory_init_start(
|
||||
init: &MemoryInitializer,
|
||||
instance: &Instance,
|
||||
) -> Result<u64, InstantiationError> {
|
||||
fn get_memory_init_start(init: &MemoryInitializer, instance: &Instance) -> Result<u64> {
|
||||
match init.base {
|
||||
Some(base) => {
|
||||
let mem64 = instance.module().memory_plans[init.memory_index]
|
||||
@@ -302,18 +243,15 @@ fn get_memory_init_start(
|
||||
}
|
||||
};
|
||||
|
||||
init.offset.checked_add(val).ok_or_else(|| {
|
||||
InstantiationError::Link(LinkError("data segment global base overflows".to_owned()))
|
||||
})
|
||||
init.offset
|
||||
.checked_add(val)
|
||||
.ok_or_else(|| anyhow!("data segment global base overflows"))
|
||||
}
|
||||
None => Ok(init.offset),
|
||||
}
|
||||
}
|
||||
|
||||
fn check_memory_init_bounds(
|
||||
instance: &Instance,
|
||||
initializers: &[MemoryInitializer],
|
||||
) -> Result<(), InstantiationError> {
|
||||
fn check_memory_init_bounds(instance: &Instance, initializers: &[MemoryInitializer]) -> Result<()> {
|
||||
for init in initializers {
|
||||
let memory = instance.get_memory(init.memory_index);
|
||||
let start = get_memory_init_start(init, instance)?;
|
||||
@@ -326,9 +264,7 @@ fn check_memory_init_bounds(
|
||||
// Initializer is in bounds
|
||||
}
|
||||
_ => {
|
||||
return Err(InstantiationError::Link(LinkError(
|
||||
"memory out of bounds: data segment does not fit".into(),
|
||||
)))
|
||||
bail!("memory out of bounds: data segment does not fit")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -336,7 +272,7 @@ fn check_memory_init_bounds(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize_memories(instance: &mut Instance, module: &Module) -> Result<(), InstantiationError> {
|
||||
fn initialize_memories(instance: &mut Instance, module: &Module) -> Result<()> {
|
||||
let memory_size_in_pages =
|
||||
&|memory| (instance.get_memory(memory).current_length() as u64) / u64::from(WASM_PAGE_SIZE);
|
||||
|
||||
@@ -392,13 +328,13 @@ fn initialize_memories(instance: &mut Instance, module: &Module) -> Result<(), I
|
||||
},
|
||||
);
|
||||
if !ok {
|
||||
return Err(InstantiationError::Trap(Trap::MemoryOutOfBounds));
|
||||
return Err(Trap::MemoryOutOfBounds.into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_init_bounds(instance: &mut Instance, module: &Module) -> Result<(), InstantiationError> {
|
||||
fn check_init_bounds(instance: &mut Instance, module: &Module) -> Result<()> {
|
||||
check_table_init_bounds(instance, module)?;
|
||||
|
||||
match &instance.module().memory_initialization {
|
||||
@@ -416,7 +352,7 @@ fn initialize_instance(
|
||||
instance: &mut Instance,
|
||||
module: &Module,
|
||||
is_bulk_memory: bool,
|
||||
) -> Result<(), InstantiationError> {
|
||||
) -> Result<()> {
|
||||
// If bulk memory is not enabled, bounds check the data and element segments before
|
||||
// making any changes. With bulk memory enabled, initializers are processed
|
||||
// in-order and side effects are observed up to the point of an out-of-bounds
|
||||
@@ -456,20 +392,17 @@ impl OnDemandInstanceAllocator {
|
||||
fn create_tables(
|
||||
store: &mut StorePtr,
|
||||
runtime_info: &Arc<dyn ModuleRuntimeInfo>,
|
||||
) -> Result<PrimaryMap<DefinedTableIndex, Table>, InstantiationError> {
|
||||
) -> Result<PrimaryMap<DefinedTableIndex, Table>> {
|
||||
let module = runtime_info.module();
|
||||
let num_imports = module.num_imported_tables;
|
||||
let mut tables: PrimaryMap<DefinedTableIndex, _> =
|
||||
PrimaryMap::with_capacity(module.table_plans.len() - num_imports);
|
||||
for (_, table) in module.table_plans.iter().skip(num_imports) {
|
||||
tables.push(
|
||||
Table::new_dynamic(table, unsafe {
|
||||
store
|
||||
.get()
|
||||
.expect("if module has table plans, store is not empty")
|
||||
})
|
||||
.map_err(InstantiationError::Resource)?,
|
||||
);
|
||||
tables.push(Table::new_dynamic(table, unsafe {
|
||||
store
|
||||
.get()
|
||||
.expect("if module has table plans, store is not empty")
|
||||
})?);
|
||||
}
|
||||
Ok(tables)
|
||||
}
|
||||
@@ -478,7 +411,7 @@ impl OnDemandInstanceAllocator {
|
||||
&self,
|
||||
store: &mut StorePtr,
|
||||
runtime_info: &Arc<dyn ModuleRuntimeInfo>,
|
||||
) -> Result<PrimaryMap<DefinedMemoryIndex, Memory>, InstantiationError> {
|
||||
) -> Result<PrimaryMap<DefinedMemoryIndex, Memory>> {
|
||||
let module = runtime_info.module();
|
||||
let creator = self
|
||||
.mem_creator
|
||||
@@ -491,23 +424,18 @@ impl OnDemandInstanceAllocator {
|
||||
let defined_memory_idx = module
|
||||
.defined_memory_index(memory_idx)
|
||||
.expect("Skipped imports, should never be None");
|
||||
let image = runtime_info
|
||||
.memory_image(defined_memory_idx)
|
||||
.map_err(|err| InstantiationError::Resource(err.into()))?;
|
||||
let image = runtime_info.memory_image(defined_memory_idx)?;
|
||||
|
||||
memories.push(
|
||||
Memory::new_dynamic(
|
||||
plan,
|
||||
creator,
|
||||
unsafe {
|
||||
store
|
||||
.get()
|
||||
.expect("if module has memory plans, store is not empty")
|
||||
},
|
||||
image,
|
||||
)
|
||||
.map_err(InstantiationError::Resource)?,
|
||||
);
|
||||
memories.push(Memory::new_dynamic(
|
||||
plan,
|
||||
creator,
|
||||
unsafe {
|
||||
store
|
||||
.get()
|
||||
.expect("if module has memory plans, store is not empty")
|
||||
},
|
||||
image,
|
||||
)?);
|
||||
}
|
||||
Ok(memories)
|
||||
}
|
||||
@@ -532,7 +460,7 @@ impl Default for OnDemandInstanceAllocator {
|
||||
pub unsafe fn allocate_single_memory_instance(
|
||||
req: InstanceAllocationRequest,
|
||||
memory: Memory,
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
) -> Result<InstanceHandle> {
|
||||
let mut memories = PrimaryMap::default();
|
||||
memories.push(memory);
|
||||
let tables = PrimaryMap::default();
|
||||
@@ -554,10 +482,7 @@ pub unsafe fn deallocate(handle: &InstanceHandle) {
|
||||
}
|
||||
|
||||
unsafe impl InstanceAllocator for OnDemandInstanceAllocator {
|
||||
unsafe fn allocate(
|
||||
&self,
|
||||
mut req: InstanceAllocationRequest,
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
unsafe fn allocate(&self, mut req: InstanceAllocationRequest) -> Result<InstanceHandle> {
|
||||
let memories = self.create_memories(&mut req.store, &req.runtime_info)?;
|
||||
let tables = Self::create_tables(&mut req.store, &req.runtime_info)?;
|
||||
let module = req.runtime_info.module();
|
||||
@@ -577,7 +502,7 @@ unsafe impl InstanceAllocator for OnDemandInstanceAllocator {
|
||||
handle: &mut InstanceHandle,
|
||||
module: &Module,
|
||||
is_bulk_memory: bool,
|
||||
) -> Result<(), InstantiationError> {
|
||||
) -> Result<()> {
|
||||
initialize_instance(handle.instance_mut(), module, is_bulk_memory)
|
||||
}
|
||||
|
||||
@@ -586,13 +511,13 @@ unsafe impl InstanceAllocator for OnDemandInstanceAllocator {
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack, FiberStackError> {
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack> {
|
||||
if self.stack_size == 0 {
|
||||
return Err(FiberStackError::NotSupported);
|
||||
bail!("fiber stacks are not supported by the allocator")
|
||||
}
|
||||
|
||||
wasmtime_fiber::FiberStack::new(self.stack_size)
|
||||
.map_err(|e| FiberStackError::Resource(e.into()))
|
||||
let stack = wasmtime_fiber::FiberStack::new(self.stack_size)?;
|
||||
Ok(stack)
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
|
||||
@@ -7,10 +7,7 @@
|
||||
//! Using the pooling instance allocator can speed up module instantiation
|
||||
//! when modules can be constrained based on configurable limits.
|
||||
|
||||
use super::{
|
||||
initialize_instance, InstanceAllocationRequest, InstanceAllocator, InstanceHandle,
|
||||
InstantiationError,
|
||||
};
|
||||
use super::{initialize_instance, InstanceAllocationRequest, InstanceAllocator, InstanceHandle};
|
||||
use crate::{instance::Instance, Memory, Mmap, Table};
|
||||
use crate::{CompiledModuleId, MemoryImageSlot, ModuleRuntimeInfo, Store};
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
@@ -41,9 +38,6 @@ use imp::{commit_table_pages, decommit_table_pages};
|
||||
#[cfg(all(feature = "async", unix))]
|
||||
use imp::{commit_stack_pages, reset_stack_pages_to_zero};
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
use super::FiberStackError;
|
||||
|
||||
fn round_up_to_pow2(n: usize, to: usize) -> usize {
|
||||
debug_assert!(to > 0);
|
||||
debug_assert!(to.is_power_of_two());
|
||||
@@ -167,7 +161,7 @@ impl InstancePool {
|
||||
&self,
|
||||
instance_index: usize,
|
||||
req: InstanceAllocationRequest,
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
) -> Result<InstanceHandle> {
|
||||
let module = req.runtime_info.module();
|
||||
|
||||
// Before doing anything else ensure that our instance slot is actually
|
||||
@@ -175,9 +169,7 @@ impl InstancePool {
|
||||
// If this fails then it's a configuration error at the `Engine` level
|
||||
// from when this pooling allocator was created and that needs updating
|
||||
// if this is to succeed.
|
||||
let offsets = self
|
||||
.validate_instance_size(module)
|
||||
.map_err(InstantiationError::Resource)?;
|
||||
let offsets = self.validate_instance_size(module)?;
|
||||
|
||||
let mut memories =
|
||||
PrimaryMap::with_capacity(module.memory_plans.len() - module.num_imported_memories);
|
||||
@@ -214,14 +206,16 @@ impl InstancePool {
|
||||
})
|
||||
}
|
||||
|
||||
fn allocate(
|
||||
&self,
|
||||
req: InstanceAllocationRequest,
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
fn allocate(&self, req: InstanceAllocationRequest) -> Result<InstanceHandle> {
|
||||
let id = self
|
||||
.index_allocator
|
||||
.alloc(req.runtime_info.unique_id())
|
||||
.ok_or_else(|| InstantiationError::Limit(self.max_instances as u32))?;
|
||||
.ok_or_else(|| {
|
||||
anyhow!(
|
||||
"maximum concurrent instance limit of {} reached",
|
||||
self.max_instances
|
||||
)
|
||||
})?;
|
||||
|
||||
match unsafe { self.initialize_instance(id.index(), req) } {
|
||||
Ok(handle) => Ok(handle),
|
||||
@@ -271,7 +265,7 @@ impl InstancePool {
|
||||
store: Option<*mut dyn Store>,
|
||||
memories: &mut PrimaryMap<DefinedMemoryIndex, Memory>,
|
||||
tables: &mut PrimaryMap<DefinedTableIndex, Table>,
|
||||
) -> Result<(), InstantiationError> {
|
||||
) -> Result<()> {
|
||||
self.allocate_memories(instance_index, runtime_info, store, memories)?;
|
||||
self.allocate_tables(instance_index, runtime_info, store, tables)?;
|
||||
|
||||
@@ -284,11 +278,10 @@ impl InstancePool {
|
||||
runtime_info: &dyn ModuleRuntimeInfo,
|
||||
store: Option<*mut dyn Store>,
|
||||
memories: &mut PrimaryMap<DefinedMemoryIndex, Memory>,
|
||||
) -> Result<(), InstantiationError> {
|
||||
) -> Result<()> {
|
||||
let module = runtime_info.module();
|
||||
|
||||
self.validate_memory_plans(module)
|
||||
.map_err(InstantiationError::Resource)?;
|
||||
self.validate_memory_plans(module)?;
|
||||
|
||||
for (memory_index, plan) in module
|
||||
.memory_plans
|
||||
@@ -321,9 +314,7 @@ impl InstancePool {
|
||||
let mut slot = self
|
||||
.memories
|
||||
.take_memory_image_slot(instance_index, defined_index);
|
||||
let image = runtime_info
|
||||
.memory_image(defined_index)
|
||||
.map_err(|err| InstantiationError::Resource(err.into()))?;
|
||||
let image = runtime_info.memory_image(defined_index)?;
|
||||
let initial_size = plan.memory.minimum * WASM_PAGE_SIZE as u64;
|
||||
|
||||
// If instantiation fails, we can propagate the error
|
||||
@@ -339,13 +330,11 @@ impl InstancePool {
|
||||
// the process to continue, because we never perform a
|
||||
// mmap that would leave an open space for someone
|
||||
// else to come in and map something.
|
||||
slot.instantiate(initial_size as usize, image, &plan.style)
|
||||
.map_err(|e| InstantiationError::Resource(e.into()))?;
|
||||
slot.instantiate(initial_size as usize, image, &plan.style)?;
|
||||
|
||||
memories.push(
|
||||
Memory::new_static(plan, memory, slot, unsafe { &mut *store.unwrap() })
|
||||
.map_err(InstantiationError::Resource)?,
|
||||
);
|
||||
memories.push(Memory::new_static(plan, memory, slot, unsafe {
|
||||
&mut *store.unwrap()
|
||||
})?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -380,11 +369,10 @@ impl InstancePool {
|
||||
runtime_info: &dyn ModuleRuntimeInfo,
|
||||
store: Option<*mut dyn Store>,
|
||||
tables: &mut PrimaryMap<DefinedTableIndex, Table>,
|
||||
) -> Result<(), InstantiationError> {
|
||||
) -> Result<()> {
|
||||
let module = runtime_info.module();
|
||||
|
||||
self.validate_table_plans(module)
|
||||
.map_err(InstantiationError::Resource)?;
|
||||
self.validate_table_plans(module)?;
|
||||
|
||||
let mut bases = self.tables.get(instance_index);
|
||||
for (_, plan) in module.table_plans.iter().skip(module.num_imported_tables) {
|
||||
@@ -393,19 +381,13 @@ impl InstancePool {
|
||||
commit_table_pages(
|
||||
base as *mut u8,
|
||||
self.tables.max_elements as usize * mem::size_of::<*mut u8>(),
|
||||
)
|
||||
.map_err(InstantiationError::Resource)?;
|
||||
)?;
|
||||
|
||||
tables.push(
|
||||
Table::new_static(
|
||||
plan,
|
||||
unsafe {
|
||||
std::slice::from_raw_parts_mut(base, self.tables.max_elements as usize)
|
||||
},
|
||||
unsafe { &mut *store.unwrap() },
|
||||
)
|
||||
.map_err(InstantiationError::Resource)?,
|
||||
);
|
||||
tables.push(Table::new_static(
|
||||
plan,
|
||||
unsafe { std::slice::from_raw_parts_mut(base, self.tables.max_elements as usize) },
|
||||
unsafe { &mut *store.unwrap() },
|
||||
)?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -930,15 +912,20 @@ impl StackPool {
|
||||
})
|
||||
}
|
||||
|
||||
fn allocate(&self) -> Result<wasmtime_fiber::FiberStack, FiberStackError> {
|
||||
fn allocate(&self) -> Result<wasmtime_fiber::FiberStack> {
|
||||
if self.stack_size == 0 {
|
||||
return Err(FiberStackError::NotSupported);
|
||||
bail!("pooling allocator not configured to enable fiber stack allocation");
|
||||
}
|
||||
|
||||
let index = self
|
||||
.index_allocator
|
||||
.alloc(None)
|
||||
.ok_or(FiberStackError::Limit(self.max_instances as u32))?
|
||||
.ok_or_else(|| {
|
||||
anyhow!(
|
||||
"maximum concurrent fiber limit of {} reached",
|
||||
self.max_instances
|
||||
)
|
||||
})?
|
||||
.index();
|
||||
|
||||
assert!(index < self.max_instances);
|
||||
@@ -952,11 +939,11 @@ impl StackPool {
|
||||
.as_mut_ptr()
|
||||
.add((index * self.stack_size) + self.page_size);
|
||||
|
||||
commit_stack_pages(bottom_of_stack, size_without_guard)
|
||||
.map_err(FiberStackError::Resource)?;
|
||||
commit_stack_pages(bottom_of_stack, size_without_guard)?;
|
||||
|
||||
wasmtime_fiber::FiberStack::from_top_ptr(bottom_of_stack.add(size_without_guard))
|
||||
.map_err(|e| FiberStackError::Resource(e.into()))
|
||||
let stack =
|
||||
wasmtime_fiber::FiberStack::from_top_ptr(bottom_of_stack.add(size_without_guard))?;
|
||||
Ok(stack)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1106,10 +1093,7 @@ unsafe impl InstanceAllocator for PoolingInstanceAllocator {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
unsafe fn allocate(
|
||||
&self,
|
||||
req: InstanceAllocationRequest,
|
||||
) -> Result<InstanceHandle, InstantiationError> {
|
||||
unsafe fn allocate(&self, req: InstanceAllocationRequest) -> Result<InstanceHandle> {
|
||||
self.instances.allocate(req)
|
||||
}
|
||||
|
||||
@@ -1118,7 +1102,7 @@ unsafe impl InstanceAllocator for PoolingInstanceAllocator {
|
||||
handle: &mut InstanceHandle,
|
||||
module: &Module,
|
||||
is_bulk_memory: bool,
|
||||
) -> Result<(), InstantiationError> {
|
||||
) -> Result<()> {
|
||||
let instance = handle.instance_mut();
|
||||
initialize_instance(instance, module, is_bulk_memory)
|
||||
}
|
||||
@@ -1128,7 +1112,7 @@ unsafe impl InstanceAllocator for PoolingInstanceAllocator {
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "async", unix))]
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack, FiberStackError> {
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack> {
|
||||
self.stacks.allocate()
|
||||
}
|
||||
|
||||
@@ -1138,14 +1122,14 @@ unsafe impl InstanceAllocator for PoolingInstanceAllocator {
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "async", windows))]
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack, FiberStackError> {
|
||||
fn allocate_fiber_stack(&self) -> Result<wasmtime_fiber::FiberStack> {
|
||||
if self.stack_size == 0 {
|
||||
return Err(FiberStackError::NotSupported);
|
||||
bail!("fiber stack allocation not supported")
|
||||
}
|
||||
|
||||
// On windows, we don't use a stack pool as we use the native fiber implementation
|
||||
wasmtime_fiber::FiberStack::new(self.stack_size)
|
||||
.map_err(|e| FiberStackError::Resource(e.into()))
|
||||
let stack = wasmtime_fiber::FiberStack::new(self.stack_size)?;
|
||||
Ok(stack)
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "async", windows))]
|
||||
@@ -1269,7 +1253,7 @@ mod test {
|
||||
host_state: Box::new(()),
|
||||
store: StorePtr::empty(),
|
||||
}) {
|
||||
Err(InstantiationError::Limit(3)) => {}
|
||||
Err(_) => {}
|
||||
_ => panic!("unexpected error"),
|
||||
};
|
||||
|
||||
@@ -1414,10 +1398,7 @@ mod test {
|
||||
|
||||
assert_eq!(pool.index_allocator.testing_freelist(), []);
|
||||
|
||||
match pool.allocate().unwrap_err() {
|
||||
FiberStackError::Limit(10) => {}
|
||||
_ => panic!("unexpected error"),
|
||||
};
|
||||
pool.allocate().unwrap_err();
|
||||
|
||||
for stack in stacks {
|
||||
pool.deallocate(&stack);
|
||||
|
||||
Reference in New Issue
Block a user