diff options
| author | Alex Shlyapnikov <alekseys@google.com> | 2018-06-08 20:40:35 +0000 |
|---|---|---|
| committer | Alex Shlyapnikov <alekseys@google.com> | 2018-06-08 20:40:35 +0000 |
| commit | dcf0097962e4506aec59fbbd01c874527d2e061d (patch) | |
| tree | 1a41b71908299f93693d176a5cb9b94ea7a9de77 | |
| parent | 9e5eafbaa8ab0d49ccfd197a91b386e9aba28972 (diff) | |
| download | bcm5719-llvm-dcf0097962e4506aec59fbbd01c874527d2e061d.tar.gz bcm5719-llvm-dcf0097962e4506aec59fbbd01c874527d2e061d.zip | |
[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
llvm-svn: 334316
17 files changed, 141 insertions, 99 deletions
diff --git a/compiler-rt/lib/asan/asan_allocator.cc b/compiler-rt/lib/asan/asan_allocator.cc index b8f7eb5741c..c0fad4fa042 100644 --- a/compiler-rt/lib/asan/asan_allocator.cc +++ b/compiler-rt/lib/asan/asan_allocator.cc @@ -925,6 +925,17 @@ void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack, 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))) { diff --git a/compiler-rt/lib/asan/asan_allocator.h b/compiler-rt/lib/asan/asan_allocator.h index 1f1a71294ed..93d6f29c5bf 100644 --- a/compiler-rt/lib/asan/asan_allocator.h +++ b/compiler-rt/lib/asan/asan_allocator.h @@ -208,6 +208,7 @@ void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack); 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); diff --git a/compiler-rt/lib/asan/asan_errors.cc b/compiler-rt/lib/asan/asan_errors.cc index bd007e5c563..abbb8e6ce56 100644 --- a/compiler-rt/lib/asan/asan_errors.cc +++ b/compiler-rt/lib/asan/asan_errors.cc @@ -216,6 +216,28 @@ void ErrorInvalidAllocationAlignment::Print() { 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()); diff --git a/compiler-rt/lib/asan/asan_errors.h b/compiler-rt/lib/asan/asan_errors.h index da721e2e175..512d2537bd0 100644 --- a/compiler-rt/lib/asan/asan_errors.h +++ b/compiler-rt/lib/asan/asan_errors.h @@ -190,6 +190,21 @@ struct ErrorInvalidAllocationAlignment : ErrorBase { 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) \ diff --git a/compiler-rt/lib/asan/asan_malloc_linux.cc b/compiler-rt/lib/asan/asan_malloc_linux.cc index 35b034e6c71..5efed5d4c59 100644 --- a/compiler-rt/lib/asan/asan_malloc_linux.cc +++ b/compiler-rt/lib/asan/asan_malloc_linux.cc @@ -158,7 +158,7 @@ INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) { #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 diff --git a/compiler-rt/lib/asan/asan_report.cc b/compiler-rt/lib/asan/asan_report.cc index 1801738fff0..18daace3990 100644 --- a/compiler-rt/lib/asan/asan_report.cc +++ b/compiler-rt/lib/asan/asan_report.cc @@ -278,6 +278,14 @@ void ReportInvalidAllocationAlignment(uptr alignment, 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); diff --git a/compiler-rt/lib/asan/asan_report.h b/compiler-rt/lib/asan/asan_report.h index e484ca90bfc..f7153d4810d 100644 --- a/compiler-rt/lib/asan/asan_report.h +++ b/compiler-rt/lib/asan/asan_report.h @@ -62,6 +62,8 @@ void ReportCallocOverflow(uptr count, uptr size, BufferedStackTrace *stack); 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, diff --git a/compiler-rt/lib/lsan/lsan_allocator.cc b/compiler-rt/lib/lsan/lsan_allocator.cc index 0aa6db63f32..f959a3de058 100644 --- a/compiler-rt/lib/lsan/lsan_allocator.cc +++ b/compiler-rt/lib/lsan/lsan_allocator.cc @@ -157,6 +157,16 @@ int lsan_posix_memalign(void **memptr, uptr alignment, uptr size, 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; diff --git a/compiler-rt/lib/lsan/lsan_allocator.h b/compiler-rt/lib/lsan/lsan_allocator.h index eebfc8b1a27..169fcc442c8 100644 --- a/compiler-rt/lib/lsan/lsan_allocator.h +++ b/compiler-rt/lib/lsan/lsan_allocator.h @@ -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); diff --git a/compiler-rt/lib/lsan/lsan_interceptors.cc b/compiler-rt/lib/lsan/lsan_interceptors.cc index b076d11de57..e60b1ab7ccd 100644 --- a/compiler-rt/lib/lsan/lsan_interceptors.cc +++ b/compiler-rt/lib/lsan/lsan_interceptors.cc @@ -122,7 +122,7 @@ INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) { 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 diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h index b61a8b2eb3b..61bd26e68cc 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h @@ -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. diff --git a/compiler-rt/test/asan/TestCases/Linux/aligned_alloc-alignment.cc b/compiler-rt/test/asan/TestCases/Linux/aligned_alloc-alignment.cc deleted file mode 100644 index 10ce95fcd45..00000000000 --- a/compiler-rt/test/asan/TestCases/Linux/aligned_alloc-alignment.cc +++ /dev/null @@ -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; -} diff --git a/compiler-rt/test/asan/TestCases/Posix/posix_memalign-alignment.cc b/compiler-rt/test/asan/TestCases/Posix/posix_memalign-alignment.cc deleted file mode 100644 index 1391292d23c..00000000000 --- a/compiler-rt/test/asan/TestCases/Posix/posix_memalign-alignment.cc +++ /dev/null @@ -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; -} diff --git a/compiler-rt/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc b/compiler-rt/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc deleted file mode 100644 index 370691d5810..00000000000 --- a/compiler-rt/test/lsan/TestCases/Linux/aligned_alloc-alignment.cc +++ /dev/null @@ -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; -} diff --git a/compiler-rt/test/lsan/TestCases/Posix/posix_memalign-alignment.cc b/compiler-rt/test/lsan/TestCases/Posix/posix_memalign-alignment.cc deleted file mode 100644 index 1b9b14ebe26..00000000000 --- a/compiler-rt/test/lsan/TestCases/Posix/posix_memalign-alignment.cc +++ /dev/null @@ -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; -} diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc b/compiler-rt/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc new file mode 100644 index 00000000000..67b66f1e1a8 --- /dev/null +++ b/compiler-rt/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 <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; +} diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc new file mode 100644 index 00000000000..457855d4442 --- /dev/null +++ b/compiler-rt/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 <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; +} |

