Move MSan lit-tests under test/msan
[blocksruntime.git] / lib / asan / lit_tests / TestCases / stack-use-after-return.cc
blob75313d4c7bd8abbcba71cb127a6ebfbda9f5787a
1 // RUN: export ASAN_OPTIONS=detect_stack_use_after_return=1
2 // RUN: %clangxx_asan -O0 %s -o %t && \
3 // RUN: not %t 2>&1 | FileCheck %s
4 // RUN: %clangxx_asan -O1 %s -o %t && \
5 // RUN: not %t 2>&1 | FileCheck %s
6 // RUN: %clangxx_asan -O2 %s -o %t && \
7 // RUN: not %t 2>&1 | FileCheck %s
8 // RUN: %clangxx_asan -O3 %s -o %t && \
9 // RUN: not %t 2>&1 | FileCheck %s
10 // RUN: ASAN_OPTIONS=detect_stack_use_after_return=0 %t
11 // Regression test for a CHECK failure with small stack size and large frame.
12 // RUN: %clangxx_asan -O3 %s -o %t -DkSize=10000 && \
13 // RUN: (ulimit -s 65; not %t) 2>&1 | FileCheck %s
15 // Test that we can find UAR in a thread other than main:
16 // RUN: %clangxx_asan -DUseThread -O2 %s -o %t && \
17 // RUN: not %t 2>&1 | FileCheck --check-prefix=THREAD %s
19 // Test the max_uar_stack_size_log/min_uar_stack_size_log flag.
21 // RUN: ASAN_OPTIONS=$ASAN_OPTIONS:max_uar_stack_size_log=20:verbosity=1 not %t 2>&1 | FileCheck --check-prefix=CHECK-20 %s
22 // RUN: ASAN_OPTIONS=$ASAN_OPTIONS:min_uar_stack_size_log=24:max_uar_stack_size_log=24:verbosity=1 not %t 2>&1 | FileCheck --check-prefix=CHECK-24 %s
24 #include <stdio.h>
25 #include <pthread.h>
27 #ifndef kSize
28 # define kSize 1
29 #endif
31 #ifndef UseThread
32 # define UseThread 0
33 #endif
35 __attribute__((noinline))
36 char *Ident(char *x) {
37 fprintf(stderr, "1: %p\n", x);
38 return x;
41 __attribute__((noinline))
42 char *Func1() {
43 char local[kSize];
44 return Ident(local);
47 __attribute__((noinline))
48 void Func2(char *x) {
49 fprintf(stderr, "2: %p\n", x);
50 *x = 1;
51 // CHECK: WRITE of size 1 {{.*}} thread T0
52 // CHECK: #0{{.*}}Func2{{.*}}stack-use-after-return.cc:[[@LINE-2]]
53 // CHECK: is located in stack of thread T0 at offset
54 // CHECK: 'local' <== Memory access at offset {{16|32}} is inside this variable
55 // THREAD: WRITE of size 1 {{.*}} thread T{{[1-9]}}
56 // THREAD: #0{{.*}}Func2{{.*}}stack-use-after-return.cc:[[@LINE-6]]
57 // THREAD: is located in stack of thread T{{[1-9]}} at offset
58 // THREAD: 'local' <== Memory access at offset {{16|32}} is inside this variable
59 // CHECK-20: T0: FakeStack created:{{.*}} stack_size_log: 20
60 // CHECK-24: T0: FakeStack created:{{.*}} stack_size_log: 24
63 void *Thread(void *unused) {
64 Func2(Func1());
65 return NULL;
68 int main(int argc, char **argv) {
69 #if UseThread
70 pthread_t t;
71 pthread_create(&t, 0, Thread, 0);
72 pthread_join(t, 0);
73 #else
74 Func2(Func1());
75 #endif
76 return 0;