[compiler-rt] r190932 - [asan] add a run-time option detect_stack_use_after_return, add verbosity output for fake stack
Kostya Serebryany
kcc at google.com
Wed Sep 18 03:35:13 PDT 2013
Author: kcc
Date: Wed Sep 18 05:35:12 2013
New Revision: 190932
URL: http://llvm.org/viewvc/llvm-project?rev=190932&view=rev
Log:
[asan] add a run-time option detect_stack_use_after_return, add verbosity output for fake stack
Modified:
compiler-rt/trunk/lib/asan/asan_fake_stack.cc
compiler-rt/trunk/lib/asan/asan_flags.h
compiler-rt/trunk/lib/asan/asan_interface_internal.h
compiler-rt/trunk/lib/asan/asan_rtl.cc
compiler-rt/trunk/lib/asan/asan_thread.cc
compiler-rt/trunk/lib/asan/asan_thread.h
compiler-rt/trunk/lib/asan/lit_tests/TestCases/stack-use-after-return.cc
Modified: compiler-rt/trunk/lib/asan/asan_fake_stack.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_fake_stack.cc?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_fake_stack.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_fake_stack.cc Wed Sep 18 05:35:12 2013
@@ -134,6 +134,8 @@ static FakeStack *GetFakeStack() {
static FakeStack *GetFakeStackFast() {
if (FakeStack *fs = GetTLSFakeStack())
return fs;
+ if (!__asan_option_detect_stack_use_after_return)
+ return 0;
return GetFakeStack();
}
Modified: compiler-rt/trunk/lib/asan/asan_flags.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_flags.h?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_flags.h (original)
+++ compiler-rt/trunk/lib/asan/asan_flags.h Wed Sep 18 05:35:12 2013
@@ -52,8 +52,8 @@ struct Flags {
bool replace_intrin;
// Used on Mac only.
bool mac_ignore_invalid_free;
- // ASan allocator flag.
- bool use_fake_stack;
+ // Enables stack-use-after-return checking at run-time.
+ bool detect_stack_use_after_return;
// ASan allocator flag. max_malloc_fill_size is the maximal amount of bytes
// that will be filled with malloc_fill_byte on malloc.
int max_malloc_fill_size, malloc_fill_byte;
Modified: compiler-rt/trunk/lib/asan/asan_interface_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interface_internal.h?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interface_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_interface_internal.h Wed Sep 18 05:35:12 2013
@@ -121,6 +121,10 @@ extern "C" {
/* OPTIONAL */ void __asan_malloc_hook(void *ptr, uptr size);
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
/* OPTIONAL */ void __asan_free_hook(void *ptr);
+
+ // Global flag, copy of ASAN_OPTIONS=detect_stack_use_after_return
+ SANITIZER_INTERFACE_ATTRIBUTE
+ extern int __asan_option_detect_stack_use_after_return;
} // extern "C"
#endif // ASAN_INTERFACE_INTERNAL_H
Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Wed Sep 18 05:35:12 2013
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "asan_allocator.h"
#include "asan_interceptors.h"
+#include "asan_interface_internal.h"
#include "asan_internal.h"
#include "asan_mapping.h"
#include "asan_poisoning.h"
@@ -26,6 +27,8 @@
#include "sanitizer_common/sanitizer_symbolizer.h"
#include "lsan/lsan_common.h"
+int __asan_option_detect_stack_use_after_return; // Global interface symbol.
+
namespace __asan {
uptr AsanMappingProfile[kAsanMappingProfileSize];
@@ -101,7 +104,8 @@ static void ParseFlagsFromString(Flags *
ParseFlag(str, &f->replace_str, "replace_str");
ParseFlag(str, &f->replace_intrin, "replace_intrin");
ParseFlag(str, &f->mac_ignore_invalid_free, "mac_ignore_invalid_free");
- ParseFlag(str, &f->use_fake_stack, "use_fake_stack");
+ ParseFlag(str, &f->detect_stack_use_after_return,
+ "detect_stack_use_after_return");
ParseFlag(str, &f->max_malloc_fill_size, "max_malloc_fill_size");
ParseFlag(str, &f->malloc_fill_byte, "malloc_fill_byte");
ParseFlag(str, &f->exitcode, "exitcode");
@@ -149,7 +153,7 @@ void InitializeFlags(Flags *f, const cha
f->replace_str = true;
f->replace_intrin = true;
f->mac_ignore_invalid_free = false;
- f->use_fake_stack = true;
+ f->detect_stack_use_after_return = true; // Also needs the compiler flag.
f->max_malloc_fill_size = 0x1000; // By default, fill only the first 4K.
f->malloc_fill_byte = 0xbe;
f->exitcode = ASAN_DEFAULT_FAILURE_EXITCODE;
@@ -455,6 +459,8 @@ void __asan_init() {
const char *options = GetEnv("ASAN_OPTIONS");
InitializeFlags(flags(), options);
__sanitizer_set_report_path(common_flags()->log_path);
+ __asan_option_detect_stack_use_after_return =
+ flags()->detect_stack_use_after_return;
if (flags()->verbosity && options) {
Report("Parsed ASAN_OPTIONS: %s\n", options);
Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Wed Sep 18 05:35:12 2013
@@ -123,8 +123,14 @@ FakeStack *AsanThread::AsyncSignalSafeLa
if (atomic_compare_exchange_strong(
reinterpret_cast<atomic_uintptr_t *>(&fake_stack_), &old_val, 1UL,
memory_order_relaxed)) {
- fake_stack_ = FakeStack::Create(Log2(RoundUpToPowerOfTwo(stack_size)));
+ uptr stack_size_log = Log2(RoundUpToPowerOfTwo(stack_size));
+ fake_stack_ = FakeStack::Create(stack_size_log);
SetTLSFakeStack(fake_stack_);
+ if (flags()->verbosity) {
+ u8 *p = reinterpret_cast<u8 *>(fake_stack_);
+ Report("T%d: FakeStack created: %p -- %p\n", tid(), p,
+ p + FakeStack::RequiredSize(stack_size_log));
+ }
return fake_stack_;
}
return 0;
Modified: compiler-rt/trunk/lib/asan/asan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.h?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.h (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.h Wed Sep 18 05:35:12 2013
@@ -89,6 +89,8 @@ class AsanThread {
}
FakeStack *fake_stack() {
+ if (!__asan_option_detect_stack_use_after_return)
+ return 0;
if (!has_fake_stack())
return AsyncSignalSafeLazyInitFakeStack();
return fake_stack_;
Modified: compiler-rt/trunk/lib/asan/lit_tests/TestCases/stack-use-after-return.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/TestCases/stack-use-after-return.cc?rev=190932&r1=190931&r2=190932&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/TestCases/stack-use-after-return.cc (original)
+++ compiler-rt/trunk/lib/asan/lit_tests/TestCases/stack-use-after-return.cc Wed Sep 18 05:35:12 2013
@@ -6,6 +6,7 @@
// RUN: not %t 2>&1 | FileCheck %s
// RUN: %clangxx_asan -fsanitize=use-after-return -O3 %s -o %t && \
// RUN: not %t 2>&1 | FileCheck %s
+// RUN: ASAN_OPTIONS=detect_stack_use_after_return=0 %t
// Regression test for a CHECK failure with small stack size and large frame.
// RUN: %clangxx_asan -fsanitize=use-after-return -O3 %s -o %t -DkSize=10000 && \
// RUN: (ulimit -s 65; not %t) 2>&1 | FileCheck %s
More information about the llvm-commits
mailing list