1
Fork 0

Allow disabling ASan instrumentation for globals

AddressSanitizer adds instrumentation to global variables unless the
[`no_sanitize_address`](https://llvm.org/docs/LangRef.html#global-attributes)
attribute is set on them.

This commit extends the existing `#[no_sanitize(address)]` attribute to
set this; previously it only had the desired effect on functions.
This commit is contained in:
Daniel Bertalan 2024-07-08 15:49:50 +02:00
parent 00ed73cdc0
commit 204b2281fa
10 changed files with 137 additions and 34 deletions

View file

@ -126,9 +126,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
[sym::coverage, ..] => self.check_coverage(attr, span, target),
[sym::optimize, ..] => self.check_optimize(hir_id, attr, span, target),
[sym::no_sanitize, ..] => {
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
}
[sym::no_sanitize, ..] => self.check_no_sanitize(attr, span, target),
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
[sym::target_feature, ..] => {
@ -450,6 +448,39 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
fn check_no_sanitize(&self, attr: &Attribute, span: Span, target: Target) {
if let Some(list) = attr.meta_item_list() {
for item in list.iter() {
let sym = item.name_or_empty();
match sym {
sym::address | sym::hwaddress => {
let is_valid =
matches!(target, Target::Fn | Target::Method(..) | Target::Static);
if !is_valid {
self.dcx().emit_err(errors::NoSanitize {
attr_span: item.span(),
defn_span: span,
accepted_kind: "a function or static",
attr_str: sym.as_str(),
});
}
}
_ => {
let is_valid = matches!(target, Target::Fn | Target::Method(..));
if !is_valid {
self.dcx().emit_err(errors::NoSanitize {
attr_span: item.span(),
defn_span: span,
accepted_kind: "a function",
attr_str: sym.as_str(),
});
}
}
}
}
}
}
fn check_generic_attr(
&self,
hir_id: HirId,

View file

@ -1848,3 +1848,14 @@ pub(crate) struct AttrCrateLevelOnlySugg {
#[primary_span]
pub attr: Span,
}
#[derive(Diagnostic)]
#[diag(passes_no_sanitize)]
pub(crate) struct NoSanitize<'a> {
#[primary_span]
pub attr_span: Span,
#[label]
pub defn_span: Span,
pub accepted_kind: &'a str,
pub attr_str: &'a str,
}