Skip to content

Commit efd46bc

Browse files
authored
[sanitizer] Allow use-after-scope front-end argument to take effect with -fsanitize=kernel-address (#137015)
Allow `-f[no]-sanitize-address-use-after-scope` to take effect under kernel-address sanitizer (`-fsanitize=kernel-address`). `use-after-scope` is now enabled by default under kernel-address sanitizer. Previously, users may have enabled `use-after-scope` checks for kernel-address sanitizer via `-mllvm -asan-use-after-scope=true`. While this may have worked for optimization levels > O0, the required lifetime intrinsics to allow for `use-after-scope` detection were not emitted under O0. This commit ensures the required lifetime intrinsics are emitted under O0 with kernel-address sanitizer.
1 parent 6d7edbb commit efd46bc

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

clang/lib/Driver/SanitizerArgs.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,6 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
10281028
StableABI = Args.hasFlag(options::OPT_fsanitize_stable_abi,
10291029
options::OPT_fno_sanitize_stable_abi, false);
10301030

1031-
AsanUseAfterScope = Args.hasFlag(
1032-
options::OPT_fsanitize_address_use_after_scope,
1033-
options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
1034-
10351031
AsanPoisonCustomArrayCookie = Args.hasFlag(
10361032
options::OPT_fsanitize_address_poison_custom_array_cookie,
10371033
options::OPT_fno_sanitize_address_poison_custom_array_cookie,
@@ -1093,7 +1089,6 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
10931089
}
10941090

10951091
} else {
1096-
AsanUseAfterScope = false;
10971092
// -fsanitize=pointer-compare/pointer-subtract requires -fsanitize=address.
10981093
SanitizerMask DetectInvalidPointerPairs =
10991094
SanitizerKind::PointerCompare | SanitizerKind::PointerSubtract;
@@ -1107,6 +1102,14 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
11071102
}
11081103
}
11091104

1105+
if (AllAddedKinds & (SanitizerKind::Address | SanitizerKind::KernelAddress)) {
1106+
AsanUseAfterScope = Args.hasFlag(
1107+
options::OPT_fsanitize_address_use_after_scope,
1108+
options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
1109+
} else {
1110+
AsanUseAfterScope = false;
1111+
}
1112+
11101113
if (AllAddedKinds & SanitizerKind::HWAddress) {
11111114
if (Arg *HwasanAbiArg =
11121115
Args.getLastArg(options::OPT_fsanitize_hwaddress_abi_EQ)) {

clang/test/CodeGen/lifetime-sanitizer.c

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// RUN: -fsanitize=address -fsanitize-address-use-after-scope \
55
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefix=LIFETIME
66
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - -O0 \
7+
// RUN: -fsanitize=kernel-address -fsanitize-address-use-after-scope \
8+
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefix=LIFETIME
9+
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - -O0 \
710
// RUN: -fsanitize=memory -Xclang -disable-llvm-passes %s | \
811
// RUN: FileCheck %s -check-prefix=LIFETIME
912
// RUN: %clang -target aarch64-linux-gnu -S -emit-llvm -o - -O0 \

clang/test/CodeGenCXX/lifetime-sanitizer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// RUN: -fsanitize=address -fsanitize-address-use-after-scope \
66
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
77
// RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
8+
// RUN: -fsanitize=kernel-address -fsanitize-address-use-after-scope \
9+
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
10+
// RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
811
// RUN: -fsanitize=memory -Xclang -disable-llvm-passes %s | \
912
// RUN: FileCheck %s -check-prefixes=CHECK,LIFETIME
1013
// RUN: %clang -w -target aarch64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \

0 commit comments

Comments
 (0)