Add cross-language LLVM CFI support to the Rust compiler
This commit adds cross-language LLVM Control Flow Integrity (CFI) support to the Rust compiler by adding the `-Zsanitizer-cfi-normalize-integers` option to be used with Clang `-fsanitize-cfi-icall-normalize-integers` for normalizing integer types (see https://reviews.llvm.org/D139395). It provides forward-edge control flow protection for C or C++ and Rust -compiled code "mixed binaries" (i.e., for when C or C++ and Rust -compiled code share the same virtual address space). For more information about LLVM CFI and cross-language LLVM CFI support for the Rust compiler, see design document in the tracking issue #89653. Cross-language LLVM CFI can be enabled with -Zsanitizer=cfi and -Zsanitizer-cfi-normalize-integers, and requires proper (i.e., non-rustc) LTO (i.e., -Clinker-plugin-lto).
This commit is contained in:
parent
fec9adcdbc
commit
004aa15b47
70 changed files with 1384 additions and 387 deletions
|
@ -494,7 +494,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
|
|||
(rust_main, start_ty, vec![arg_argc, arg_argv])
|
||||
};
|
||||
|
||||
let result = bx.call(start_ty, None, start_fn, &args, None);
|
||||
let result = bx.call(start_ty, None, None, start_fn, &args, None);
|
||||
let cast = bx.intcast(result, cx.type_int(), true);
|
||||
bx.ret(cast);
|
||||
|
||||
|
|
|
@ -28,8 +28,9 @@ impl<'a, 'tcx> VirtualIndex {
|
|||
if bx.cx().sess().opts.unstable_opts.virtual_function_elimination
|
||||
&& bx.cx().sess().lto() == Lto::Fat
|
||||
{
|
||||
let typeid =
|
||||
bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), expect_dyn_trait_in_self(ty)));
|
||||
let typeid = bx
|
||||
.typeid_metadata(typeid_for_trait_ref(bx.tcx(), expect_dyn_trait_in_self(ty)))
|
||||
.unwrap();
|
||||
let vtable_byte_offset = self.0 * bx.data_layout().pointer_size.bytes();
|
||||
let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
|
||||
bx.pointercast(func, llty)
|
||||
|
|
|
@ -19,7 +19,6 @@ use rustc_middle::ty::{self, Instance, Ty};
|
|||
use rustc_session::config::OptLevel;
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_span::{sym, Symbol};
|
||||
use rustc_symbol_mangling::typeid::typeid_for_fnabi;
|
||||
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode, Reg};
|
||||
use rustc_target::abi::{self, HasDataLayout, WrappingRange};
|
||||
use rustc_target::spec::abi::Abi;
|
||||
|
@ -163,6 +162,12 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
|
|||
// do an invoke, otherwise do a call.
|
||||
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
|
||||
|
||||
let fn_attrs = if bx.tcx().def_kind(fx.instance.def_id()).has_codegen_attrs() {
|
||||
Some(bx.tcx().codegen_fn_attrs(fx.instance.def_id()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if !fn_abi.can_unwind {
|
||||
unwind = mir::UnwindAction::Unreachable;
|
||||
}
|
||||
|
@ -190,6 +195,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
|
|||
};
|
||||
let invokeret = bx.invoke(
|
||||
fn_ty,
|
||||
fn_attrs,
|
||||
Some(&fn_abi),
|
||||
fn_ptr,
|
||||
&llargs,
|
||||
|
@ -211,7 +217,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
|
|||
}
|
||||
MergingSucc::False
|
||||
} else {
|
||||
let llret = bx.call(fn_ty, Some(&fn_abi), fn_ptr, &llargs, self.funclet(fx));
|
||||
let llret = bx.call(fn_ty, fn_attrs, Some(&fn_abi), fn_ptr, &llargs, self.funclet(fx));
|
||||
if fx.mir[self.bb].is_cleanup {
|
||||
// Cleanup is always the cold path. Don't inline
|
||||
// drop glue. Also, when there is a deeply-nested
|
||||
|
@ -1051,48 +1057,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
self.codegen_argument(bx, location, &mut llargs, last_arg);
|
||||
}
|
||||
|
||||
let (is_indirect_call, fn_ptr) = match (llfn, instance) {
|
||||
(Some(llfn), _) => (true, llfn),
|
||||
(None, Some(instance)) => (false, bx.get_fn_addr(instance)),
|
||||
_ => span_bug!(span, "no llfn for call"),
|
||||
let fn_ptr = match (instance, llfn) {
|
||||
(Some(instance), None) => bx.get_fn_addr(instance),
|
||||
(_, Some(llfn)) => llfn,
|
||||
_ => span_bug!(span, "no instance or llfn for call"),
|
||||
};
|
||||
|
||||
// For backends that support CFI using type membership (i.e., testing whether a given
|
||||
// pointer is associated with a type identifier).
|
||||
if bx.tcx().sess.is_sanitizer_cfi_enabled() && is_indirect_call {
|
||||
// Emit type metadata and checks.
|
||||
// FIXME(rcvalle): Add support for generalized identifiers.
|
||||
// FIXME(rcvalle): Create distinct unnamed MDNodes for internal identifiers.
|
||||
let typeid = typeid_for_fnabi(bx.tcx(), fn_abi);
|
||||
let typeid_metadata = self.cx.typeid_metadata(typeid);
|
||||
|
||||
// Test whether the function pointer is associated with the type identifier.
|
||||
let cond = bx.type_test(fn_ptr, typeid_metadata);
|
||||
let bb_pass = bx.append_sibling_block("type_test.pass");
|
||||
let bb_fail = bx.append_sibling_block("type_test.fail");
|
||||
bx.cond_br(cond, bb_pass, bb_fail);
|
||||
|
||||
bx.switch_to_block(bb_pass);
|
||||
let merging_succ = helper.do_call(
|
||||
self,
|
||||
bx,
|
||||
fn_abi,
|
||||
fn_ptr,
|
||||
&llargs,
|
||||
target.as_ref().map(|&target| (ret_dest, target)),
|
||||
unwind,
|
||||
&copied_constant_arguments,
|
||||
false,
|
||||
);
|
||||
assert_eq!(merging_succ, MergingSucc::False);
|
||||
|
||||
bx.switch_to_block(bb_fail);
|
||||
bx.abort();
|
||||
bx.unreachable();
|
||||
|
||||
return MergingSucc::False;
|
||||
}
|
||||
|
||||
helper.do_call(
|
||||
self,
|
||||
bx,
|
||||
|
@ -1640,7 +1610,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, LangItem::PanicCannotUnwind);
|
||||
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
|
||||
|
||||
let llret = bx.call(fn_ty, Some(&fn_abi), fn_ptr, &[], funclet.as_ref());
|
||||
let llret = bx.call(fn_ty, None, Some(&fn_abi), fn_ptr, &[], funclet.as_ref());
|
||||
bx.do_not_inline(llret);
|
||||
|
||||
bx.unreachable();
|
||||
|
|
|
@ -694,7 +694,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
|||
let fn_ptr = bx.get_fn_addr(instance);
|
||||
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
|
||||
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
|
||||
bx.call(fn_ty, Some(fn_abi), fn_ptr, &[], None)
|
||||
let fn_attrs = if bx.tcx().def_kind(instance.def_id()).has_codegen_attrs() {
|
||||
Some(bx.tcx().codegen_fn_attrs(instance.def_id()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
bx.call(fn_ty, fn_attrs, Some(fn_abi), fn_ptr, &[], None)
|
||||
} else {
|
||||
bx.get_static(def_id)
|
||||
};
|
||||
|
|
|
@ -14,6 +14,7 @@ use crate::mir::operand::OperandRef;
|
|||
use crate::mir::place::PlaceRef;
|
||||
use crate::MemFlags;
|
||||
|
||||
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
|
||||
use rustc_middle::ty::layout::{HasParamEnv, TyAndLayout};
|
||||
use rustc_middle::ty::Ty;
|
||||
use rustc_span::Span;
|
||||
|
@ -72,6 +73,7 @@ pub trait BuilderMethods<'a, 'tcx>:
|
|||
fn invoke(
|
||||
&mut self,
|
||||
llty: Self::Type,
|
||||
fn_attrs: Option<&CodegenFnAttrs>,
|
||||
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
|
||||
llfn: Self::Value,
|
||||
args: &[Self::Value],
|
||||
|
@ -321,6 +323,7 @@ pub trait BuilderMethods<'a, 'tcx>:
|
|||
fn call(
|
||||
&mut self,
|
||||
llty: Self::Type,
|
||||
fn_attrs: Option<&CodegenFnAttrs>,
|
||||
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
|
||||
llfn: Self::Value,
|
||||
args: &[Self::Value],
|
||||
|
|
|
@ -128,12 +128,16 @@ pub trait LayoutTypeMethods<'tcx>: Backend<'tcx> {
|
|||
) -> Self::Type;
|
||||
}
|
||||
|
||||
// For backends that support CFI using type membership (i.e., testing whether a given pointer is
|
||||
// For backends that support CFI using type membership (i.e., testing whether a given pointer is
|
||||
// associated with a type identifier).
|
||||
pub trait TypeMembershipMethods<'tcx>: Backend<'tcx> {
|
||||
fn set_type_metadata(&self, function: Self::Function, typeid: String);
|
||||
fn typeid_metadata(&self, typeid: String) -> Self::Value;
|
||||
fn set_kcfi_type_metadata(&self, function: Self::Function, typeid: u32);
|
||||
fn add_type_metadata(&self, _function: Self::Function, _typeid: String) {}
|
||||
fn set_type_metadata(&self, _function: Self::Function, _typeid: String) {}
|
||||
fn typeid_metadata(&self, _typeid: String) -> Option<Self::Value> {
|
||||
None
|
||||
}
|
||||
fn add_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}
|
||||
fn set_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}
|
||||
}
|
||||
|
||||
pub trait ArgAbiMethods<'tcx>: HasCodegen<'tcx> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue