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:
@@ -34,7 +34,6 @@ pub mod function_runner;
|
||||
mod match_directive;
|
||||
mod runner;
|
||||
mod runone;
|
||||
mod runtest_environment;
|
||||
mod subtest;
|
||||
|
||||
mod test_alias_analysis;
|
||||
|
||||
@@ -1,60 +1,18 @@
|
||||
use anyhow::anyhow;
|
||||
use cranelift_codegen::ir::{ArgumentPurpose, Function};
|
||||
use cranelift_reader::parse_heap_command;
|
||||
use cranelift_reader::{Comment, HeapCommand};
|
||||
use cranelift_reader::Comment;
|
||||
|
||||
/// Stores info about the expected environment for a test function.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RuntestEnvironment {
|
||||
pub heaps: Vec<HeapCommand>,
|
||||
}
|
||||
pub struct RuntestEnvironment {}
|
||||
|
||||
impl RuntestEnvironment {
|
||||
/// Parse the environment from a set of comments
|
||||
pub fn parse(comments: &[Comment]) -> anyhow::Result<Self> {
|
||||
let mut env = RuntestEnvironment { heaps: Vec::new() };
|
||||
|
||||
for comment in comments.iter() {
|
||||
if let Some(heap_command) = parse_heap_command(comment.text)? {
|
||||
let heap_index = env.heaps.len() as u64;
|
||||
let expected_ptr = heap_index * 16;
|
||||
if Some(expected_ptr) != heap_command.ptr_offset.map(|p| p.into()) {
|
||||
return Err(anyhow!(
|
||||
"Invalid ptr offset, expected vmctx+{}",
|
||||
expected_ptr
|
||||
));
|
||||
}
|
||||
|
||||
let expected_bound = (heap_index * 16) + 8;
|
||||
if Some(expected_bound) != heap_command.bound_offset.map(|p| p.into()) {
|
||||
return Err(anyhow!(
|
||||
"Invalid bound offset, expected vmctx+{}",
|
||||
expected_bound
|
||||
));
|
||||
}
|
||||
|
||||
env.heaps.push(heap_command);
|
||||
};
|
||||
}
|
||||
|
||||
let mut env = RuntestEnvironment {};
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
pub fn is_active(&self) -> bool {
|
||||
!self.heaps.is_empty()
|
||||
}
|
||||
|
||||
/// Allocates memory for heaps
|
||||
pub fn allocate_memory(&self) -> Vec<HeapMemory> {
|
||||
self.heaps
|
||||
.iter()
|
||||
.map(|cmd| {
|
||||
let size: u64 = cmd.size.into();
|
||||
vec![0u8; size as usize]
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Validates the signature of a [Function] ensuring that if this environment is active, the
|
||||
/// function has a `vmctx` argument
|
||||
pub fn validate_signature(&self, func: &Function) -> Result<(), String> {
|
||||
@@ -76,5 +34,3 @@ impl RuntestEnvironment {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) type HeapMemory = Vec<u8>;
|
||||
|
||||
@@ -4,17 +4,14 @@
|
||||
//! using [RunCommand](cranelift_reader::RunCommand)s.
|
||||
|
||||
use crate::runone::FileUpdate;
|
||||
use crate::runtest_environment::RuntestEnvironment;
|
||||
use crate::subtest::SubTest;
|
||||
use anyhow::{anyhow, Context};
|
||||
use cranelift_codegen::data_value::DataValue;
|
||||
use cranelift_codegen::ir::types::I64;
|
||||
use anyhow::Context;
|
||||
use cranelift_codegen::ir::Function;
|
||||
use cranelift_codegen::isa::TargetIsa;
|
||||
use cranelift_codegen::settings::Flags;
|
||||
use cranelift_codegen::{self, ir};
|
||||
use cranelift_interpreter::environment::FunctionStore;
|
||||
use cranelift_interpreter::interpreter::{HeapInit, Interpreter, InterpreterState};
|
||||
use cranelift_interpreter::interpreter::{Interpreter, InterpreterState};
|
||||
use cranelift_interpreter::step::ControlFlow;
|
||||
use cranelift_reader::{parse_run_command, Details, TestCommand, TestFile};
|
||||
use log::{info, trace};
|
||||
@@ -62,10 +59,7 @@ impl SubTest for TestInterpret {
|
||||
for (func, details) in &testfile.functions {
|
||||
info!("Test: {}({}) interpreter", self.name(), func.name);
|
||||
|
||||
let test_env = RuntestEnvironment::parse(&details.comments[..])?;
|
||||
test_env.validate_signature(&func).map_err(|s| anyhow!(s))?;
|
||||
|
||||
run_test(&test_env, &func_store, func, details).context(self.name())?;
|
||||
run_test(&func_store, func, details).context(self.name())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -80,12 +74,7 @@ impl SubTest for TestInterpret {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_test(
|
||||
test_env: &RuntestEnvironment,
|
||||
func_store: &FunctionStore,
|
||||
func: &Function,
|
||||
details: &Details,
|
||||
) -> anyhow::Result<()> {
|
||||
fn run_test(func_store: &FunctionStore, func: &Function, details: &Details) -> anyhow::Result<()> {
|
||||
for comment in details.comments.iter() {
|
||||
if let Some(command) = parse_run_command(comment.text, &func.signature)? {
|
||||
trace!("Parsed run command: {}", command);
|
||||
@@ -94,14 +83,9 @@ fn run_test(
|
||||
.run(|func_name, run_args| {
|
||||
// Rebuild the interpreter state on every run to ensure that we don't accidentally depend on
|
||||
// some leftover state
|
||||
let mut state =
|
||||
InterpreterState::default().with_function_store(func_store.clone());
|
||||
let state = InterpreterState::default().with_function_store(func_store.clone());
|
||||
|
||||
let mut args = Vec::with_capacity(run_args.len());
|
||||
if test_env.is_active() {
|
||||
let vmctx_addr = register_heaps(&mut state, test_env);
|
||||
args.push(vmctx_addr);
|
||||
}
|
||||
args.extend_from_slice(run_args);
|
||||
|
||||
// Because we have stored function names with a leading %, we need to re-add it.
|
||||
@@ -119,34 +103,3 @@ fn run_test(
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build a VMContext struct with the layout described in docs/testing.md.
|
||||
pub fn register_heaps<'a>(
|
||||
state: &mut InterpreterState<'a>,
|
||||
test_env: &RuntestEnvironment,
|
||||
) -> DataValue {
|
||||
let mem = test_env.allocate_memory();
|
||||
let vmctx_struct = mem
|
||||
.into_iter()
|
||||
// This memory layout (a contiguous list of base + bound ptrs)
|
||||
// is enforced by the RuntestEnvironment when parsing the heap
|
||||
// directives. So we are safe to replicate that here.
|
||||
.flat_map(|mem| {
|
||||
let heap_len = mem.len() as u64;
|
||||
let heap = state.register_heap(HeapInit::FromBacking(mem));
|
||||
[
|
||||
state.get_heap_address(I64, heap, 0).unwrap(),
|
||||
state.get_heap_address(I64, heap, heap_len).unwrap(),
|
||||
]
|
||||
})
|
||||
.map(|addr| {
|
||||
let mut mem = [0u8; 8];
|
||||
addr.write_to_slice(&mut mem[..]);
|
||||
mem
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
let vmctx_heap = state.register_heap(HeapInit::FromBacking(vmctx_struct));
|
||||
state.get_heap_address(I64, vmctx_heap, 0).unwrap()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
use crate::function_runner::{CompiledTestFile, TestFileCompiler};
|
||||
use crate::runone::FileUpdate;
|
||||
use crate::runtest_environment::{HeapMemory, RuntestEnvironment};
|
||||
use crate::subtest::{Context, SubTest};
|
||||
use anyhow::Context as _;
|
||||
use cranelift_codegen::data_value::DataValue;
|
||||
@@ -117,22 +116,16 @@ fn run_test(
|
||||
func: &ir::Function,
|
||||
context: &Context,
|
||||
) -> anyhow::Result<()> {
|
||||
let test_env = RuntestEnvironment::parse(&context.details.comments[..])?;
|
||||
|
||||
for comment in context.details.comments.iter() {
|
||||
if let Some(command) = parse_run_command(comment.text, &func.signature)? {
|
||||
trace!("Parsed run command: {}", command);
|
||||
|
||||
command
|
||||
.run(|_, run_args| {
|
||||
test_env.validate_signature(&func)?;
|
||||
let (_heaps, _ctx_struct, vmctx_ptr) =
|
||||
build_vmctx_struct(&test_env, context.isa.unwrap().pointer_type());
|
||||
let (_ctx_struct, _vmctx_ptr) =
|
||||
build_vmctx_struct(context.isa.unwrap().pointer_type());
|
||||
|
||||
let mut args = Vec::with_capacity(run_args.len());
|
||||
if test_env.is_active() {
|
||||
args.push(vmctx_ptr);
|
||||
}
|
||||
args.extend_from_slice(run_args);
|
||||
|
||||
let trampoline = testfile.get_trampoline(func).unwrap();
|
||||
@@ -215,22 +208,13 @@ impl SubTest for TestRun {
|
||||
}
|
||||
|
||||
/// Build a VMContext struct with the layout described in docs/testing.md.
|
||||
pub fn build_vmctx_struct(
|
||||
test_env: &RuntestEnvironment,
|
||||
ptr_ty: Type,
|
||||
) -> (Vec<HeapMemory>, Vec<u64>, DataValue) {
|
||||
let heaps = test_env.allocate_memory();
|
||||
|
||||
let context_struct: Vec<u64> = heaps
|
||||
.iter()
|
||||
.flat_map(|heap| [heap.as_ptr(), heap.as_ptr().wrapping_add(heap.len())])
|
||||
.map(|p| p as usize as u64)
|
||||
.collect();
|
||||
pub fn build_vmctx_struct(ptr_ty: Type) -> (Vec<u64>, DataValue) {
|
||||
let context_struct: Vec<u64> = Vec::new();
|
||||
|
||||
let ptr = context_struct.as_ptr() as usize as i128;
|
||||
let ptr_dv =
|
||||
DataValue::from_integer(ptr, ptr_ty).expect("Failed to cast pointer to native target size");
|
||||
|
||||
// Return all these to make sure we don't deallocate the heaps too early
|
||||
(heaps, context_struct, ptr_dv)
|
||||
(context_struct, ptr_dv)
|
||||
}
|
||||
|
||||
@@ -154,8 +154,11 @@ pub struct TestHeap {
|
||||
}
|
||||
|
||||
impl TestHeap {
|
||||
pub fn to_ir(&self, name_to_ir_global: &BTreeMap<String, ir::GlobalValue>) -> ir::HeapData {
|
||||
ir::HeapData {
|
||||
pub fn to_ir(
|
||||
&self,
|
||||
name_to_ir_global: &BTreeMap<String, ir::GlobalValue>,
|
||||
) -> cranelift_wasm::HeapData {
|
||||
cranelift_wasm::HeapData {
|
||||
base: name_to_ir_global[&self.base],
|
||||
min_size: self.min_size.into(),
|
||||
offset_guard_size: self.offset_guard_size.into(),
|
||||
@@ -187,15 +190,18 @@ pub struct TestHeapStyle {
|
||||
}
|
||||
|
||||
impl TestHeapStyle {
|
||||
pub fn to_ir(&self, name_to_ir_global: &BTreeMap<String, ir::GlobalValue>) -> ir::HeapStyle {
|
||||
pub fn to_ir(
|
||||
&self,
|
||||
name_to_ir_global: &BTreeMap<String, ir::GlobalValue>,
|
||||
) -> cranelift_wasm::HeapStyle {
|
||||
match self.kind.as_str() {
|
||||
"static" => ir::HeapStyle::Static {
|
||||
"static" => cranelift_wasm::HeapStyle::Static {
|
||||
bound: match &self.bound {
|
||||
toml::Value::Integer(x) => u64::try_from(*x).unwrap().into(),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
},
|
||||
"dynamic" => ir::HeapStyle::Dynamic {
|
||||
"dynamic" => cranelift_wasm::HeapStyle::Dynamic {
|
||||
bound_gv: match &self.bound {
|
||||
toml::Value::String(g) => name_to_ir_global[g],
|
||||
_ => unreachable!(),
|
||||
|
||||
@@ -49,6 +49,7 @@ impl<'data> ModuleEnvironment<'data> for ModuleEnv {
|
||||
&self.inner.info,
|
||||
self.inner.expected_reachability.clone(),
|
||||
self.config.clone(),
|
||||
self.heap_access_spectre_mitigation,
|
||||
);
|
||||
let func_index = FuncIndex::new(
|
||||
self.inner.get_num_func_imports() + self.inner.info.function_bodies.len(),
|
||||
@@ -234,6 +235,7 @@ pub struct FuncEnv<'a> {
|
||||
pub config: TestConfig,
|
||||
pub name_to_ir_global: BTreeMap<String, ir::GlobalValue>,
|
||||
pub next_heap: usize,
|
||||
pub heap_access_spectre_mitigation: bool,
|
||||
}
|
||||
|
||||
impl<'a> FuncEnv<'a> {
|
||||
@@ -241,6 +243,7 @@ impl<'a> FuncEnv<'a> {
|
||||
mod_info: &'a cranelift_wasm::DummyModuleInfo,
|
||||
expected_reachability: Option<cranelift_wasm::ExpectedReachability>,
|
||||
config: TestConfig,
|
||||
heap_access_spectre_mitigation: bool,
|
||||
) -> Self {
|
||||
let inner = cranelift_wasm::DummyFuncEnvironment::new(mod_info, expected_reachability);
|
||||
Self {
|
||||
@@ -248,6 +251,7 @@ impl<'a> FuncEnv<'a> {
|
||||
config,
|
||||
name_to_ir_global: Default::default(),
|
||||
next_heap: 0,
|
||||
heap_access_spectre_mitigation,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,6 +260,10 @@ impl<'a> TargetEnvironment for FuncEnv<'a> {
|
||||
fn target_config(&self) -> TargetFrontendConfig {
|
||||
self.inner.target_config()
|
||||
}
|
||||
|
||||
fn heap_access_spectre_mitigation(&self) -> bool {
|
||||
self.heap_access_spectre_mitigation
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
@@ -263,7 +271,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
func: &mut ir::Function,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
) -> cranelift_wasm::WasmResult<ir::Heap> {
|
||||
) -> cranelift_wasm::WasmResult<cranelift_wasm::Heap> {
|
||||
if self.next_heap < self.config.heaps.len() {
|
||||
let heap = &self.config.heaps[self.next_heap];
|
||||
self.next_heap += 1;
|
||||
@@ -311,7 +319,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
self.name_to_ir_global.insert(global_name.to_string(), g);
|
||||
}
|
||||
|
||||
Ok(func.create_heap(heap.to_ir(&self.name_to_ir_global)))
|
||||
Ok(self.inner.heaps.push(heap.to_ir(&self.name_to_ir_global)))
|
||||
} else {
|
||||
self.inner.make_heap(func, index)
|
||||
}
|
||||
@@ -378,7 +386,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
heap: ir::Heap,
|
||||
heap: cranelift_wasm::Heap,
|
||||
val: ir::Value,
|
||||
) -> cranelift_wasm::WasmResult<ir::Value> {
|
||||
self.inner.translate_memory_grow(pos, index, heap, val)
|
||||
@@ -388,7 +396,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
heap: ir::Heap,
|
||||
heap: cranelift_wasm::Heap,
|
||||
) -> cranelift_wasm::WasmResult<ir::Value> {
|
||||
self.inner.translate_memory_size(pos, index, heap)
|
||||
}
|
||||
@@ -397,9 +405,9 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
src_index: cranelift_wasm::MemoryIndex,
|
||||
src_heap: ir::Heap,
|
||||
src_heap: cranelift_wasm::Heap,
|
||||
dst_index: cranelift_wasm::MemoryIndex,
|
||||
dst_heap: ir::Heap,
|
||||
dst_heap: cranelift_wasm::Heap,
|
||||
dst: ir::Value,
|
||||
src: ir::Value,
|
||||
len: ir::Value,
|
||||
@@ -412,7 +420,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
heap: ir::Heap,
|
||||
heap: cranelift_wasm::Heap,
|
||||
dst: ir::Value,
|
||||
val: ir::Value,
|
||||
len: ir::Value,
|
||||
@@ -425,7 +433,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
heap: ir::Heap,
|
||||
heap: cranelift_wasm::Heap,
|
||||
seg_index: u32,
|
||||
dst: ir::Value,
|
||||
src: ir::Value,
|
||||
@@ -574,7 +582,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
heap: ir::Heap,
|
||||
heap: cranelift_wasm::Heap,
|
||||
addr: ir::Value,
|
||||
expected: ir::Value,
|
||||
timeout: ir::Value,
|
||||
@@ -587,7 +595,7 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
&mut self,
|
||||
pos: cranelift_codegen::cursor::FuncCursor,
|
||||
index: cranelift_wasm::MemoryIndex,
|
||||
heap: ir::Heap,
|
||||
heap: cranelift_wasm::Heap,
|
||||
addr: ir::Value,
|
||||
count: ir::Value,
|
||||
) -> cranelift_wasm::WasmResult<ir::Value> {
|
||||
@@ -598,4 +606,11 @@ impl<'a> FuncEnvironment for FuncEnv<'a> {
|
||||
fn unsigned_add_overflow_condition(&self) -> ir::condcodes::IntCC {
|
||||
self.inner.unsigned_add_overflow_condition()
|
||||
}
|
||||
|
||||
fn heaps(
|
||||
&self,
|
||||
) -> &cranelift_codegen::entity::PrimaryMap<cranelift_wasm::Heap, cranelift_wasm::HeapData>
|
||||
{
|
||||
self.inner.heaps()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user