[PATCH] D48476: Detecting leaked instructions with metadata and freeing the metadata to avoid use-after-free

Arvīds Kokins via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 22 03:19:57 PDT 2018


akokins created this revision.
Herald added a subscriber: llvm-commits.

Original bug <https://bugs.llvm.org/show_bug.cgi?id=33327>

When instructions with metadata are accidentally leaked, the result is a difficult-to-find memory corruption in `~LLVMContextImpl` that leads to random crashes.

This patch implements the detection and graceful handling of such issues.


Repository:
  rL LLVM

https://reviews.llvm.org/D48476

Files:
  lib/IR/LLVMContextImpl.cpp


Index: lib/IR/LLVMContextImpl.cpp
===================================================================
--- lib/IR/LLVMContextImpl.cpp
+++ lib/IR/LLVMContextImpl.cpp
@@ -48,6 +48,15 @@
   while (!OwnedModules.empty())
     delete *OwnedModules.begin();
 
+  // Drop metadata references from leaked Instructions to prevent random crashes
+#ifndef NDEBUG
+  for (auto &Pair : InstructionMetadata)
+    Pair.first->dump();
+  assert(InstructionMetadata.empty() &&
+         "Instructions with metadata have been leaked");
+#endif
+  InstructionMetadata.clear();
+
   // Drop references for MDNodes.  Do this before Values get deleted to avoid
   // unnecessary RAUW when nodes are still unresolved.
   for (auto *I : DistinctMDNodes)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48476.152445.patch
Type: text/x-patch
Size: 729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180622/d15d416b/attachment.bin>


More information about the llvm-commits mailing list