Skip to content

Commit 2fa7a35

Browse files
committed
Add default sanitizers to TargetOptions
Some sanitizers are part of a system's ABI, like the shadow call stack on Aarch64 and RISC-V Fuchsia. Typically ABI options have other spellings, but LLVM has, for historical reasons, marked this as a sanitizer instead of an alternate ABI option. As a result, Fuchsia targets may not be compiled against the correct ABI unless this option is set. This hasn't caused correctness problems, since the backend reserves the SCS register, and thus preserves its value. But this is an issue for unwinding, as the SCS will not be an array of PCs describing the call complete call chain, and will have gaps from callers that don't use the correct ABI. In the long term, I'd like to see all the sanitizer configs that all frontends copy from clang moved into llvm's libFrontend, and exposed so that frontend consumers can use a small set of simple APIs to use sanitizers in a consistent way across the LLVM ecosystem, but that work is not yet ready today.
1 parent 40311c4 commit 2fa7a35

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ pub(crate) fn sanitize_attrs<'ll>(
101101
no_sanitize: SanitizerSet,
102102
) -> SmallVec<[&'ll Attribute; 4]> {
103103
let mut attrs = SmallVec::new();
104-
let enabled = cx.tcx.sess.opts.unstable_opts.sanitizer - no_sanitize;
104+
let enabled = (cx.sess().target.default_sanitizers | cx.tcx.sess.opts.unstable_opts.sanitizer)
105+
- no_sanitize;
105106
if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
106107
attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
107108
}

compiler/rustc_target/src/spec/json.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ impl Target {
656656
key!(split_debuginfo, SplitDebuginfo)?;
657657
key!(supported_split_debuginfo, fallible_list)?;
658658
key!(supported_sanitizers, SanitizerSet)?;
659+
key!(default_sanitizers, SanitizerSet)?;
659660
key!(generate_arange_section, bool);
660661
key!(supports_stack_protector, bool);
661662
key!(small_data_threshold_support, SmallDataThresholdSupport)?;
@@ -835,6 +836,7 @@ impl ToJson for Target {
835836
target_option_val!(split_debuginfo);
836837
target_option_val!(supported_split_debuginfo);
837838
target_option_val!(supported_sanitizers);
839+
target_option_val!(default_sanitizers);
838840
target_option_val!(c_enum_min_bits);
839841
target_option_val!(generate_arange_section);
840842
target_option_val!(supports_stack_protector);

compiler/rustc_target/src/spec/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,13 @@ pub struct TargetOptions {
26412641
/// distributed with the target, the sanitizer should still appear in this list for the target.
26422642
pub supported_sanitizers: SanitizerSet,
26432643

2644+
/// The sanitizers that are enabled by default on this target.
2645+
///
2646+
/// Note that the support here is at a codegen level. If the machine code with sanitizer
2647+
/// enabled can generated on this target, but the necessary supporting libraries are not
2648+
/// distributed with the target, the sanitizer should still appear in this list for the target.
2649+
pub default_sanitizers: SanitizerSet,
2650+
26442651
/// Minimum number of bits in #[repr(C)] enum. Defaults to the size of c_int
26452652
pub c_enum_min_bits: Option<u64>,
26462653

@@ -2884,6 +2891,7 @@ impl Default for TargetOptions {
28842891
// `Off` is supported by default, but targets can remove this manually, e.g. Windows.
28852892
supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
28862893
supported_sanitizers: SanitizerSet::empty(),
2894+
default_sanitizers: SanitizerSet::empty(),
28872895
c_enum_min_bits: None,
28882896
generate_arange_section: true,
28892897
supports_stack_protector: true,

compiler/rustc_target/src/spec/targets/aarch64_unknown_fuchsia.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) fn target() -> Target {
1212
| SanitizerSet::CFI
1313
| SanitizerSet::LEAK
1414
| SanitizerSet::SHADOWCALLSTACK;
15+
base.default_sanitizers = SanitizerSet::SHADOWCALLSTACK;
1516
base.supports_xray = true;
1617

1718
base.add_pre_link_args(

compiler/rustc_target/src/spec/targets/riscv64gc_unknown_fuchsia.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(crate) fn target() -> Target {
99
base.max_atomic_width = Some(64);
1010
base.stack_probes = StackProbeType::Inline;
1111
base.supported_sanitizers = SanitizerSet::SHADOWCALLSTACK;
12+
base.default_sanitizers = SanitizerSet::SHADOWCALLSTACK;
1213
base.supports_xray = true;
1314

1415
Target {

0 commit comments

Comments
 (0)