Index: compiler-rt/trunk/lib/asan/asan_allocator.h =================================================================== --- compiler-rt/trunk/lib/asan/asan_allocator.h +++ compiler-rt/trunk/lib/asan/asan_allocator.h @@ -208,6 +208,7 @@ 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); Index: compiler-rt/trunk/lib/asan/asan_allocator.cc =================================================================== --- compiler-rt/trunk/lib/asan/asan_allocator.cc +++ compiler-rt/trunk/lib/asan/asan_allocator.cc @@ -925,6 +925,17 @@ 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))) { Index: compiler-rt/trunk/lib/asan/asan_errors.h =================================================================== --- compiler-rt/trunk/lib/asan/asan_errors.h +++ compiler-rt/trunk/lib/asan/asan_errors.h @@ -190,6 +190,21 @@ 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 @@ macro(CallocOverflow) \ macro(PvallocOverflow) \ macro(InvalidAllocationAlignment) \ + macro(InvalidAlignedAllocAlignment) \ macro(InvalidPosixMemalignAlignment) \ macro(AllocationSizeTooBig) \ macro(RssLimitExceeded) \ Index: compiler-rt/trunk/lib/asan/asan_errors.cc =================================================================== --- compiler-rt/trunk/lib/asan/asan_errors.cc +++ compiler-rt/trunk/lib/asan/asan_errors.cc @@ -216,6 +216,28 @@ 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()); Index: compiler-rt/trunk/lib/asan/asan_malloc_linux.cc =================================================================== --- compiler-rt/trunk/lib/asan/asan_malloc_linux.cc +++ compiler-rt/trunk/lib/asan/asan_malloc_linux.cc @@ -158,7 +158,7 @@ #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 Index: compiler-rt/trunk/lib/asan/asan_report.h =================================================================== --- compiler-rt/trunk/lib/asan/asan_report.h +++ compiler-rt/trunk/lib/asan/asan_report.h @@ -62,6 +62,8 @@ 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, Index: compiler-rt/trunk/lib/asan/asan_report.cc =================================================================== --- compiler-rt/trunk/lib/asan/asan_report.cc +++ compiler-rt/trunk/lib/asan/asan_report.cc @@ -278,6 +278,14 @@ 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); Index: compiler-rt/trunk/lib/lsan/lsan_allocator.h =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_allocator.h +++ compiler-rt/trunk/lib/lsan/lsan_allocator.h @@ -92,6 +92,7 @@ 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); Index: compiler-rt/trunk/lib/lsan/lsan_allocator.cc =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_allocator.cc +++ compiler-rt/trunk/lib/lsan/lsan_allocator.cc @@ -157,6 +157,16 @@ 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; Index: compiler-rt/trunk/lib/lsan/lsan_interceptors.cc =================================================================== --- compiler-rt/trunk/lib/lsan/lsan_interceptors.cc +++ compiler-rt/trunk/lib/lsan/lsan_interceptors.cc @@ -122,7 +122,7 @@ 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 Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_checks.h @@ -44,16 +44,18 @@ // 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. Index: compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc =================================================================== --- compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc +++ compiler-rt/trunk/test/asan/TestCases/Linux/aligned_alloc-alignment.cc @@ -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 -#include - -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; -} Index: compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc =================================================================== --- compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc +++ compiler-rt/trunk/test/asan/TestCases/Posix/posix_memalign-alignment.cc @@ -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 -#include - -int main() { - void *p = reinterpret_cast(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; -} Index: compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc =================================================================== --- compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc +++ compiler-rt/trunk/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc @@ -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 -#include - -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; -} Index: compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc =================================================================== --- compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc +++ compiler-rt/trunk/test/lsan/TestCases/Posix/posix_memalign-alignment.cc @@ -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 -#include - -int main() { - void *p = reinterpret_cast(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; -} Index: compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc =================================================================== --- compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc +++ compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc @@ -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 +#include +#include + +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; +} Index: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc =================================================================== --- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc +++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc @@ -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 +#include +#include + +int main(int argc, char **argv) { + assert(argc == 2); + const int alignment = atoi(argv[1]); + + void *p = reinterpret_cast(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; +}