Skip to content

Commit 6933be5

Browse files
authored
Rollup merge of rust-lang#105452 - rcvalle:rust-cfi-3, r=bjorn3
Add cross-language LLVM CFI support to the Rust compiler This PR 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 rust-lang#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). Thank you again, ``@bjorn3,`` ``@nikic,`` ``@samitolvanen,`` and the Rust community for all the help!
2 parents 7a48ba9 + 0d183f8 commit 6933be5

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
501501
if options.contains(InlineAsmOptions::NORETURN) {
502502
let builtin_unreachable = self.context.get_builtin_function("__builtin_unreachable");
503503
let builtin_unreachable: RValue<'gcc> = unsafe { std::mem::transmute(builtin_unreachable) };
504-
self.call(self.type_void(), None, builtin_unreachable, &[], None);
504+
self.call(self.type_void(), None, None, builtin_unreachable, &[], None);
505505
}
506506

507507
// Write results to outputs.

src/builder.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_codegen_ssa::traits::{
3535
};
3636
use rustc_data_structures::fx::FxHashSet;
3737
use rustc_middle::bug;
38+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
3839
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
3940
use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
4041
use rustc_span::Span;
@@ -455,12 +456,12 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
455456
}
456457

457458
#[cfg(feature="master")]
458-
fn invoke(&mut self, typ: Type<'gcc>, _fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
459+
fn invoke(&mut self, typ: Type<'gcc>, fn_attrs: Option<&CodegenFnAttrs>, _fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
459460
let try_block = self.current_func().new_block("try");
460461

461462
let current_block = self.block.clone();
462463
self.block = try_block;
463-
let call = self.call(typ, None, func, args, None); // TODO(antoyo): use funclet here?
464+
let call = self.call(typ, fn_attrs, None, func, args, None); // TODO(antoyo): use funclet here?
464465
self.block = current_block;
465466

466467
let return_value = self.current_func()
@@ -483,8 +484,8 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
483484
}
484485

485486
#[cfg(not(feature="master"))]
486-
fn invoke(&mut self, typ: Type<'gcc>, fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
487-
let call_site = self.call(typ, None, func, args, None);
487+
fn invoke(&mut self, typ: Type<'gcc>, fn_attrs: &CodegenFnAttrs, fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
488+
let call_site = self.call(typ, fn_attrs, None, func, args, None);
488489
let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
489490
self.llbb().end_with_conditional(None, condition, then, catch);
490491
if let Some(_fn_abi) = fn_abi {
@@ -1351,6 +1352,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
13511352
fn call(
13521353
&mut self,
13531354
_typ: Type<'gcc>,
1355+
_fn_attrs: Option<&CodegenFnAttrs>,
13541356
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
13551357
func: RValue<'gcc>,
13561358
args: &[RValue<'gcc>],

src/intrinsic/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
113113
_ if simple.is_some() => {
114114
// FIXME(antoyo): remove this cast when the API supports function.
115115
let func = unsafe { std::mem::transmute(simple.expect("simple")) };
116-
self.call(self.type_void(), None, func, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None)
116+
self.call(self.type_void(), None, None, func, &args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(), None)
117117
},
118118
sym::likely => {
119119
self.expect(args[0].immediate(), true)
@@ -326,7 +326,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
326326
let masked = self.and(addr, mask);
327327
self.bitcast(masked, void_ptr_type)
328328
},
329-
329+
330330
_ if name_str.starts_with("simd_") => {
331331
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
332332
Ok(llval) => llval,
@@ -354,7 +354,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
354354
fn abort(&mut self) {
355355
let func = self.context.get_builtin_function("abort");
356356
let func: RValue<'gcc> = unsafe { std::mem::transmute(func) };
357-
self.call(self.type_void(), None, func, &[], None);
357+
self.call(self.type_void(), None, None, func, &[], None);
358358
}
359359

360360
fn assume(&mut self, value: Self::Value) {
@@ -1135,7 +1135,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
11351135

11361136
fn try_intrinsic<'a, 'b, 'gcc, 'tcx>(bx: &'b mut Builder<'a, 'gcc, 'tcx>, try_func: RValue<'gcc>, data: RValue<'gcc>, _catch_func: RValue<'gcc>, dest: RValue<'gcc>) {
11371137
if bx.sess().panic_strategy() == PanicStrategy::Abort {
1138-
bx.call(bx.type_void(), None, try_func, &[data], None);
1138+
bx.call(bx.type_void(), None, None, try_func, &[data], None);
11391139
// Return 0 unconditionally from the intrinsic call;
11401140
// we can never unwind.
11411141
let ret_align = bx.tcx.data_layout.i32_align.abi;
@@ -1204,21 +1204,21 @@ fn codegen_gnu_try<'gcc>(bx: &mut Builder<'_, 'gcc, '_>, try_func: RValue<'gcc>,
12041204
let zero = bx.cx.context.new_rvalue_zero(bx.int_type);
12051205
let ptr = bx.cx.context.new_call(None, eh_pointer_builtin, &[zero]);
12061206
let catch_ty = bx.type_func(&[bx.type_i8p(), bx.type_i8p()], bx.type_void());
1207-
bx.call(catch_ty, None, catch_func, &[data, ptr], None);
1207+
bx.call(catch_ty, None, None, catch_func, &[data, ptr], None);
12081208
bx.ret(bx.const_i32(1));
12091209

12101210
// NOTE: the blocks must be filled before adding the try/catch, otherwise gcc will not
12111211
// generate a try/catch.
12121212
// FIXME(antoyo): add a check in the libgccjit API to prevent this.
12131213
bx.switch_to_block(current_block);
1214-
bx.invoke(try_func_ty, None, try_func, &[data], then, catch, None);
1214+
bx.invoke(try_func_ty, None, None, try_func, &[data], then, catch, None);
12151215
});
12161216

12171217
let func = unsafe { std::mem::transmute(func) };
12181218

12191219
// Note that no invoke is used here because by definition this function
12201220
// can't panic (that's what it's catching).
1221-
let ret = bx.call(llty, None, func, &[try_func, data, catch_func], None);
1221+
let ret = bx.call(llty, None, None, func, &[try_func, data, catch_func], None);
12221222
let i32_align = bx.tcx().data_layout.i32_align.abi;
12231223
bx.store(ret, dest, i32_align);
12241224
}

src/type_.rs

-12
Original file line numberDiff line numberDiff line change
@@ -280,16 +280,4 @@ pub fn struct_fields<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout
280280
}
281281

282282
impl<'gcc, 'tcx> TypeMembershipMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
283-
fn set_type_metadata(&self, _function: RValue<'gcc>, _typeid: String) {
284-
// Unsupported.
285-
}
286-
287-
fn typeid_metadata(&self, _typeid: String) -> RValue<'gcc> {
288-
// Unsupported.
289-
self.context.new_rvalue_from_int(self.int_type, 0)
290-
}
291-
292-
fn set_kcfi_type_metadata(&self, _function: RValue<'gcc>, _kcfi_typeid: u32) {
293-
// Unsupported.
294-
}
295283
}

0 commit comments

Comments
 (0)