[LLVMContext] Detecting leaked instructions with metadata
authorVedant Kumar <vsk@apple.com>
Fri, 29 Jun 2018 20:13:13 +0000 (20:13 +0000)
committerVedant Kumar <vsk@apple.com>
Fri, 29 Jun 2018 20:13:13 +0000 (20:13 +0000)
When instructions with metadata are accidentally leaked, the result is a
difficult-to-find memory corruption in ~LLVMContextImpl that leads to
random crashes.

Patch by ArvÄ«ds Kokins!

llvm-svn: 336010

llvm/lib/IR/LLVMContextImpl.cpp

index f0ac458..3c34ca5 100644 (file)
@@ -48,6 +48,14 @@ LLVMContextImpl::~LLVMContextImpl() {
   while (!OwnedModules.empty())
     delete *OwnedModules.begin();
 
+#ifndef NDEBUG
+  // Check for metadata references from leaked Instructions.
+  for (auto &Pair : InstructionMetadata)
+    Pair.first->dump();
+  assert(InstructionMetadata.empty() &&
+         "Instructions with metadata have been leaked");
+#endif
+
   // Drop references for MDNodes.  Do this before Values get deleted to avoid
   // unnecessary RAUW when nodes are still unresolved.
   for (auto *I : DistinctMDNodes)