Remove heaps from core Cranelift, push them into cranelift-wasm (#5386)

* cranelift-wasm: translate Wasm loads into lower-level CLIF operations

Rather than using `heap_{load,store,addr}`.

* cranelift: Remove the `heap_{addr,load,store}` instructions

These are now legalized in the `cranelift-wasm` frontend.

* cranelift: Remove the `ir::Heap` entity from CLIF

* Port basic memory operation tests to .wat filetests

* Remove test for verifying CLIF heaps

* Remove `heap_addr` from replace_branching_instructions_and_cfg_predecessors.clif test

* Remove `heap_addr` from readonly.clif test

* Remove `heap_addr` from `table_addr.clif` test

* Remove `heap_addr` from the simd-fvpromote_low.clif test

* Remove `heap_addr` from simd-fvdemote.clif test

* Remove `heap_addr` from the load-op-store.clif test

* Remove the CLIF heap runtest

* Remove `heap_addr` from the global_value.clif test

* Remove `heap_addr` from fpromote.clif runtests

* Remove `heap_addr` from fdemote.clif runtests

* Remove `heap_addr` from memory.clif parser test

* Remove `heap_addr` from reject_load_readonly.clif test

* Remove `heap_addr` from reject_load_notrap.clif test

* Remove `heap_addr` from load_readonly_notrap.clif test

* Remove `static-heap-without-guard-pages.clif` test

Will be subsumed when we port `make-heap-load-store-tests.sh` to generating
`.wat` tests.

* Remove `static-heap-with-guard-pages.clif` test

Will be subsumed when we port `make-heap-load-store-tests.sh` over to `.wat`
tests.

* Remove more heap tests

These will be subsumed by porting `make-heap-load-store-tests.sh` over to `.wat`
tests.

* Remove `heap_addr` from `simple-alias.clif` test

* Remove `heap_addr` from partial-redundancy.clif test

* Remove `heap_addr` from multiple-blocks.clif test

* Remove `heap_addr` from fence.clif test

* Remove `heap_addr` from extends.clif test

* Remove runtests that rely on heaps

Heaps are not a thing in CLIF or the interpreter anymore

* Add generated load/store `.wat` tests

* Enable memory-related wasm features in `.wat` tests

* Remove CLIF heap from fcmp-mem-bug.clif test

* Add a mode for compiling `.wat` all the way to assembly in filetests

* Also generate WAT to assembly tests in `make-load-store-tests.sh`

* cargo fmt

* Reinstate `f{de,pro}mote.clif` tests without the heap bits

* Remove undefined doc link

* Remove outdated SVG and dot file from docs

* Add docs about `None` returns for base address computation helpers

* Factor out `env.heap_access_spectre_mitigation()` to a local

* Expand docs for `FuncEnvironment::heaps` trait method

* Restore f{de,pro}mote+load clif runtests with stack memory
This commit is contained in:
Nick Fitzgerald
2022-12-14 16:26:45 -08:00
committed by GitHub
parent e03d65cca7
commit c0b587ac5f
198 changed files with 2494 additions and 4232 deletions

View File

@@ -11,12 +11,12 @@ use crate::step::{step, ControlFlow, StepError};
use crate::value::{Value, ValueError};
use cranelift_codegen::data_value::DataValue;
use cranelift_codegen::ir::{
ArgumentPurpose, Block, FuncRef, Function, GlobalValue, GlobalValueData, Heap, LibCall,
StackSlot, TrapCode, Type, Value as ValueRef,
ArgumentPurpose, Block, FuncRef, Function, GlobalValue, GlobalValueData, LibCall, StackSlot,
TrapCode, Type, Value as ValueRef,
};
use log::trace;
use smallvec::SmallVec;
use std::convert::{TryFrom, TryInto};
use std::convert::TryFrom;
use std::fmt::Debug;
use std::iter;
use thiserror::Error;
@@ -175,21 +175,6 @@ pub enum InterpreterError {
FuelExhausted,
}
pub type HeapBacking = Vec<u8>;
/// Represents a registered heap with an interpreter.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HeapId(u32);
/// Options for initializing a heap memory region
#[derive(Debug)]
pub enum HeapInit {
/// A zero initialized heap with `size` bytes
Zeroed(usize),
/// Initializes the heap with the backing memory unchanged.
FromBacking(HeapBacking),
}
pub type LibCallValues<V> = SmallVec<[V; 1]>;
pub type LibCallHandler<V> = fn(LibCall, LibCallValues<V>) -> Result<LibCallValues<V>, TrapCode>;
@@ -201,7 +186,6 @@ pub struct InterpreterState<'a> {
/// Number of bytes from the bottom of the stack where the current frame's stack space is
pub frame_offset: usize,
pub stack: Vec<u8>,
pub heaps: Vec<HeapBacking>,
pub pinned_reg: DataValue,
}
@@ -213,7 +197,6 @@ impl Default for InterpreterState<'_> {
frame_stack: vec![],
frame_offset: 0,
stack: Vec::with_capacity(1024),
heaps: Vec::new(),
pinned_reg: DataValue::U64(0),
}
}
@@ -230,57 +213,6 @@ impl<'a> InterpreterState<'a> {
self
}
/// Registers a static heap and returns a reference to it
///
/// This heap reference can be used to generate a heap pointer, which
/// can be used inside the interpreter to load / store values into the heap.
///
/// ```rust
/// # use cranelift_codegen::ir::types::I64;
/// # use cranelift_interpreter::interpreter::{InterpreterState, HeapInit};
/// let mut state = InterpreterState::default();
/// let heap0 = state.register_heap(HeapInit::Zeroed(1024));
///
/// let backing = Vec::from([10u8; 24]);
/// let heap1 = state.register_heap(HeapInit::FromBacking(backing));
/// ```
pub fn register_heap(&mut self, init: HeapInit) -> HeapId {
let heap_id = HeapId(self.heaps.len() as u32);
self.heaps.push(match init {
HeapInit::Zeroed(size) => iter::repeat(0).take(size).collect(),
HeapInit::FromBacking(backing) => backing,
});
heap_id
}
/// Returns a heap address that can be used inside the interpreter
///
/// ```rust
/// # use cranelift_codegen::ir::types::I64;
/// # use cranelift_interpreter::interpreter::{InterpreterState, HeapInit};
/// let mut state = InterpreterState::default();
/// let heap_id = state.register_heap(HeapInit::Zeroed(1024));
/// let heap_base = state.get_heap_address(I64, heap_id, 0);
/// let heap_bound = state.get_heap_address(I64, heap_id, 1024);
/// ```
pub fn get_heap_address(
&self,
ty: Type,
heap_id: HeapId,
offset: u64,
) -> Result<DataValue, MemoryError> {
let size = AddressSize::try_from(ty)?;
let heap_id = heap_id.0 as u64;
let addr = Address::from_parts(size, AddressRegion::Heap, heap_id, offset)?;
self.validate_address(&addr)?;
let dv = addr.try_into()?;
Ok(dv)
}
fn current_frame_mut(&mut self) -> &mut Frame<'a> {
let num_frames = self.frame_stack.len();
match num_frames {
@@ -371,33 +303,6 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {
Address::from_parts(size, AddressRegion::Stack, 0, final_offset)
}
/// Builds an [Address] for the [Heap] referenced in the currently executing function.
///
/// A CLIF Heap is essentially a GlobalValue and some metadata about that memory
/// region, such as bounds. Since heaps are based on Global Values it means that
/// once that GV is resolved we can essentially end up anywhere in memory.
///
/// To build an [Address] we perform GV resolution, and try to ensure that we end up
/// in a valid region of memory.
fn heap_address(
&self,
size: AddressSize,
heap: Heap,
offset: u64,
) -> Result<Address, MemoryError> {
let heap_data = &self.get_current_function().heaps[heap];
let heap_base = self.resolve_global_value(heap_data.base)?;
let mut addr = Address::try_from(heap_base)?;
addr.size = size;
addr.offset += offset;
// After resolving the address can point anywhere, we need to check if it's
// still valid.
self.validate_address(&addr)?;
Ok(addr)
}
fn checked_load(&self, addr: Address, ty: Type) -> Result<DataValue, MemoryError> {
let load_size = ty.bytes() as usize;
let addr_start = addr.offset as usize;
@@ -411,14 +316,6 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {
&self.stack[addr_start..addr_end]
}
AddressRegion::Heap => {
let heap_mem = match self.heaps.get(addr.entry as usize) {
Some(mem) if addr_end <= mem.len() => mem,
_ => return Err(MemoryError::OutOfBoundsLoad { addr, load_size }),
};
&heap_mem[addr_start..addr_end]
}
_ => unimplemented!(),
};
@@ -438,14 +335,6 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {
&mut self.stack[addr_start..addr_end]
}
AddressRegion::Heap => {
let heap_mem = match self.heaps.get_mut(addr.entry as usize) {
Some(mem) if addr_end <= mem.len() => mem,
_ => return Err(MemoryError::OutOfBoundsStore { addr, store_size }),
};
&mut heap_mem[addr_start..addr_end]
}
_ => unimplemented!(),
};
@@ -561,24 +450,7 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {
if addr.offset > stack_len {
return Err(MemoryError::InvalidEntry {
entry: addr.entry,
max: self.heaps.len() as u64,
});
}
}
AddressRegion::Heap => {
let heap_len = self
.heaps
.get(addr.entry as usize)
.ok_or_else(|| MemoryError::InvalidEntry {
entry: addr.entry,
max: self.heaps.len() as u64,
})
.map(|heap| heap.len() as u64)?;
if addr.offset > heap_len {
return Err(MemoryError::InvalidOffset {
offset: addr.offset,
max: heap_len,
max: self.stack.len() as u64,
});
}
}
@@ -602,7 +474,6 @@ mod tests {
use super::*;
use crate::step::CraneliftTrap;
use cranelift_codegen::ir::immediates::Ieee32;
use cranelift_codegen::ir::types::I64;
use cranelift_codegen::ir::TrapCode;
use cranelift_reader::parse_functions;
use smallvec::smallvec;
@@ -957,53 +828,6 @@ mod tests {
assert_eq!(trap, CraneliftTrap::User(TrapCode::HeapOutOfBounds));
}
/// Most heap tests are in .clif files using the filetest machinery. However, this is a sanity
/// check that the heap mechanism works without the rest of the filetest infrastructure
#[test]
fn heap_sanity_test() {
let code = "
function %heap_load_store(i64 vmctx) -> i8 {
gv0 = vmctx
gv1 = load.i64 notrap aligned gv0+0
; gv2/3 do nothing, but makes sure we understand the iadd_imm mechanism
gv2 = iadd_imm.i64 gv1, 1
gv3 = iadd_imm.i64 gv2, -1
heap0 = static gv3, min 0x1000, bound 0x1_0000_0000, offset_guard 0, index_type i64
block0(v0: i64):
v1 = iconst.i64 0
v2 = iconst.i64 123
v3 = heap_addr.i64 heap0, v1, 0, 8
store.i64 v2, v3
v4 = load.i64 v3
v5 = icmp eq v2, v4
return v5
}";
let func = parse_functions(code).unwrap().into_iter().next().unwrap();
let mut env = FunctionStore::default();
env.add(func.name.to_string(), &func);
let mut state = InterpreterState::default().with_function_store(env);
let heap0 = state.register_heap(HeapInit::Zeroed(0x1000));
let base_addr = state.get_heap_address(I64, heap0, 0).unwrap();
// Build a vmctx struct by writing the base pointer at index 0
let mut vmctx_struct = vec![0u8; 8];
base_addr.write_to_slice(&mut vmctx_struct[..]);
// This is our vmctx "heap"
let vmctx = state.register_heap(HeapInit::FromBacking(vmctx_struct));
let vmctx_addr = state.get_heap_address(I64, vmctx, 0).unwrap();
let result = Interpreter::new(state)
.call_by_name("%heap_load_store", &[vmctx_addr])
.unwrap()
.unwrap_return();
assert_eq!(result, vec![DataValue::I8(1)])
}
#[test]
fn srem_trap() {
let code = "function %test() -> i64 {