[compiler-rt] [sanitizer] Fix asserts in asan and tsan in pthread interceptors. (PR #75394)

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 10 00:26:22 PST 2024


================
@@ -53,16 +56,30 @@ void ThreadArgRetval::Finish(uptr thread, void* retval) {
 u32 ThreadArgRetval::BeforeJoin(uptr thread) const {
   __sanitizer::Lock lock(&mtx_);
   auto t = data_.find(thread);
-  CHECK(t);
-  CHECK(!t->second.detached);
-  return t->second.gen;
+  bool detect_invalid_join = common_flags()->detect_invalid_join;
+  if (t && !t->second.detached) {
+    return t->second.gen;
+  }
+  if (!t && detect_invalid_join) {
+    Report("ERROR: %s: Joining already joined thread, aborting.\n",
+           SanitizerToolName);
+    Die();
+  } else if (t && t->second.detached && detect_invalid_join) {
+    Report("ERROR: %s: Joining detached thread, aborting.\n",
+           SanitizerToolName);
+    Die();
+  } else
+    // FIXME: Not sure if strictly necessary to reserve an invalid value.
+    // Could we return 0 instead?
+    return kInvalidGen;
 }
 
 void ThreadArgRetval::AfterJoin(uptr thread, u32 gen) {
   __sanitizer::Lock lock(&mtx_);
   auto t = data_.find(thread);
-  if (!t || gen != t->second.gen) {
-    // Thread was reused and erased by any other event.
+  if (!t || gen != t->second.gen || gen == kInvalidGen) {
----------------
vitalybuka wrote:

we should not have t->second.gen = kInvalidGen, 

to make sure skip invalid here `.gen = gen_++;`

https://github.com/llvm/llvm-project/pull/75394


More information about the llvm-commits mailing list