================
@@ -9822,6 +9825,65 @@ void Sema::CheckMaxUnsignedZero(const CallExpr *Call,
         << FixItHint::CreateRemoval(RemovalRange);
 }
 
+//===--- CHECK: Warn on use of `std::atomic_thread_fence` with TSan. 
------===//
+void Sema::CheckUseOfAtomicThreadFenceWithTSan(const CallExpr *Call,
+                                               const FunctionDecl *FDecl) {
+  // Thread sanitizer currently does not support `std::atomic_thread_fence`,
+  // leading to false positives. Example issue:
+  // https://github.com/llvm/llvm-project/issues/52942
+
+  if (!Call || !FDecl)
+    return;
+
+  if (!IsStdFunction(FDecl, "atomic_thread_fence"))
+    return;
+
+  // Check that TSan is enabled in this context
+  const auto EnabledTSanMask =
+      Context.getLangOpts().Sanitize.Mask & (SanitizerKind::Thread);
+  if (!EnabledTSanMask)
+    return;
+
+  // Check that the file isn't in the no-sanitize list
+  const auto &NoSanitizeList = Context.getNoSanitizeList();
+  if (NoSanitizeList.containsLocation(EnabledTSanMask,
+                                      Call->getSourceRange().getBegin()))
+    return;
+
+  std::unique_ptr<MangleContext> MC(Context.createMangleContext());
+
+  // Check that the calling function or lambda:
+  //  - Does not have any attributes preventing TSan checking
+  //  - Is not in the ignore list
+  auto IsNotSanitized = [&](NamedDecl *Decl) {
+    const auto SpecificAttrs = Decl->specific_attrs<NoSanitizeAttr>();
+    const auto IsNoSanitizeThreadAttr = [](NoSanitizeAttr *Attr) {
+      return static_cast<bool>(Attr->getMask() & SanitizerKind::Thread);
+    };
+
+    // Get mangled name for ignorelist lookup
+    std::string MangledName;
+    if (MC->shouldMangleDeclName(Decl)) {
+      llvm::raw_string_ostream S = llvm::raw_string_ostream(MangledName);
+      MC->mangleName(Decl, S);
+    } else {
+      MangledName = Decl->getName();
+    }
+
+    return Decl &&
+           (Decl->hasAttr<DisableSanitizerInstrumentationAttr>() ||
+            std::any_of(SpecificAttrs.begin(), SpecificAttrs.end(),
+                        IsNoSanitizeThreadAttr) ||
+            NoSanitizeList.containsFunction(EnabledTSanMask, MangledName));
+  };
+  if (IsNotSanitized(getCurFunctionOrMethodDecl()))
+    return;
+  if (IsNotSanitized(getCurFunctionDecl(/*AllowLambdas*/ true)))
+    return;
----------------
BStott6 wrote:

Nevermind, I was under the impression that a no-sanitize attribute on the 
enclosing function would also apply to lambas created within, but this is not 
the case so you are right.

https://github.com/llvm/llvm-project/pull/166542
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to