[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 04:23:48 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL312526: [Decompression] Fail gracefully when out of memory (authored by JDevlieghere).

Changed prior to commit:
  https://reviews.llvm.org/D37447?vs=113814&id=113832#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D37447

Files:
  llvm/trunk/include/llvm/Object/Decompressor.h
  llvm/trunk/lib/Object/Decompressor.cpp
  llvm/trunk/test/DebugInfo/Inputs/dwarfdump-decompression-invalid-size.elf-x86-64
  llvm/trunk/test/DebugInfo/dwarfdump-decompression-invalid-size.test


Index: llvm/trunk/include/llvm/Object/Decompressor.h
===================================================================
--- llvm/trunk/include/llvm/Object/Decompressor.h
+++ llvm/trunk/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, this);
     Out.resize(DecompressedSize);
+    remove_bad_alloc_error_handler();
     return decompress({Out.data(), (size_t)DecompressedSize});
   }
 
@@ -52,11 +55,14 @@
   static bool isGnuStyle(StringRef Name);
 
 private:
-  Decompressor(StringRef Data);
+  static void outOfMemoryHandler(void *Data, const std::string &Message, bool);
+
+  Decompressor(StringRef Name, StringRef Data);
 
   Error consumeCompressedGnuHeader();
   Error consumeCompressedZLibHeader(bool Is64Bit, bool IsLittleEndian);
 
+  StringRef SectionName;
   StringRef SectionData;
   uint64_t DecompressedSize;
 };
Index: llvm/trunk/test/DebugInfo/dwarfdump-decompression-invalid-size.test
===================================================================
--- llvm/trunk/test/DebugInfo/dwarfdump-decompression-invalid-size.test
+++ llvm/trunk/test/DebugInfo/dwarfdump-decompression-invalid-size.test
@@ -0,0 +1,13 @@
+// dwarfdump-decompression-invalid-size.elf-x86-64 is prepared using following
+// source code and invocation:
+// test.cpp:
+// int main() { return 0; }
+//
+// gcc test.cpp -o out -g -Wl,--compress-debug-sections,zlib
+//
+// After that result object was modified manually. Decompressed size of
+// .debug_frame section was changed to 0xffffffffffffffff in compression
+// header.
+RUN: not llvm-dwarfdump %p/Inputs/dwarfdump-decompression-invalid-size.elf-x86-64 2>&1 | FileCheck %s
+
+CHECK: decompression of '.debug_frame' failed: unable to allocate 18446744073709551615 bytes.
Index: llvm/trunk/lib/Object/Decompressor.cpp
===================================================================
--- llvm/trunk/lib/Object/Decompressor.cpp
+++ llvm/trunk/lib/Object/Decompressor.cpp
@@ -23,16 +23,16 @@
   if (!zlib::isAvailable())
     return createError("zlib is not available");
 
-  Decompressor D(Data);
+  Decompressor D(Name, Data);
   Error Err = isGnuStyle(Name) ? D.consumeCompressedGnuHeader()
                                : D.consumeCompressedZLibHeader(Is64Bit, IsLE);
   if (Err)
     return std::move(Err);
   return D;
 }
 
-Decompressor::Decompressor(StringRef Data)
-    : SectionData(Data), DecompressedSize(0) {}
+Decompressor::Decompressor(StringRef Name, StringRef Data)
+    : SectionName(Name), SectionData(Data), DecompressedSize(0) {}
 
 Error Decompressor::consumeCompressedGnuHeader() {
   if (!SectionData.startswith("ZLIB"))
@@ -92,3 +92,11 @@
   size_t Size = Buffer.size();
   return zlib::uncompress(SectionData, Buffer.data(), Size);
 }
+
+void Decompressor::outOfMemoryHandler(void *Data, const std::string &Message,
+                                      bool) {
+  const auto *D = static_cast<const Decompressor *>(Data);
+  report_fatal_error("decompression of '" + Twine(D->SectionName) +
+                     "' failed: unable to allocate " +
+                     Twine(D->DecompressedSize) + " bytes.");
+}


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


More information about the llvm-commits mailing list