Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Thread Safety Analysis: Support reentrant capabilities #137133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ Improvements to Clang's diagnostics
as function arguments or return value respectively. Note that
:doc:`ThreadSafetyAnalysis` still does not perform alias analysis. The
feature will be default-enabled with ``-Wthread-safety`` in a future release.
- The :doc:`ThreadSafetyAnalysis` now supports reentrant capabilities.
- Clang will now do a better job producing common nested names, when producing
common types for ternary operator, template argument deduction and multiple return auto deduction.
- The ``-Wsign-compare`` warning now treats expressions with bitwise not(~) and minus(-) as signed integers
Expand Down
18 changes: 18 additions & 0 deletions 18 clang/docs/ThreadSafetyAnalysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,21 @@ class can be used as a capability. The string argument specifies the kind of
capability in error messages, e.g. ``"mutex"``. See the ``Container`` example
given above, or the ``Mutex`` class in :ref:`mutexheader`.

REENTRANT_CAPABILITY
--------------------

``REENTRANT_CAPABILITY`` is an attribute on capability classes, denoting that
they are reentrant. Marking a capability as reentrant means that acquiring the
same capability multiple times is safe. Acquiring the same capability with
different access privileges (exclusive vs. shared) again is not considered
reentrant by the analysis.

Note: In many cases this attribute is only required where a capability is
acquired reentrant within the same function, such as via macros or other
helpers. Otherwise, best practice is to avoid explicitly acquiring a capability
multiple times within the same function, and letting the analysis produce
warnings on double-acquisition attempts.

.. _scoped_capability:

SCOPED_CAPABILITY
Expand Down Expand Up @@ -846,6 +861,9 @@ implementation.
#define CAPABILITY(x) \
THREAD_ANNOTATION_ATTRIBUTE__(capability(x))

#define REENTRANT_CAPABILITY \
THREAD_ANNOTATION_ATTRIBUTE__(reentrant_capability)

#define SCOPED_CAPABILITY \
THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)

Expand Down
4 changes: 3 additions & 1 deletion 4 clang/include/clang/Analysis/Analyses/ThreadSafety.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ class ThreadSafetyHandler {
/// \param LocEndOfScope -- The location of the end of the scope where the
/// mutex is no longer held
/// \param LEK -- which of the three above cases we should warn for
/// \param ReentrancyMismatch -- mismatching reentrancy depth
virtual void handleMutexHeldEndOfScope(StringRef Kind, Name LockName,
SourceLocation LocLocked,
SourceLocation LocEndOfScope,
LockErrorKind LEK) {}
LockErrorKind LEK,
bool ReentrancyMismatch = false) {}

/// Warn when a mutex is held exclusively and shared at the same point. For
/// example, if a mutex is locked exclusively during an if branch and shared
Expand Down
22 changes: 13 additions & 9 deletions 22 clang/include/clang/Analysis/Analyses/ThreadSafetyCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYCOMMON_H

#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
#include "clang/Analysis/Analyses/ThreadSafetyTraverse.h"
Expand Down Expand Up @@ -272,27 +273,34 @@ class CFGWalker {
class CapabilityExpr {
private:
static constexpr unsigned FlagNegative = 1u << 0;
static constexpr unsigned FlagReentrant = 1u << 1;

/// The capability expression and flags.
llvm::PointerIntPair<const til::SExpr *, 1, unsigned> CapExpr;
llvm::PointerIntPair<const til::SExpr *, 2, unsigned> CapExpr;

/// The kind of capability as specified by @ref CapabilityAttr::getName.
StringRef CapKind;

public:
CapabilityExpr() : CapExpr(nullptr, 0) {}
CapabilityExpr(const til::SExpr *E, StringRef Kind, bool Neg)
: CapExpr(E, Neg ? FlagNegative : 0), CapKind(Kind) {}
CapabilityExpr(const til::SExpr *E, StringRef Kind, bool Neg, bool Reentrant)
: CapExpr(E, (Neg ? FlagNegative : 0) | (Reentrant ? FlagReentrant : 0)),
CapKind(Kind) {}
// Infers `Kind` and `Reentrant` from `QT`.
CapabilityExpr(const til::SExpr *E, QualType QT, bool Neg);
melver marked this conversation as resolved.
Show resolved Hide resolved

// Don't allow implicitly-constructed StringRefs since we'll capture them.
template <typename T> CapabilityExpr(const til::SExpr *, T, bool) = delete;
template <typename T>
CapabilityExpr(const til::SExpr *, T, bool, bool) = delete;

const til::SExpr *sexpr() const { return CapExpr.getPointer(); }
StringRef getKind() const { return CapKind; }
bool negative() const { return CapExpr.getInt() & FlagNegative; }
bool reentrant() const { return CapExpr.getInt() & FlagReentrant; }

CapabilityExpr operator!() const {
return CapabilityExpr(CapExpr.getPointer(), CapKind, !negative());
return CapabilityExpr(CapExpr.getPointer(), CapKind, !negative(),
reentrant());
}

bool equals(const CapabilityExpr &other) const {
Expand Down Expand Up @@ -389,10 +397,6 @@ class SExprBuilder {
// Translate a variable reference.
til::LiteralPtr *createVariable(const VarDecl *VD);

// Create placeholder for this: we don't know the VarDecl on construction yet.
std::pair<til::LiteralPtr *, StringRef>
createThisPlaceholder(const Expr *Exp);

// Translate a clang statement or expression to a TIL expression.
// Also performs substitution of variables; Ctx provides the context.
// Dispatches on the type of S.
Expand Down
7 changes: 7 additions & 0 deletions 7 clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -4038,6 +4038,13 @@ def LocksExcluded : InheritableAttr {
let Documentation = [Undocumented];
}

def ReentrantCapability : InheritableAttr {
let Spellings = [Clang<"reentrant_capability">];
let Subjects = SubjectList<[Record, TypedefName]>;
let Documentation = [Undocumented];
melver marked this conversation as resolved.
Show resolved Hide resolved
let SimpleHandler = 1;
}

// C/C++ consumed attributes.

def Consumable : InheritableAttr {
Expand Down
7 changes: 5 additions & 2 deletions 7 clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4082,6 +4082,9 @@ def warn_thread_attribute_not_on_scoped_lockable_param : Warning<
"%0 attribute applies to function parameters only if their type is a "
"reference to a 'scoped_lockable'-annotated type">,
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
def warn_thread_attribute_requires_preceded : Warning<
"%0 attribute on %1 must be preceded by %2 attribute">,
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
def err_attribute_argument_out_of_bounds_extra_info : Error<
"%0 attribute parameter %1 is out of bounds: "
"%plural{0:no parameters to index into|"
Expand All @@ -4105,10 +4108,10 @@ def warn_expecting_locked : Warning<
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
// FIXME: improve the error message about locks not in scope
def warn_lock_some_predecessors : Warning<
"%0 '%1' is not held on every path through here">,
"%0 '%1' is not held on every path through here%select{| with equal reentrancy depth}2">,
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
def warn_expecting_lock_held_on_loop : Warning<
"expecting %0 '%1' to be held at start of each loop">,
"expecting %0 '%1' to be held at start of each loop%select{| with equal reentrancy depth}2">,
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
def note_locked_here : Note<"%0 acquired here">;
def note_unlocked_here : Note<"%0 released here">;
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.