[compiler-rt] r177993 - [asan] print thread number while reporting invalid-free and double-free; add tests; also add a test for use-after-poison

Kostya Serebryany kcc at google.com
Tue Mar 26 01:01:37 PDT 2013


Author: kcc
Date: Tue Mar 26 03:01:37 2013
New Revision: 177993

URL: http://llvm.org/viewvc/llvm-project?rev=177993&view=rev
Log:
[asan] print thread number while reporting invalid-free and double-free; add tests; also add a test for use-after-poison

Added:
    compiler-rt/trunk/lib/asan/lit_tests/double-free.cc
    compiler-rt/trunk/lib/asan/lit_tests/invalid-free.cc
    compiler-rt/trunk/lib/asan/lit_tests/use-after-poison.cc
Modified:
    compiler-rt/trunk/lib/asan/asan_report.cc

Modified: compiler-rt/trunk/lib/asan/asan_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_report.cc?rev=177993&r1=177992&r2=177993&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_report.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_report.cc Tue Mar 26 03:01:37 2013
@@ -516,7 +516,13 @@ void ReportDoubleFree(uptr addr, StackTr
   ScopedInErrorReport in_report;
   Decorator d;
   Printf("%s", d.Warning());
-  Report("ERROR: AddressSanitizer: attempting double-free on %p:\n", addr);
+  char tname[128];
+  u32 curr_tid = GetCurrentTidOrInvalid();
+  Report("ERROR: AddressSanitizer: attempting double-free on %p in "
+         "thread T%d%s:\n",
+         addr, curr_tid,
+         ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
+
   Printf("%s", d.EndWarning());
   PrintStack(stack);
   DescribeHeapAddress(addr, 1);
@@ -527,8 +533,11 @@ void ReportFreeNotMalloced(uptr addr, St
   ScopedInErrorReport in_report;
   Decorator d;
   Printf("%s", d.Warning());
+  char tname[128];
+  u32 curr_tid = GetCurrentTidOrInvalid();
   Report("ERROR: AddressSanitizer: attempting free on address "
-             "which was not malloc()-ed: %p\n", addr);
+             "which was not malloc()-ed: %p in thread T%d%s\n", addr,
+         curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
   Printf("%s", d.EndWarning());
   PrintStack(stack);
   DescribeHeapAddress(addr, 1);

Added: compiler-rt/trunk/lib/asan/lit_tests/double-free.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/double-free.cc?rev=177993&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/double-free.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/double-free.cc Tue Mar 26 03:01:37 2013
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -m64 -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
+
+#include <stdlib.h>
+#include <string.h>
+int main(int argc, char **argv) {
+  char *x = (char*)malloc(10 * sizeof(char));
+  memset(x, 0, 10);
+  int res = x[argc];
+  free(x);
+  free(x + argc - 1);  // BOOM
+  // CHECK: AddressSanitizer: attempting double-free{{.*}}in thread T0
+  // CHECK: double-free.cc:[[@LINE-2]]
+  // CHECK: freed by thread T0 here:
+  // CHECK: double-free.cc:[[@LINE-5]]
+  // CHECK: allocated by thread T0 here:
+  // CHECK: double-free.cc:[[@LINE-10]]
+  return res;
+}

Added: compiler-rt/trunk/lib/asan/lit_tests/invalid-free.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/invalid-free.cc?rev=177993&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/invalid-free.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/invalid-free.cc Tue Mar 26 03:01:37 2013
@@ -0,0 +1,16 @@
+// RUN: %clangxx_asan -m64 -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
+
+#include <stdlib.h>
+#include <string.h>
+int main(int argc, char **argv) {
+  char *x = (char*)malloc(10 * sizeof(char));
+  memset(x, 0, 10);
+  int res = x[argc];
+  free(x + 5);  // BOOM
+  // CHECK: AddressSanitizer: attempting free on address{{.*}}in thread T0
+  // CHECK: invalid-free.cc:[[@LINE-2]]
+  // CHECK: is located 5 bytes inside of 10-byte region
+  // CHECK: allocated by thread T0 here:
+  // CHECK: invalid-free.cc:[[@LINE-8]]
+  return res;
+}

Added: compiler-rt/trunk/lib/asan/lit_tests/use-after-poison.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/use-after-poison.cc?rev=177993&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/use-after-poison.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/use-after-poison.cc Tue Mar 26 03:01:37 2013
@@ -0,0 +1,19 @@
+// Check that __asan_poison_memory_region works.
+// RUN: %clangxx_asan -m64 -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
+//
+// Check that we can disable it
+// RUN: ASAN_OPTIONS=allow_user_poisoning=0 %t
+
+#include <stdlib.h>
+
+extern "C" void __asan_poison_memory_region(void *, size_t);
+
+int main(int argc, char **argv) {
+  char *x = new char[16];
+  __asan_poison_memory_region(x, 16);
+  int res = x[argc * 10];  // BOOOM
+  // CHECK: ERROR: AddressSanitizer: use-after-poison
+  // CHECK: main{{.*}}use-after-poison.cc:[[@LINE-2]]
+  delete [] x;
+  return res;
+}





More information about the llvm-commits mailing list