diff options
author | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2014-02-18 11:49:52 +0000 |
---|---|---|
committer | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2014-02-18 11:49:52 +0000 |
commit | 627fdafefd13a8f8d379a397bab53438d0fab75a (patch) | |
tree | 54a75fed316cd1cb3a0ab4a39d881b0c6a5abf64 /test/asan/TestCases/stack-overflow.cc | |
parent | 37f2aec2a0442293947967f49aa071034b728521 (diff) |
[asan] Stack overflow detection.
Report segmentation faults near or below stack bottom as stack-overflow
(not stack-buffer-overflow!).
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@201565 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/asan/TestCases/stack-overflow.cc')
-rw-r--r-- | test/asan/TestCases/stack-overflow.cc | 57 |
1 files changed, 44 insertions, 13 deletions
diff --git a/test/asan/TestCases/stack-overflow.cc b/test/asan/TestCases/stack-overflow.cc index adf1c0784..c5887d521 100644 --- a/test/asan/TestCases/stack-overflow.cc +++ b/test/asan/TestCases/stack-overflow.cc @@ -1,16 +1,47 @@ -// RUN: %clangxx_asan -O0 %s -o %t && not %t 2>&1 | FileCheck %s -// RUN: %clangxx_asan -O1 %s -o %t && not %t 2>&1 | FileCheck %s -// RUN: %clangxx_asan -O2 %s -o %t && not %t 2>&1 | FileCheck %s -// RUN: %clangxx_asan -O3 %s -o %t && not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O0 %s -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O1 %s -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O2 %s -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O3 %s -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s + +// RUN: %clangxx_asan -O0 %s -DTHREAD -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O1 %s -DTHREAD -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O2 %s -DTHREAD -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s +// RUN: %clangxx_asan -O3 %s -DTHREAD -o %t && ASAN_OPTIONS=use_sigaltstack=1 not %t 2>&1 | FileCheck %s + +#include <assert.h> +#include <stdlib.h> +#include <pthread.h> + +const int BS = 1024; +volatile char x; + +void large_frame_func(char *p, int level) { + char buf[BS]; + if (p) + assert(p - buf >= BS); + buf[rand() % BS] = 1; + buf[rand() % BS] = 2; + x = buf[rand() % BS]; + volatile int y = 1; + if (y) + large_frame_func(buf, level + 1); + // CHECK: {{stack-overflow on address 0x.* \(pc 0x.* sp 0x.* bp 0x.* T.*\)}} + // Frame 0 may be anywhere (in rand(), for example). + // CHECK: {{ #1 0x.* in large_frame_func.*stack-overflow.cc:}}[[@LINE-3]] +} + +void *ThreadFn(void* unused) { + large_frame_func(0, 0); + return 0; +} -#include <string.h> int main(int argc, char **argv) { - char x[10]; - memset(x, 0, 10); - int res = x[argc * 10]; // BOOOM - // CHECK: {{READ of size 1 at 0x.* thread T0}} - // CHECK: {{ #0 0x.* in main .*stack-overflow.cc:}}[[@LINE-2]] - // CHECK: {{Address 0x.* is located in stack of thread T0 at offset}} - // CHECK-NEXT: in{{.*}}main{{.*}}stack-overflow.cc - return res; +#ifdef THREAD + pthread_t t; + pthread_create(&t, 0, ThreadFn, 0); + pthread_join(t, 0); +#else + large_frame_func(0, 0); +#endif + return 0; } |