2011-08-20 05:59:58 +00:00
|
|
|
//===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
|
2010-04-19 12:51:02 +00: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 01:03:03 +00:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2010-04-19 12:51:02 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-03-10 01:34:17 +00:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
[analyzer] Fix a crash during C++17 aggregate construction of base objects.
Since C++17, classes that have base classes can potentially be initialized as
aggregates. Trying to construct such objects through brace initialization was
causing the analyzer to crash when the base class has a non-trivial constructor,
while figuring target region for the base class constructor, because the parent
stack frame didn't contain the constructor of the subclass, because there is
no constructor for subclass, merely aggregate initialization.
This patch avoids the crash, but doesn't provide the actually correct region
for the constructor, which still remains to be fixed. Instead, construction
goes into a fake temporary region which would be immediately discarded. Similar
extremely conservative approach is used for other cases in which the logic for
finding the target region is not yet implemented, including aggregate
initialization with fields instead of base-regions (which is not C++17-specific
but also never worked, just didn't crash).
Differential revision: https://reviews.llvm.org/D40841
rdar://problem/35441058
llvm-svn: 321128
2017-12-20 00:40:38 +00:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2012-08-03 23:31:15 +00:00
|
|
|
#include "clang/Basic/PrettyStackTrace.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2010-04-19 12:51:02 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 07:20:52 +00:00
|
|
|
using namespace ento;
|
2010-04-19 12:51:02 +00:00
|
|
|
|
2011-07-28 23:07:36 +00:00
|
|
|
void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
2012-08-22 06:26:15 +00:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2011-10-02 00:54:48 +00:00
|
|
|
const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
|
2012-01-26 21:29:00 +00:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2012-01-06 22:09:28 +00:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2010-04-19 12:51:02 +00:00
|
|
|
|
2013-07-25 22:32:35 +00:00
|
|
|
state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME);
|
2013-02-22 01:51:15 +00:00
|
|
|
Bldr.generateNode(ME, Pred, state);
|
2010-04-19 12:51:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-16 02:14:06 +00: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 00:32:15 +00:00
|
|
|
void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
|
2013-03-16 02:14:06 +00: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 00:32:15 +00:00
|
|
|
|
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
|
|
|
|
ExplodedNodeSet Dst;
|
|
|
|
Bldr.takeNodes(Pred);
|
|
|
|
|
|
|
|
SVal V = Call.getArgSVal(0);
|
|
|
|
|
2013-03-16 02:14:06 +00:00
|
|
|
// If the value being copied is not unknown, load from its location to get
|
|
|
|
// an aggregate rvalue.
|
2013-02-20 22:23:23 +00:00
|
|
|
if (Optional<Loc> L = V.getAs<Loc>())
|
2013-02-15 00:32:15 +00:00
|
|
|
V = Pred->getState()->getSVal(*L);
|
2013-03-16 02:14:06 +00:00
|
|
|
else
|
2016-10-31 21:11:20 +00:00
|
|
|
assert(V.isUnknownOrUndef());
|
2013-02-15 00:32:15 +00:00
|
|
|
|
2013-03-16 02:14:06 +00:00
|
|
|
const Expr *CallExpr = Call.getOriginExpr();
|
|
|
|
evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
|
2013-02-15 00:32:15 +00:00
|
|
|
|
2013-03-16 02:14:06 +00:00
|
|
|
PostStmt PS(CallExpr, LCtx);
|
2013-02-15 00:32:15 +00:00
|
|
|
for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ProgramStateRef State = (*I)->getState();
|
2013-03-16 02:14:06 +00:00
|
|
|
if (AlwaysReturnsLValue)
|
|
|
|
State = State->BindExpr(CallExpr, LCtx, ThisVal);
|
|
|
|
else
|
|
|
|
State = bindReturnValue(Call, LCtx, State);
|
2013-02-15 00:32:15 +00:00
|
|
|
Bldr.generateNode(PS, State, *I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 01:39:08 +00: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-01 16:40:06 +00:00
|
|
|
|
2015-12-17 00:28:33 +00:00
|
|
|
const MemRegion *
|
|
|
|
ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
|
|
|
|
ExplodedNode *Pred) {
|
2014-04-01 16:40:06 +00: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 00:28:33 +00:00
|
|
|
if (auto Elem = findElementDirectlyInitializedByCurrentConstructor()) {
|
|
|
|
if (Optional<CFGStmt> StmtElem = Elem->getAs<CFGStmt>()) {
|
2018-01-17 22:34:23 +00:00
|
|
|
if (const CXXNewExpr *CNE = dyn_cast<CXXNewExpr>(StmtElem->getStmt())) {
|
|
|
|
if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
|
|
|
|
// TODO: Detect when the allocator returns a null pointer.
|
|
|
|
// Constructor shall not be called in this case.
|
2018-01-17 22:51:19 +00:00
|
|
|
if (const SubRegion *MR = dyn_cast_or_null<SubRegion>(
|
|
|
|
getCXXNewAllocatorValue(State, CNE, LCtx).getAsRegion())) {
|
|
|
|
if (CNE->isArray()) {
|
|
|
|
// TODO: This code exists only to trigger the suppression for
|
|
|
|
// array constructors. In fact, we need to call the constructor
|
|
|
|
// for every allocated element, not just the first one!
|
|
|
|
return getStoreManager().GetElementZeroRegion(
|
|
|
|
MR, CNE->getType()->getPointeeType());
|
|
|
|
}
|
2018-01-17 22:34:23 +00:00
|
|
|
return MR;
|
2018-01-17 22:51:19 +00:00
|
|
|
}
|
2014-04-01 16:40:06 +00:00
|
|
|
}
|
2018-01-17 22:34:23 +00:00
|
|
|
} else if (auto *DS = dyn_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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unexpected directly initialized element!");
|
2014-04-01 16:40:06 +00:00
|
|
|
}
|
2015-12-17 00:28:33 +00:00
|
|
|
} else if (Optional<CFGInitializer> InitElem = Elem->getAs<CFGInitializer>()) {
|
2014-04-01 16:40:06 +00:00
|
|
|
const CXXCtorInitializer *Init = InitElem->getInitializer();
|
|
|
|
assert(Init->isAnyMemberInitializer());
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
2015-12-17 00:28:33 +00:00
|
|
|
Loc ThisPtr =
|
|
|
|
getSValBuilder().getCXXThis(CurCtor, LCtx->getCurrentStackFrame());
|
2014-04-01 16:40:06 +00: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 00:28:33 +00:00
|
|
|
MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
|
2014-04-01 16:40:06 +00:00
|
|
|
return MRMgr.getCXXTempObjectRegion(CE, LCtx);
|
|
|
|
}
|
|
|
|
|
2015-12-17 00:28:33 +00: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;
|
|
|
|
}
|
2018-01-17 22:34:23 +00:00
|
|
|
if (isa<CXXNewExpr>(StmtElem->getStmt())) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-17 00:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02 19:28:12 +00:00
|
|
|
void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
|
2011-04-08 22:42:35 +00:00
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &destNodes) {
|
2012-07-26 20:04:13 +00:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2012-07-26 20:04:16 +00:00
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
2014-05-27 02:45:47 +00:00
|
|
|
const MemRegion *Target = nullptr;
|
2013-04-03 01:39:08 +00: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-26 20:04:13 +00:00
|
|
|
|
|
|
|
switch (CE->getConstructionKind()) {
|
|
|
|
case CXXConstructExpr::CK_Complete: {
|
2015-12-17 00:28:33 +00:00
|
|
|
Target = getRegionForConstructedObject(CE, Pred);
|
2012-07-26 20:04:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CXXConstructExpr::CK_VirtualBase:
|
2013-06-25 01:55:59 +00: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:
|
[analyzer] Fix a crash during C++17 aggregate construction of base objects.
Since C++17, classes that have base classes can potentially be initialized as
aggregates. Trying to construct such objects through brace initialization was
causing the analyzer to crash when the base class has a non-trivial constructor,
while figuring target region for the base class constructor, because the parent
stack frame didn't contain the constructor of the subclass, because there is
no constructor for subclass, merely aggregate initialization.
This patch avoids the crash, but doesn't provide the actually correct region
for the constructor, which still remains to be fixed. Instead, construction
goes into a fake temporary region which would be immediately discarded. Similar
extremely conservative approach is used for other cases in which the logic for
finding the target region is not yet implemented, including aggregate
initialization with fields instead of base-regions (which is not C++17-specific
but also never worked, just didn't crash).
Differential revision: https://reviews.llvm.org/D40841
rdar://problem/35441058
llvm-svn: 321128
2017-12-20 00:40:38 +00:00
|
|
|
// In C++17, classes with non-virtual bases may be aggregates, so they would
|
|
|
|
// be initialized as aggregates without a constructor call, so we may have
|
|
|
|
// a base class constructed directly into an initializer list without
|
|
|
|
// having the derived-class constructor call on the previous stack frame.
|
|
|
|
// Initializer lists may be nested into more initializer lists that
|
|
|
|
// correspond to surrounding aggregate initializations.
|
|
|
|
// FIXME: For now this code essentially bails out. We need to find the
|
|
|
|
// correct target region and set it.
|
|
|
|
// FIXME: Instead of relying on the ParentMap, we should have the
|
|
|
|
// trigger-statement (InitListExpr in this case) passed down from CFG or
|
|
|
|
// otherwise always available during construction.
|
|
|
|
if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(CE))) {
|
|
|
|
MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
|
|
|
|
Target = MRMgr.getCXXTempObjectRegion(CE, LCtx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
2012-07-26 20:04:13 +00:00
|
|
|
case CXXConstructExpr::CK_Delegating: {
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
|
|
|
Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
|
|
|
|
LCtx->getCurrentStackFrame());
|
2012-07-26 20:04:16 +00:00
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
2012-07-26 20:04:13 +00:00
|
|
|
|
|
|
|
if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
|
|
|
|
Target = ThisVal.getAsRegion();
|
|
|
|
} else {
|
|
|
|
// Cast to the base type.
|
2013-02-21 03:12:32 +00:00
|
|
|
bool IsVirtual =
|
|
|
|
(CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
|
|
|
|
SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
|
|
|
|
IsVirtual);
|
2012-07-26 20:04:25 +00:00
|
|
|
Target = BaseVal.getAsRegion();
|
2012-07-26 20:04:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 20:22:09 +00:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXConstructorCall> Call =
|
|
|
|
CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
|
2010-04-19 12:51:02 +00:00
|
|
|
|
2012-07-02 19:28:12 +00:00
|
|
|
ExplodedNodeSet DstPreVisit;
|
|
|
|
getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
|
2013-06-25 01:56:08 +00: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.
|
2017-01-13 00:50:57 +00:00
|
|
|
State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal, LCtx);
|
2014-05-27 02:45:47 +00:00
|
|
|
Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
|
|
|
|
ProgramPoint::PreStmtKind);
|
2013-06-25 01:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 19:28:16 +00:00
|
|
|
ExplodedNodeSet DstPreCall;
|
2013-06-25 01:56:08 +00:00
|
|
|
getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
|
2012-07-30 20:22:09 +00:00
|
|
|
*Call, *this);
|
2011-04-08 22:42:35 +00:00
|
|
|
|
2013-02-15 00:32:15 +00:00
|
|
|
ExplodedNodeSet DstEvaluated;
|
|
|
|
StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
|
|
|
|
|
2013-06-21 16:30:32 +00: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 00:32:15 +00:00
|
|
|
} else {
|
|
|
|
for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
|
|
|
|
I != E; ++I)
|
|
|
|
defaultEvalCall(Bldr, *I, *Call);
|
|
|
|
}
|
2011-04-08 22:42:35 +00:00
|
|
|
|
2016-12-19 22:23:22 +00:00
|
|
|
// If the CFG was contructed without elements for temporary destructors
|
|
|
|
// and the just-called constructor created a temporary object then
|
|
|
|
// stop exploration if the temporary object has a noreturn constructor.
|
|
|
|
// This can lose coverage because the destructor, if it were present
|
|
|
|
// in the CFG, would be called at the end of the full expression or
|
|
|
|
// later (for life-time extended temporaries) -- but avoids infeasible
|
|
|
|
// paths when no-return temporary destructors are used for assertions.
|
|
|
|
const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
|
|
|
|
if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
|
|
|
|
const MemRegion *Target = Call->getCXXThisVal().getAsRegion();
|
|
|
|
if (Target && isa<CXXTempObjectRegion>(Target) &&
|
|
|
|
Call->getDecl()->getParent()->isAnyDestructorNoReturn()) {
|
|
|
|
|
|
|
|
for (ExplodedNode *N : DstEvaluated) {
|
|
|
|
Bldr.generateSink(CE, N, N->getState());
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no need to run the PostCall and PostStmtchecker
|
|
|
|
// callbacks because we just generated sinks on all nodes in th
|
|
|
|
// frontier.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 19:28:16 +00:00
|
|
|
ExplodedNodeSet DstPostCall;
|
2013-02-15 00:32:15 +00:00
|
|
|
getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
|
2012-07-30 20:22:09 +00:00
|
|
|
*Call, *this);
|
2012-07-02 19:28:16 +00:00
|
|
|
getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
|
2010-04-19 12:51:02 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 20:04:13 +00:00
|
|
|
void ExprEngine::VisitCXXDestructor(QualType ObjectType,
|
|
|
|
const MemRegion *Dest,
|
|
|
|
const Stmt *S,
|
2012-09-06 20:37:08 +00:00
|
|
|
bool IsBaseDtor,
|
2015-09-08 03:50:52 +00:00
|
|
|
ExplodedNode *Pred,
|
2012-07-26 20:04:13 +00:00
|
|
|
ExplodedNodeSet &Dst) {
|
2012-07-30 20:22:09 +00:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
2012-07-26 20:04:25 +00: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-25 16:06:17 +00:00
|
|
|
SVal DestVal = UnknownVal();
|
|
|
|
if (Dest)
|
|
|
|
DestVal = loc::MemRegionVal(Dest);
|
2013-04-03 01:39:08 +00:00
|
|
|
DestVal = makeZeroElementRegion(State, DestVal, ObjectType);
|
|
|
|
Dest = DestVal.getAsRegion();
|
2012-07-26 20:04:25 +00:00
|
|
|
|
2012-07-26 20:04:13 +00:00
|
|
|
const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
|
|
|
|
assert(RecordDecl && "Only CXXRecordDecls should have destructors");
|
|
|
|
const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
|
|
|
|
|
2012-07-30 20:22:09 +00:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXDestructorCall> Call =
|
2012-09-06 20:37:08 +00:00
|
|
|
CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
|
2011-10-23 02:31:52 +00:00
|
|
|
|
2012-08-03 23:31:15 +00:00
|
|
|
PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
|
|
|
|
Call->getSourceRange().getBegin(),
|
|
|
|
"Error evaluating destructor");
|
|
|
|
|
2012-07-10 22:07:47 +00:00
|
|
|
ExplodedNodeSet DstPreCall;
|
|
|
|
getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
|
2012-07-30 20:22:09 +00:00
|
|
|
*Call, *this);
|
2010-11-20 06:53:12 +00:00
|
|
|
|
2012-07-10 22:07:47 +00:00
|
|
|
ExplodedNodeSet DstInvalidated;
|
2012-08-22 06:26:15 +00:00
|
|
|
StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
|
2012-07-10 22:07:47 +00:00
|
|
|
for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
|
|
|
|
I != E; ++I)
|
2012-07-30 20:22:09 +00:00
|
|
|
defaultEvalCall(Bldr, *I, *Call);
|
2012-07-10 22:07:47 +00:00
|
|
|
|
|
|
|
ExplodedNodeSet DstPostCall;
|
|
|
|
getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
|
2012-07-30 20:22:09 +00:00
|
|
|
*Call, *this);
|
2010-11-20 06:53:12 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 02:21:06 +00: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);
|
|
|
|
|
2018-01-17 22:34:23 +00:00
|
|
|
ExplodedNodeSet DstPostCall;
|
|
|
|
StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
|
2018-01-17 22:58:35 +00:00
|
|
|
for (auto I : DstPreCall) {
|
|
|
|
// FIXME: Provide evalCall for checkers?
|
2018-01-17 22:34:23 +00:00
|
|
|
defaultEvalCall(CallBldr, I, *Call);
|
2018-01-17 22:58:35 +00:00
|
|
|
}
|
2018-01-17 22:51:19 +00:00
|
|
|
// If the call is inlined, DstPostCall will be empty and we bail out now.
|
2018-01-17 22:34:23 +00:00
|
|
|
|
|
|
|
// Store return value of operator new() for future use, until the actual
|
|
|
|
// CXXNewExpr gets processed.
|
|
|
|
ExplodedNodeSet DstPostValue;
|
|
|
|
StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
|
|
|
|
for (auto I : DstPostCall) {
|
2018-01-17 22:51:19 +00:00
|
|
|
// FIXME: Because CNE serves as the "call site" for the allocator (due to
|
|
|
|
// lack of a better expression in the AST), the conjured return value symbol
|
|
|
|
// is going to be of the same type (C++ object pointer type). Technically
|
|
|
|
// this is not correct because the operator new's prototype always says that
|
|
|
|
// it returns a 'void *'. So we should change the type of the symbol,
|
|
|
|
// and then evaluate the cast over the symbolic pointer from 'void *' to
|
|
|
|
// the object pointer type. But without changing the symbol's type it
|
|
|
|
// is breaking too much to evaluate the no-op symbolic cast over it, so we
|
|
|
|
// skip it for now.
|
2018-01-17 22:34:23 +00:00
|
|
|
ProgramStateRef State = I->getState();
|
2018-01-24 20:32:26 +00:00
|
|
|
SVal RetVal = State->getSVal(CNE, LCtx);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// C++11 [basic.stc.dynamic.allocation]p3.
|
|
|
|
if (const FunctionDecl *FD = CNE->getOperatorNew()) {
|
|
|
|
QualType Ty = FD->getType();
|
|
|
|
if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
|
|
|
|
if (!ProtoType->isNothrow(getContext()))
|
|
|
|
State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueBldr.generateNode(CNE, I,
|
|
|
|
setCXXNewAllocatorValue(State, CNE, LCtx, RetVal));
|
2018-01-17 22:34:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 23:46:13 +00:00
|
|
|
ExplodedNodeSet DstPostPostCallCallback;
|
|
|
|
getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
|
|
|
|
DstPostValue, *Call, *this);
|
|
|
|
for (auto I : DstPostPostCallCallback) {
|
|
|
|
getCheckerManager().runCheckersForNewAllocator(
|
|
|
|
CNE, getCXXNewAllocatorValue(I->getState(), CNE, LCtx), Dst, I, *this);
|
|
|
|
}
|
2014-02-11 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 18:53:44 +00:00
|
|
|
void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
|
2010-04-19 12:51:02 +00:00
|
|
|
ExplodedNodeSet &Dst) {
|
2012-07-02 22:21:47 +00: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 03:50:52 +00:00
|
|
|
|
2012-08-22 06:26:15 +00:00
|
|
|
unsigned blockCount = currBldrCtx->blockCount();
|
2012-02-17 23:13:45 +00:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2018-01-17 22:34:23 +00:00
|
|
|
SVal symVal = UnknownVal();
|
2013-03-28 16:10:38 +00:00
|
|
|
FunctionDecl *FD = CNE->getOperatorNew();
|
|
|
|
|
2018-01-17 22:58:35 +00:00
|
|
|
bool IsStandardGlobalOpNewFunction =
|
|
|
|
FD->isReplaceableGlobalAllocationFunction();
|
2013-03-28 16:10:38 +00:00
|
|
|
|
2018-01-17 22:34:23 +00:00
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
|
|
|
// Retrieve the stored operator new() return value.
|
|
|
|
if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
|
|
|
|
symVal = getCXXNewAllocatorValue(State, CNE, LCtx);
|
|
|
|
State = clearCXXNewAllocatorValue(State, CNE, LCtx);
|
|
|
|
}
|
|
|
|
|
2015-09-08 03:50:52 +00: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-28 16:10:38 +00:00
|
|
|
// a custom global allocator.
|
2018-01-17 22:34:23 +00:00
|
|
|
if (symVal.isUnknown()) {
|
|
|
|
if (IsStandardGlobalOpNewFunction)
|
|
|
|
symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
|
|
|
|
else
|
|
|
|
symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
|
|
|
|
blockCount);
|
|
|
|
}
|
2011-03-31 04:04:48 +00:00
|
|
|
|
2012-07-30 20:22:09 +00:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXAllocatorCall> Call =
|
|
|
|
CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
|
|
|
|
|
2018-01-17 22:34:23 +00:00
|
|
|
if (!AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) {
|
|
|
|
// Invalidate placement args.
|
|
|
|
// 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);
|
|
|
|
if (!State)
|
|
|
|
return;
|
2012-07-02 22:21:47 +00:00
|
|
|
|
2018-01-24 20:32:26 +00: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.
|
|
|
|
// C++11 [basic.stc.dynamic.allocation]p3.
|
|
|
|
if (FD) {
|
|
|
|
QualType Ty = FD->getType();
|
|
|
|
if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
|
|
|
|
if (!ProtoType->isNothrow(getContext()))
|
|
|
|
if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
|
|
|
|
State = State->assume(*dSymVal, true);
|
|
|
|
}
|
2012-10-20 02:32:51 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 01:31:48 +00:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
|
|
|
|
2018-01-17 22:51:19 +00:00
|
|
|
SVal Result = symVal;
|
|
|
|
|
2011-03-31 04:04:48 +00:00
|
|
|
if (CNE->isArray()) {
|
|
|
|
// FIXME: allocating an array requires simulating the constructors.
|
|
|
|
// For now, just return a symbolicated region.
|
2018-01-17 22:58:35 +00:00
|
|
|
if (const SubRegion *NewReg =
|
|
|
|
dyn_cast_or_null<SubRegion>(symVal.getAsRegion())) {
|
2018-01-17 22:51:19 +00:00
|
|
|
QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
|
|
|
|
const ElementRegion *EleReg =
|
|
|
|
getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
|
|
|
|
Result = loc::MemRegionVal(EleReg);
|
|
|
|
}
|
|
|
|
State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
|
2012-06-20 01:32:01 +00:00
|
|
|
Bldr.generateNode(CNE, Pred, State);
|
2011-03-31 04:04:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-04-19 12:51:02 +00:00
|
|
|
|
2012-07-02 22:21:47 +00: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.)
|
2012-06-20 01:32:01 +00: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 01:31:48 +00:00
|
|
|
Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
|
|
|
|
CNE->getPlacementArg(0)->getType());
|
2012-06-20 01:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 01:31:48 +00:00
|
|
|
// Bind the address of the object, then check to see if we cached out.
|
|
|
|
State = State->BindExpr(CNE, LCtx, Result);
|
2013-03-30 01:31:42 +00:00
|
|
|
ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
|
|
|
|
if (!NewN)
|
|
|
|
return;
|
2013-03-27 18:10:35 +00:00
|
|
|
|
2012-07-16 23:38:09 +00: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-27 18:10:35 +00:00
|
|
|
assert(Bldr.getResults().size() == 1);
|
2013-03-30 01:31:42 +00:00
|
|
|
Bldr.takeNodes(NewN);
|
2013-03-30 01:31:48 +00:00
|
|
|
evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
|
|
|
|
/*FirstInit=*/IsStandardGlobalOpNewFunction);
|
2012-07-16 23:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-19 12:51:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 03:50:52 +00:00
|
|
|
void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
|
2011-10-24 18:26:19 +00:00
|
|
|
ExplodedNode *Pred, ExplodedNodeSet &Dst) {
|
2012-08-22 06:26:15 +00:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2012-02-29 08:42:57 +00:00
|
|
|
ProgramStateRef state = Pred->getState();
|
|
|
|
Bldr.generateNode(CDE, Pred, state);
|
2010-04-21 02:17:31 +00:00
|
|
|
}
|
2010-04-19 12:51:02 +00:00
|
|
|
|
2012-03-10 01:34:17 +00:00
|
|
|
void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
const VarDecl *VD = CS->getExceptionDecl();
|
2012-03-16 05:58:15 +00:00
|
|
|
if (!VD) {
|
|
|
|
Dst.Add(Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 01:34:17 +00:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2012-08-22 06:26:06 +00:00
|
|
|
SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
|
2012-08-22 06:26:15 +00:00
|
|
|
currBldrCtx->blockCount());
|
2012-03-10 01:34:17 +00:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2017-01-13 00:50:57 +00:00
|
|
|
state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
|
2012-03-10 01:34:17 +00:00
|
|
|
|
2012-08-22 06:26:15 +00:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2012-03-10 01:34:17 +00:00
|
|
|
Bldr.generateNode(CS, Pred, state);
|
|
|
|
}
|
|
|
|
|
2010-12-22 18:53:44 +00:00
|
|
|
void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
|
2010-04-21 02:17:31 +00:00
|
|
|
ExplodedNodeSet &Dst) {
|
2012-08-22 06:26:15 +00:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
2011-10-24 18:26:19 +00:00
|
|
|
|
2010-04-19 12:51:02 +00:00
|
|
|
// Get the this object region from StoreManager.
|
2012-01-06 22:09:28 +00:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
2010-04-19 12:51:02 +00:00
|
|
|
const MemRegion *R =
|
2010-12-02 07:49:45 +00:00
|
|
|
svalBuilder.getRegionManager().getCXXThisRegion(
|
2010-04-19 12:51:02 +00:00
|
|
|
getContext().getCanonicalType(TE->getType()),
|
2012-01-06 22:09:28 +00:00
|
|
|
LCtx);
|
2010-04-19 12:51:02 +00:00
|
|
|
|
2012-01-26 21:29:00 +00:00
|
|
|
ProgramStateRef state = Pred->getState();
|
2010-04-19 12:51:02 +00:00
|
|
|
SVal V = state->getSVal(loc::MemRegionVal(R));
|
2012-01-06 22:09:28 +00:00
|
|
|
Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
|
2010-04-19 12:51:02 +00:00
|
|
|
}
|
2015-09-11 16:55:01 +00: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);
|
2016-09-01 11:11:46 +00:00
|
|
|
|
2015-09-11 16:55:01 +00:00
|
|
|
ProgramStateRef State = Pred->getState();
|
2016-09-01 11:11:46 +00:00
|
|
|
|
2015-09-11 16:55:01 +00:00
|
|
|
// 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-07 23:01:53 +00: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);
|
|
|
|
}
|
|
|
|
|
2017-01-13 00:50:57 +00:00
|
|
|
State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
|
2015-09-11 16:55:01 +00: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);
|
|
|
|
}
|