File: asan-symbolize-templated-cxx.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (62 lines) | stat: -rw-r--r-- 2,086 bytes parent folder | download | duplicates (37)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// UNSUPPORTED: ios
// RUN: %clangxx_asan -O0 -g %s -o %t.executable
// RUN: %env_asan_opts="symbolize=0" not %run %t.executable > %t_no_module_map.log 2>&1
// RUN: %asan_symbolize --force-system-symbolizer < %t_no_module_map.log 2>&1 | FileCheck %s
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>

// This test is deliberately convoluted so that there is a function call
// in the stack trace that contains nested parentheses.

template <class CallBackTy>
class IntWrapper {
  int value_;
  std::function<CallBackTy> callback_;

public:
  IntWrapper(int value, std::function<CallBackTy> callback) : value_(value), callback_(callback) {}
  int &operator=(const int &new_value) {
    value_ = new_value;
    callback_(value_);
  }
};

using IntW = IntWrapper<void(int)>;
IntW *a;

template <class T>
void writeToA(T new_value) {
  // CHECK: heap-use-after-free
  // NOTE: atos seems to emit the `void` return type here for some reason.
  // CHECK: #{{[0-9]+}} 0x{{.+}} in {{(void +)?}}writeToA<IntWrapper<void{{ *}}(int)>{{ *}}>(IntWrapper<void{{ *}}(int)>) asan-symbolize-templated-cxx.cpp:[[@LINE+1]]
  *a = new_value;
}

extern "C" void callback(int new_value) {
  printf("new value is %d\n", new_value);
}

template <class T, class V>
struct Foo {
  std::function<T> call;
  Foo(std::function<T> c) : call(c) {}
  void doCall(V new_value) {
    // CHECK: #{{[0-9]+}} 0x{{.+}} in Foo<void (IntWrapper<void{{ *}}(int)>),{{ *}}IntWrapper<void{{ *}}(int)>{{ *}}>::doCall(IntWrapper<void{{ *}}(int)>) asan-symbolize-templated-cxx.cpp:[[@LINE+1]]
    call(new_value);
  }
};

int main() {
  a = new IntW(0, callback);
  assert(a);
  // Foo<void(IntWrapper<void(int)>)>
  // This type is deliberately convoluted so that the demangled type contains nested parentheses.
  // In particular trying to match parentheses using a least-greedy regex approach will fail.
  Foo<void(IntW), IntW> foo(writeToA<IntW>);
  delete a;
  // CHECK: #{{[0-9]+}} 0x{{.+}} in main asan-symbolize-templated-cxx.cpp:[[@LINE+1]]
  foo.doCall(IntW(5, callback)); // BOOM
  return 0;
}