Skip to content

Commit

Permalink
Use uniform mechanism for OOM errors handling
Browse files Browse the repository at this point in the history
In r325551 many calls of malloc/calloc/realloc were replaces with calls of
their safe counterparts defined in the namespace llvm. There functions
generate crash if memory cannot be allocated, such behavior facilitates
handling of out of memory errors on Windows.

If the result of *alloc function were checked for success, the function was
not replaced with the safe variant. In these cases the calling function made
the error handling, like:

    T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
    if (NewElts == nullptr)
      report_bad_alloc_error("Allocation of SmallVector element failed.");

Actually knowledge about the function where OOM occurred is useless. Moreover
having a single entry point for OOM handling is convenient for investigation
of memory problems. This change removes custom OOM errors handling and
replaces them with calls to functions `llvm::safe_*alloc`.

Declarations of `safe_*alloc` are moved to a separate include file, to avoid
cyclic dependency in SmallVector.h

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


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333390 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
spavloff committed May 29, 2018
1 parent 7cdb0e2 commit 5c9fd60
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 85 deletions.
5 changes: 2 additions & 3 deletions include/llvm/ADT/SmallVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemAlloc.h"
#include "llvm/Support/type_traits.h"
#include "llvm/Support/ErrorHandling.h"
#include <algorithm>
Expand Down Expand Up @@ -238,9 +239,7 @@ void SmallVectorTemplateBase<T, isPodLike>::grow(size_t MinSize) {
size_t NewCapacity = size_t(NextPowerOf2(CurCapacity+2));
if (NewCapacity < MinSize)
NewCapacity = MinSize;
T *NewElts = static_cast<T*>(malloc(NewCapacity*sizeof(T)));
if (NewElts == nullptr)
report_bad_alloc_error("Allocation of SmallVector element failed.");
T *NewElts = static_cast<T*>(llvm::safe_malloc(NewCapacity*sizeof(T)));

// Move the elements over.
this->uninitialized_move(this->begin(), this->end(), NewElts);
Expand Down
4 changes: 1 addition & 3 deletions include/llvm/ADT/StringMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ class StringMapEntry : public StringMapEntryBase {

StringMapEntry *NewItem =
static_cast<StringMapEntry*>(Allocator.Allocate(AllocSize,Alignment));

if (NewItem == nullptr)
report_bad_alloc_error("Allocation of StringMap entry failed.");
assert(NewItem && "Unhandled out-of-memory");

// Construct the value.
new (NewItem) StringMapEntry(KeyLength, std::forward<InitTy>(InitVals)...);
Expand Down
37 changes: 3 additions & 34 deletions include/llvm/Support/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemAlloc.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
Expand Down Expand Up @@ -95,11 +96,7 @@ class MallocAllocator : public AllocatorBase<MallocAllocator> {

LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size,
size_t /*Alignment*/) {
void* memPtr = malloc(Size);
if (memPtr == nullptr)
report_bad_alloc_error("Allocation in MallocAllocator failed.");

return memPtr;
return safe_malloc(Size);
}

// Pull in base class overloads.
Expand Down Expand Up @@ -439,34 +436,6 @@ template <typename T> class SpecificBumpPtrAllocator {
T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }
};

/// \{
/// Counterparts of allocation functions defined in namespace 'std', which crash
/// on allocation failure instead of returning null pointer.

LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
void *Result = std::malloc(Sz);
if (Result == nullptr)
report_bad_alloc_error("Allocation failed.");
return Result;
}

LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
size_t Sz) {
void *Result = std::calloc(Count, Sz);
if (Result == nullptr)
report_bad_alloc_error("Allocation failed.");
return Result;
}

LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
void *Result = std::realloc(Ptr, Sz);
if (Result == nullptr)
report_bad_alloc_error("Allocation failed.");
return Result;
}

/// \}

} // end namespace llvm

template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
Expand Down
48 changes: 48 additions & 0 deletions include/llvm/Support/MemAlloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file defines counterparts of C library allocation functions defined in
/// the namespace 'std'. The new allocation functions crash on allocation
/// failure instead of returning null pointer.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_MEMALLOC_H
#define LLVM_SUPPORT_MEMALLOC_H

#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"

namespace llvm {

LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
void *Result = std::malloc(Sz);
if (Result == nullptr)
report_bad_alloc_error("Allocation failed");
return Result;
}

LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
size_t Sz) {
void *Result = std::calloc(Count, Sz);
if (Result == nullptr)
report_bad_alloc_error("Allocation failed");
return Result;
}

LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
void *Result = std::realloc(Ptr, Sz);
if (Result == nullptr)
report_bad_alloc_error("Allocation failed");
return Result;
}

}
#endif
10 changes: 5 additions & 5 deletions lib/Demangle/ItaniumDemangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// - C++ modules TS

#include "llvm/Demangle/Demangle.h"

#include "llvm/Demangle/Compiler.h"
#include "llvm/Support/MemAlloc.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -89,7 +89,7 @@ class OutputStream {
BufferCapacity *= 2;
if (BufferCapacity < N + CurrentPosition)
BufferCapacity = N + CurrentPosition;
Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity));
Buffer = static_cast<char *>(llvm::safe_realloc(Buffer, BufferCapacity));
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ class Node {

#ifndef NDEBUG
LLVM_DUMP_METHOD void dump() const {
char *Buffer = static_cast<char*>(std::malloc(1024));
char *Buffer = static_cast<char*>(llvm::safe_malloc(1024));
OutputStream S(Buffer, 1024);
print(S);
S += '\0';
Expand Down Expand Up @@ -1947,11 +1947,11 @@ class PODSmallVector {
void reserve(size_t NewCap) {
size_t S = size();
if (isInline()) {
auto* Tmp = static_cast<T*>(std::malloc(NewCap * sizeof(T)));
auto* Tmp = static_cast<T*>(llvm::safe_malloc(NewCap * sizeof(T)));
std::copy(First, Last, Tmp);
First = Tmp;
} else
First = static_cast<T*>(std::realloc(First, NewCap * sizeof(T)));
First = static_cast<T*>(llvm::safe_realloc(First, NewCap * sizeof(T)));
Last = First + S;
Cap = First + NewCap;
}
Expand Down
6 changes: 2 additions & 4 deletions lib/IR/DataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,8 @@ const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
// Otherwise, create the struct layout. Because it is variable length, we
// malloc it, then use placement new.
int NumElts = Ty->getNumElements();
StructLayout *L =
(StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
if (L == nullptr)
report_bad_alloc_error("Allocation of StructLayout elements failed.");
StructLayout *L = (StructLayout *)
safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));

// Set SL before calling StructLayout's ctor. The ctor could cause other
// entries to be added to TheMap, invalidating our reference.
Expand Down
7 changes: 2 additions & 5 deletions lib/Support/FoldingSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,8 @@ static void **GetBucketFor(unsigned Hash, void **Buckets, unsigned NumBuckets) {

/// AllocateBuckets - Allocated initialized bucket memory.
static void **AllocateBuckets(unsigned NumBuckets) {
void **Buckets = static_cast<void**>(calloc(NumBuckets+1, sizeof(void*)));

if (Buckets == nullptr)
report_bad_alloc_error("Allocation of Buckets failed.");

void **Buckets = static_cast<void**>(safe_calloc(NumBuckets + 1,
sizeof(void*)));
// Set the very last bucket to be a non-null "pointer".
Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
return Buckets;
Expand Down
5 changes: 1 addition & 4 deletions lib/Support/Mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ MutexImpl::MutexImpl( bool recursive)
{
// Declare the pthread_mutex data structures
pthread_mutex_t* mutex =
static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));

if (mutex == nullptr)
report_bad_alloc_error("Mutex allocation failed");
static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));

pthread_mutexattr_t attr;

Expand Down
20 changes: 5 additions & 15 deletions lib/Support/SmallPtrSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ void SmallPtrSetImplBase::shrink_and_clear() {
NumNonEmpty = NumTombstones = 0;

// Install the new array. Clear all the buckets to empty.
CurArray = (const void**)malloc(sizeof(void*) * CurArraySize);
if (CurArray == nullptr)
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
CurArray = (const void**)safe_malloc(sizeof(void*) * CurArraySize);

memset(CurArray, -1, CurArraySize*sizeof(void*));
}
Expand Down Expand Up @@ -100,9 +98,7 @@ void SmallPtrSetImplBase::Grow(unsigned NewSize) {
bool WasSmall = isSmall();

// Install the new array. Clear all the buckets to empty.
const void **NewBuckets = (const void**) malloc(sizeof(void*) * NewSize);
if (NewBuckets == nullptr)
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
const void **NewBuckets = (const void**) safe_malloc(sizeof(void*) * NewSize);

// Reset member only if memory was allocated successfully
CurArray = NewBuckets;
Expand Down Expand Up @@ -132,9 +128,7 @@ SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage,
CurArray = SmallArray;
// Otherwise, allocate new heap space (unless we were the same size)
} else {
CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize);
if (CurArray == nullptr)
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
CurArray = (const void**)safe_malloc(sizeof(void*) * that.CurArraySize);
}

// Copy over the that array.
Expand Down Expand Up @@ -163,16 +157,12 @@ void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) {
// Otherwise, allocate new heap space (unless we were the same size)
} else if (CurArraySize != RHS.CurArraySize) {
if (isSmall())
CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize);
CurArray = (const void**)safe_malloc(sizeof(void*) * RHS.CurArraySize);
else {
const void **T = (const void**)realloc(CurArray,
const void **T = (const void**)safe_realloc(CurArray,
sizeof(void*) * RHS.CurArraySize);
if (!T)
free(CurArray);
CurArray = T;
}
if (CurArray == nullptr)
report_bad_alloc_error("Allocation of SmallPtrSet bucket array failed.");
}

CopyHelper(RHS);
Expand Down
8 changes: 2 additions & 6 deletions lib/Support/SmallVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes,

void *NewElts;
if (BeginX == FirstEl) {
NewElts = malloc(NewCapacityInBytes);
if (NewElts == nullptr)
report_bad_alloc_error("Allocation of SmallVector element failed.");
NewElts = safe_malloc(NewCapacityInBytes);

// Copy the elements over. No need to run dtors on PODs.
memcpy(NewElts, this->BeginX, CurSizeBytes);
} else {
// If this wasn't grown from the inline copy, grow the allocated space.
NewElts = realloc(this->BeginX, NewCapacityInBytes);
if (NewElts == nullptr)
report_bad_alloc_error("Reallocation of SmallVector element failed.");
NewElts = safe_realloc(this->BeginX, NewCapacityInBytes);
}

this->EndX = (char*)NewElts+CurSizeBytes;
Expand Down
8 changes: 2 additions & 6 deletions lib/Support/StringMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ void StringMapImpl::init(unsigned InitSize) {
NumTombstones = 0;

TheTable = static_cast<StringMapEntryBase **>(
std::calloc(NewNumBuckets+1,
safe_calloc(NewNumBuckets+1,
sizeof(StringMapEntryBase **) + sizeof(unsigned)));
if (TheTable == nullptr)
report_bad_alloc_error("Allocation of StringMap table failed.");

// Set the member only if TheTable was successfully allocated
NumBuckets = NewNumBuckets;
Expand Down Expand Up @@ -220,9 +218,7 @@ unsigned StringMapImpl::RehashTable(unsigned BucketNo) {
// Allocate one extra bucket which will always be non-empty. This allows the
// iterators to stop at end.
auto NewTableArray = static_cast<StringMapEntryBase **>(
std::calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
if (NewTableArray == nullptr)
report_bad_alloc_error("Allocation of StringMap hash table failed.");
safe_calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));

unsigned *NewHashArray = (unsigned *)(NewTableArray + NewSize + 1);
NewTableArray[NewSize] = (StringMapEntryBase*)2;
Expand Down

0 comments on commit 5c9fd60

Please sign in to comment.