Skip to content

Sanitizers target modificators #138736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ impl CStore {
match (&left_name_val, &right_name_val) {
(Some(l), Some(r)) => match l.1.opt.cmp(&r.1.opt) {
cmp::Ordering::Equal => {
if l.0.tech_value != r.0.tech_value {
if !l.1.consistent(&tcx.sess.opts, Some(&r.1)) {
report_diff(
&l.0.prefix,
&l.0.name,
Expand All @@ -428,20 +428,28 @@ impl CStore {
right_name_val = None;
}
cmp::Ordering::Greater => {
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
if !r.1.consistent(&tcx.sess.opts, None) {
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
}
right_name_val = None;
}
cmp::Ordering::Less => {
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
if !l.1.consistent(&tcx.sess.opts, None) {
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
}
left_name_val = None;
}
},
(Some(l), None) => {
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
if !l.1.consistent(&tcx.sess.opts, None) {
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
}
left_name_val = None;
}
(None, Some(r)) => {
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
if !r.1.consistent(&tcx.sess.opts, None) {
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
}
right_name_val = None;
}
(None, None) => break,
Expand Down
71 changes: 69 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,77 @@ pub struct TargetModifier {
pub value_name: String,
}

mod target_modifier_consistency_check {
use super::*;
pub(super) fn sanitizer(l: &TargetModifier, r: Option<&TargetModifier>) -> bool {
let mut lparsed: SanitizerSet = Default::default();
let lval = if l.value_name.is_empty() { None } else { Some(l.value_name.as_str()) };
parse::parse_sanitizers(&mut lparsed, lval);

let mut rparsed: SanitizerSet = Default::default();
let rval = r.filter(|v| !v.value_name.is_empty()).map(|v| v.value_name.as_str());
parse::parse_sanitizers(&mut rparsed, rval);

// Some sanitizers need to be target modifiers, and some do not.
// For now, we should mark all sanitizers as target modifiers except for these:
// AddressSanitizer, LeakSanitizer
let tmod_sanitizers = SanitizerSet::MEMORY
| SanitizerSet::THREAD
| SanitizerSet::HWADDRESS
| SanitizerSet::CFI
| SanitizerSet::MEMTAG
| SanitizerSet::SHADOWCALLSTACK
| SanitizerSet::KCFI
| SanitizerSet::KERNELADDRESS
| SanitizerSet::SAFESTACK
| SanitizerSet::DATAFLOW;

lparsed & tmod_sanitizers == rparsed & tmod_sanitizers
}
pub(super) fn sanitizer_cfi_normalize_integers(
opts: &Options,
l: &TargetModifier,
r: Option<&TargetModifier>,
) -> bool {
// For kCFI, the helper flag -Zsanitizer-cfi-normalize-integers should also be a target modifier
if opts.unstable_opts.sanitizer.contains(SanitizerSet::KCFI) {
if let Some(r) = r {
return l.extend().tech_value == r.extend().tech_value;
} else {
return false;
}
}
true
}
}

impl TargetModifier {
pub fn extend(&self) -> ExtendedTargetModifierInfo {
self.opt.reparse(&self.value_name)
}
// Custom consistency check for target modifiers (or default `l.tech_value == r.tech_value`)
// When other is None, consistency with default value is checked
pub fn consistent(&self, opts: &Options, other: Option<&TargetModifier>) -> bool {
assert!(other.is_none() || self.opt == other.unwrap().opt);
match self.opt {
OptionsTargetModifiers::UnstableOptions(unstable) => match unstable {
UnstableOptionsTargetModifiers::sanitizer => {
return target_modifier_consistency_check::sanitizer(self, other);
}
UnstableOptionsTargetModifiers::sanitizer_cfi_normalize_integers => {
return target_modifier_consistency_check::sanitizer_cfi_normalize_integers(
opts, self, other,
);
}
_ => {}
},
_ => {}
};
match other {
Some(other) => self.extend().tech_value == other.extend().tech_value,
None => false,
}
}
}

fn tmod_push_impl(
Expand Down Expand Up @@ -2444,13 +2511,13 @@ options! {
remark_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
"directory into which to write optimization remarks (if not specified, they will be \
written to standard error output)"),
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED],
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED TARGET_MODIFIER],
"use a sanitizer"),
sanitizer_cfi_canonical_jump_tables: Option<bool> = (Some(true), parse_opt_bool, [TRACKED],
"enable canonical jump tables (default: yes)"),
sanitizer_cfi_generalize_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
"enable generalizing pointer types (default: no)"),
sanitizer_cfi_normalize_integers: Option<bool> = (None, parse_opt_bool, [TRACKED],
sanitizer_cfi_normalize_integers: Option<bool> = (None, parse_opt_bool, [TRACKED TARGET_MODIFIER],
"enable normalizing integer types (default: no)"),
sanitizer_dataflow_abilist: Vec<String> = (Vec::new(), parse_comma_list, [TRACKED],
"additional ABI list files that control how shadow parameters are passed (comma separated)"),
Expand Down
2 changes: 2 additions & 0 deletions tests/codegen/naked-asan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//@ needs-sanitizer-address
//@ compile-flags: -Zsanitizer=address -Ctarget-feature=-crt-static

//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![no_std]
#![feature(abi_x86_interrupt)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//
//@ revisions:ASAN ASAN-FAT-LTO
//@ compile-flags: -Zsanitizer=address -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer
//@[ASAN] compile-flags:
//@[ASAN-FAT-LTO] compile-flags: -Cprefer-dynamic=false -Clto=fat

Expand Down
2 changes: 2 additions & 0 deletions tests/codegen/sanitizer/cfi/add-canonical-jump-tables-flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi

//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

pub fn foo() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers

//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer,sanitizer-cfi-normalize-integers

#![crate_type = "lib"]

pub fn foo() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/cfi/dbg-location-on-cfi-blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static -Cdebuginfo=1
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![feature(no_sanitize)]
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/cfi/emit-type-checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![feature(cfi_encoding, extern_types)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![feature(type_alias_impl_trait)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![feature(type_alias_impl_trait)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![feature(type_alias_impl_trait)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]
#![feature(extern_types)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-generalize-pointers
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers -Zsanitizer-cfi-generalize-pointers
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer,sanitizer-cfi-normalize-integers

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer,sanitizer-cfi-normalize-integers

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Ctarget-feature=-crt-static -Zsanitizer=cfi
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/cfi/external_weak_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clinker-plugin-lto -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer
#![crate_type = "bin"]
#![feature(linkage)]

Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/cfi/generalize-pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-generalize-pointers -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/cfi/normalize-integers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-cfi
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers -Copt-level=0
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer,sanitizer-cfi-normalize-integers

#![crate_type = "lib"]

Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/dataflow-instrument-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
//@ needs-sanitizer-dataflow
//@ compile-flags: -Copt-level=0 -Zsanitizer=dataflow
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer

#![crate_type = "lib"]

Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/memory-track-origins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//@ revisions:MSAN-0 MSAN-1 MSAN-2 MSAN-1-LTO MSAN-2-LTO
//
//@ compile-flags: -Zsanitizer=memory -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer
//@[MSAN-0] compile-flags:
//@[MSAN-1] compile-flags: -Zsanitizer-memory-track-origins=1
//@[MSAN-2] compile-flags: -Zsanitizer-memory-track-origins
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/sanitizer/no-sanitize-inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//@ needs-sanitizer-leak
//@ revisions: ASAN LSAN
//@ compile-flags: -Copt-level=3 -Zmir-opt-level=4 -Ctarget-feature=-crt-static
//@ compile-flags: -C unsafe-allow-abi-mismatch=sanitizer
//@[ASAN] compile-flags: -Zsanitizer=address
//@[LSAN] compile-flags: -Zsanitizer=leak

Expand Down
Loading
Loading