-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Selectively disable sanitizer instrumentation #68164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# `no_sanitize` | ||
|
||
The tracking issue for this feature is: [#39699] | ||
|
||
[#39699]: https://github.com/rust-lang/rust/issues/39699 | ||
|
||
------------------------ | ||
|
||
The `no_sanitize` attribute can be used to selectively disable sanitizer | ||
instrumentation in an annotated function. This might be useful to: avoid | ||
instrumentation overhead in a performance critical function, or avoid | ||
instrumenting code that contains constructs unsupported by given sanitizer. | ||
|
||
The precise effect of this annotation depends on particular sanitizer in use. | ||
For example, with `no_sanitize(thread)`, the thread sanitizer will no longer | ||
instrument non-atomic store / load operations, but it will instrument atomic | ||
operations to avoid reporting false positives and provide meaning full stack | ||
traces. | ||
|
||
## Examples | ||
|
||
``` rust | ||
#![feature(no_sanitize)] | ||
|
||
#[no_sanitize(address)] | ||
fn foo() { | ||
// ... | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// ignore-tidy-filelength | ||
|
||
//! "Collection" is the process of determining the type and other external | ||
//! details of each item in Rust. Collection is specifically concerned | ||
//! with *inter-procedural* things -- for example, for a function | ||
|
@@ -2743,6 +2745,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { | |
|
||
let mut inline_span = None; | ||
let mut link_ordinal_span = None; | ||
let mut no_sanitize_span = None; | ||
for attr in attrs.iter() { | ||
if attr.check_name(sym::cold) { | ||
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD; | ||
|
@@ -2832,6 +2835,24 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { | |
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) { | ||
codegen_fn_attrs.link_ordinal = ordinal; | ||
} | ||
} else if attr.check_name(sym::no_sanitize) { | ||
no_sanitize_span = Some(attr.span); | ||
if let Some(list) = attr.meta_item_list() { | ||
for item in list.iter() { | ||
if item.check_name(sym::address) { | ||
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_SANITIZE_ADDRESS; | ||
} else if item.check_name(sym::memory) { | ||
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_SANITIZE_MEMORY; | ||
} else if item.check_name(sym::thread) { | ||
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_SANITIZE_THREAD; | ||
} else { | ||
tcx.sess | ||
.struct_span_err(item.span(), "invalid argument for `no_sanitize`") | ||
.note("expected one of: `address`, `memory` or `thread`") | ||
.emit(); | ||
} | ||
} | ||
} | ||
Comment on lines
+2840
to
+2855
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to extract this to a function. |
||
} | ||
} | ||
|
||
|
@@ -2911,7 +2932,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { | |
// purpose functions as they wouldn't have the right target features | ||
// enabled. For that reason we also forbid #[inline(always)] as it can't be | ||
// respected. | ||
|
||
if codegen_fn_attrs.target_features.len() > 0 { | ||
if codegen_fn_attrs.inline == InlineAttr::Always { | ||
if let Some(span) = inline_span { | ||
|
@@ -2924,6 +2944,22 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { | |
} | ||
} | ||
|
||
if codegen_fn_attrs.flags.intersects(CodegenFnAttrFlags::NO_SANITIZE_ANY) { | ||
if codegen_fn_attrs.inline == InlineAttr::Always { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We issue this lint for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rustc and LLVM go out of the way to respect On the other hand the |
||
if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) { | ||
let hir_id = tcx.hir().as_local_hir_id(id).unwrap(); | ||
tcx.struct_span_lint_hir( | ||
lint::builtin::INLINE_NO_SANITIZE, | ||
hir_id, | ||
no_sanitize_span, | ||
"`no_sanitize` will have no effect after inlining", | ||
) | ||
.span_note(inline_span, "inlining requested here") | ||
.emit(); | ||
} | ||
} | ||
} | ||
|
||
// Weak lang items have the same semantics as "std internal" symbols in the | ||
// sense that they're preserved through all our LTO passes and only | ||
// strippable by the linker. | ||
|
Uh oh!
There was an error while loading. Please reload this page.