2016-09-21 04:28:50 +08:00
|
|
|
//===-- BlockInCriticalSectionChecker.cpp -----------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-09-21 04:28:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Defines a checker for blocks in critical sections. This checker should find
|
|
|
|
// the calls to blocking functions (for example: sleep, getc, fgets, read,
|
|
|
|
// recv etc.) inside a critical section. When sleep(x) is called while a mutex
|
|
|
|
// is held, other threades cannot lock the same mutex. This might take some
|
|
|
|
// time, leading to bad performance or even deadlock.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2016-09-21 04:28:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-10-30 18:09:55 +08:00
|
|
|
class BlockInCriticalSectionChecker : public Checker<check::PostCall> {
|
|
|
|
|
|
|
|
mutable IdentifierInfo *IILockGuard, *IIUniqueLock;
|
2016-09-21 04:28:50 +08:00
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
CallDescription LockFn, UnlockFn, SleepFn, GetcFn, FgetsFn, ReadFn, RecvFn,
|
|
|
|
PthreadLockFn, PthreadTryLockFn, PthreadUnlockFn,
|
|
|
|
MtxLock, MtxTimedLock, MtxTryLock, MtxUnlock;
|
2016-09-21 04:28:50 +08:00
|
|
|
|
2017-10-30 18:09:55 +08:00
|
|
|
StringRef ClassLockGuard, ClassUniqueLock;
|
|
|
|
|
|
|
|
mutable bool IdentifierInfoInitialized;
|
|
|
|
|
2016-09-21 04:28:50 +08:00
|
|
|
std::unique_ptr<BugType> BlockInCritSectionBugType;
|
|
|
|
|
2017-10-30 18:09:55 +08:00
|
|
|
void initIdentifierInfo(ASTContext &Ctx) const;
|
|
|
|
|
2016-09-21 04:28:50 +08:00
|
|
|
void reportBlockInCritSection(SymbolRef FileDescSym,
|
|
|
|
const CallEvent &call,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlockInCriticalSectionChecker();
|
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
bool isBlockingFunction(const CallEvent &Call) const;
|
|
|
|
bool isLockFunction(const CallEvent &Call) const;
|
|
|
|
bool isUnlockFunction(const CallEvent &Call) const;
|
|
|
|
|
2016-09-21 04:28:50 +08:00
|
|
|
/// Process unlock.
|
|
|
|
/// Process lock.
|
|
|
|
/// Process blocking functions (sleep, getc, fgets, read, recv)
|
|
|
|
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
REGISTER_TRAIT_WITH_PROGRAMSTATE(MutexCounter, unsigned)
|
|
|
|
|
|
|
|
BlockInCriticalSectionChecker::BlockInCriticalSectionChecker()
|
2017-10-30 18:09:55 +08:00
|
|
|
: IILockGuard(nullptr), IIUniqueLock(nullptr),
|
|
|
|
LockFn("lock"), UnlockFn("unlock"), SleepFn("sleep"), GetcFn("getc"),
|
2017-03-10 22:50:12 +08:00
|
|
|
FgetsFn("fgets"), ReadFn("read"), RecvFn("recv"),
|
|
|
|
PthreadLockFn("pthread_mutex_lock"),
|
|
|
|
PthreadTryLockFn("pthread_mutex_trylock"),
|
|
|
|
PthreadUnlockFn("pthread_mutex_unlock"),
|
|
|
|
MtxLock("mtx_lock"),
|
|
|
|
MtxTimedLock("mtx_timedlock"),
|
|
|
|
MtxTryLock("mtx_trylock"),
|
2017-10-30 18:09:55 +08:00
|
|
|
MtxUnlock("mtx_unlock"),
|
|
|
|
ClassLockGuard("lock_guard"),
|
|
|
|
ClassUniqueLock("unique_lock"),
|
|
|
|
IdentifierInfoInitialized(false) {
|
2016-09-21 04:28:50 +08:00
|
|
|
// Initialize the bug type.
|
|
|
|
BlockInCritSectionBugType.reset(
|
|
|
|
new BugType(this, "Call to blocking function in critical section",
|
|
|
|
"Blocking Error"));
|
|
|
|
}
|
|
|
|
|
2017-10-30 18:09:55 +08:00
|
|
|
void BlockInCriticalSectionChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
|
|
|
if (!IdentifierInfoInitialized) {
|
|
|
|
/* In case of checking C code, or when the corresponding headers are not
|
|
|
|
* included, we might end up query the identifier table every time when this
|
|
|
|
* function is called instead of early returning it. To avoid this, a bool
|
|
|
|
* variable (IdentifierInfoInitialized) is used and the function will be run
|
|
|
|
* only once. */
|
|
|
|
IILockGuard = &Ctx.Idents.get(ClassLockGuard);
|
|
|
|
IIUniqueLock = &Ctx.Idents.get(ClassUniqueLock);
|
|
|
|
IdentifierInfoInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
bool BlockInCriticalSectionChecker::isBlockingFunction(const CallEvent &Call) const {
|
|
|
|
if (Call.isCalled(SleepFn)
|
|
|
|
|| Call.isCalled(GetcFn)
|
|
|
|
|| Call.isCalled(FgetsFn)
|
|
|
|
|| Call.isCalled(ReadFn)
|
|
|
|
|| Call.isCalled(RecvFn)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockInCriticalSectionChecker::isLockFunction(const CallEvent &Call) const {
|
2017-10-30 18:09:55 +08:00
|
|
|
if (const auto *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
|
|
|
|
auto IdentifierInfo = Ctor->getDecl()->getParent()->getIdentifier();
|
|
|
|
if (IdentifierInfo == IILockGuard || IdentifierInfo == IIUniqueLock)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
if (Call.isCalled(LockFn)
|
|
|
|
|| Call.isCalled(PthreadLockFn)
|
|
|
|
|| Call.isCalled(PthreadTryLockFn)
|
|
|
|
|| Call.isCalled(MtxLock)
|
|
|
|
|| Call.isCalled(MtxTimedLock)
|
|
|
|
|| Call.isCalled(MtxTryLock)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockInCriticalSectionChecker::isUnlockFunction(const CallEvent &Call) const {
|
2017-10-30 18:09:55 +08:00
|
|
|
if (const auto *Dtor = dyn_cast<CXXDestructorCall>(&Call)) {
|
2019-10-13 19:30:06 +08:00
|
|
|
const auto *DRecordDecl = cast<CXXRecordDecl>(Dtor->getDecl()->getParent());
|
2017-10-30 18:09:55 +08:00
|
|
|
auto IdentifierInfo = DRecordDecl->getIdentifier();
|
|
|
|
if (IdentifierInfo == IILockGuard || IdentifierInfo == IIUniqueLock)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
if (Call.isCalled(UnlockFn)
|
|
|
|
|| Call.isCalled(PthreadUnlockFn)
|
|
|
|
|| Call.isCalled(MtxUnlock)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-21 04:28:50 +08:00
|
|
|
void BlockInCriticalSectionChecker::checkPostCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2017-10-30 18:09:55 +08:00
|
|
|
initIdentifierInfo(C.getASTContext());
|
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
if (!isBlockingFunction(Call)
|
|
|
|
&& !isLockFunction(Call)
|
|
|
|
&& !isUnlockFunction(Call))
|
2016-09-21 04:28:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
unsigned mutexCount = State->get<MutexCounter>();
|
2017-03-10 22:50:12 +08:00
|
|
|
if (isUnlockFunction(Call) && mutexCount > 0) {
|
2016-09-21 04:28:50 +08:00
|
|
|
State = State->set<MutexCounter>(--mutexCount);
|
|
|
|
C.addTransition(State);
|
2017-03-10 22:50:12 +08:00
|
|
|
} else if (isLockFunction(Call)) {
|
2016-09-21 04:28:50 +08:00
|
|
|
State = State->set<MutexCounter>(++mutexCount);
|
|
|
|
C.addTransition(State);
|
|
|
|
} else if (mutexCount > 0) {
|
|
|
|
SymbolRef BlockDesc = Call.getReturnValue().getAsSymbol();
|
|
|
|
reportBlockInCritSection(BlockDesc, Call, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockInCriticalSectionChecker::reportBlockInCritSection(
|
|
|
|
SymbolRef BlockDescSym, const CallEvent &Call, CheckerContext &C) const {
|
|
|
|
ExplodedNode *ErrNode = C.generateNonFatalErrorNode();
|
|
|
|
if (!ErrNode)
|
|
|
|
return;
|
|
|
|
|
2017-03-10 22:50:12 +08:00
|
|
|
std::string msg;
|
|
|
|
llvm::raw_string_ostream os(msg);
|
|
|
|
os << "Call to blocking function '" << Call.getCalleeIdentifier()->getName()
|
|
|
|
<< "' inside of critical section";
|
2019-09-10 04:34:40 +08:00
|
|
|
auto R = std::make_unique<PathSensitiveBugReport>(*BlockInCritSectionBugType,
|
|
|
|
os.str(), ErrNode);
|
2016-09-21 04:28:50 +08:00
|
|
|
R->addRange(Call.getSourceRange());
|
|
|
|
R->markInteresting(BlockDescSym);
|
|
|
|
C.emitReport(std::move(R));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerBlockInCriticalSectionChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<BlockInCriticalSectionChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterBlockInCriticalSectionChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|