[PATCH] D79597: [asan_symbolize] Fix bug handling C++ symbols when using Atos.

Dan Liew via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 19 16:34:48 PDT 2020


delcypher updated this revision to Diff 265078.
delcypher added a comment.

Tweak comment per feedback.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79597/new/

https://reviews.llvm.org/D79597

Files:
  compiler-rt/lib/asan/scripts/asan_symbolize.py
  compiler-rt/test/asan/TestCases/Darwin/asan-symbolize-templated-cxx.cpp


Index: compiler-rt/test/asan/TestCases/Darwin/asan-symbolize-templated-cxx.cpp
===================================================================
--- /dev/null
+++ compiler-rt/test/asan/TestCases/Darwin/asan-symbolize-templated-cxx.cpp
@@ -0,0 +1,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;
+}
Index: compiler-rt/lib/asan/scripts/asan_symbolize.py
===================================================================
--- compiler-rt/lib/asan/scripts/asan_symbolize.py
+++ compiler-rt/lib/asan/scripts/asan_symbolize.py
@@ -275,11 +275,14 @@
       atos_line = self.atos.readline()
     # A well-formed atos response looks like this:
     #   foo(type1, type2) (in object.name) (filename.cc:80)
+    # NOTE:
+    #   * For C functions atos omits parentheses and argument types.
+    #   * For C++ functions the function name (i.e., `foo` above) may contain
+    #     templates which may contain parentheses.
     match = re.match('^(.*) \(in (.*)\) \((.*:\d*)\)$', atos_line)
     logging.debug('atos_line: %s', atos_line)
     if match:
       function_name = match.group(1)
-      function_name = re.sub('\(.*?\)', '', function_name)
       file_name = fix_filename(match.group(3))
       return ['%s in %s %s' % (addr, function_name, file_name)]
     else:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79597.265078.patch
Type: text/x-patch
Size: 3367 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200519/e44bf45b/attachment.bin>


More information about the llvm-commits mailing list