Skip to content

Commit 8f871a6

Browse files
committed
sanitizers: Add support for stable sanitizers
Add suppport for specifying stable sanitizers in addition to the existing supported sanitizers.
1 parent 46e8d20 commit 8f871a6

File tree

123 files changed

+323
-253
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+323
-253
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(crate) use rustc_middle::ty::layout::{WIDE_PTR_ADDR, WIDE_PTR_EXTRA};
1515
use rustc_middle::{bug, ty};
1616
use rustc_session::config;
1717
pub(crate) use rustc_target::callconv::*;
18-
use rustc_target::spec::SanitizerSet;
1918
use smallvec::SmallVec;
2019

2120
use crate::attributes::llfn_attrs_from_instance;
@@ -84,7 +83,7 @@ fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'
8483
attrs.push(llattr.create_attr(cx.llcx));
8584
}
8685
}
87-
} else if cx.tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
86+
} else if cx.tcx.sess.is_sanitizer_memory_enabled() {
8887
// If we're not optimising, *but* memory sanitizer is on, emit noundef, since it affects
8988
// memory sanitizer's behavior.
9089

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) fn sanitize_attrs<'ll>(
8484
no_sanitize: SanitizerSet,
8585
) -> SmallVec<[&'ll Attribute; 4]> {
8686
let mut attrs = SmallVec::new();
87-
let enabled = cx.tcx.sess.opts.unstable_opts.sanitizer - no_sanitize;
87+
let enabled = cx.tcx.sess.opts.cg.sanitize - no_sanitize;
8888
if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
8989
attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
9090
}
@@ -217,13 +217,7 @@ fn probestack_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
217217
// Currently stack probes seem somewhat incompatible with the address
218218
// sanitizer and thread sanitizer. With asan we're already protected from
219219
// stack overflow anyway so we don't really need stack probes regardless.
220-
if cx
221-
.sess()
222-
.opts
223-
.unstable_opts
224-
.sanitizer
225-
.intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD)
226-
{
220+
if cx.sess().is_sanitizer_address_enabled() || cx.sess().is_sanitizer_thread_enabled() {
227221
return None;
228222
}
229223

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ fn add_sanitizer_libraries(
13111311
return;
13121312
}
13131313

1314-
let sanitizer = sess.opts.unstable_opts.sanitizer;
1314+
let sanitizer = sess.opts.cg.sanitize;
13151315
if sanitizer.contains(SanitizerSet::ADDRESS) {
13161316
link_sanitizer_runtime(sess, flavor, linker, "asan");
13171317
}
@@ -2444,11 +2444,7 @@ fn add_order_independent_options(
24442444
&& crate_type == CrateType::Executable
24452445
&& !matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, _))
24462446
{
2447-
let prefix = if sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::ADDRESS) {
2448-
"asan/"
2449-
} else {
2450-
""
2451-
};
2447+
let prefix = if sess.is_sanitizer_address_enabled() { "asan/" } else { "" };
24522448
cmd.link_arg(format!("--dynamic-linker={prefix}ld.so.1"));
24532449
}
24542450

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::query::LocalCrate;
1313
use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Instance, SymbolName, TyCtxt};
1414
use rustc_middle::util::Providers;
1515
use rustc_session::config::{CrateType, OomStrategy};
16-
use rustc_target::spec::{SanitizerSet, TlsModel};
16+
use rustc_target::spec::TlsModel;
1717
use tracing::debug;
1818

1919
use crate::base::allocator_kind_for_codegen;
@@ -247,15 +247,15 @@ fn exported_symbols_provider_local(
247247
}));
248248
}
249249

250-
if tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
250+
if tcx.sess.is_sanitizer_memory_enabled() {
251251
let mut msan_weak_symbols = Vec::new();
252252

253253
// Similar to profiling, preserve weak msan symbol during LTO.
254-
if tcx.sess.opts.unstable_opts.sanitizer_recover.contains(SanitizerSet::MEMORY) {
254+
if tcx.sess.is_sanitizer_memory_recover_enabled() {
255255
msan_weak_symbols.push("__msan_keep_going");
256256
}
257257

258-
if tcx.sess.opts.unstable_opts.sanitizer_memory_track_origins != 0 {
258+
if tcx.sess.is_sanitizer_memory_track_origins_enabled() {
259259
msan_weak_symbols.push("__msan_track_origins");
260260
}
261261

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl ModuleConfig {
184184
debug_info_for_profiling: sess.opts.unstable_opts.debug_info_for_profiling,
185185
instrument_coverage: if_regular!(sess.instrument_coverage(), false),
186186

187-
sanitizer: if_regular!(sess.opts.unstable_opts.sanitizer, SanitizerSet::empty()),
187+
sanitizer: if_regular!(sess.opts.cg.sanitize, SanitizerSet::empty()),
188188
sanitizer_dataflow_abilist: if_regular!(
189189
sess.opts.unstable_opts.sanitizer_dataflow_abilist.clone(),
190190
Vec::new()

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ fn test_codegen_options_tracking_hash() {
634634
tracked!(profile_use, Some(PathBuf::from("abc")));
635635
tracked!(relocation_model, Some(RelocModel::Pic));
636636
tracked!(relro_level, Some(RelroLevel::Full));
637+
tracked!(sanitize, SanitizerSet::ADDRESS);
637638
tracked!(soft_float, true);
638639
tracked!(split_debuginfo, Some(SplitDebuginfo::Packed));
639640
tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
@@ -837,7 +838,6 @@ fn test_unstable_options_tracking_hash() {
837838
tracked!(regparm, Some(3));
838839
tracked!(relax_elf_relocations, Some(true));
839840
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
840-
tracked!(sanitizer, SanitizerSet::ADDRESS);
841841
tracked!(sanitizer_cfi_canonical_jump_tables, None);
842842
tracked!(sanitizer_cfi_generalize_pointers, Some(true));
843843
tracked!(sanitizer_cfi_normalize_integers, Some(true));

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn walk_native_lib_search_dirs<R>(
5757
if sess.target.vendor == "fortanix"
5858
|| sess.target.os == "linux"
5959
|| sess.target.os == "fuchsia"
60-
|| sess.target.is_like_osx && !sess.opts.unstable_opts.sanitizer.is_empty()
60+
|| sess.target.is_like_osx && !sess.opts.cg.sanitize.is_empty()
6161
{
6262
f(&sess.target_tlib_path.dir, false)?;
6363
}

compiler/rustc_session/messages.ftl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ session_branch_protection_requires_aarch64 = `-Zbranch-protection` is only suppo
33
44
session_cannot_enable_crt_static_linux = sanitizer is incompatible with statically linked libc, disable it using `-C target-feature=-crt-static`
55
6-
session_cannot_mix_and_match_sanitizers = `-Zsanitizer={$first}` is incompatible with `-Zsanitizer={$second}`
6+
session_cannot_mix_and_match_sanitizers = `-Csanitize={$first}` is incompatible with `-Csanitize={$second}`
77
88
session_cli_feature_diagnostic_help =
99
add `-Zcrate-attr="feature({$feature})"` to the command-line options to enable
@@ -90,15 +90,15 @@ session_profile_sample_use_file_does_not_exist = file `{$path}` passed to `-C pr
9090
9191
session_profile_use_file_does_not_exist = file `{$path}` passed to `-C profile-use` does not exist
9292
93-
session_sanitizer_cfi_canonical_jump_tables_requires_cfi = `-Zsanitizer-cfi-canonical-jump-tables` requires `-Zsanitizer=cfi`
93+
session_sanitizer_cfi_canonical_jump_tables_requires_cfi = `-Zsanitizer-cfi-canonical-jump-tables` requires `-Csanitize=cfi`
9494
95-
session_sanitizer_cfi_generalize_pointers_requires_cfi = `-Zsanitizer-cfi-generalize-pointers` requires `-Zsanitizer=cfi` or `-Zsanitizer=kcfi`
95+
session_sanitizer_cfi_generalize_pointers_requires_cfi = `-Zsanitizer-cfi-generalize-pointers` requires `-Csanitize=cfi` or `-Csanitize=kcfi`
9696
97-
session_sanitizer_cfi_normalize_integers_requires_cfi = `-Zsanitizer-cfi-normalize-integers` requires `-Zsanitizer=cfi` or `-Zsanitizer=kcfi`
97+
session_sanitizer_cfi_normalize_integers_requires_cfi = `-Zsanitizer-cfi-normalize-integers` requires `-Csanitize=cfi` or `-Csanitize=kcfi`
9898
99-
session_sanitizer_cfi_requires_lto = `-Zsanitizer=cfi` requires `-Clto` or `-Clinker-plugin-lto`
99+
session_sanitizer_cfi_requires_lto = `-Csanitize=cfi` requires `-Clto` or `-Clinker-plugin-lto`
100100
101-
session_sanitizer_cfi_requires_single_codegen_unit = `-Zsanitizer=cfi` with `-Clto` requires `-Ccodegen-units=1`
101+
session_sanitizer_cfi_requires_single_codegen_unit = `-Csanitize=cfi` with `-Clto` requires `-Ccodegen-units=1`
102102
103103
session_sanitizer_kcfi_requires_panic_abort = `-Z sanitizer=kcfi` requires `-C panic=abort`
104104

compiler/rustc_session/src/config/cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
210210
ins_sym!(sym::relocation_model, sess.target.relocation_model.desc_symbol());
211211
}
212212

213-
for mut s in sess.opts.unstable_opts.sanitizer {
213+
for mut s in sess.opts.cg.sanitize {
214214
// KASAN is still ASAN under the hood, so it uses the same attribute.
215215
if s == SanitizerSet::KERNELADDRESS {
216216
s = SanitizerSet::ADDRESS;

compiler/rustc_session/src/options.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -808,25 +808,14 @@ pub mod parse {
808808
}
809809

810810
pub(crate) fn parse_sanitizers(slot: &mut SanitizerSet, v: Option<&str>) -> bool {
811-
if let Some(v) = v {
812-
for s in v.split(',') {
813-
*slot |= match s {
814-
"address" => SanitizerSet::ADDRESS,
815-
"cfi" => SanitizerSet::CFI,
816-
"dataflow" => SanitizerSet::DATAFLOW,
817-
"kcfi" => SanitizerSet::KCFI,
818-
"kernel-address" => SanitizerSet::KERNELADDRESS,
819-
"leak" => SanitizerSet::LEAK,
820-
"memory" => SanitizerSet::MEMORY,
821-
"memtag" => SanitizerSet::MEMTAG,
822-
"shadow-call-stack" => SanitizerSet::SHADOWCALLSTACK,
823-
"thread" => SanitizerSet::THREAD,
824-
"hwaddress" => SanitizerSet::HWADDRESS,
825-
"safestack" => SanitizerSet::SAFESTACK,
826-
_ => return false,
827-
}
811+
if let Some(s) = v {
812+
let sanitizer_set = SanitizerSet::from_comma_list(s);
813+
if sanitizer_set.is_ok() {
814+
*slot |= sanitizer_set.unwrap();
815+
true
816+
} else {
817+
false
828818
}
829-
true
830819
} else {
831820
false
832821
}
@@ -1647,6 +1636,8 @@ options! {
16471636
"output remarks for these optimization passes (space separated, or \"all\")"),
16481637
rpath: bool = (false, parse_bool, [UNTRACKED],
16491638
"set rpath values in libs/exes (default: no)"),
1639+
sanitize: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED],
1640+
"use one or multiple sanitizers"),
16501641
save_temps: bool = (false, parse_bool, [UNTRACKED],
16511642
"save all temporary output files during compilation (default: no)"),
16521643
soft_float: bool = (false, parse_bool, [TRACKED],
@@ -2017,8 +2008,6 @@ options! {
20172008
remark_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
20182009
"directory into which to write optimization remarks (if not specified, they will be \
20192010
written to standard error output)"),
2020-
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED],
2021-
"use a sanitizer"),
20222011
sanitizer_cfi_canonical_jump_tables: Option<bool> = (Some(true), parse_opt_bool, [TRACKED],
20232012
"enable canonical jump tables (default: yes)"),
20242013
sanitizer_cfi_generalize_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],

compiler/rustc_session/src/session.rs

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,12 @@ impl Session {
375375
self.opts.unstable_opts.coverage_options.no_mir_spans
376376
}
377377

378+
pub fn is_sanitizer_address_enabled(&self) -> bool {
379+
self.opts.cg.sanitize.contains(SanitizerSet::ADDRESS)
380+
}
381+
378382
pub fn is_sanitizer_cfi_enabled(&self) -> bool {
379-
self.opts.unstable_opts.sanitizer.contains(SanitizerSet::CFI)
383+
self.opts.cg.sanitize.contains(SanitizerSet::CFI)
380384
}
381385

382386
pub fn is_sanitizer_cfi_canonical_jump_tables_disabled(&self) -> bool {
@@ -395,8 +399,32 @@ impl Session {
395399
self.opts.unstable_opts.sanitizer_cfi_normalize_integers == Some(true)
396400
}
397401

402+
pub fn is_sanitizer_hwaddress_enabled(&self) -> bool {
403+
self.opts.cg.sanitize.contains(SanitizerSet::HWADDRESS)
404+
}
405+
398406
pub fn is_sanitizer_kcfi_enabled(&self) -> bool {
399-
self.opts.unstable_opts.sanitizer.contains(SanitizerSet::KCFI)
407+
self.opts.cg.sanitize.contains(SanitizerSet::KCFI)
408+
}
409+
410+
pub fn is_sanitizer_kernel_address_enabled(&self) -> bool {
411+
self.opts.cg.sanitize.contains(SanitizerSet::KERNELADDRESS)
412+
}
413+
414+
pub fn is_sanitizer_memory_enabled(&self) -> bool {
415+
self.opts.cg.sanitize.contains(SanitizerSet::MEMORY)
416+
}
417+
418+
pub fn is_sanitizer_memory_recover_enabled(&self) -> bool {
419+
self.opts.unstable_opts.sanitizer_recover.contains(SanitizerSet::MEMORY)
420+
}
421+
422+
pub fn is_sanitizer_memory_track_origins_enabled(&self) -> bool {
423+
self.opts.unstable_opts.sanitizer_memory_track_origins != 0
424+
}
425+
426+
pub fn is_sanitizer_thread_enabled(&self) -> bool {
427+
self.opts.cg.sanitize.contains(SanitizerSet::THREAD)
400428
}
401429

402430
pub fn is_split_lto_unit_enabled(&self) -> bool {
@@ -613,7 +641,10 @@ impl Session {
613641
// AddressSanitizer and KernelAddressSanitizer uses lifetimes to detect use after scope bugs.
614642
// MemorySanitizer uses lifetimes to detect use of uninitialized stack variables.
615643
// HWAddressSanitizer will use lifetimes to detect use after scope bugs in the future.
616-
|| self.opts.unstable_opts.sanitizer.intersects(SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS | SanitizerSet::MEMORY | SanitizerSet::HWADDRESS)
644+
|| self.is_sanitizer_address_enabled()
645+
|| self.is_sanitizer_kernel_address_enabled()
646+
|| self.is_sanitizer_memory_enabled()
647+
|| self.is_sanitizer_hwaddress_enabled()
617648
}
618649

619650
pub fn diagnostic_width(&self) -> usize {
@@ -741,7 +772,7 @@ impl Session {
741772
let more_names = self.opts.output_types.contains_key(&OutputType::LlvmAssembly)
742773
|| self.opts.output_types.contains_key(&OutputType::Bitcode)
743774
// AddressSanitizer and MemorySanitizer use alloca name when reporting an issue.
744-
|| self.opts.unstable_opts.sanitizer.intersects(SanitizerSet::ADDRESS | SanitizerSet::MEMORY);
775+
|| self.is_sanitizer_address_enabled() || self.is_sanitizer_memory_enabled();
745776
!more_names
746777
}
747778
}
@@ -1196,14 +1227,19 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
11961227
}
11971228
}
11981229

1199-
// Sanitizers can only be used on platforms that we know have working sanitizer codegen.
1200-
let supported_sanitizers = sess.target.options.supported_sanitizers;
1201-
let mut unsupported_sanitizers = sess.opts.unstable_opts.sanitizer - supported_sanitizers;
1230+
let supported_sanitizers = if sess.unstable_options() {
1231+
sess.target.options.supported_sanitizers | sess.target.options.stable_sanitizers
1232+
} else {
1233+
sess.target.options.stable_sanitizers
1234+
};
1235+
let mut unsupported_sanitizers = sess.opts.cg.sanitize - supported_sanitizers;
1236+
12021237
// Niche: if `fixed-x18`, or effectively switching on `reserved-x18` flag, is enabled
12031238
// we should allow Shadow Call Stack sanitizer.
12041239
if sess.opts.unstable_opts.fixed_x18 && sess.target.arch == "aarch64" {
12051240
unsupported_sanitizers -= SanitizerSet::SHADOWCALLSTACK;
12061241
}
1242+
12071243
match unsupported_sanitizers.into_iter().count() {
12081244
0 => {}
12091245
1 => {
@@ -1218,18 +1254,15 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
12181254
}
12191255

12201256
// Cannot mix and match mutually-exclusive sanitizers.
1221-
if let Some((first, second)) = sess.opts.unstable_opts.sanitizer.mutually_exclusive() {
1257+
if let Some((first, second)) = sess.opts.cg.sanitize.mutually_exclusive() {
12221258
sess.dcx().emit_err(errors::CannotMixAndMatchSanitizers {
12231259
first: first.to_string(),
12241260
second: second.to_string(),
12251261
});
12261262
}
12271263

12281264
// Cannot enable crt-static with sanitizers on Linux
1229-
if sess.crt_static(None)
1230-
&& !sess.opts.unstable_opts.sanitizer.is_empty()
1231-
&& !sess.target.is_like_msvc
1232-
{
1265+
if sess.crt_static(None) && !sess.opts.cg.sanitize.is_empty() && !sess.target.is_like_msvc {
12331266
sess.dcx().emit_err(errors::CannotEnableCrtStaticLinux);
12341267
}
12351268

0 commit comments

Comments
 (0)