[PATCH] D37447: [Decompression] Fail gracefully when out of memory

Jonas Devlieghere via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 5 01:03:23 PDT 2017


JDevlieghere updated this revision to Diff 113805.
JDevlieghere added a comment.

Use `report_fatal_error`


Repository:
  rL LLVM

https://reviews.llvm.org/D37447

Files:
  include/llvm/Object/Decompressor.h
  lib/Object/Decompressor.cpp
  test/tools/llvm-dwarfdump/Inputs/dwarf-invalid-compression
  test/tools/llvm-dwarfdump/dwarf-invalid-compression.test


Index: test/tools/llvm-dwarfdump/dwarf-invalid-compression.test
===================================================================
--- /dev/null
+++ test/tools/llvm-dwarfdump/dwarf-invalid-compression.test
@@ -0,0 +1,2 @@
+RUN: not llvm-dwarfdump %p/Inputs/dwarf-invalid-compression 2>&1 | FileCheck %s
+CHECK: Decompression failed: unable to allocate 2314885530818453536 bytes.
Index: lib/Object/Decompressor.cpp
===================================================================
--- lib/Object/Decompressor.cpp
+++ lib/Object/Decompressor.cpp
@@ -92,3 +92,9 @@
   size_t Size = Buffer.size();
   return zlib::uncompress(SectionData, Buffer.data(), Size);
 }
+
+void Decompressor::outOfMemoryHandler(void *SizePtr, const std::string &Message,
+                                      bool) {
+  report_fatal_error("Decompression failed: unable to allocate " +
+                     Twine(*static_cast<uint64_t *>(SizePtr)) + " bytes.");
+}
Index: include/llvm/Object/Decompressor.h
===================================================================
--- include/llvm/Object/Decompressor.h
+++ include/llvm/Object/Decompressor.h
@@ -13,6 +13,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/ErrorHandling.h"
 
 namespace llvm {
 namespace object {
@@ -31,7 +32,9 @@
   /// @brief Resize the buffer and uncompress section data into it.
   /// @param Out         Destination buffer.
   template <class T> Error resizeAndDecompress(T &Out) {
+    install_bad_alloc_error_handler(outOfMemoryHandler, &DecompressedSize);
     Out.resize(DecompressedSize);
+    remove_bad_alloc_error_handler();
     return decompress({Out.data(), (size_t)DecompressedSize});
   }
 
@@ -52,6 +55,8 @@
   static bool isGnuStyle(StringRef Name);
 
 private:
+  static void outOfMemoryHandler(void *SizePtr, const std::string &Message,
+                                 bool);
   Decompressor(StringRef Data);
 
   Error consumeCompressedGnuHeader();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37447.113805.patch
Type: text/x-patch
Size: 2011 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170905/03bbf122/attachment.bin>


More information about the llvm-commits mailing list