Skip to content

Commit 9ee7903

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 1b89895 commit 9ee7903

File tree

13 files changed

+93
-22
lines changed

13 files changed

+93
-22
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
254254
}
255255
}
256256
}
257-
sym::link_ordinal => {
258-
link_ordinal_span = Some(attr.span());
259-
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
260-
codegen_fn_attrs.link_ordinal = ordinal;
261-
}
262-
}
263257
sym::sanitize => sanitize_span = Some(attr.span()),
264258
sym::instruction_set => {
265259
codegen_fn_attrs.instruction_set =
@@ -541,10 +535,13 @@ fn parse_sanitize_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> SanitizerSet {
541535
};
542536
let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
543537
match segments.as_slice() {
544-
[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) => {
545542
result |= SanitizerSet::ADDRESS | SanitizerSet::KERNELADDRESS
546543
}
547-
[sym::address] if set.value_str() == Some(sym::on) => {
544+
[sym::address] | [sym::kernel_address] if set.value_str() == Some(sym::on) => {
548545
result &= !SanitizerSet::ADDRESS;
549546
result &= !SanitizerSet::KERNELADDRESS;
550547
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ pub(crate) struct InvalidSanitize {
11081108
pub span: Span,
11091109
}
11101110

1111+
#[derive(Diagnostic)]
11111112
#[diag(codegen_ssa_target_feature_safe_trait)]
11121113
pub(crate) struct TargetFeatureSafeTrait {
11131114
#[primary_span]

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
@@ -549,7 +549,7 @@ passes_object_lifetime_err =
549549
{$repr}
550550
551551
passes_only_has_effect_on =
552-
`#[{$attr_name}]` only has an effect on {$passes_only_has_effect_on ->
552+
`#[{$attr_name}]` only has an effect on {$target_name ->
553553
[function] functions
554554
[module] modules
555555
[implementation_block] implementation blocks

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@ symbols! {
12451245
iterator,
12461246
iterator_collect_fn,
12471247
kcfi,
1248+
kernel_address,
12481249
keylocker_x86,
12491250
keyword,
12501251
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
@@ -11,7 +11,7 @@
1111
#![feature(min_generic_const_args)]
1212
#![feature(ffi_const, ffi_pure)]
1313
#![feature(coverage_attribute)]
14-
#![feature(no_sanitize)]
14+
#![feature(sanitize)]
1515
#![feature(marker_trait_attr)]
1616
#![feature(thread_local)]
1717
#![feature(must_not_suspend)]
@@ -89,7 +89,7 @@
8989
//~^ ERROR malformed
9090
#[coverage]
9191
//~^ ERROR malformed `coverage` attribute input
92-
#[no_sanitize]
92+
#[sanitize]
9393
//~^ ERROR malformed
9494
#[ignore()]
9595
//~^ ERROR valid forms for the attribute are

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ LL | #[coverage(off)]
5353
LL | #[coverage(on)]
5454
| ++++
5555

56-
error: malformed `no_sanitize` attribute input
56+
error: malformed `sanitize` attribute input
5757
--> $DIR/malformed-attrs.rs:92:1
5858
|
59-
LL | #[no_sanitize]
60-
| ^^^^^^^^^^^^^^ help: must be of the form: `#[no_sanitize(address, kcfi, memory, thread)]`
59+
LL | #[sanitize]
60+
| ^^^^^^^^^^^ help: must be of the form: `#[sanitize(address = "on|off", cfi = "on|off")]`
6161

6262
error: malformed `proc_macro` attribute input
6363
--> $DIR/malformed-attrs.rs:99:1

0 commit comments

Comments
 (0)