Skip to content

Commit 4b4437c

Browse files
committed
[asan] Enable detect_stack_use_after_return=1 by default
By default -fsanitize=address already compiles with this check, why not use it. For compatibly it can be disabled with env ASAN_OPTIONS=detect_stack_use_after_return=0. Reviewed By: eugenis, kda, #sanitizers, hans Differential Revision: https://reviews.llvm.org/D124057
1 parent debfb96 commit 4b4437c

15 files changed

+29
-19
lines changed

clang/docs/AddressSanitizer.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ following types of bugs:
1515
* Out-of-bounds accesses to heap, stack and globals
1616
* Use-after-free
1717
* Use-after-return (clang flag ``-fsanitize-address-use-after-return=(never|runtime|always)`` default: ``runtime``)
18-
* Enable ``runtime`` with: ``ASAN_OPTIONS=detect_stack_use_after_return=1``
18+
* Disable ``runtime`` with: ``ASAN_OPTIONS=detect_stack_use_after_return=0``
1919
* Use-after-scope (clang flag ``-fsanitize-address-use-after-scope``)
2020
* Double-free, invalid free
2121
* Memory leaks (experimental)
@@ -143,17 +143,17 @@ Stack Use After Return (UAR)
143143
AddressSanitizer can optionally detect stack use after return problems.
144144
This is available by default, or explicitly
145145
(``-fsanitize-address-use-after-return=runtime``).
146-
To enable this check at runtime, set the environment variable
147-
``ASAN_OPTIONS=detect_stack_use_after_return=1``.
146+
To disable this check at runtime, set the environment variable
147+
``ASAN_OPTIONS=detect_stack_use_after_return=0``.
148148

149149
Enabling this check (``-fsanitize-address-use-after-return=always``) will
150150
reduce code size. The code size may be reduced further by completely
151151
eliminating this check (``-fsanitize-address-use-after-return=never``).
152152

153153
To summarize: ``-fsanitize-address-use-after-return=<mode>``
154154
* ``never``: Completely disables detection of UAR errors (reduces code size).
155-
* ``runtime``: Adds the code for detection, but must be enabled via the
156-
runtime environment (``ASAN_OPTIONS=detect_stack_use_after_return=1``).
155+
* ``runtime``: Adds the code for detection, but it can be disable via the
156+
runtime environment (``ASAN_OPTIONS=detect_stack_use_after_return=0``).
157157
* ``always``: Enables detection of UAR errors in all cases. (reduces code
158158
size, but not as much as ``never``).
159159

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ Non-comprehensive list of changes in this release
173173
- Improve the dump format, dump both bitwidth(if its a bitfield) and field value.
174174
- Remove anonymous tag locations.
175175
- Beautify dump format, add indent for nested struct and struct members.
176-
- Previously disabled sanitizer options now enabled by default
176+
- Previously disabled sanitizer options now enabled by default:
177+
- ASAN_OPTIONS=detect_stack_use_after_return=1.
177178
- MSAN_OPTIONS=poison_in_dtor=1.
178179

179180
New Compiler Flags

compiler-rt/lib/asan/asan_flags.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ASAN_FLAG(
4949
"to find more errors.")
5050
ASAN_FLAG(bool, replace_intrin, true,
5151
"If set, uses custom wrappers for memset/memcpy/memmove intrinsics.")
52-
ASAN_FLAG(bool, detect_stack_use_after_return, false,
52+
ASAN_FLAG(bool, detect_stack_use_after_return, true,
5353
"Enables stack-use-after-return checking at run-time.")
5454
ASAN_FLAG(int, min_uar_stack_size_log, 16, // We can't do smaller anyway.
5555
"Minimum fake stack size log.")

compiler-rt/lib/asan/tests/asan_interface_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ TEST(AddressSanitizerInterface, HandleNoReturnTest) {
413413
__asan_poison_memory_region(array, sizeof(array));
414414
BAD_ACCESS(array, 20);
415415
__asan_handle_no_return();
416+
// Fake stack does not need to be unpoisoned.
417+
if (__asan_get_current_fake_stack())
418+
return;
416419
// It unpoisons the whole thread stack.
417420
GOOD_ACCESS(array, 20);
418421
}

compiler-rt/test/asan/TestCases/Posix/gc-test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// RUN: %clangxx_asan %s -pthread -o %t
22
// RUN: %env_asan_opts=detect_stack_use_after_return=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK1
33
// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK0
4+
// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK1
45
// RUN: %clangxx_asan -O3 %s -pthread -o %t
56
// RUN: %env_asan_opts=detect_stack_use_after_return=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK1
67
// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK0
8+
// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK1
79
// REQUIRES: stable-runtime
810

911
#include <assert.h>

compiler-rt/test/asan/TestCases/Posix/stack-use-after-return.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %clangxx_asan -O1 %s -pthread -o %t && %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
33
// RUN: %clangxx_asan -O2 %s -pthread -o %t && %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
44
// RUN: %clangxx_asan -O3 %s -pthread -o %t && %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
5+
// RUN: not %run %t 2>&1 | FileCheck %s
56
// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t
67
// RUN: %clangxx_asan -O0 %s -pthread -o %t -fsanitize-address-use-after-return=always && not %run %t 2>&1 | FileCheck %s
78
// RUN: %clangxx_asan -O1 %s -pthread -o %t -fsanitize-address-use-after-return=always && not %run %t 2>&1 | FileCheck %s

compiler-rt/test/asan/TestCases/Posix/unpoison-alternate-stack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Don't optimize, otherwise the variables which create redzones might be
55
// dropped.
66
// RUN: %clangxx_asan -fexceptions -O0 %s -o %t -pthread
7-
// RUN: %run %t
7+
// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t
88

99
#include <algorithm>
1010
#include <cassert>

compiler-rt/test/asan/TestCases/Windows/stack_use_after_return.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cl_asan -Od %s -Fe%t
22
// RUN: %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
3+
// RUN: not %run %t 2>&1 | FileCheck %s
34
// RUN: %clang_cl_asan -Od %s -Fe%t -fsanitize-address-use-after-return=always
45
// RUN: not %run %t 2>&1 | FileCheck %s
56

compiler-rt/test/asan/TestCases/alloca_loop_unpoisoning.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t
2-
// RUN: %run %t 2>&1
2+
// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t 2>&1
33
//
44
// REQUIRES: stable-runtime
55

compiler-rt/test/asan/TestCases/contiguous_container.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clangxx_asan -fexceptions -O %s -o %t && %run %t
1+
// RUN: %clangxx_asan -fexceptions -O %s -o %t && %env_asan_opts=detect_stack_use_after_return=0 %run %t
22
//
33
// Test __sanitizer_annotate_contiguous_container.
44

compiler-rt/test/asan/TestCases/handle_noreturn_bug.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Regression test: __asan_handle_no_return should unpoison stack even with poison_heap=0.
22
// Fails with debug checks: https://bugs.llvm.org/show_bug.cgi?id=46862
33
// XFAIL: !compiler-rt-optimized
4-
// RUN: %clangxx_asan -O0 %s -o %t && \
5-
// RUN: %env_asan_opts=poison_heap=1 %run %t && \
6-
// RUN: %env_asan_opts=poison_heap=0 %run %t
4+
// RUN: %clangxx_asan -O0 %s -o %t
5+
// RUN: %env_asan_opts=detect_stack_use_after_return=0:poison_heap=1 %run %t
6+
// RUN: %env_asan_opts=detect_stack_use_after_return=0:poison_heap=0 %run %t
77

88
#include <sanitizer/asan_interface.h>
99

compiler-rt/test/asan/TestCases/heavy_uar_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: %clangxx_asan -O0 %s -o %t && %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
2-
// RUN: %clangxx_asan -O2 %s -o %t && %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
1+
// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2+
// RUN: %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
3+
// RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
4+
// RUN: %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
35
// RUN: %clangxx_asan -O0 %s -o %t -fsanitize-address-use-after-return=always && not %run %t 2>&1 | FileCheck %s
46
// RUN: %clangxx_asan -O2 %s -o %t -fsanitize-address-use-after-return=always && not %run %t 2>&1 | FileCheck %s
57
// XFAIL: windows-msvc

compiler-rt/test/asan/TestCases/intercept-rethrow-exception.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// REQUIRES: shared_cxxabi
55

66
// RUN: %clangxx_asan -fexceptions -O0 %s -o %t
7-
// RUN: %run %t
7+
// RUN: %env_asan_opts=detect_stack_use_after_return=0 %run %t
88

99
// The current implementation of this functionality requires special
1010
// combination of libraries that are not used by default on NetBSD

compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-success.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair
22

33
// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 %run %t
4-
// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2,detect_stack_use_after_return=1 %run %t
4+
// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2,detect_stack_use_after_return=0 %run %t
55

66
#include <assert.h>
77
#include <stdlib.h>

llvm/include/llvm/Transforms/Instrumentation/AddressSanitizerOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ enum class AsanDtorKind {
2222
/// Mode of ASan detect stack use after return
2323
enum class AsanDetectStackUseAfterReturnMode {
2424
Never, ///< Never detect stack use after return.
25-
Runtime, ///< Detect stack use after return if runtime flag is enabled
26-
///< (ASAN_OPTIONS=detect_stack_use_after_return=1)
25+
Runtime, ///< Detect stack use after return if not disabled runtime with
26+
///< (ASAN_OPTIONS=detect_stack_use_after_return=0).
2727
Always, ///< Always detect stack use after return.
2828
Invalid, ///< Not a valid detect mode.
2929
};

0 commit comments

Comments
 (0)