Skip to content

Commit 758fd0b

Browse files
committed
Make 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 4292cb6 commit 758fd0b

File tree

15 files changed

+94
-27
lines changed

15 files changed

+94
-27
lines changed

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ codegen_ssa_invalid_monomorphization_unsupported_symbol = invalid monomorphizati
166166
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}`
167167
168168
codegen_ssa_invalid_sanitize = invalid argument for `sanitize`
169-
.note = expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`
169+
.note = expected one of: `address`, `kernel_address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow_call_stack`, or `thread`
170170
171171
codegen_ssa_invalid_windows_subsystem = invalid windows subsystem `{$subsystem}`, only `windows` and `console` are allowed
172172

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,13 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
535535
};
536536
let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
537537
match segments.as_slice() {
538-
[sym::address] if set.value_str() == Some(sym::off) => {
538+
// Similar to clang, sanitize(address = ..) and
539+
// sanitize(kernel_address = ..) control both ASan and KASan
540+
// Source: https://reviews.llvm.org/D44981.
541+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::off) => {
539542
result |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
540543
}
541-
[sym::address] if set.value_str() == Some(sym::on) => {
544+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::on) => {
542545
result &= !SanitizerSet::ADDRESS;
543546
result &= !SanitizerSet::KERNELADDRESS;
544547
}

compiler/rustc_feature/src/removed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ declare_features! (
192192
(removed, no_debug, "1.43.0", Some(29721), Some("removed due to lack of demand"), 69667),
193193
// Allows the use of `no_sanitize` attribute.
194194
/// 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")"#), 1234),
195+
(removed, no_sanitize, "CURRENT_RUSTC_VERSION", Some(39699), Some(r#"renamed to sanitize(xyz = "on|off")"#), 142681),
196196
/// Note: this feature was previously recorded in a separate
197197
/// `STABLE_REMOVED` list because it, uniquely, was once stable but was
198198
/// then removed. But there was no utility storing it separately, so now

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ passes_object_lifetime_err =
553553
{$repr}
554554
555555
passes_only_has_effect_on =
556-
`#[{$attr_name}]` only has an effect on {$passes_only_has_effect_on ->
556+
`#[{$attr_name}]` only has an effect on {$target_name ->
557557
[function] functions
558558
[module] modules
559559
[trait_implementation_block] trait implementation blocks

compiler/rustc_passes/src/check_attr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
313313
[sym::diagnostic, sym::on_unimplemented, ..] => {
314314
self.check_diagnostic_on_unimplemented(attr.span(), hir_id, target)
315315
}
316-
[sym::coverage, ..] => self.check_coverage(attr.span(), span, target),
317316
[sym::sanitize, ..] => {
318317
self.check_sanitize(attr, span, target)
319318
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ symbols! {
12471247
iterator,
12481248
iterator_collect_fn,
12491249
kcfi,
1250+
kernel_address,
12501251
keylocker_x86,
12511252
keyword,
12521253
kind,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Verifies that the `#[sanitize(address = "off")]` attribute also turns off
2+
// the kernel address sanitizer.
3+
//
4+
//@ add-core-stubs
5+
//@ compile-flags: -Zsanitizer=kernel-address -Ctarget-feature=-crt-static -Copt-level=0
6+
//@ revisions: aarch64 riscv64imac riscv64gc x86_64
7+
//@[aarch64] compile-flags: --target aarch64-unknown-none
8+
//@[aarch64] needs-llvm-components: aarch64
9+
//@[riscv64imac] compile-flags: --target riscv64imac-unknown-none-elf
10+
//@[riscv64imac] needs-llvm-components: riscv
11+
//@[riscv64gc] compile-flags: --target riscv64gc-unknown-none-elf
12+
//@[riscv64gc] needs-llvm-components: riscv
13+
//@[x86_64] compile-flags: --target x86_64-unknown-none
14+
//@[x86_64] needs-llvm-components: x86
15+
16+
#![crate_type = "rlib"]
17+
#![feature(no_core, sanitize, lang_items)]
18+
#![no_core]
19+
20+
extern crate minicore;
21+
use minicore::*;
22+
23+
// CHECK-LABEL: ; sanitize_off_asan_kasan::unsanitized
24+
// CHECK-NEXT: ; Function Attrs:
25+
// CHECK-NOT: sanitize_address
26+
// CHECK: start:
27+
// CHECK-NOT: call void @__asan_report_load
28+
// CHECK: }
29+
#[sanitize(address = "off")]
30+
pub fn unsanitized(b: &mut u8) -> u8 {
31+
*b
32+
}
33+
34+
// CHECK-LABEL: ; sanitize_off_asan_kasan::sanitized
35+
// CHECK-NEXT: ; Function Attrs:
36+
// CHECK: sanitize_address
37+
// CHECK: start:
38+
// CHECK: call void @__asan_report_load
39+
// CHECK: }
40+
pub fn sanitized(b: &mut u8) -> u8 {
41+
*b
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Verifies that the `#[sanitize(kernel_address = "off")]` attribute also turns off
2+
// the address sanitizer.
3+
//
4+
//@ needs-sanitizer-address
5+
//@ compile-flags: -Zsanitizer=address -Ctarget-feature=-crt-static -Copt-level=0
6+
7+
#![crate_type = "lib"]
8+
#![feature(sanitize)]
9+
10+
// CHECK-LABEL: ; sanitize_off_kasan_asan::unsanitized
11+
// CHECK-NEXT: ; Function Attrs:
12+
// CHECK-NOT: sanitize_address
13+
// CHECK: start:
14+
// CHECK-NOT: call void @__asan_report_load
15+
// CHECK: }
16+
#[sanitize(kernel_address = "off")]
17+
pub fn unsanitized(b: &mut u8) -> u8 {
18+
*b
19+
}
20+
21+
// CHECK-LABEL: ; sanitize_off_kasan_asan::sanitized
22+
// CHECK-NEXT: ; Function Attrs:
23+
// CHECK: sanitize_address
24+
// CHECK: start:
25+
// CHECK: call void @__asan_report_load
26+
// CHECK: }
27+
pub fn sanitized(b: &mut u8) -> u8 {
28+
*b
29+
}

tests/ui/attributes/malformed-attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#![feature(min_generic_const_args)]
1313
#![feature(ffi_const, ffi_pure)]
1414
#![feature(coverage_attribute)]
15-
#![feature(no_sanitize)]
15+
#![feature(sanitize)]
1616
#![feature(marker_trait_attr)]
1717
#![feature(thread_local)]
1818
#![feature(must_not_suspend)]
@@ -90,7 +90,7 @@
9090
//~^ ERROR malformed
9191
#[coverage]
9292
//~^ ERROR malformed `coverage` attribute input
93-
#[no_sanitize]
93+
#[sanitize]
9494
//~^ ERROR malformed
9595
#[ignore()]
9696
//~^ ERROR valid forms for the attribute are

0 commit comments

Comments
 (0)