2011-08-20 13:59:58 +08:00
|
|
|
//===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
|
2010-04-19 20:51:02 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C++ expression evaluation engine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2010-04-19 20:51:02 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-03-10 09:34:17 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2012-08-04 07:31:15 +08:00
|
|
|
#include "clang/Basic/PrettyStackTrace.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2010-04-19 20:51:02 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2011-07-29 07:07:36 +08:00
|
|
|
void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
2012-08-22 14:26:15 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2011-10-02 08:54:48 +08:00
|
|
|
const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2013-07-26 06:32:35 +08:00
|
|
|
state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME);
|
2013-02-22 09:51:15 +08:00
|
|
|
Bldr.generateNode(ME, Pred, state);
|
2010-04-19 20:51:02 +08:00
|
|
|
}
|
|
|
|
|
2013-03-16 10:14:06 +08:00
|
|
|
// FIXME: This is the sort of code that should eventually live in a Core
|
|
|
|
// checker rather than as a special case in ExprEngine.
|
2013-02-15 08:32:15 +08:00
|
|
|
void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
|
2013-03-16 10:14:06 +08:00
|
|
|
const CallEvent &Call) {
|
|
|
|
SVal ThisVal;
|
|
|
|
bool AlwaysReturnsLValue;
|
|
|
|
if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
|
|
|
|
assert(Ctor->getDecl()->isTrivial());
|
|
|
|
assert(Ctor->getDecl()->isCopyOrMoveConstructor());
|
|
|
|
ThisVal = Ctor->getCXXThisVal();
|
|
|
|
AlwaysReturnsLValue = false;
|
|
|
|
} else {
|
|
|
|
assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
|
|
|
|
assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
|
|
|
|
OO_Equal);
|
|
|
|
ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
|
|
|
|
AlwaysReturnsLValue = true;
|
|
|
|
}
|
2013-02-15 08:32:15 +08:00
|
|
|
|
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
|
|
|
|
ExplodedNodeSet Dst;
|
|
|
|
Bldr.takeNodes(Pred);
|
|
|
|
|
|
|
|
SVal V = Call.getArgSVal(0);
|
|
|
|
|
2013-03-16 10:14:06 +08:00
|
|
|
// If the value being copied is not unknown, load from its location to get
|
|
|
|
// an aggregate rvalue.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<Loc> L = V.getAs<Loc>())
|
2013-02-15 08:32:15 +08:00
|
|
|
V = Pred->getState()->getSVal(*L);
|
2013-03-16 10:14:06 +08:00
|
|
|
else
|
|
|
|
assert(V.isUnknown());
|
2013-02-15 08:32:15 +08:00
|
|
|
|
2013-03-16 10:14:06 +08:00
|
|
|
const Expr *CallExpr = Call.getOriginExpr();
|
|
|
|
evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
|
2013-02-15 08:32:15 +08:00
|
|
|
|
2013-03-16 10:14:06 +08:00
|
|
|
PostStmt PS(CallExpr, LCtx);
|
2013-02-15 08:32:15 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ProgramStateRef State = (*I)->getState();
|
2013-03-16 10:14:06 +08:00
|
|
|
if (AlwaysReturnsLValue)
|
|
|
|
State = State->BindExpr(CallExpr, LCtx, ThisVal);
|
|
|
|
else
|
|
|
|
State = bindReturnValue(Call, LCtx, State);
|
2013-02-15 08:32:15 +08:00
|
|
|
Bldr.generateNode(PS, State, *I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 09:39:08 +08:00
|
|
|
|
|
|
|
/// Returns a region representing the first element of a (possibly
|
|
|
|
/// multi-dimensional) array.
|
|
|
|
///
|
|
|
|
/// On return, \p Ty will be set to the base type of the array.
|
|
|
|
///
|
|
|
|
/// If the type is not an array type at all, the original value is returned.
|
|
|
|
static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue,
|
|
|
|
QualType &Ty) {
|
|
|
|
SValBuilder &SVB = State->getStateManager().getSValBuilder();
|
|
|
|
ASTContext &Ctx = SVB.getContext();
|
|
|
|
|
|
|
|
while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
|
|
|
|
Ty = AT->getElementType();
|
|
|
|
LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return LValue;
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:40:06 +08:00
|
|
|
|
2015-12-17 08:28:33 +08:00
|
|
|
const MemRegion *
|
|
|
|
ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
|
|
|
|
ExplodedNode *Pred) {
|
2014-04-02 00:40:06 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
|
|
|
// See if we're constructing an existing region by looking at the next
|
|
|
|
// element in the CFG.
|
|
|
|
|
2015-12-17 08:28:33 +08:00
|
|
|
if (auto Elem = findElementDirectlyInitializedByCurrentConstructor()) {
|
|
|
|
if (Optional<CFGStmt> StmtElem = Elem->getAs<CFGStmt>()) {
|
|
|
|
auto *DS = cast<DeclStmt>(StmtElem->getStmt());
|
|
|
|
if (const auto *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) {
|
|
|
|
if (Var->getInit() && Var->getInit()->IgnoreImplicit() == CE) {
|
|
|
|
SVal LValue = State->getLValue(Var, LCtx);
|
|
|
|
QualType Ty = Var->getType();
|
|
|
|
LValue = makeZeroElementRegion(State, LValue, Ty);
|
|
|
|
return LValue.getAsRegion();
|
2014-04-02 00:40:06 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-17 08:28:33 +08:00
|
|
|
} else if (Optional<CFGInitializer> InitElem = Elem->getAs<CFGInitializer>()) {
|
2014-04-02 00:40:06 +08:00
|
|
|
const CXXCtorInitializer *Init = InitElem->getInitializer();
|
|
|
|
assert(Init->isAnyMemberInitializer());
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
2015-12-17 08:28:33 +08:00
|
|
|
Loc ThisPtr =
|
|
|
|
getSValBuilder().getCXXThis(CurCtor, LCtx->getCurrentStackFrame());
|
2014-04-02 00:40:06 +08:00
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
|
|
|
|
|
|
|
const ValueDecl *Field;
|
|
|
|
SVal FieldVal;
|
|
|
|
if (Init->isIndirectMemberInitializer()) {
|
|
|
|
Field = Init->getIndirectMember();
|
|
|
|
FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
|
|
|
|
} else {
|
|
|
|
Field = Init->getMember();
|
|
|
|
FieldVal = State->getLValue(Init->getMember(), ThisVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType Ty = Field->getType();
|
|
|
|
FieldVal = makeZeroElementRegion(State, FieldVal, Ty);
|
|
|
|
return FieldVal.getAsRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This will eventually need to handle new-expressions as well.
|
|
|
|
// Don't forget to update the pre-constructor initialization code in
|
|
|
|
// ExprEngine::VisitCXXConstructExpr.
|
|
|
|
}
|
|
|
|
// If we couldn't find an existing region to construct into, assume we're
|
|
|
|
// constructing a temporary.
|
2015-12-17 08:28:33 +08:00
|
|
|
MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
|
2014-04-02 00:40:06 +08:00
|
|
|
return MRMgr.getCXXTempObjectRegion(CE, LCtx);
|
|
|
|
}
|
|
|
|
|
2015-12-17 08:28:33 +08:00
|
|
|
/// Returns true if the initializer for \Elem can be a direct
|
|
|
|
/// constructor.
|
|
|
|
static bool canHaveDirectConstructor(CFGElement Elem){
|
|
|
|
// DeclStmts and CXXCtorInitializers for fields can be directly constructed.
|
|
|
|
|
|
|
|
if (Optional<CFGStmt> StmtElem = Elem.getAs<CFGStmt>()) {
|
|
|
|
if (isa<DeclStmt>(StmtElem->getStmt())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Elem.getKind() == CFGElement::Initializer) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<CFGElement>
|
|
|
|
ExprEngine::findElementDirectlyInitializedByCurrentConstructor() {
|
|
|
|
const NodeBuilderContext &CurrBldrCtx = getBuilderContext();
|
|
|
|
// See if we're constructing an existing region by looking at the next
|
|
|
|
// element in the CFG.
|
|
|
|
const CFGBlock *B = CurrBldrCtx.getBlock();
|
|
|
|
assert(isa<CXXConstructExpr>(((*B)[currStmtIdx]).castAs<CFGStmt>().getStmt()));
|
|
|
|
unsigned int NextStmtIdx = currStmtIdx + 1;
|
|
|
|
if (NextStmtIdx >= B->size())
|
|
|
|
return None;
|
|
|
|
|
|
|
|
CFGElement Next = (*B)[NextStmtIdx];
|
|
|
|
|
|
|
|
// Is this a destructor? If so, we might be in the middle of an assignment
|
|
|
|
// to a local or member: look ahead one more element to see what we find.
|
|
|
|
while (Next.getAs<CFGImplicitDtor>() && NextStmtIdx + 1 < B->size()) {
|
|
|
|
++NextStmtIdx;
|
|
|
|
Next = (*B)[NextStmtIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canHaveDirectConstructor(Next))
|
|
|
|
return Next;
|
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXConstructExpr *
|
|
|
|
ExprEngine::findDirectConstructorForCurrentCFGElement() {
|
|
|
|
// Go backward in the CFG to see if the previous element (ignoring
|
|
|
|
// destructors) was a CXXConstructExpr. If so, that constructor
|
|
|
|
// was constructed directly into an existing region.
|
|
|
|
// This process is essentially the inverse of that performed in
|
|
|
|
// findElementDirectlyInitializedByCurrentConstructor().
|
|
|
|
if (currStmtIdx == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const CFGBlock *B = getBuilderContext().getBlock();
|
|
|
|
assert(canHaveDirectConstructor((*B)[currStmtIdx]));
|
|
|
|
|
|
|
|
unsigned int PreviousStmtIdx = currStmtIdx - 1;
|
|
|
|
CFGElement Previous = (*B)[PreviousStmtIdx];
|
|
|
|
|
|
|
|
while (Previous.getAs<CFGImplicitDtor>() && PreviousStmtIdx > 0) {
|
|
|
|
--PreviousStmtIdx;
|
|
|
|
Previous = (*B)[PreviousStmtIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Optional<CFGStmt> PrevStmtElem = Previous.getAs<CFGStmt>()) {
|
|
|
|
if (auto *CtorExpr = dyn_cast<CXXConstructExpr>(PrevStmtElem->getStmt())) {
|
|
|
|
return CtorExpr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:12 +08:00
|
|
|
void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
|
2011-04-09 06:42:35 +08:00
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &destNodes) {
|
2012-07-27 04:04:13 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2012-07-27 04:04:16 +08:00
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *Target = nullptr;
|
2013-04-03 09:39:08 +08:00
|
|
|
|
|
|
|
// FIXME: Handle arrays, which run the same constructor for every element.
|
|
|
|
// For now, we just run the first constructor (which should still invalidate
|
|
|
|
// the entire array).
|
2012-07-27 04:04:13 +08:00
|
|
|
|
|
|
|
switch (CE->getConstructionKind()) {
|
|
|
|
case CXXConstructExpr::CK_Complete: {
|
2015-12-17 08:28:33 +08:00
|
|
|
Target = getRegionForConstructedObject(CE, Pred);
|
2012-07-27 04:04:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CXXConstructExpr::CK_VirtualBase:
|
2013-06-25 09:55:59 +08:00
|
|
|
// Make sure we are not calling virtual base class initializers twice.
|
|
|
|
// Only the most-derived object should initialize virtual base classes.
|
|
|
|
if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) {
|
|
|
|
const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer);
|
|
|
|
if (OuterCtor) {
|
|
|
|
switch (OuterCtor->getConstructionKind()) {
|
|
|
|
case CXXConstructExpr::CK_NonVirtualBase:
|
|
|
|
case CXXConstructExpr::CK_VirtualBase:
|
|
|
|
// Bail out!
|
|
|
|
destNodes.Add(Pred);
|
|
|
|
return;
|
|
|
|
case CXXConstructExpr::CK_Complete:
|
|
|
|
case CXXConstructExpr::CK_Delegating:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
case CXXConstructExpr::CK_NonVirtualBase:
|
2012-07-27 04:04:13 +08:00
|
|
|
case CXXConstructExpr::CK_Delegating: {
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
|
|
|
Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
|
|
|
|
LCtx->getCurrentStackFrame());
|
2012-07-27 04:04:16 +08:00
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
2012-07-27 04:04:13 +08:00
|
|
|
|
|
|
|
if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
|
|
|
|
Target = ThisVal.getAsRegion();
|
|
|
|
} else {
|
|
|
|
// Cast to the base type.
|
2013-02-21 11:12:32 +08:00
|
|
|
bool IsVirtual =
|
|
|
|
(CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
|
|
|
|
SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
|
|
|
|
IsVirtual);
|
2012-07-27 04:04:25 +08:00
|
|
|
Target = BaseVal.getAsRegion();
|
2012-07-27 04:04:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXConstructorCall> Call =
|
|
|
|
CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2012-07-03 03:28:12 +08:00
|
|
|
ExplodedNodeSet DstPreVisit;
|
|
|
|
getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
|
2013-06-25 09:56:08 +08:00
|
|
|
|
|
|
|
ExplodedNodeSet PreInitialized;
|
|
|
|
{
|
|
|
|
StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
|
|
|
|
if (CE->requiresZeroInitialization()) {
|
|
|
|
// Type of the zero doesn't matter.
|
|
|
|
SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy);
|
|
|
|
|
|
|
|
for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
|
|
|
|
E = DstPreVisit.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ProgramStateRef State = (*I)->getState();
|
|
|
|
// FIXME: Once we properly handle constructors in new-expressions, we'll
|
|
|
|
// need to invalidate the region before setting a default value, to make
|
|
|
|
// sure there aren't any lingering bindings around. This probably needs
|
|
|
|
// to happen regardless of whether or not the object is zero-initialized
|
|
|
|
// to handle random fields of a placement-initialized object picking up
|
|
|
|
// old bindings. We might only want to do it when we need to, though.
|
|
|
|
// FIXME: This isn't actually correct for arrays -- we need to zero-
|
|
|
|
// initialize the entire array, not just the first element -- but our
|
|
|
|
// handling of arrays everywhere else is weak as well, so this shouldn't
|
|
|
|
// actually make things worse. Placement new makes this tricky as well,
|
|
|
|
// since it's then possible to be initializing one part of a multi-
|
|
|
|
// dimensional array.
|
|
|
|
State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal);
|
2014-05-27 10:45:47 +08:00
|
|
|
Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
|
|
|
|
ProgramPoint::PreStmtKind);
|
2013-06-25 09:56:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:28:16 +08:00
|
|
|
ExplodedNodeSet DstPreCall;
|
2013-06-25 09:56:08 +08:00
|
|
|
getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
|
2012-07-31 04:22:09 +08:00
|
|
|
*Call, *this);
|
2011-04-09 06:42:35 +08:00
|
|
|
|
2013-02-15 08:32:15 +08:00
|
|
|
ExplodedNodeSet DstEvaluated;
|
|
|
|
StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
|
|
|
|
|
2013-06-22 00:30:32 +08:00
|
|
|
bool IsArray = isa<ElementRegion>(Target);
|
|
|
|
if (CE->getConstructor()->isTrivial() &&
|
|
|
|
CE->getConstructor()->isCopyOrMoveConstructor() &&
|
|
|
|
!IsArray) {
|
|
|
|
// FIXME: Handle other kinds of trivial constructors as well.
|
|
|
|
for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
|
|
|
|
I != E; ++I)
|
|
|
|
performTrivialCopy(Bldr, *I, *Call);
|
|
|
|
|
2013-02-15 08:32:15 +08:00
|
|
|
} else {
|
|
|
|
for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
|
|
|
|
I != E; ++I)
|
|
|
|
defaultEvalCall(Bldr, *I, *Call);
|
|
|
|
}
|
2011-04-09 06:42:35 +08:00
|
|
|
|
2012-07-03 03:28:16 +08:00
|
|
|
ExplodedNodeSet DstPostCall;
|
2013-02-15 08:32:15 +08:00
|
|
|
getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
|
2012-07-31 04:22:09 +08:00
|
|
|
*Call, *this);
|
2012-07-03 03:28:16 +08:00
|
|
|
getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
|
2010-04-19 20:51:02 +08:00
|
|
|
}
|
|
|
|
|
2012-07-27 04:04:13 +08:00
|
|
|
void ExprEngine::VisitCXXDestructor(QualType ObjectType,
|
|
|
|
const MemRegion *Dest,
|
|
|
|
const Stmt *S,
|
2012-09-07 04:37:08 +08:00
|
|
|
bool IsBaseDtor,
|
2015-09-08 11:50:52 +08:00
|
|
|
ExplodedNode *Pred,
|
2012-07-27 04:04:13 +08:00
|
|
|
ExplodedNodeSet &Dst) {
|
2012-07-31 04:22:09 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
2012-07-27 04:04:25 +08:00
|
|
|
// FIXME: We need to run the same destructor on every element of the array.
|
|
|
|
// This workaround will just run the first destructor (which will still
|
|
|
|
// invalidate the entire array).
|
2013-09-26 00:06:17 +08:00
|
|
|
SVal DestVal = UnknownVal();
|
|
|
|
if (Dest)
|
|
|
|
DestVal = loc::MemRegionVal(Dest);
|
2013-04-03 09:39:08 +08:00
|
|
|
DestVal = makeZeroElementRegion(State, DestVal, ObjectType);
|
|
|
|
Dest = DestVal.getAsRegion();
|
2012-07-27 04:04:25 +08:00
|
|
|
|
2012-07-27 04:04:13 +08:00
|
|
|
const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
|
|
|
|
assert(RecordDecl && "Only CXXRecordDecls should have destructors");
|
|
|
|
const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
|
|
|
|
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXDestructorCall> Call =
|
2012-09-07 04:37:08 +08:00
|
|
|
CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
|
2011-10-23 10:31:52 +08:00
|
|
|
|
2012-08-04 07:31:15 +08:00
|
|
|
PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
|
|
|
|
Call->getSourceRange().getBegin(),
|
|
|
|
"Error evaluating destructor");
|
|
|
|
|
2012-07-11 06:07:47 +08:00
|
|
|
ExplodedNodeSet DstPreCall;
|
|
|
|
getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
|
2012-07-31 04:22:09 +08:00
|
|
|
*Call, *this);
|
2010-11-20 14:53:12 +08:00
|
|
|
|
2012-07-11 06:07:47 +08:00
|
|
|
ExplodedNodeSet DstInvalidated;
|
2012-08-22 14:26:15 +08:00
|
|
|
StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
|
2012-07-11 06:07:47 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
|
|
|
|
I != E; ++I)
|
2012-07-31 04:22:09 +08:00
|
|
|
defaultEvalCall(Bldr, *I, *Call);
|
2012-07-11 06:07:47 +08:00
|
|
|
|
|
|
|
ExplodedNodeSet DstPostCall;
|
|
|
|
getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
|
2012-07-31 04:22:09 +08:00
|
|
|
*Call, *this);
|
2010-11-20 14:53:12 +08:00
|
|
|
}
|
|
|
|
|
2014-02-11 10:21:06 +08:00
|
|
|
void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
|
|
|
|
CNE->getStartLoc(),
|
|
|
|
"Error evaluating New Allocator Call");
|
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXAllocatorCall> Call =
|
|
|
|
CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
|
|
|
|
|
|
|
|
ExplodedNodeSet DstPreCall;
|
|
|
|
getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
|
|
|
|
*Call, *this);
|
|
|
|
|
|
|
|
ExplodedNodeSet DstInvalidated;
|
|
|
|
StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
|
|
|
|
for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
|
|
|
|
I != E; ++I)
|
|
|
|
defaultEvalCall(Bldr, *I, *Call);
|
|
|
|
getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
|
|
|
|
*Call, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-23 02:53:44 +08:00
|
|
|
void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
|
2010-04-19 20:51:02 +08:00
|
|
|
ExplodedNodeSet &Dst) {
|
2012-07-03 06:21:47 +08:00
|
|
|
// FIXME: Much of this should eventually migrate to CXXAllocatorCall.
|
|
|
|
// Also, we need to decide how allocators actually work -- they're not
|
|
|
|
// really part of the CXXNewExpr because they happen BEFORE the
|
|
|
|
// CXXConstructExpr subexpression. See PR12014 for some discussion.
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-08-22 14:26:15 +08:00
|
|
|
unsigned blockCount = currBldrCtx->blockCount();
|
2012-02-18 07:13:45 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2013-03-29 00:10:38 +08:00
|
|
|
DefinedOrUnknownSVal symVal = UnknownVal();
|
|
|
|
FunctionDecl *FD = CNE->getOperatorNew();
|
|
|
|
|
|
|
|
bool IsStandardGlobalOpNewFunction = false;
|
|
|
|
if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) {
|
|
|
|
if (FD->getNumParams() == 2) {
|
|
|
|
QualType T = FD->getParamDecl(1)->getType();
|
|
|
|
if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
|
|
|
|
// NoThrow placement new behaves as a standard new.
|
|
|
|
IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// Placement forms are considered non-standard.
|
|
|
|
IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1);
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
// We assume all standard global 'operator new' functions allocate memory in
|
|
|
|
// heap. We realize this is an approximation that might not correctly model
|
2013-03-29 00:10:38 +08:00
|
|
|
// a custom global allocator.
|
|
|
|
if (IsStandardGlobalOpNewFunction)
|
|
|
|
symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
|
|
|
|
else
|
2014-05-27 10:45:47 +08:00
|
|
|
symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
|
2013-03-29 00:10:38 +08:00
|
|
|
blockCount);
|
2011-03-31 12:04:48 +08:00
|
|
|
|
2013-03-29 00:10:38 +08:00
|
|
|
ProgramStateRef State = Pred->getState();
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXAllocatorCall> Call =
|
|
|
|
CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
|
|
|
|
|
2012-07-03 06:21:47 +08:00
|
|
|
// Invalidate placement args.
|
2012-07-31 04:22:09 +08:00
|
|
|
// FIXME: Once we figure out how we want allocators to work,
|
|
|
|
// we should be using the usual pre-/(default-)eval-/post-call checks here.
|
|
|
|
State = Call->invalidateRegions(blockCount);
|
2013-03-30 09:31:48 +08:00
|
|
|
if (!State)
|
|
|
|
return;
|
2012-07-03 06:21:47 +08:00
|
|
|
|
2013-08-28 16:04:08 +08:00
|
|
|
// If this allocation function is not declared as non-throwing, failures
|
|
|
|
// /must/ be signalled by exceptions, and thus the return value will never be
|
|
|
|
// NULL. -fno-exceptions does not influence this semantics.
|
|
|
|
// FIXME: GCC has a -fcheck-new option, which forces it to consider the case
|
|
|
|
// where new can return NULL. If we end up supporting that option, we can
|
|
|
|
// consider adding a check for it here.
|
2012-10-20 10:32:51 +08:00
|
|
|
// C++11 [basic.stc.dynamic.allocation]p3.
|
2013-08-28 16:04:08 +08:00
|
|
|
if (FD) {
|
2012-10-20 10:32:51 +08:00
|
|
|
QualType Ty = FD->getType();
|
|
|
|
if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>())
|
|
|
|
if (!ProtoType->isNothrow(getContext()))
|
|
|
|
State = State->assume(symVal, true);
|
|
|
|
}
|
|
|
|
|
2013-03-30 09:31:48 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
|
|
|
|
2011-03-31 12:04:48 +08:00
|
|
|
if (CNE->isArray()) {
|
|
|
|
// FIXME: allocating an array requires simulating the constructors.
|
|
|
|
// For now, just return a symbolicated region.
|
2013-02-20 13:52:05 +08:00
|
|
|
const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion();
|
2012-07-03 06:21:47 +08:00
|
|
|
QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
|
|
|
|
const ElementRegion *EleReg =
|
|
|
|
getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
|
2012-06-20 09:32:01 +08:00
|
|
|
State = State->BindExpr(CNE, Pred->getLocationContext(),
|
2012-01-07 06:09:28 +08:00
|
|
|
loc::MemRegionVal(EleReg));
|
2012-06-20 09:32:01 +08:00
|
|
|
Bldr.generateNode(CNE, Pred, State);
|
2011-03-31 12:04:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2012-07-03 06:21:47 +08:00
|
|
|
// FIXME: Once we have proper support for CXXConstructExprs inside
|
|
|
|
// CXXNewExpr, we need to make sure that the constructed object is not
|
|
|
|
// immediately invalidated here. (The placement call should happen before
|
|
|
|
// the constructor call anyway.)
|
2013-03-30 09:31:48 +08:00
|
|
|
SVal Result = symVal;
|
2012-06-20 09:32:01 +08:00
|
|
|
if (FD && FD->isReservedGlobalPlacementOperator()) {
|
|
|
|
// Non-array placement new should always return the placement location.
|
|
|
|
SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
|
2013-03-30 09:31:48 +08:00
|
|
|
Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
|
|
|
|
CNE->getPlacementArg(0)->getType());
|
2012-06-20 09:32:01 +08:00
|
|
|
}
|
|
|
|
|
2013-03-30 09:31:48 +08:00
|
|
|
// Bind the address of the object, then check to see if we cached out.
|
|
|
|
State = State->BindExpr(CNE, LCtx, Result);
|
2013-03-30 09:31:42 +08:00
|
|
|
ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
|
|
|
|
if (!NewN)
|
|
|
|
return;
|
2013-03-28 02:10:35 +08:00
|
|
|
|
2012-07-17 07:38:09 +08:00
|
|
|
// If the type is not a record, we won't have a CXXConstructExpr as an
|
|
|
|
// initializer. Copy the value over.
|
|
|
|
if (const Expr *Init = CNE->getInitializer()) {
|
|
|
|
if (!isa<CXXConstructExpr>(Init)) {
|
2013-03-28 02:10:35 +08:00
|
|
|
assert(Bldr.getResults().size() == 1);
|
2013-03-30 09:31:42 +08:00
|
|
|
Bldr.takeNodes(NewN);
|
2013-03-30 09:31:48 +08:00
|
|
|
evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
|
|
|
|
/*FirstInit=*/IsStandardGlobalOpNewFunction);
|
2012-07-17 07:38:09 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-19 20:51:02 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
|
2011-10-25 02:26:19 +08:00
|
|
|
ExplodedNode *Pred, ExplodedNodeSet &Dst) {
|
2012-08-22 14:26:15 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2012-02-29 16:42:57 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
|
|
|
Bldr.generateNode(CDE, Pred, state);
|
2010-04-21 10:17:31 +08:00
|
|
|
}
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2012-03-10 09:34:17 +08:00
|
|
|
void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
const VarDecl *VD = CS->getExceptionDecl();
|
2012-03-16 13:58:15 +08:00
|
|
|
if (!VD) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 09:34:17 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2012-08-22 14:26:06 +08:00
|
|
|
SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
|
2012-08-22 14:26:15 +08:00
|
|
|
currBldrCtx->blockCount());
|
2012-03-10 09:34:17 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
|
|
|
state = state->bindLoc(state->getLValue(VD, LCtx), V);
|
|
|
|
|
2012-08-22 14:26:15 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2012-03-10 09:34:17 +08:00
|
|
|
Bldr.generateNode(CS, Pred, state);
|
|
|
|
}
|
|
|
|
|
2010-12-23 02:53:44 +08:00
|
|
|
void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
|
2010-04-21 10:17:31 +08:00
|
|
|
ExplodedNodeSet &Dst) {
|
2012-08-22 14:26:15 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2011-10-25 02:26:19 +08:00
|
|
|
|
2010-04-19 20:51:02 +08:00
|
|
|
// Get the this object region from StoreManager.
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2010-04-19 20:51:02 +08:00
|
|
|
const MemRegion *R =
|
2010-12-02 15:49:45 +08:00
|
|
|
svalBuilder.getRegionManager().getCXXThisRegion(
|
2010-04-19 20:51:02 +08:00
|
|
|
getContext().getCanonicalType(TE->getType()),
|
2012-01-07 06:09:28 +08:00
|
|
|
LCtx);
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2010-04-19 20:51:02 +08:00
|
|
|
SVal V = state->getSVal(loc::MemRegionVal(R));
|
2012-01-07 06:09:28 +08:00
|
|
|
Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
|
2010-04-19 20:51:02 +08:00
|
|
|
}
|
2015-09-12 00:55:01 +08:00
|
|
|
|
|
|
|
void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
const LocationContext *LocCtxt = Pred->getLocationContext();
|
|
|
|
|
|
|
|
// Get the region of the lambda itself.
|
|
|
|
const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
|
|
|
|
LE, LocCtxt);
|
|
|
|
SVal V = loc::MemRegionVal(R);
|
|
|
|
|
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
|
|
|
// If we created a new MemRegion for the lambda, we should explicitly bind
|
|
|
|
// the captures.
|
|
|
|
CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
|
|
|
|
for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
|
|
|
|
e = LE->capture_init_end();
|
|
|
|
i != e; ++i, ++CurField) {
|
2015-12-08 07:01:53 +08:00
|
|
|
FieldDecl *FieldForCapture = *CurField;
|
|
|
|
SVal FieldLoc = State->getLValue(FieldForCapture, V);
|
|
|
|
|
|
|
|
SVal InitVal;
|
|
|
|
if (!FieldForCapture->hasCapturedVLAType()) {
|
|
|
|
Expr *InitExpr = *i;
|
|
|
|
assert(InitExpr && "Capture missing initialization expression");
|
|
|
|
InitVal = State->getSVal(InitExpr, LocCtxt);
|
|
|
|
} else {
|
|
|
|
// The field stores the length of a captured variable-length array.
|
|
|
|
// These captures don't have initialization expressions; instead we
|
|
|
|
// get the length from the VLAType size expression.
|
|
|
|
Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
|
|
|
|
InitVal = State->getSVal(SizeExpr, LocCtxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
State = State->bindLoc(FieldLoc, InitVal);
|
2015-09-12 00:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decay the Loc into an RValue, because there might be a
|
|
|
|
// MaterializeTemporaryExpr node above this one which expects the bound value
|
|
|
|
// to be an RValue.
|
|
|
|
SVal LambdaRVal = State->getSVal(R);
|
|
|
|
|
|
|
|
ExplodedNodeSet Tmp;
|
|
|
|
StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
|
|
|
|
// FIXME: is this the right program point kind?
|
|
|
|
Bldr.generateNode(LE, Pred,
|
|
|
|
State->BindExpr(LE, LocCtxt, LambdaRVal),
|
|
|
|
nullptr, ProgramPoint::PostLValueKind);
|
|
|
|
|
|
|
|
// FIXME: Move all post/pre visits to ::Visit().
|
|
|
|
getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
|
|
|
|
}
|