diff options
author | Alex Shlyapnikov <alekseys@google.com> | 2018-01-17 23:20:36 +0000 |
---|---|---|
committer | Alex Shlyapnikov <alekseys@google.com> | 2018-01-17 23:20:36 +0000 |
commit | 6ae9c03ffaf72765033e3bec140b52741b0177aa (patch) | |
tree | 75e9316b11ef9017050e32747421c3b3674a1995 /lib/msan | |
parent | 47982b47999810f261eaff6b599e661be092ae10 (diff) |
[Sanitizers] Make common allocator agnostic to failure handling modes.
Summary:
Make common allocator agnostic to failure handling modes and move the
decision up to the particular sanitizer's allocator, where the context
is available (call stack, parameters, return nullptr/crash mode etc.)
It simplifies the common allocator and allows the particular sanitizer's
allocator to generate more specific and detailed error reports (which
will be implemented later).
The behavior is largely the same, except one case, the violation of the
common allocator's check for "size + alignment" overflow is now reportied
as OOM instead of "bad request". It feels like a worthy tradeoff and
"size + alignment" is huge in this case anyway (thus, can be interpreted
as not enough memory to satisfy the request). There's also a Report()
statement added there.
Reviewers: eugenis
Subscribers: kubamracek, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D42198
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@322784 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/msan')
-rw-r--r-- | lib/msan/msan_allocator.cc | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/msan/msan_allocator.cc b/lib/msan/msan_allocator.cc index 0f9942324..8a23edf2c 100644 --- a/lib/msan/msan_allocator.cc +++ b/lib/msan/msan_allocator.cc @@ -141,7 +141,7 @@ static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment, if (size > kMaxAllowedMallocSize) { Report("WARNING: MemorySanitizer failed to allocate %p bytes\n", (void *)size); - return Allocator::FailureHandler::OnBadRequest(); + return ReturnNullOrDieOnFailure::OnBadRequest(); } MsanThread *t = GetCurrentThread(); void *allocated; @@ -153,6 +153,8 @@ static void *MsanAllocate(StackTrace *stack, uptr size, uptr alignment, AllocatorCache *cache = &fallback_allocator_cache; allocated = allocator.Allocate(cache, size, alignment); } + if (UNLIKELY(!allocated)) + return ReturnNullOrDieOnFailure::OnOOM(); Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated)); meta->requested_size = size; @@ -236,7 +238,7 @@ void *msan_malloc(uptr size, StackTrace *stack) { void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack) { if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) - return SetErrnoOnNull(Allocator::FailureHandler::OnBadRequest()); + return SetErrnoOnNull(ReturnNullOrDieOnFailure::OnBadRequest()); return SetErrnoOnNull(MsanAllocate(stack, nmemb * size, sizeof(u64), true)); } @@ -258,7 +260,7 @@ void *msan_pvalloc(uptr size, StackTrace *stack) { uptr PageSize = GetPageSizeCached(); if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) { errno = errno_ENOMEM; - return Allocator::FailureHandler::OnBadRequest(); + return ReturnNullOrDieOnFailure::OnBadRequest(); } // pvalloc(0) should allocate one page. size = size ? RoundUpTo(size, PageSize) : PageSize; @@ -268,7 +270,7 @@ void *msan_pvalloc(uptr size, StackTrace *stack) { void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack) { if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) { errno = errno_EINVAL; - return Allocator::FailureHandler::OnBadRequest(); + return ReturnNullOrDieOnFailure::OnBadRequest(); } return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false)); } @@ -276,7 +278,7 @@ void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack) { void *msan_memalign(uptr alignment, uptr size, StackTrace *stack) { if (UNLIKELY(!IsPowerOfTwo(alignment))) { errno = errno_EINVAL; - return Allocator::FailureHandler::OnBadRequest(); + return ReturnNullOrDieOnFailure::OnBadRequest(); } return SetErrnoOnNull(MsanAllocate(stack, size, alignment, false)); } @@ -284,7 +286,7 @@ void *msan_memalign(uptr alignment, uptr size, StackTrace *stack) { int msan_posix_memalign(void **memptr, uptr alignment, uptr size, StackTrace *stack) { if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) { - Allocator::FailureHandler::OnBadRequest(); + ReturnNullOrDieOnFailure::OnBadRequest(); return errno_EINVAL; } void *ptr = MsanAllocate(stack, size, alignment, false); |