Implement the pooling instance allocator.
This commit implements the pooling instance allocator. The allocation strategy can be set with `Config::with_allocation_strategy`. The pooling strategy uses the pooling instance allocator to preallocate a contiguous region of memory for instantiating modules that adhere to various limits. The intention of the pooling instance allocator is to reserve as much of the host address space needed for instantiating modules ahead of time and to reuse committed memory pages wherever possible.
This commit is contained in:
66
Cargo.lock
generated
66
Cargo.lock
generated
@@ -202,6 +202,25 @@ dependencies = [
|
||||
"which",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bindgen"
|
||||
version = "0.57.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd4865004a46a0aafb2a0a5eb19d3c9fc46ee5f063a6cfc605c69ac9ecf5263d"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cexpr",
|
||||
"clang-sys",
|
||||
"lazy_static",
|
||||
"lazycell",
|
||||
"peeking_take_while",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"regex",
|
||||
"rustc-hash",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit-set"
|
||||
version = "0.5.2"
|
||||
@@ -1556,6 +1575,19 @@ version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0debeb9fcf88823ea64d64e4a815ab1643f33127d995978e099942ce38f25238"
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cc",
|
||||
"cfg-if 0.1.10",
|
||||
"libc",
|
||||
"void",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "5.1.2"
|
||||
@@ -1703,7 +1735,7 @@ version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fb64bef270a1ff665b0b2e28ebfa213e6205a007ce88223d020730225d6008f"
|
||||
dependencies = [
|
||||
"bindgen",
|
||||
"bindgen 0.55.1",
|
||||
"cmake",
|
||||
]
|
||||
|
||||
@@ -2918,6 +2950,30 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "userfaultfd"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18d8164d4a8198fa546e7553b529f53e82907214a25fafda4a6f90d978b30a5c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
"nix",
|
||||
"thiserror",
|
||||
"userfaultfd-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "userfaultfd-sys"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ada4f4ae167325015f52cc65f9fb6c251b868d8fb3b6dd0ce2d60e497c4870a"
|
||||
dependencies = [
|
||||
"bindgen 0.57.0",
|
||||
"cc",
|
||||
"cfg-if 0.1.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
@@ -2930,6 +2986,12 @@ version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
|
||||
|
||||
[[package]]
|
||||
name = "void"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
||||
|
||||
[[package]]
|
||||
name = "wait-timeout"
|
||||
version = "0.2.0"
|
||||
@@ -3397,8 +3459,10 @@ dependencies = [
|
||||
"memoffset",
|
||||
"more-asserts",
|
||||
"psm",
|
||||
"rand 0.7.3",
|
||||
"region",
|
||||
"thiserror",
|
||||
"userfaultfd",
|
||||
"wasmtime-environ",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
@@ -50,6 +50,7 @@ fn align(offset: u32, width: u32) -> u32 {
|
||||
|
||||
/// This class computes offsets to fields within `VMContext` and other
|
||||
/// related structs that JIT code accesses directly.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct VMOffsets {
|
||||
/// The size in bytes of a pointer on the target.
|
||||
pub pointer_size: u8,
|
||||
|
||||
@@ -24,10 +24,14 @@ cfg-if = "1.0"
|
||||
backtrace = "0.3.55"
|
||||
lazy_static = "1.3.0"
|
||||
psm = "0.1.11"
|
||||
rand = "0.7.3"
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
winapi = { version = "0.3.7", features = ["winbase", "memoryapi", "errhandlingapi"] }
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
userfaultfd = { version = "0.3.0", optional = true }
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
|
||||
|
||||
@@ -26,6 +26,12 @@ use wasmtime_environ::{
|
||||
ir, Module, ModuleTranslation, ModuleType, OwnedDataInitializer, TableElements, VMOffsets,
|
||||
};
|
||||
|
||||
mod pooling;
|
||||
|
||||
pub use self::pooling::{
|
||||
InstanceLimits, ModuleLimits, PoolingAllocationStrategy, PoolingInstanceAllocator,
|
||||
};
|
||||
|
||||
/// Represents a request for a new runtime instance.
|
||||
pub struct InstanceAllocationRequest<'a> {
|
||||
/// The module being instantiated.
|
||||
@@ -72,11 +78,18 @@ pub enum InstantiationError {
|
||||
/// 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.
|
||||
#[derive(Error, Debug)]
|
||||
pub enum FiberStackError {
|
||||
/// Insufficient resources available for the request.
|
||||
#[error("Insufficient resources: {0}")]
|
||||
Resource(String),
|
||||
/// An error for when the allocator doesn't support custom fiber stacks.
|
||||
#[error("Custom fiber stacks are not supported by the allocator")]
|
||||
NotSupported,
|
||||
@@ -218,7 +231,7 @@ unsafe fn initialize_vmcontext(
|
||||
globals.len(),
|
||||
);
|
||||
|
||||
// Initialize the defined functions
|
||||
// Initialize the functions
|
||||
for (index, sig) in instance.module.functions.iter() {
|
||||
let type_index = lookup_shared_signature(*sig);
|
||||
|
||||
|
||||
1666
crates/runtime/src/instance/allocator/pooling.rs
Normal file
1666
crates/runtime/src/instance/allocator/pooling.rs
Normal file
File diff suppressed because it is too large
Load Diff
22
crates/runtime/src/instance/allocator/pooling/linux.rs
Normal file
22
crates/runtime/src/instance/allocator/pooling/linux.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::Mmap;
|
||||
|
||||
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
|
||||
region::protect(addr, len, region::Protection::READ_WRITE).is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn decommit(addr: *mut u8, len: usize) {
|
||||
region::protect(addr, len, region::Protection::NONE).unwrap();
|
||||
|
||||
// On Linux, this is enough to cause the kernel to initialize the pages to 0 on next access
|
||||
assert_eq!(
|
||||
libc::madvise(addr as _, len, libc::MADV_DONTNEED),
|
||||
0,
|
||||
"madvise failed to mark pages as missing: {}",
|
||||
std::io::Error::last_os_error()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
|
||||
Mmap::accessible_reserved(accessible_size, mapping_size)
|
||||
.map_err(|e| format!("failed to allocate pool memory: {}", e))
|
||||
}
|
||||
26
crates/runtime/src/instance/allocator/pooling/unix.rs
Normal file
26
crates/runtime/src/instance/allocator/pooling/unix.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::Mmap;
|
||||
|
||||
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
|
||||
region::protect(addr, len, region::Protection::READ_WRITE).is_ok()
|
||||
}
|
||||
|
||||
pub unsafe fn decommit(addr: *mut u8, len: usize) {
|
||||
assert_eq!(
|
||||
libc::mmap(
|
||||
addr as _,
|
||||
len,
|
||||
libc::PROT_NONE,
|
||||
libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_FIXED,
|
||||
-1,
|
||||
0,
|
||||
) as *mut u8,
|
||||
addr,
|
||||
"mmap failed to remap pages: {}",
|
||||
std::io::Error::last_os_error()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
|
||||
Mmap::accessible_reserved(accessible_size, mapping_size)
|
||||
.map_err(|e| format!("failed to allocate pool memory: {}", e))
|
||||
}
|
||||
21
crates/runtime/src/instance/allocator/pooling/windows.rs
Normal file
21
crates/runtime/src/instance/allocator/pooling/windows.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::Mmap;
|
||||
use winapi::um::memoryapi::{VirtualAlloc, VirtualFree};
|
||||
use winapi::um::winnt::{MEM_COMMIT, MEM_DECOMMIT, PAGE_READWRITE};
|
||||
|
||||
pub unsafe fn make_accessible(addr: *mut u8, len: usize) -> bool {
|
||||
// This doesn't use the `region` crate because the memory needs to be committed
|
||||
!VirtualAlloc(addr as _, len, MEM_COMMIT, PAGE_READWRITE).is_null()
|
||||
}
|
||||
|
||||
pub unsafe fn decommit(addr: *mut u8, len: usize) {
|
||||
assert!(
|
||||
VirtualFree(addr as _, len, MEM_DECOMMIT) != 0,
|
||||
"failed to decommit memory pages: {}",
|
||||
std::io::Error::last_os_error()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn create_memory_map(accessible_size: usize, mapping_size: usize) -> Result<Mmap, String> {
|
||||
Mmap::accessible_reserved(accessible_size, mapping_size)
|
||||
.map_err(|e| format!("failed to allocate pool memory: {}", e))
|
||||
}
|
||||
@@ -38,8 +38,9 @@ pub use crate::export::*;
|
||||
pub use crate::externref::*;
|
||||
pub use crate::imports::Imports;
|
||||
pub use crate::instance::{
|
||||
FiberStackError, InstanceAllocationRequest, InstanceAllocator, InstanceHandle,
|
||||
InstantiationError, LinkError, OnDemandInstanceAllocator, RuntimeInstance,
|
||||
FiberStackError, InstanceAllocationRequest, InstanceAllocator, InstanceHandle, InstanceLimits,
|
||||
InstantiationError, LinkError, ModuleLimits, OnDemandInstanceAllocator,
|
||||
PoolingAllocationStrategy, PoolingInstanceAllocator, RuntimeInstance,
|
||||
};
|
||||
pub use crate::jit_int::GdbJitImageRegistration;
|
||||
pub use crate::memory::{Memory, RuntimeLinearMemory, RuntimeMemoryCreator};
|
||||
|
||||
@@ -164,7 +164,7 @@ impl RuntimeLinearMemory for MmapMemory {
|
||||
|
||||
/// Return a `VMMemoryDefinition` for exposing the memory to compiled wasm code.
|
||||
fn vmmemory(&self) -> VMMemoryDefinition {
|
||||
let mut mmap = self.mmap.borrow_mut();
|
||||
let mmap = self.mmap.borrow();
|
||||
VMMemoryDefinition {
|
||||
base: mmap.alloc.as_mut_ptr(),
|
||||
current_length: mmap.size as usize * WASM_PAGE_SIZE as usize,
|
||||
@@ -177,7 +177,7 @@ enum MemoryStorage {
|
||||
base: *mut u8,
|
||||
size: Cell<u32>,
|
||||
maximum: u32,
|
||||
make_accessible: Option<fn(*mut u8, usize) -> bool>,
|
||||
make_accessible: unsafe fn(*mut u8, usize) -> bool,
|
||||
},
|
||||
Dynamic(Box<dyn RuntimeLinearMemory>),
|
||||
}
|
||||
@@ -203,15 +203,15 @@ impl Memory {
|
||||
plan: &MemoryPlan,
|
||||
base: *mut u8,
|
||||
maximum: u32,
|
||||
make_accessible: Option<fn(*mut u8, usize) -> bool>,
|
||||
make_accessible: unsafe fn(*mut u8, usize) -> bool,
|
||||
) -> Result<Self, String> {
|
||||
if plan.memory.minimum > 0 {
|
||||
if let Some(make_accessible) = &make_accessible {
|
||||
if !make_accessible(base, plan.memory.minimum as usize * WASM_PAGE_SIZE as usize) {
|
||||
if unsafe {
|
||||
!make_accessible(base, plan.memory.minimum as usize * WASM_PAGE_SIZE as usize)
|
||||
} {
|
||||
return Err("memory cannot be made accessible".into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
storage: MemoryStorage::Static {
|
||||
@@ -258,11 +258,9 @@ impl Memory {
|
||||
let start = usize::try_from(old_size).unwrap() * WASM_PAGE_SIZE as usize;
|
||||
let len = usize::try_from(delta).unwrap() * WASM_PAGE_SIZE as usize;
|
||||
|
||||
if let Some(make_accessible) = make_accessible {
|
||||
if !make_accessible(unsafe { base.add(start) }, len) {
|
||||
if unsafe { !make_accessible(base.add(start), len) } {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
size.set(new_size);
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ impl Mmap {
|
||||
}
|
||||
|
||||
/// Return the allocated memory as a mutable pointer to u8.
|
||||
pub fn as_mut_ptr(&mut self) -> *mut u8 {
|
||||
pub fn as_mut_ptr(&self) -> *mut u8 {
|
||||
self.ptr as *mut u8
|
||||
}
|
||||
|
||||
@@ -247,6 +247,11 @@ impl Mmap {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) unsafe fn from_raw(ptr: usize, len: usize) -> Self {
|
||||
Self { ptr, len }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Mmap {
|
||||
|
||||
@@ -66,6 +66,20 @@ enum TableElements {
|
||||
ExternRefs(Vec<Option<VMExternRef>>),
|
||||
}
|
||||
|
||||
// Ideally this should be static assertion that table elements are pointer-sized
|
||||
#[inline(always)]
|
||||
pub(crate) fn max_table_element_size() -> usize {
|
||||
debug_assert_eq!(
|
||||
std::mem::size_of::<*mut VMCallerCheckedAnyfunc>(),
|
||||
std::mem::size_of::<*const ()>()
|
||||
);
|
||||
debug_assert_eq!(
|
||||
std::mem::size_of::<Option<VMExternRef>>(),
|
||||
std::mem::size_of::<*const ()>()
|
||||
);
|
||||
std::mem::size_of::<*const ()>()
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum TableStorage {
|
||||
Static {
|
||||
|
||||
@@ -14,7 +14,10 @@ use wasmtime_environ::settings::{self, Configurable, SetError};
|
||||
use wasmtime_environ::{isa, isa::TargetIsa, Tunables};
|
||||
use wasmtime_jit::{native, CompilationStrategy, Compiler};
|
||||
use wasmtime_profiling::{JitDumpAgent, NullProfilerAgent, ProfilingAgent, VTuneAgent};
|
||||
use wasmtime_runtime::{InstanceAllocator, OnDemandInstanceAllocator};
|
||||
use wasmtime_runtime::{InstanceAllocator, OnDemandInstanceAllocator, PoolingInstanceAllocator};
|
||||
|
||||
// Re-export the limit structures for the pooling allocator
|
||||
pub use wasmtime_runtime::{InstanceLimits, ModuleLimits, PoolingAllocationStrategy};
|
||||
|
||||
/// Represents the module instance allocation strategy to use.
|
||||
#[derive(Clone)]
|
||||
@@ -26,6 +29,19 @@ pub enum InstanceAllocationStrategy {
|
||||
///
|
||||
/// This is the default allocation strategy for Wasmtime.
|
||||
OnDemand,
|
||||
/// The pooling instance allocation strategy.
|
||||
///
|
||||
/// A pool of resources is created in advance and module instantiation reuses resources
|
||||
/// from the pool. Resources are returned to the pool when the `Store` referencing the instance
|
||||
/// is dropped.
|
||||
Pooling {
|
||||
/// The allocation strategy to use.
|
||||
strategy: PoolingAllocationStrategy,
|
||||
/// The module limits to use.
|
||||
module_limits: ModuleLimits,
|
||||
/// The instance limits to use.
|
||||
instance_limits: InstanceLimits,
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for InstanceAllocationStrategy {
|
||||
@@ -205,6 +221,9 @@ impl Config {
|
||||
/// on stack overflow, a host function that overflows the stack will
|
||||
/// abort the process.
|
||||
///
|
||||
/// `max_wasm_stack` must be set prior to setting an instance allocation
|
||||
/// strategy.
|
||||
///
|
||||
/// By default this option is 1 MiB.
|
||||
pub fn max_wasm_stack(&mut self, size: usize) -> Result<&mut Self> {
|
||||
#[cfg(feature = "async")]
|
||||
@@ -216,6 +235,12 @@ impl Config {
|
||||
bail!("wasm stack size cannot be zero");
|
||||
}
|
||||
|
||||
if self.instance_allocator.is_some() {
|
||||
bail!(
|
||||
"wasm stack size cannot be modified after setting an instance allocation strategy"
|
||||
);
|
||||
}
|
||||
|
||||
self.max_wasm_stack = size;
|
||||
Ok(self)
|
||||
}
|
||||
@@ -230,12 +255,20 @@ impl Config {
|
||||
/// close to one another; doing so may cause host functions to overflow the
|
||||
/// stack and abort the process.
|
||||
///
|
||||
/// `async_stack_size` must be set prior to setting an instance allocation
|
||||
/// strategy.
|
||||
///
|
||||
/// 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");
|
||||
}
|
||||
if self.instance_allocator.is_some() {
|
||||
bail!(
|
||||
"async stack size cannot be modified after setting an instance allocation strategy"
|
||||
);
|
||||
}
|
||||
self.async_stack_size = size;
|
||||
Ok(self)
|
||||
}
|
||||
@@ -577,14 +610,35 @@ impl Config {
|
||||
}
|
||||
|
||||
/// Sets the instance allocation strategy to use.
|
||||
pub fn with_instance_allocation_strategy(
|
||||
pub fn with_allocation_strategy(
|
||||
&mut self,
|
||||
strategy: InstanceAllocationStrategy,
|
||||
) -> &mut Self {
|
||||
) -> Result<&mut Self> {
|
||||
self.instance_allocator = match strategy {
|
||||
InstanceAllocationStrategy::OnDemand => None,
|
||||
InstanceAllocationStrategy::Pooling {
|
||||
strategy,
|
||||
module_limits,
|
||||
instance_limits,
|
||||
} => {
|
||||
#[cfg(feature = "async")]
|
||||
let stack_size = self.async_stack_size;
|
||||
|
||||
#[cfg(not(feature = "async"))]
|
||||
let stack_size = 0;
|
||||
|
||||
Some(Arc::new(
|
||||
PoolingInstanceAllocator::new(
|
||||
strategy,
|
||||
module_limits,
|
||||
instance_limits,
|
||||
stack_size,
|
||||
)
|
||||
.map_err(|e| anyhow::anyhow!(e))?,
|
||||
))
|
||||
}
|
||||
};
|
||||
self
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Configures the maximum size, in bytes, where a linear memory is
|
||||
|
||||
@@ -364,3 +364,37 @@ fn fuel_eventually_finishes() {
|
||||
let instance = Instance::new_async(&store, &module, &[]);
|
||||
run(instance).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn async_with_pooling_stacks() {
|
||||
let mut config = Config::new();
|
||||
config
|
||||
.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 1,
|
||||
table_elements: 0,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
address_space_size: 1,
|
||||
},
|
||||
})
|
||||
.expect("pooling allocator created");
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
let store = Store::new_async(&engine);
|
||||
let func = Func::new_async(
|
||||
&store,
|
||||
FuncType::new(None, None),
|
||||
(),
|
||||
move |_caller, _state, _params, _results| Box::new(async { Ok(()) }),
|
||||
);
|
||||
run(func.call_async(&[])).unwrap();
|
||||
run(func.call_async(&[])).unwrap();
|
||||
let future1 = func.call_async(&[]);
|
||||
let future2 = func.call_async(&[]);
|
||||
run(future2).unwrap();
|
||||
run(future1).unwrap();
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ mod module;
|
||||
mod module_linking;
|
||||
mod module_serialize;
|
||||
mod name;
|
||||
mod pooling_allocator;
|
||||
mod stack_overflow;
|
||||
mod table;
|
||||
mod traps;
|
||||
|
||||
430
tests/all/pooling_allocator.rs
Normal file
430
tests/all/pooling_allocator.rs
Normal file
@@ -0,0 +1,430 @@
|
||||
use anyhow::Result;
|
||||
use wasmtime::*;
|
||||
|
||||
#[test]
|
||||
fn successful_instantiation() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 1,
|
||||
table_elements: 10,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
address_space_size: 1,
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
let module = Module::new(&engine, r#"(module (memory 1) (table 10 funcref))"#)?;
|
||||
|
||||
// Module should instantiate
|
||||
let store = Store::new(&engine);
|
||||
Instance::new(&store, &module, &[])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memory_limit() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 3,
|
||||
table_elements: 10,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
address_space_size: 196608,
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
// Module should fail to validate because the minimum is greater than the configured limit
|
||||
match Module::new(&engine, r#"(module (memory 4))"#) {
|
||||
Ok(_) => panic!("module compilation should fail"),
|
||||
Err(e) => assert_eq!(e.to_string(), "Validation error: memory index 0 has a minimum page size of 4 which exceeds the limit of 3")
|
||||
}
|
||||
|
||||
let module = Module::new(
|
||||
&engine,
|
||||
r#"(module (memory (export "m") 0) (func (export "f") (result i32) (memory.grow (i32.const 1))))"#,
|
||||
)?;
|
||||
|
||||
// Instantiate the module and grow the memory via the `f` function
|
||||
{
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let f = instance.get_func("f").unwrap().get0::<i32>().unwrap();
|
||||
|
||||
assert_eq!(f().expect("function should not trap"), 0);
|
||||
assert_eq!(f().expect("function should not trap"), 1);
|
||||
assert_eq!(f().expect("function should not trap"), 2);
|
||||
assert_eq!(f().expect("function should not trap"), -1);
|
||||
assert_eq!(f().expect("function should not trap"), -1);
|
||||
}
|
||||
|
||||
// Instantiate the module and grow the memory via the Wasmtime API
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
|
||||
let memory = instance.get_memory("m").unwrap();
|
||||
assert_eq!(memory.size(), 0);
|
||||
assert_eq!(memory.grow(1).expect("memory should grow"), 0);
|
||||
assert_eq!(memory.size(), 1);
|
||||
assert_eq!(memory.grow(1).expect("memory should grow"), 1);
|
||||
assert_eq!(memory.size(), 2);
|
||||
assert_eq!(memory.grow(1).expect("memory should grow"), 2);
|
||||
assert_eq!(memory.size(), 3);
|
||||
assert!(memory.grow(1).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memory_init() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 2,
|
||||
table_elements: 0,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
..Default::default()
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
let module = Module::new(
|
||||
&engine,
|
||||
r#"(module (memory (export "m") 2) (data (i32.const 65530) "this data spans multiple pages") (data (i32.const 10) "hello world"))"#,
|
||||
)?;
|
||||
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let memory = instance.get_memory("m").unwrap();
|
||||
|
||||
unsafe {
|
||||
assert_eq!(
|
||||
&memory.data_unchecked()[65530..65560],
|
||||
b"this data spans multiple pages"
|
||||
);
|
||||
assert_eq!(&memory.data_unchecked()[10..21], b"hello world");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memory_guard_page_trap() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 2,
|
||||
table_elements: 0,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
..Default::default()
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
let module = Module::new(
|
||||
&engine,
|
||||
r#"(module (memory (export "m") 0) (func (export "f") (param i32) local.get 0 i32.load drop))"#,
|
||||
)?;
|
||||
|
||||
// Instantiate the module and check for out of bounds trap
|
||||
for _ in 0..10 {
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let m = instance.get_memory("m").unwrap();
|
||||
let f = instance.get_func("f").unwrap().get1::<i32, ()>().unwrap();
|
||||
|
||||
let trap = f(0).expect_err("function should trap");
|
||||
assert!(trap.to_string().contains("out of bounds"));
|
||||
|
||||
let trap = f(1).expect_err("function should trap");
|
||||
assert!(trap.to_string().contains("out of bounds"));
|
||||
|
||||
m.grow(1).expect("memory should grow");
|
||||
f(0).expect("function should not trap");
|
||||
|
||||
let trap = f(65536).expect_err("function should trap");
|
||||
assert!(trap.to_string().contains("out of bounds"));
|
||||
|
||||
let trap = f(65537).expect_err("function should trap");
|
||||
assert!(trap.to_string().contains("out of bounds"));
|
||||
|
||||
m.grow(1).expect("memory should grow");
|
||||
f(65536).expect("function should not trap");
|
||||
|
||||
m.grow(1).expect_err("memory should be at the limit");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(target_arch = "aarch64", ignore)] // https://github.com/bytecodealliance/wasmtime/pull/2518#issuecomment-747280133
|
||||
fn memory_zeroed() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 1,
|
||||
table_elements: 0,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
address_space_size: 1,
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
let module = Module::new(&engine, r#"(module (memory (export "m") 1))"#)?;
|
||||
|
||||
// Instantiate the module repeatedly after writing data to the entire memory
|
||||
for _ in 0..10 {
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let memory = instance.get_memory("m").unwrap();
|
||||
|
||||
assert_eq!(memory.size(), 1);
|
||||
assert_eq!(memory.data_size(), 65536);
|
||||
|
||||
let ptr = memory.data_ptr();
|
||||
|
||||
unsafe {
|
||||
for i in 0..8192 {
|
||||
assert_eq!(*ptr.cast::<u64>().offset(i), 0);
|
||||
}
|
||||
std::ptr::write_bytes(ptr, 0xFE, memory.data_size());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_limit() -> Result<()> {
|
||||
const TABLE_ELEMENTS: u32 = 10;
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 1,
|
||||
table_elements: TABLE_ELEMENTS,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
address_space_size: 1,
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
// Module should fail to validate because the minimum is greater than the configured limit
|
||||
match Module::new(&engine, r#"(module (table 31 funcref))"#) {
|
||||
Ok(_) => panic!("module compilation should fail"),
|
||||
Err(e) => assert_eq!(e.to_string(), "Validation error: table index 0 has a minimum element size of 31 which exceeds the limit of 10")
|
||||
}
|
||||
|
||||
let module = Module::new(
|
||||
&engine,
|
||||
r#"(module (table (export "t") 0 funcref) (func (export "f") (result i32) (table.grow (ref.null func) (i32.const 1))))"#,
|
||||
)?;
|
||||
|
||||
// Instantiate the module and grow the table via the `f` function
|
||||
{
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let f = instance.get_func("f").unwrap().get0::<i32>().unwrap();
|
||||
|
||||
for i in 0..TABLE_ELEMENTS {
|
||||
assert_eq!(f().expect("function should not trap"), i as i32);
|
||||
}
|
||||
|
||||
assert_eq!(f().expect("function should not trap"), -1);
|
||||
assert_eq!(f().expect("function should not trap"), -1);
|
||||
}
|
||||
|
||||
// Instantiate the module and grow the table via the Wasmtime API
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
|
||||
let table = instance.get_table("t").unwrap();
|
||||
|
||||
for i in 0..TABLE_ELEMENTS {
|
||||
assert_eq!(table.size(), i);
|
||||
assert_eq!(
|
||||
table
|
||||
.grow(1, Val::FuncRef(None))
|
||||
.expect("table should grow"),
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(table.size(), TABLE_ELEMENTS);
|
||||
assert!(table.grow(1, Val::FuncRef(None)).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_init() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 0,
|
||||
table_elements: 6,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
..Default::default()
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
let module = Module::new(
|
||||
&engine,
|
||||
r#"(module (table (export "t") 6 funcref) (elem (i32.const 1) 1 2 3 4) (elem (i32.const 0) 0) (func) (func (param i32)) (func (param i32 i32)) (func (param i32 i32 i32)) (func (param i32 i32 i32 i32)))"#,
|
||||
)?;
|
||||
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let table = instance.get_table("t").unwrap();
|
||||
|
||||
for i in 0..5 {
|
||||
let v = table.get(i).expect("table should have entry");
|
||||
let f = v
|
||||
.funcref()
|
||||
.expect("expected funcref")
|
||||
.expect("expected non-null value");
|
||||
assert_eq!(f.ty().params().len(), i as usize);
|
||||
}
|
||||
|
||||
assert!(
|
||||
table
|
||||
.get(5)
|
||||
.expect("table should have entry")
|
||||
.funcref()
|
||||
.expect("expected funcref")
|
||||
.is_none(),
|
||||
"funcref should be null"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(target_arch = "aarch64", ignore)] // https://github.com/bytecodealliance/wasmtime/pull/2518#issuecomment-747280133
|
||||
fn table_zeroed() -> Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 1,
|
||||
table_elements: 10,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: 1,
|
||||
address_space_size: 1,
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
|
||||
let module = Module::new(&engine, r#"(module (table (export "t") 10 funcref))"#)?;
|
||||
|
||||
// Instantiate the module repeatedly after filling table elements
|
||||
for _ in 0..10 {
|
||||
let store = Store::new(&engine);
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let table = instance.get_table("t").unwrap();
|
||||
let f = Func::wrap(&store, || {});
|
||||
|
||||
assert_eq!(table.size(), 10);
|
||||
|
||||
for i in 0..10 {
|
||||
match table.get(i).unwrap() {
|
||||
Val::FuncRef(r) => assert!(r.is_none()),
|
||||
_ => panic!("expected a funcref"),
|
||||
}
|
||||
table.set(i, Val::FuncRef(Some(f.clone()))).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instantiation_limit() -> Result<()> {
|
||||
const INSTANCE_LIMIT: u32 = 10;
|
||||
let mut config = Config::new();
|
||||
config.with_allocation_strategy(InstanceAllocationStrategy::Pooling {
|
||||
strategy: PoolingAllocationStrategy::NextAvailable,
|
||||
module_limits: ModuleLimits {
|
||||
memory_pages: 1,
|
||||
table_elements: 10,
|
||||
..Default::default()
|
||||
},
|
||||
instance_limits: InstanceLimits {
|
||||
count: INSTANCE_LIMIT,
|
||||
address_space_size: 1,
|
||||
},
|
||||
})?;
|
||||
|
||||
let engine = Engine::new(&config);
|
||||
let module = Module::new(&engine, r#"(module)"#)?;
|
||||
|
||||
// Instantiate to the limit
|
||||
{
|
||||
let store = Store::new(&engine);
|
||||
|
||||
for _ in 0..INSTANCE_LIMIT {
|
||||
Instance::new(&store, &module, &[])?;
|
||||
}
|
||||
|
||||
match Instance::new(&store, &module, &[]) {
|
||||
Ok(_) => panic!("instantiation should fail"),
|
||||
Err(e) => assert_eq!(
|
||||
e.to_string(),
|
||||
format!(
|
||||
"Limit of {} concurrent instances has been reached",
|
||||
INSTANCE_LIMIT
|
||||
)
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// With the above store dropped, ensure instantiations can be made
|
||||
|
||||
let store = Store::new(&engine);
|
||||
|
||||
for _ in 0..INSTANCE_LIMIT {
|
||||
Instance::new(&store, &module, &[])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user