xref: /llvm-project/compiler-rt/test/hwasan/TestCases/use-after-scope-types.cpp (revision 6cc9244baa63fcb7c6f35f46dab9fa17a421a6ce)
1 // This is the ASAN test of the same name ported to HWAsan.
2 
3 // RUN: %clangxx_hwasan -std=c++11 -O0 %s -o %t
4 // RUN: %clangxx_hwasan -fno-exceptions -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 || riscv64-target-arch
31 
32 #include <stdlib.h>
33 #include <string>
34 #include <vector>
35 
36 template <class T>
37 struct Ptr {
StorePtr38   void Store(T *ptr) { t = ptr; }
39 
AccessPtr40   void Access() { *t = {}; }
41 
42   T *t;
43 };
44 
45 template <class T, size_t N>
46 struct Ptr<T[N]> {
47   using Type = T[N];
StorePtr48   void Store(Type *ptr) { t = *ptr; }
49 
AccessPtr50   void Access() { *t = {}; }
51 
52   T *t;
53 };
54 
55 template <class T>
test()56 __attribute__((noinline)) void test() {
57   Ptr<T> ptr;
58   {
59     T x;
60     ptr.Store(&x);
61   }
62 
63   ptr.Access();
64   // CHECK: ERROR: HWAddressSanitizer: tag-mismatch
65   // CHECK:  #{{[0-9]+}} 0x{{.*}} in {{(void )?test.*\((void)?\) .*}}use-after-scope-types.cpp
66   // CHECK: Cause: stack tag-mismatch
67 }
68 
main(int argc,char ** argv)69 int main(int argc, char **argv) {
70   using Tests = void (*)();
71   Tests tests[] = {
72       &test<bool>,
73       &test<char>,
74       &test<int>,
75       &test<double>,
76       &test<float>,
77       &test<void *>,
78       &test<std::vector<std::string>>,
79       &test<int[3]>,
80       &test<int[1000]>,
81       &test<char[3]>,
82       &test<char[1000]>,
83   };
84 
85   int n = atoi(argv[1]);
86   if (n == sizeof(tests) / sizeof(tests[0])) {
87     for (auto te : tests)
88       te();
89   } else {
90     tests[n]();
91   }
92 
93   return 0;
94 }
95