[PATCH] D86892: Improve error handling for SmallVector programming errors.

Geoffrey Martin-Noble via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 12:50:53 PDT 2020


GMNGeoffrey created this revision.
GMNGeoffrey added a reviewer: chandlerc.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
GMNGeoffrey requested review of this revision.

This patch changes errors in SmallVector::grow_pod that are indepdent of
memory capacity to be reported using report_fatal_error instead of
report_bad_alloc_error, which falsely signals an OOM.

It also makes report_bad_alloc_error to print the failure reason passed
to it and fixes the documentation to indicate that it calls `abort()`
not "an assertion".


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86892

Files:
  llvm/include/llvm/Support/ErrorHandling.h
  llvm/lib/Support/ErrorHandling.cpp
  llvm/lib/Support/SmallVector.cpp


Index: llvm/lib/Support/SmallVector.cpp
===================================================================
--- llvm/lib/Support/SmallVector.cpp
+++ llvm/lib/Support/SmallVector.cpp
@@ -49,14 +49,19 @@
   // Ensure we can fit the new capacity.
   // This is only going to be applicable when the capacity is 32 bit.
   if (MinCapacity > SizeTypeMax())
-    report_bad_alloc_error("SmallVector capacity overflow during allocation");
+    report_fatal_error("SmallVector unable to grow. Requested capacity (" +
+                       std::to_string(MinCapacity) +
+                       ") is larger than maximum capacity for size type (" +
+                       std::to_string(SizeTypeMax()) + ")");
 
   // Ensure we can meet the guarantee of space for at least one more element.
   // The above check alone will not catch the case where grow is called with a
   // default MinCapacity of 0, but the current capacity cannot be increased.
   // This is only going to be applicable when the capacity is 32 bit.
   if (capacity() == SizeTypeMax())
-    report_bad_alloc_error("SmallVector capacity unable to grow");
+    report_fatal_error(
+        "SmallVector capacity unable to grow. Already at maximum size " +
+        std::to_string(SizeTypeMax()));
 
   // In theory 2*capacity can overflow if the capacity is 64 bit, but the
   // original capacity would never be large enough for this to be a problem.
Index: llvm/lib/Support/ErrorHandling.cpp
===================================================================
--- llvm/lib/Support/ErrorHandling.cpp
+++ llvm/lib/Support/ErrorHandling.cpp
@@ -170,6 +170,7 @@
   // an OOM to stderr and abort.
   char OOMMessage[] = "LLVM ERROR: out of memory\n";
   ssize_t written = ::write(2, OOMMessage, strlen(OOMMessage));
+  written = ::write(2, Reason, strlen(Reason));
   (void)written;
   abort();
 #endif
Index: llvm/include/llvm/Support/ErrorHandling.h
===================================================================
--- llvm/include/llvm/Support/ErrorHandling.h
+++ llvm/include/llvm/Support/ErrorHandling.h
@@ -110,9 +110,9 @@
 /// the following unwind succeeds, e.g. do not trigger additional allocations
 /// in the unwind chain.
 ///
-/// If no error handler is installed (default), then a bad_alloc exception
-/// is thrown, if LLVM is compiled with exception support, otherwise an
-/// assertion is called.
+/// If no error handler is installed (default), throws a bad_alloc exception
+/// if LLVM is compiled with exception support. Otherwise prints the error
+/// to standard error and calls abort().
 LLVM_ATTRIBUTE_NORETURN void report_bad_alloc_error(const char *Reason,
                                                     bool GenCrashDiag = true);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86892.289012.patch
Type: text/x-patch
Size: 2725 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200831/2b34ff89/attachment.bin>


More information about the llvm-commits mailing list