[PATCH] D43010: Report fatal error in the case of out of memory

Serge Pavlov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 02:28:48 PST 2018


sepavloff created this revision.
sepavloff added a reviewer: rnk.
Herald added subscribers: javed.absar, MatzeB.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D43010

Files:
  include/llvm/ADT/BitVector.h
  include/llvm/ADT/SparseMultiSet.h
  include/llvm/ADT/SparseSet.h
  include/llvm/Support/Allocator.h
  include/llvm/Support/ErrorHandling.h
  include/llvm/Support/OnDiskHashTable.h
  lib/CodeGen/InterferenceCache.cpp
  lib/CodeGen/LiveIntervalUnion.cpp
  lib/CodeGen/RegisterPressure.cpp
  lib/ExecutionEngine/Interpreter/Execution.cpp
  lib/Object/Object.cpp
  lib/Support/ErrorHandling.cpp
  lib/Support/FoldingSet.cpp
  lib/Support/RWMutex.cpp
  lib/Support/StringMap.cpp
  lib/Support/Unix/Signals.inc
  lib/Support/Windows/RWMutex.inc
  tools/llvm-c-test/attributes.c
  tools/llvm-c-test/echo.cpp
  unittests/Support/AllocatorTest.cpp
  unittests/Support/ManagedStatic.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43010.133179.patch
Type: text/x-patch
Size: 16278 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180207/286dc51e/attachment.bin>


More information about the llvm-commits mailing list