diff options
author | Serge Pavlov <sepavloff@gmail.com> | 2018-02-20 05:41:26 +0000 |
---|---|---|
committer | Serge Pavlov <sepavloff@gmail.com> | 2018-02-20 05:41:26 +0000 |
commit | 76d8ccee2e2aee1c7f7c6749ed8f555583ce3143 (patch) | |
tree | 5f0e9f8eb634b9d9f52af47590724d14ba8b27dd /llvm/lib | |
parent | db211892edde7dd6fafd108ecf770a2fecf8651d (diff) | |
download | bcm5719-llvm-76d8ccee2e2aee1c7f7c6749ed8f555583ce3143.tar.gz bcm5719-llvm-76d8ccee2e2aee1c7f7c6749ed8f555583ce3143.zip |
Report fatal error in the case of out of memory
This is the second part of recommit of r325224. The previous part was
committed in r325426, which deals with C++ memory allocation. Solution
for C memory allocation involved functions `llvm::malloc` and similar.
This was a fragile solution because it caused ambiguity errors in some
cases. In this commit the new functions have names like `llvm::safe_malloc`.
The relevant part of original comment is below, updated for new function
names.
Analysis of fails in the case of out of memory errors can be tricky on
Windows. Such error emerges at the point where memory allocation function
fails, but manifests itself when null pointer is used. These two points
may be distant from each other. Besides, next runs may not exhibit
allocation error.
In some cases memory is allocated by a call to some of C allocation
functions, malloc, calloc and realloc. They are used for interoperability
with C code, when allocated object has variable size and when it is
necessary to avoid call of constructors. In many calls the result is not
checked for null pointer. To simplify checks, new functions are defined
in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`.
They behave as corresponding standard functions but produce fatal error if
allocation fails. This change replaces the standard functions like 'malloc'
in the cases when the result of the allocation function is not checked
for null pointer.
Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statement is added.
Differential Revision: https://reviews.llvm.org/D43010
llvm-svn: 325551
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/CodeGen/InterferenceCache.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/CodeGen/LiveIntervalUnion.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/CodeGen/RegisterPressure.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/ExecutionEngine/Interpreter/Execution.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/Object/Object.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/Support/RWMutex.cpp | 3 | ||||
-rw-r--r-- | llvm/lib/Support/StringMap.cpp | 13 | ||||
-rw-r--r-- | llvm/lib/Support/Unix/Signals.inc | 2 | ||||
-rw-r--r-- | llvm/lib/Support/Windows/RWMutex.inc | 4 |
9 files changed, 16 insertions, 18 deletions
diff --git a/llvm/lib/CodeGen/InterferenceCache.cpp b/llvm/lib/CodeGen/InterferenceCache.cpp index 72227cc7bba..82f6e8d8e23 100644 --- a/llvm/lib/CodeGen/InterferenceCache.cpp +++ b/llvm/lib/CodeGen/InterferenceCache.cpp @@ -48,8 +48,8 @@ void InterferenceCache::reinitPhysRegEntries() { if (PhysRegEntriesCount == TRI->getNumRegs()) return; free(PhysRegEntries); PhysRegEntriesCount = TRI->getNumRegs(); - PhysRegEntries = (unsigned char*) - calloc(PhysRegEntriesCount, sizeof(unsigned char)); + PhysRegEntries = static_cast<unsigned char*>( + safe_calloc(PhysRegEntriesCount, sizeof(unsigned char))); } void InterferenceCache::init(MachineFunction *mf, diff --git a/llvm/lib/CodeGen/LiveIntervalUnion.cpp b/llvm/lib/CodeGen/LiveIntervalUnion.cpp index 3e742a6c2f2..36428e0335f 100644 --- a/llvm/lib/CodeGen/LiveIntervalUnion.cpp +++ b/llvm/lib/CodeGen/LiveIntervalUnion.cpp @@ -187,7 +187,7 @@ void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc, clear(); Size = NSize; LIUs = static_cast<LiveIntervalUnion*>( - malloc(sizeof(LiveIntervalUnion)*NSize)); + safe_malloc(sizeof(LiveIntervalUnion)*NSize)); for (unsigned i = 0; i != Size; ++i) new(LIUs + i) LiveIntervalUnion(Alloc); } diff --git a/llvm/lib/CodeGen/RegisterPressure.cpp b/llvm/lib/CodeGen/RegisterPressure.cpp index bc1af1594c2..97e5851e025 100644 --- a/llvm/lib/CodeGen/RegisterPressure.cpp +++ b/llvm/lib/CodeGen/RegisterPressure.cpp @@ -635,7 +635,7 @@ void PressureDiffs::init(unsigned N) { } Max = Size; free(PDiffArray); - PDiffArray = reinterpret_cast<PressureDiff*>(calloc(N, sizeof(PressureDiff))); + PDiffArray = static_cast<PressureDiff*>(safe_calloc(N, sizeof(PressureDiff))); } void PressureDiffs::addInstruction(unsigned Idx, diff --git a/llvm/lib/ExecutionEngine/Interpreter/Execution.cpp b/llvm/lib/ExecutionEngine/Interpreter/Execution.cpp index 96844439e72..358366c765f 100644 --- a/llvm/lib/ExecutionEngine/Interpreter/Execution.cpp +++ b/llvm/lib/ExecutionEngine/Interpreter/Execution.cpp @@ -974,7 +974,7 @@ void Interpreter::visitAllocaInst(AllocaInst &I) { unsigned MemToAlloc = std::max(1U, NumElements * TypeSize); // Allocate enough memory to hold the type... - void *Memory = malloc(MemToAlloc); + void *Memory = safe_malloc(MemToAlloc); DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x " << NumElements << " (Total: " << MemToAlloc << ") at " diff --git a/llvm/lib/Object/Object.cpp b/llvm/lib/Object/Object.cpp index 1d2859cfbe9..5fd823e0117 100644 --- a/llvm/lib/Object/Object.cpp +++ b/llvm/lib/Object/Object.cpp @@ -228,7 +228,7 @@ uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI) { const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) { SmallVector<char, 0> ret; (*unwrap(RI))->getTypeName(ret); - char *str = static_cast<char*>(malloc(ret.size())); + char *str = static_cast<char*>(safe_malloc(ret.size())); std::copy(ret.begin(), ret.end(), str); return str; } diff --git a/llvm/lib/Support/RWMutex.cpp b/llvm/lib/Support/RWMutex.cpp index 83c6d1d52b4..8182319541e 100644 --- a/llvm/lib/Support/RWMutex.cpp +++ b/llvm/lib/Support/RWMutex.cpp @@ -11,6 +11,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Support/Allocator.h" #include "llvm/Support/RWMutex.h" #include "llvm/Config/config.h" @@ -49,7 +50,7 @@ RWMutexImpl::RWMutexImpl() { // Declare the pthread_rwlock data structures pthread_rwlock_t* rwlock = - static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t))); + static_cast<pthread_rwlock_t*>(safe_malloc(sizeof(pthread_rwlock_t))); #ifdef __APPLE__ // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init. diff --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp index 4341da2d97b..9382c3ce29e 100644 --- a/llvm/lib/Support/StringMap.cpp +++ b/llvm/lib/Support/StringMap.cpp @@ -57,10 +57,9 @@ void StringMapImpl::init(unsigned InitSize) { NumItems = 0; NumTombstones = 0; - TheTable = (StringMapEntryBase **)calloc(NewNumBuckets+1, - sizeof(StringMapEntryBase **) + - sizeof(unsigned)); - + TheTable = static_cast<StringMapEntryBase **>( + std::calloc(NewNumBuckets+1, + sizeof(StringMapEntryBase **) + sizeof(unsigned))); if (TheTable == nullptr) report_bad_alloc_error("Allocation of StringMap table failed."); @@ -219,10 +218,8 @@ unsigned StringMapImpl::RehashTable(unsigned BucketNo) { unsigned NewBucketNo = BucketNo; // Allocate one extra bucket which will always be non-empty. This allows the // iterators to stop at end. - StringMapEntryBase **NewTableArray = - (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) + - sizeof(unsigned)); - + 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."); diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc index aaf760c5b61..a626b251ccd 100644 --- a/llvm/lib/Support/Unix/Signals.inc +++ b/llvm/lib/Support/Unix/Signals.inc @@ -138,7 +138,7 @@ static void CreateSigAltStack() { return; stack_t AltStack = {}; - AltStack.ss_sp = reinterpret_cast<char *>(malloc(AltStackSize)); + AltStack.ss_sp = static_cast<char *>(safe_malloc(AltStackSize)); NewAltStackPointer = AltStack.ss_sp; // Save to avoid reporting a leak. AltStack.ss_size = AltStackSize; if (sigaltstack(&AltStack, &OldAltStack) != 0) diff --git a/llvm/lib/Support/Windows/RWMutex.inc b/llvm/lib/Support/Windows/RWMutex.inc index ac60c2fc05b..5eb9351eee5 100644 --- a/llvm/lib/Support/Windows/RWMutex.inc +++ b/llvm/lib/Support/Windows/RWMutex.inc @@ -74,10 +74,10 @@ static bool loadSRW() { sys::RWMutexImpl::RWMutexImpl() { if (loadSRW()) { - data_ = calloc(1, sizeof(SRWLOCK)); + data_ = safe_calloc(1, sizeof(SRWLOCK)); fpInitializeSRWLock(static_cast<PSRWLOCK>(data_)); } else { - data_ = calloc(1, sizeof(CRITICAL_SECTION)); + data_ = safe_calloc(1, sizeof(CRITICAL_SECTION)); InitializeCriticalSection(static_cast<LPCRITICAL_SECTION>(data_)); } } |