Skip to content

Commit c47aa67

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 53c1bbf commit c47aa67

40 files changed

+259
-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
@@ -110,32 +110,6 @@ fn parse_linkage_attr(tcx: TyCtxt<'_>, did: LocalDefId, attr: &Attribute) -> Opt
110110
Some(linkage)
111111
}
112112

113-
// FIXME(jdonszelmann): remove when no_sanitize becomes a parsed attr
114-
fn parse_no_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> Option<SanitizerSet> {
115-
let list = attr.meta_item_list()?;
116-
let mut sanitizer_set = SanitizerSet::empty();
117-
118-
for item in list.iter() {
119-
match item.name() {
120-
Some(sym::address) => {
121-
sanitizer_set |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
122-
}
123-
Some(sym::cfi) => sanitizer_set |= SanitizerSet::CFI,
124-
Some(sym::kcfi) => sanitizer_set |= SanitizerSet::KCFI,
125-
Some(sym::memory) => sanitizer_set |= SanitizerSet::MEMORY,
126-
Some(sym::memtag) => sanitizer_set |= SanitizerSet::MEMTAG,
127-
Some(sym::shadow_call_stack) => sanitizer_set |= SanitizerSet::SHADOWCALLSTACK,
128-
Some(sym::thread) => sanitizer_set |= SanitizerSet::THREAD,
129-
Some(sym::hwaddress) => sanitizer_set |= SanitizerSet::HWADDRESS,
130-
_ => {
131-
tcx.dcx().emit_err(errors::InvalidNoSanitize { span: item.span() });
132-
}
133-
}
134-
}
135-
136-
Some(sanitizer_set)
137-
}
138-
139113
// FIXME(jdonszelmann): remove when patchable_function_entry becomes a parsed attr
140114
fn parse_patchable_function_entry(
141115
tcx: TyCtxt<'_>,
@@ -194,7 +168,6 @@ fn parse_patchable_function_entry(
194168
#[derive(Default)]
195169
struct InterestingAttributeDiagnosticSpans {
196170
link_ordinal: Option<Span>,
197-
no_sanitize: Option<Span>,
198171
sanitize: Option<Span>,
199172
inline: Option<Span>,
200173
no_mangle: Option<Span>,
@@ -372,11 +345,6 @@ fn process_builtin_attrs(
372345
codegen_fn_attrs.linkage = linkage;
373346
}
374347
}
375-
sym::no_sanitize => {
376-
interesting_spans.no_sanitize = Some(attr.span());
377-
codegen_fn_attrs.no_sanitize |=
378-
parse_no_sanitize_attr(tcx, attr).unwrap_or_default();
379-
}
380348
sym::sanitize => interesting_spans.sanitize = Some(attr.span()),
381349
sym::instruction_set => {
382350
codegen_fn_attrs.instruction_set = parse_instruction_set_attr(tcx, attr)
@@ -500,24 +468,14 @@ fn check_result(
500468
if !codegen_fn_attrs.no_sanitize.is_empty()
501469
&& codegen_fn_attrs.inline.always()
502470
&& let (Some(no_sanitize_span), Some(inline_span)) =
503-
(interesting_spans.no_sanitize, interesting_spans.inline)
471+
(interesting_spans.sanitize, interesting_spans.inline)
504472
{
505473
let hir_id = tcx.local_def_id_to_hir_id(did);
506474
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
507475
lint.primary_message("`no_sanitize` will have no effect after inlining");
508476
lint.span_note(inline_span, "inlining requested here");
509477
})
510478
}
511-
if !codegen_fn_attrs.no_sanitize.is_empty()
512-
&& codegen_fn_attrs.inline.always()
513-
&& let (Some(sanitize_span), Some(inline_span)) = (interesting_spans.sanitize, interesting_spans.inline)
514-
{
515-
let hir_id = tcx.local_def_id_to_hir_id(did);
516-
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
517-
lint.primary_message("setting `sanitize` off will have no effect after inlining");
518-
lint.span_note(inline_span, "inlining requested here");
519-
})
520-
}
521479

522480
// error when specifying link_name together with link_ordinal
523481
if let Some(_) = codegen_fn_attrs.link_name
@@ -641,9 +599,14 @@ fn opt_trait_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
641599
}
642600

643601
/// For an attr that has the `sanitize` attribute, read the list of
644-
/// disabled sanitizers.
645-
fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
646-
let mut result = SanitizerSet::empty();
602+
/// disabled sanitizers. `current_attr` holds the information about
603+
/// previously parsed attributes.
604+
fn parse_sanitize_attr(
605+
tcx: TyCtxt<'_>,
606+
attr: &Attribute,
607+
current_attr: SanitizerSet,
608+
) -> SanitizerSet {
609+
let mut result = current_attr;
647610
if let Some(list) = attr.meta_item_list() {
648611
for item in list.iter() {
649612
let MetaItemInner::MetaItem(set) = item else {
@@ -652,10 +615,13 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
652615
};
653616
let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
654617
match segments.as_slice() {
655-
[sym::address] if set.value_str() == Some(sym::off) => {
618+
// Similar to clang, sanitize(address = ..) and
619+
// sanitize(kernel_address = ..) control both ASan and KASan
620+
// Source: https://reviews.llvm.org/D44981.
621+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::off) => {
656622
result |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
657623
}
658-
[sym::address] if set.value_str() == Some(sym::on) => {
624+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::on) => {
659625
result &= !SanitizerSet::ADDRESS;
660626
result &= !SanitizerSet::KERNELADDRESS;
661627
}
@@ -703,19 +669,20 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
703669
}
704670

705671
fn disabled_sanitizers_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerSet {
706-
// Check for a sanitize annotation directly on this def.
707-
if let Some(attr) = tcx.get_attr(did, sym::sanitize) {
708-
return parse_sanitize_attr(tcx, attr);
709-
}
710-
711-
// Otherwise backtrack.
712-
match tcx.opt_local_parent(did) {
672+
// Backtrack to the crate root.
673+
let disabled = match tcx.opt_local_parent(did) {
713674
// Check the parent (recursively).
714675
Some(parent) => tcx.disabled_sanitizers_for(parent),
715676
// We reached the crate root without seeing an attribute, so
716677
// there is no sanitizers to exclude.
717678
None => SanitizerSet::empty(),
679+
};
680+
681+
// Check for a sanitize annotation directly on this def.
682+
if let Some(attr) = tcx.get_attr(did, sym::sanitize) {
683+
return parse_sanitize_attr(tcx, attr, disabled);
718684
}
685+
disabled
719686
}
720687

721688
/// 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
@@ -63,8 +63,8 @@ pub struct CodegenFnAttrs {
6363
/// The `#[link_section = "..."]` attribute, or what executable section this
6464
/// should be placed in.
6565
pub link_section: Option<Symbol>,
66-
/// The `#[no_sanitize(...)]` attribute. Indicates sanitizers for which
67-
/// instrumentation should be disabled inside the annotated function.
66+
/// The `#[sanitize(xyz = "off")]` attribute. Indicates sanitizers for which
67+
/// instrumentation should be disabled inside the function.
6868
pub no_sanitize: SanitizerSet,
6969
/// The `#[instruction_set(set)]` attribute. Indicates if the generated code should
7070
/// 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
@@ -341,9 +341,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
341341
[sym::diagnostic, sym::on_unimplemented, ..] => {
342342
self.check_diagnostic_on_unimplemented(attr.span(), hir_id, target)
343343
}
344-
[sym::no_sanitize, ..] => {
345-
self.check_no_sanitize(attr, span, target)
346-
}
347344
[sym::sanitize, ..] => {
348345
self.check_sanitize(attr, span, target)
349346
}
@@ -662,42 +659,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
662659
}
663660
}
664661

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

0 commit comments

Comments
 (0)