diff options
| author | Serge Pavlov <sepavloff@gmail.com> | 2018-02-15 09:20:26 +0000 |
|---|---|---|
| committer | Serge Pavlov <sepavloff@gmail.com> | 2018-02-15 09:20:26 +0000 |
| commit | 431502a675edc407791fecde0cf96b0d658b789a (patch) | |
| tree | 48492d407fa621e3d386e3473bc62778fe4bf6a7 /llvm/unittests | |
| parent | a06d0802049d01f2ba1197a492fcc6836cdd1a60 (diff) | |
| download | bcm5719-llvm-431502a675edc407791fecde0cf96b0d658b789a.tar.gz bcm5719-llvm-431502a675edc407791fecde0cf96b0d658b789a.zip | |
Report fatal error in the case of out of memory
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.
Usual programming practice does not require checking result of 'operator
new' because it throws 'std::bad_alloc' in the case of allocation error.
However, LLVM is usually built with exceptions turned off, so 'new' can
return null pointer. This change installs custom new handler, which causes
fatal error in the case of out of memory. The handler is installed
automatically prior to call to 'main' during construction of a static
object defined in 'lib/Support/ErrorHandling.cpp'. If the application does
not use this file, the handler may be installed manually by a call to
'llvm::install_out_of_memory_new_handler', declared in
'include/llvm/Support/ErrorHandling.h".
There are calls to 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 against null pointer. To simplify
checks, new functions are defined in the namespace 'llvm' with the
same names as these C function. These functions produce fatal error if
allocation fails. User should use 'llvm::malloc' instead of 'std::malloc'
in order to use the safe variant. This change replaces 'std::malloc'
in the cases when the result of allocation function is not checked against
null pointer.
Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statements are added.
Differential Revision: https://reviews.llvm.org/D43010
llvm-svn: 325224
Diffstat (limited to 'llvm/unittests')
| -rw-r--r-- | llvm/unittests/Support/AllocatorTest.cpp | 2 | ||||
| -rw-r--r-- | llvm/unittests/Support/ManagedStatic.cpp | 6 |
2 files changed, 5 insertions, 3 deletions
diff --git a/llvm/unittests/Support/AllocatorTest.cpp b/llvm/unittests/Support/AllocatorTest.cpp index 4897c47eb28..b26c8a6ec3e 100644 --- a/llvm/unittests/Support/AllocatorTest.cpp +++ b/llvm/unittests/Support/AllocatorTest.cpp @@ -147,7 +147,7 @@ public: // Allocate space for the alignment, the slab, and a void* that goes right // before the slab. size_t Alignment = 4096; - void *MemBase = malloc(Size + Alignment - 1 + sizeof(void*)); + void *MemBase = llvm::malloc(Size + Alignment - 1 + sizeof(void*)); // Find the slab start. void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment); diff --git a/llvm/unittests/Support/ManagedStatic.cpp b/llvm/unittests/Support/ManagedStatic.cpp index 07e324cdfb6..700b765f367 100644 --- a/llvm/unittests/Support/ManagedStatic.cpp +++ b/llvm/unittests/Support/ManagedStatic.cpp @@ -6,6 +6,8 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// + +#include "llvm/Support/Allocator.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Config/config.h" #ifdef HAVE_PTHREAD_H @@ -30,7 +32,7 @@ namespace test1 { // Valgrind's leak checker complains glibc's stack allocation. // To appease valgrind, we provide our own stack for each thread. void *allocate_stack(pthread_attr_t &a, size_t n = 65536) { - void *stack = malloc(n); + void *stack = llvm::malloc(n); pthread_attr_init(&a); #if defined(__linux__) pthread_attr_setstack(&a, stack, n); @@ -83,7 +85,7 @@ TEST(ManagedStaticTest, NestedStatics) { namespace CustomCreatorDeletor { struct CustomCreate { static void *call() { - void *Mem = std::malloc(sizeof(int)); + void *Mem = llvm::malloc(sizeof(int)); *((int *)Mem) = 42; return Mem; } |

