Skip to content

Commit 90c564e

Browse files
committed
Remove the no_sanitize attribute in favor of sanitize
This removes the #[no_sanitize] attribute, which was behind an unstable feature named no_sanitize. Instead, we introduce the sanitize attribute which is more powerful and allows to be extended in the future (instead of just focusing on turning sanitizers off). This also makes sanitize(kernel_address = ..) attribute work with -Zsanitize=address To do it the same as how clang disables address sanitizer, we now disable ASAN on sanitize(kernel_address = "off") and KASAN on sanitize(address = "off"). The same was added to clang in https://reviews.llvm.org/D44981.
1 parent 401884e commit 90c564e

40 files changed

+271
-409
lines changed

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,8 @@ codegen_ssa_invalid_monomorphization_unsupported_symbol = invalid monomorphizati
171171
172172
codegen_ssa_invalid_monomorphization_unsupported_symbol_of_size = invalid monomorphization of `{$name}` intrinsic: unsupported {$symbol} from `{$in_ty}` with element `{$in_elem}` of size `{$size}` to `{$ret_ty}`
173173
174-
codegen_ssa_invalid_no_sanitize = invalid argument for `no_sanitize`
175-
.note = expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`
176-
177174
codegen_ssa_invalid_sanitize = invalid argument for `sanitize`
178-
.note = expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`
175+
.note = expected one of: `address`, `kernel_address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow_call_stack`, or `thread`
179176
180177
codegen_ssa_invalid_windows_subsystem = invalid windows subsystem `{$subsystem}`, only `windows` and `console` are allowed
181178

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,6 @@ fn parse_instruction_set_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> Option<Instr
7777
}
7878
}
7979

80-
// FIXME(jdonszelmann): remove when no_sanitize becomes a parsed attr
81-
fn parse_no_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> Option<SanitizerSet> {
82-
let list = attr.meta_item_list()?;
83-
let mut sanitizer_set = SanitizerSet::empty();
84-
85-
for item in list.iter() {
86-
match item.name() {
87-
Some(sym::address) => {
88-
sanitizer_set |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
89-
}
90-
Some(sym::cfi) => sanitizer_set |= SanitizerSet::CFI,
91-
Some(sym::kcfi) => sanitizer_set |= SanitizerSet::KCFI,
92-
Some(sym::memory) => sanitizer_set |= SanitizerSet::MEMORY,
93-
Some(sym::memtag) => sanitizer_set |= SanitizerSet::MEMTAG,
94-
Some(sym::shadow_call_stack) => sanitizer_set |= SanitizerSet::SHADOWCALLSTACK,
95-
Some(sym::thread) => sanitizer_set |= SanitizerSet::THREAD,
96-
Some(sym::hwaddress) => sanitizer_set |= SanitizerSet::HWADDRESS,
97-
_ => {
98-
tcx.dcx().emit_err(errors::InvalidNoSanitize { span: item.span() });
99-
}
100-
}
101-
}
102-
103-
Some(sanitizer_set)
104-
}
105-
10680
// FIXME(jdonszelmann): remove when patchable_function_entry becomes a parsed attr
10781
fn parse_patchable_function_entry(
10882
tcx: TyCtxt<'_>,
@@ -161,7 +135,6 @@ fn parse_patchable_function_entry(
161135
#[derive(Default)]
162136
struct InterestingAttributeDiagnosticSpans {
163137
link_ordinal: Option<Span>,
164-
no_sanitize: Option<Span>,
165138
sanitize: Option<Span>,
166139
inline: Option<Span>,
167140
no_mangle: Option<Span>,
@@ -339,11 +312,6 @@ fn process_builtin_attrs(
339312
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
340313
}
341314
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
342-
sym::no_sanitize => {
343-
interesting_spans.no_sanitize = Some(attr.span());
344-
codegen_fn_attrs.no_sanitize |=
345-
parse_no_sanitize_attr(tcx, attr).unwrap_or_default();
346-
}
347315
sym::sanitize => interesting_spans.sanitize = Some(attr.span()),
348316
sym::instruction_set => {
349317
codegen_fn_attrs.instruction_set = parse_instruction_set_attr(tcx, attr)
@@ -467,24 +435,14 @@ fn check_result(
467435
if !codegen_fn_attrs.no_sanitize.is_empty()
468436
&& codegen_fn_attrs.inline.always()
469437
&& let (Some(no_sanitize_span), Some(inline_span)) =
470-
(interesting_spans.no_sanitize, interesting_spans.inline)
438+
(interesting_spans.sanitize, interesting_spans.inline)
471439
{
472440
let hir_id = tcx.local_def_id_to_hir_id(did);
473441
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
474442
lint.primary_message("`no_sanitize` will have no effect after inlining");
475443
lint.span_note(inline_span, "inlining requested here");
476444
})
477445
}
478-
if !codegen_fn_attrs.no_sanitize.is_empty()
479-
&& codegen_fn_attrs.inline.always()
480-
&& let (Some(sanitize_span), Some(inline_span)) = (interesting_spans.sanitize, interesting_spans.inline)
481-
{
482-
let hir_id = tcx.local_def_id_to_hir_id(did);
483-
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
484-
lint.primary_message("setting `sanitize` off will have no effect after inlining");
485-
lint.span_note(inline_span, "inlining requested here");
486-
})
487-
}
488446

489447
// error when specifying link_name together with link_ordinal
490448
if let Some(_) = codegen_fn_attrs.link_name
@@ -608,9 +566,14 @@ fn opt_trait_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
608566
}
609567

610568
/// For an attr that has the `sanitize` attribute, read the list of
611-
/// disabled sanitizers.
612-
fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
613-
let mut result = SanitizerSet::empty();
569+
/// disabled sanitizers. `current_attr` holds the information about
570+
/// previously parsed attributes.
571+
fn parse_sanitize_attr(
572+
tcx: TyCtxt<'_>,
573+
attr: &Attribute,
574+
current_attr: SanitizerSet,
575+
) -> SanitizerSet {
576+
let mut result = current_attr;
614577
if let Some(list) = attr.meta_item_list() {
615578
for item in list.iter() {
616579
let MetaItemInner::MetaItem(set) = item else {
@@ -619,10 +582,13 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
619582
};
620583
let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
621584
match segments.as_slice() {
622-
[sym::address] if set.value_str() == Some(sym::off) => {
585+
// Similar to clang, sanitize(address = ..) and
586+
// sanitize(kernel_address = ..) control both ASan and KASan
587+
// Source: https://reviews.llvm.org/D44981.
588+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::off) => {
623589
result |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
624590
}
625-
[sym::address] if set.value_str() == Some(sym::on) => {
591+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::on) => {
626592
result &= !SanitizerSet::ADDRESS;
627593
result &= !SanitizerSet::KERNELADDRESS;
628594
}
@@ -670,19 +636,20 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
670636
}
671637

672638
fn disabled_sanitizers_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerSet {
673-
// Check for a sanitize annotation directly on this def.
674-
if let Some(attr) = tcx.get_attr(did, sym::sanitize) {
675-
return parse_sanitize_attr(tcx, attr);
676-
}
677-
678-
// Otherwise backtrack.
679-
match tcx.opt_local_parent(did) {
639+
// Backtrack to the crate root.
640+
let disabled = match tcx.opt_local_parent(did) {
680641
// Check the parent (recursively).
681642
Some(parent) => tcx.disabled_sanitizers_for(parent),
682643
// We reached the crate root without seeing an attribute, so
683644
// there is no sanitizers to exclude.
684645
None => SanitizerSet::empty(),
646+
};
647+
648+
// Check for a sanitize annotation directly on this def.
649+
if let Some(attr) = tcx.get_attr(did, sym::sanitize) {
650+
return parse_sanitize_attr(tcx, attr, disabled);
685651
}
652+
disabled
686653
}
687654

688655
/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,14 +1120,6 @@ impl IntoDiagArg for ExpectedPointerMutability {
11201120
}
11211121
}
11221122

1123-
#[derive(Diagnostic)]
1124-
#[diag(codegen_ssa_invalid_no_sanitize)]
1125-
#[note]
1126-
pub(crate) struct InvalidNoSanitize {
1127-
#[primary_span]
1128-
pub span: Span,
1129-
}
1130-
11311123
#[derive(Diagnostic)]
11321124
#[diag(codegen_ssa_invalid_sanitize)]
11331125
#[note]

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,12 +741,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
741741
ErrorPreceding, EncodeCrossCrate::No
742742
),
743743
gated!(
744-
no_sanitize, Normal,
745-
template!(List: &["address, kcfi, memory, thread"]), DuplicatesOk,
746-
EncodeCrossCrate::No, experimental!(no_sanitize)
747-
),
748-
gated!(
749-
sanitize, Normal, template!(List: r#"address = "on|off", cfi = "on|off""#), ErrorPreceding,
744+
sanitize, Normal, template!(List: &[r#"address = "on|off""#, r#"kernel_address = "on|off""#, r#"cfi = "on|off""#, r#"hwaddress = "on|off""#, r#"kcfi = "on|off""#, r#"memory = "on|off""#, r#"memtag = "on|off""#, r#"shadow_call_stack = "on|off""#, r#"thread = "on|off""#]), ErrorPreceding,
750745
EncodeCrossCrate::No, sanitize, experimental!(sanitize),
751746
),
752747
gated!(

compiler/rustc_feature/src/removed.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ declare_features! (
190190
(removed, no_coverage, "1.74.0", Some(84605), Some("renamed to `coverage_attribute`"), 114656),
191191
/// Allows `#[no_debug]`.
192192
(removed, no_debug, "1.43.0", Some(29721), Some("removed due to lack of demand"), 69667),
193+
// Allows the use of `no_sanitize` attribute.
194+
/// The feature was renamed to `sanitize` and the attribute to `#[sanitize(xyz = "on|off")]`
195+
(removed, no_sanitize, "CURRENT_RUSTC_VERSION", Some(39699), Some(r#"renamed to sanitize(xyz = "on|off")"#), 142681),
193196
/// Note: this feature was previously recorded in a separate
194197
/// `STABLE_REMOVED` list because it, uniquely, was once stable but was
195198
/// then removed. But there was no utility storing it separately, so now

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,6 @@ declare_features! (
592592
(unstable, new_range, "1.86.0", Some(123741)),
593593
/// Allows `#![no_core]`.
594594
(unstable, no_core, "1.3.0", Some(29639)),
595-
/// Allows the use of `no_sanitize` attribute.
596-
(unstable, no_sanitize, "1.42.0", Some(39699)),
597595
/// Allows using the `non_exhaustive_omitted_patterns` lint.
598596
(unstable, non_exhaustive_omitted_patterns_lint, "1.57.0", Some(89554)),
599597
/// Allows `for<T>` binders in where-clauses

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,18 +2301,18 @@ declare_lint! {
23012301

23022302
declare_lint! {
23032303
/// The `inline_no_sanitize` lint detects incompatible use of
2304-
/// [`#[inline(always)]`][inline] and [`#[no_sanitize(...)]`][no_sanitize].
2304+
/// [`#[inline(always)]`][inline] and [`#[sanitize(xyz = "off")]`][sanitize].
23052305
///
23062306
/// [inline]: https://doc.rust-lang.org/reference/attributes/codegen.html#the-inline-attribute
2307-
/// [no_sanitize]: https://doc.rust-lang.org/nightly/unstable-book/language-features/no-sanitize.html
2307+
/// [sanitize]: https://doc.rust-lang.org/nightly/unstable-book/language-features/no-sanitize.html
23082308
///
23092309
/// ### Example
23102310
///
23112311
/// ```rust
2312-
/// #![feature(no_sanitize)]
2312+
/// #![feature(sanitize)]
23132313
///
23142314
/// #[inline(always)]
2315-
/// #[no_sanitize(address)]
2315+
/// #[sanitize(address = "off")]
23162316
/// fn x() {}
23172317
///
23182318
/// fn main() {
@@ -2325,11 +2325,11 @@ declare_lint! {
23252325
/// ### Explanation
23262326
///
23272327
/// The use of the [`#[inline(always)]`][inline] attribute prevents the
2328-
/// the [`#[no_sanitize(...)]`][no_sanitize] attribute from working.
2328+
/// the [`#[sanitize(xyz = "off")]`][sanitize] attribute from working.
23292329
/// Consider temporarily removing `inline` attribute.
23302330
pub INLINE_NO_SANITIZE,
23312331
Warn,
2332-
"detects incompatible use of `#[inline(always)]` and `#[no_sanitize(...)]`",
2332+
r#"detects incompatible use of `#[inline(always)]` and `#[sanitize(... = "off")]`"#,
23332333
}
23342334

23352335
declare_lint! {

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ pub struct CodegenFnAttrs {
6262
/// The `#[link_section = "..."]` attribute, or what executable section this
6363
/// should be placed in.
6464
pub link_section: Option<Symbol>,
65-
/// The `#[no_sanitize(...)]` attribute. Indicates sanitizers for which
66-
/// instrumentation should be disabled inside the annotated function.
65+
/// The `#[sanitize(xyz = "off")]` attribute. Indicates sanitizers for which
66+
/// instrumentation should be disabled inside the function.
6767
pub no_sanitize: SanitizerSet,
6868
/// The `#[instruction_set(set)]` attribute. Indicates if the generated code should
6969
/// be generated against a specific instruction set. Only usable on architectures which allow

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,6 @@ passes_no_mangle_foreign =
542542
.note = symbol names in extern blocks are not mangled
543543
.suggestion = remove this attribute
544544
545-
passes_no_sanitize =
546-
`#[no_sanitize({$attr_str})]` should be applied to {$accepted_kind}
547-
.label = not {$accepted_kind}
548-
549545
passes_non_exhaustive_with_default_field_values =
550546
`#[non_exhaustive]` can't be used to annotate items with default field values
551547
.label = this struct has default field values

compiler/rustc_passes/src/check_attr.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
344344
[sym::diagnostic, sym::on_unimplemented, ..] => {
345345
self.check_diagnostic_on_unimplemented(attr.span(), hir_id, target)
346346
}
347-
[sym::no_sanitize, ..] => {
348-
self.check_no_sanitize(attr, span, target)
349-
}
350347
[sym::sanitize, ..] => {
351348
self.check_sanitize(attr, span, target)
352349
}
@@ -664,42 +661,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
664661
}
665662
}
666663

667-
fn check_no_sanitize(&self, attr: &Attribute, span: Span, target: Target) {
668-
if let Some(list) = attr.meta_item_list() {
669-
for item in list.iter() {
670-
let sym = item.name();
671-
match sym {
672-
Some(s @ sym::address | s @ sym::hwaddress) => {
673-
let is_valid =
674-
matches!(target, Target::Fn | Target::Method(..) | Target::Static);
675-
if !is_valid {
676-
self.dcx().emit_err(errors::NoSanitize {
677-
attr_span: item.span(),
678-
defn_span: span,
679-
accepted_kind: "a function or static",
680-
attr_str: s.as_str(),
681-
});
682-
}
683-
}
684-
_ => {
685-
let is_valid = matches!(target, Target::Fn | Target::Method(..));
686-
if !is_valid {
687-
self.dcx().emit_err(errors::NoSanitize {
688-
attr_span: item.span(),
689-
defn_span: span,
690-
accepted_kind: "a function",
691-
attr_str: &match sym {
692-
Some(name) => name.to_string(),
693-
None => "...".to_string(),
694-
},
695-
});
696-
}
697-
}
698-
}
699-
}
700-
}
701-
}
702-
703664
/// Checks that the `#[sanitize(..)]` attribute is applied to a
704665
/// function/closure/method, or to an impl block or module.
705666
fn check_sanitize(&self, attr: &Attribute, target_span: Span, target: Target) {

0 commit comments

Comments
 (0)