llvm.org GIT mirror llvm / 06c71d8
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325551 91177308-0d34-0410-b5e6-96231b3b80d8 Serge Pavlov 2 years ago
18 changed file(s) with 64 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
827827 }
828828
829829 MutableArrayRef allocate(size_t NumWords) {
830 BitWord *RawBits = (BitWord *)std::malloc(NumWords * sizeof(BitWord));
830 BitWord *RawBits = static_cast(
831 safe_malloc(NumWords * sizeof(BitWord)));
831832 return MutableArrayRef(RawBits, NumWords);
832833 }
833834
866867 void grow(unsigned NewSize) {
867868 size_t NewCapacity = std::max(NumBitWords(NewSize), Bits.size() * 2);
868869 assert(NewCapacity > 0 && "realloc-ing zero space");
869 BitWord *NewBits =
870 (BitWord *)std::realloc(Bits.data(), NewCapacity * sizeof(BitWord));
870 BitWord *NewBits = static_cast(
871 safe_realloc(Bits.data(), NewCapacity * sizeof(BitWord)));
871872 Bits = MutableArrayRef(NewBits, NewCapacity);
872873 clear_unused_bits();
873874 }
210210 // The Sparse array doesn't actually need to be initialized, so malloc
211211 // would be enough here, but that will cause tools like valgrind to
212212 // complain about branching on uninitialized data.
213 Sparse = reinterpret_cast(calloc(U, sizeof(SparseT)));
213 Sparse = static_cast(safe_calloc(U, sizeof(SparseT)));
214214 Universe = U;
215215 }
216216
2121
2222 #include "llvm/ADT/STLExtras.h"
2323 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/Allocator.h"
2425 #include
2526 #include
2627 #include
162163 // The Sparse array doesn't actually need to be initialized, so malloc
163164 // would be enough here, but that will cause tools like valgrind to
164165 // complain about branching on uninitialized data.
165 Sparse = reinterpret_cast(calloc(U, sizeof(SparseT)));
166 Sparse = static_cast(safe_calloc(U, sizeof(SparseT)));
166167 Universe = U;
167168 }
168169
438438 T *Allocate(size_t num = 1) { return Allocator.Allocate(num); }
439439 };
440440
441 /// \{
442 /// Counterparts of allocation functions defined in namespace 'std', which crash
443 /// on allocation failure instead of returning null pointer.
444
445 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
446 void *Result = std::malloc(Sz);
447 if (Result == nullptr)
448 report_bad_alloc_error("Allocation failed.");
449 return Result;
450 }
451
452 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
453 size_t Sz) {
454 void *Result = std::calloc(Count, Sz);
455 if (Result == nullptr)
456 report_bad_alloc_error("Allocation failed.");
457 return Result;
458 }
459
460 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
461 void *Result = std::realloc(Ptr, Sz);
462 if (Result == nullptr)
463 report_bad_alloc_error("Allocation failed.");
464 return Result;
465 }
466
467 /// \}
468
441469 } // end namespace llvm
442470
443471 template
9494
9595 /// \brief Resize the hash table, moving the old entries into the new buckets.
9696 void resize(size_t NewSize) {
97 Bucket *NewBuckets = (Bucket *)std::calloc(NewSize, sizeof(Bucket));
97 Bucket *NewBuckets = static_cast(
98 safe_calloc(NewSize, sizeof(Bucket)));
9899 // Populate NewBuckets with the old entries.
99100 for (size_t I = 0; I < NumBuckets; ++I)
100101 for (Item *E = Buckets[I].Head; E;) {
225226 NumBuckets = 64;
226227 // Note that we do not need to run the constructors of the individual
227228 // Bucket objects since 'calloc' returns bytes that are all 0.
228 Buckets = (Bucket *)std::calloc(NumBuckets, sizeof(Bucket));
229 Buckets = static_cast(safe_calloc(NumBuckets, sizeof(Bucket)));
229230 }
230231
231232 ~OnDiskChainedHashTableGenerator() { std::free(Buckets); }
4747 if (PhysRegEntriesCount == TRI->getNumRegs()) return;
4848 free(PhysRegEntries);
4949 PhysRegEntriesCount = TRI->getNumRegs();
50 PhysRegEntries = (unsigned char*)
51 calloc(PhysRegEntriesCount, sizeof(unsigned char));
50 PhysRegEntries = static_cast(
51 safe_calloc(PhysRegEntriesCount, sizeof(unsigned char)));
5252 }
5353
5454 void InterferenceCache::init(MachineFunction *mf,
186186 clear();
187187 Size = NSize;
188188 LIUs = static_cast(
189 malloc(sizeof(LiveIntervalUnion)*NSize));
189 safe_malloc(sizeof(LiveIntervalUnion)*NSize));
190190 for (unsigned i = 0; i != Size; ++i)
191191 new(LIUs + i) LiveIntervalUnion(Alloc);
192192 }
634634 }
635635 Max = Size;
636636 free(PDiffArray);
637 PDiffArray = reinterpret_cast(calloc(N, sizeof(PressureDiff)));
637 PDiffArray = static_cast(safe_calloc(N, sizeof(PressureDiff)));
638638 }
639639
640640 void PressureDiffs::addInstruction(unsigned Idx,
973973 unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
974974
975975 // Allocate enough memory to hold the type...
976 void *Memory = malloc(MemToAlloc);
976 void *Memory = safe_malloc(MemToAlloc);
977977
978978 DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
979979 << NumElements << " (Total: " << MemToAlloc << ") at "
227227 const char *LLVMGetRelocationTypeName(LLVMRelocationIteratorRef RI) {
228228 SmallVector ret;
229229 (*unwrap(RI))->getTypeName(ret);
230 char *str = static_cast(malloc(ret.size()));
230 char *str = static_cast(safe_malloc(ret.size()));
231231 std::copy(ret.begin(), ret.end(), str);
232232 return str;
233233 }
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/Support/Allocator.h"
1314 #include "llvm/Support/RWMutex.h"
1415 #include "llvm/Config/config.h"
1516
4849 {
4950 // Declare the pthread_rwlock data structures
5051 pthread_rwlock_t* rwlock =
51 static_cast(malloc(sizeof(pthread_rwlock_t)));
52 static_cast(safe_malloc(sizeof(pthread_rwlock_t)));
5253
5354 #ifdef __APPLE__
5455 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
5656 NumItems = 0;
5757 NumTombstones = 0;
5858
59 TheTable = (StringMapEntryBase **)calloc(NewNumBuckets+1,
60 sizeof(StringMapEntryBase **) +
61 sizeof(unsigned));
62
59 TheTable = static_cast(
60 std::calloc(NewNumBuckets+1,
61 sizeof(StringMapEntryBase **) + sizeof(unsigned)));
6362 if (TheTable == nullptr)
6463 report_bad_alloc_error("Allocation of StringMap table failed.");
6564
218217 unsigned NewBucketNo = BucketNo;
219218 // Allocate one extra bucket which will always be non-empty. This allows the
220219 // iterators to stop at end.
221 StringMapEntryBase **NewTableArray =
222 (StringMapEntryBase **)calloc(NewSize+1, sizeof(StringMapEntryBase *) +
223 sizeof(unsigned));
224
220 auto NewTableArray = static_cast(
221 std::calloc(NewSize+1, sizeof(StringMapEntryBase *) + sizeof(unsigned)));
225222 if (NewTableArray == nullptr)
226223 report_bad_alloc_error("Allocation of StringMap hash table failed.");
227224
137137 return;
138138
139139 stack_t AltStack = {};
140 AltStack.ss_sp = reinterpret_cast(malloc(AltStackSize));
140 AltStack.ss_sp = static_cast(safe_malloc(AltStackSize));
141141 NewAltStackPointer = AltStack.ss_sp; // Save to avoid reporting a leak.
142142 AltStack.ss_size = AltStackSize;
143143 if (sigaltstack(&AltStack, &OldAltStack) != 0)
7373
7474 sys::RWMutexImpl::RWMutexImpl() {
7575 if (loadSRW()) {
76 data_ = calloc(1, sizeof(SRWLOCK));
76 data_ = safe_calloc(1, sizeof(SRWLOCK));
7777 fpInitializeSRWLock(static_cast(data_));
7878 } else {
79 data_ = calloc(1, sizeof(CRITICAL_SECTION));
79 data_ = safe_calloc(1, sizeof(CRITICAL_SECTION));
8080 InitializeCriticalSection(static_cast(data_));
8181 }
8282 }
1313
1414 #include "llvm-c-test.h"
1515
16 #include
1617 #include
1718
1819 int llvm_test_function_attributes(void) {
2930 int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx);
3031 LLVMAttributeRef *Attrs =
3132 (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef));
33 assert(Attrs);
3234 LLVMGetAttributesAtIndex(F, Idx, Attrs);
3335 free(Attrs);
3436 }
6062 int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx);
6163 LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(
6264 AttrCount * sizeof(LLVMAttributeRef));
65 assert(Attrs);
6366 LLVMGetCallSiteAttributes(I, Idx, Attrs);
6467 free(Attrs);
6568 }
8989 unsigned ParamCount = LLVMCountParamTypes(Src);
9090 LLVMTypeRef* Params = nullptr;
9191 if (ParamCount > 0) {
92 Params = (LLVMTypeRef*) malloc(ParamCount * sizeof(LLVMTypeRef));
92 Params = static_cast(
93 safe_malloc(ParamCount * sizeof(LLVMTypeRef)));
9394 LLVMGetParamTypes(Src, Params);
9495 for (unsigned i = 0; i < ParamCount; i++)
9596 Params[i] = Clone(Params[i]);
146146 // Allocate space for the alignment, the slab, and a void* that goes right
147147 // before the slab.
148148 size_t Alignment = 4096;
149 void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*));
149 void *MemBase = safe_malloc(Size + Alignment - 1 + sizeof(void*));
150150
151151 // Find the slab start.
152152 void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
55 // License. See LICENSE.TXT for details.
66 //
77 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/Allocator.h"
810 #include "llvm/Support/ManagedStatic.h"
911 #include "llvm/Config/config.h"
1012 #ifdef HAVE_PTHREAD_H
2931 // Valgrind's leak checker complains glibc's stack allocation.
3032 // To appease valgrind, we provide our own stack for each thread.
3133 void *allocate_stack(pthread_attr_t &a, size_t n = 65536) {
32 void *stack = malloc(n);
34 void *stack = safe_malloc(n);
3335 pthread_attr_init(&a);
3436 #if defined(__linux__)
3537 pthread_attr_setstack(&a, stack, n);
8284 namespace CustomCreatorDeletor {
8385 struct CustomCreate {
8486 static void *call() {
85 void *Mem = std::malloc(sizeof(int));
87 void *Mem = safe_malloc(sizeof(int));
8688 *((int *)Mem) = 42;
8789 return Mem;
8890 }