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