Skip to content

Commit a466546

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 a466546

File tree

10 files changed

+86
-15
lines changed

10 files changed

+86
-15
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_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/feature-gates/feature-gate-sanitize.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![feature(no_sanitize)] //~ ERROR feature has been removed [E0557]
1+
//@ normalize-stderr: "you are using [0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?( \([^)]*\))?" -> "you are using $$RUSTC_VERSION"
2+
#![feature(no_sanitize)] //~ ERROR feature has been removed
23

34
#[sanitize(address = "on")]
45
//~^ ERROR the `#[sanitize]` attribute is an experimental feature

tests/ui/feature-gates/feature-gate-sanitize.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0557]: feature has been removed
2-
--> $DIR/feature-gate-sanitize.rs:1:12
2+
--> $DIR/feature-gate-sanitize.rs:2:12
33
|
44
LL | #![feature(no_sanitize)]
55
| ^^^^^^^^^^^ feature has been removed
@@ -8,7 +8,7 @@ LL | #![feature(no_sanitize)]
88
= note: renamed to sanitize(xyz = "on|off")
99

1010
error[E0658]: the `#[sanitize]` attribute is an experimental feature
11-
--> $DIR/feature-gate-sanitize.rs:3:1
11+
--> $DIR/feature-gate-sanitize.rs:4:1
1212
|
1313
LL | #[sanitize(address = "on")]
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/sanitize-attr/invalid-sanitize.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ error: invalid argument for `sanitize`
4040
LL | #[sanitize(brontosaurus = "off")]
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4242
|
43-
= note: expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`
43+
= note: expected one of: `address`, `kernel_address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow_call_stack`, or `thread`
4444

4545
error: invalid argument for `sanitize`
4646
--> $DIR/invalid-sanitize.rs:15:1
4747
|
4848
LL | #[sanitize(address = "bogus")]
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050
|
51-
= note: expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`
51+
= note: expected one of: `address`, `kernel_address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow_call_stack`, or `thread`
5252

5353
error: aborting due to 6 previous errors
5454

0 commit comments

Comments
 (0)