xref: /llvm-project/compiler-rt/test/asan/TestCases/Darwin/asan-symbolize-templated-cxx.cpp (revision 5811f3a9f8d300bfa764ffebedeb37fd13b73a4b)
1 // UNSUPPORTED: ios
2 // RUN: %clangxx_asan -O0 -g %s -o %t.executable
3 // RUN: %env_asan_opts="symbolize=0" not %run %t.executable > %t_no_module_map.log 2>&1
4 // RUN: %asan_symbolize --force-system-symbolizer < %t_no_module_map.log 2>&1 | FileCheck %s
5 #include <cassert>
6 #include <cstdio>
7 #include <cstdlib>
8 #include <functional>
9 
10 // This test is deliberately convoluted so that there is a function call
11 // in the stack trace that contains nested parentheses.
12 
13 template <class CallBackTy>
14 class IntWrapper {
15   int value_;
16   std::function<CallBackTy> callback_;
17 
18 public:
IntWrapper(int value,std::function<CallBackTy> callback)19   IntWrapper(int value, std::function<CallBackTy> callback) : value_(value), callback_(callback) {}
operator =(const int & new_value)20   int &operator=(const int &new_value) {
21     value_ = new_value;
22     callback_(value_);
23   }
24 };
25 
26 using IntW = IntWrapper<void(int)>;
27 IntW *a;
28 
29 template <class T>
writeToA(T new_value)30 void writeToA(T new_value) {
31   // CHECK: heap-use-after-free
32   // NOTE: atos seems to emit the `void` return type here for some reason.
33   // CHECK: #{{[0-9]+}} 0x{{.+}} in {{(void +)?}}writeToA<IntWrapper<void{{ *}}(int)>{{ *}}>(IntWrapper<void{{ *}}(int)>) asan-symbolize-templated-cxx.cpp:[[@LINE+1]]
34   *a = new_value;
35 }
36 
callback(int new_value)37 extern "C" void callback(int new_value) {
38   printf("new value is %d\n", new_value);
39 }
40 
41 template <class T, class V>
42 struct Foo {
43   std::function<T> call;
FooFoo44   Foo(std::function<T> c) : call(c) {}
doCallFoo45   void doCall(V new_value) {
46     // CHECK: #{{[0-9]+}} 0x{{.+}} in Foo<void (IntWrapper<void{{ *}}(int)>),{{ *}}IntWrapper<void{{ *}}(int)>{{ *}}>::doCall(IntWrapper<void{{ *}}(int)>) asan-symbolize-templated-cxx.cpp:[[@LINE+1]]
47     call(new_value);
48   }
49 };
50 
main()51 int main() {
52   a = new IntW(0, callback);
53   assert(a);
54   // Foo<void(IntWrapper<void(int)>)>
55   // This type is deliberately convoluted so that the demangled type contains nested parentheses.
56   // In particular trying to match parentheses using a least-greedy regex approach will fail.
57   Foo<void(IntW), IntW> foo(writeToA<IntW>);
58   delete a;
59   // CHECK: #{{[0-9]+}} 0x{{.+}} in main asan-symbolize-templated-cxx.cpp:[[@LINE+1]]
60   foo.doCall(IntW(5, callback)); // BOOM
61   return 0;
62 }
63