Skip to content

Commit 8448eb2

Browse files
committed
replace SanitizerSet in CodegenFnAttrs by new type
1 parent 4068baf commit 8448eb2

File tree

11 files changed

+1450
-37
lines changed

11 files changed

+1450
-37
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, OptimizeAttr};
33
use rustc_hir::def_id::DefId;
44
use rustc_middle::middle::codegen_fn_attrs::{
5-
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
5+
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
66
};
77
use rustc_middle::ty::{self, TyCtxt};
88
use rustc_session::config::{BranchProtection, FunctionReturn, OptLevel, PAuthKey, PacRet};
@@ -98,10 +98,11 @@ fn patchable_function_entry_attrs<'ll>(
9898
pub(crate) fn sanitize_attrs<'ll, 'tcx>(
9999
cx: &SimpleCx<'ll>,
100100
tcx: TyCtxt<'tcx>,
101-
no_sanitize: SanitizerSet,
101+
sanitizer_fn_attr: SanitizerFnAttrs,
102102
) -> SmallVec<[&'ll Attribute; 4]> {
103103
let mut attrs = SmallVec::new();
104-
let enabled = tcx.sess.opts.unstable_opts.sanitizer - no_sanitize;
104+
// TODO: add realtim sanitizer
105+
let enabled = tcx.sess.opts.unstable_opts.sanitizer - sanitizer_fn_attr.disabled;
105106
if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
106107
attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
107108
}
@@ -417,7 +418,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
417418
// not used.
418419
} else {
419420
// Do not set sanitizer attributes for naked functions.
420-
to_add.extend(sanitize_attrs(cx, tcx, codegen_fn_attrs.no_sanitize));
421+
to_add.extend(sanitize_attrs(cx, tcx, codegen_fn_attrs.sanitizers));
421422

422423
// For non-naked functions, set branch protection attributes on aarch64.
423424
if let Some(BranchProtection { bti, pac_ret, gcs }) =

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_codegen_ssa::traits::*;
2020
use rustc_data_structures::small_c_str::SmallCStr;
2121
use rustc_hir::attrs::Linkage;
2222
use rustc_middle::dep_graph;
23-
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
23+
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, SanitizerFnAttrs};
2424
use rustc_middle::mir::mono::Visibility;
2525
use rustc_middle::ty::TyCtxt;
2626
use rustc_session::config::DebugInfo;
@@ -105,7 +105,7 @@ pub(crate) fn compile_codegen_unit(
105105
if let Some(entry) =
106106
maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit)
107107
{
108-
let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerSet::empty());
108+
let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default());
109109
attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs);
110110
}
111111

@@ -191,10 +191,10 @@ pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
191191
}
192192

193193
pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
194-
if attrs.no_sanitize.contains(SanitizerSet::ADDRESS) {
194+
if attrs.sanitizers.disabled.contains(SanitizerSet::ADDRESS) {
195195
unsafe { llvm::LLVMRustSetNoSanitizeAddress(llval) };
196196
}
197-
if attrs.no_sanitize.contains(SanitizerSet::HWADDRESS) {
197+
if attrs.sanitizers.disabled.contains(SanitizerSet::HWADDRESS) {
198198
unsafe { llvm::LLVMRustSetNoSanitizeHWAddress(llval) };
199199
}
200200
}

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
18031803
&& is_indirect_call
18041804
{
18051805
if let Some(fn_attrs) = fn_attrs
1806-
&& fn_attrs.no_sanitize.contains(SanitizerSet::CFI)
1806+
&& fn_attrs.sanitizers.disabled.contains(SanitizerSet::CFI)
18071807
{
18081808
return;
18091809
}
@@ -1861,7 +1861,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
18611861
&& is_indirect_call
18621862
{
18631863
if let Some(fn_attrs) = fn_attrs
1864-
&& fn_attrs.no_sanitize.contains(SanitizerSet::KCFI)
1864+
&& fn_attrs.sanitizers.disabled.contains(SanitizerSet::KCFI)
18651865
{
18661866
return None;
18671867
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
99
use rustc_hir::{self as hir, Attribute, LangItem, find_attr, lang_items};
1010
use rustc_middle::middle::codegen_fn_attrs::{
11-
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
11+
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
1212
};
1313
use rustc_middle::query::Providers;
1414
use rustc_middle::span_bug;
1515
use rustc_middle::ty::{self as ty, TyCtxt};
1616
use rustc_session::lint;
1717
use rustc_session::parse::feature_err;
1818
use rustc_span::{Ident, Span, sym};
19-
use rustc_target::spec::SanitizerSet;
2019

2120
use crate::errors;
2221
use crate::target_features::{
@@ -351,7 +350,8 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
351350
Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
352351

353352
// Compute the disabled sanitizers.
354-
codegen_fn_attrs.no_sanitize |= tcx.disabled_sanitizers_for(did);
353+
codegen_fn_attrs.sanitizers.disabled |=
354+
tcx.sanitizer_settings_for(did).disabled;
355355
// On trait methods, inherit the `#[align]` of the trait's method prototype.
356356
codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
357357

@@ -455,14 +455,14 @@ fn check_result(
455455
}
456456

457457
// warn that inline has no effect when no_sanitize is present
458-
if !codegen_fn_attrs.no_sanitize.is_empty()
458+
if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
459459
&& codegen_fn_attrs.inline.always()
460-
&& let (Some(no_sanitize_span), Some(inline_span)) =
460+
&& let (Some(sanitize_span), Some(inline_span)) =
461461
(interesting_spans.sanitize, interesting_spans.inline)
462462
{
463463
let hir_id = tcx.local_def_id_to_hir_id(did);
464-
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
465-
lint.primary_message("setting `sanitize` off will have no effect after inlining");
464+
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
465+
lint.primary_message("non-default `sanitize` will have no effect after inlining");
466466
lint.span_note(inline_span, "inlining requested here");
467467
})
468468
}
@@ -576,30 +576,30 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
576576
codegen_fn_attrs
577577
}
578578

579-
fn disabled_sanitizers_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerSet {
579+
fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
580580
// Backtrack to the crate root.
581-
let mut disabled = match tcx.opt_local_parent(did) {
581+
let mut settings = match tcx.opt_local_parent(did) {
582582
// Check the parent (recursively).
583-
Some(parent) => tcx.disabled_sanitizers_for(parent),
583+
Some(parent) => tcx.sanitizer_settings_for(parent),
584584
// We reached the crate root without seeing an attribute, so
585585
// there is no sanitizers to exclude.
586-
None => SanitizerSet::empty(),
586+
None => SanitizerFnAttrs::default(),
587587
};
588588

589589
// Check for a sanitize annotation directly on this def.
590590
if let Some((on_set, off_set)) = find_attr!(tcx.get_all_attrs(did), AttributeKind::Sanitize {on_set, off_set, ..} => (on_set, off_set))
591591
{
592592
// the on set is the set of sanitizers explicitly enabled.
593593
// we mask those out since we want the set of disabled sanitizers here
594-
disabled &= !*on_set;
594+
settings.disabled &= !*on_set;
595595
// the off set is the set of sanitizers explicitly disabled.
596596
// we or those in here.
597-
disabled |= *off_set;
597+
settings.disabled |= *off_set;
598598
// the on set and off set are distjoint since there's a third option: unset.
599599
// a node may not set the sanitizer setting in which case it inherits from parents.
600600
// the code above in this function does this backtracking
601601
}
602-
disabled
602+
settings
603603
}
604604

605605
/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
@@ -731,7 +731,7 @@ pub(crate) fn provide(providers: &mut Providers) {
731731
codegen_fn_attrs,
732732
should_inherit_track_caller,
733733
inherited_align,
734-
disabled_sanitizers_for,
734+
sanitizer_settings_for,
735735
..*providers
736736
};
737737
}

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ pub struct CodegenFnAttrs {
8080
/// The `#[link_section = "..."]` attribute, or what executable section this
8181
/// should be placed in.
8282
pub link_section: Option<Symbol>,
83-
/// The `#[sanitize(xyz = "off")]` attribute. Indicates sanitizers for which
84-
/// instrumentation should be disabled inside the function.
85-
pub no_sanitize: SanitizerSet,
83+
/// The `#[sanitize(xyz = "off")]` attribute. Indicates the settings for each
84+
/// sanitizer for this function.
85+
pub sanitizers: SanitizerFnAttrs,
8686
/// The `#[instruction_set(set)]` attribute. Indicates if the generated code should
8787
/// be generated against a specific instruction set. Only usable on architectures which allow
8888
/// switching between multiple instruction sets.
@@ -209,7 +209,7 @@ impl CodegenFnAttrs {
209209
linkage: None,
210210
import_linkage: None,
211211
link_section: None,
212-
no_sanitize: SanitizerSet::empty(),
212+
sanitizers: SanitizerFnAttrs::default(),
213213
instruction_set: None,
214214
alignment: None,
215215
patchable_function_entry: None,
@@ -241,3 +241,14 @@ impl CodegenFnAttrs {
241241
}
242242
}
243243
}
244+
245+
#[derive(Clone, Copy, Debug, HashStable, TyEncodable, TyDecodable, Eq, PartialEq)]
246+
pub struct SanitizerFnAttrs {
247+
pub disabled: SanitizerSet,
248+
}
249+
250+
impl SanitizerFnAttrs {
251+
pub const fn default() -> Self {
252+
Self { disabled: SanitizerSet::empty() }
253+
}
254+
}

compiler/rustc_middle/src/query/erase.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ trivial! {
344344
rustc_middle::ty::UnusedGenericParams,
345345
rustc_middle::ty::util::AlwaysRequiresDrop,
346346
rustc_middle::ty::Visibility<rustc_span::def_id::DefId>,
347+
rustc_middle::middle::codegen_fn_attrs::SanitizerFnAttrs,
347348
rustc_session::config::CrateType,
348349
rustc_session::config::EntryFnType,
349350
rustc_session::config::OptLevel,
@@ -361,7 +362,6 @@ trivial! {
361362
rustc_span::Symbol,
362363
rustc_span::Ident,
363364
rustc_target::spec::PanicStrategy,
364-
rustc_target::spec::SanitizerSet,
365365
rustc_type_ir::Variance,
366366
u32,
367367
usize,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ use rustc_session::lint::LintExpectationId;
9797
use rustc_span::def_id::LOCAL_CRATE;
9898
use rustc_span::source_map::Spanned;
9999
use rustc_span::{DUMMY_SP, Span, Symbol};
100-
use rustc_target::spec::{PanicStrategy, SanitizerSet};
100+
use rustc_target::spec::PanicStrategy;
101101
use {rustc_abi as abi, rustc_ast as ast, rustc_hir as hir};
102102

103103
pub use self::keys::{AsLocalKey, Key, LocalCrate};
104104
pub use self::plumbing::{IntoQueryParam, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk};
105105
use crate::infer::canonical::{self, Canonical};
106106
use crate::lint::LintExpectation;
107107
use crate::metadata::ModChild;
108-
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
108+
use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, SanitizerFnAttrs};
109109
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
110110
use crate::middle::deduced_param_attrs::DeducedParamAttrs;
111111
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
@@ -2723,8 +2723,8 @@ rustc_queries! {
27232723
/// `#[sanitize(xyz = "on")]` on this def and any enclosing defs, up to the
27242724
/// crate root.
27252725
///
2726-
/// Returns the set of sanitizers that is explicitly disabled for this def.
2727-
query disabled_sanitizers_for(key: LocalDefId) -> SanitizerSet {
2726+
/// Returns the sanitizer settings for this def.
2727+
query sanitizer_settings_for(key: LocalDefId) -> SanitizerFnAttrs {
27282728
desc { |tcx| "checking what set of sanitizers are enabled on `{}`", tcx.def_path_str(key) }
27292729
feedable
27302730
}

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ fn check_codegen_attributes<'tcx, I: Inliner<'tcx>>(
818818
}
819819

820820
let codegen_fn_attrs = tcx.codegen_fn_attrs(inliner.caller_def_id());
821-
if callee_attrs.no_sanitize != codegen_fn_attrs.no_sanitize {
821+
if callee_attrs.sanitizers != codegen_fn_attrs.sanitizers {
822822
return Err("incompatible sanitizer set");
823823
}
824824

0 commit comments

Comments
 (0)