Skip to content

Commit f448a60

Browse files
committed
Allow use-after-scope front-end argument to take effect with -fsanitize=kernel-address
Lifetime intrinsics required for detection of use-after-scope are not emitted under kernel-address sanitizer (`-fsanitize=kernel-address`) when paired with `-O0` & `-fsanitize-address-use-after-scope`. This is because with `-fsanitize=kernel-address -O0` under `shouldEmitLifetimeMarkers` in `clang\lib\CodeGen\CodeGenFunction.cpp`, `CGOpts.SanitizeAddressUseAfterScope` is set to `false`. Therefore, the following check, `CGOpts.OptimizationLevel != 0`, is run which evaluates to `false` thus preventing the emission of lifetime markers. The reason `CGOpts.SanitizeAddressUseAfterScope` is false stems from the fact that this variable is normally set via the frontend flag `-fsanitize-address-use-after-scope`, however, this flag only takes effect under normal address sanitizer due to the gated logic in `clang\lib\Driver\SanitizerArgs.cpp`, specifically, `if (AllAddedKinds & SanitizerKind::Address)`. This check excludes `SanitizerKind::KernelAddress` from consideration, so even if `-fsanitize-address-use-after-scope` is supplied as a front-end argument, it won't be passed to `cc1` thus preventing `use-after-scope` checks from being emitted under `-fsanitize-kernel-address -O0`. Higher optimization levels will allow emission of lifetime markers regardless thanks to the logic in `shouldEmitLifetimeMarkers`. This PR allows `-fsanitize-address-use-after-scope` to take effect under kernel-address sanitizer.
1 parent 2e389cb commit f448a60

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,13 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
10991099
}
11001100

11011101
} else {
1102-
AsanUseAfterScope = false;
1102+
if (AllAddedKinds & SanitizerKind::KernelAddress) {
1103+
AsanUseAfterScope = Args.hasFlag(
1104+
options::OPT_fsanitize_address_use_after_scope,
1105+
options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
1106+
} else {
1107+
AsanUseAfterScope = false;
1108+
}
11031109
// -fsanitize=pointer-compare/pointer-subtract requires -fsanitize=address.
11041110
SanitizerMask DetectInvalidPointerPairs =
11051111
SanitizerKind::PointerCompare | SanitizerKind::PointerSubtract;

clang/test/CodeGen/lifetime-sanitizer.c

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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)