1919#include " llvm/Support/Debug.h"
2020#include " llvm/Support/Errc.h"
2121#include " llvm/Support/Error.h"
22+ #include " llvm/Support/Memory.h"
2223#include " llvm/Support/Process.h"
2324#include " llvm/Support/Signals.h"
2425#include " llvm/Support/Threading.h"
3940
4041using namespace llvm ;
4142
43+ class MemoryReserver {
44+ private:
45+ std::error_code EC;
46+ sys::MemoryBlock Ptr;
47+
48+ public:
49+ MemoryReserver (size_t Pages): Ptr() {
50+ using sys::Memory;
51+ unsigned int PSize = sys::Process::getPageSizeEstimate ();
52+ Ptr = Memory::allocateMappedMemory (PSize * Pages, nullptr , Memory::MF_READ | Memory::MF_WRITE, EC);
53+ assert (!EC);
54+ }
55+
56+ ~MemoryReserver () {
57+ release ();
58+ }
59+
60+ void release () {
61+ EC = sys::Memory::releaseMappedMemory (Ptr);
62+ assert (!EC);
63+ }
64+ };
65+
4266static fatal_error_handler_t ErrorHandler = nullptr ;
4367static void *ErrorHandlerUserData = nullptr ;
4468
4569static fatal_error_handler_t BadAllocErrorHandler = nullptr ;
4670static void *BadAllocErrorHandlerUserData = nullptr ;
4771
72+ static MemoryReserver OOMReservedMemory (1024 );
73+
4874#if LLVM_ENABLE_THREADS == 1
4975// Mutexes to synchronize installing error handlers and calling error handlers.
5076// Do not use ManagedStatic, or that may allocate memory while attempting to
@@ -152,6 +178,7 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
152178#if LLVM_ENABLE_THREADS == 1
153179 std::lock_guard<std::mutex> Lock (BadAllocErrorHandlerMutex);
154180#endif
181+ OOMReservedMemory.release ();
155182 Handler = BadAllocErrorHandler;
156183 HandlerData = BadAllocErrorHandlerUserData;
157184 }
@@ -172,7 +199,8 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
172199 (void )!::write (2 , OOMMessage, strlen (OOMMessage));
173200 (void )!::write (2 , Reason, strlen (Reason));
174201 (void )!::write (2 , Newline, strlen (Newline));
175- abort ();
202+ sys::PrintStackTrace (llvm::errs ());
203+ ::_Exit (235 );
176204#endif
177205}
178206
0 commit comments