Skip to content

Commit 1624bc2

Browse files
committed
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
1 parent bb85366 commit 1624bc2

62 files changed

Lines changed: 1223 additions & 595 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn main() -> anyhow::Result<()> {
3434
test_directory_module(out, "tests/misc_testsuite/module-linking", strategy)?;
3535
test_directory_module(out, "tests/misc_testsuite/simd", strategy)?;
3636
test_directory_module(out, "tests/misc_testsuite/threads", strategy)?;
37+
test_directory_module(out, "tests/misc_testsuite/memory64", strategy)?;
3738
Ok(())
3839
})?;
3940

@@ -53,6 +54,7 @@ fn main() -> anyhow::Result<()> {
5354
"tests/spec_testsuite/proposals/bulk-memory-operations",
5455
strategy,
5556
)?;
57+
test_directory_module(out, "tests/spec_testsuite/proposals/memory64", strategy)?;
5658
} else {
5759
println!(
5860
"cargo:warning=The spec testsuite is disabled. To enable, run `git submodule \
@@ -157,7 +159,7 @@ fn write_testsuite_tests(
157159

158160
writeln!(out, "#[test]")?;
159161
// Ignore when using QEMU for running tests (limited memory).
160-
if ignore(testsuite, &testname, strategy) || (pooling && platform_is_emulated()) {
162+
if ignore(testsuite, &testname, strategy) {
161163
writeln!(out, "#[ignore]")?;
162164
}
163165

@@ -213,7 +215,3 @@ fn ignore(testsuite: &str, testname: &str, strategy: &str) -> bool {
213215
fn platform_is_s390x() -> bool {
214216
env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "s390x"
215217
}
216-
217-
fn platform_is_emulated() -> bool {
218-
env::var("WASMTIME_TEST_NO_HOG_MEMORY").unwrap_or_default() == "1"
219-
}

cranelift/codegen/meta/src/shared/formats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl Formats {
273273
heap_addr: Builder::new("HeapAddr")
274274
.imm(&entities.heap)
275275
.value()
276-
.imm(&imm.uimm32)
276+
.imm(&imm.uimm64)
277277
.build(),
278278

279279
// Accessing a WebAssembly table.

cranelift/codegen/meta/src/shared/immediates.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub(crate) struct Immediates {
1414
/// counts on shift instructions.
1515
pub uimm8: OperandKind,
1616

17-
/// An unsigned 32-bit immediate integer operand.
18-
pub uimm32: OperandKind,
17+
/// An unsigned 64-bit immediate integer operand.
18+
pub uimm64: OperandKind,
1919

2020
/// An unsigned 128-bit immediate integer operand.
2121
///
@@ -97,8 +97,8 @@ impl Immediates {
9797
imm64: new_imm("imm", "ir::immediates::Imm64").with_doc("A 64-bit immediate integer."),
9898
uimm8: new_imm("imm", "ir::immediates::Uimm8")
9999
.with_doc("An 8-bit immediate unsigned integer."),
100-
uimm32: new_imm("imm", "ir::immediates::Uimm32")
101-
.with_doc("A 32-bit immediate unsigned integer."),
100+
uimm64: new_imm("imm", "ir::immediates::Uimm64")
101+
.with_doc("A 64-bit immediate unsigned integer."),
102102
uimm128: new_imm("imm", "ir::Immediate")
103103
.with_doc("A 128-bit immediate unsigned integer."),
104104
pool_constant: new_imm("constant_handle", "ir::Constant")

cranelift/codegen/meta/src/shared/instructions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ pub(crate) fn define(
15341534

15351535
let H = &Operand::new("H", &entities.heap);
15361536
let p = &Operand::new("p", HeapOffset);
1537-
let Size = &Operand::new("Size", &imm.uimm32).with_doc("Size in bytes");
1537+
let Size = &Operand::new("Size", &imm.uimm64).with_doc("Size in bytes");
15381538

15391539
ig.push(
15401540
Inst::new(

cranelift/codegen/src/ir/instructions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ impl InstructionData {
302302
// 32-bit
303303
&InstructionData::UnaryIeee32 { imm, .. } => Some(DataValue::from(imm)),
304304
&InstructionData::HeapAddr { imm, .. } => {
305-
let imm: u32 = imm.into();
306-
Some(DataValue::from(imm as i32)) // Note the switch from unsigned to signed.
305+
let imm: u64 = imm.into();
306+
Some(DataValue::from(imm as i64)) // Note the switch from unsigned to signed.
307307
}
308308
&InstructionData::Load { offset, .. }
309309
| &InstructionData::LoadComplex { offset, .. }

cranelift/codegen/src/legalizer/heap.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ fn dynamic_addr(
5353
inst: ir::Inst,
5454
heap: ir::Heap,
5555
offset: ir::Value,
56-
access_size: u32,
56+
access_size: u64,
5757
bound_gv: ir::GlobalValue,
5858
func: &mut ir::Function,
5959
) {
60-
let access_size = u64::from(access_size);
6160
let offset_ty = func.dfg.value_type(offset);
6261
let addr_ty = func.dfg.value_type(func.dfg.first_result(inst));
6362
let min_size = func.heaps[heap].min_size.into();
@@ -113,12 +112,11 @@ fn static_addr(
113112
inst: ir::Inst,
114113
heap: ir::Heap,
115114
mut offset: ir::Value,
116-
access_size: u32,
115+
access_size: u64,
117116
bound: u64,
118117
func: &mut ir::Function,
119118
cfg: &mut ControlFlowGraph,
120119
) {
121-
let access_size = u64::from(access_size);
122120
let offset_ty = func.dfg.value_type(offset);
123121
let addr_ty = func.dfg.value_type(func.dfg.first_result(inst));
124122
let mut pos = FuncCursor::new(func).at_inst(inst);

cranelift/reader/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,7 +3064,7 @@ impl<'a> Parser<'a> {
30643064
self.match_token(Token::Comma, "expected ',' between operands")?;
30653065
let arg = self.match_value("expected SSA value heap address")?;
30663066
self.match_token(Token::Comma, "expected ',' between operands")?;
3067-
let imm = self.match_uimm32("expected 32-bit integer size")?;
3067+
let imm = self.match_uimm64("expected 64-bit integer size")?;
30683068
InstructionData::HeapAddr {
30693069
opcode,
30703070
heap,

cranelift/wasm/src/code_translator.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,10 +2164,6 @@ fn prepare_addr<FE: FuncEnvironment + ?Sized>(
21642164
environ: &mut FE,
21652165
) -> WasmResult<(MemFlags, Value, Offset32)> {
21662166
let addr = state.pop1();
2167-
// This function will need updates for 64-bit memories
2168-
debug_assert_eq!(builder.func.dfg.value_type(addr), I32);
2169-
let offset = u32::try_from(memarg.offset).unwrap();
2170-
21712167
let heap = state.get_heap(builder.func, memarg.memory, environ)?;
21722168
let offset_guard_size: u64 = builder.func.heaps[heap].offset_guard_size.into();
21732169

@@ -2222,25 +2218,27 @@ fn prepare_addr<FE: FuncEnvironment + ?Sized>(
22222218
// offsets we're checking here are zero. This means that we'll hit the fast
22232219
// path and emit zero conditional traps for bounds checks
22242220
let adjusted_offset = if offset_guard_size == 0 {
2225-
u64::from(offset) + u64::from(access_size)
2221+
memarg.offset.saturating_add(u64::from(access_size))
22262222
} else {
22272223
assert!(access_size < 1024);
2228-
cmp::max(u64::from(offset) / offset_guard_size * offset_guard_size, 1)
2224+
cmp::max(memarg.offset / offset_guard_size * offset_guard_size, 1)
22292225
};
22302226
debug_assert!(adjusted_offset > 0); // want to bounds check at least 1 byte
2231-
let check_size = u32::try_from(adjusted_offset).unwrap_or(u32::MAX);
22322227
let base = builder
22332228
.ins()
2234-
.heap_addr(environ.pointer_type(), heap, addr, check_size);
2229+
.heap_addr(environ.pointer_type(), heap, addr, adjusted_offset);
22352230

22362231
// Native load/store instructions take a signed `Offset32` immediate, so adjust the base
22372232
// pointer if necessary.
2238-
let (addr, offset) = if offset > i32::MAX as u32 {
2239-
// Offset doesn't fit in the load/store instruction.
2240-
let adj = builder.ins().iadd_imm(base, i64::from(i32::MAX) + 1);
2241-
(adj, (offset - (i32::MAX as u32 + 1)) as i32)
2242-
} else {
2243-
(base, offset as i32)
2233+
let (addr, offset) = match i32::try_from(memarg.offset) {
2234+
Ok(val) => (base, val),
2235+
Err(_) => {
2236+
// Note the switch from u64 offset to i64 here, but this should be
2237+
// ok because we're already guaranteed this won't overflow if we
2238+
// reach this point after the `heap_addr` instruction above.
2239+
let adj = builder.ins().iadd_imm(base, memarg.offset as i64);
2240+
(adj, 0)
2241+
}
22442242
};
22452243

22462244
// Note that we don't set `is_aligned` here, even if the load instruction's

cranelift/wasm/src/environ/dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
792792
&mut self,
793793
_memory_index: MemoryIndex,
794794
_base: Option<GlobalIndex>,
795-
_offset: u32,
795+
_offset: u64,
796796
_data: &'data [u8],
797797
) -> WasmResult<()> {
798798
// We do nothing

cranelift/wasm/src/environ/spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ pub trait ModuleEnvironment<'data>: TargetEnvironment {
995995
&mut self,
996996
memory_index: MemoryIndex,
997997
base: Option<GlobalIndex>,
998-
offset: u32,
998+
offset: u64,
999999
data: &'data [u8],
10001000
) -> WasmResult<()>;
10011001

0 commit comments

Comments
 (0)