* Implement the memory64 proposal in Wasmtime This commit implements the WebAssembly [memory64 proposal][proposal] in both Wasmtime and Cranelift. In terms of work done Cranelift ended up needing very little work here since most of it was already prepared for 64-bit memories at one point or another. Most of the work in Wasmtime is largely refactoring, changing a bunch of `u32` values to something else. A number of internal and public interfaces are changing as a result of this commit, for example: * Acessors on `wasmtime::Memory` that work with pages now all return `u64` unconditionally rather than `u32`. This makes it possible to accommodate 64-bit memories with this API, but we may also want to consider `usize` here at some point since the host can't grow past `usize`-limited pages anyway. * The `wasmtime::Limits` structure is removed in favor of minimum/maximum methods on table/memory types. * Many libcall intrinsics called by jit code now unconditionally take `u64` arguments instead of `u32`. Return values are `usize`, however, since the return value, if successful, is always bounded by host memory while arguments can come from any guest. * The `heap_addr` clif instruction now takes a 64-bit offset argument instead of a 32-bit one. It turns out that the legalization of `heap_addr` already worked with 64-bit offsets, so this change was fairly trivial to make. * The runtime implementation of mmap-based linear memories has changed to largely work in `usize` quantities in its API and in bytes instead of pages. This simplifies various aspects and reflects that mmap-memories are always bound by `usize` since that's what the host is using to address things, and additionally most calculations care about bytes rather than pages except for the very edge where we're going to/from wasm. Overall I've tried to minimize the amount of `as` casts as possible, using checked `try_from` and checked arithemtic with either error handling or explicit `unwrap()` calls to tell us about bugs in the future. Most locations have relatively obvious things to do with various implications on various hosts, and I think they should all be roughly of the right shape but time will tell. I mostly relied on the compiler complaining that various types weren't aligned to figure out type-casting, and I manually audited some of the more obvious locations. I suspect we have a number of hidden locations that will panic on 32-bit hosts if 64-bit modules try to run there, but otherwise I think we should be generally ok (famous last words). In any case I wouldn't want to enable this by default naturally until we've fuzzed it for some time. In terms of the actual underlying implementation, no one should expect memory64 to be all that fast. Right now it's implemented with "dynamic" heaps which have a few consequences: * All memory accesses are bounds-checked. I'm not sure how aggressively Cranelift tries to optimize out bounds checks, but I suspect not a ton since we haven't stressed this much historically. * Heaps are always precisely sized. This means that every call to `memory.grow` will incur a `memcpy` of memory from the old heap to the new. We probably want to at least look into `mremap` on Linux and otherwise try to implement schemes where dynamic heaps have some reserved pages to grow into to help amortize the cost of `memory.grow`. The memory64 spec test suite is scheduled to now run on CI, but as with all the other spec test suites it's really not all that comprehensive. I've tried adding more tests for basic things as I've had to implement guards for them, but I wouldn't really consider the testing adequate from just this PR itself. I did try to take care in one test to actually allocate a 4gb+ heap and then avoid running that in the pooling allocator or in emulation because otherwise that may fail or take excessively long. [proposal]: https://github.com/WebAssembly/memory64/blob/master/proposals/memory64/Overview.md * Fix some tests * More test fixes * Fix wasmtime tests * Fix doctests * Revert to 32-bit immediate offsets in `heap_addr` This commit updates the generation of addresses in wasm code to always use 32-bit offsets for `heap_addr`, and if the calculated offset is bigger than 32-bits we emit a manual add with an overflow check. * Disable memory64 for spectest fuzzing * Fix wrong offset being added to heap addr * More comments! * Clarify bytes/pages
388 lines
11 KiB
Rust
388 lines
11 KiB
Rust
use anyhow::Result;
|
|
use wasmtime::*;
|
|
|
|
const WASM_PAGE_SIZE: usize = wasmtime_environ::WASM_PAGE_SIZE as usize;
|
|
|
|
#[test]
|
|
fn test_limits() -> Result<()> {
|
|
let engine = Engine::default();
|
|
let module = Module::new(
|
|
&engine,
|
|
r#"(module (memory (export "m") 0) (table (export "t") 0 anyfunc))"#,
|
|
)?;
|
|
|
|
let mut store = Store::new(
|
|
&engine,
|
|
StoreLimitsBuilder::new()
|
|
.memory_size(10 * WASM_PAGE_SIZE)
|
|
.table_elements(5)
|
|
.build(),
|
|
);
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
|
|
let instance = Instance::new(&mut store, &module, &[])?;
|
|
|
|
// Test instance exports and host objects hitting the limit
|
|
for memory in std::array::IntoIter::new([
|
|
instance.get_memory(&mut store, "m").unwrap(),
|
|
Memory::new(&mut store, MemoryType::new(0, None))?,
|
|
]) {
|
|
memory.grow(&mut store, 3)?;
|
|
memory.grow(&mut store, 5)?;
|
|
memory.grow(&mut store, 2)?;
|
|
|
|
assert_eq!(
|
|
memory
|
|
.grow(&mut store, 1)
|
|
.map_err(|e| e.to_string())
|
|
.unwrap_err(),
|
|
"failed to grow memory by `1`"
|
|
);
|
|
}
|
|
|
|
// Test instance exports and host objects hitting the limit
|
|
for table in std::array::IntoIter::new([
|
|
instance.get_table(&mut store, "t").unwrap(),
|
|
Table::new(
|
|
&mut store,
|
|
TableType::new(ValType::FuncRef, 0, None),
|
|
Val::FuncRef(None),
|
|
)?,
|
|
]) {
|
|
table.grow(&mut store, 2, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 1, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 2, Val::FuncRef(None))?;
|
|
|
|
assert_eq!(
|
|
table
|
|
.grow(&mut store, 1, Val::FuncRef(None))
|
|
.map_err(|e| e.to_string())
|
|
.unwrap_err(),
|
|
"failed to grow table by `1`"
|
|
);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_limits_memory_only() -> Result<()> {
|
|
let engine = Engine::default();
|
|
let module = Module::new(
|
|
&engine,
|
|
r#"(module (memory (export "m") 0) (table (export "t") 0 anyfunc))"#,
|
|
)?;
|
|
|
|
let mut store = Store::new(
|
|
&engine,
|
|
StoreLimitsBuilder::new()
|
|
.memory_size(10 * WASM_PAGE_SIZE)
|
|
.build(),
|
|
);
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
|
|
let instance = Instance::new(&mut store, &module, &[])?;
|
|
|
|
// Test instance exports and host objects hitting the limit
|
|
for memory in std::array::IntoIter::new([
|
|
instance.get_memory(&mut store, "m").unwrap(),
|
|
Memory::new(&mut store, MemoryType::new(0, None))?,
|
|
]) {
|
|
memory.grow(&mut store, 3)?;
|
|
memory.grow(&mut store, 5)?;
|
|
memory.grow(&mut store, 2)?;
|
|
|
|
assert_eq!(
|
|
memory
|
|
.grow(&mut store, 1)
|
|
.map_err(|e| e.to_string())
|
|
.unwrap_err(),
|
|
"failed to grow memory by `1`"
|
|
);
|
|
}
|
|
|
|
// Test instance exports and host objects *not* hitting the limit
|
|
for table in std::array::IntoIter::new([
|
|
instance.get_table(&mut store, "t").unwrap(),
|
|
Table::new(
|
|
&mut store,
|
|
TableType::new(ValType::FuncRef, 0, None),
|
|
Val::FuncRef(None),
|
|
)?,
|
|
]) {
|
|
table.grow(&mut store, 2, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 1, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 2, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 1, Val::FuncRef(None))?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_initial_memory_limits_exceeded() -> Result<()> {
|
|
let engine = Engine::default();
|
|
let module = Module::new(&engine, r#"(module (memory (export "m") 11))"#)?;
|
|
|
|
let mut store = Store::new(
|
|
&engine,
|
|
StoreLimitsBuilder::new()
|
|
.memory_size(10 * WASM_PAGE_SIZE)
|
|
.build(),
|
|
);
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
|
|
match Instance::new(&mut store, &module, &[]) {
|
|
Ok(_) => unreachable!(),
|
|
Err(e) => assert_eq!(
|
|
e.to_string(),
|
|
"Insufficient resources: memory minimum size of 11 pages exceeds memory limits"
|
|
),
|
|
}
|
|
|
|
match Memory::new(&mut store, MemoryType::new(25, None)) {
|
|
Ok(_) => unreachable!(),
|
|
Err(e) => assert_eq!(
|
|
e.to_string(),
|
|
"Insufficient resources: memory minimum size of 25 pages exceeds memory limits"
|
|
),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_limits_table_only() -> Result<()> {
|
|
let engine = Engine::default();
|
|
let module = Module::new(
|
|
&engine,
|
|
r#"(module (memory (export "m") 0) (table (export "t") 0 anyfunc))"#,
|
|
)?;
|
|
|
|
let mut store = Store::new(&engine, StoreLimitsBuilder::new().table_elements(5).build());
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
|
|
let instance = Instance::new(&mut store, &module, &[])?;
|
|
|
|
// Test instance exports and host objects *not* hitting the limit
|
|
for memory in std::array::IntoIter::new([
|
|
instance.get_memory(&mut store, "m").unwrap(),
|
|
Memory::new(&mut store, MemoryType::new(0, None))?,
|
|
]) {
|
|
memory.grow(&mut store, 3)?;
|
|
memory.grow(&mut store, 5)?;
|
|
memory.grow(&mut store, 2)?;
|
|
memory.grow(&mut store, 1)?;
|
|
}
|
|
|
|
// Test instance exports and host objects hitting the limit
|
|
for table in std::array::IntoIter::new([
|
|
instance.get_table(&mut store, "t").unwrap(),
|
|
Table::new(
|
|
&mut store,
|
|
TableType::new(ValType::FuncRef, 0, None),
|
|
Val::FuncRef(None),
|
|
)?,
|
|
]) {
|
|
table.grow(&mut store, 2, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 1, Val::FuncRef(None))?;
|
|
table.grow(&mut store, 2, Val::FuncRef(None))?;
|
|
|
|
assert_eq!(
|
|
table
|
|
.grow(&mut store, 1, Val::FuncRef(None))
|
|
.map_err(|e| e.to_string())
|
|
.unwrap_err(),
|
|
"failed to grow table by `1`"
|
|
);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_initial_table_limits_exceeded() -> Result<()> {
|
|
let engine = Engine::default();
|
|
let module = Module::new(&engine, r#"(module (table (export "t") 23 anyfunc))"#)?;
|
|
|
|
let mut store = Store::new(&engine, StoreLimitsBuilder::new().table_elements(4).build());
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
|
|
match Instance::new(&mut store, &module, &[]) {
|
|
Ok(_) => unreachable!(),
|
|
Err(e) => assert_eq!(
|
|
e.to_string(),
|
|
"Insufficient resources: table minimum size of 23 elements exceeds table limits"
|
|
),
|
|
}
|
|
|
|
match Table::new(
|
|
&mut store,
|
|
TableType::new(ValType::FuncRef, 99, None),
|
|
Val::FuncRef(None),
|
|
) {
|
|
Ok(_) => unreachable!(),
|
|
Err(e) => assert_eq!(
|
|
e.to_string(),
|
|
"Insufficient resources: table minimum size of 99 elements exceeds table limits"
|
|
),
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_pooling_allocator_initial_limits_exceeded() -> Result<()> {
|
|
let mut config = Config::new();
|
|
config.wasm_multi_memory(true);
|
|
config.allocation_strategy(InstanceAllocationStrategy::Pooling {
|
|
strategy: PoolingAllocationStrategy::NextAvailable,
|
|
module_limits: ModuleLimits {
|
|
memories: 2,
|
|
..Default::default()
|
|
},
|
|
instance_limits: InstanceLimits {
|
|
count: 1,
|
|
..Default::default()
|
|
},
|
|
});
|
|
|
|
let engine = Engine::new(&config)?;
|
|
let module = Module::new(
|
|
&engine,
|
|
r#"(module (memory (export "m1") 2) (memory (export "m2") 5))"#,
|
|
)?;
|
|
|
|
let mut store = Store::new(
|
|
&engine,
|
|
StoreLimitsBuilder::new()
|
|
.memory_size(3 * WASM_PAGE_SIZE)
|
|
.build(),
|
|
);
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
|
|
match Instance::new(&mut store, &module, &[]) {
|
|
Ok(_) => unreachable!(),
|
|
Err(e) => assert_eq!(
|
|
e.to_string(),
|
|
"Insufficient resources: memory minimum size of 5 pages exceeds memory limits"
|
|
),
|
|
}
|
|
|
|
// An instance should still be able to be created after the failure above
|
|
let module = Module::new(&engine, r#"(module (memory (export "m") 2))"#)?;
|
|
|
|
Instance::new(&mut store, &module, &[])?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
struct MemoryContext {
|
|
host_memory_used: usize,
|
|
wasm_memory_used: usize,
|
|
memory_limit: usize,
|
|
limit_exceeded: bool,
|
|
}
|
|
|
|
impl ResourceLimiter for MemoryContext {
|
|
fn memory_growing(&mut self, current: usize, desired: usize, maximum: Option<usize>) -> bool {
|
|
// Check if the desired exceeds a maximum (either from Wasm or from the host)
|
|
assert!(desired < maximum.unwrap_or(usize::MAX));
|
|
|
|
assert_eq!(current as usize, self.wasm_memory_used);
|
|
let desired = desired as usize;
|
|
|
|
if desired + self.host_memory_used > self.memory_limit {
|
|
self.limit_exceeded = true;
|
|
return false;
|
|
}
|
|
|
|
self.wasm_memory_used = desired;
|
|
true
|
|
}
|
|
|
|
fn table_growing(&mut self, _current: u32, _desired: u32, _maximum: Option<u32>) -> bool {
|
|
true
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_custom_limiter() -> Result<()> {
|
|
let engine = Engine::default();
|
|
let mut linker = Linker::new(&engine);
|
|
|
|
// This approximates a function that would "allocate" resources that the host tracks.
|
|
// Here this is a simple function that increments the current host memory "used".
|
|
linker.func_wrap(
|
|
"",
|
|
"alloc",
|
|
|mut caller: Caller<'_, MemoryContext>, size: u32| -> u32 {
|
|
let mut ctx = caller.data_mut();
|
|
let size = size as usize;
|
|
|
|
if size + ctx.host_memory_used + ctx.wasm_memory_used <= ctx.memory_limit {
|
|
ctx.host_memory_used += size;
|
|
return 1;
|
|
}
|
|
|
|
ctx.limit_exceeded = true;
|
|
|
|
0
|
|
},
|
|
)?;
|
|
|
|
let module = Module::new(
|
|
&engine,
|
|
r#"(module (import "" "alloc" (func $alloc (param i32) (result i32))) (memory (export "m") 0) (func (export "f") (param i32) (result i32) local.get 0 call $alloc))"#,
|
|
)?;
|
|
|
|
let context = MemoryContext {
|
|
host_memory_used: 0,
|
|
wasm_memory_used: 0,
|
|
memory_limit: 1 << 20, // 16 wasm pages is the limit for both wasm + host memory
|
|
limit_exceeded: false,
|
|
};
|
|
|
|
let mut store = Store::new(&engine, context);
|
|
store.limiter(|s| s as &mut dyn ResourceLimiter);
|
|
let instance = linker.instantiate(&mut store, &module)?;
|
|
let memory = instance.get_memory(&mut store, "m").unwrap();
|
|
|
|
// Grow the memory by 640 KiB
|
|
memory.grow(&mut store, 3)?;
|
|
memory.grow(&mut store, 5)?;
|
|
memory.grow(&mut store, 2)?;
|
|
|
|
assert!(!store.data().limit_exceeded);
|
|
|
|
// Grow the host "memory" by 384 KiB
|
|
let f = instance.get_typed_func::<u32, u32, _>(&mut store, "f")?;
|
|
|
|
assert_eq!(f.call(&mut store, 1 * 0x10000)?, 1);
|
|
assert_eq!(f.call(&mut store, 3 * 0x10000)?, 1);
|
|
assert_eq!(f.call(&mut store, 2 * 0x10000)?, 1);
|
|
|
|
// Memory is at the maximum, but the limit hasn't been exceeded
|
|
assert!(!store.data().limit_exceeded);
|
|
|
|
// Try to grow the memory again
|
|
assert_eq!(
|
|
memory
|
|
.grow(&mut store, 1)
|
|
.map_err(|e| e.to_string())
|
|
.unwrap_err(),
|
|
"failed to grow memory by `1`"
|
|
);
|
|
|
|
assert!(store.data().limit_exceeded);
|
|
|
|
// Try to grow the host "memory" again
|
|
assert_eq!(f.call(&mut store, 1)?, 0);
|
|
|
|
assert!(store.data().limit_exceeded);
|
|
|
|
drop(store);
|
|
|
|
Ok(())
|
|
}
|