2012-01-04 07:18:57 +08:00
|
|
|
//=======- VirtualCallChecker.cpp --------------------------------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-09-08 11:50:52 +08:00
|
|
|
// This file defines a checker that checks virtual function calls during
|
2012-01-04 07:18:57 +08:00
|
|
|
// construction or destruction of C++ objects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ClangSACheckers.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-01-04 07:18:57 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WalkAST : public StmtVisitor<WalkAST> {
|
2014-02-12 05:49:21 +08:00
|
|
|
const CheckerBase *Checker;
|
2012-01-04 07:18:57 +08:00
|
|
|
BugReporter &BR;
|
|
|
|
AnalysisDeclContext *AC;
|
|
|
|
|
2016-12-10 09:16:09 +08:00
|
|
|
/// The root constructor or destructor whose callees are being analyzed.
|
|
|
|
const CXXMethodDecl *RootMethod = nullptr;
|
|
|
|
|
|
|
|
/// Whether the checker should walk into bodies of called functions.
|
|
|
|
/// Controlled by the "Interprocedural" analyzer-config option.
|
|
|
|
bool IsInterprocedural = false;
|
|
|
|
|
|
|
|
/// Whether the checker should only warn for calls to pure virtual functions
|
|
|
|
/// (which is undefined behavior) or for all virtual functions (which may
|
|
|
|
/// may result in unexpected behavior).
|
|
|
|
bool ReportPureOnly = false;
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
typedef const CallExpr * WorkListUnit;
|
|
|
|
typedef SmallVector<WorkListUnit, 20> DFSWorkList;
|
|
|
|
|
|
|
|
/// A vector representing the worklist which has a chain of CallExprs.
|
|
|
|
DFSWorkList WList;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
// PreVisited : A CallExpr to this FunctionDecl is in the worklist, but the
|
|
|
|
// body has not been visited yet.
|
|
|
|
// PostVisited : A CallExpr to this FunctionDecl is in the worklist, and the
|
|
|
|
// body has been visited.
|
|
|
|
enum Kind { NotVisited,
|
2015-09-08 11:50:52 +08:00
|
|
|
PreVisited, /**< A CallExpr to this FunctionDecl is in the
|
2012-01-04 07:18:57 +08:00
|
|
|
worklist, but the body has not yet been
|
|
|
|
visited. */
|
|
|
|
PostVisited /**< A CallExpr to this FunctionDecl is in the
|
|
|
|
worklist, and the body has been visited. */
|
2012-06-07 01:32:50 +08:00
|
|
|
};
|
2012-01-04 07:18:57 +08:00
|
|
|
|
|
|
|
/// A DenseMap that records visited states of FunctionDecls.
|
|
|
|
llvm::DenseMap<const FunctionDecl *, Kind> VisitedFunctions;
|
|
|
|
|
|
|
|
/// The CallExpr whose body is currently being visited. This is used for
|
|
|
|
/// generating bug reports. This is null while visiting the body of a
|
|
|
|
/// constructor or destructor.
|
|
|
|
const CallExpr *visitingCallExpr;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
public:
|
2016-12-10 09:16:09 +08:00
|
|
|
WalkAST(const CheckerBase *checker, BugReporter &br, AnalysisDeclContext *ac,
|
|
|
|
const CXXMethodDecl *rootMethod, bool isInterprocedural,
|
|
|
|
bool reportPureOnly)
|
|
|
|
: Checker(checker), BR(br), AC(ac), RootMethod(rootMethod),
|
|
|
|
IsInterprocedural(isInterprocedural), ReportPureOnly(reportPureOnly),
|
|
|
|
visitingCallExpr(nullptr) {
|
|
|
|
// Walking should always start from either a constructor or a destructor.
|
|
|
|
assert(isa<CXXConstructorDecl>(rootMethod) ||
|
|
|
|
isa<CXXDestructorDecl>(rootMethod));
|
|
|
|
}
|
2014-02-12 05:49:21 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
bool hasWork() const { return !WList.empty(); }
|
|
|
|
|
|
|
|
/// This method adds a CallExpr to the worklist and marks the callee as
|
|
|
|
/// being PreVisited.
|
|
|
|
void Enqueue(WorkListUnit WLUnit) {
|
|
|
|
const FunctionDecl *FD = WLUnit->getDirectCallee();
|
|
|
|
if (!FD || !FD->getBody())
|
2015-09-08 11:50:52 +08:00
|
|
|
return;
|
2012-01-04 07:18:57 +08:00
|
|
|
Kind &K = VisitedFunctions[FD];
|
|
|
|
if (K != NotVisited)
|
|
|
|
return;
|
|
|
|
K = PreVisited;
|
|
|
|
WList.push_back(WLUnit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method returns an item from the worklist without removing it.
|
|
|
|
WorkListUnit Dequeue() {
|
|
|
|
assert(!WList.empty());
|
2015-09-08 11:50:52 +08:00
|
|
|
return WList.back();
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
void Execute() {
|
|
|
|
while (hasWork()) {
|
|
|
|
WorkListUnit WLUnit = Dequeue();
|
|
|
|
const FunctionDecl *FD = WLUnit->getDirectCallee();
|
|
|
|
assert(FD && FD->getBody());
|
|
|
|
|
|
|
|
if (VisitedFunctions[FD] == PreVisited) {
|
|
|
|
// If the callee is PreVisited, walk its body.
|
|
|
|
// Visit the body.
|
|
|
|
SaveAndRestore<const CallExpr *> SaveCall(visitingCallExpr, WLUnit);
|
|
|
|
Visit(FD->getBody());
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
// Mark the function as being PostVisited to indicate we have
|
|
|
|
// scanned the body.
|
|
|
|
VisitedFunctions[FD] = PostVisited;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, the callee is PostVisited.
|
|
|
|
// Remove it from the worklist.
|
|
|
|
assert(VisitedFunctions[FD] == PostVisited);
|
|
|
|
WList.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stmt visitor methods.
|
|
|
|
void VisitCallExpr(CallExpr *CE);
|
|
|
|
void VisitCXXMemberCallExpr(CallExpr *CE);
|
|
|
|
void VisitStmt(Stmt *S) { VisitChildren(S); }
|
|
|
|
void VisitChildren(Stmt *S);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
void ReportVirtualCall(const CallExpr *CE, bool isPure);
|
|
|
|
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AST walking.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WalkAST::VisitChildren(Stmt *S) {
|
2015-07-03 23:12:24 +08:00
|
|
|
for (Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
Visit(Child);
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WalkAST::VisitCallExpr(CallExpr *CE) {
|
|
|
|
VisitChildren(CE);
|
2016-12-10 09:16:09 +08:00
|
|
|
if (IsInterprocedural)
|
|
|
|
Enqueue(CE);
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WalkAST::VisitCXXMemberCallExpr(CallExpr *CE) {
|
|
|
|
VisitChildren(CE);
|
|
|
|
bool callIsNonVirtual = false;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
// Several situations to elide for checking.
|
|
|
|
if (MemberExpr *CME = dyn_cast<MemberExpr>(CE->getCallee())) {
|
|
|
|
// If the member access is fully qualified (i.e., X::F), then treat
|
|
|
|
// this as a non-virtual call and do not warn.
|
|
|
|
if (CME->getQualifier())
|
|
|
|
callIsNonVirtual = true;
|
|
|
|
|
2014-08-21 18:25:03 +08:00
|
|
|
if (Expr *base = CME->getBase()->IgnoreImpCasts()) {
|
|
|
|
// Elide analyzing the call entirely if the base pointer is not 'this'.
|
2012-01-04 07:18:57 +08:00
|
|
|
if (!isa<CXXThisExpr>(base))
|
|
|
|
return;
|
2014-08-21 18:25:03 +08:00
|
|
|
|
|
|
|
// If the most derived class is marked final, we know that now subclass
|
|
|
|
// can override this member.
|
|
|
|
if (base->getBestDynamicClassType()->hasAttr<FinalAttr>())
|
|
|
|
callIsNonVirtual = true;
|
|
|
|
}
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the callee.
|
|
|
|
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CE->getDirectCallee());
|
2014-08-21 18:25:03 +08:00
|
|
|
if (MD && MD->isVirtual() && !callIsNonVirtual && !MD->hasAttr<FinalAttr>() &&
|
|
|
|
!MD->getParent()->hasAttr<FinalAttr>())
|
2012-01-04 07:18:57 +08:00
|
|
|
ReportVirtualCall(CE, MD->isPure());
|
|
|
|
|
2016-12-10 09:16:09 +08:00
|
|
|
if (IsInterprocedural)
|
|
|
|
Enqueue(CE);
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WalkAST::ReportVirtualCall(const CallExpr *CE, bool isPure) {
|
2016-12-10 09:16:09 +08:00
|
|
|
if (ReportPureOnly && !isPure)
|
|
|
|
return;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<100> buf;
|
2012-01-04 07:18:57 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2016-12-10 09:16:09 +08:00
|
|
|
// FIXME: The interprocedural diagnostic experience here is not good.
|
|
|
|
// Ultimately this checker should be re-written to be path sensitive.
|
|
|
|
// For now, only diagnose intraprocedurally, by default.
|
|
|
|
if (IsInterprocedural) {
|
|
|
|
os << "Call Path : ";
|
|
|
|
// Name of current visiting CallExpr.
|
|
|
|
os << *CE->getDirectCallee();
|
|
|
|
|
|
|
|
// Name of the CallExpr whose body is current being walked.
|
|
|
|
if (visitingCallExpr)
|
|
|
|
os << " <-- " << *visitingCallExpr->getDirectCallee();
|
|
|
|
// Names of FunctionDecls in worklist with state PostVisited.
|
|
|
|
for (SmallVectorImpl<const CallExpr *>::iterator I = WList.end(),
|
2012-01-04 07:18:57 +08:00
|
|
|
E = WList.begin(); I != E; --I) {
|
2016-12-10 09:16:09 +08:00
|
|
|
const FunctionDecl *FD = (*(I-1))->getDirectCallee();
|
|
|
|
assert(FD);
|
|
|
|
if (VisitedFunctions[FD] == PostVisited)
|
|
|
|
os << " <-- " << *FD;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "\n";
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticLocation CELoc =
|
|
|
|
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
|
|
|
|
SourceRange R = CE->getCallee()->getSourceRange();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2016-12-10 09:16:09 +08:00
|
|
|
os << "Call to ";
|
|
|
|
if (isPure)
|
|
|
|
os << "pure ";
|
|
|
|
|
|
|
|
os << "virtual function during ";
|
|
|
|
|
|
|
|
if (isa<CXXConstructorDecl>(RootMethod))
|
|
|
|
os << "construction ";
|
|
|
|
else
|
|
|
|
os << "destruction ";
|
|
|
|
|
|
|
|
if (isPure)
|
|
|
|
os << "has undefined behavior";
|
|
|
|
else
|
|
|
|
os << "will not dispatch to derived class";
|
|
|
|
|
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker,
|
|
|
|
"Call to virtual function during construction or "
|
|
|
|
"destruction",
|
|
|
|
"C++ Object Lifecycle", os.str(), CELoc, R);
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VirtualCallChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VirtualCallChecker : public Checker<check::ASTDecl<CXXRecordDecl> > {
|
|
|
|
public:
|
2016-12-10 09:16:09 +08:00
|
|
|
DefaultBool isInterprocedural;
|
|
|
|
DefaultBool isPureOnly;
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
void checkASTDecl(const CXXRecordDecl *RD, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
2016-12-10 09:16:09 +08:00
|
|
|
AnalysisDeclContext *ADC = mgr.getAnalysisDeclContext(RD);
|
2012-01-04 07:18:57 +08:00
|
|
|
|
|
|
|
// Check the constructors.
|
2014-03-14 00:51:27 +08:00
|
|
|
for (const auto *I : RD->ctors()) {
|
2012-01-04 07:18:57 +08:00
|
|
|
if (!I->isCopyOrMoveConstructor())
|
|
|
|
if (Stmt *Body = I->getBody()) {
|
2016-12-10 09:16:09 +08:00
|
|
|
WalkAST walker(this, BR, ADC, I, isInterprocedural, isPureOnly);
|
2012-01-04 07:18:57 +08:00
|
|
|
walker.Visit(Body);
|
|
|
|
walker.Execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the destructor.
|
|
|
|
if (CXXDestructorDecl *DD = RD->getDestructor())
|
|
|
|
if (Stmt *Body = DD->getBody()) {
|
2016-12-10 09:16:09 +08:00
|
|
|
WalkAST walker(this, BR, ADC, DD, isInterprocedural, isPureOnly);
|
2012-01-04 07:18:57 +08:00
|
|
|
walker.Visit(Body);
|
|
|
|
walker.Execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-01-04 07:18:57 +08:00
|
|
|
|
|
|
|
void ento::registerVirtualCallChecker(CheckerManager &mgr) {
|
2016-12-10 09:16:09 +08:00
|
|
|
VirtualCallChecker *checker = mgr.registerChecker<VirtualCallChecker>();
|
|
|
|
checker->isInterprocedural =
|
|
|
|
mgr.getAnalyzerOptions().getBooleanOption("Interprocedural", false,
|
|
|
|
checker);
|
|
|
|
|
|
|
|
checker->isPureOnly =
|
|
|
|
mgr.getAnalyzerOptions().getBooleanOption("PureOnly", false,
|
|
|
|
checker);
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|