| 1 | // RUN: %clangxx_asan -O0 %s -o %t |
| 2 | // RUN: not %run %t 0 2>&1 | FileCheck %s |
| 3 | // RUN: not %run %t 1 2>&1 | FileCheck %s |
| 4 | // RUN: not %run %t 2 2>&1 | FileCheck %s |
| 5 | // RUN: not %run %t 3 2>&1 | FileCheck %s |
| 6 | // RUN: not %run %t 4 2>&1 | FileCheck %s |
| 7 | // RUN: not %run %t 5 2>&1 | FileCheck %s |
| 8 | // RUN: not %run %t 6 2>&1 | FileCheck %s |
| 9 | // RUN: not %run %t 7 2>&1 | FileCheck %s |
| 10 | // RUN: not %run %t 8 2>&1 | FileCheck %s |
| 11 | // RUN: not %run %t 9 2>&1 | FileCheck %s |
| 12 | // RUN: not %run %t 10 2>&1 | FileCheck %s |
| 13 | |
| 14 | #include "defines.h" |
| 15 | #include <stdlib.h> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | template <class T> struct Ptr { |
| 20 | void Store(T *ptr) { t = ptr; } |
| 21 | |
| 22 | void Access() { *t = {}; } |
| 23 | |
| 24 | T *t; |
| 25 | }; |
| 26 | |
| 27 | template <class T, size_t N> struct Ptr<T[N]> { |
| 28 | using Type = T[N]; |
| 29 | void Store(Type *ptr) { t = *ptr; } |
| 30 | |
| 31 | void Access() { *t = {}; } |
| 32 | |
| 33 | T *t; |
| 34 | }; |
| 35 | |
| 36 | template <class T> ATTRIBUTE_NOINLINE void test() { |
| 37 | Ptr<T> ptr; |
| 38 | { |
| 39 | T x; |
| 40 | ptr.Store(&x); |
| 41 | } |
| 42 | |
| 43 | ptr.Access(); |
| 44 | // CHECK: ERROR: AddressSanitizer: stack-use-after-scope |
| 45 | // CHECK: #{{[0-9]+}} 0x{{.*}} in {{(void )?test.*\((void)?\) .*}}use-after-scope-types.cpp |
| 46 | // CHECK: Address 0x{{.*}} is located in stack of thread T{{.*}} at offset [[OFFSET:[^ ]+]] in frame |
| 47 | // {{\[}}[[OFFSET]], {{[0-9]+}}) 'x' |
| 48 | } |
| 49 | |
| 50 | int main(int argc, char **argv) { |
| 51 | using Tests = void (*)(); |
| 52 | Tests tests[] = { |
| 53 | &test<bool>, |
| 54 | &test<char>, |
| 55 | &test<int>, |
| 56 | &test<double>, |
| 57 | &test<float>, |
| 58 | &test<void*>, |
| 59 | &test<std::vector<std::string>>, |
| 60 | &test<int[3]>, |
| 61 | &test<int[1000]>, |
| 62 | &test<char[3]>, |
| 63 | &test<char[1000]>, |
| 64 | }; |
| 65 | |
| 66 | int n = atoi(nptr: argv[1]); |
| 67 | if (n == sizeof(tests) / sizeof(tests[0])) { |
| 68 | for (auto te : tests) |
| 69 | te(); |
| 70 | } else { |
| 71 | tests[n](); |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |