[compiler-rt] r334316 - [Sanitizers] Check alignment != 0 for aligned_alloc and posix_memalign

Alex Shlyapnikov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 8 13:40:35 PDT 2018


Author: alekseyshl
Date: Fri Jun  8 13:40:35 2018
New Revision: 334316

URL: http://llvm.org/viewvc/llvm-project?rev=334316&view=rev
Log:
[Sanitizers] Check alignment != 0 for aligned_alloc and posix_memalign

Summary:
Move the corresponding tests to the common folder (as all of the
sanitizer allocators will support this feature soon) and add the checks
specific to aligned_alloc to ASan and LSan allocators.

Reviewers: vitalybuka

Subscribers: srhines, kubamracek, delcypher, #sanitizers, llvm-commits

Differential Revision: https://reviews.llvm.org/D47924

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
Removed:
    compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc
    compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc
    compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc
    compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc
Modified:
    compiler-rt/trunk/lib/asan/asan_allocator.cc
    compiler-rt/trunk/lib/asan/asan_allocator.h
    compiler-rt/trunk/lib/asan/asan_errors.cc
    compiler-rt/trunk/lib/asan/asan_errors.h
    compiler-rt/trunk/lib/asan/asan_malloc_linux.cc
    compiler-rt/trunk/lib/asan/asan_report.cc
    compiler-rt/trunk/lib/asan/asan_report.h
    compiler-rt/trunk/lib/lsan/lsan_allocator.cc
    compiler-rt/trunk/lib/lsan/lsan_allocator.h
    compiler-rt/trunk/lib/lsan/lsan_interceptors.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h

Modified: compiler-rt/trunk/lib/asan/asan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator.cc?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator.cc Fri Jun  8 13:40:35 2018
@@ -925,6 +925,17 @@ void *asan_memalign(uptr alignment, uptr
       instance.Allocate(size, alignment, stack, alloc_type, true));
 }
 
+void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack) {
+  if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
+    errno = errno_EINVAL;
+    if (AllocatorMayReturnNull())
+      return nullptr;
+    ReportInvalidAlignedAllocAlignment(size, alignment, stack);
+  }
+  return SetErrnoOnNull(
+      instance.Allocate(size, alignment, stack, FROM_MALLOC, true));
+}
+
 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
                         BufferedStackTrace *stack) {
   if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {

Modified: compiler-rt/trunk/lib/asan/asan_allocator.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator.h?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator.h (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator.h Fri Jun  8 13:40:35 2018
@@ -208,6 +208,7 @@ void *asan_realloc(void *p, uptr size, B
 void *asan_valloc(uptr size, BufferedStackTrace *stack);
 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
 
+void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack);
 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
                         BufferedStackTrace *stack);
 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);

Modified: compiler-rt/trunk/lib/asan/asan_errors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_errors.cc?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_errors.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_errors.cc Fri Jun  8 13:40:35 2018
@@ -216,6 +216,28 @@ void ErrorInvalidAllocationAlignment::Pr
   ReportErrorSummary(scariness.GetDescription(), stack);
 }
 
+void ErrorInvalidAlignedAllocAlignment::Print() {
+  Decorator d;
+  Printf("%s", d.Warning());
+  char tname[128];
+#if SANITIZER_POSIX
+  Report("ERROR: AddressSanitizer: invalid alignment requested in "
+         "aligned_alloc: %zd, alignment must be a power of two and the "
+         "requested size 0x%zx must be a multiple of alignment "
+         "(thread T%d%s)\n", alignment, size, tid,
+         ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
+#else
+  Report("ERROR: AddressSanitizer: invalid alignment requested in "
+         "aligned_alloc: %zd, the requested size 0x%zx must be a multiple of "
+         "alignment (thread T%d%s)\n", alignment, size, tid,
+         ThreadNameWithParenthesis(tid, tname, sizeof(tname)));
+#endif
+  Printf("%s", d.Default());
+  stack->Print();
+  PrintHintAllocatorCannotReturnNull("ASAN_OPTIONS");
+  ReportErrorSummary(scariness.GetDescription(), stack);
+}
+
 void ErrorInvalidPosixMemalignAlignment::Print() {
   Decorator d;
   Printf("%s", d.Warning());

Modified: compiler-rt/trunk/lib/asan/asan_errors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_errors.h?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_errors.h (original)
+++ compiler-rt/trunk/lib/asan/asan_errors.h Fri Jun  8 13:40:35 2018
@@ -190,6 +190,21 @@ struct ErrorInvalidAllocationAlignment :
   void Print();
 };
 
+struct ErrorInvalidAlignedAllocAlignment : ErrorBase {
+  const BufferedStackTrace *stack;
+  uptr size;
+  uptr alignment;
+
+  ErrorInvalidAlignedAllocAlignment() = default;  // (*)
+  ErrorInvalidAlignedAllocAlignment(u32 tid, BufferedStackTrace *stack_,
+                                    uptr size_, uptr alignment_)
+      : ErrorBase(tid, 10, "invalid-aligned-alloc-alignment"),
+        stack(stack_),
+        size(size_),
+        alignment(alignment_) {}
+  void Print();
+};
+
 struct ErrorInvalidPosixMemalignAlignment : ErrorBase {
   const BufferedStackTrace *stack;
   uptr alignment;
@@ -360,6 +375,7 @@ struct ErrorGeneric : ErrorBase {
   macro(CallocOverflow)                         \
   macro(PvallocOverflow)                        \
   macro(InvalidAllocationAlignment)             \
+  macro(InvalidAlignedAllocAlignment)           \
   macro(InvalidPosixMemalignAlignment)          \
   macro(AllocationSizeTooBig)                   \
   macro(RssLimitExceeded)                       \

Modified: compiler-rt/trunk/lib/asan/asan_malloc_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_malloc_linux.cc?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_malloc_linux.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_malloc_linux.cc Fri Jun  8 13:40:35 2018
@@ -158,7 +158,7 @@ INTERCEPTOR(void*, __libc_memalign, uptr
 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
 INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
   GET_STACK_TRACE_MALLOC;
-  return asan_memalign(boundary, size, &stack, FROM_MALLOC);
+  return asan_aligned_alloc(boundary, size, &stack);
 }
 #endif // SANITIZER_INTERCEPT_ALIGNED_ALLOC
 

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=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_report.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_report.cc Fri Jun  8 13:40:35 2018
@@ -278,6 +278,14 @@ void ReportInvalidAllocationAlignment(up
   in_report.ReportError(error);
 }
 
+void ReportInvalidAlignedAllocAlignment(uptr size, uptr alignment,
+                                        BufferedStackTrace *stack) {
+  ScopedInErrorReport in_report(/*fatal*/ true);
+  ErrorInvalidAlignedAllocAlignment error(GetCurrentTidOrInvalid(), stack,
+                                          size, alignment);
+  in_report.ReportError(error);
+}
+
 void ReportInvalidPosixMemalignAlignment(uptr alignment,
                                          BufferedStackTrace *stack) {
   ScopedInErrorReport in_report(/*fatal*/ true);

Modified: compiler-rt/trunk/lib/asan/asan_report.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_report.h?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_report.h (original)
+++ compiler-rt/trunk/lib/asan/asan_report.h Fri Jun  8 13:40:35 2018
@@ -62,6 +62,8 @@ void ReportCallocOverflow(uptr count, up
 void ReportPvallocOverflow(uptr size, BufferedStackTrace *stack);
 void ReportInvalidAllocationAlignment(uptr alignment,
                                       BufferedStackTrace *stack);
+void ReportInvalidAlignedAllocAlignment(uptr size, uptr alignment,
+                                        BufferedStackTrace *stack);
 void ReportInvalidPosixMemalignAlignment(uptr alignment,
                                          BufferedStackTrace *stack);
 void ReportAllocationSizeTooBig(uptr user_size, uptr total_size, uptr max_size,

Modified: compiler-rt/trunk/lib/lsan/lsan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_allocator.cc?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_allocator.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_allocator.cc Fri Jun  8 13:40:35 2018
@@ -157,6 +157,16 @@ int lsan_posix_memalign(void **memptr, u
   return 0;
 }
 
+void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack) {
+  if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
+    errno = errno_EINVAL;
+    if (AllocatorMayReturnNull())
+      return nullptr;
+    ReportInvalidAlignedAllocAlignment(size, alignment, &stack);
+  }
+  return SetErrnoOnNull(Allocate(stack, size, alignment, kAlwaysClearMemory));
+}
+
 void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack) {
   if (UNLIKELY(!IsPowerOfTwo(alignment))) {
     errno = errno_EINVAL;

Modified: compiler-rt/trunk/lib/lsan/lsan_allocator.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_allocator.h?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_allocator.h (original)
+++ compiler-rt/trunk/lib/lsan/lsan_allocator.h Fri Jun  8 13:40:35 2018
@@ -92,6 +92,7 @@ AllocatorCache *GetAllocatorCache();
 
 int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
                         const StackTrace &stack);
+void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
 void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
 void *lsan_malloc(uptr size, const StackTrace &stack);
 void lsan_free(void *p);

Modified: compiler-rt/trunk/lib/lsan/lsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_interceptors.cc?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_interceptors.cc Fri Jun  8 13:40:35 2018
@@ -122,7 +122,7 @@ INTERCEPTOR(void *, __libc_memalign, upt
 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
   ENSURE_LSAN_INITED;
   GET_STACK_TRACE_MALLOC;
-  return lsan_memalign(alignment, size, stack);
+  return lsan_aligned_alloc(alignment, size, stack);
 }
 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
 #else

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h?rev=334316&r1=334315&r2=334316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h Fri Jun  8 13:40:35 2018
@@ -44,16 +44,18 @@ INLINE void *SetErrnoOnNull(void *ptr) {
 // of alignment.
 INLINE bool CheckAlignedAllocAlignmentAndSize(uptr alignment, uptr size) {
 #if SANITIZER_POSIX
-  return IsPowerOfTwo(alignment) && (size & (alignment - 1)) == 0;
+  return alignment != 0 && IsPowerOfTwo(alignment) &&
+         (size & (alignment - 1)) == 0;
 #else
-  return size % alignment == 0;
+  return alignment != 0 && size % alignment == 0;
 #endif
 }
 
 // Checks posix_memalign() parameters, verifies that alignment is a power of two
 // and a multiple of sizeof(void *).
 INLINE bool CheckPosixMemalignAlignment(uptr alignment) {
-  return IsPowerOfTwo(alignment) && (alignment % sizeof(void *)) == 0; // NOLINT
+  return alignment != 0 && IsPowerOfTwo(alignment) &&
+         (alignment % sizeof(void *)) == 0; // NOLINT
 }
 
 // Returns true if calloc(size, n) call overflows on size*n calculation.

Removed: compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc?rev=334315&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc (removed)
@@ -1,25 +0,0 @@
-// RUN: %clangxx_asan -O0 %s -o %t
-// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s
-// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
-
-// UNSUPPORTED: android
-
-// REQUIRES: stable-runtime
-
-#include <stdio.h>
-#include <stdlib.h>
-
-extern void *aligned_alloc(size_t alignment, size_t size);
-
-int main() {
-  void *p = aligned_alloc(17, 100);
-  // CHECK: ERROR: AddressSanitizer: invalid allocation alignment: 17
-  // CHECK: {{#0 0x.* in .*}}{{aligned_alloc|memalign}}
-  // CHECK: {{#1 0x.* in main .*aligned_alloc-alignment.cc:}}[[@LINE-3]]
-  // CHECK: SUMMARY: AddressSanitizer: invalid-allocation-alignment
-
-  printf("pointer after failed aligned_alloc: %zd\n", (size_t)p);
-  // CHECK-NULL: pointer after failed aligned_alloc: 0
-
-  return 0;
-}

Removed: compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc?rev=334315&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc (removed)
@@ -1,22 +0,0 @@
-// RUN: %clangxx_asan -O0 %s -o %t
-// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s
-// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
-
-// REQUIRES: stable-runtime
-
-#include <stdio.h>
-#include <stdlib.h>
-
-int main() {
-  void *p = reinterpret_cast<void*>(42);
-  int res = posix_memalign(&p, 17, 100);
-  // CHECK: ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: 17
-  // CHECK: {{#0 0x.* in .*posix_memalign}}
-  // CHECK: {{#1 0x.* in main .*posix_memalign-alignment.cc:}}[[@LINE-3]]
-  // CHECK: SUMMARY: AddressSanitizer: invalid-posix-memalign-alignment
-
-  printf("pointer after failed posix_memalign: %zd\n", (size_t)p);
-  // CHECK-NULL: pointer after failed posix_memalign: 42
-
-  return 0;
-}

Removed: compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc?rev=334315&view=auto
==============================================================================
--- compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc (original)
+++ compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc (removed)
@@ -1,25 +0,0 @@
-// RUN: %clangxx_lsan -O0 %s -o %t
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s
-// RUN: %env_lsan_opts=allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
-
-// UNSUPPORTED: android
-
-// REQUIRES: stable-runtime
-
-#include <stdio.h>
-#include <stdlib.h>
-
-extern void *aligned_alloc(size_t alignment, size_t size);
-
-int main() {
-  void *p = aligned_alloc(17, 100);
-  // CHECK: {{ERROR: .*Sanitizer: invalid allocation alignment: 17}}
-  // CHECK: {{#0 0x.* in .*}}{{aligned_alloc|memalign}}
-  // CHECK: {{#1 0x.* in main .*aligned_alloc-alignment.cc:}}[[@LINE-3]]
-  // CHECK: {{SUMMARY: .*Sanitizer: invalid-allocation-alignment}}
-
-  printf("pointer after failed aligned_alloc: %zd\n", (size_t)p);
-  // CHECK-NULL: pointer after failed aligned_alloc: 0
-
-  return 0;
-}

Removed: compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc?rev=334315&view=auto
==============================================================================
--- compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc (original)
+++ compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc (removed)
@@ -1,22 +0,0 @@
-// RUN: %clangxx_lsan -O0 %s -o %t
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s
-// RUN: %env_lsan_opts=allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
-
-// REQUIRES: stable-runtime
-
-#include <stdio.h>
-#include <stdlib.h>
-
-int main() {
-  void *p = reinterpret_cast<void*>(42);
-  int res = posix_memalign(&p, 17, 100);
-  // CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in posix_memalign: 17}}
-  // CHECK: {{#0 0x.* in .*posix_memalign}}
-  // CHECK: {{#1 0x.* in main .*posix_memalign-alignment.cc:}}[[@LINE-3]]
-  // CHECK: {{SUMMARY: .*Sanitizer: invalid-posix-memalign-alignment}}
-
-  printf("pointer after failed posix_memalign: %zd\n", (size_t)p);
-  // CHECK-NULL: pointer after failed posix_memalign: 42
-
-  return 0;
-}

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc?rev=334316&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc Fri Jun  8 13:40:35 2018
@@ -0,0 +1,32 @@
+// RUN: %clangxx -O0 %s -o %t
+// RUN: %tool_options=allocator_may_return_null=0 not %run %t 17 2>&1 | FileCheck %s
+// RUN: %tool_options=allocator_may_return_null=0 not %run %t 0 2>&1 | FileCheck %s
+// RUN: %tool_options=allocator_may_return_null=1 %run %t 17 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %tool_options=allocator_may_return_null=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+
+// UNSUPPORTED: android, msan, tsan, ubsan
+
+// REQUIRES: stable-runtime
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+extern void *aligned_alloc(size_t alignment, size_t size);
+
+int main(int argc, char **argv) {
+  assert(argc == 2);
+  const int alignment = atoi(argv[1]);
+
+  void *p = aligned_alloc(alignment, 100);
+  // CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in aligned_alloc}}
+  // Handle a case when aligned_alloc is aliased by memalign.
+  // CHECK: {{#0 0x.* in .*}}{{aligned_alloc|memalign}}
+  // CHECK: {{#1 0x.* in main .*aligned_alloc-alignment.cc:}}[[@LINE-4]]
+  // CHECK: {{SUMMARY: .*Sanitizer: invalid-aligned-alloc-alignment}}
+
+  printf("pointer after failed aligned_alloc: %zd\n", (size_t)p);
+  // CHECK-NULL: pointer after failed aligned_alloc: 0
+
+  return 0;
+}

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc?rev=334316&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc Fri Jun  8 13:40:35 2018
@@ -0,0 +1,31 @@
+// RUN: %clangxx -O0 %s -o %t
+// RUN: %tool_options=allocator_may_return_null=0 not %run %t 17 2>&1 | FileCheck %s
+// RUN: %tool_options=allocator_may_return_null=0 not %run %t 0 2>&1 | FileCheck %s
+// RUN: %tool_options=allocator_may_return_null=1 %run %t 17 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %tool_options=allocator_may_return_null=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+
+// REQUIRES: stable-runtime
+
+// UNSUPPORTED: msan, tsan, ubsan
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+  assert(argc == 2);
+  const int alignment = atoi(argv[1]);
+
+  void *p = reinterpret_cast<void*>(42);
+
+  int res = posix_memalign(&p, alignment, 100);
+  // CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in posix_memalign}}
+  // CHECK: {{#0 0x.* in .*posix_memalign}}
+  // CHECK: {{#1 0x.* in main .*posix_memalign-alignment.cc:}}[[@LINE-3]]
+  // CHECK: {{SUMMARY: .*Sanitizer: invalid-posix-memalign-alignment}}
+
+  printf("pointer after failed posix_memalign: %zd\n", (size_t)p);
+  // CHECK-NULL: pointer after failed posix_memalign: 42
+
+  return 0;
+}




More information about the llvm-commits mailing list