[GISel]: Fix undefined behavior in IRTranslator
authorAditya Nandakumar <aditya_nandakumar@apple.com>
Wed, 17 May 2017 17:41:55 +0000 (17:41 +0000)
committerAditya Nandakumar <aditya_nandakumar@apple.com>
Wed, 17 May 2017 17:41:55 +0000 (17:41 +0000)
Make sure IRTranslator->MachineIRBuilder->DebugLoc doesn't
outlive the DILocation. Clear it at the end of
IRTranslator::runOnMachineFunction

llvm-svn: 303277

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

index 811858f..77dfb13 100644 (file)
@@ -1129,6 +1129,11 @@ void IRTranslator::finalizeFunction() {
   ValToVReg.clear();
   FrameIndices.clear();
   MachinePreds.clear();
+  // MachineIRBuilder::DebugLoc can outlive the DILocation it holds. Clear it
+  // to avoid accessing free’d memory (in runOnMachineFunction) and to avoid
+  // destroying it twice (in ~IRTranslator() and ~LLVMContext())
+  EntryBuilder = MachineIRBuilder();
+  CurBuilder = MachineIRBuilder();
 }
 
 bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {