27#include "llvm/ADT/STLExtras.h"
28#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/StringExtras.h"
40struct CritSectionMarker {
41 const Expr *LockExpr{};
42 const MemRegion *LockReg{};
44 void Profile(llvm::FoldingSetNodeID &ID)
const {
49 [[nodiscard]]
constexpr bool
51 return LockExpr ==
Other.LockExpr && LockReg ==
Other.LockReg;
53 [[nodiscard]]
constexpr bool
55 return !(*
this ==
Other);
59class CallDescriptionBasedMatcher {
60 CallDescription LockFn;
61 CallDescription UnlockFn;
64 CallDescriptionBasedMatcher(CallDescription &&LockFn,
65 CallDescription &&UnlockFn)
66 : LockFn(std::move(LockFn)), UnlockFn(std::move(UnlockFn)) {}
67 [[nodiscard]]
bool matches(
const CallEvent &
Call,
bool IsLock)
const {
69 return LockFn.matches(
Call);
71 return UnlockFn.matches(
Call);
75class FirstArgMutexDescriptor :
public CallDescriptionBasedMatcher {
77 FirstArgMutexDescriptor(CallDescription &&LockFn, CallDescription &&UnlockFn)
78 : CallDescriptionBasedMatcher(std::move(LockFn), std::move(UnlockFn)) {}
80 [[nodiscard]]
const MemRegion *
getRegion(
const CallEvent &
Call,
bool)
const {
81 return Call.getArgSVal(0).getAsRegion();
85class MemberMutexDescriptor :
public CallDescriptionBasedMatcher {
87 MemberMutexDescriptor(CallDescription &&LockFn, CallDescription &&UnlockFn)
88 : CallDescriptionBasedMatcher(std::move(LockFn), std::move(UnlockFn)) {}
90 [[nodiscard]]
const MemRegion *
getRegion(
const CallEvent &
Call,
bool)
const {
95class RAIIMutexDescriptor {
96 mutable const IdentifierInfo *Guard{};
97 mutable bool IdentifierInfoInitialized{};
98 mutable llvm::SmallString<32> GuardName{};
100 void initIdentifierInfo(
const CallEvent &
Call)
const {
101 if (!IdentifierInfoInitialized) {
107 const auto &ASTCtx =
Call.getASTContext();
108 Guard = &ASTCtx.Idents.get(GuardName);
112 template <
typename T>
bool matchesImpl(
const CallEvent &
Call)
const {
113 const T *
C = dyn_cast<T>(&
Call);
116 const IdentifierInfo *II =
123 if constexpr (std::is_same_v<T, CXXConstructorCall>) {
124 if (GuardName ==
"unique_lock" &&
C->getNumArgs() >= 2) {
125 const Expr *SecondArg =
C->getArgExpr(1);
128 RD && RD->
getName() ==
"defer_lock_t" && RD->isInStdNamespace()) {
138 RAIIMutexDescriptor(StringRef GuardName) : GuardName(GuardName) {}
139 [[nodiscard]]
bool matches(
const CallEvent &
Call,
bool IsLock)
const {
140 initIdentifierInfo(
Call);
142 return matchesImpl<CXXConstructorCall>(
Call);
144 return matchesImpl<CXXDestructorCall>(
Call);
146 [[nodiscard]]
const MemRegion *
getRegion(
const CallEvent &
Call,
148 const MemRegion *LockRegion =
nullptr;
150 if (std::optional<SVal> Object =
Call.getReturnValueUnderConstruction()) {
151 LockRegion =
Object->getAsRegion();
160using MutexDescriptor =
161 std::variant<FirstArgMutexDescriptor, MemberMutexDescriptor,
162 RAIIMutexDescriptor>;
166 const CallDescription OpenFunction{CDM::CLibrary, {
"open"}, 2};
168 const int NonBlockMacroVal;
169 bool Satisfied =
false;
172 SuppressNonBlockingStreams(
SymbolRef StreamSym,
int NonBlockMacroVal)
173 : StreamSym(StreamSym), NonBlockMacroVal(NonBlockMacroVal) {}
175 static void *getTag() {
180 void Profile(llvm::FoldingSetNodeID &ID)
const override {
181 ID.AddPointer(getTag());
185 BugReporterContext &BRC,
186 PathSensitiveBugReport &BR)
override {
190 std::optional<StmtPoint> Point = N->
getLocationAs<StmtPoint>();
194 const auto *CE = Point->getStmtAs<CallExpr>();
195 if (!CE || !OpenFunction.matchesAsWritten(*CE))
208 if ((*FlagVal & NonBlockMacroVal) != 0)
215class BlockInCriticalSectionChecker :
public Checker<check::PostCall> {
217 const std::array<MutexDescriptor, 9> MutexDescriptors{
225 MemberMutexDescriptor(
229 {CDM::CXXMethod, {
"std",
"unlock"}, 0}),
230 FirstArgMutexDescriptor({CDM::CLibrary, {
"pthread_mutex_lock"}, 1},
231 {CDM::CLibrary, {
"pthread_mutex_unlock"}, 1}),
232 FirstArgMutexDescriptor({CDM::CLibrary, {
"mtx_lock"}, 1},
233 {CDM::CLibrary, {
"mtx_unlock"}, 1}),
234 FirstArgMutexDescriptor({CDM::CLibrary, {
"pthread_mutex_trylock"}, 1},
235 {CDM::CLibrary, {
"pthread_mutex_unlock"}, 1}),
236 FirstArgMutexDescriptor({CDM::CLibrary, {
"mtx_trylock"}, 1},
237 {CDM::CLibrary, {
"mtx_unlock"}, 1}),
238 FirstArgMutexDescriptor({CDM::CLibrary, {
"mtx_timedlock"}, 1},
239 {CDM::CLibrary, {
"mtx_unlock"}, 1}),
240 RAIIMutexDescriptor(
"lock_guard"),
241 RAIIMutexDescriptor(
"unique_lock"),
242 RAIIMutexDescriptor(
"scoped_lock")};
244 const CallDescriptionSet BlockingFunctions{{CDM::CLibrary, {
"sleep"}},
245 {CDM::CLibrary, {
"getc"}},
246 {CDM::CLibrary, {
"fgets"}},
247 {CDM::CLibrary, {
"read"}},
248 {CDM::CLibrary, {
"recv"}}};
250 const BugType BlockInCritSectionBugType{
251 this,
"Call to blocking function in critical section",
"Blocking Error"};
253 using O_NONBLOCKValueTy = std::optional<int>;
254 mutable std::optional<O_NONBLOCKValueTy> O_NONBLOCKValue;
256 void reportBlockInCritSection(
const CallEvent &call, CheckerContext &
C)
const;
258 [[nodiscard]]
const NoteTag *createCritSectionNote(CritSectionMarker M,
259 CheckerContext &
C)
const;
261 [[nodiscard]] std::optional<MutexDescriptor>
262 checkDescriptorMatch(
const CallEvent &
Call, CheckerContext &
C,
265 void handleLock(
const MutexDescriptor &Mutex,
const CallEvent &
Call,
266 CheckerContext &
C)
const;
268 void handleUnlock(
const MutexDescriptor &Mutex,
const CallEvent &
Call,
269 CheckerContext &
C)
const;
271 [[nodiscard]]
bool isBlockingInCritSection(
const CallEvent &
Call,
272 CheckerContext &
C)
const;
278 void checkPostCall(
const CallEvent &
Call, CheckerContext &
C)
const;
298std::optional<MutexDescriptor>
299BlockInCriticalSectionChecker::checkDescriptorMatch(
const CallEvent &
Call,
302 const auto Descriptor =
303 llvm::find_if(MutexDescriptors, [&
Call, IsLock](
auto &&Descriptor) {
305 [&
Call, IsLock](
auto &&DescriptorImpl) {
306 return DescriptorImpl.matches(
Call, IsLock);
310 if (Descriptor != MutexDescriptors.end())
317 const auto *BaseClassRegion = dyn_cast<CXXBaseObjectRegion>(Reg);
320 Reg = BaseClassRegion->getSuperRegion();
326 const MutexDescriptor &Descriptor,
335void BlockInCriticalSectionChecker::handleLock(
336 const MutexDescriptor &LockDescriptor,
const CallEvent &
Call,
337 CheckerContext &
C)
const {
338 const MemRegion *MutexRegion =
343 const CritSectionMarker MarkToAdd{
Call.getOriginExpr(), MutexRegion};
345 C.getState()->add<ActiveCritSections>(MarkToAdd);
346 C.addTransition(StateWithLockEvent, createCritSectionNote(MarkToAdd,
C));
349void BlockInCriticalSectionChecker::handleUnlock(
350 const MutexDescriptor &UnlockDescriptor,
const CallEvent &
Call,
351 CheckerContext &
C)
const {
352 const MemRegion *MutexRegion =
358 const auto ActiveSections = State->get<ActiveCritSections>();
359 const auto MostRecentLock =
360 llvm::find_if(ActiveSections, [MutexRegion](
auto &&Marker) {
361 return Marker.LockReg == MutexRegion;
363 if (MostRecentLock == ActiveSections.end())
367 auto &Factory = State->get_context<ActiveCritSections>();
368 llvm::ImmutableList<CritSectionMarker> NewList = Factory.getEmptyList();
369 for (
auto It = ActiveSections.begin(), End = ActiveSections.end(); It != End;
371 if (It != MostRecentLock)
372 NewList = Factory.add(*It, NewList);
375 State = State->set<ActiveCritSections>(NewList);
376 C.addTransition(State);
379bool BlockInCriticalSectionChecker::isBlockingInCritSection(
380 const CallEvent &
Call, CheckerContext &
C)
const {
382 !
C.getState()->get<ActiveCritSections>().isEmpty();
385void BlockInCriticalSectionChecker::checkPostCall(
const CallEvent &
Call,
386 CheckerContext &
C)
const {
387 if (isBlockingInCritSection(
Call,
C)) {
388 reportBlockInCritSection(
Call,
C);
389 }
else if (std::optional<MutexDescriptor> LockDesc =
390 checkDescriptorMatch(
Call,
C,
true)) {
391 handleLock(*LockDesc,
Call,
C);
392 }
else if (std::optional<MutexDescriptor> UnlockDesc =
393 checkDescriptorMatch(
Call,
C,
false)) {
394 handleUnlock(*UnlockDesc,
Call,
C);
398void BlockInCriticalSectionChecker::reportBlockInCritSection(
399 const CallEvent &
Call, CheckerContext &
C)
const {
400 ExplodedNode *ErrNode =
C.generateNonFatalErrorNode(
C.getState());
405 llvm::raw_string_ostream os(msg);
406 os <<
"Call to blocking function '" <<
Call.getCalleeIdentifier()->getName()
407 <<
"' inside of critical section";
408 auto R = std::make_unique<PathSensitiveBugReport>(BlockInCritSectionBugType,
414 StringRef FuncName =
Call.getCalleeIdentifier()->getName();
415 if (FuncName ==
"read" || FuncName ==
"recv") {
416 SVal SV =
Call.getArgSVal(0);
417 SValBuilder &SVB =
C.getSValBuilder();
419 ConditionTruthVal CTV =
420 state->areEqual(SV, SVB.
makeIntVal(-1,
C.getASTContext().IntTy));
425 if (!O_NONBLOCKValue)
427 "O_NONBLOCK",
C.getBugReporter().getPreprocessor());
428 if (*O_NONBLOCKValue)
429 R->addVisitor<SuppressNonBlockingStreams>(SR, **O_NONBLOCKValue);
432 R->addRange(
Call.getSourceRange());
433 R->markInteresting(
Call.getReturnValue());
434 C.emitReport(std::move(R));
438BlockInCriticalSectionChecker::createCritSectionNote(CritSectionMarker M,
439 CheckerContext &
C)
const {
440 const BugType *BT = &this->BlockInCritSectionBugType;
441 return C.getNoteTag([M, BT](PathSensitiveBugReport &BR,
442 llvm::raw_ostream &
OS) {
447 const auto CritSectionBegins =
449 llvm::SmallVector<CritSectionMarker, 4> LocksForMutex;
451 CritSectionBegins, std::back_inserter(LocksForMutex),
452 [M](
const auto &Marker) {
return Marker.LockReg == M.LockReg; });
453 if (LocksForMutex.empty())
458 std::reverse(LocksForMutex.begin(), LocksForMutex.end());
462 const auto Position =
463 llvm::find_if(std::as_const(LocksForMutex), [M](
const auto &Marker) {
464 return Marker.LockExpr == M.LockExpr;
466 if (Position == LocksForMutex.end())
471 if (LocksForMutex.size() == 1) {
472 OS <<
"Entering critical section here";
476 const auto IndexOfLock =
477 std::distance(std::as_const(LocksForMutex).begin(), Position);
479 const auto OrdinalOfLock = IndexOfLock + 1;
480 OS <<
"Entering critical section for the " << OrdinalOfLock
481 << llvm::getOrdinalSuffix(OrdinalOfLock) <<
" time here";
485void ento::registerBlockInCriticalSectionChecker(CheckerManager &mgr) {
489bool ento::shouldRegisterBlockInCriticalSectionChecker(
490 const CheckerManager &mgr) {
static const MemRegion * skipStdBaseClassRegion(const MemRegion *Reg)
static const MemRegion * getRegion(const CallEvent &Call, const MutexDescriptor &Descriptor, bool IsLock)
#define REGISTER_LIST_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable list type NameTy, suitable for placement into the ProgramState.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
const BugType & getBugType() const
BugReporterVisitors are used to add custom diagnostics along a path.
bool contains(const CallEvent &Call) const
Represents an abstract call to a function or method along a particular path.
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
bool isConstrainedTrue() const
Return true if the constraint is perfectly constrained to 'true'.
const ProgramStateRef & getState() const
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
std::optional< T > getLocationAs() const &
MemRegion - The root abstract class for all memory regions.
const ExplodedNode * getErrorNode() const
void markInvalid(const void *Tag, const void *Data)
Marks the current report as invalid, meaning that it is probably a false positive and should not be r...
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
const llvm::APSInt * getAsInteger() const
If this SVal is loc::ConcreteInt or nonloc::ConcreteInt, return a pointer to APSInt which is held in ...
bool isWithinStdNamespace(const Decl *D)
Returns true if declaration D is in std namespace or any nested namespace or class scope.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
std::optional< int > tryExpandAsInteger(StringRef Macro, const Preprocessor &PP)
Try to parse the value of a defined preprocessor macro.
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
bool matches(const til::SExpr *E1, const til::SExpr *E2)
The JSON file list parser is used to communicate input to InstallAPI.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
const FunctionProtoType * T
bool operator!=(CanQual< T > x, CanQual< U > y)
U cast(CodeGen::Address addr)
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
CritSectionMarker & reference
CritSectionMarker * pointer
std::ptrdiff_t difference_type
CritSectionMarker value_type
std::forward_iterator_tag iterator_category