Skip to content

Commit c0d7277

Browse files
committed
Auto merge of #138736 - azhogin:azhogin/sanitizers-target-modificators, r=rcvalle
Sanitizers target modificators Depends on bool flag fix: #138483. 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 For kCFI, the helper flag -Zsanitizer-cfi-normalize-integers should also be a target modifier. Many test errors was with sanizer flags inconsistent with std deps. Tests are fixed with `-C unsafe-allow-abi-mismatch`.
2 parents 3507a74 + bf38919 commit c0d7277

File tree

86 files changed

+311
-68
lines changed

Some content is hidden

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

86 files changed

+311
-68
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl CStore {
407407
match (&left_name_val, &right_name_val) {
408408
(Some(l), Some(r)) => match l.1.opt.cmp(&r.1.opt) {
409409
cmp::Ordering::Equal => {
410-
if l.0.tech_value != r.0.tech_value {
410+
if !l.1.consistent(&tcx.sess.opts, Some(&r.1)) {
411411
report_diff(
412412
&l.0.prefix,
413413
&l.0.name,
@@ -419,20 +419,28 @@ impl CStore {
419419
right_name_val = None;
420420
}
421421
cmp::Ordering::Greater => {
422-
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
422+
if !r.1.consistent(&tcx.sess.opts, None) {
423+
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
424+
}
423425
right_name_val = None;
424426
}
425427
cmp::Ordering::Less => {
426-
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
428+
if !l.1.consistent(&tcx.sess.opts, None) {
429+
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
430+
}
427431
left_name_val = None;
428432
}
429433
},
430434
(Some(l), None) => {
431-
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
435+
if !l.1.consistent(&tcx.sess.opts, None) {
436+
report_diff(&l.0.prefix, &l.0.name, Some(&l.1.value_name), None);
437+
}
432438
left_name_val = None;
433439
}
434440
(None, Some(r)) => {
435-
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
441+
if !r.1.consistent(&tcx.sess.opts, None) {
442+
report_diff(&r.0.prefix, &r.0.name, None, Some(&r.1.value_name));
443+
}
436444
right_name_val = None;
437445
}
438446
(None, None) => break,

compiler/rustc_session/src/options.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,77 @@ pub struct TargetModifier {
8383
pub value_name: String,
8484
}
8585

86+
mod target_modifier_consistency_check {
87+
use super::*;
88+
pub(super) fn sanitizer(l: &TargetModifier, r: Option<&TargetModifier>) -> bool {
89+
let mut lparsed: SanitizerSet = Default::default();
90+
let lval = if l.value_name.is_empty() { None } else { Some(l.value_name.as_str()) };
91+
parse::parse_sanitizers(&mut lparsed, lval);
92+
93+
let mut rparsed: SanitizerSet = Default::default();
94+
let rval = r.filter(|v| !v.value_name.is_empty()).map(|v| v.value_name.as_str());
95+
parse::parse_sanitizers(&mut rparsed, rval);
96+
97+
// Some sanitizers need to be target modifiers, and some do not.
98+
// For now, we should mark all sanitizers as target modifiers except for these:
99+
// AddressSanitizer, LeakSanitizer
100+
let tmod_sanitizers = SanitizerSet::MEMORY
101+
| SanitizerSet::THREAD
102+
| SanitizerSet::HWADDRESS
103+
| SanitizerSet::CFI
104+
| SanitizerSet::MEMTAG
105+
| SanitizerSet::SHADOWCALLSTACK
106+
| SanitizerSet::KCFI
107+
| SanitizerSet::KERNELADDRESS
108+
| SanitizerSet::SAFESTACK
109+
| SanitizerSet::DATAFLOW;
110+
111+
lparsed & tmod_sanitizers == rparsed & tmod_sanitizers
112+
}
113+
pub(super) fn sanitizer_cfi_normalize_integers(
114+
opts: &Options,
115+
l: &TargetModifier,
116+
r: Option<&TargetModifier>,
117+
) -> bool {
118+
// For kCFI, the helper flag -Zsanitizer-cfi-normalize-integers should also be a target modifier
119+
if opts.unstable_opts.sanitizer.contains(SanitizerSet::KCFI) {
120+
if let Some(r) = r {
121+
return l.extend().tech_value == r.extend().tech_value;
122+
} else {
123+
return false;
124+
}
125+
}
126+
true
127+
}
128+
}
129+
86130
impl TargetModifier {
87131
pub fn extend(&self) -> ExtendedTargetModifierInfo {
88132
self.opt.reparse(&self.value_name)
89133
}
134+
// Custom consistency check for target modifiers (or default `l.tech_value == r.tech_value`)
135+
// When other is None, consistency with default value is checked
136+
pub fn consistent(&self, opts: &Options, other: Option<&TargetModifier>) -> bool {
137+
assert!(other.is_none() || self.opt == other.unwrap().opt);
138+
match self.opt {
139+
OptionsTargetModifiers::UnstableOptions(unstable) => match unstable {
140+
UnstableOptionsTargetModifiers::sanitizer => {
141+
return target_modifier_consistency_check::sanitizer(self, other);
142+
}
143+
UnstableOptionsTargetModifiers::sanitizer_cfi_normalize_integers => {
144+
return target_modifier_consistency_check::sanitizer_cfi_normalize_integers(
145+
opts, self, other,
146+
);
147+
}
148+
_ => {}
149+
},
150+
_ => {}
151+
};
152+
match other {
153+
Some(other) => self.extend().tech_value == other.extend().tech_value,
154+
None => false,
155+
}
156+
}
90157
}
91158

92159
fn tmod_push_impl(
@@ -2502,13 +2569,13 @@ written to standard error output)"),
25022569
retpoline_external_thunk: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER],
25032570
"enables retpoline-external-thunk, retpoline-indirect-branches and retpoline-indirect-calls \
25042571
target features (default: no)"),
2505-
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED],
2572+
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED TARGET_MODIFIER],
25062573
"use a sanitizer"),
25072574
sanitizer_cfi_canonical_jump_tables: Option<bool> = (Some(true), parse_opt_bool, [TRACKED],
25082575
"enable canonical jump tables (default: yes)"),
25092576
sanitizer_cfi_generalize_pointers: Option<bool> = (None, parse_opt_bool, [TRACKED],
25102577
"enable generalizing pointer types (default: no)"),
2511-
sanitizer_cfi_normalize_integers: Option<bool> = (None, parse_opt_bool, [TRACKED],
2578+
sanitizer_cfi_normalize_integers: Option<bool> = (None, parse_opt_bool, [TRACKED TARGET_MODIFIER],
25122579
"enable normalizing integer types (default: no)"),
25132580
sanitizer_dataflow_abilist: Vec<String> = (Vec::new(), parse_comma_list, [TRACKED],
25142581
"additional ABI list files that control how shadow parameters are passed (comma separated)"),

tests/codegen-llvm/sanitizer/address-sanitizer-globals-tracking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//@ only-linux
2020
//
2121
//@ revisions:ASAN ASAN-FAT-LTO
22-
//@ compile-flags: -Zsanitizer=address -Ctarget-feature=-crt-static
22+
//@ compile-flags: -Zsanitizer=address -Ctarget-feature=-crt-static -C unsafe-allow-abi-mismatch=sanitizer
2323
// [ASAN] no extra compile-flags
2424
//@[ASAN-FAT-LTO] compile-flags: -Cprefer-dynamic=false -Clto=fat
2525

tests/codegen-llvm/sanitizer/cfi/add-canonical-jump-tables-flag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that "CFI Canonical Jump Tables" module flag is added.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi
4+
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -C unsafe-allow-abi-mismatch=sanitizer
55

66
#![crate_type = "lib"]
77

tests/codegen-llvm/sanitizer/cfi/add-cfi-normalize-integers-flag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that "cfi-normalize-integers" module flag is added.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers
4+
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -Zsanitizer-cfi-normalize-integers -C unsafe-allow-abi-mismatch=sanitizer,sanitizer-cfi-normalize-integers
55

66
#![crate_type = "lib"]
77

tests/codegen-llvm/sanitizer/cfi/add-enable-split-lto-unit-flag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that "EnableSplitLTOUnit" module flag is added.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi
4+
//@ compile-flags: -Clto -Ctarget-feature=-crt-static -Zsanitizer=cfi -C unsafe-allow-abi-mismatch=sanitizer
55

66
#![crate_type = "lib"]
77

tests/codegen-llvm/sanitizer/cfi/dbg-location-on-cfi-blocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that the parent block's debug information are assigned to the inserted cfi block.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static -Cdebuginfo=1
4+
//@ compile-flags: -Clto -Cno-prepopulate-passes -Copt-level=0 -Zsanitizer=cfi -Ctarget-feature=-crt-static -Cdebuginfo=1 -C unsafe-allow-abi-mismatch=sanitizer
55

66
#![crate_type = "lib"]
77

tests/codegen-llvm/sanitizer/cfi/emit-type-checks-attr-no-sanitize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that pointer type membership tests for indirect calls are omitted.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
4+
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0 -C unsafe-allow-abi-mismatch=sanitizer
55

66
#![crate_type = "lib"]
77
#![feature(no_sanitize)]

tests/codegen-llvm/sanitizer/cfi/emit-type-checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that pointer type membership tests for indirect calls are emitted.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
4+
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0 -C unsafe-allow-abi-mismatch=sanitizer
55

66
#![crate_type = "lib"]
77

tests/codegen-llvm/sanitizer/cfi/emit-type-metadata-attr-cfi-encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that user-defined CFI encoding for types are emitted.
22
//
33
//@ needs-sanitizer-cfi
4-
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0
4+
//@ compile-flags: -Clto -Cno-prepopulate-passes -Ctarget-feature=-crt-static -Zsanitizer=cfi -Copt-level=0 -C unsafe-allow-abi-mismatch=sanitizer
55

66
#![crate_type = "lib"]
77
#![feature(cfi_encoding, extern_types)]

0 commit comments

Comments
 (0)