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) {
|
2013-09-02 17:09:15 +08:00
|
|
|
// FIXME: This check is just a temporary workaround, because
|
|
|
|
// ProcessTemporaryDtor sends us NULL regions. It will not be necessary once
|
|
|
|
// we can properly process temporary destructors.
|
|
|
|
if (!LValue.getAsRegion())
|
|
|
|
return LValue;
|
|
|
|
|
2013-04-03 09:39:08 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2012-07-27 04:04:13 +08:00
|
|
|
const MemRegion *Target = 0;
|
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: {
|
2012-07-27 04:04:16 +08:00
|
|
|
// See if we're constructing an existing region by looking at the next
|
|
|
|
// element in the CFG.
|
2012-08-22 14:26:15 +08:00
|
|
|
const CFGBlock *B = currBldrCtx->getBlock();
|
|
|
|
if (currStmtIdx + 1 < B->size()) {
|
|
|
|
CFGElement Next = (*B)[currStmtIdx+1];
|
2012-07-27 04:04:16 +08:00
|
|
|
|
|
|
|
// Is this a constructor for a local variable?
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGStmt> StmtElem = Next.getAs<CFGStmt>()) {
|
|
|
|
if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) {
|
2012-07-27 04:04:25 +08:00
|
|
|
if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) {
|
|
|
|
if (Var->getInit()->IgnoreImplicit() == CE) {
|
2013-04-03 09:39:08 +08:00
|
|
|
SVal LValue = State->getLValue(Var, LCtx);
|
2012-07-27 04:04:25 +08:00
|
|
|
QualType Ty = Var->getType();
|
2013-04-03 09:39:08 +08:00
|
|
|
LValue = makeZeroElementRegion(State, LValue, Ty);
|
|
|
|
Target = LValue.getAsRegion();
|
2012-07-27 04:04:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 04:04:21 +08:00
|
|
|
// Is this a constructor for a member?
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGInitializer> InitElem = Next.getAs<CFGInitializer>()) {
|
|
|
|
const CXXCtorInitializer *Init = InitElem->getInitializer();
|
2012-07-27 04:04:21 +08:00
|
|
|
assert(Init->isAnyMemberInitializer());
|
|
|
|
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
|
|
|
Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
|
|
|
|
LCtx->getCurrentStackFrame());
|
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
|
|
|
|
2013-04-03 09:39:08 +08:00
|
|
|
const ValueDecl *Field;
|
|
|
|
SVal FieldVal;
|
2012-07-27 04:04:21 +08:00
|
|
|
if (Init->isIndirectMemberInitializer()) {
|
2013-04-03 09:39:08 +08:00
|
|
|
Field = Init->getIndirectMember();
|
|
|
|
FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
|
2012-07-27 04:04:21 +08:00
|
|
|
} else {
|
2013-04-03 09:39:08 +08:00
|
|
|
Field = Init->getMember();
|
|
|
|
FieldVal = State->getLValue(Init->getMember(), ThisVal);
|
2012-07-27 04:04:21 +08:00
|
|
|
}
|
2013-04-03 09:39:08 +08:00
|
|
|
|
|
|
|
QualType Ty = Field->getType();
|
|
|
|
FieldVal = makeZeroElementRegion(State, FieldVal, Ty);
|
|
|
|
Target = FieldVal.getAsRegion();
|
2012-07-27 04:04:21 +08:00
|
|
|
}
|
|
|
|
|
2012-07-27 04:04:16 +08:00
|
|
|
// FIXME: This will eventually need to handle new-expressions as well.
|
2013-06-25 09:56:08 +08:00
|
|
|
// Don't forget to update the pre-constructor initialization code below.
|
2012-07-27 04:04:16 +08:00
|
|
|
}
|
|
|
|
|
2012-08-28 01:50:07 +08:00
|
|
|
// If we couldn't find an existing region to construct into, assume we're
|
|
|
|
// constructing a temporary.
|
|
|
|
if (!Target) {
|
|
|
|
MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
|
|
|
|
Target = MRMgr.getCXXTempObjectRegion(CE, LCtx);
|
|
|
|
}
|
2012-07-27 04:04:16 +08:00
|
|
|
|
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);
|
|
|
|
Bldr.generateNode(CE, *I, State, /*tag=*/0, ProgramPoint::PreStmtKind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2012-07-27 04:04:13 +08:00
|
|
|
ExplodedNode *Pred,
|
|
|
|
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-04-03 09:39:08 +08:00
|
|
|
SVal DestVal = loc::MemRegionVal(Dest);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
2011-03-31 12:04:48 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume all standard global 'operator new' functions allocate memory in
|
|
|
|
// heap. We realize this is an approximation that might not correctly model
|
|
|
|
// a custom global allocator.
|
|
|
|
if (IsStandardGlobalOpNewFunction)
|
|
|
|
symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
|
|
|
|
else
|
|
|
|
symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, CNE->getType(),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-23 02:53:44 +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
|
|
|
}
|