Skip to content

Commit 1f0f2c4

Browse files
rcvalledavidtwco
andcommitted
sanitizers: Create the rustc_sanitizers crate
Create the rustc_sanitizers crate and move the source code for the CFI and KCFI sanitizers to it. Co-authored-by: David Wood <[email protected]>
1 parent 537aab7 commit 1f0f2c4

File tree

17 files changed

+915
-784
lines changed

17 files changed

+915
-784
lines changed

Diff for: Cargo.lock

+16-3
Original file line numberDiff line numberDiff line change
@@ -3670,6 +3670,7 @@ dependencies = [
36703670
"rustc_metadata",
36713671
"rustc_middle",
36723672
"rustc_query_system",
3673+
"rustc_sanitizers",
36733674
"rustc_session",
36743675
"rustc_span",
36753676
"rustc_symbol_mangling",
@@ -4558,6 +4559,21 @@ dependencies = [
45584559
"tracing",
45594560
]
45604561

4562+
[[package]]
4563+
name = "rustc_sanitizers"
4564+
version = "0.0.0"
4565+
dependencies = [
4566+
"bitflags 2.5.0",
4567+
"rustc_data_structures",
4568+
"rustc_hir",
4569+
"rustc_middle",
4570+
"rustc_span",
4571+
"rustc_target",
4572+
"rustc_trait_selection",
4573+
"tracing",
4574+
"twox-hash",
4575+
]
4576+
45614577
[[package]]
45624578
name = "rustc_serialize"
45634579
version = "0.0.0"
@@ -4633,7 +4649,6 @@ dependencies = [
46334649
name = "rustc_symbol_mangling"
46344650
version = "0.0.0"
46354651
dependencies = [
4636-
"bitflags 2.5.0",
46374652
"punycode",
46384653
"rustc-demangle",
46394654
"rustc_data_structures",
@@ -4643,9 +4658,7 @@ dependencies = [
46434658
"rustc_session",
46444659
"rustc_span",
46454660
"rustc_target",
4646-
"rustc_trait_selection",
46474661
"tracing",
4648-
"twox-hash",
46494662
]
46504663

46514664
[[package]]

Diff for: compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rustc_macros = { path = "../rustc_macros" }
2828
rustc_metadata = { path = "../rustc_metadata" }
2929
rustc_middle = { path = "../rustc_middle" }
3030
rustc_query_system = { path = "../rustc_query_system" }
31+
rustc_sanitizers = { path = "../rustc_sanitizers" }
3132
rustc_session = { path = "../rustc_session" }
3233
rustc_span = { path = "../rustc_span" }
3334
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }

Diff for: compiler/rustc_codegen_llvm/src/builder.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ use rustc_middle::ty::layout::{
2020
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
2121
};
2222
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
23+
use rustc_sanitizers::{cfi, kcfi};
2324
use rustc_session::config::OptLevel;
2425
use rustc_span::Span;
25-
use rustc_symbol_mangling::typeid::{
26-
kcfi_typeid_for_fnabi, kcfi_typeid_for_instance, typeid_for_fnabi, typeid_for_instance,
27-
TypeIdOptions,
28-
};
2926
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
3027
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
3128
use smallvec::SmallVec;
@@ -1632,18 +1629,18 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16321629
return;
16331630
}
16341631

1635-
let mut options = TypeIdOptions::empty();
1632+
let mut options = cfi::TypeIdOptions::empty();
16361633
if self.tcx.sess.is_sanitizer_cfi_generalize_pointers_enabled() {
1637-
options.insert(TypeIdOptions::GENERALIZE_POINTERS);
1634+
options.insert(cfi::TypeIdOptions::GENERALIZE_POINTERS);
16381635
}
16391636
if self.tcx.sess.is_sanitizer_cfi_normalize_integers_enabled() {
1640-
options.insert(TypeIdOptions::NORMALIZE_INTEGERS);
1637+
options.insert(cfi::TypeIdOptions::NORMALIZE_INTEGERS);
16411638
}
16421639

16431640
let typeid = if let Some(instance) = instance {
1644-
typeid_for_instance(self.tcx, instance, options)
1641+
cfi::typeid_for_instance(self.tcx, instance, options)
16451642
} else {
1646-
typeid_for_fnabi(self.tcx, fn_abi, options)
1643+
cfi::typeid_for_fnabi(self.tcx, fn_abi, options)
16471644
};
16481645
let typeid_metadata = self.cx.typeid_metadata(typeid).unwrap();
16491646

@@ -1680,18 +1677,18 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16801677
return None;
16811678
}
16821679

1683-
let mut options = TypeIdOptions::empty();
1680+
let mut options = kcfi::TypeIdOptions::empty();
16841681
if self.tcx.sess.is_sanitizer_cfi_generalize_pointers_enabled() {
1685-
options.insert(TypeIdOptions::GENERALIZE_POINTERS);
1682+
options.insert(kcfi::TypeIdOptions::GENERALIZE_POINTERS);
16861683
}
16871684
if self.tcx.sess.is_sanitizer_cfi_normalize_integers_enabled() {
1688-
options.insert(TypeIdOptions::NORMALIZE_INTEGERS);
1685+
options.insert(kcfi::TypeIdOptions::NORMALIZE_INTEGERS);
16891686
}
16901687

16911688
let kcfi_typeid = if let Some(instance) = instance {
1692-
kcfi_typeid_for_instance(self.tcx, instance, options)
1689+
kcfi::typeid_for_instance(self.tcx, instance, options)
16931690
} else {
1694-
kcfi_typeid_for_fnabi(self.tcx, fn_abi, options)
1691+
kcfi::typeid_for_fnabi(self.tcx, fn_abi, options)
16951692
};
16961693

16971694
Some(llvm::OperandBundleDef::new("kcfi", &[self.const_u32(kcfi_typeid)]))

Diff for: compiler/rustc_codegen_llvm/src/declare.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ use itertools::Itertools;
2222
use rustc_codegen_ssa::traits::TypeMembershipMethods;
2323
use rustc_data_structures::fx::FxIndexSet;
2424
use rustc_middle::ty::{Instance, Ty};
25-
use rustc_symbol_mangling::typeid::{
26-
kcfi_typeid_for_fnabi, kcfi_typeid_for_instance, typeid_for_fnabi, typeid_for_instance,
27-
TypeIdOptions,
28-
};
25+
use rustc_sanitizers::{cfi, kcfi};
2926
use smallvec::SmallVec;
3027

3128
/// Declare a function.
@@ -145,47 +142,49 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
145142
if let Some(instance) = instance {
146143
let mut typeids = FxIndexSet::default();
147144
for options in [
148-
TypeIdOptions::GENERALIZE_POINTERS,
149-
TypeIdOptions::NORMALIZE_INTEGERS,
150-
TypeIdOptions::USE_CONCRETE_SELF,
145+
cfi::TypeIdOptions::GENERALIZE_POINTERS,
146+
cfi::TypeIdOptions::NORMALIZE_INTEGERS,
147+
cfi::TypeIdOptions::USE_CONCRETE_SELF,
151148
]
152149
.into_iter()
153150
.powerset()
154-
.map(TypeIdOptions::from_iter)
151+
.map(cfi::TypeIdOptions::from_iter)
155152
{
156-
let typeid = typeid_for_instance(self.tcx, instance, options);
153+
let typeid = cfi::typeid_for_instance(self.tcx, instance, options);
157154
if typeids.insert(typeid.clone()) {
158155
self.add_type_metadata(llfn, typeid);
159156
}
160157
}
161158
} else {
162-
for options in
163-
[TypeIdOptions::GENERALIZE_POINTERS, TypeIdOptions::NORMALIZE_INTEGERS]
164-
.into_iter()
165-
.powerset()
166-
.map(TypeIdOptions::from_iter)
159+
for options in [
160+
cfi::TypeIdOptions::GENERALIZE_POINTERS,
161+
cfi::TypeIdOptions::NORMALIZE_INTEGERS,
162+
]
163+
.into_iter()
164+
.powerset()
165+
.map(cfi::TypeIdOptions::from_iter)
167166
{
168-
let typeid = typeid_for_fnabi(self.tcx, fn_abi, options);
167+
let typeid = cfi::typeid_for_fnabi(self.tcx, fn_abi, options);
169168
self.add_type_metadata(llfn, typeid);
170169
}
171170
}
172171
}
173172

174173
if self.tcx.sess.is_sanitizer_kcfi_enabled() {
175174
// LLVM KCFI does not support multiple !kcfi_type attachments
176-
let mut options = TypeIdOptions::empty();
175+
let mut options = kcfi::TypeIdOptions::empty();
177176
if self.tcx.sess.is_sanitizer_cfi_generalize_pointers_enabled() {
178-
options.insert(TypeIdOptions::GENERALIZE_POINTERS);
177+
options.insert(kcfi::TypeIdOptions::GENERALIZE_POINTERS);
179178
}
180179
if self.tcx.sess.is_sanitizer_cfi_normalize_integers_enabled() {
181-
options.insert(TypeIdOptions::NORMALIZE_INTEGERS);
180+
options.insert(kcfi::TypeIdOptions::NORMALIZE_INTEGERS);
182181
}
183182

184183
if let Some(instance) = instance {
185-
let kcfi_typeid = kcfi_typeid_for_instance(self.tcx, instance, options);
184+
let kcfi_typeid = kcfi::typeid_for_instance(self.tcx, instance, options);
186185
self.set_kcfi_type_metadata(llfn, kcfi_typeid);
187186
} else {
188-
let kcfi_typeid = kcfi_typeid_for_fnabi(self.tcx, fn_abi, options);
187+
let kcfi_typeid = kcfi::typeid_for_fnabi(self.tcx, fn_abi, options);
189188
self.set_kcfi_type_metadata(llfn, kcfi_typeid);
190189
}
191190
}

Diff for: compiler/rustc_sanitizers/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "rustc_sanitizers"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
bitflags = "2.5.0"
8+
tracing = "0.1"
9+
twox-hash = "1.6.3"
10+
rustc_data_structures = { path = "../rustc_data_structures" }
11+
rustc_hir = { path = "../rustc_hir" }
12+
rustc_middle = { path = "../rustc_middle" }
13+
rustc_span = { path = "../rustc_span" }
14+
rustc_target = { path = "../rustc_target" }
15+
rustc_trait_selection = { path = "../rustc_trait_selection" }

Diff for: compiler/rustc_sanitizers/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The `rustc_sanitizers` crate contains the source code for providing support for
2+
the [sanitizers](https://github.com/google/sanitizers) to the Rust compiler.

Diff for: compiler/rustc_sanitizers/src/cfi/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! LLVM Control Flow Integrity (CFI) and cross-language LLVM CFI support for the Rust compiler.
2+
//!
3+
//! For more information about LLVM CFI and cross-language LLVM CFI support for the Rust compiler,
4+
//! see design document in the tracking issue #89653.
5+
pub mod typeid;
6+
pub use crate::cfi::typeid::{typeid_for_fnabi, typeid_for_instance, TypeIdOptions};

0 commit comments

Comments
 (0)