Skip to content

Commit

Permalink
Merge #2065
Browse files Browse the repository at this point in the history
2065: Add dereferenceable attribute to the vmctx pointer. r=nlewycky a=nlewycky



Co-authored-by: Nick Lewycky <[email protected]>
Co-authored-by: nlewycky <[email protected]>
  • Loading branch information
bors[bot] and nlewycky authored Jan 27, 2021
2 parents 6b13216 + e668e34 commit 3db1b41
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
12 changes: 11 additions & 1 deletion lib/compiler-llvm/src/abi/aarch64_systemv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use inkwell::{
};
use wasmer_compiler::CompileError;
use wasmer_types::{FunctionType as FuncSig, Type};
use wasmer_vm::VMOffsets;

use std::convert::TryInto;

Expand Down Expand Up @@ -42,6 +43,7 @@ impl Abi for Aarch64SystemV {
&self,
context: &'ctx Context,
intrinsics: &Intrinsics<'ctx>,
offsets: Option<&VMOffsets>,
sig: &FuncSig,
) -> Result<(FunctionType<'ctx>, Vec<(Attribute, AttributeLoc)>), CompileError> {
let user_param_types = sig.params().iter().map(|&ty| type_to_llvm(intrinsics, ty));
Expand All @@ -56,7 +58,15 @@ impl Abi for Aarch64SystemV {
AttributeLoc::Param(i),
),
(
context.create_enum_attribute(Attribute::get_named_enum_kind_id("nonnull"), 0),
if let Some(offsets) = offsets {
context.create_enum_attribute(
Attribute::get_named_enum_kind_id("dereferenceable"),
offsets.size_of_vmctx().into(),
)
} else {
context
.create_enum_attribute(Attribute::get_named_enum_kind_id("nonnull"), 0)
},
AttributeLoc::Param(i),
),
(
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler-llvm/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use inkwell::{
};
use wasmer_compiler::CompileError;
use wasmer_types::FunctionType as FuncSig;
use wasmer_vm::VMOffsets;

mod aarch64_systemv;
mod x86_64_systemv;
Expand Down Expand Up @@ -49,6 +50,7 @@ pub trait Abi {
&self,
context: &'ctx Context,
intrinsics: &Intrinsics<'ctx>,
offsets: Option<&VMOffsets>,
sig: &FuncSig,
) -> Result<(FunctionType<'ctx>, Vec<(Attribute, AttributeLoc)>), CompileError>;

Expand Down
12 changes: 11 additions & 1 deletion lib/compiler-llvm/src/abi/x86_64_systemv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use inkwell::{
};
use wasmer_compiler::CompileError;
use wasmer_types::{FunctionType as FuncSig, Type};
use wasmer_vm::VMOffsets;

use std::convert::TryInto;

Expand Down Expand Up @@ -45,6 +46,7 @@ impl Abi for X86_64SystemV {
&self,
context: &'ctx Context,
intrinsics: &Intrinsics<'ctx>,
offsets: Option<&VMOffsets>,
sig: &FuncSig,
) -> Result<(FunctionType<'ctx>, Vec<(Attribute, AttributeLoc)>), CompileError> {
let user_param_types = sig.params().iter().map(|&ty| type_to_llvm(intrinsics, ty));
Expand All @@ -60,7 +62,15 @@ impl Abi for X86_64SystemV {
AttributeLoc::Param(i),
),
(
context.create_enum_attribute(Attribute::get_named_enum_kind_id("nonnull"), 0),
if let Some(offsets) = offsets {
context.create_enum_attribute(
Attribute::get_named_enum_kind_id("dereferenceable"),
offsets.size_of_vmctx().into(),
)
} else {
context
.create_enum_attribute(Attribute::get_named_enum_kind_id("nonnull"), 0)
},
AttributeLoc::Param(i),
),
(
Expand Down
7 changes: 5 additions & 2 deletions lib/compiler-llvm/src/trampoline/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ impl FuncTrampoline {
module.set_data_layout(&target_machine.get_target_data().get_data_layout());
let intrinsics = Intrinsics::declare(&module, &self.ctx);

let (callee_ty, callee_attrs) = self.abi.func_type_to_llvm(&self.ctx, &intrinsics, ty)?;
let (callee_ty, callee_attrs) =
self.abi
.func_type_to_llvm(&self.ctx, &intrinsics, None, ty)?;
let trampoline_ty = intrinsics.void_ty.fn_type(
&[
intrinsics.ctx_ptr_ty.as_basic_type_enum(), // vmctx ptr
Expand Down Expand Up @@ -181,7 +183,8 @@ impl FuncTrampoline {
let intrinsics = Intrinsics::declare(&module, &self.ctx);

let (trampoline_ty, trampoline_attrs) =
self.abi.func_type_to_llvm(&self.ctx, &intrinsics, ty)?;
self.abi
.func_type_to_llvm(&self.ctx, &intrinsics, None, ty)?;
let trampoline_func = module.add_function(name, trampoline_ty, Some(Linkage::External));
for (attr, attr_loc) in trampoline_attrs {
trampoline_func.add_attribute(attr_loc, attr);
Expand Down
15 changes: 10 additions & 5 deletions lib/compiler-llvm/src/translator/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use wasmer_types::{
FunctionIndex, FunctionType, GlobalIndex, LocalFunctionIndex, MemoryIndex, SignatureIndex,
TableIndex, Type,
};
use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle};
use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle, VMOffsets};

const FUNCTION_SECTION: &str = "__TEXT,wasmer_function";

Expand Down Expand Up @@ -101,10 +101,12 @@ impl FuncTranslator {
.get(wasm_module.functions[func_index])
.unwrap();

// TODO: pointer width
let offsets = VMOffsets::new(8, &wasm_module);
let intrinsics = Intrinsics::declare(&module, &self.ctx);
let (func_type, func_attrs) =
self.abi
.func_type_to_llvm(&self.ctx, &intrinsics, wasm_fn_type)?;
.func_type_to_llvm(&self.ctx, &intrinsics, Some(&offsets), wasm_fn_type)?;

let func = module.add_function(&function_name, func_type, Some(Linkage::External));
for (attr, attr_loc) in &func_attrs {
Expand Down Expand Up @@ -2350,9 +2352,12 @@ impl<'ctx, 'a> LLVMFunctionCodeGenerator<'ctx, 'a> {
self.builder.build_unreachable();
self.builder.position_at_end(continue_block);

let (llvm_func_type, llvm_func_attrs) =
self.abi
.func_type_to_llvm(&self.context, &self.intrinsics, func_type)?;
let (llvm_func_type, llvm_func_attrs) = self.abi.func_type_to_llvm(
&self.context,
&self.intrinsics,
Some(self.ctx.get_offsets()),
func_type,
)?;

let params = self.state.popn_save_extra(func_type.params().len())?;

Expand Down
16 changes: 13 additions & 3 deletions lib/compiler-llvm/src/translator/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,13 +934,18 @@ impl<'ctx, 'a> CtxType<'ctx, 'a> {
func_type: &FuncType,
function_name: &str,
) -> Result<&FunctionCache<'ctx>, CompileError> {
let (cached_functions, ctx_ptr_value) = (&mut self.cached_functions, &self.ctx_ptr_value);
let (cached_functions, ctx_ptr_value, offsets) = (
&mut self.cached_functions,
&self.ctx_ptr_value,
&self.offsets,
);
Ok(match cached_functions.entry(function_index) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
debug_assert!(module.get_function(function_name).is_none());
let (llvm_func_type, llvm_func_attrs) =
self.abi.func_type_to_llvm(context, intrinsics, func_type)?;
self.abi
.func_type_to_llvm(context, intrinsics, Some(offsets), func_type)?;
let func =
module.add_function(function_name, llvm_func_type, Some(Linkage::External));
for (attr, attr_loc) in &llvm_func_attrs {
Expand Down Expand Up @@ -973,7 +978,8 @@ impl<'ctx, 'a> CtxType<'ctx, 'a> {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let (llvm_func_type, llvm_func_attrs) =
self.abi.func_type_to_llvm(context, intrinsics, func_type)?;
self.abi
.func_type_to_llvm(context, intrinsics, Some(offsets), func_type)?;
debug_assert!(wasm_module.local_func_index(function_index).is_none());
let offset = offsets.vmctx_vmfunction_import(function_index);
let offset = intrinsics.i32_ty.const_int(offset.into(), false);
Expand Down Expand Up @@ -1097,6 +1103,10 @@ impl<'ctx, 'a> CtxType<'ctx, 'a> {
.into_pointer_value()
})
}

pub fn get_offsets(&self) -> &VMOffsets {
&self.offsets
}
}

// Given an instruction that operates on memory, mark the access as not aliasing
Expand Down

0 comments on commit 3db1b41

Please sign in to comment.