[PATCH] tsan: support C++ exceptions

Dmitry Vyukov dvyukov at google.com
Thu Jun 25 11:30:12 PDT 2015

Hi chandlerc,

Work-in-progress, does not work yet.
The idea is to emit a cleanup landing pad for every function that will call __tsan_func_exit in case of exceptions flying by.
Problem one: the cleanup BB does not have predecessors. As the result it is not called in the function that throws.
Problem two: I am not sure how this interacts with functions that have [several nested] try/catch blocks -- the cleanup block must be executed only when we leave function, but not for inner try/catch blocks. So the cleanup block must be somehow "attached" to function exit.
I am looking for advice and/or examples of code that does a similar thing.



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;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10740.28481.patch
Type: text/x-patch
Size: 948 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150625/c8dc52b3/attachment.bin>

More information about the llvm-commits mailing list