[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:40:15 PDT 2017
JDevlieghere updated this revision to Diff 113809.
JDevlieghere added a subscriber: kcc.
JDevlieghere added a comment.
Thanks! I've updated the diff to include the section name.
My test case is the one from oss-fuzz, so unfortunately the section name doesn't say much. I don't know enough about the binary file format to change it.
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 of ' ' failed: unable to allocate 2314885530818453536 bytes.
Index: lib/Object/Decompressor.cpp
===================================================================
--- lib/Object/Decompressor.cpp
+++ 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) {
+ auto *D = static_cast<class Decompressor *>(Data);
+ report_fatal_error("Decompression of '" + D->getSectionName() +
+ "' failed: unable to allocate " +
+ Twine(D->getDecompressedSize()) + " 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,14 +32,19 @@
/// @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});
}
/// @brief Uncompress section data to raw buffer provided.
/// @param Buffer Destination buffer.
Error decompress(MutableArrayRef<char> Buffer);
+ /// @brief Return name of the section that is being decompressed.
+ StringRef getSectionName() { return SectionData; }
+
/// @brief Return memory buffer size required for decompression.
uint64_t getDecompressedSize() { return DecompressedSize; }
@@ -52,11 +58,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;
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37447.113809.patch
Type: text/x-patch
Size: 3376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170905/0302303b/attachment.bin>
More information about the llvm-commits
mailing list