[ASan] Demangle global names in error reports.
authorAlexey Samsonov <samsonov@google.com>
Wed, 27 Mar 2013 10:41:22 +0000 (10:41 +0000)
committerAlexey Samsonov <samsonov@google.com>
Wed, 27 Mar 2013 10:41:22 +0000 (10:41 +0000)
llvm-svn: 178131

compiler-rt/lib/asan/asan_report.cc
compiler-rt/lib/asan/lit_tests/global-demangle.cc [new file with mode: 0644]

index 11ffa43..522d713 100644 (file)
@@ -189,6 +189,12 @@ static void PrintGlobalNameIfASCII(const __asan_global &g) {
   Printf("  '%s' is ascii string '%s'\n", g.name, (char*)g.beg);
 }
 
+static const char *MaybeDemangleGlobalName(const char *name) {
+  // We can spoil names of globals with C linkage, so use an heuristic
+  // approach to check if the name should be demangled.
+  return (name[0] == '_' && name[1] == 'Z') ? Demangle(name) : name;
+}
+
 bool DescribeAddressRelativeToGlobal(uptr addr, uptr size,
                                      const __asan_global &g) {
   static const uptr kMinimalDistanceFromAnotherGlobal = 64;
@@ -208,7 +214,7 @@ bool DescribeAddressRelativeToGlobal(uptr addr, uptr size,
     Printf("%p is located %zd bytes inside", (void*)addr, addr - g.beg);
   }
   Printf(" of global variable '%s' from '%s' (0x%zx) of size %zu\n",
-             g.name, g.module_name, g.beg, g.size);
+             MaybeDemangleGlobalName(g.name), g.module_name, g.beg, g.size);
   Printf("%s", d.EndLocation());
   PrintGlobalNameIfASCII(g);
   return true;
diff --git a/compiler-rt/lib/asan/lit_tests/global-demangle.cc b/compiler-rt/lib/asan/lit_tests/global-demangle.cc
new file mode 100644 (file)
index 0000000..94d2849
--- /dev/null
@@ -0,0 +1,17 @@
+// Don't run through %symbolize to avoid c++filt demangling.
+// RUN: %clangxx_asan -m64 -O0 %s -o %t && %t 2>&1 | FileCheck %s
+
+namespace XXX {
+class YYY {
+ public:
+  static int ZZZ[];
+};
+int YYY::ZZZ[] = {0, 1, 2, 3};
+}
+
+int main(int argc, char **argv) {
+  return XXX::YYY::ZZZ[argc + 5];  // BOOM
+  // CHECK: {{READ of size 4 at 0x.*}}
+  // CHECK: {{0x.* is located 8 bytes to the right of global variable}}
+  // CHECK: 'XXX::YYY::ZZZ' {{.*}} of size 16
+}