Skip to content

Commit a242c85

Browse files
committed
replace SanitizerSet in CodegenFnAttrs by new type
1 parent c9537a9 commit a242c85

File tree

11 files changed

+51
-37
lines changed

11 files changed

+51
-37
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 4 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,10 @@ 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+
let enabled = tcx.sess.opts.unstable_opts.sanitizer - sanitizer_fn_attr.disabled;
105105
if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
106106
attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
107107
}
@@ -417,7 +417,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
417417
// not used.
418418
} else {
419419
// Do not set sanitizer attributes for naked functions.
420-
to_add.extend(sanitize_attrs(cx, tcx, codegen_fn_attrs.no_sanitize));
420+
to_add.extend(sanitize_attrs(cx, tcx, codegen_fn_attrs.sanitizers));
421421

422422
// For non-naked functions, set branch protection attributes on aarch64.
423423
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/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#![feature(box_as_ptr)]
3939
#![feature(box_patterns)]
4040
#![feature(closure_track_caller)]
41+
#![feature(const_default)]
42+
#![feature(const_trait_impl)]
4143
#![feature(core_intrinsics)]
4244
#![feature(debug_closure_helpers)]
4345
#![feature(decl_macro)]

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 16 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,15 @@ 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 const Default for SanitizerFnAttrs {
251+
fn default() -> Self {
252+
Self { disabled: SanitizerSet::empty() }
253+
}
254+
255+
}

compiler/rustc_middle/src/query/erase.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ trivial! {
348348
rustc_middle::ty::UnusedGenericParams,
349349
rustc_middle::ty::util::AlwaysRequiresDrop,
350350
rustc_middle::ty::Visibility<rustc_span::def_id::DefId>,
351+
rustc_middle::middle::codegen_fn_attrs::SanitizerFnAttrs,
351352
rustc_session::config::CrateType,
352353
rustc_session::config::EntryFnType,
353354
rustc_session::config::OptLevel,
@@ -365,7 +366,6 @@ trivial! {
365366
rustc_span::Symbol,
366367
rustc_span::Ident,
367368
rustc_target::spec::PanicStrategy,
368-
rustc_target::spec::SanitizerSet,
369369
rustc_type_ir::Variance,
370370
u32,
371371
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};
@@ -2729,8 +2729,8 @@ rustc_queries! {
27292729
/// `#[sanitize(xyz = "on")]` on this def and any enclosing defs, up to the
27302730
/// crate root.
27312731
///
2732-
/// Returns the set of sanitizers that is explicitly disabled for this def.
2733-
query disabled_sanitizers_for(key: LocalDefId) -> SanitizerSet {
2732+
/// Returns the sanitizer settings for this def.
2733+
query sanitizer_settings_for(key: LocalDefId) -> SanitizerFnAttrs {
27342734
desc { |tcx| "checking what set of sanitizers are enabled on `{}`", tcx.def_path_str(key) }
27352735
feedable
27362736
}

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

tests/ui/sanitizer/inline-always-sanitize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#[inline(always)]
66
//~^ NOTE inlining requested here
77
#[sanitize(address = "off")]
8-
//~^ WARN setting `sanitize` off will have no effect after inlining
8+
//~^ WARN non-default `sanitize` will have no effect after inlining
99
//~| NOTE on by default
1010
fn x() {
1111
}

0 commit comments

Comments
 (0)