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
13template <class CallBackTy>
14class IntWrapper {
15 int value_;
16 std::function<CallBackTy> callback_;
17
18public:
19 IntWrapper(int value, std::function<CallBackTy> callback) : value_(value), callback_(callback) {}
20 int &operator=(const int &new_value) {
21 value_ = new_value;
22 callback_(value_);
23 }
24};
25
26using IntW = IntWrapper<void(int)>;
27IntW *a;
28
29template <class T>
30void 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
37extern "C" void callback(int new_value) {
38 printf(format: "new value is %d\n", new_value);
39}
40
41template <class T, class V>
42struct Foo {
43 std::function<T> call;
44 Foo(std::function<T> c) : call(c) {}
45 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
51int 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(new_value: IntW(5, callback)); // BOOM
61 return 0;
62}
63

source code of compiler-rt/test/asan/TestCases/Darwin/asan-symbolize-templated-cxx.cpp