Index: lib/Transforms/Instrumentation/ThreadSanitizer.cpp =================================================================== --- lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -400,6 +400,16 @@ IRBuilder<> IRBRet(RetInst); IRBRet.CreateCall(TsanFuncExit, {}); } + // Create cleanup landing pad to call __tsan_func_exit on exceptions. + LLVMContext &C = F.getContext(); + BasicBlock *CleanupBB = BasicBlock::Create(C, "", &F); + IRBuilder<> IRBCleanup(CleanupBB); + Type *ExnTy = + StructType::get(Type::getInt8PtrTy(C), Type::getInt32Ty(C), nullptr); + Constant *PersFn = F.getParent()->getOrInsertFunction( + "__gcc_personality_v0", FunctionType::get(Type::getInt32Ty(C), true)); + IRBCleanup.CreateLandingPad(ExnTy, PersFn, 0)->setCleanup(true); + IRBCleanup.CreateCall(TsanFuncExit, {}); Res = true; } return Res;