* 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
381 lines
14 KiB
Rust
381 lines
14 KiB
Rust
//! Helper functions and structures for the translation.
|
|
use crate::environ::{TargetEnvironment, WasmResult, WasmType};
|
|
use crate::wasm_unsupported;
|
|
use core::convert::TryInto;
|
|
use core::u32;
|
|
use cranelift_codegen::entity::entity_impl;
|
|
use cranelift_codegen::ir;
|
|
use cranelift_codegen::ir::immediates::V128Imm;
|
|
use cranelift_frontend::FunctionBuilder;
|
|
#[cfg(feature = "enable-serde")]
|
|
use serde::{Deserialize, Serialize};
|
|
use wasmparser::{FuncValidator, WasmFuncType, WasmModuleResources};
|
|
|
|
/// Index type of a function (imported or defined) inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct FuncIndex(u32);
|
|
entity_impl!(FuncIndex);
|
|
|
|
/// Index type of a defined function inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct DefinedFuncIndex(u32);
|
|
entity_impl!(DefinedFuncIndex);
|
|
|
|
/// Index type of a defined table inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
pub struct DefinedTableIndex(u32);
|
|
entity_impl!(DefinedTableIndex);
|
|
|
|
/// Index type of a defined memory inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
pub struct DefinedMemoryIndex(u32);
|
|
entity_impl!(DefinedMemoryIndex);
|
|
|
|
/// Index type of a defined global inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
pub struct DefinedGlobalIndex(u32);
|
|
entity_impl!(DefinedGlobalIndex);
|
|
|
|
/// Index type of a table (imported or defined) inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct TableIndex(u32);
|
|
entity_impl!(TableIndex);
|
|
|
|
/// Index type of a global variable (imported or defined) inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct GlobalIndex(u32);
|
|
entity_impl!(GlobalIndex);
|
|
|
|
/// Index type of a linear memory (imported or defined) inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct MemoryIndex(u32);
|
|
entity_impl!(MemoryIndex);
|
|
|
|
/// Index type of a signature (imported or defined) inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct SignatureIndex(u32);
|
|
entity_impl!(SignatureIndex);
|
|
|
|
/// Index type of a passive data segment inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct DataIndex(u32);
|
|
entity_impl!(DataIndex);
|
|
|
|
/// Index type of a passive element segment inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct ElemIndex(u32);
|
|
entity_impl!(ElemIndex);
|
|
|
|
/// Index type of a type inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct TypeIndex(u32);
|
|
entity_impl!(TypeIndex);
|
|
|
|
/// Index type of a module inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct ModuleIndex(u32);
|
|
entity_impl!(ModuleIndex);
|
|
|
|
/// Index type of an instance inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct InstanceIndex(u32);
|
|
entity_impl!(InstanceIndex);
|
|
|
|
/// Index type of an event inside the WebAssembly module.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct TagIndex(u32);
|
|
entity_impl!(TagIndex);
|
|
|
|
/// Specialized index for just module types.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct ModuleTypeIndex(u32);
|
|
entity_impl!(ModuleTypeIndex);
|
|
|
|
/// Specialized index for just instance types.
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct InstanceTypeIndex(u32);
|
|
entity_impl!(InstanceTypeIndex);
|
|
|
|
/// An index of an entity.
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub enum EntityIndex {
|
|
/// Function index.
|
|
Function(FuncIndex),
|
|
/// Table index.
|
|
Table(TableIndex),
|
|
/// Memory index.
|
|
Memory(MemoryIndex),
|
|
/// Global index.
|
|
Global(GlobalIndex),
|
|
/// Module index.
|
|
Module(ModuleIndex),
|
|
/// Instance index.
|
|
Instance(InstanceIndex),
|
|
}
|
|
|
|
/// A type of an item in a wasm module where an item is typically something that
|
|
/// can be exported.
|
|
#[allow(missing_docs)]
|
|
#[derive(Clone, Debug)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub enum EntityType {
|
|
/// A global variable with the specified content type
|
|
Global(Global),
|
|
/// A linear memory with the specified limits
|
|
Memory(Memory),
|
|
/// An event definition.
|
|
Tag(Tag),
|
|
/// A table with the specified element type and limits
|
|
Table(Table),
|
|
/// A function type where the index points to the type section and records a
|
|
/// function signature.
|
|
Function(SignatureIndex),
|
|
/// An instance where the index points to the type section and records a
|
|
/// instance's exports.
|
|
Instance(InstanceTypeIndex),
|
|
/// A module where the index points to the type section and records a
|
|
/// module's imports and exports.
|
|
Module(ModuleTypeIndex),
|
|
}
|
|
|
|
/// A WebAssembly global.
|
|
///
|
|
/// Note that we record both the original Wasm type and the Cranelift IR type
|
|
/// used to represent it. This is because multiple different kinds of Wasm types
|
|
/// might be represented with the same Cranelift IR type. For example, both a
|
|
/// Wasm `i64` and a `funcref` might be represented with a Cranelift `i64` on
|
|
/// 64-bit architectures, and when GC is not required for func refs.
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct Global {
|
|
/// The Wasm type of the value stored in the global.
|
|
pub wasm_ty: crate::WasmType,
|
|
/// The Cranelift IR type of the value stored in the global.
|
|
pub ty: ir::Type,
|
|
/// A flag indicating whether the value may change at runtime.
|
|
pub mutability: bool,
|
|
/// The source of the initial value.
|
|
pub initializer: GlobalInit,
|
|
}
|
|
|
|
/// Globals are initialized via the `const` operators or by referring to another import.
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub enum GlobalInit {
|
|
/// An `i32.const`.
|
|
I32Const(i32),
|
|
/// An `i64.const`.
|
|
I64Const(i64),
|
|
/// An `f32.const`.
|
|
F32Const(u32),
|
|
/// An `f64.const`.
|
|
F64Const(u64),
|
|
/// A `vconst`.
|
|
V128Const(V128Imm),
|
|
/// A `global.get` of another global.
|
|
GetGlobal(GlobalIndex),
|
|
/// A `ref.null`.
|
|
RefNullConst,
|
|
/// A `ref.func <index>`.
|
|
RefFunc(FuncIndex),
|
|
///< The global is imported from, and thus initialized by, a different module.
|
|
Import,
|
|
}
|
|
|
|
/// WebAssembly table.
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct Table {
|
|
/// The table elements' Wasm type.
|
|
pub wasm_ty: WasmType,
|
|
/// The table elements' Cranelift type.
|
|
pub ty: TableElementType,
|
|
/// The minimum number of elements in the table.
|
|
pub minimum: u32,
|
|
/// The maximum number of elements in the table.
|
|
pub maximum: Option<u32>,
|
|
}
|
|
|
|
/// WebAssembly table element. Can be a function or a scalar type.
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub enum TableElementType {
|
|
/// A scalar type.
|
|
Val(ir::Type),
|
|
/// A function.
|
|
Func,
|
|
}
|
|
|
|
/// WebAssembly linear memory.
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct Memory {
|
|
/// The minimum number of pages in the memory.
|
|
pub minimum: u64,
|
|
/// The maximum number of pages in the memory.
|
|
pub maximum: Option<u64>,
|
|
/// Whether the memory may be shared between multiple threads.
|
|
pub shared: bool,
|
|
/// Whether or not this is a 64-bit memory
|
|
pub memory64: bool,
|
|
}
|
|
|
|
/// WebAssembly event.
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
|
|
pub struct Tag {
|
|
/// The event signature type.
|
|
pub ty: TypeIndex,
|
|
}
|
|
|
|
/// Helper function translating wasmparser types to Cranelift types when possible.
|
|
pub fn type_to_type<PE: TargetEnvironment + ?Sized>(
|
|
ty: wasmparser::Type,
|
|
environ: &PE,
|
|
) -> WasmResult<ir::Type> {
|
|
match ty {
|
|
wasmparser::Type::I32 => Ok(ir::types::I32),
|
|
wasmparser::Type::I64 => Ok(ir::types::I64),
|
|
wasmparser::Type::F32 => Ok(ir::types::F32),
|
|
wasmparser::Type::F64 => Ok(ir::types::F64),
|
|
wasmparser::Type::V128 => Ok(ir::types::I8X16),
|
|
wasmparser::Type::ExternRef | wasmparser::Type::FuncRef => {
|
|
Ok(environ.reference_type(ty.try_into()?))
|
|
}
|
|
ty => Err(wasm_unsupported!("type_to_type: wasm type {:?}", ty)),
|
|
}
|
|
}
|
|
|
|
/// Helper function translating wasmparser possible table types to Cranelift types when possible,
|
|
/// or None for Func tables.
|
|
pub fn tabletype_to_type<PE: TargetEnvironment + ?Sized>(
|
|
ty: wasmparser::Type,
|
|
environ: &PE,
|
|
) -> WasmResult<Option<ir::Type>> {
|
|
match ty {
|
|
wasmparser::Type::I32 => Ok(Some(ir::types::I32)),
|
|
wasmparser::Type::I64 => Ok(Some(ir::types::I64)),
|
|
wasmparser::Type::F32 => Ok(Some(ir::types::F32)),
|
|
wasmparser::Type::F64 => Ok(Some(ir::types::F64)),
|
|
wasmparser::Type::V128 => Ok(Some(ir::types::I8X16)),
|
|
wasmparser::Type::ExternRef => Ok(Some(environ.reference_type(ty.try_into()?))),
|
|
wasmparser::Type::FuncRef => Ok(None),
|
|
ty => Err(wasm_unsupported!(
|
|
"tabletype_to_type: table wasm type {:?}",
|
|
ty
|
|
)),
|
|
}
|
|
}
|
|
|
|
/// Get the parameter and result types for the given Wasm blocktype.
|
|
pub fn blocktype_params_results<'a, T>(
|
|
validator: &'a FuncValidator<T>,
|
|
ty_or_ft: wasmparser::TypeOrFuncType,
|
|
) -> WasmResult<(
|
|
impl ExactSizeIterator<Item = wasmparser::Type> + Clone + 'a,
|
|
impl ExactSizeIterator<Item = wasmparser::Type> + Clone + 'a,
|
|
)>
|
|
where
|
|
T: WasmModuleResources,
|
|
{
|
|
return Ok(match ty_or_ft {
|
|
wasmparser::TypeOrFuncType::Type(ty) => {
|
|
let (params, results): (&'static [wasmparser::Type], &'static [wasmparser::Type]) =
|
|
match ty {
|
|
wasmparser::Type::I32 => (&[], &[wasmparser::Type::I32]),
|
|
wasmparser::Type::I64 => (&[], &[wasmparser::Type::I64]),
|
|
wasmparser::Type::F32 => (&[], &[wasmparser::Type::F32]),
|
|
wasmparser::Type::F64 => (&[], &[wasmparser::Type::F64]),
|
|
wasmparser::Type::V128 => (&[], &[wasmparser::Type::V128]),
|
|
wasmparser::Type::ExternRef => (&[], &[wasmparser::Type::ExternRef]),
|
|
wasmparser::Type::FuncRef => (&[], &[wasmparser::Type::FuncRef]),
|
|
wasmparser::Type::EmptyBlockType => (&[], &[]),
|
|
ty => return Err(wasm_unsupported!("blocktype_params_results: type {:?}", ty)),
|
|
};
|
|
(
|
|
itertools::Either::Left(params.iter().copied()),
|
|
itertools::Either::Left(results.iter().copied()),
|
|
)
|
|
}
|
|
wasmparser::TypeOrFuncType::FuncType(ty_index) => {
|
|
let ty = validator
|
|
.resources()
|
|
.func_type_at(ty_index)
|
|
.expect("should be valid");
|
|
(
|
|
itertools::Either::Right(ty.inputs()),
|
|
itertools::Either::Right(ty.outputs()),
|
|
)
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Create a `Block` with the given Wasm parameters.
|
|
pub fn block_with_params<PE: TargetEnvironment + ?Sized>(
|
|
builder: &mut FunctionBuilder,
|
|
params: impl IntoIterator<Item = wasmparser::Type>,
|
|
environ: &PE,
|
|
) -> WasmResult<ir::Block> {
|
|
let block = builder.create_block();
|
|
for ty in params {
|
|
match ty {
|
|
wasmparser::Type::I32 => {
|
|
builder.append_block_param(block, ir::types::I32);
|
|
}
|
|
wasmparser::Type::I64 => {
|
|
builder.append_block_param(block, ir::types::I64);
|
|
}
|
|
wasmparser::Type::F32 => {
|
|
builder.append_block_param(block, ir::types::F32);
|
|
}
|
|
wasmparser::Type::F64 => {
|
|
builder.append_block_param(block, ir::types::F64);
|
|
}
|
|
wasmparser::Type::ExternRef | wasmparser::Type::FuncRef => {
|
|
builder.append_block_param(block, environ.reference_type(ty.try_into()?));
|
|
}
|
|
wasmparser::Type::V128 => {
|
|
builder.append_block_param(block, ir::types::I8X16);
|
|
}
|
|
ty => {
|
|
return Err(wasm_unsupported!(
|
|
"block_with_params: type {:?} in multi-value block's signature",
|
|
ty
|
|
))
|
|
}
|
|
}
|
|
}
|
|
Ok(block)
|
|
}
|
|
|
|
/// Turns a `wasmparser` `f32` into a `Cranelift` one.
|
|
pub fn f32_translation(x: wasmparser::Ieee32) -> ir::immediates::Ieee32 {
|
|
ir::immediates::Ieee32::with_bits(x.bits())
|
|
}
|
|
|
|
/// Turns a `wasmparser` `f64` into a `Cranelift` one.
|
|
pub fn f64_translation(x: wasmparser::Ieee64) -> ir::immediates::Ieee64 {
|
|
ir::immediates::Ieee64::with_bits(x.bits())
|
|
}
|
|
|
|
/// Special VMContext value label. It is tracked as 0xffff_fffe label.
|
|
pub fn get_vmctx_value_label() -> ir::ValueLabel {
|
|
const VMCTX_LABEL: u32 = 0xffff_fffe;
|
|
ir::ValueLabel::from_u32(VMCTX_LABEL)
|
|
}
|