A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Cranelift/Wasmtime/Pulley/Debugging: use little-endian mode to spill/reload vectors in guest-debugging slot and ABI clobbers. (#12585)
Chris Fallind9038ed024c4
diff --git a/cranelift/codegen/src/isa/pulley_shared/abi.rs b/cranelift/codegen/src/isa/pulley_shared/abi.rsindex 8032e481c3..f4907beccd 100644--- a/cranelift/codegen/src/isa/pulley_shared/abi.rs+++ b/cranelift/codegen/src/isa/pulley_shared/abi.rs@@ -345,9 +345,11 @@ where}for (offset, ty, reg) in frame_layout.manually_managed_clobbers(&style) {- insts.push(- Inst::gen_store(Amode::SpOffset { offset }, reg, ty, MemFlags::trusted()).into(),- );+ let mut flags = MemFlags::trusted();+ if ty.is_vector() {+ flags.set_endianness(ir::Endianness::Little);+ }+ insts.push(Inst::gen_store(Amode::SpOffset { offset }, reg, ty, flags).into());}insts@@ -366,12 +368,16 @@ where// Restore clobbered registers that are manually managed in Cranelift.for (offset, ty, reg) in frame_layout.manually_managed_clobbers(&style) {+ let mut flags = MemFlags::trusted();+ if ty.is_vector() {+ flags.set_endianness(ir::Endianness::Little);+ }insts.push(Inst::gen_load(Writable::from_reg(reg),Amode::SpOffset { offset },ty,- MemFlags::trusted(),+ flags,).into(),);diff --git a/cranelift/filetests/filetests/isa/pulley32/preserve-all.clif b/cranelift/filetests/filetests/isa/pulley32/preserve-all.clifindex 26ad386ded..c7d523d4f6 100644--- a/cranelift/filetests/filetests/isa/pulley32/preserve-all.clif+++ b/cranelift/filetests/filetests/isa/pulley32/preserve-all.clif@@ -87,38 +87,38 @@ block0(v0: i64):; fstore64 sp+528, f29 // flags = notrap aligned; fstore64 sp+520, f30 // flags = notrap aligned; fstore64 sp+512, f31 // flags = notrap aligned-; vstore128 sp+504, v0 // flags = notrap aligned-; vstore128 sp+496, v1 // flags = notrap aligned-; vstore128 sp+488, v2 // flags = notrap aligned-; vstore128 sp+480, v3 // flags = notrap aligned-; vstore128 sp+472, v4 // flags = notrap aligned-; vstore128 sp+464, v5 // flags = notrap aligned-; vstore128 sp+456, v6 // flags = notrap aligned-; vstore128 sp+448, v7 // flags = notrap aligned-; vstore128 sp+440, v8 // flags = notrap aligned-; vstore128 sp+432, v9 // flags = notrap aligned-; vstore128 sp+424, v10 // flags = notrap aligned-; vstore128 sp+416, v11 // flags = notrap aligned-; vstore128 sp+408, v12 // flags = notrap aligned-; vstore128 sp+400, v13 // flags = notrap aligned-; vstore128 sp+392, v14 // flags = notrap aligned-; vstore128 sp+384, v15 // flags = notrap aligned-; vstore128 sp+376, v16 // flags = notrap aligned-; vstore128 sp+368, v17 // flags = notrap aligned-; vstore128 sp+360, v18 // flags = notrap aligned-; vstore128 sp+352, v19 // flags = notrap aligned-; vstore128 sp+344, v20 // flags = notrap aligned-; vstore128 sp+336, v21 // flags = notrap aligned-; vstore128 sp+328, v22 // flags = notrap aligned-; vstore128 sp+320, v23 // flags = notrap aligned-; vstore128 sp+312, v24 // flags = notrap aligned-; vstore128 sp+304, v25 // flags = notrap aligned-; vstore128 sp+296, v26 // flags = notrap aligned-; vstore128 sp+288, v27 // flags = notrap aligned-; vstore128 sp+280, v28 // flags = notrap aligned-; vstore128 sp+272, v29 // flags = notrap aligned-; vstore128 sp+264, v30 // flags = notrap aligned-; vstore128 sp+256, v31 // flags = notrap aligned+; vstore128 sp+504, v0 // flags = notrap aligned little+; vstore128 sp+496, v1 // flags = notrap aligned little+; vstore128 sp+488, v2 // flags = notrap aligned little+; vstore128 sp+480, v3 // flags = notrap aligned little+; vstore128 sp+472, v4 // flags = notrap aligned little+; vstore128 sp+464, v5 // flags = notrap aligned little+; vstore128 sp+456, v6 // flags = notrap aligned little+; vstore128 sp+448, v7 // flags = notrap aligned little+; vstore128 sp+440, v8 // flags = notrap aligned little+; vstore128 sp+432, v9 // flags = notrap aligned little+; vstore128 sp+424, v10 // flags = notrap aligned little+; vstore128 sp+416, v11 // flags = notrap aligned little+; vstore128 sp+408, v12 // flags = notrap aligned little+; vstore128 sp+400, v13 // flags = notrap aligned little+; vstore128 sp+392, v14 // flags = notrap aligned little+; vstore128 sp+384, v15 // flags = notrap aligned little+; vstore128 sp+376, v16 // flags = notrap aligned little+; vstore128 sp+368, v17 // flags = notrap aligned little+; vstore128 sp+360, v18 // flags = notrap aligned little+; vstore128 sp+352, v19 // flags = notrap aligned little+; vstore128 sp+344, v20 // flags = notrap aligned little+; vstore128 sp+336, v21 // flags = notrap aligned little+; vstore128 sp+328, v22 // flags = notrap aligned little+; vstore128 sp+320, v23 // flags = notrap aligned little+; vstore128 sp+312, v24 // flags = notrap aligned little+; vstore128 sp+304, v25 // flags = notrap aligned little+; vstore128 sp+296, v26 // flags = notrap aligned little+; vstore128 sp+288, v27 // flags = notrap aligned little+; vstore128 sp+280, v28 // flags = notrap aligned little+; vstore128 sp+272, v29 // flags = notrap aligned little+; vstore128 sp+264, v30 // flags = notrap aligned little+; vstore128 sp+256, v31 // flags = notrap aligned little; block0:; indirect_call_host CallInfo { dest: TestCase(%libcall), uses: [CallArgPair { vreg: p0i, preg: p0i }], defs: [], clobbers: PRegSet { bits: [65535, 4294967295, 4294967295, 0] }, callee_conv: SystemV, caller_conv: PreserveAll, callee_pop_size: 0, try_call_info: None, patchable: false }; x0 = xload64 sp+888 // flags = notrap aligned@@ -169,38 +169,38 @@ block0(v0: i64):; f29 = fload64 sp+528 // flags = notrap aligned; f30 = fload64 sp+520 // flags = notrap aligned; f31 = fload64 sp+512 // flags = notrap aligned-; v0 = vload128 sp+504 // flags = notrap aligned-; v1 = vload128 sp+496 // flags = notrap aligned-; v2 = vload128 sp+488 // flags = notrap aligned-; v3 = vload128 sp+480 // flags = notrap aligned-; v4 = vload128 sp+472 // flags = notrap aligned-; v5 = vload128 sp+464 // flags = notrap aligned-; v6 = vload128 sp+456 // flags = notrap aligned-; v7 = vload128 sp+448 // flags = notrap aligned-; v8 = vload128 sp+440 // flags = notrap aligned-; v9 = vload128 sp+432 // flags = notrap aligned-; v10 = vload128 sp+424 // flags = notrap aligned-; v11 = vload128 sp+416 // flags = notrap aligned-; v12 = vload128 sp+408 // flags = notrap aligned-; v13 = vload128 sp+400 // flags = notrap aligned-; v14 = vload128 sp+392 // flags = notrap aligned-; v15 = vload128 sp+384 // flags = notrap aligned-; v16 = vload128 sp+376 // flags = notrap aligned-; v17 = vload128 sp+368 // flags = notrap aligned-; v18 = vload128 sp+360 // flags = notrap aligned-; v19 = vload128 sp+352 // flags = notrap aligned-; v20 = vload128 sp+344 // flags = notrap aligned-; v21 = vload128 sp+336 // flags = notrap aligned-; v22 = vload128 sp+328 // flags = notrap aligned-; v23 = vload128 sp+320 // flags = notrap aligned-; v24 = vload128 sp+312 // flags = notrap aligned-; v25 = vload128 sp+304 // flags = notrap aligned-; v26 = vload128 sp+296 // flags = notrap aligned-; v27 = vload128 sp+288 // flags = notrap aligned-; v28 = vload128 sp+280 // flags = notrap aligned-; v29 = vload128 sp+272 // flags = notrap aligned-; v30 = vload128 sp+264 // flags = notrap aligned-; v31 = vload128 sp+256 // flags = notrap aligned+; v0 = vload128 sp+504 // flags = notrap aligned little+; v1 = vload128 sp+496 // flags = notrap aligned little+; v2 = vload128 sp+488 // flags = notrap aligned little+; v3 = vload128 sp+480 // flags = notrap aligned little+; v4 = vload128 sp+472 // flags = notrap aligned little+; v5 = vload128 sp+464 // flags = notrap aligned little+; v6 = vload128 sp+456 // flags = notrap aligned little+; v7 = vload128 sp+448 // flags = notrap aligned little+; v8 = vload128 sp+440 // flags = notrap aligned little+; v9 = vload128 sp+432 // flags = notrap aligned little+; v10 = vload128 sp+424 // flags = notrap aligned little+; v11 = vload128 sp+416 // flags = notrap aligned little+; v12 = vload128 sp+408 // flags = notrap aligned little+; v13 = vload128 sp+400 // flags = notrap aligned little+; v14 = vload128 sp+392 // flags = notrap aligned little+; v15 = vload128 sp+384 // flags = notrap aligned little+; v16 = vload128 sp+376 // flags = notrap aligned little+; v17 = vload128 sp+368 // flags = notrap aligned little+; v18 = vload128 sp+360 // flags = notrap aligned little+; v19 = vload128 sp+352 // flags = notrap aligned little+; v20 = vload128 sp+344 // flags = notrap aligned little+; v21 = vload128 sp+336 // flags = notrap aligned little+; v22 = vload128 sp+328 // flags = notrap aligned little+; v23 = vload128 sp+320 // flags = notrap aligned little+; v24 = vload128 sp+312 // flags = notrap aligned little+; v25 = vload128 sp+304 // flags = notrap aligned little+; v26 = vload128 sp+296 // flags = notrap aligned little+; v27 = vload128 sp+288 // flags = notrap aligned little+; v28 = vload128 sp+280 // flags = notrap aligned little+; v29 = vload128 sp+272 // flags = notrap aligned little+; v30 = vload128 sp+264 // flags = notrap aligned little+; v31 = vload128 sp+256 // flags = notrap aligned little; pop_frame_restore 896, {}; ret;diff --git a/cranelift/filetests/filetests/isa/pulley64/preserve-all.clif b/cranelift/filetests/filetests/isa/pulley64/preserve-all.clifindex e7cf31ce03..2b6a28ce9e 100644--- a/cranelift/filetests/filetests/isa/pulley64/preserve-all.clif+++ b/cranelift/filetests/filetests/isa/pulley64/preserve-all.clif@@ -87,38 +87,38 @@ block0(v0: i64):; fstore64 sp+528, f29 // flags = notrap aligned; fstore64 sp+520, f30 // flags = notrap aligned; fstore64 sp+512, f31 // flags = notrap aligned-; vstore128 sp+504, v0 // flags = notrap aligned-; vstore128 sp+496, v1 // flags = notrap aligned-; vstore128 sp+488, v2 // flags = notrap aligned-; vstore128 sp+480, v3 // flags = notrap aligned-; vstore128 sp+472, v4 // flags = notrap aligned-; vstore128 sp+464, v5 // flags = notrap aligned-; vstore128 sp+456, v6 // flags = notrap aligned-; vstore128 sp+448, v7 // flags = notrap aligned-; vstore128 sp+440, v8 // flags = notrap aligned-; vstore128 sp+432, v9 // flags = notrap aligned-; vstore128 sp+424, v10 // flags = notrap aligned-; vstore128 sp+416, v11 // flags = notrap aligned-; vstore128 sp+408, v12 // flags = notrap aligned-; vstore128 sp+400, v13 // flags = notrap aligned-; vstore128 sp+392, v14 // flags = notrap aligned-; vstore128 sp+384, v15 // flags = notrap aligned-; vstore128 sp+376, v16 // flags = notrap aligned-; vstore128 sp+368, v17 // flags = notrap aligned-; vstore128 sp+360, v18 // flags = notrap aligned-; vstore128 sp+352, v19 // flags = notrap aligned-; vstore128 sp+344, v20 // flags = notrap aligned-; vstore128 sp+336, v21 // flags = notrap aligned-; vstore128 sp+328, v22 // flags = notrap aligned-; vstore128 sp+320, v23 // flags = notrap aligned-; vstore128 sp+312, v24 // flags = notrap aligned-; vstore128 sp+304, v25 // flags = notrap aligned-; vstore128 sp+296, v26 // flags = notrap aligned-; vstore128 sp+288, v27 // flags = notrap aligned-; vstore128 sp+280, v28 // flags = notrap aligned-; vstore128 sp+272, v29 // flags = notrap aligned-; vstore128 sp+264, v30 // flags = notrap aligned-; vstore128 sp+256, v31 // flags = notrap aligned+; vstore128 sp+504, v0 // flags = notrap aligned little+; vstore128 sp+496, v1 // flags = notrap aligned little+; vstore128 sp+488, v2 // flags = notrap aligned little+; vstore128 sp+480, v3 // flags = notrap aligned little+; vstore128 sp+472, v4 // flags = notrap aligned little+; vstore128 sp+464, v5 // flags = notrap aligned little+; vstore128 sp+456, v6 // flags = notrap aligned little+; vstore128 sp+448, v7 // flags = notrap aligned little+; vstore128 sp+440, v8 // flags = notrap aligned little+; vstore128 sp+432, v9 // flags = notrap aligned little+; vstore128 sp+424, v10 // flags = notrap aligned little+; vstore128 sp+416, v11 // flags = notrap aligned little+; vstore128 sp+408, v12 // flags = notrap aligned little+; vstore128 sp+400, v13 // flags = notrap aligned little+; vstore128 sp+392, v14 // flags = notrap aligned little+; vstore128 sp+384, v15 // flags = notrap aligned little+; vstore128 sp+376, v16 // flags = notrap aligned little+; vstore128 sp+368, v17 // flags = notrap aligned little+; vstore128 sp+360, v18 // flags = notrap aligned little+; vstore128 sp+352, v19 // flags = notrap aligned little+; vstore128 sp+344, v20 // flags = notrap aligned little+; vstore128 sp+336, v21 // flags = notrap aligned little+; vstore128 sp+328, v22 // flags = notrap aligned little+; vstore128 sp+320, v23 // flags = notrap aligned little+; vstore128 sp+312, v24 // flags = notrap aligned little+; vstore128 sp+304, v25 // flags = notrap aligned little+; vstore128 sp+296, v26 // flags = notrap aligned little+; vstore128 sp+288, v27 // flags = notrap aligned little+; vstore128 sp+280, v28 // flags = notrap aligned little+; vstore128 sp+272, v29 // flags = notrap aligned little+; vstore128 sp+264, v30 // flags = notrap aligned little+; vstore128 sp+256, v31 // flags = notrap aligned little; block0:; indirect_call_host CallInfo { dest: TestCase(%libcall), uses: [CallArgPair { vreg: p0i, preg: p0i }], defs: [], clobbers: PRegSet { bits: [65535, 4294967295, 4294967295, 0] }, callee_conv: SystemV, caller_conv: PreserveAll, callee_pop_size: 0, try_call_info: None, patchable: false }; x0 = xload64 sp+888 // flags = notrap aligned@@ -169,38 +169,38 @@ block0(v0: i64):; f29 = fload64 sp+528 // flags = notrap aligned; f30 = fload64 sp+520 // flags = notrap aligned; f31 = fload64 sp+512 // flags = notrap aligned-; v0 = vload128 sp+504 // flags = notrap aligned-; v1 = vload128 sp+496 // flags = notrap aligned-; v2 = vload128 sp+488 // flags = notrap aligned-; v3 = vload128 sp+480 // flags = notrap aligned-; v4 = vload128 sp+472 // flags = notrap aligned-; v5 = vload128 sp+464 // flags = notrap aligned-; v6 = vload128 sp+456 // flags = notrap aligned-; v7 = vload128 sp+448 // flags = notrap aligned-; v8 = vload128 sp+440 // flags = notrap aligned-; v9 = vload128 sp+432 // flags = notrap aligned-; v10 = vload128 sp+424 // flags = notrap aligned-; v11 = vload128 sp+416 // flags = notrap aligned-; v12 = vload128 sp+408 // flags = notrap aligned-; v13 = vload128 sp+400 // flags = notrap aligned-; v14 = vload128 sp+392 // flags = notrap aligned-; v15 = vload128 sp+384 // flags = notrap aligned-; v16 = vload128 sp+376 // flags = notrap aligned-; v17 = vload128 sp+368 // flags = notrap aligned-; v18 = vload128 sp+360 // flags = notrap aligned-; v19 = vload128 sp+352 // flags = notrap aligned-; v20 = vload128 sp+344 // flags = notrap aligned-; v21 = vload128 sp+336 // flags = notrap aligned-; v22 = vload128 sp+328 // flags = notrap aligned-; v23 = vload128 sp+320 // flags = notrap aligned-; v24 = vload128 sp+312 // flags = notrap aligned-; v25 = vload128 sp+304 // flags = notrap aligned-; v26 = vload128 sp+296 // flags = notrap aligned-; v27 = vload128 sp+288 // flags = notrap aligned-; v28 = vload128 sp+280 // flags = notrap aligned-; v29 = vload128 sp+272 // flags = notrap aligned-; v30 = vload128 sp+264 // flags = notrap aligned-; v31 = vload128 sp+256 // flags = notrap aligned+; v0 = vload128 sp+504 // flags = notrap aligned little+; v1 = vload128 sp+496 // flags = notrap aligned little+; v2 = vload128 sp+488 // flags = notrap aligned little+; v3 = vload128 sp+480 // flags = notrap aligned little+; v4 = vload128 sp+472 // flags = notrap aligned little+; v5 = vload128 sp+464 // flags = notrap aligned little+; v6 = vload128 sp+456 // flags = notrap aligned little+; v7 = vload128 sp+448 // flags = notrap aligned little+; v8 = vload128 sp+440 // flags = notrap aligned little+; v9 = vload128 sp+432 // flags = notrap aligned little+; v10 = vload128 sp+424 // flags = notrap aligned little+; v11 = vload128 sp+416 // flags = notrap aligned little+; v12 = vload128 sp+408 // flags = notrap aligned little+; v13 = vload128 sp+400 // flags = notrap aligned little+; v14 = vload128 sp+392 // flags = notrap aligned little+; v15 = vload128 sp+384 // flags = notrap aligned little+; v16 = vload128 sp+376 // flags = notrap aligned little+; v17 = vload128 sp+368 // flags = notrap aligned little+; v18 = vload128 sp+360 // flags = notrap aligned little+; v19 = vload128 sp+352 // flags = notrap aligned little+; v20 = vload128 sp+344 // flags = notrap aligned little+; v21 = vload128 sp+336 // flags = notrap aligned little+; v22 = vload128 sp+328 // flags = notrap aligned little+; v23 = vload128 sp+320 // flags = notrap aligned little+; v24 = vload128 sp+312 // flags = notrap aligned little+; v25 = vload128 sp+304 // flags = notrap aligned little+; v26 = vload128 sp+296 // flags = notrap aligned little+; v27 = vload128 sp+288 // flags = notrap aligned little+; v28 = vload128 sp+280 // flags = notrap aligned little+; v29 = vload128 sp+272 // flags = notrap aligned little+; v30 = vload128 sp+264 // flags = notrap aligned little+; v31 = vload128 sp+256 // flags = notrap aligned little; pop_frame_restore 896, {}; ret;diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rsindex 5bb1698096..2025ed7dd7 100644--- a/crates/cranelift/src/func_environ.rs+++ b/crates/cranelift/src/func_environ.rs@@ -11,7 +11,9 @@ use cranelift_codegen::cursor::FuncCursor;use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};use cranelift_codegen::ir::immediates::{Imm64, Offset32, V128Imm};use cranelift_codegen::ir::pcc::Fact;-use cranelift_codegen::ir::{self, BlockArg, ExceptionTableData, ExceptionTableItem, types};+use cranelift_codegen::ir::{+ self, BlockArg, Endianness, ExceptionTableData, ExceptionTableItem, types,+};use cranelift_codegen::ir::{ArgumentPurpose, ConstantData, Function, InstBuilder, MemFlags};use cranelift_codegen::ir::{Block, types::*};use cranelift_codegen::isa::{CallConv, TargetFrontendConfig, TargetIsa};@@ -1239,6 +1241,26 @@ impl<'module_environment> FuncEnvironment<'module_environment> {}}+ fn memflags_for_debug_slot_value_wasm_ty(&self, ty: WasmValType) -> MemFlags {+ // Store vectors in little-endian format: this is+ // universally supported, while native or+ // big-endian formats may not be in all cases+ // (e.g. Pulley on s390x).+ let mut flags = MemFlags::trusted();+ if ty == WasmValType::V128 {+ flags.set_endianness(Endianness::Little);+ }+ flags+ }++ fn memflags_for_debug_slot_value_clif_ty(&self, ty: ir::Type) -> MemFlags {+ let mut flags = MemFlags::trusted();+ if ty.is_vector() {+ flags.set_endianness(Endianness::Little);+ }+ flags+ }+/// Update the state slot layout with a new layout given a local.pub(crate) fn add_state_slot_local(&mut self,@@ -1249,7 +1271,16 @@ impl<'module_environment> FuncEnvironment<'module_environment> {if let Some((slot, b)) = &mut self.state_slot {let offset = b.add_local(FrameValType::from(ty));if let Some(init) = init {- builder.ins().stack_store(init, *slot, offset.offset());+ let slot = *slot;+ let address = builder+ .ins()+ .stack_addr(self.pointer_type(), slot, offset.offset());+ builder.ins().store(+ self.memflags_for_debug_slot_value_wasm_ty(ty),+ init,+ address,+ 0,+ );}}}@@ -1293,7 +1324,16 @@ impl<'module_environment> FuncEnvironment<'module_environment> {self.stacks.stack_shape.push(this_shape);let value = self.stacks.stack[i];- builder.ins().stack_store(value, slot, offset.offset());+ let address =+ builder+ .ins()+ .stack_addr(self.pointer_type(), slot, offset.offset());+ builder.ins().store(+ self.memflags_for_debug_slot_value_wasm_ty(wasm_ty),+ value,+ address,+ 0,+ );} else {// Unreachable code with unknown type -- no// flushes for this or later-pushed values.@@ -1342,7 +1382,16 @@ impl<'module_environment> FuncEnvironment<'module_environment> {) {if let Some((slot, b)) = &self.state_slot {let offset = b.local_offset(local);- builder.ins().stack_store(value, *slot, offset.offset());+ let address = builder+ .ins()+ .stack_addr(self.pointer_type(), *slot, offset.offset());+ let ty = builder.func.dfg.value_type(value);+ builder.ins().store(+ self.memflags_for_debug_slot_value_clif_ty(ty),+ value,+ address,+ 0,+ );}}@@ -1353,6 +1402,10 @@ impl<'module_environment> FuncEnvironment<'module_environment> {// slot. This is relied upon in// crates/wasmtime/src/runtime/debug.rs in// `raw_instance()`. See also the slot layout computation in crates/environ/src/+ //+ // This is a native-endian store (the only mode for+ // `stack_store`) because it is read by host code directly+ // as a pointer.builder.ins().stack_store(vmctx, slot, 0);}}diff --git a/crates/wasmtime/src/runtime/debug.rs b/crates/wasmtime/src/runtime/debug.rsindex 9432700341..868029303b 100644--- a/crates/wasmtime/src/runtime/debug.rs+++ b/crates/wasmtime/src/runtime/debug.rs@@ -717,7 +717,8 @@ unsafe fn read_value(Val::F64(value)}FrameValType::V128 => {- let value = unsafe { *(address as *const u128) };+ // Vectors are always stored as little-endian.+ let value = unsafe { u128::from_le_bytes(*(address as *const [u8; 16])) };Val::V128(value.into())}FrameValType::AnyRef => {