[sanitizer] support c11 aligned_alloc, Linux only for now
authorKostya Serebryany <kcc@google.com>
Fri, 4 Jul 2014 07:30:34 +0000 (07:30 +0000)
committerKostya Serebryany <kcc@google.com>
Fri, 4 Jul 2014 07:30:34 +0000 (07:30 +0000)
llvm-svn: 212322

compiler-rt/lib/asan/asan_malloc_linux.cc
compiler-rt/lib/lsan/lsan_interceptors.cc
compiler-rt/lib/msan/msan_interceptors.cc
compiler-rt/lib/tsan/rtl/tsan_interceptors.cc
compiler-rt/test/sanitizer_common/CMakeLists.txt
compiler-rt/test/sanitizer_common/TestCases/Linux/aligned_alloc.c [new file with mode: 0644]
compiler-rt/test/sanitizer_common/TestCases/print-stack-trace.cc
compiler-rt/test/sanitizer_common/lit.common.cfg

index 0c368d8..077a50c 100644 (file)
@@ -106,6 +106,11 @@ INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
   return asan_memalign(boundary, size, &stack, FROM_MALLOC);
 }
 
+INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
+  GET_STACK_TRACE_MALLOC;
+  return asan_memalign(boundary, size, &stack, FROM_MALLOC);
+}
+
 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
   GET_STACK_TRACE_MALLOC;
   void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
index 38dc62e..ad8ca90 100644 (file)
@@ -105,6 +105,12 @@ INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
   return Allocate(stack, size, alignment, kAlwaysClearMemory);
 }
 
+INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
+  ENSURE_LSAN_INITED;
+  GET_STACK_TRACE;
+  return Allocate(stack, size, alignment, kAlwaysClearMemory);
+}
+
 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
   ENSURE_LSAN_INITED;
   GET_STACK_TRACE;
index 7525818..74a19b8 100644 (file)
@@ -162,6 +162,13 @@ INTERCEPTOR(void *, memalign, SIZE_T boundary, SIZE_T size) {
   return ptr;
 }
 
+INTERCEPTOR(void *, aligned_alloc, SIZE_T boundary, SIZE_T size) {
+  GET_MALLOC_STACK_TRACE;
+  CHECK_EQ(boundary & (boundary - 1), 0);
+  void *ptr = MsanReallocate(&stack, 0, size, boundary, false);
+  return ptr;
+}
+
 INTERCEPTOR(void *, __libc_memalign, SIZE_T boundary, SIZE_T size) {
   GET_MALLOC_STACK_TRACE;
   CHECK_EQ(boundary & (boundary - 1), 0);
index 53c7c26..37e4650 100644 (file)
@@ -738,6 +738,11 @@ TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
   return user_alloc(thr, pc, sz, align);
 }
 
+TSAN_INTERCEPTOR(void*, aligned_alloc, uptr align, uptr sz) {
+  SCOPED_INTERCEPTOR_RAW(memalign, align, sz);
+  return user_alloc(thr, pc, sz, align);
+}
+
 TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
   SCOPED_INTERCEPTOR_RAW(valloc, sz);
   return user_alloc(thr, pc, sz, GetPageSizeCached());
index dcb09ca..13eecbd 100644 (file)
@@ -10,6 +10,7 @@ endif()
 if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT ANDROID)
   list(APPEND SUPPORTED_TOOLS tsan)
   list(APPEND SUPPORTED_TOOLS msan)
+  list(APPEND SUPPORTED_TOOLS lsan)
 endif()
 
 # Create a separate config for each tool we support.
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/aligned_alloc.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/aligned_alloc.c
new file mode 100644 (file)
index 0000000..12af18d
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang -std=c11 -O0 %s -o %t && %run %t
+#include <stdlib.h>
+extern void *aligned_alloc (size_t alignment, size_t size);
+int main() {
+  volatile void *p = aligned_alloc(128, 1024);
+  free((void*)p);
+  return 0;
+}
index 98d9ddf..c84d0da 100644 (file)
@@ -3,7 +3,7 @@
 //
 // Not yet implemented for TSan.
 // https://code.google.com/p/address-sanitizer/issues/detail?id=243
-// XFAIL: tsan
+// XFAIL: tsan,lsan
 
 #include <sanitizer/common_interface_defs.h>
 
index 6e2d772..6e768b1 100644 (file)
@@ -11,6 +11,8 @@ elif config.tool_name == "tsan":
   tool_cflags = ["-fsanitize=thread"]
 elif config.tool_name == "msan":
   tool_cflags = ["-fsanitize=memory"]
+elif config.tool_name == "lsan":
+  tool_cflags = ["-fsanitize=leak"]
 else:
   lit_config.fatal("Unknown tool for sanitizer_common tests: %r" % config.tool_name)