[llvm-commits] [compiler-rt] r161754 - in /compiler-rt/trunk/lib/asan: asan_interface.h asan_report.cc tests/asan_noinst_test.cc

Alexey Samsonov samsonov at google.com
Mon Aug 13 04:23:40 PDT 2012


Author: samsonov
Date: Mon Aug 13 06:23:40 2012
New Revision: 161754

URL: http://llvm.org/viewvc/llvm-project?rev=161754&view=rev
Log:
[ASan] Add __asan_set_on_error_callback() interface function that allows user to set a callback to be called right when ASan detects an error

Modified:
    compiler-rt/trunk/lib/asan/asan_interface.h
    compiler-rt/trunk/lib/asan/asan_report.cc
    compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc

Modified: compiler-rt/trunk/lib/asan/asan_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interface.h?rev=161754&r1=161753&r2=161754&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interface.h (original)
+++ compiler-rt/trunk/lib/asan/asan_interface.h Mon Aug 13 06:23:40 2012
@@ -118,6 +118,13 @@
   void __asan_set_error_report_callback(void (*callback)(const char*))
       SANITIZER_INTERFACE_ATTRIBUTE;
 
+  // Sets the callback to be called right when ASan detects an error.
+  // This can be used to notice cases when ASan detects an error, but the
+  // program crashes before ASan report is printed.
+  // Passing 0 unsets the callback.
+  void __asan_set_on_error_callback(void (*callback)(void))
+      SANITIZER_INTERFACE_ATTRIBUTE;
+
   // Returns the estimated number of bytes that will be reserved by allocator
   // for request of "size" bytes. If ASan allocator can't allocate that much
   // memory, returns the maximal possible allocation size, otherwise returns

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=161754&r1=161753&r2=161754&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_report.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_report.cc Mon Aug 13 06:23:40 2012
@@ -21,7 +21,7 @@
 
 namespace __asan {
 
-// ---------------------- Error report callback ------------------- {{{1
+// -------------------- User-specified callbacks ----------------- {{{1
 static void (*error_report_callback)(const char*);
 static char *error_message_buffer = 0;
 static uptr error_message_buffer_pos = 0;
@@ -40,6 +40,8 @@
   }
 }
 
+static void (*on_error_callback)(void);
+
 // ---------------------- Helper functions ----------------------- {{{1
 
 static void PrintBytes(const char *before, uptr *a) {
@@ -219,6 +221,9 @@
       SleepForSeconds(Max(5, flags()->sleep_before_dying + 1));
       Die();
     }
+    if (on_error_callback) {
+      on_error_callback();
+    }
     AsanPrintf("===================================================="
                "=============\n");
     AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
@@ -409,3 +414,7 @@
     error_message_buffer_pos = 0;
   }
 }
+
+void NOINLINE __asan_set_on_error_callback(void (*callback)(void)) {
+  on_error_callback = callback;
+}

Modified: compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc?rev=161754&r1=161753&r2=161754&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc Mon Aug 13 06:23:40 2012
@@ -530,6 +530,12 @@
   __asan_set_death_callback(NULL);
 }
 
+TEST(AddressSanitizerInterface, OnErrorCallbackTest) {
+  __asan_set_on_error_callback(MyDeathCallback);
+  EXPECT_DEATH(DoDoubleFree(), "MyDeathCallback.*double-free");
+  __asan_set_on_error_callback(NULL);
+}
+
 static const char* kUseAfterPoisonErrorMessage = "use-after-poison";
 
 #define GOOD_ACCESS(ptr, offset)  \





More information about the llvm-commits mailing list