Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Tag loads of the builtin-functions array's elements alias regions (#13781)

Nick Fitzgerald2753ee739335
diff --git a/crates/cranelift/src/alias_region.rs b/crates/cranelift/src/alias_region.rs
index c38d5e495f..ff70cdf87d 100644
--- a/crates/cranelift/src/alias_region.rs
+++ b/crates/cranelift/src/alias_region.rs
@@ -5,9 +5,9 @@ use cranelift_codegen::{
ir::{self, InstBuilder as _},
};
use wasmtime_environ::{
- DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, GetPtrSize, GlobalIndex,
- MemoryIndex, ModuleInternedTypeIndex, OwnedMemoryIndex, PtrSize as _, RuntimeDataIndex,
- StaticModuleIndex, TableIndex, TagIndex, VMOffsets,
+ BuiltinFunctionIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex,
+ GetPtrSize, GlobalIndex, MemoryIndex, ModuleInternedTypeIndex, OwnedMemoryIndex, PtrSize as _,
+ RuntimeDataIndex, StaticModuleIndex, TableIndex, TagIndex, VMOffsets,
component::{
LoweredIndex, ResourceIndex, RuntimeCallbackIndex, RuntimeComponentInstanceIndex,
RuntimeMemoryIndex, RuntimePostReturnIndex, VMComponentOffsets,
@@ -35,6 +35,7 @@ enum VmType {
VMFuncRef,
TypeIdsArray,
EpochCounter,
+ BuiltinFunctionsArray,
}
/// A key that uniquely identifies an alias region across an entire compilation.
@@ -146,6 +147,7 @@ impl AliasRegionKey {
const VM_FUNC_REF_KIND: u32 = Self::new_kind(0b11000);
const TYPE_IDS_ARRAY_KIND: u32 = Self::new_kind(0b11001);
const EPOCH_COUNTER_KIND: u32 = Self::new_kind(0b11010);
+ const BUILTIN_FUNCTIONS_KIND: u32 = Self::new_kind(0b11011);
/// Encode this key into a raw `u32` suitable for use as an
/// `AliasRegionData::user_id`.
@@ -173,6 +175,7 @@ impl AliasRegionKey {
VmType::VMFuncRef => Self::VM_FUNC_REF_KIND,
VmType::TypeIdsArray => Self::TYPE_IDS_ARRAY_KIND,
VmType::EpochCounter => Self::EPOCH_COUNTER_KIND,
+ VmType::BuiltinFunctionsArray => Self::BUILTIN_FUNCTIONS_KIND,
};
kind | (offset & Self::OFFSET_MASK)
}
@@ -1943,6 +1946,45 @@ where
}
}
+/// Builtin-functions array methods.
+impl<Offsets> AliasRegions<Offsets>
+where
+ Offsets: GetPtrSize,
+{
+ /// Load a host function pointer element out of a builtin-functions array
+ /// (either `VMContext::builtin_functions` or
+ /// `VMComponentContext::builtin_functions`).
+ pub fn builtin_functions_array_element(
+ &mut self,
+ cursor: &mut FuncCursor<'_>,
+ array: ir::Value,
+ builtin: BuiltinFunctionIndex,
+ ) -> ir::Value {
+ let offset = builtin
+ .index()
+ .checked_mul(self.pointer_type.bytes())
+ .unwrap();
+
+ let region = self.region(
+ cursor.func,
+ AliasRegionKey::Vm {
+ ty: VmType::BuiltinFunctionsArray,
+ offset,
+ },
+ );
+
+ cursor.ins().load(
+ self.pointer_type,
+ ir::MemFlagsData::trusted()
+ .with_readonly()
+ .with_can_move()
+ .with_alias_region(Some(region)),
+ array,
+ i32::try_from(offset).unwrap(),
+ )
+ }
+}
+
/// `VMComponentContext`-related methods, used when compiling component
/// trampolines.
impl AliasRegions<VMComponentOffsets<u8>> {
diff --git a/crates/cranelift/src/compiler.rs b/crates/cranelift/src/compiler.rs
index fa435e3424..d857b4fced 100644
--- a/crates/cranelift/src/compiler.rs
+++ b/crates/cranelift/src/compiler.rs
@@ -1318,20 +1318,15 @@ impl Compiler {
where
O: GetPtrSize,
{
- let isa = &*self.isa;
- let pointer_type = isa.pointer_type();
-
// Builtins are stored in an array in all `VMContext`s. First load the
- // base pointer of the array and then load the entry of the array that
- // corresponds to this builtin.
+ // base pointer of the array...
let array_addr = alias_regions.vmctx_builtin_functions(&mut builder.cursor(), vmctx);
-
- let body_offset = i32::try_from(builtin.index() * pointer_type.bytes()).unwrap();
- let func_addr = builder.ins().load(
- pointer_type,
- ir::MemFlagsData::trusted().with_readonly().with_can_move(),
+ // ... and then load the entry in the array that corresponds to this
+ // builtin.
+ let func_addr = alias_regions.builtin_functions_array_element(
+ &mut builder.cursor(),
array_addr,
- body_offset,
+ builtin,
);
let sig = builder.func.import_signature(sig);
diff --git a/tests/disas/riscv64-component-builtins.wat b/tests/disas/riscv64-component-builtins.wat
index 4514493cd9..65ad2d6fc9 100644
--- a/tests/disas/riscv64-component-builtins.wat
+++ b/tests/disas/riscv64-component-builtins.wat
@@ -17,6 +17,7 @@
;; region3 = 1476395040 "VMComponentContext+0x20"
;; region4 = 1476395016 "VMComponentContext+0x8"
;; region5 = 16 "VMContext+0x10"
+;; region6 = 3623878984 "BuiltinFunctionsArray+0x148"
;; sig0 = (i64 sext, i32 sext, i32 sext, i32 sext) -> i64 sext system_v
;; sig1 = (i64 sext vmctx) system_v
;;
@@ -38,7 +39,7 @@
;;
;; block1 cold:
;; v14 = load.i64 notrap aligned readonly can_move region5 v1+16
-;; v15 = load.i64 notrap aligned readonly can_move v14+328
+;; v15 = load.i64 notrap aligned readonly can_move region6 v14+328
;; call_indirect sig1, v15(v1)
;; trap user1
;;