Skip to content

Commit bb34360

Browse files
committed
codegen, symbol_mangling: prefix/move LLVM CFI functionality to avoid confusion.
1 parent 727812e commit bb34360

File tree

7 files changed

+51
-52
lines changed

7 files changed

+51
-52
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,11 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
783783
// TODO(antoyo)
784784
}
785785

786-
fn type_metadata(&mut self, _function: RValue<'gcc>, _typeid: String) {
786+
fn llvm_cfi_type_metadata(&mut self, _function: RValue<'gcc>, _typeid: String) {
787787
// Unsupported.
788788
}
789789

790-
fn typeid_metadata(&mut self, _typeid: String) -> RValue<'gcc> {
790+
fn llvm_cfi_typeid_metadata(&mut self, _typeid: String) -> RValue<'gcc> {
791791
// Unsupported.
792792
self.context.new_rvalue_from_int(self.int_type, 0)
793793
}

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
621621
}
622622
}
623623

624-
fn type_metadata(&mut self, function: &'ll Value, typeid: String) {
625-
let typeid_metadata = self.typeid_metadata(typeid);
624+
// FIXME(eddyb) this does not belong in `Builder`, it's global.
625+
fn llvm_cfi_type_metadata(&mut self, function: &'ll Value, typeid: String) {
626+
let typeid_metadata = self.llvm_cfi_typeid_metadata(typeid);
626627
let v = [self.const_usize(0), typeid_metadata];
627628
unsafe {
628629
llvm::LLVMGlobalSetMetadata(
@@ -637,7 +638,8 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
637638
}
638639
}
639640

640-
fn typeid_metadata(&mut self, typeid: String) -> Self::Value {
641+
// FIXME(eddyb) this does not belong in `Builder`, it's global.
642+
fn llvm_cfi_typeid_metadata(&mut self, typeid: String) -> Self::Value {
641643
unsafe {
642644
llvm::LLVMMDStringInContext(
643645
self.cx.llcx,

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
2020
use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
2121
use rustc_span::source_map::Span;
2222
use rustc_span::{sym, Symbol};
23-
use rustc_symbol_mangling::typeid_for_fnabi;
23+
use rustc_symbol_mangling::llvm_cfi_typeid_for_fn_abi;
2424
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
2525
use rustc_target::abi::{self, HasDataLayout, WrappingRange};
2626
use rustc_target::spec::abi::Abi;
@@ -908,8 +908,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
908908
// Emit type metadata and checks.
909909
// FIXME(rcvalle): Add support for generalized identifiers.
910910
// FIXME(rcvalle): Create distinct unnamed MDNodes for internal identifiers.
911-
let typeid = typeid_for_fnabi(bx.tcx(), fn_abi);
912-
let typeid_metadata = bx.typeid_metadata(typeid);
911+
let typeid = llvm_cfi_typeid_for_fn_abi(bx.tcx(), fn_abi);
912+
let typeid_metadata = bx.llvm_cfi_typeid_metadata(typeid);
913913

914914
// Test whether the function pointer is associated with the type identifier.
915915
let cond = bx.type_test(fn_ptr, typeid_metadata);

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::mir;
33
use rustc_middle::mir::interpret::ErrorHandled;
44
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
55
use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
6-
use rustc_symbol_mangling::typeid_for_fnabi;
6+
use rustc_symbol_mangling::llvm_cfi_typeid_for_fn_abi;
77
use rustc_target::abi::call::{FnAbi, PassMode};
88

99
use std::iter;
@@ -252,8 +252,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
252252
// For backends that support CFI using type membership (i.e., testing whether a given pointer
253253
// is associated with a type identifier).
254254
if cx.tcx().sess.is_sanitizer_cfi_enabled() {
255-
let typeid = typeid_for_fnabi(cx.tcx(), fn_abi);
256-
bx.type_metadata(llfn, typeid);
255+
let typeid = llvm_cfi_typeid_for_fn_abi(cx.tcx(), fn_abi);
256+
bx.llvm_cfi_type_metadata(llfn, typeid);
257257
}
258258
}
259259

compiler/rustc_codegen_ssa/src/traits/builder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ pub trait BuilderMethods<'a, 'tcx>:
160160

161161
fn range_metadata(&mut self, load: Self::Value, range: WrappingRange);
162162
fn nonnull_metadata(&mut self, load: Self::Value);
163-
fn type_metadata(&mut self, function: Self::Function, typeid: String);
164-
fn typeid_metadata(&mut self, typeid: String) -> Self::Value;
163+
164+
// FIXME(eddyb) these do not belong in `Builder`, they're global.
165+
fn llvm_cfi_type_metadata(&mut self, function: Self::Function, typeid: String);
166+
fn llvm_cfi_typeid_metadata(&mut self, typeid: String) -> Self::Value;
165167

166168
fn store(&mut self, val: Self::Value, ptr: Self::Value, align: Align) -> Self::Value;
167169
fn store_with_flags(

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,40 @@ fn symbol_name_provider<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty
150150
ty::SymbolName::new(tcx, &symbol_name)
151151
}
152152

153-
/// This function computes the typeid for the given function ABI.
154-
pub fn typeid_for_fnabi<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> String {
155-
v0::mangle_typeid_for_fnabi(tcx, fn_abi)
153+
/// This function computes the LLVM CFI typeid for the given `FnAbi`.
154+
pub fn llvm_cfi_typeid_for_fn_abi<'tcx>(
155+
_tcx: TyCtxt<'tcx>,
156+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
157+
) -> String {
158+
// LLVM uses type metadata to allow IR modules to aggregate pointers by their types.[1] This
159+
// type metadata is used by LLVM Control Flow Integrity to test whether a given pointer is
160+
// associated with a type identifier (i.e., test type membership).
161+
//
162+
// Clang uses the Itanium C++ ABI's[2] virtual tables and RTTI typeinfo structure name[3] as
163+
// type metadata identifiers for function pointers. The typeinfo name encoding is a
164+
// two-character code (i.e., “TS”) prefixed to the type encoding for the function.
165+
//
166+
// For cross-language LLVM CFI support, a compatible encoding must be used by either
167+
//
168+
// a. Using a superset of types that encompasses types used by Clang (i.e., Itanium C++ ABI's
169+
// type encodings[4]), or at least types used at the FFI boundary.
170+
// b. Reducing the types to the least common denominator between types used by Clang (or at
171+
// least types used at the FFI boundary) and Rust compilers (if even possible).
172+
// c. Creating a new ABI for cross-language CFI and using it for Clang and Rust compilers (and
173+
// possibly other compilers).
174+
//
175+
// Option (b) may weaken the protection for Rust-compiled only code, so it should be provided
176+
// as an alternative to a Rust-specific encoding for when mixing Rust and C and C++ -compiled
177+
// code. Option (c) would require changes to Clang to use the new ABI.
178+
//
179+
// [1] https://llvm.org/docs/TypeMetadata.html
180+
// [2] https://itanium-cxx-abi.github.io/cxx-abi/abi.html
181+
// [3] https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-special-vtables
182+
// [4] https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-type
183+
//
184+
// FIXME(rcvalle): See comment above.
185+
let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize;
186+
format!("typeid{}", arg_count)
156187
}
157188

158189
/// Computes the symbol name for the given instance. This function will call

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_middle::ty::print::{Print, Printer};
1111
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
1212
use rustc_middle::ty::{self, FloatTy, Instance, IntTy, Ty, TyCtxt, TypeFoldable, UintTy};
1313
use rustc_span::symbol::kw;
14-
use rustc_target::abi::call::FnAbi;
1514
use rustc_target::abi::Integer;
1615
use rustc_target::spec::abi::Abi;
1716

@@ -58,41 +57,6 @@ pub(super) fn mangle<'tcx>(
5857
std::mem::take(&mut cx.out)
5958
}
6059

61-
pub(super) fn mangle_typeid_for_fnabi<'tcx>(
62-
_tcx: TyCtxt<'tcx>,
63-
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
64-
) -> String {
65-
// LLVM uses type metadata to allow IR modules to aggregate pointers by their types.[1] This
66-
// type metadata is used by LLVM Control Flow Integrity to test whether a given pointer is
67-
// associated with a type identifier (i.e., test type membership).
68-
//
69-
// Clang uses the Itanium C++ ABI's[2] virtual tables and RTTI typeinfo structure name[3] as
70-
// type metadata identifiers for function pointers. The typeinfo name encoding is a
71-
// two-character code (i.e., “TS”) prefixed to the type encoding for the function.
72-
//
73-
// For cross-language LLVM CFI support, a compatible encoding must be used by either
74-
//
75-
// a. Using a superset of types that encompasses types used by Clang (i.e., Itanium C++ ABI's
76-
// type encodings[4]), or at least types used at the FFI boundary.
77-
// b. Reducing the types to the least common denominator between types used by Clang (or at
78-
// least types used at the FFI boundary) and Rust compilers (if even possible).
79-
// c. Creating a new ABI for cross-language CFI and using it for Clang and Rust compilers (and
80-
// possibly other compilers).
81-
//
82-
// Option (b) may weaken the protection for Rust-compiled only code, so it should be provided
83-
// as an alternative to a Rust-specific encoding for when mixing Rust and C and C++ -compiled
84-
// code. Option (c) would require changes to Clang to use the new ABI.
85-
//
86-
// [1] https://llvm.org/docs/TypeMetadata.html
87-
// [2] https://itanium-cxx-abi.github.io/cxx-abi/abi.html
88-
// [3] https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-special-vtables
89-
// [4] https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-type
90-
//
91-
// FIXME(rcvalle): See comment above.
92-
let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize;
93-
format!("typeid{}", arg_count)
94-
}
95-
9660
struct BinderLevel {
9761
/// The range of distances from the root of what's
9862
/// being printed, to the lifetimes in a binder.

0 commit comments

Comments
 (0)