Implement the memory64 proposal in Wasmtime (#3153)
* 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
This commit is contained in:
@@ -45,18 +45,25 @@ pub const DEFAULT_MEMORY_LIMIT: usize = 10000;
|
||||
/// An instance can be created with a resource limiter so that hosts can take into account
|
||||
/// non-WebAssembly resource usage to determine if a linear memory or table should grow.
|
||||
pub trait ResourceLimiter {
|
||||
/// Notifies the resource limiter that an instance's linear memory has been requested to grow.
|
||||
/// Notifies the resource limiter that an instance's linear memory has been
|
||||
/// requested to grow.
|
||||
///
|
||||
/// * `current` is the current size of the linear memory in WebAssembly page units.
|
||||
/// * `desired` is the desired size of the linear memory in WebAssembly page units.
|
||||
/// * `maximum` is either the linear memory's maximum or a maximum from an instance allocator,
|
||||
/// also in WebAssembly page units. A value of `None` indicates that the linear memory is
|
||||
/// unbounded.
|
||||
/// * `current` is the current size of the linear memory in bytes.
|
||||
/// * `desired` is the desired size of the linear memory in bytes.
|
||||
/// * `maximum` is either the linear memory's maximum or a maximum from an
|
||||
/// instance allocator, also in bytes. A value of `None`
|
||||
/// indicates that the linear memory is unbounded.
|
||||
///
|
||||
/// This function should return `true` to indicate that the growing operation is permitted or
|
||||
/// `false` if not permitted. Returning `true` when a maximum has been exceeded will have no
|
||||
/// effect as the linear memory will not grow.
|
||||
fn memory_growing(&mut self, current: u32, desired: u32, maximum: Option<u32>) -> bool;
|
||||
/// This function should return `true` to indicate that the growing
|
||||
/// operation is permitted or `false` if not permitted. Returning `true`
|
||||
/// when a maximum has been exceeded will have no effect as the linear
|
||||
/// memory will not grow.
|
||||
///
|
||||
/// This function is not guaranteed to be invoked for all requests to
|
||||
/// `memory.grow`. Requests where the allocation requested size doesn't fit
|
||||
/// in `usize` or exceeds the memory's listed maximum size may not invoke
|
||||
/// this method.
|
||||
fn memory_growing(&mut self, current: usize, desired: usize, maximum: Option<usize>) -> bool;
|
||||
|
||||
/// Notifies the resource limiter that an instance's table has been requested to grow.
|
||||
///
|
||||
@@ -406,8 +413,9 @@ impl Instance {
|
||||
/// Grow memory by the specified amount of pages.
|
||||
///
|
||||
/// Returns `None` if memory can't be grown by the specified amount
|
||||
/// of pages.
|
||||
pub(crate) fn memory_grow(&mut self, index: MemoryIndex, delta: u32) -> Option<u32> {
|
||||
/// of pages. Returns `Some` with the old size in bytes if growth was
|
||||
/// successful.
|
||||
pub(crate) fn memory_grow(&mut self, index: MemoryIndex, delta: u64) -> Option<usize> {
|
||||
let (idx, instance) = if let Some(idx) = self.module.defined_memory_index(index) {
|
||||
(idx, self)
|
||||
} else {
|
||||
@@ -616,26 +624,18 @@ impl Instance {
|
||||
pub(crate) fn memory_copy(
|
||||
&mut self,
|
||||
dst_index: MemoryIndex,
|
||||
dst: u32,
|
||||
dst: u64,
|
||||
src_index: MemoryIndex,
|
||||
src: u32,
|
||||
len: u32,
|
||||
src: u64,
|
||||
len: u64,
|
||||
) -> Result<(), Trap> {
|
||||
// https://webassembly.github.io/reference-types/core/exec/instructions.html#exec-memory-copy
|
||||
|
||||
let src_mem = self.get_memory(src_index);
|
||||
let dst_mem = self.get_memory(dst_index);
|
||||
|
||||
if src.checked_add(len).map_or(true, |n| {
|
||||
usize::try_from(n).unwrap() > src_mem.current_length
|
||||
}) || dst.checked_add(len).map_or(true, |m| {
|
||||
usize::try_from(m).unwrap() > dst_mem.current_length
|
||||
}) {
|
||||
return Err(Trap::wasm(ir::TrapCode::HeapOutOfBounds));
|
||||
}
|
||||
|
||||
let dst = usize::try_from(dst).unwrap();
|
||||
let src = usize::try_from(src).unwrap();
|
||||
let src = self.validate_inbounds(src_mem.current_length, src, len)?;
|
||||
let dst = self.validate_inbounds(dst_mem.current_length, dst, len)?;
|
||||
|
||||
// Bounds and casts are checked above, by this point we know that
|
||||
// everything is safe.
|
||||
@@ -648,6 +648,19 @@ impl Instance {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_inbounds(&self, max: usize, ptr: u64, len: u64) -> Result<usize, Trap> {
|
||||
let oob = || Trap::wasm(ir::TrapCode::HeapOutOfBounds);
|
||||
let end = ptr
|
||||
.checked_add(len)
|
||||
.and_then(|i| usize::try_from(i).ok())
|
||||
.ok_or_else(oob)?;
|
||||
if end > max {
|
||||
Err(oob())
|
||||
} else {
|
||||
Ok(ptr as usize)
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform the `memory.fill` operation on a locally defined memory.
|
||||
///
|
||||
/// # Errors
|
||||
@@ -656,25 +669,17 @@ impl Instance {
|
||||
pub(crate) fn memory_fill(
|
||||
&mut self,
|
||||
memory_index: MemoryIndex,
|
||||
dst: u32,
|
||||
val: u32,
|
||||
len: u32,
|
||||
dst: u64,
|
||||
val: u8,
|
||||
len: u64,
|
||||
) -> Result<(), Trap> {
|
||||
let memory = self.get_memory(memory_index);
|
||||
|
||||
if dst.checked_add(len).map_or(true, |m| {
|
||||
usize::try_from(m).unwrap() > memory.current_length
|
||||
}) {
|
||||
return Err(Trap::wasm(ir::TrapCode::HeapOutOfBounds));
|
||||
}
|
||||
|
||||
let dst = isize::try_from(dst).unwrap();
|
||||
let val = val as u8;
|
||||
let dst = self.validate_inbounds(memory.current_length, dst, len)?;
|
||||
|
||||
// Bounds and casts are checked above, by this point we know that
|
||||
// everything is safe.
|
||||
unsafe {
|
||||
let dst = memory.base.offset(dst);
|
||||
let dst = memory.base.add(dst);
|
||||
ptr::write_bytes(dst, val, len as usize);
|
||||
}
|
||||
|
||||
@@ -692,7 +697,7 @@ impl Instance {
|
||||
&mut self,
|
||||
memory_index: MemoryIndex,
|
||||
data_index: DataIndex,
|
||||
dst: u32,
|
||||
dst: u64,
|
||||
src: u32,
|
||||
len: u32,
|
||||
) -> Result<(), Trap> {
|
||||
@@ -713,29 +718,22 @@ impl Instance {
|
||||
&mut self,
|
||||
memory_index: MemoryIndex,
|
||||
data: &[u8],
|
||||
dst: u32,
|
||||
dst: u64,
|
||||
src: u32,
|
||||
len: u32,
|
||||
) -> Result<(), Trap> {
|
||||
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-memory-init
|
||||
|
||||
let memory = self.get_memory(memory_index);
|
||||
let dst = self.validate_inbounds(memory.current_length, dst, len.into())?;
|
||||
let src = self.validate_inbounds(data.len(), src.into(), len.into())?;
|
||||
let len = len as usize;
|
||||
|
||||
if src
|
||||
.checked_add(len)
|
||||
.map_or(true, |n| usize::try_from(n).unwrap() > data.len())
|
||||
|| dst.checked_add(len).map_or(true, |m| {
|
||||
usize::try_from(m).unwrap() > memory.current_length
|
||||
})
|
||||
{
|
||||
return Err(Trap::wasm(ir::TrapCode::HeapOutOfBounds));
|
||||
}
|
||||
|
||||
let src_slice = &data[src as usize..(src + len) as usize];
|
||||
let src_slice = &data[src..(src + len)];
|
||||
|
||||
unsafe {
|
||||
let dst_start = memory.base.add(dst as usize);
|
||||
let dst_slice = slice::from_raw_parts_mut(dst_start, len as usize);
|
||||
let dst_start = memory.base.add(dst);
|
||||
let dst_slice = slice::from_raw_parts_mut(dst_start, len);
|
||||
dst_slice.copy_from_slice(src_slice);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user