Skip to content

Commit 95bdb34

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 3ef065b commit 95bdb34

39 files changed

+239
-406
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 & 56 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>,
@@ -331,11 +304,6 @@ fn process_builtin_attrs(
331304
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
332305
}
333306
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
334-
sym::no_sanitize => {
335-
interesting_spans.no_sanitize = Some(attr.span());
336-
codegen_fn_attrs.no_sanitize |=
337-
parse_no_sanitize_attr(tcx, attr).unwrap_or_default();
338-
}
339307
sym::sanitize => interesting_spans.sanitize = Some(attr.span()),
340308
sym::instruction_set => {
341309
codegen_fn_attrs.instruction_set = parse_instruction_set_attr(tcx, attr)
@@ -459,21 +427,10 @@ fn check_result(
459427
if !codegen_fn_attrs.no_sanitize.is_empty()
460428
&& codegen_fn_attrs.inline.always()
461429
&& let (Some(no_sanitize_span), Some(inline_span)) =
462-
(interesting_spans.no_sanitize, interesting_spans.inline)
463-
{
464-
let hir_id = tcx.local_def_id_to_hir_id(did);
465-
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
466-
lint.primary_message("`no_sanitize` will have no effect after inlining");
467-
lint.span_note(inline_span, "inlining requested here");
468-
})
469-
}
470-
if !codegen_fn_attrs.no_sanitize.is_empty()
471-
&& codegen_fn_attrs.inline.always()
472-
&& let (Some(sanitize_span), Some(inline_span)) =
473430
(interesting_spans.sanitize, interesting_spans.inline)
474431
{
475432
let hir_id = tcx.local_def_id_to_hir_id(did);
476-
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
433+
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
477434
lint.primary_message("setting `sanitize` off will have no effect after inlining");
478435
lint.span_note(inline_span, "inlining requested here");
479436
})
@@ -601,9 +558,14 @@ fn opt_trait_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
601558
}
602559

603560
/// For an attr that has the `sanitize` attribute, read the list of
604-
/// disabled sanitizers.
605-
fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
606-
let mut result = SanitizerSet::empty();
561+
/// disabled sanitizers. `current_attr` holds the information about
562+
/// previously parsed attributes.
563+
fn parse_sanitize_attr(
564+
tcx: TyCtxt<'_>,
565+
attr: &Attribute,
566+
current_attr: SanitizerSet,
567+
) -> SanitizerSet {
568+
let mut result = current_attr;
607569
if let Some(list) = attr.meta_item_list() {
608570
for item in list.iter() {
609571
let MetaItemInner::MetaItem(set) = item else {
@@ -612,10 +574,13 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
612574
};
613575
let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
614576
match segments.as_slice() {
615-
[sym::address] if set.value_str() == Some(sym::off) => {
577+
// Similar to clang, sanitize(address = ..) and
578+
// sanitize(kernel_address = ..) control both ASan and KASan
579+
// Source: https://reviews.llvm.org/D44981.
580+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::off) => {
616581
result |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
617582
}
618-
[sym::address] if set.value_str() == Some(sym::on) => {
583+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::on) => {
619584
result &= !SanitizerSet::ADDRESS;
620585
result &= !SanitizerSet::KERNELADDRESS;
621586
}
@@ -663,19 +628,20 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
663628
}
664629

665630
fn disabled_sanitizers_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerSet {
666-
// Check for a sanitize annotation directly on this def.
667-
if let Some(attr) = tcx.get_attr(did, sym::sanitize) {
668-
return parse_sanitize_attr(tcx, attr);
669-
}
670-
671-
// Otherwise backtrack.
672-
match tcx.opt_local_parent(did) {
631+
// Backtrack to the crate root.
632+
let disabled = match tcx.opt_local_parent(did) {
673633
// Check the parent (recursively).
674634
Some(parent) => tcx.disabled_sanitizers_for(parent),
675635
// We reached the crate root without seeing an attribute, so
676636
// there is no sanitizers to exclude.
677637
None => SanitizerSet::empty(),
638+
};
639+
640+
// Check for a sanitize annotation directly on this def.
641+
if let Some(attr) = tcx.get_attr(did, sym::sanitize) {
642+
return parse_sanitize_attr(tcx, attr, disabled);
678643
}
644+
disabled
679645
}
680646

681647
/// 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: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,6 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
740740
template!(List: &["set"], "https://doc.rust-lang.org/reference/attributes/codegen.html#the-instruction_set-attribute"),
741741
ErrorPreceding, EncodeCrossCrate::No
742742
),
743-
gated!(
744-
no_sanitize, Normal,
745-
template!(List: &["address, kcfi, memory, thread"]), DuplicatesOk,
746-
EncodeCrossCrate::No, experimental!(no_sanitize)
747-
),
748743
gated!(
749744
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),

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
@@ -61,8 +61,8 @@ pub struct CodegenFnAttrs {
6161
/// The `#[link_section = "..."]` attribute, or what executable section this
6262
/// should be placed in.
6363
pub link_section: Option<Symbol>,
64-
/// The `#[no_sanitize(...)]` attribute. Indicates sanitizers for which
65-
/// instrumentation should be disabled inside the annotated function.
64+
/// The `#[sanitize(xyz = "off")]` attribute. Indicates sanitizers for which
65+
/// instrumentation should be disabled inside the function.
6666
pub no_sanitize: SanitizerSet,
6767
/// The `#[instruction_set(set)]` attribute. Indicates if the generated code should
6868
/// 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
@@ -444,10 +444,6 @@ passes_no_main_function =
444444
.teach_note = If you don't know the basics of Rust, you can go look to the Rust Book to get started: https://doc.rust-lang.org/book/
445445
.non_function_main = non-function item at `crate::main` is found
446446
447-
passes_no_sanitize =
448-
`#[no_sanitize({$attr_str})]` should be applied to {$accepted_kind}
449-
.label = not {$accepted_kind}
450-
451447
passes_non_exhaustive_with_default_field_values =
452448
`#[non_exhaustive]` can't be used to annotate items with default field values
453449
.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
@@ -258,9 +258,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
258258
[sym::diagnostic, sym::on_unimplemented, ..] => {
259259
self.check_diagnostic_on_unimplemented(attr.span(), hir_id, target)
260260
}
261-
[sym::no_sanitize, ..] => {
262-
self.check_no_sanitize(attr, span, target)
263-
}
264261
[sym::sanitize, ..] => {
265262
self.check_sanitize(attr, span, target)
266263
}
@@ -485,42 +482,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
485482
}
486483
}
487484

488-
fn check_no_sanitize(&self, attr: &Attribute, span: Span, target: Target) {
489-
if let Some(list) = attr.meta_item_list() {
490-
for item in list.iter() {
491-
let sym = item.name();
492-
match sym {
493-
Some(s @ sym::address | s @ sym::hwaddress) => {
494-
let is_valid =
495-
matches!(target, Target::Fn | Target::Method(..) | Target::Static);
496-
if !is_valid {
497-
self.dcx().emit_err(errors::NoSanitize {
498-
attr_span: item.span(),
499-
defn_span: span,
500-
accepted_kind: "a function or static",
501-
attr_str: s.as_str(),
502-
});
503-
}
504-
}
505-
_ => {
506-
let is_valid = matches!(target, Target::Fn | Target::Method(..));
507-
if !is_valid {
508-
self.dcx().emit_err(errors::NoSanitize {
509-
attr_span: item.span(),
510-
defn_span: span,
511-
accepted_kind: "a function",
512-
attr_str: &match sym {
513-
Some(name) => name.to_string(),
514-
None => "...".to_string(),
515-
},
516-
});
517-
}
518-
}
519-
}
520-
}
521-
}
522-
}
523-
524485
/// Checks that the `#[sanitize(..)]` attribute is applied to a
525486
/// function/closure/method, or to an impl block or module.
526487
fn check_sanitize(&self, attr: &Attribute, target_span: Span, target: Target) {

0 commit comments

Comments
 (0)