From 69ee62cef82eef7d23d2c71fb133ba80cac18339 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Fri, 29 Jun 2018 20:13:13 +0000 Subject: [PATCH] [LLVMContext] Detecting leaked instructions with metadata MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp index f0ac458..3c34ca5 100644 --- a/llvm/lib/IR/LLVMContextImpl.cpp +++ b/llvm/lib/IR/LLVMContextImpl.cpp @@ -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) -- 2.7.4