2018-03-13 02:27:36 +08:00
|
|
|
//===- GCDAntipatternChecker.cpp ---------------------------------*- C++ -*-==//
|
2018-03-06 06:03:32 +08:00
|
|
|
//
|
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
|
2018-03-06 06:03:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-03-13 02:27:36 +08:00
|
|
|
// This file defines GCDAntipatternChecker which checks against a common
|
2018-03-06 06:03:32 +08:00
|
|
|
// antipattern when synchronous API is emulated from asynchronous callbacks
|
2018-03-13 02:27:36 +08:00
|
|
|
// using a semaphore:
|
|
|
|
//
|
|
|
|
// dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
2018-03-06 06:03:32 +08:00
|
|
|
//
|
|
|
|
// AnyCFunctionCall(^{
|
|
|
|
// // code…
|
2018-03-13 02:27:36 +08:00
|
|
|
// dispatch_semaphore_signal(sema);
|
2018-03-06 06:03:32 +08:00
|
|
|
// })
|
2018-03-13 02:27:36 +08:00
|
|
|
// dispatch_semaphore_wait(sema, *)
|
2018-03-06 06:03:32 +08:00
|
|
|
//
|
|
|
|
// Such code is a common performance problem, due to inability of GCD to
|
2018-03-13 02:27:36 +08:00
|
|
|
// properly handle QoS when a combination of queues and semaphores is used.
|
2018-03-06 06:03:32 +08:00
|
|
|
// Good code would either use asynchronous API (when available), or perform
|
|
|
|
// the necessary action in asynchronous callback.
|
|
|
|
//
|
|
|
|
// Currently, the check is performed using a simple heuristical AST pattern
|
|
|
|
// matching.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2018-03-06 06:03:32 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
using namespace ast_matchers;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
// ID of a node at which the diagnostic would be emitted.
|
|
|
|
const char *WarnAtNode = "waitcall";
|
2018-03-06 06:03:32 +08:00
|
|
|
|
2018-03-13 02:27:36 +08:00
|
|
|
class GCDAntipatternChecker : public Checker<check::ASTCodeBody> {
|
2018-03-06 06:03:32 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D,
|
|
|
|
AnalysisManager &AM,
|
|
|
|
BugReporter &BR) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto callsName(const char *FunctionName)
|
|
|
|
-> decltype(callee(functionDecl())) {
|
|
|
|
return callee(functionDecl(hasName(FunctionName)));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto equalsBoundArgDecl(int ArgIdx, const char *DeclName)
|
|
|
|
-> decltype(hasArgument(0, expr())) {
|
|
|
|
return hasArgument(ArgIdx, ignoringParenCasts(declRefExpr(
|
|
|
|
to(varDecl(equalsBoundNode(DeclName))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto bindAssignmentToDecl(const char *DeclName) -> decltype(hasLHS(expr())) {
|
|
|
|
return hasLHS(ignoringParenImpCasts(
|
|
|
|
declRefExpr(to(varDecl().bind(DeclName)))));
|
|
|
|
}
|
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
/// The pattern is very common in tests, and it is OK to use it there.
|
|
|
|
/// We have to heuristics for detecting tests: method name starts with "test"
|
|
|
|
/// (used in XCTest), and a class name contains "mock" or "test" (used in
|
|
|
|
/// helpers which are not tests themselves, but used exclusively in tests).
|
|
|
|
static bool isTest(const Decl *D) {
|
2018-03-06 08:18:21 +08:00
|
|
|
if (const auto* ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
std::string DeclName = ND->getNameAsString();
|
|
|
|
if (StringRef(DeclName).startswith("test"))
|
2018-03-23 08:16:02 +08:00
|
|
|
return true;
|
2018-03-06 08:18:21 +08:00
|
|
|
}
|
2018-03-13 02:27:36 +08:00
|
|
|
if (const auto *OD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
if (const auto *CD = dyn_cast<ObjCContainerDecl>(OD->getParent())) {
|
|
|
|
std::string ContainerName = CD->getNameAsString();
|
|
|
|
StringRef CN(ContainerName);
|
|
|
|
if (CN.contains_lower("test") || CN.contains_lower("mock"))
|
2018-03-23 08:16:02 +08:00
|
|
|
return true;
|
2018-03-13 02:27:36 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-23 08:16:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto findGCDAntiPatternWithSemaphore() -> decltype(compoundStmt()) {
|
2018-03-06 06:03:32 +08:00
|
|
|
|
|
|
|
const char *SemaphoreBinding = "semaphore_name";
|
2018-07-17 04:32:57 +08:00
|
|
|
auto SemaphoreCreateM = callExpr(allOf(
|
|
|
|
callsName("dispatch_semaphore_create"),
|
|
|
|
hasArgument(0, ignoringParenCasts(integerLiteral(equals(0))))));
|
2018-03-06 06:03:32 +08:00
|
|
|
|
|
|
|
auto SemaphoreBindingM = anyOf(
|
|
|
|
forEachDescendant(
|
|
|
|
varDecl(hasDescendant(SemaphoreCreateM)).bind(SemaphoreBinding)),
|
|
|
|
forEachDescendant(binaryOperator(bindAssignmentToDecl(SemaphoreBinding),
|
|
|
|
hasRHS(SemaphoreCreateM))));
|
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
auto HasBlockArgumentM = hasAnyArgument(hasType(
|
|
|
|
hasCanonicalType(blockPointerType())
|
|
|
|
));
|
|
|
|
|
|
|
|
auto ArgCallsSignalM = hasAnyArgument(stmt(hasDescendant(callExpr(
|
|
|
|
allOf(
|
|
|
|
callsName("dispatch_semaphore_signal"),
|
|
|
|
equalsBoundArgDecl(0, SemaphoreBinding)
|
|
|
|
)))));
|
|
|
|
|
|
|
|
auto HasBlockAndCallsSignalM = allOf(HasBlockArgumentM, ArgCallsSignalM);
|
|
|
|
|
|
|
|
auto HasBlockCallingSignalM =
|
|
|
|
forEachDescendant(
|
|
|
|
stmt(anyOf(
|
|
|
|
callExpr(HasBlockAndCallsSignalM),
|
|
|
|
objcMessageExpr(HasBlockAndCallsSignalM)
|
|
|
|
)));
|
|
|
|
|
2018-03-06 06:03:32 +08:00
|
|
|
auto SemaphoreWaitM = forEachDescendant(
|
|
|
|
callExpr(
|
|
|
|
allOf(
|
|
|
|
callsName("dispatch_semaphore_wait"),
|
|
|
|
equalsBoundArgDecl(0, SemaphoreBinding)
|
|
|
|
)
|
2018-03-23 08:16:02 +08:00
|
|
|
).bind(WarnAtNode));
|
|
|
|
|
|
|
|
return compoundStmt(
|
|
|
|
SemaphoreBindingM, HasBlockCallingSignalM, SemaphoreWaitM);
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto findGCDAntiPatternWithGroup() -> decltype(compoundStmt()) {
|
|
|
|
|
|
|
|
const char *GroupBinding = "group_name";
|
|
|
|
auto DispatchGroupCreateM = callExpr(callsName("dispatch_group_create"));
|
|
|
|
|
|
|
|
auto GroupBindingM = anyOf(
|
|
|
|
forEachDescendant(
|
|
|
|
varDecl(hasDescendant(DispatchGroupCreateM)).bind(GroupBinding)),
|
|
|
|
forEachDescendant(binaryOperator(bindAssignmentToDecl(GroupBinding),
|
|
|
|
hasRHS(DispatchGroupCreateM))));
|
|
|
|
|
|
|
|
auto GroupEnterM = forEachDescendant(
|
|
|
|
stmt(callExpr(allOf(callsName("dispatch_group_enter"),
|
|
|
|
equalsBoundArgDecl(0, GroupBinding)))));
|
2018-03-06 06:03:32 +08:00
|
|
|
|
2018-03-07 10:54:01 +08:00
|
|
|
auto HasBlockArgumentM = hasAnyArgument(hasType(
|
2018-03-06 06:03:32 +08:00
|
|
|
hasCanonicalType(blockPointerType())
|
2018-03-07 10:54:01 +08:00
|
|
|
));
|
2018-03-06 06:03:32 +08:00
|
|
|
|
2018-03-14 01:27:01 +08:00
|
|
|
auto ArgCallsSignalM = hasAnyArgument(stmt(hasDescendant(callExpr(
|
2018-03-06 06:03:32 +08:00
|
|
|
allOf(
|
2018-03-23 08:16:02 +08:00
|
|
|
callsName("dispatch_group_leave"),
|
|
|
|
equalsBoundArgDecl(0, GroupBinding)
|
2018-03-14 01:27:01 +08:00
|
|
|
)))));
|
2018-03-07 10:54:01 +08:00
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
auto HasBlockAndCallsLeaveM = allOf(HasBlockArgumentM, ArgCallsSignalM);
|
2018-03-07 10:54:01 +08:00
|
|
|
|
|
|
|
auto AcceptsBlockM =
|
|
|
|
forEachDescendant(
|
|
|
|
stmt(anyOf(
|
2018-03-23 08:16:02 +08:00
|
|
|
callExpr(HasBlockAndCallsLeaveM),
|
|
|
|
objcMessageExpr(HasBlockAndCallsLeaveM)
|
2018-03-07 10:54:01 +08:00
|
|
|
)));
|
2018-03-06 06:03:32 +08:00
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
auto GroupWaitM = forEachDescendant(
|
|
|
|
callExpr(
|
|
|
|
allOf(
|
|
|
|
callsName("dispatch_group_wait"),
|
|
|
|
equalsBoundArgDecl(0, GroupBinding)
|
|
|
|
)
|
|
|
|
).bind(WarnAtNode));
|
2018-03-06 06:03:32 +08:00
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
return compoundStmt(GroupBindingM, GroupEnterM, AcceptsBlockM, GroupWaitM);
|
2018-03-06 06:03:32 +08:00
|
|
|
}
|
|
|
|
|
2018-03-23 08:16:02 +08:00
|
|
|
static void emitDiagnostics(const BoundNodes &Nodes,
|
|
|
|
const char* Type,
|
|
|
|
BugReporter &BR,
|
|
|
|
AnalysisDeclContext *ADC,
|
|
|
|
const GCDAntipatternChecker *Checker) {
|
|
|
|
const auto *SW = Nodes.getNodeAs<CallExpr>(WarnAtNode);
|
2018-03-06 06:03:32 +08:00
|
|
|
assert(SW);
|
2018-03-23 08:16:02 +08:00
|
|
|
|
|
|
|
std::string Diagnostics;
|
|
|
|
llvm::raw_string_ostream OS(Diagnostics);
|
2018-05-17 06:46:47 +08:00
|
|
|
OS << "Waiting on a callback using a " << Type << " creates useless threads "
|
|
|
|
<< "and is subject to priority inversion; consider "
|
2018-03-23 08:16:02 +08:00
|
|
|
<< "using a synchronous API or changing the caller to be asynchronous";
|
|
|
|
|
2018-03-06 06:03:32 +08:00
|
|
|
BR.EmitBasicReport(
|
2018-03-23 08:16:02 +08:00
|
|
|
ADC->getDecl(),
|
|
|
|
Checker,
|
|
|
|
/*Name=*/"GCD performance anti-pattern",
|
|
|
|
/*Category=*/"Performance",
|
|
|
|
OS.str(),
|
|
|
|
PathDiagnosticLocation::createBegin(SW, BR.getSourceManager(), ADC),
|
|
|
|
SW->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GCDAntipatternChecker::checkASTCodeBody(const Decl *D,
|
|
|
|
AnalysisManager &AM,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
if (isTest(D))
|
|
|
|
return;
|
|
|
|
|
|
|
|
AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
|
|
|
|
|
|
|
|
auto SemaphoreMatcherM = findGCDAntiPatternWithSemaphore();
|
|
|
|
auto Matches = match(SemaphoreMatcherM, *D->getBody(), AM.getASTContext());
|
|
|
|
for (BoundNodes Match : Matches)
|
|
|
|
emitDiagnostics(Match, "semaphore", BR, ADC, this);
|
|
|
|
|
|
|
|
auto GroupMatcherM = findGCDAntiPatternWithGroup();
|
|
|
|
Matches = match(GroupMatcherM, *D->getBody(), AM.getASTContext());
|
|
|
|
for (BoundNodes Match : Matches)
|
|
|
|
emitDiagnostics(Match, "group", BR, ADC, this);
|
2018-03-06 06:03:32 +08:00
|
|
|
}
|
|
|
|
|
2019-01-26 22:23:08 +08:00
|
|
|
} // end of anonymous namespace
|
2018-03-06 06:03:32 +08:00
|
|
|
|
2018-03-13 02:27:36 +08:00
|
|
|
void ento::registerGCDAntipattern(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<GCDAntipatternChecker>();
|
2018-03-06 06:03:32 +08:00
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterGCDAntipattern(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|