aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2026-05-08 17:59:54 +0000
committerDimitry Andric <dim@FreeBSD.org>2026-05-08 17:59:54 +0000
commitd9b272a19d39f71665b529860bfed731bcfd99f5 (patch)
treecd0d7dea36444540fdd78709e77be92efb83ac2b
parentbd1e789b8452a8c2f166a3b4defb95330c71dadd (diff)
Merge commit 871038759afb from llvm git (by Marco Elver):
Thread Safety Analysis: Fix pointer handling of variables with deprecated attributes (#148974) de10e44b6fe7 ("Thread Safety Analysis: Support warning on passing/returning pointers to guarded variables") added checks for passing pointer to guarded variables. While new features do not necessarily need to support the deprecated attributes (`guarded_var`, and `pt_guarded_var`), we need to ensure that such features do not cause the compiler to crash. As such, code such as this: struct { int v __attribute__((guarded_var)); } p; int *g() { return &p.v; // handleNoMutexHeld() with POK_ReturnPointer } Would crash in debug builds with the assertion in handleNoMutexHeld() triggering. The assertion is meant to capture the fact that this helper should only be used for warnings on variables (which the deprecated attributes only applied to). To fix, the function handleNoMutexHeld() should handle all POK cases that apply to variables explicitly, and produce a best-effort warning. We refrain from introducing new warnings to avoid unnecessary code bloat for deprecated features. Fixes: https://github.com/llvm/llvm-project/issues/140330 This fixes an assertion while building the net/openvswitch port: "Assertion failed: ((POK == POK_VarAccess || POK == POK_VarDereference) && "Only works for variables"), function handleNoMutexHeld, file /usr/src/contrib/llvm-project/clang/lib/Sema/AnalysisBasedWarnings.cpp, line 2120.' Reported by: cy PR: 295101, 292067 MFC after: 1 month
-rw-r--r--contrib/llvm-project/clang/lib/Sema/AnalysisBasedWarnings.cpp25
1 files changed, 20 insertions, 5 deletions
diff --git a/contrib/llvm-project/clang/lib/Sema/AnalysisBasedWarnings.cpp b/contrib/llvm-project/clang/lib/Sema/AnalysisBasedWarnings.cpp
index ec39bca6039f..ab55cbceeaa8 100644
--- a/contrib/llvm-project/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/contrib/llvm-project/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2116,11 +2116,26 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler {
void handleNoMutexHeld(const NamedDecl *D, ProtectedOperationKind POK,
AccessKind AK, SourceLocation Loc) override {
- assert((POK == POK_VarAccess || POK == POK_VarDereference) &&
- "Only works for variables");
- unsigned DiagID = POK == POK_VarAccess?
- diag::warn_variable_requires_any_lock:
- diag::warn_var_deref_requires_any_lock;
+ unsigned DiagID = 0;
+ switch (POK) {
+ case POK_VarAccess:
+ case POK_PassByRef:
+ case POK_ReturnByRef:
+ case POK_PassPointer:
+ case POK_ReturnPointer:
+ DiagID = diag::warn_variable_requires_any_lock;
+ break;
+ case POK_VarDereference:
+ case POK_PtPassByRef:
+ case POK_PtReturnByRef:
+ case POK_PtPassPointer:
+ case POK_PtReturnPointer:
+ DiagID = diag::warn_var_deref_requires_any_lock;
+ break;
+ case POK_FunctionCall:
+ llvm_unreachable("Only works for variables");
+ break;
+ }
PartialDiagnosticAt Warning(Loc, S.PDiag(DiagID)
<< D << getLockKindFromAccessKind(AK));
Warnings.emplace_back(std::move(Warning), getNotes());