-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[sanitizer] Allow use-after-scope front-end argument to take effect with -fsanitize=kernel-address #137015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sanitizer] Allow use-after-scope front-end argument to take effect with -fsanitize=kernel-address #137015
Conversation
…ze=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.
|
@llvm/pr-subscribers-clang Author: Douglas (dgg5503) ChangesLifetime intrinsics required for detection of use-after-scope are not emitted under kernel-address sanitizer ( This is because with llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp Lines 60 to 75 in 24c8605
The reason llvm-project/clang/lib/Driver/SanitizerArgs.cpp Line 1004 in 24c8605
This check excludes This PR allows Full diff: https://github.com/llvm/llvm-project/pull/137015.diff 3 Files Affected:
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index f27cb813012f2..b428ded90a72e 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -1099,7 +1099,13 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
}
} else {
- AsanUseAfterScope = false;
+ if (AllAddedKinds & SanitizerKind::KernelAddress) {
+ AsanUseAfterScope = Args.hasFlag(
+ options::OPT_fsanitize_address_use_after_scope,
+ options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
+ } else {
+ AsanUseAfterScope = false;
+ }
// -fsanitize=pointer-compare/pointer-subtract requires -fsanitize=address.
SanitizerMask DetectInvalidPointerPairs =
SanitizerKind::PointerCompare | SanitizerKind::PointerSubtract;
diff --git a/clang/test/CodeGen/lifetime-sanitizer.c b/clang/test/CodeGen/lifetime-sanitizer.c
index b15d692b79e36..68879fda1e1a5 100644
--- a/clang/test/CodeGen/lifetime-sanitizer.c
+++ b/clang/test/CodeGen/lifetime-sanitizer.c
@@ -4,6 +4,9 @@
// RUN: -fsanitize=address -fsanitize-address-use-after-scope \
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefix=LIFETIME
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - -O0 \
+// RUN: -fsanitize=kernel-address -fsanitize-address-use-after-scope \
+// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefix=LIFETIME
+// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - -O0 \
// RUN: -fsanitize=memory -Xclang -disable-llvm-passes %s | \
// RUN: FileCheck %s -check-prefix=LIFETIME
// RUN: %clang -target aarch64-linux-gnu -S -emit-llvm -o - -O0 \
diff --git a/clang/test/CodeGenCXX/lifetime-sanitizer.cpp b/clang/test/CodeGenCXX/lifetime-sanitizer.cpp
index 33a8566092519..225d5e28921b8 100644
--- a/clang/test/CodeGenCXX/lifetime-sanitizer.cpp
+++ b/clang/test/CodeGenCXX/lifetime-sanitizer.cpp
@@ -5,6 +5,9 @@
// RUN: -fsanitize=address -fsanitize-address-use-after-scope \
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
// RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
+// RUN: -fsanitize=kernel-address -fsanitize-address-use-after-scope \
+// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
+// RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
// RUN: -fsanitize=memory -Xclang -disable-llvm-passes %s | \
// RUN: FileCheck %s -check-prefixes=CHECK,LIFETIME
// RUN: %clang -w -target aarch64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
|
|
@llvm/pr-subscribers-clang-driver Author: Douglas (dgg5503) ChangesLifetime intrinsics required for detection of use-after-scope are not emitted under kernel-address sanitizer ( This is because with llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp Lines 60 to 75 in 24c8605
The reason llvm-project/clang/lib/Driver/SanitizerArgs.cpp Line 1004 in 24c8605
This check excludes This PR allows Full diff: https://github.com/llvm/llvm-project/pull/137015.diff 3 Files Affected:
diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
index f27cb813012f2..b428ded90a72e 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -1099,7 +1099,13 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
}
} else {
- AsanUseAfterScope = false;
+ if (AllAddedKinds & SanitizerKind::KernelAddress) {
+ AsanUseAfterScope = Args.hasFlag(
+ options::OPT_fsanitize_address_use_after_scope,
+ options::OPT_fno_sanitize_address_use_after_scope, AsanUseAfterScope);
+ } else {
+ AsanUseAfterScope = false;
+ }
// -fsanitize=pointer-compare/pointer-subtract requires -fsanitize=address.
SanitizerMask DetectInvalidPointerPairs =
SanitizerKind::PointerCompare | SanitizerKind::PointerSubtract;
diff --git a/clang/test/CodeGen/lifetime-sanitizer.c b/clang/test/CodeGen/lifetime-sanitizer.c
index b15d692b79e36..68879fda1e1a5 100644
--- a/clang/test/CodeGen/lifetime-sanitizer.c
+++ b/clang/test/CodeGen/lifetime-sanitizer.c
@@ -4,6 +4,9 @@
// RUN: -fsanitize=address -fsanitize-address-use-after-scope \
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefix=LIFETIME
// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - -O0 \
+// RUN: -fsanitize=kernel-address -fsanitize-address-use-after-scope \
+// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefix=LIFETIME
+// RUN: %clang -target x86_64-linux-gnu -S -emit-llvm -o - -O0 \
// RUN: -fsanitize=memory -Xclang -disable-llvm-passes %s | \
// RUN: FileCheck %s -check-prefix=LIFETIME
// RUN: %clang -target aarch64-linux-gnu -S -emit-llvm -o - -O0 \
diff --git a/clang/test/CodeGenCXX/lifetime-sanitizer.cpp b/clang/test/CodeGenCXX/lifetime-sanitizer.cpp
index 33a8566092519..225d5e28921b8 100644
--- a/clang/test/CodeGenCXX/lifetime-sanitizer.cpp
+++ b/clang/test/CodeGenCXX/lifetime-sanitizer.cpp
@@ -5,6 +5,9 @@
// RUN: -fsanitize=address -fsanitize-address-use-after-scope \
// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
// RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
+// RUN: -fsanitize=kernel-address -fsanitize-address-use-after-scope \
+// RUN: -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
+// RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
// RUN: -fsanitize=memory -Xclang -disable-llvm-passes %s | \
// RUN: FileCheck %s -check-prefixes=CHECK,LIFETIME
// RUN: %clang -w -target aarch64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
|
|
Hi @nikic, I added you as a reviewer since your name popped up as a suggestion. Please feel free to remove yourself from the reviewers list if you prefer. |
vitalybuka
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
|
However, please be aware that default for Asan is ON. Should Kasan default be OFF in the beginning? |
I'd be happy to flip the default to OFF for KASAN to preserve existing expectations if there are no objections. I'll wait a few hours for others to respond. |
@vitalybuka I'm not sure how many users test a kernel exclusively in With that said, I'm inclined to keep this defaulted to ON like normal ASAN. Does this sound reasonable, or am I missing some negative side-effects that you had in mind when posting this comment? Would it be worth mentioning this change as a release note regardless? EDIT: I realize one detail I missed when I wrote the description of the PR. The 'workaround' to get |
|
Ping for @vitalybuka , I'll merge this in by EOD if there are no objections based on my latest comment (for real this time 😀). |
LGTM, I guess it's easy to add -fno-sanitize-address-use-after-scope into kernel build files if needed. |
|
@vitalybuka , great, thanks so much for the review. |
…ith -fsanitize=kernel-address (llvm#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.
…ith -fsanitize=kernel-address (llvm#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.
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 -O0undershouldEmitLifetimeMarkersinclang\lib\CodeGen\CodeGenFunction.cpp,CGOpts.SanitizeAddressUseAfterScopeis set tofalse. Therefore, the following check,CGOpts.OptimizationLevel != 0, is run which evaluates tofalsethus preventing the emission of lifetime markers:llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp
Lines 60 to 75 in 24c8605
The reason
CGOpts.SanitizeAddressUseAfterScopeis 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 inclang\lib\Driver\SanitizerArgs.cpp, specifically,if (AllAddedKinds & SanitizerKind::Address):llvm-project/clang/lib/Driver/SanitizerArgs.cpp
Line 1004 in 24c8605
And later on down in this block:
llvm-project/clang/lib/Driver/SanitizerArgs.cpp
Lines 1037 to 1039 in 24c8605
This check excludes
SanitizerKind::KernelAddressfrom consideration, so even if-fsanitize-address-use-after-scopeis supplied as a front-end argument, it won't be passed tocc1thus preventinguse-after-scopechecks from being emitted under-fsanitize-kernel-address -O0. Higher optimization levels will allow emission of lifetime markers regardless thanks to the logic inshouldEmitLifetimeMarkers.This PR allows
-fsanitize-address-use-after-scopeto take effect under kernel-address sanitizer.