2011-08-20 13:59:58 +08:00
|
|
|
//===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
|
2010-04-19 20:51:02 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-04-19 20:51:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C++ expression evaluation engine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2018-02-28 04:03:35 +08:00
|
|
|
#include "clang/Analysis/ConstructionContext.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"
|
[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 08:40:38 +08:00
|
|
|
#include "clang/AST/ParentMap.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);
|
2019-11-17 18:41:55 +08:00
|
|
|
const Expr *tempExpr = ME->getSubExpr()->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;
|
2018-02-28 05:10:08 +08:00
|
|
|
const CXXRecordDecl *ThisRD = nullptr;
|
2013-03-16 10:14:06 +08:00
|
|
|
if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
|
|
|
|
assert(Ctor->getDecl()->isTrivial());
|
|
|
|
assert(Ctor->getDecl()->isCopyOrMoveConstructor());
|
|
|
|
ThisVal = Ctor->getCXXThisVal();
|
2018-02-28 05:10:08 +08:00
|
|
|
ThisRD = Ctor->getDecl()->getParent();
|
2013-03-16 10:14:06 +08:00
|
|
|
AlwaysReturnsLValue = false;
|
|
|
|
} else {
|
|
|
|
assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
|
|
|
|
assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
|
|
|
|
OO_Equal);
|
|
|
|
ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
|
2018-02-28 05:10:08 +08:00
|
|
|
ThisRD = cast<CXXMethodDecl>(Call.getDecl())->getParent();
|
2013-03-16 10:14:06 +08:00
|
|
|
AlwaysReturnsLValue = true;
|
|
|
|
}
|
2013-02-15 08:32:15 +08:00
|
|
|
|
2018-02-28 05:10:08 +08:00
|
|
|
assert(ThisRD);
|
|
|
|
if (ThisRD->isEmpty()) {
|
|
|
|
// Do nothing for empty classes. Otherwise it'd retrieve an UnknownVal
|
|
|
|
// and bind it and RegionStore would think that the actual value
|
|
|
|
// in this region at this offset is unknown.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
2016-11-01 05:11:20 +08:00
|
|
|
assert(V.isUnknownOrUndef());
|
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
|
|
|
|
2018-02-15 10:51:58 +08:00
|
|
|
SVal ExprEngine::makeZeroElementRegion(ProgramStateRef State, SVal LValue,
|
|
|
|
QualType &Ty, bool &IsArray) {
|
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);
|
2018-02-02 06:17:05 +08:00
|
|
|
IsArray = true;
|
2013-04-03 09:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return LValue;
|
|
|
|
}
|
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
std::pair<ProgramStateRef, SVal> ExprEngine::handleConstructionContext(
|
2018-06-14 09:20:12 +08:00
|
|
|
const Expr *E, ProgramStateRef State, const LocationContext *LCtx,
|
|
|
|
const ConstructionContext *CC, EvalCallOptions &CallOpts) {
|
2018-12-20 07:14:06 +08:00
|
|
|
SValBuilder &SVB = getSValBuilder();
|
|
|
|
MemRegionManager &MRMgr = SVB.getRegionManager();
|
|
|
|
ASTContext &ACtx = SVB.getContext();
|
2014-04-02 00:40:06 +08:00
|
|
|
|
2018-02-10 10:55:08 +08:00
|
|
|
// See if we're constructing an existing region by looking at the
|
|
|
|
// current construction context.
|
2018-02-16 03:17:44 +08:00
|
|
|
if (CC) {
|
2018-02-28 04:03:35 +08:00
|
|
|
switch (CC->getKind()) {
|
2018-06-14 09:54:21 +08:00
|
|
|
case ConstructionContext::CXX17ElidedCopyVariableKind:
|
2018-02-28 04:03:35 +08:00
|
|
|
case ConstructionContext::SimpleVariableKind: {
|
2018-06-14 09:54:21 +08:00
|
|
|
const auto *DSCC = cast<VariableConstructionContext>(CC);
|
2018-02-28 04:03:35 +08:00
|
|
|
const auto *DS = DSCC->getDeclStmt();
|
|
|
|
const auto *Var = cast<VarDecl>(DS->getSingleDecl());
|
|
|
|
SVal LValue = State->getLValue(Var, LCtx);
|
|
|
|
QualType Ty = Var->getType();
|
2018-06-14 09:32:46 +08:00
|
|
|
LValue =
|
|
|
|
makeZeroElementRegion(State, LValue, Ty, CallOpts.IsArrayCtorOrDtor);
|
|
|
|
State =
|
|
|
|
addObjectUnderConstruction(State, DSCC->getDeclStmt(), LCtx, LValue);
|
|
|
|
return std::make_pair(State, LValue);
|
2018-02-28 04:03:35 +08:00
|
|
|
}
|
2018-06-14 09:54:21 +08:00
|
|
|
case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind:
|
2018-03-23 06:02:38 +08:00
|
|
|
case ConstructionContext::SimpleConstructorInitializerKind: {
|
2018-02-28 04:03:35 +08:00
|
|
|
const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC);
|
|
|
|
const auto *Init = ICC->getCXXCtorInitializer();
|
2014-04-02 00:40:06 +08:00
|
|
|
assert(Init->isAnyMemberInitializer());
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
2015-12-17 08:28:33 +08:00
|
|
|
Loc ThisPtr =
|
2018-12-20 07:14:06 +08:00
|
|
|
SVB.getCXXThis(CurCtor, LCtx->getStackFrame());
|
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();
|
2018-02-02 06:17:05 +08:00
|
|
|
FieldVal = makeZeroElementRegion(State, FieldVal, Ty,
|
2018-02-15 10:51:58 +08:00
|
|
|
CallOpts.IsArrayCtorOrDtor);
|
2018-06-14 09:40:49 +08:00
|
|
|
State = addObjectUnderConstruction(State, Init, LCtx, FieldVal);
|
2018-06-14 09:20:12 +08:00
|
|
|
return std::make_pair(State, FieldVal);
|
2014-04-02 00:40:06 +08:00
|
|
|
}
|
2018-02-28 04:03:35 +08:00
|
|
|
case ConstructionContext::NewAllocatedObjectKind: {
|
[analyzer] Evaluate all non-checker config options before analysis
In earlier patches regarding AnalyzerOptions, a lot of effort went into
gathering all config options, and changing the interface so that potential
misuse can be eliminited.
Up until this point, AnalyzerOptions only evaluated an option when it was
querried. For example, if we had a "-no-false-positives" flag, AnalyzerOptions
would store an Optional field for it that would be None up until somewhere in
the code until the flag's getter function is called.
However, now that we're confident that we've gathered all configs, we can
evaluate off of them before analysis, so we can emit a error on invalid input
even if that prticular flag will not matter in that particular run of the
analyzer. Another very big benefit of this is that debug.ConfigDumper will now
show the value of all configs every single time.
Also, almost all options related class have a similar interface, so uniformity
is also a benefit.
The implementation for errors on invalid input will be commited shorty.
Differential Revision: https://reviews.llvm.org/D53692
llvm-svn: 348031
2018-12-01 04:44:00 +08:00
|
|
|
if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
|
2018-02-28 04:03:35 +08:00
|
|
|
const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC);
|
|
|
|
const auto *NE = NECC->getCXXNewExpr();
|
2018-06-01 09:59:48 +08:00
|
|
|
SVal V = *getObjectUnderConstruction(State, NE, LCtx);
|
|
|
|
if (const SubRegion *MR =
|
|
|
|
dyn_cast_or_null<SubRegion>(V.getAsRegion())) {
|
2018-02-28 04:03:35 +08:00
|
|
|
if (NE->isArray()) {
|
|
|
|
// TODO: In fact, we need to call the constructor for every
|
|
|
|
// allocated element, not just the first one!
|
|
|
|
CallOpts.IsArrayCtorOrDtor = true;
|
2018-06-14 09:20:12 +08:00
|
|
|
return std::make_pair(
|
|
|
|
State, loc::MemRegionVal(getStoreManager().GetElementZeroRegion(
|
|
|
|
MR, NE->getType()->getPointeeType())));
|
2018-02-28 04:03:35 +08:00
|
|
|
}
|
2018-06-14 09:20:12 +08:00
|
|
|
return std::make_pair(State, V);
|
2018-02-28 04:03:35 +08:00
|
|
|
}
|
2018-06-01 09:59:48 +08:00
|
|
|
// TODO: Detect when the allocator returns a null pointer.
|
|
|
|
// Constructor shall not be called in this case.
|
2018-02-28 04:03:35 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-14 09:59:35 +08:00
|
|
|
case ConstructionContext::SimpleReturnedValueKind:
|
|
|
|
case ConstructionContext::CXX17ElidedCopyReturnedValueKind: {
|
2018-03-13 07:22:35 +08:00
|
|
|
// The temporary is to be managed by the parent stack frame.
|
|
|
|
// So build it in the parent stack frame if we're not in the
|
|
|
|
// top frame of the analysis.
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *SFC = LCtx->getStackFrame();
|
2018-03-31 03:21:18 +08:00
|
|
|
if (const LocationContext *CallerLCtx = SFC->getParent()) {
|
|
|
|
auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()]
|
|
|
|
.getAs<CFGCXXRecordTypedCall>();
|
|
|
|
if (!RTC) {
|
|
|
|
// We were unable to find the correct construction context for the
|
|
|
|
// call in the parent stack frame. This is equivalent to not being
|
|
|
|
// able to find construction context at all.
|
2018-06-14 09:20:12 +08:00
|
|
|
break;
|
2018-03-31 03:21:18 +08:00
|
|
|
}
|
2019-05-08 06:33:13 +08:00
|
|
|
if (isa<BlockInvocationContext>(CallerLCtx)) {
|
|
|
|
// Unwrap block invocation contexts. They're mostly part of
|
|
|
|
// the current stack frame.
|
|
|
|
CallerLCtx = CallerLCtx->getParent();
|
|
|
|
assert(!isa<BlockInvocationContext>(CallerLCtx));
|
|
|
|
}
|
2020-02-18 02:42:50 +08:00
|
|
|
return handleConstructionContext(
|
2018-06-14 09:59:35 +08:00
|
|
|
cast<Expr>(SFC->getCallSite()), State, CallerLCtx,
|
|
|
|
RTC->getConstructionContext(), CallOpts);
|
2018-06-14 09:20:12 +08:00
|
|
|
} else {
|
2018-12-20 07:14:06 +08:00
|
|
|
// We are on the top frame of the analysis. We do not know where is the
|
|
|
|
// object returned to. Conjure a symbolic region for the return value.
|
|
|
|
// TODO: We probably need a new MemRegion kind to represent the storage
|
|
|
|
// of that SymbolicRegion, so that we cound produce a fancy symbol
|
|
|
|
// instead of an anonymous conjured symbol.
|
|
|
|
// TODO: Do we need to track the region to avoid having it dead
|
|
|
|
// too early? It does die too early, at least in C++17, but because
|
|
|
|
// putting anything into a SymbolicRegion causes an immediate escape,
|
|
|
|
// it doesn't cause any leak false positives.
|
|
|
|
const auto *RCC = cast<ReturnedValueConstructionContext>(CC);
|
|
|
|
// Make sure that this doesn't coincide with any other symbol
|
|
|
|
// conjured for the returned expression.
|
|
|
|
static const int TopLevelSymRegionTag = 0;
|
|
|
|
const Expr *RetE = RCC->getReturnStmt()->getRetValue();
|
|
|
|
assert(RetE && "Void returns should not have a construction context");
|
|
|
|
QualType ReturnTy = RetE->getType();
|
|
|
|
QualType RegionTy = ACtx.getPointerType(ReturnTy);
|
|
|
|
SVal V = SVB.conjureSymbolVal(&TopLevelSymRegionTag, RetE, SFC,
|
|
|
|
RegionTy, currBldrCtx->blockCount());
|
2018-06-14 09:20:12 +08:00
|
|
|
return std::make_pair(State, V);
|
2018-03-13 07:22:35 +08:00
|
|
|
}
|
2018-06-14 09:59:35 +08:00
|
|
|
llvm_unreachable("Unhandled return value construction context!");
|
2018-06-14 09:20:12 +08:00
|
|
|
}
|
2018-06-28 08:30:18 +08:00
|
|
|
case ConstructionContext::ElidedTemporaryObjectKind: {
|
[analyzer] Evaluate all non-checker config options before analysis
In earlier patches regarding AnalyzerOptions, a lot of effort went into
gathering all config options, and changing the interface so that potential
misuse can be eliminited.
Up until this point, AnalyzerOptions only evaluated an option when it was
querried. For example, if we had a "-no-false-positives" flag, AnalyzerOptions
would store an Optional field for it that would be None up until somewhere in
the code until the flag's getter function is called.
However, now that we're confident that we've gathered all configs, we can
evaluate off of them before analysis, so we can emit a error on invalid input
even if that prticular flag will not matter in that particular run of the
analyzer. Another very big benefit of this is that debug.ConfigDumper will now
show the value of all configs every single time.
Also, almost all options related class have a similar interface, so uniformity
is also a benefit.
The implementation for errors on invalid input will be commited shorty.
Differential Revision: https://reviews.llvm.org/D53692
llvm-svn: 348031
2018-12-01 04:44:00 +08:00
|
|
|
assert(AMgr.getAnalyzerOptions().ShouldElideConstructors);
|
2018-06-28 08:30:18 +08:00
|
|
|
const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC);
|
|
|
|
const CXXBindTemporaryExpr *BTE = TCC->getCXXBindTemporaryExpr();
|
|
|
|
const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr();
|
|
|
|
const CXXConstructExpr *CE = TCC->getConstructorAfterElision();
|
|
|
|
|
|
|
|
// Support pre-C++17 copy elision. We'll have the elidable copy
|
|
|
|
// constructor in the AST and in the CFG, but we'll skip it
|
|
|
|
// and construct directly into the final object. This call
|
|
|
|
// also sets the CallOpts flags for us.
|
|
|
|
SVal V;
|
2018-08-01 04:45:53 +08:00
|
|
|
// If the elided copy/move constructor is not supported, there's still
|
|
|
|
// benefit in trying to model the non-elided constructor.
|
|
|
|
// Stash our state before trying to elide, as it'll get overwritten.
|
|
|
|
ProgramStateRef PreElideState = State;
|
|
|
|
EvalCallOptions PreElideCallOpts = CallOpts;
|
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
std::tie(State, V) = handleConstructionContext(
|
2018-06-28 08:30:18 +08:00
|
|
|
CE, State, LCtx, TCC->getConstructionContextAfterElision(), CallOpts);
|
|
|
|
|
2018-08-01 04:45:53 +08:00
|
|
|
// FIXME: This definition of "copy elision has not failed" is unreliable.
|
|
|
|
// It doesn't indicate that the constructor will actually be inlined
|
|
|
|
// later; it is still up to evalCall() to decide.
|
|
|
|
if (!CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion) {
|
|
|
|
// Remember that we've elided the constructor.
|
|
|
|
State = addObjectUnderConstruction(State, CE, LCtx, V);
|
2018-06-28 08:30:18 +08:00
|
|
|
|
2018-08-01 04:45:53 +08:00
|
|
|
// Remember that we've elided the destructor.
|
|
|
|
if (BTE)
|
|
|
|
State = elideDestructor(State, BTE, LCtx);
|
2018-06-28 08:30:18 +08:00
|
|
|
|
2018-08-01 04:45:53 +08:00
|
|
|
// Instead of materialization, shamelessly return
|
|
|
|
// the final object destination.
|
|
|
|
if (MTE)
|
|
|
|
State = addObjectUnderConstruction(State, MTE, LCtx, V);
|
2018-06-28 08:30:18 +08:00
|
|
|
|
2018-08-01 04:45:53 +08:00
|
|
|
return std::make_pair(State, V);
|
|
|
|
} else {
|
|
|
|
// Copy elision failed. Revert the changes and proceed as if we have
|
|
|
|
// a simple temporary.
|
|
|
|
State = PreElideState;
|
|
|
|
CallOpts = PreElideCallOpts;
|
|
|
|
}
|
2018-08-01 18:34:13 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2018-06-28 08:30:18 +08:00
|
|
|
}
|
2018-06-28 08:04:54 +08:00
|
|
|
case ConstructionContext::SimpleTemporaryObjectKind: {
|
2018-08-01 04:45:53 +08:00
|
|
|
const auto *TCC = cast<TemporaryObjectConstructionContext>(CC);
|
2018-06-14 09:20:12 +08:00
|
|
|
const CXXBindTemporaryExpr *BTE = TCC->getCXXBindTemporaryExpr();
|
|
|
|
const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr();
|
2018-06-28 08:18:52 +08:00
|
|
|
SVal V = UnknownVal();
|
2018-06-14 09:20:12 +08:00
|
|
|
|
|
|
|
if (MTE) {
|
|
|
|
if (const ValueDecl *VD = MTE->getExtendingDecl()) {
|
|
|
|
assert(MTE->getStorageDuration() != SD_FullExpression);
|
|
|
|
if (!VD->getType()->isReferenceType()) {
|
|
|
|
// We're lifetime-extended by a surrounding aggregate.
|
|
|
|
// Automatic destructors aren't quite working in this case
|
|
|
|
// on the CFG side. We should warn the caller about that.
|
|
|
|
// FIXME: Is there a better way to retrieve this information from
|
|
|
|
// the MaterializeTemporaryExpr?
|
|
|
|
CallOpts.IsTemporaryLifetimeExtendedViaAggregate = true;
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 08:11:42 +08:00
|
|
|
|
|
|
|
if (MTE->getStorageDuration() == SD_Static ||
|
|
|
|
MTE->getStorageDuration() == SD_Thread)
|
|
|
|
V = loc::MemRegionVal(MRMgr.getCXXStaticTempObjectRegion(E));
|
2018-06-14 09:20:12 +08:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:11:42 +08:00
|
|
|
if (V.isUnknown())
|
|
|
|
V = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
|
2018-06-14 09:20:12 +08:00
|
|
|
|
|
|
|
if (BTE)
|
|
|
|
State = addObjectUnderConstruction(State, BTE, LCtx, V);
|
|
|
|
|
|
|
|
if (MTE)
|
|
|
|
State = addObjectUnderConstruction(State, MTE, LCtx, V);
|
|
|
|
|
2018-02-28 04:03:35 +08:00
|
|
|
CallOpts.IsTemporaryCtorOrDtor = true;
|
2018-06-14 09:20:12 +08:00
|
|
|
return std::make_pair(State, V);
|
2018-02-28 04:03:35 +08:00
|
|
|
}
|
2018-08-01 04:45:53 +08:00
|
|
|
case ConstructionContext::ArgumentKind: {
|
2018-08-15 08:33:55 +08:00
|
|
|
// Arguments are technically temporaries.
|
|
|
|
CallOpts.IsTemporaryCtorOrDtor = true;
|
|
|
|
|
|
|
|
const auto *ACC = cast<ArgumentConstructionContext>(CC);
|
|
|
|
const Expr *E = ACC->getCallLikeExpr();
|
|
|
|
unsigned Idx = ACC->getIndex();
|
|
|
|
const CXXBindTemporaryExpr *BTE = ACC->getCXXBindTemporaryExpr();
|
|
|
|
|
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
SVal V = UnknownVal();
|
|
|
|
auto getArgLoc = [&](CallEventRef<> Caller) -> Optional<SVal> {
|
2019-08-02 04:41:13 +08:00
|
|
|
const LocationContext *FutureSFC =
|
|
|
|
Caller->getCalleeStackFrame(currBldrCtx->blockCount());
|
2018-08-15 08:33:55 +08:00
|
|
|
// Return early if we are unable to reliably foresee
|
|
|
|
// the future stack frame.
|
|
|
|
if (!FutureSFC)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
// This should be equivalent to Caller->getDecl() for now, but
|
|
|
|
// FutureSFC->getDecl() is likely to support better stuff (like
|
|
|
|
// virtual functions) earlier.
|
|
|
|
const Decl *CalleeD = FutureSFC->getDecl();
|
|
|
|
|
|
|
|
// FIXME: Support for variadic arguments is not implemented here yet.
|
|
|
|
if (CallEvent::isVariadic(CalleeD))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
// Operator arguments do not correspond to operator parameters
|
|
|
|
// because this-argument is implemented as a normal argument in
|
|
|
|
// operator call expressions but not in operator declarations.
|
|
|
|
const VarRegion *VR = Caller->getParameterLocation(
|
2019-08-02 04:41:13 +08:00
|
|
|
*Caller->getAdjustedParameterIndex(Idx), currBldrCtx->blockCount());
|
2018-08-15 08:33:55 +08:00
|
|
|
if (!VR)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
return loc::MemRegionVal(VR);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (const auto *CE = dyn_cast<CallExpr>(E)) {
|
|
|
|
CallEventRef<> Caller = CEMgr.getSimpleCall(CE, State, LCtx);
|
|
|
|
if (auto OptV = getArgLoc(Caller))
|
|
|
|
V = *OptV;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
State = addObjectUnderConstruction(State, {CE, Idx}, LCtx, V);
|
|
|
|
} else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) {
|
|
|
|
// Don't bother figuring out the target region for the future
|
|
|
|
// constructor because we won't need it.
|
|
|
|
CallEventRef<> Caller =
|
|
|
|
CEMgr.getCXXConstructorCall(CCE, /*Target=*/nullptr, State, LCtx);
|
|
|
|
if (auto OptV = getArgLoc(Caller))
|
|
|
|
V = *OptV;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
State = addObjectUnderConstruction(State, {CCE, Idx}, LCtx, V);
|
|
|
|
} else if (const auto *ME = dyn_cast<ObjCMessageExpr>(E)) {
|
|
|
|
CallEventRef<> Caller = CEMgr.getObjCMethodCall(ME, State, LCtx);
|
|
|
|
if (auto OptV = getArgLoc(Caller))
|
|
|
|
V = *OptV;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
State = addObjectUnderConstruction(State, {ME, Idx}, LCtx, V);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!V.isUnknown());
|
|
|
|
|
|
|
|
if (BTE)
|
|
|
|
State = addObjectUnderConstruction(State, BTE, LCtx, V);
|
|
|
|
|
|
|
|
return std::make_pair(State, V);
|
2018-08-01 04:45:53 +08:00
|
|
|
}
|
2018-02-28 04:03:35 +08:00
|
|
|
}
|
2014-04-02 00:40:06 +08:00
|
|
|
}
|
|
|
|
// If we couldn't find an existing region to construct into, assume we're
|
2018-02-02 06:17:05 +08:00
|
|
|
// constructing a temporary. Notify the caller of our failure.
|
2018-02-15 10:51:58 +08:00
|
|
|
CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
|
2018-06-14 09:20:12 +08:00
|
|
|
return std::make_pair(
|
|
|
|
State, loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)));
|
2014-04-02 00:40:06 +08:00
|
|
|
}
|
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
void ExprEngine::handleConstructor(const Expr *E,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &destNodes) {
|
|
|
|
const auto *CE = dyn_cast<CXXConstructExpr>(E);
|
|
|
|
const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(E);
|
|
|
|
assert(CE || CIE);
|
|
|
|
|
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();
|
|
|
|
|
2018-06-01 09:59:48 +08:00
|
|
|
SVal Target = UnknownVal();
|
2013-04-03 09:39:08 +08:00
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
if (CE) {
|
|
|
|
if (Optional<SVal> ElidedTarget =
|
|
|
|
getObjectUnderConstruction(State, CE, LCtx)) {
|
|
|
|
// We've previously modeled an elidable constructor by pretending that it
|
|
|
|
// in fact constructs into the correct target. This constructor can
|
|
|
|
// therefore be skipped.
|
|
|
|
Target = *ElidedTarget;
|
|
|
|
StmtNodeBuilder Bldr(Pred, destNodes, *currBldrCtx);
|
|
|
|
State = finishObjectConstruction(State, CE, LCtx);
|
|
|
|
if (auto L = Target.getAs<Loc>())
|
|
|
|
State = State->BindExpr(CE, LCtx, State->getSVal(*L, CE->getType()));
|
|
|
|
Bldr.generateNode(CE, Pred, State);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-28 08:30:18 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-02 06:17:05 +08:00
|
|
|
EvalCallOptions CallOpts;
|
2018-02-16 03:17:44 +08:00
|
|
|
auto C = getCurrentCFGElement().getAs<CFGConstructor>();
|
2018-02-28 04:03:35 +08:00
|
|
|
assert(C || getCurrentCFGElement().getAs<CFGStmt>());
|
2018-02-16 03:17:44 +08:00
|
|
|
const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr;
|
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
const CXXConstructExpr::ConstructionKind CK =
|
|
|
|
CE ? CE->getConstructionKind() : CIE->getConstructionKind();
|
|
|
|
switch (CK) {
|
2012-07-27 04:04:13 +08:00
|
|
|
case CXXConstructExpr::CK_Complete: {
|
2020-02-18 02:42:50 +08:00
|
|
|
// Inherited constructors are always base class constructors.
|
|
|
|
assert(CE && !CIE && "A complete constructor is inherited?!");
|
|
|
|
|
|
|
|
// The target region is found from construction context.
|
2018-06-14 09:20:12 +08:00
|
|
|
std::tie(State, Target) =
|
2020-02-18 02:42:50 +08:00
|
|
|
handleConstructionContext(CE, State, LCtx, CC, CallOpts);
|
2012-07-27 04:04:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-05-25 07:37:08 +08:00
|
|
|
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.
|
2019-05-25 07:37:08 +08:00
|
|
|
const auto *OuterCtor = dyn_cast_or_null<CXXConstructExpr>(
|
|
|
|
LCtx->getStackFrame()->getCallSite());
|
|
|
|
assert(
|
|
|
|
(!OuterCtor ||
|
|
|
|
OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Complete ||
|
|
|
|
OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Delegating) &&
|
|
|
|
("This virtual base should have already been initialized by "
|
|
|
|
"the most derived class!"));
|
|
|
|
(void)OuterCtor;
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2019-05-25 07:37:08 +08:00
|
|
|
}
|
2013-06-25 09:55:59 +08:00
|
|
|
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 08:40:38 +08: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.
|
2020-02-18 02:42:50 +08:00
|
|
|
if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(E))) {
|
[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 08:40:38 +08:00
|
|
|
MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
|
2020-02-18 02:42:50 +08:00
|
|
|
Target = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx));
|
2018-02-15 10:51:58 +08:00
|
|
|
CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
|
[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 08:40:38 +08:00
|
|
|
break;
|
|
|
|
}
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-07-27 04:04:13 +08:00
|
|
|
case CXXConstructExpr::CK_Delegating: {
|
|
|
|
const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
|
|
|
|
Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
|
2018-06-27 09:51:55 +08:00
|
|
|
LCtx->getStackFrame());
|
2012-07-27 04:04:16 +08:00
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
2012-07-27 04:04:13 +08:00
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
if (CK == CXXConstructExpr::CK_Delegating) {
|
2018-06-01 09:59:48 +08:00
|
|
|
Target = ThisVal;
|
2012-07-27 04:04:13 +08:00
|
|
|
} else {
|
|
|
|
// Cast to the base type.
|
2020-02-18 02:42:50 +08:00
|
|
|
bool IsVirtual = (CK == CXXConstructExpr::CK_VirtualBase);
|
|
|
|
SVal BaseVal =
|
|
|
|
getStoreManager().evalDerivedToBase(ThisVal, E->getType(), IsVirtual);
|
2018-06-01 09:59:48 +08:00
|
|
|
Target = BaseVal;
|
2012-07-27 04:04:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-14 09:20:12 +08:00
|
|
|
if (State != Pred->getState()) {
|
|
|
|
static SimpleProgramPointTag T("ExprEngine",
|
|
|
|
"Prepare for object construction");
|
|
|
|
ExplodedNodeSet DstPrepare;
|
|
|
|
StmtNodeBuilder BldrPrepare(Pred, DstPrepare, *currBldrCtx);
|
2020-02-18 02:42:50 +08:00
|
|
|
BldrPrepare.generateNode(E, Pred, State, &T, ProgramPoint::PreStmtKind);
|
2018-06-14 09:20:12 +08:00
|
|
|
assert(DstPrepare.size() <= 1);
|
|
|
|
if (DstPrepare.size() == 0)
|
|
|
|
return;
|
|
|
|
Pred = *BldrPrepare.begin();
|
|
|
|
}
|
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
const MemRegion *TargetRegion = Target.getAsRegion();
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
2020-02-18 02:42:50 +08:00
|
|
|
CallEventRef<> Call =
|
|
|
|
CIE ? (CallEventRef<>)CEMgr.getCXXInheritedConstructorCall(
|
|
|
|
CIE, TargetRegion, State, LCtx)
|
|
|
|
: (CallEventRef<>)CEMgr.getCXXConstructorCall(
|
|
|
|
CE, TargetRegion, State, LCtx);
|
2010-04-19 20:51:02 +08:00
|
|
|
|
2012-07-03 03:28:12 +08:00
|
|
|
ExplodedNodeSet DstPreVisit;
|
2020-02-18 02:42:50 +08:00
|
|
|
getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, E, *this);
|
2013-06-25 09:56:08 +08:00
|
|
|
|
|
|
|
ExplodedNodeSet PreInitialized;
|
2020-02-18 02:42:50 +08:00
|
|
|
if (CE) {
|
|
|
|
// FIXME: Is it possible and/or useful to do this before PreStmt?
|
2013-06-25 09:56:08 +08:00
|
|
|
StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
|
2018-02-16 03:17:44 +08:00
|
|
|
for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
|
|
|
|
E = DstPreVisit.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
ProgramStateRef State = (*I)->getState();
|
|
|
|
if (CE->requiresZeroInitialization()) {
|
2013-06-25 09:56:08 +08:00
|
|
|
// 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.
|
2018-06-01 09:59:48 +08:00
|
|
|
State = State->bindDefaultZero(Target, LCtx);
|
2013-06-25 09:56:08 +08:00
|
|
|
}
|
2018-02-16 03:17:44 +08:00
|
|
|
|
|
|
|
Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
|
|
|
|
ProgramPoint::PreStmtKind);
|
2013-06-25 09:56:08 +08:00
|
|
|
}
|
2020-02-18 02:42:50 +08:00
|
|
|
} else {
|
|
|
|
PreInitialized = DstPreVisit;
|
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);
|
|
|
|
|
2020-02-18 02:42:50 +08:00
|
|
|
if (CE && CE->getConstructor()->isTrivial() &&
|
2013-06-22 00:30:32 +08:00
|
|
|
CE->getConstructor()->isCopyOrMoveConstructor() &&
|
2018-02-15 10:51:58 +08:00
|
|
|
!CallOpts.IsArrayCtorOrDtor) {
|
2013-06-22 00:30:32 +08:00
|
|
|
// 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)
|
2018-02-02 06:17:05 +08:00
|
|
|
defaultEvalCall(Bldr, *I, *Call, CallOpts);
|
2013-02-15 08:32:15 +08:00
|
|
|
}
|
2011-04-09 06:42:35 +08:00
|
|
|
|
2018-04-06 23:14:32 +08:00
|
|
|
// If the CFG was constructed without elements for temporary destructors
|
2016-12-20 06:23:22 +08:00
|
|
|
// 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) {
|
2020-02-18 02:42:50 +08:00
|
|
|
if (TargetRegion && isa<CXXTempObjectRegion>(TargetRegion) &&
|
|
|
|
cast<CXXConstructorDecl>(Call->getDecl())
|
|
|
|
->getParent()->isAnyDestructorNoReturn()) {
|
2018-02-10 11:14:22 +08:00
|
|
|
|
|
|
|
// If we've inlined the constructor, then DstEvaluated would be empty.
|
|
|
|
// In this case we still want a sink, which could be implemented
|
|
|
|
// in processCallExit. But we don't have that implemented at the moment,
|
|
|
|
// so if you hit this assertion, see if you can avoid inlining
|
|
|
|
// the respective constructor when analyzer-config cfg-temporary-dtors
|
|
|
|
// is set to false.
|
|
|
|
// Otherwise there's nothing wrong with inlining such constructor.
|
|
|
|
assert(!DstEvaluated.empty() &&
|
|
|
|
"We should not have inlined this constructor!");
|
2016-12-20 06:23:22 +08:00
|
|
|
|
|
|
|
for (ExplodedNode *N : DstEvaluated) {
|
2020-02-18 02:42:50 +08:00
|
|
|
Bldr.generateSink(E, N, N->getState());
|
2016-12-20 06:23:22 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 11:14:22 +08:00
|
|
|
// There is no need to run the PostCall and PostStmt checker
|
2016-12-20 06:23:22 +08:00
|
|
|
// callbacks because we just generated sinks on all nodes in th
|
|
|
|
// frontier.
|
|
|
|
return;
|
|
|
|
}
|
2018-02-10 11:14:22 +08:00
|
|
|
}
|
2016-12-20 06:23:22 +08:00
|
|
|
|
2018-08-15 08:33:55 +08:00
|
|
|
ExplodedNodeSet DstPostArgumentCleanup;
|
|
|
|
for (auto I : DstEvaluated)
|
|
|
|
finishArgumentConstruction(DstPostArgumentCleanup, I, *Call);
|
|
|
|
|
|
|
|
// If there were other constructors called for object-type arguments
|
|
|
|
// of this constructor, clean them up.
|
2012-07-03 03:28:16 +08:00
|
|
|
ExplodedNodeSet DstPostCall;
|
2018-08-15 08:33:55 +08:00
|
|
|
getCheckerManager().runCheckersForPostCall(DstPostCall,
|
|
|
|
DstPostArgumentCleanup,
|
2012-07-31 04:22:09 +08:00
|
|
|
*Call, *this);
|
2020-02-18 02:42:50 +08:00
|
|
|
getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, E, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
handleConstructor(CE, Pred, Dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprEngine::VisitCXXInheritedCtorInitExpr(
|
|
|
|
const CXXInheritedCtorInitExpr *CE, ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
handleConstructor(CE, Pred, Dst);
|
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,
|
2018-02-15 10:51:58 +08:00
|
|
|
ExplodedNodeSet &Dst,
|
2019-08-21 05:41:17 +08:00
|
|
|
EvalCallOptions &CallOpts) {
|
2019-01-19 07:05:07 +08:00
|
|
|
assert(S && "A destructor without a trigger!");
|
2012-07-31 04:22:09 +08:00
|
|
|
const LocationContext *LCtx = Pred->getLocationContext();
|
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
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();
|
2019-01-19 07:05:07 +08:00
|
|
|
// FIXME: There should always be a Decl, otherwise the destructor call
|
|
|
|
// shouldn't have been added to the CFG in the first place.
|
|
|
|
if (!DtorDecl) {
|
|
|
|
// Skip the invalid destructor. We cannot simply return because
|
|
|
|
// it would interrupt the analysis instead.
|
|
|
|
static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
|
|
|
|
// FIXME: PostImplicitCall with a null decl may crash elsewhere anyway.
|
|
|
|
PostImplicitCall PP(/*Decl=*/nullptr, S->getEndLoc(), LCtx, &T);
|
|
|
|
NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
|
|
|
Bldr.generateNode(PP, Pred->getState(), Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-21 05:41:17 +08:00
|
|
|
if (!Dest) {
|
|
|
|
// We're trying to destroy something that is not a region. This may happen
|
|
|
|
// for a variety of reasons (unknown target region, concrete integer instead
|
|
|
|
// of target region, etc.). The current code makes an attempt to recover.
|
|
|
|
// FIXME: We probably don't really need to recover when we're dealing
|
|
|
|
// with concrete integers specifically.
|
|
|
|
CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true;
|
|
|
|
if (const Expr *E = dyn_cast_or_null<Expr>(S)) {
|
|
|
|
Dest = MRMgr.getCXXTempObjectRegion(E, Pred->getLocationContext());
|
|
|
|
} else {
|
|
|
|
static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
|
|
|
|
NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
|
|
|
Bldr.generateSink(Pred->getLocation().withTag(&T),
|
|
|
|
Pred->getState(), Pred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXDestructorCall> Call =
|
2019-08-21 05:41:17 +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)
|
2018-02-02 06:17:05 +08:00
|
|
|
defaultEvalCall(Bldr, *I, *Call, CallOpts);
|
2012-07-11 06:07:47 +08:00
|
|
|
|
|
|
|
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(),
|
2018-08-10 05:05:56 +08:00
|
|
|
CNE->getBeginLoc(),
|
2014-02-11 10:21:06 +08:00
|
|
|
"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-18 06:34:23 +08:00
|
|
|
ExplodedNodeSet DstPostCall;
|
|
|
|
StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx);
|
2018-01-18 06:58:35 +08:00
|
|
|
for (auto I : DstPreCall) {
|
|
|
|
// FIXME: Provide evalCall for checkers?
|
2018-01-18 06:34:23 +08:00
|
|
|
defaultEvalCall(CallBldr, I, *Call);
|
2018-01-18 06:58:35 +08:00
|
|
|
}
|
2018-01-18 06:51:19 +08:00
|
|
|
// If the call is inlined, DstPostCall will be empty and we bail out now.
|
2018-01-18 06:34:23 +08: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-18 06:51:19 +08: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-18 06:34:23 +08:00
|
|
|
ProgramStateRef State = I->getState();
|
2018-01-25 04:32:26 +08: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>())
|
2018-05-03 11:58:32 +08:00
|
|
|
if (!ProtoType->isNothrow())
|
2018-01-25 04:32:26 +08:00
|
|
|
State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
|
|
|
|
}
|
|
|
|
|
2018-06-01 09:59:48 +08:00
|
|
|
ValueBldr.generateNode(
|
|
|
|
CNE, I, addObjectUnderConstruction(State, CNE, LCtx, RetVal));
|
2018-01-18 06:34:23 +08:00
|
|
|
}
|
|
|
|
|
2018-01-18 07:46:13 +08:00
|
|
|
ExplodedNodeSet DstPostPostCallCallback;
|
|
|
|
getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
|
|
|
|
DstPostValue, *Call, *this);
|
|
|
|
for (auto I : DstPostPostCallCallback) {
|
|
|
|
getCheckerManager().runCheckersForNewAllocator(
|
2018-06-01 09:59:48 +08:00
|
|
|
CNE, *getObjectUnderConstruction(I->getState(), CNE, LCtx), Dst, I,
|
|
|
|
*this);
|
2018-01-18 07:46:13 +08:00
|
|
|
}
|
2014-02-11 10:21:06 +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.
|
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();
|
2018-01-18 06:34:23 +08:00
|
|
|
SVal symVal = UnknownVal();
|
2013-03-29 00:10:38 +08:00
|
|
|
FunctionDecl *FD = CNE->getOperatorNew();
|
|
|
|
|
2018-01-18 06:58:35 +08:00
|
|
|
bool IsStandardGlobalOpNewFunction =
|
|
|
|
FD->isReplaceableGlobalAllocationFunction();
|
2013-03-29 00:10:38 +08:00
|
|
|
|
2018-01-18 06:34:23 +08:00
|
|
|
ProgramStateRef State = Pred->getState();
|
|
|
|
|
|
|
|
// Retrieve the stored operator new() return value.
|
[analyzer] Evaluate all non-checker config options before analysis
In earlier patches regarding AnalyzerOptions, a lot of effort went into
gathering all config options, and changing the interface so that potential
misuse can be eliminited.
Up until this point, AnalyzerOptions only evaluated an option when it was
querried. For example, if we had a "-no-false-positives" flag, AnalyzerOptions
would store an Optional field for it that would be None up until somewhere in
the code until the flag's getter function is called.
However, now that we're confident that we've gathered all configs, we can
evaluate off of them before analysis, so we can emit a error on invalid input
even if that prticular flag will not matter in that particular run of the
analyzer. Another very big benefit of this is that debug.ConfigDumper will now
show the value of all configs every single time.
Also, almost all options related class have a similar interface, so uniformity
is also a benefit.
The implementation for errors on invalid input will be commited shorty.
Differential Revision: https://reviews.llvm.org/D53692
llvm-svn: 348031
2018-12-01 04:44:00 +08:00
|
|
|
if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
|
2018-06-01 09:59:48 +08:00
|
|
|
symVal = *getObjectUnderConstruction(State, CNE, LCtx);
|
|
|
|
State = finishObjectConstruction(State, CNE, LCtx);
|
2018-01-18 06:34:23 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2018-01-18 06:34:23 +08: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 12:04:48 +08:00
|
|
|
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager &CEMgr = getStateManager().getCallEventManager();
|
|
|
|
CallEventRef<CXXAllocatorCall> Call =
|
|
|
|
CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
|
|
|
|
|
[analyzer] Evaluate all non-checker config options before analysis
In earlier patches regarding AnalyzerOptions, a lot of effort went into
gathering all config options, and changing the interface so that potential
misuse can be eliminited.
Up until this point, AnalyzerOptions only evaluated an option when it was
querried. For example, if we had a "-no-false-positives" flag, AnalyzerOptions
would store an Optional field for it that would be None up until somewhere in
the code until the flag's getter function is called.
However, now that we're confident that we've gathered all configs, we can
evaluate off of them before analysis, so we can emit a error on invalid input
even if that prticular flag will not matter in that particular run of the
analyzer. Another very big benefit of this is that debug.ConfigDumper will now
show the value of all configs every single time.
Also, almost all options related class have a similar interface, so uniformity
is also a benefit.
The implementation for errors on invalid input will be commited shorty.
Differential Revision: https://reviews.llvm.org/D53692
llvm-svn: 348031
2018-12-01 04:44:00 +08:00
|
|
|
if (!AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
|
2018-01-18 06:34:23 +08:00
|
|
|
// Invalidate placement args.
|
|
|
|
// FIXME: Once we figure out how we want allocators to work,
|
2019-09-13 03:09:24 +08:00
|
|
|
// we should be using the usual pre-/(default-)eval-/post-call checkers
|
|
|
|
// here.
|
2018-01-18 06:34:23 +08:00
|
|
|
State = Call->invalidateRegions(blockCount);
|
|
|
|
if (!State)
|
|
|
|
return;
|
2012-07-03 06:21:47 +08:00
|
|
|
|
2018-01-25 04:32:26 +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.
|
|
|
|
// C++11 [basic.stc.dynamic.allocation]p3.
|
|
|
|
if (FD) {
|
|
|
|
QualType Ty = FD->getType();
|
|
|
|
if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
|
2018-05-03 11:58:32 +08:00
|
|
|
if (!ProtoType->isNothrow())
|
2018-01-25 04:32:26 +08:00
|
|
|
if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
|
|
|
|
State = State->assume(*dSymVal, true);
|
|
|
|
}
|
2012-10-20 10:32:51 +08:00
|
|
|
}
|
|
|
|
|
2013-03-30 09:31:48 +08:00
|
|
|
StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
|
|
|
|
|
2018-01-18 06:51:19 +08:00
|
|
|
SVal Result = symVal;
|
|
|
|
|
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.
|
2019-08-29 02:44:38 +08:00
|
|
|
if (const auto *NewReg = cast_or_null<SubRegion>(symVal.getAsRegion())) {
|
|
|
|
QualType ObjTy = CNE->getType()->getPointeeType();
|
2018-01-18 06:51:19 +08:00
|
|
|
const ElementRegion *EleReg =
|
|
|
|
getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
|
|
|
|
Result = loc::MemRegionVal(EleReg);
|
|
|
|
}
|
|
|
|
State = State->BindExpr(CNE, Pred->getLocationContext(), Result);
|
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.)
|
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();
|
2017-01-13 08:50:57 +08:00
|
|
|
state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
|
2012-03-10 09:34:17 +08:00
|
|
|
|
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);
|
2016-09-01 19:11:46 +08:00
|
|
|
|
2015-09-12 00:55:01 +08:00
|
|
|
ProgramStateRef State = Pred->getState();
|
2016-09-01 19:11:46 +08:00
|
|
|
|
2015-09-12 00:55:01 +08: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-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);
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:57 +08:00
|
|
|
State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
|
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);
|
|
|
|
}
|