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:
Alex Crichton
2021-08-12 09:40:20 -05:00
committed by GitHub
parent 76a93dc112
commit e68aa99588
60 changed files with 1356 additions and 635 deletions

View File

@@ -2,6 +2,7 @@ use crate::{
handle_result, wasm_extern_t, wasm_memorytype_t, wasm_store_t, wasmtime_error_t, CStoreContext,
CStoreContextMut,
};
use std::convert::TryFrom;
use wasmtime::{Extern, Memory};
#[derive(Clone)]
@@ -72,7 +73,7 @@ pub unsafe extern "C" fn wasm_memory_data_size(m: &wasm_memory_t) -> usize {
#[no_mangle]
pub unsafe extern "C" fn wasm_memory_size(m: &wasm_memory_t) -> wasm_memory_pages_t {
m.memory().size(m.ext.store.context())
u32::try_from(m.memory().size(m.ext.store.context())).unwrap()
}
#[no_mangle]
@@ -82,7 +83,7 @@ pub unsafe extern "C" fn wasm_memory_grow(
) -> bool {
let memory = m.memory();
let mut store = m.ext.store.context_mut();
memory.grow(&mut store, delta).is_ok()
memory.grow(&mut store, u64::from(delta)).is_ok()
}
#[no_mangle]
@@ -113,7 +114,7 @@ pub extern "C" fn wasmtime_memory_data_size(store: CStoreContext<'_>, mem: &Memo
}
#[no_mangle]
pub extern "C" fn wasmtime_memory_size(store: CStoreContext<'_>, mem: &Memory) -> u32 {
pub extern "C" fn wasmtime_memory_size(store: CStoreContext<'_>, mem: &Memory) -> u64 {
mem.size(store)
}
@@ -121,8 +122,8 @@ pub extern "C" fn wasmtime_memory_size(store: CStoreContext<'_>, mem: &Memory) -
pub extern "C" fn wasmtime_memory_grow(
store: CStoreContextMut<'_>,
mem: &Memory,
delta: u32,
prev_size: &mut u32,
delta: u64,
prev_size: &mut u64,
) -> Option<Box<wasmtime_error_t>> {
handle_result(mem.grow(store, delta), |prev| *prev_size = prev)
}

View File

@@ -1,5 +1,3 @@
use wasmtime::Limits;
#[repr(C)]
#[derive(Clone)]
pub struct wasm_limits_t {
@@ -8,13 +6,12 @@ pub struct wasm_limits_t {
}
impl wasm_limits_t {
pub(crate) fn to_wasmtime(&self) -> Limits {
let max = if self.max == u32::max_value() {
pub(crate) fn max(&self) -> Option<u32> {
if self.max == u32::max_value() {
None
} else {
Some(self.max)
};
Limits::new(self.min, max)
}
}
}

View File

@@ -1,5 +1,6 @@
use crate::{wasm_externtype_t, wasm_limits_t, CExternType};
use once_cell::unsync::OnceCell;
use std::convert::TryFrom;
use wasmtime::MemoryType;
#[repr(transparent)]
@@ -50,22 +51,63 @@ impl CMemoryType {
#[no_mangle]
pub extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm_memorytype_t> {
Box::new(wasm_memorytype_t::new(MemoryType::new(
limits.to_wasmtime(),
limits.min,
limits.max(),
)))
}
#[no_mangle]
pub extern "C" fn wasm_memorytype_limits(mt: &wasm_memorytype_t) -> &wasm_limits_t {
let mt = mt.ty();
mt.limits_cache.get_or_init(|| {
let limits = mt.ty.limits();
wasm_limits_t {
min: limits.min(),
max: limits.max().unwrap_or(u32::max_value()),
}
mt.limits_cache.get_or_init(|| wasm_limits_t {
min: u32::try_from(mt.ty.minimum()).unwrap(),
max: u32::try_from(mt.ty.maximum().unwrap_or(u64::from(u32::max_value()))).unwrap(),
})
}
#[no_mangle]
pub extern "C" fn wasmtime_memorytype_new(
minimum: u64,
maximum_specified: bool,
maximum: u64,
memory64: bool,
) -> Box<wasm_memorytype_t> {
let maximum = if maximum_specified {
Some(maximum)
} else {
None
};
Box::new(wasm_memorytype_t::new(if memory64 {
MemoryType::new64(minimum, maximum)
} else {
MemoryType::new(
u32::try_from(minimum).unwrap(),
maximum.map(|i| u32::try_from(i).unwrap()),
)
}))
}
#[no_mangle]
pub extern "C" fn wasmtime_memorytype_minimum(mt: &wasm_memorytype_t) -> u64 {
mt.ty().ty.minimum()
}
#[no_mangle]
pub extern "C" fn wasmtime_memorytype_maximum(mt: &wasm_memorytype_t, out: &mut u64) -> bool {
match mt.ty().ty.maximum() {
Some(max) => {
*out = max;
true
}
None => false,
}
}
#[no_mangle]
pub extern "C" fn wasmtime_memorytype_is64(mt: &wasm_memorytype_t) -> bool {
mt.ty().ty.is_64()
}
#[no_mangle]
pub extern "C" fn wasm_memorytype_as_externtype(ty: &wasm_memorytype_t) -> &wasm_externtype_t {
&ty.ext

View File

@@ -56,7 +56,8 @@ pub extern "C" fn wasm_tabletype_new(
) -> Box<wasm_tabletype_t> {
Box::new(wasm_tabletype_t::new(TableType::new(
ty.ty,
limits.to_wasmtime(),
limits.min,
limits.max(),
)))
}
@@ -71,12 +72,9 @@ pub extern "C" fn wasm_tabletype_element(tt: &wasm_tabletype_t) -> &wasm_valtype
#[no_mangle]
pub extern "C" fn wasm_tabletype_limits(tt: &wasm_tabletype_t) -> &wasm_limits_t {
let tt = tt.ty();
tt.limits_cache.get_or_init(|| {
let limits = tt.ty.limits();
wasm_limits_t {
min: limits.min(),
max: limits.max().unwrap_or(u32::max_value()),
}
tt.limits_cache.get_or_init(|| wasm_limits_t {
min: tt.ty.minimum(),
max: tt.ty.maximum().unwrap_or(u32::max_value()),
})
}