1 // This is the ASAN test of the same name ported to HWAsan.
2 
3 // RUN: %clangxx_hwasan -mllvm -hwasan-use-after-scope -std=c++11 -O0 %s -o %t
4 // RUN: %clangxx_hwasan -fno-exceptions -mllvm -hwasan-use-after-scope -std=c++11 -O0 %s -o %t-noexcept
5 
6 // RUN: not %run %t 0 2>&1 | FileCheck %s
7 // RUN: not %run %t 1 2>&1 | FileCheck %s
8 // RUN: not %run %t 2 2>&1 | FileCheck %s
9 // RUN: not %run %t 3 2>&1 | FileCheck %s
10 // RUN: not %run %t 4 2>&1 | FileCheck %s
11 // RUN: not %run %t 5 2>&1 | FileCheck %s
12 // RUN: not %run %t 6 2>&1 | FileCheck %s
13 // RUN: not %run %t 7 2>&1 | FileCheck %s
14 // RUN: not %run %t 8 2>&1 | FileCheck %s
15 // RUN: not %run %t 9 2>&1 | FileCheck %s
16 // RUN: not %run %t 10 2>&1 | FileCheck %s
17 
18 // RUN: not %run %t-noexcept 0 2>&1 | FileCheck %s
19 // RUN: not %run %t-noexcept 1 2>&1 | FileCheck %s
20 // RUN: not %run %t-noexcept 2 2>&1 | FileCheck %s
21 // RUN: not %run %t-noexcept 3 2>&1 | FileCheck %s
22 // RUN: not %run %t-noexcept 4 2>&1 | FileCheck %s
23 // RUN: not %run %t-noexcept 5 2>&1 | FileCheck %s
24 // RUN: not %run %t-noexcept 6 2>&1 | FileCheck %s
25 // RUN: not %run %t-noexcept 7 2>&1 | FileCheck %s
26 // RUN: not %run %t-noexcept 8 2>&1 | FileCheck %s
27 // RUN: not %run %t-noexcept 9 2>&1 | FileCheck %s
28 // RUN: not %run %t-noexcept 10 2>&1 | FileCheck %s
29 
30 // REQUIRES: aarch64-target-arch
31 // REQUIRES: stable-runtime
32 
33 #include <stdlib.h>
34 #include <string>
35 #include <vector>
36 
37 template <class T>
38 struct Ptr {
StorePtr39   void Store(T *ptr) { t = ptr; }
40 
AccessPtr41   void Access() { *t = {}; }
42 
43   T *t;
44 };
45 
46 template <class T, size_t N>
47 struct Ptr<T[N]> {
48   using Type = T[N];
StorePtr49   void Store(Type *ptr) { t = *ptr; }
50 
AccessPtr51   void Access() { *t = {}; }
52 
53   T *t;
54 };
55 
56 template <class T>
test()57 __attribute__((noinline)) void test() {
58   Ptr<T> ptr;
59   {
60     T x;
61     ptr.Store(&x);
62   }
63 
64   ptr.Access();
65   // CHECK: ERROR: HWAddressSanitizer: tag-mismatch
66   // CHECK:  #{{[0-9]+}} 0x{{.*}} in {{(void )?test.*\((void)?\) .*}}use-after-scope-types.cpp
67   // CHECK: Cause: stack tag-mismatch
68 }
69 
main(int argc,char ** argv)70 int main(int argc, char **argv) {
71   using Tests = void (*)();
72   Tests tests[] = {
73       &test<bool>,
74       &test<char>,
75       &test<int>,
76       &test<double>,
77       &test<float>,
78       &test<void *>,
79       &test<std::vector<std::string>>,
80       &test<int[3]>,
81       &test<int[1000]>,
82       &test<char[3]>,
83       &test<char[1000]>,
84   };
85 
86   int n = atoi(argv[1]);
87   if (n == sizeof(tests) / sizeof(tests[0])) {
88     for (auto te : tests)
89       te();
90   } else {
91     tests[n]();
92   }
93 
94   return 0;
95 }
96