#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/ParentMap.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/Type.h"
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/CFGStmtMap.h"
#include "clang/Analysis/PathDiagnostic.h"
#include "clang/Analysis/ProgramPoint.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/Specifiers.h"
#include "clang/CrossTU/CrossTranslationUnit.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeInfo.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ImmutableList.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <utility>
#define DEBUG_TYPE "static-analyzer-call-event"
using namespace clang;
using namespace ento;
QualType CallEvent::getResultType() const {
ASTContext &Ctx = getState()->getStateManager().getContext();
const Expr *E = getOriginExpr();
if (!E)
return Ctx.VoidTy;
return Ctx.getReferenceQualifiedType(E);
}
static bool isCallback(QualType T) {
if (T->isBlockPointerType() ||
T->isFunctionPointerType() ||
T->isObjCSelType())
return true;
if (T->isAnyPointerType() || T->isReferenceType())
T = T->getPointeeType();
if (const RecordType *RT = T->getAsStructureType()) {
const RecordDecl *RD = RT->getDecl();
for (const auto *I : RD->fields()) {
QualType FieldT = I->getType();
if (FieldT->isBlockPointerType() || FieldT->isFunctionPointerType())
return true;
}
}
return false;
}
static bool isVoidPointerToNonConst(QualType T) {
if (const auto *PT = T->getAs<PointerType>()) {
QualType PointeeTy = PT->getPointeeType();
if (PointeeTy.isConstQualified())
return false;
return PointeeTy->isVoidType();
} else
return false;
}
bool CallEvent::hasNonNullArgumentsWithType(bool (*Condition)(QualType)) const {
unsigned NumOfArgs = getNumArgs();
if (!getDecl())
return false;
unsigned Idx = 0;
for (CallEvent::param_type_iterator I = param_type_begin(),
E = param_type_end();
I != E && Idx < NumOfArgs; ++I, ++Idx) {
if (getArgSVal(Idx).isZeroConstant())
continue;
if (Condition(*I))
return true;
}
return false;
}
bool CallEvent::hasNonZeroCallbackArg() const {
return hasNonNullArgumentsWithType(isCallback);
}
bool CallEvent::hasVoidPointerToNonConstArg() const {
return hasNonNullArgumentsWithType(isVoidPointerToNonConst);
}
bool CallEvent::isGlobalCFunction(StringRef FunctionName) const {
const auto *FD = dyn_cast_or_null<FunctionDecl>(getDecl());
if (!FD)
return false;
return CheckerContext::isCLibraryFunction(FD, FunctionName);
}
AnalysisDeclContext *CallEvent::getCalleeAnalysisDeclContext() const {
const Decl *D = getDecl();
if (!D)
return nullptr;
AnalysisDeclContext *ADC =
LCtx->getAnalysisDeclContext()->getManager()->getContext(D);
return ADC;
}
const StackFrameContext *
CallEvent::getCalleeStackFrame(unsigned BlockCount) const {
AnalysisDeclContext *ADC = getCalleeAnalysisDeclContext();
if (!ADC)
return nullptr;
const Expr *E = getOriginExpr();
if (!E)
return nullptr;
CFGStmtMap *Map = LCtx->getAnalysisDeclContext()->getCFGStmtMap();
const CFGBlock *B = Map->getBlock(E);
assert(B);
unsigned Idx = 0, Sz = B->size();
for (; Idx < Sz; ++Idx)
if (auto StmtElem = (*B)[Idx].getAs<CFGStmt>())
if (StmtElem->getStmt() == E)
break;
assert(Idx < Sz);
return ADC->getManager()->getStackFrame(ADC, LCtx, E, B, BlockCount, Idx);
}
const ParamVarRegion
*CallEvent::getParameterLocation(unsigned Index, unsigned BlockCount) const {
const StackFrameContext *SFC = getCalleeStackFrame(BlockCount);
if (!SFC)
return nullptr;
const ParamVarRegion *PVR =
State->getStateManager().getRegionManager().getParamVarRegion(
getOriginExpr(), Index, SFC);
return PVR;
}
static bool isPointerToConst(QualType Ty) {
QualType PointeeTy = Ty->getPointeeType();
if (PointeeTy == QualType())
return false;
if (!PointeeTy.isConstQualified())
return false;
if (PointeeTy->isAnyPointerType())
return false;
return true;
}
static void findPtrToConstParams(llvm::SmallSet<unsigned, 4> &PreserveArgs,
const CallEvent &Call) {
unsigned Idx = 0;
for (CallEvent::param_type_iterator I = Call.param_type_begin(),
E = Call.param_type_end();
I != E; ++I, ++Idx) {
if (isPointerToConst(*I))
PreserveArgs.insert(Idx);
}
}
ProgramStateRef CallEvent::invalidateRegions(unsigned BlockCount,
ProgramStateRef Orig) const {
ProgramStateRef Result = (Orig ? Orig : getState());
if (const Decl *callee = getDecl())
if (callee->hasAttr<PureAttr>() || callee->hasAttr<ConstAttr>())
return Result;
SmallVector<SVal, 8> ValuesToInvalidate;
RegionAndSymbolInvalidationTraits ETraits;
getExtraInvalidatedValues(ValuesToInvalidate, &ETraits);
llvm::SmallSet<unsigned, 4> PreserveArgs;
if (!argumentsMayEscape())
findPtrToConstParams(PreserveArgs, *this);
for (unsigned Idx = 0, Count = getNumArgs(); Idx != Count; ++Idx) {
if (PreserveArgs.count(Idx))
if (const MemRegion *MR = getArgSVal(Idx).getAsRegion())
ETraits.setTrait(MR->getBaseRegion(),
RegionAndSymbolInvalidationTraits::TK_PreserveContents);
ValuesToInvalidate.push_back(getArgSVal(Idx));
if (getKind() != CE_CXXAllocator)
if (isArgumentConstructedDirectly(Idx))
if (auto AdjIdx = getAdjustedParameterIndex(Idx))
if (const TypedValueRegion *TVR =
getParameterLocation(*AdjIdx, BlockCount))
ValuesToInvalidate.push_back(loc::MemRegionVal(TVR));
}
return Result->invalidateRegions(ValuesToInvalidate, getOriginExpr(),
BlockCount, getLocationContext(),
true,
nullptr, this, &ETraits);
}
ProgramPoint CallEvent::getProgramPoint(bool IsPreVisit,
const ProgramPointTag *Tag) const {
if (const Expr *E = getOriginExpr()) {
if (IsPreVisit)
return PreStmt(E, getLocationContext(), Tag);
return PostStmt(E, getLocationContext(), Tag);
}
const Decl *D = getDecl();
assert(D && "Cannot get a program point without a statement or decl");
SourceLocation Loc = getSourceRange().getBegin();
if (IsPreVisit)
return PreImplicitCall(D, Loc, getLocationContext(), Tag);
return PostImplicitCall(D, Loc, getLocationContext(), Tag);
}
SVal CallEvent::getArgSVal(unsigned Index) const {
const Expr *ArgE = getArgExpr(Index);
if (!ArgE)
return UnknownVal();
return getSVal(ArgE);
}
SourceRange CallEvent::getArgSourceRange(unsigned Index) const {
const Expr *ArgE = getArgExpr(Index);
if (!ArgE)
return {};
return ArgE->getSourceRange();
}
SVal CallEvent::getReturnValue() const {
const Expr *E = getOriginExpr();
if (!E)
return UndefinedVal();
return getSVal(E);
}
LLVM_DUMP_METHOD void CallEvent::dump() const { dump(llvm::errs()); }
void CallEvent::dump(raw_ostream &Out) const {
ASTContext &Ctx = getState()->getStateManager().getContext();
if (const Expr *E = getOriginExpr()) {
E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
return;
}
if (const Decl *D = getDecl()) {
Out << "Call to ";
D->print(Out, Ctx.getPrintingPolicy());
return;
}
Out << "Unknown call (type " << getKindAsString() << ")";
}
bool CallEvent::isCallStmt(const Stmt *S) {
return isa<CallExpr, ObjCMessageExpr, CXXConstructExpr, CXXNewExpr>(S);
}
QualType CallEvent::getDeclaredResultType(const Decl *D) {
assert(D);
if (const auto *FD = dyn_cast<FunctionDecl>(D))
return FD->getReturnType();
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
return MD->getReturnType();
if (const auto *BD = dyn_cast<BlockDecl>(D)) {
if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten()) {
QualType Ty = TSI->getType();
if (const FunctionType *FT = Ty->getAs<FunctionType>())
Ty = FT->getReturnType();
if (!Ty->isDependentType())
return Ty;
}
return {};
}
llvm_unreachable("unknown callable kind");
}
bool CallEvent::isVariadic(const Decl *D) {
assert(D);
if (const auto *FD = dyn_cast<FunctionDecl>(D))
return FD->isVariadic();
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
return MD->isVariadic();
if (const auto *BD = dyn_cast<BlockDecl>(D))
return BD->isVariadic();
llvm_unreachable("unknown callable kind");
}
static bool isTransparentUnion(QualType T) {
const RecordType *UT = T->getAsUnionType();
return UT && UT->getDecl()->hasAttr<TransparentUnionAttr>();
}
static SVal processArgument(SVal Value, const Expr *ArgumentExpr,
const ParmVarDecl *Parameter, SValBuilder &SVB) {
QualType ParamType = Parameter->getType();
QualType ArgumentType = ArgumentExpr->getType();
if (isTransparentUnion(ParamType) &&
!isTransparentUnion(ArgumentType)) {
BasicValueFactory &BVF = SVB.getBasicValueFactory();
llvm::ImmutableList<SVal> CompoundSVals = BVF.getEmptySValList();
CompoundSVals = BVF.prependSVal(Value, CompoundSVals);
return SVB.makeCompoundVal(ParamType, CompoundSVals);
}
return Value;
}
static void addParameterValuesToBindings(const StackFrameContext *CalleeCtx,
CallEvent::BindingsTy &Bindings,
SValBuilder &SVB,
const CallEvent &Call,
ArrayRef<ParmVarDecl*> parameters) {
MemRegionManager &MRMgr = SVB.getRegionManager();
unsigned NumArgs = Call.getNumArgs();
unsigned Idx = 0;
ArrayRef<ParmVarDecl*>::iterator I = parameters.begin(), E = parameters.end();
for (; I != E && Idx < NumArgs; ++I, ++Idx) {
assert(*I && "Formal parameter has no decl?");
if (Call.getKind() != CE_CXXAllocator)
if (Call.isArgumentConstructedDirectly(Call.getASTArgumentIndex(Idx)))
continue;
SVal ArgVal = Call.getArgSVal(Idx);
const Expr *ArgExpr = Call.getArgExpr(Idx);
if (!ArgVal.isUnknown()) {
Loc ParamLoc = SVB.makeLoc(
MRMgr.getParamVarRegion(Call.getOriginExpr(), Idx, CalleeCtx));
Bindings.push_back(
std::make_pair(ParamLoc, processArgument(ArgVal, ArgExpr, *I, SVB)));
}
}
}
const ConstructionContext *CallEvent::getConstructionContext() const {
const StackFrameContext *StackFrame = getCalleeStackFrame(0);
if (!StackFrame)
return nullptr;
const CFGElement Element = StackFrame->getCallSiteCFGElement();
if (const auto Ctor = Element.getAs<CFGConstructor>()) {
return Ctor->getConstructionContext();
}
if (const auto RecCall = Element.getAs<CFGCXXRecordTypedCall>()) {
return RecCall->getConstructionContext();
}
return nullptr;
}
Optional<SVal>
CallEvent::getReturnValueUnderConstruction() const {
const auto *CC = getConstructionContext();
if (!CC)
return None;
EvalCallOptions CallOpts;
ExprEngine &Engine = getState()->getStateManager().getOwningEngine();
SVal RetVal =
Engine.computeObjectUnderConstruction(getOriginExpr(), getState(),
getLocationContext(), CC, CallOpts);
return RetVal;
}
ArrayRef<ParmVarDecl*> AnyFunctionCall::parameters() const {
const FunctionDecl *D = getDecl();
if (!D)
return None;
return D->parameters();
}
RuntimeDefinition AnyFunctionCall::getRuntimeDefinition() const {
const FunctionDecl *FD = getDecl();
if (!FD)
return {};
AnalysisDeclContext *AD =
getLocationContext()->getAnalysisDeclContext()->
getManager()->getContext(FD);
bool IsAutosynthesized;
Stmt* Body = AD->getBody(IsAutosynthesized);
LLVM_DEBUG({
if (IsAutosynthesized)
llvm::dbgs() << "Using autosynthesized body for " << FD->getName()
<< "\n";
});
ExprEngine &Engine = getState()->getStateManager().getOwningEngine();
cross_tu::CrossTranslationUnitContext &CTUCtx =
*Engine.getCrossTranslationUnitContext();
AnalyzerOptions &Opts = Engine.getAnalysisManager().options;
if (Body) {
const Decl* Decl = AD->getDecl();
if (Opts.IsNaiveCTUEnabled && CTUCtx.isImportedAsNew(Decl)) {
if (CTUCtx.hasError(Decl))
return {};
return RuntimeDefinition(Decl, true);
}
return RuntimeDefinition(Decl, false);
}
if (!Opts.IsNaiveCTUEnabled)
return {};
llvm::Expected<const FunctionDecl *> CTUDeclOrError =
CTUCtx.getCrossTUDefinition(FD, Opts.CTUDir, Opts.CTUIndexName,
Opts.DisplayCTUProgress);
if (!CTUDeclOrError) {
handleAllErrors(CTUDeclOrError.takeError(),
[&](const cross_tu::IndexError &IE) {
CTUCtx.emitCrossTUDiagnostics(IE);
});
return {};
}
return RuntimeDefinition(*CTUDeclOrError, true);
}
void AnyFunctionCall::getInitialStackFrameContents(
const StackFrameContext *CalleeCtx,
BindingsTy &Bindings) const {
const auto *D = cast<FunctionDecl>(CalleeCtx->getDecl());
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
addParameterValuesToBindings(CalleeCtx, Bindings, SVB, *this,
D->parameters());
}
bool AnyFunctionCall::argumentsMayEscape() const {
if (CallEvent::argumentsMayEscape() || hasVoidPointerToNonConstArg())
return true;
const FunctionDecl *D = getDecl();
if (!D)
return true;
const IdentifierInfo *II = D->getIdentifier();
if (!II)
return false;
if (II->isStr("pthread_setspecific"))
return true;
if (II->isStr("xpc_connection_set_context"))
return true;
if (II->isStr("funopen"))
return true;
if (II->isStr("__cxa_demangle"))
return true;
StringRef FName = II->getName();
if (FName.endswith("NoCopy"))
return true;
if (FName.startswith("NS") && FName.contains("Insert"))
return true;
if (FName.startswith("CF") || FName.startswith("CG")) {
return StrInStrNoCase(FName, "InsertValue") != StringRef::npos ||
StrInStrNoCase(FName, "AddValue") != StringRef::npos ||
StrInStrNoCase(FName, "SetValue") != StringRef::npos ||
StrInStrNoCase(FName, "WithData") != StringRef::npos ||
StrInStrNoCase(FName, "AppendValue") != StringRef::npos ||
StrInStrNoCase(FName, "SetAttribute") != StringRef::npos;
}
return false;
}
const FunctionDecl *SimpleFunctionCall::getDecl() const {
const FunctionDecl *D = getOriginExpr()->getDirectCallee();
if (D)
return D;
return getSVal(getOriginExpr()->getCallee()).getAsFunctionDecl();
}
const FunctionDecl *CXXInstanceCall::getDecl() const {
const auto *CE = cast_or_null<CallExpr>(getOriginExpr());
if (!CE)
return AnyFunctionCall::getDecl();
const FunctionDecl *D = CE->getDirectCallee();
if (D)
return D;
return getSVal(CE->getCallee()).getAsFunctionDecl();
}
void CXXInstanceCall::getExtraInvalidatedValues(
ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const {
SVal ThisVal = getCXXThisVal();
Values.push_back(ThisVal);
if (const auto *D = cast_or_null<CXXMethodDecl>(getDecl())) {
if (!D->isConst())
return;
const Expr *Ex = getCXXThisExpr()->IgnoreParenBaseCasts();
QualType T = Ex->getType();
if (T->isPointerType()) T = T->getPointeeType();
const CXXRecordDecl *ParentRecord = T->getAsCXXRecordDecl();
assert(ParentRecord);
if (ParentRecord->hasMutableFields())
return;
const MemRegion *ThisRegion = ThisVal.getAsRegion();
if (!ThisRegion)
return;
ETraits->setTrait(ThisRegion->getBaseRegion(),
RegionAndSymbolInvalidationTraits::TK_PreserveContents);
}
}
SVal CXXInstanceCall::getCXXThisVal() const {
const Expr *Base = getCXXThisExpr();
if (!Base)
return UnknownVal();
SVal ThisVal = getSVal(Base);
assert(ThisVal.isUnknownOrUndef() || isa<Loc>(ThisVal));
return ThisVal;
}
RuntimeDefinition CXXInstanceCall::getRuntimeDefinition() const {
const Decl *D = getDecl();
if (!D)
return {};
const auto *MD = cast<CXXMethodDecl>(D);
if (!MD->isVirtual())
return AnyFunctionCall::getRuntimeDefinition();
const MemRegion *R = getCXXThisVal().getAsRegion();
if (!R)
return {};
DynamicTypeInfo DynType = getDynamicTypeInfo(getState(), R);
if (!DynType.isValid())
return {};
QualType RegionType = DynType.getType()->getPointeeType();
assert(!RegionType.isNull() && "DynamicTypeInfo should always be a pointer.");
const CXXRecordDecl *RD = RegionType->getAsCXXRecordDecl();
if (!RD || !RD->hasDefinition())
return {};
const CXXMethodDecl *Result = MD->getCorrespondingMethodInClass(RD, true);
if (!Result) {
assert(!RD->isDerivedFrom(MD->getParent()) && "Couldn't find known method");
return {};
}
const FunctionDecl *Definition;
if (!Result->hasBody(Definition)) {
if (!DynType.canBeASubClass())
return AnyFunctionCall::getRuntimeDefinition();
return {};
}
if (DynType.canBeASubClass())
return RuntimeDefinition(Definition, R->StripCasts());
return RuntimeDefinition(Definition, nullptr);
}
void CXXInstanceCall::getInitialStackFrameContents(
const StackFrameContext *CalleeCtx,
BindingsTy &Bindings) const {
AnyFunctionCall::getInitialStackFrameContents(CalleeCtx, Bindings);
SVal ThisVal = getCXXThisVal();
if (!ThisVal.isUnknown()) {
ProgramStateManager &StateMgr = getState()->getStateManager();
SValBuilder &SVB = StateMgr.getSValBuilder();
const auto *MD = cast<CXXMethodDecl>(CalleeCtx->getDecl());
Loc ThisLoc = SVB.getCXXThis(MD, CalleeCtx);
if (MD->getCanonicalDecl() != getDecl()->getCanonicalDecl()) {
ASTContext &Ctx = SVB.getContext();
const CXXRecordDecl *Class = MD->getParent();
QualType Ty = Ctx.getPointerType(Ctx.getRecordType(Class));
Optional<SVal> V =
StateMgr.getStoreManager().evalBaseToDerived(ThisVal, Ty);
if (!V) {
const CXXMethodDecl *StaticMD = cast<CXXMethodDecl>(getDecl());
const CXXRecordDecl *StaticClass = StaticMD->getParent();
QualType StaticTy = Ctx.getPointerType(Ctx.getRecordType(StaticClass));
ThisVal = SVB.evalCast(ThisVal, Ty, StaticTy);
} else
ThisVal = *V;
}
if (!ThisVal.isUnknown())
Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
}
}
const Expr *CXXMemberCall::getCXXThisExpr() const {
return getOriginExpr()->getImplicitObjectArgument();
}
RuntimeDefinition CXXMemberCall::getRuntimeDefinition() const {
if (const auto *ME = dyn_cast<MemberExpr>(getOriginExpr()->getCallee()))
if (ME->hasQualifier())
return AnyFunctionCall::getRuntimeDefinition();
return CXXInstanceCall::getRuntimeDefinition();
}
const Expr *CXXMemberOperatorCall::getCXXThisExpr() const {
return getOriginExpr()->getArg(0);
}
const BlockDataRegion *BlockCall::getBlockRegion() const {
const Expr *Callee = getOriginExpr()->getCallee();
const MemRegion *DataReg = getSVal(Callee).getAsRegion();
return dyn_cast_or_null<BlockDataRegion>(DataReg);
}
ArrayRef<ParmVarDecl*> BlockCall::parameters() const {
const BlockDecl *D = getDecl();
if (!D)
return None;
return D->parameters();
}
void BlockCall::getExtraInvalidatedValues(ValueList &Values,
RegionAndSymbolInvalidationTraits *ETraits) const {
if (const MemRegion *R = getBlockRegion())
Values.push_back(loc::MemRegionVal(R));
}
void BlockCall::getInitialStackFrameContents(const StackFrameContext *CalleeCtx,
BindingsTy &Bindings) const {
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
ArrayRef<ParmVarDecl*> Params;
if (isConversionFromLambda()) {
auto *LambdaOperatorDecl = cast<CXXMethodDecl>(CalleeCtx->getDecl());
Params = LambdaOperatorDecl->parameters();
const VarRegion *CapturedLambdaRegion = getRegionStoringCapturedLambda();
SVal ThisVal = loc::MemRegionVal(CapturedLambdaRegion);
Loc ThisLoc = SVB.getCXXThis(LambdaOperatorDecl, CalleeCtx);
Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
} else {
Params = cast<BlockDecl>(CalleeCtx->getDecl())->parameters();
}
addParameterValuesToBindings(CalleeCtx, Bindings, SVB, *this,
Params);
}
SVal AnyCXXConstructorCall::getCXXThisVal() const {
if (Data)
return loc::MemRegionVal(static_cast<const MemRegion *>(Data));
return UnknownVal();
}
void AnyCXXConstructorCall::getExtraInvalidatedValues(ValueList &Values,
RegionAndSymbolInvalidationTraits *ETraits) const {
SVal V = getCXXThisVal();
if (SymbolRef Sym = V.getAsSymbol(true))
ETraits->setTrait(Sym,
RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
Values.push_back(V);
}
void AnyCXXConstructorCall::getInitialStackFrameContents(
const StackFrameContext *CalleeCtx,
BindingsTy &Bindings) const {
AnyFunctionCall::getInitialStackFrameContents(CalleeCtx, Bindings);
SVal ThisVal = getCXXThisVal();
if (!ThisVal.isUnknown()) {
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
const auto *MD = cast<CXXMethodDecl>(CalleeCtx->getDecl());
Loc ThisLoc = SVB.getCXXThis(MD, CalleeCtx);
Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
}
}
const StackFrameContext *
CXXInheritedConstructorCall::getInheritingStackFrame() const {
const StackFrameContext *SFC = getLocationContext()->getStackFrame();
while (isa<CXXInheritedCtorInitExpr>(SFC->getCallSite()))
SFC = SFC->getParent()->getStackFrame();
return SFC;
}
SVal CXXDestructorCall::getCXXThisVal() const {
if (Data)
return loc::MemRegionVal(DtorDataTy::getFromOpaqueValue(Data).getPointer());
return UnknownVal();
}
RuntimeDefinition CXXDestructorCall::getRuntimeDefinition() const {
if (isBaseDestructor())
return AnyFunctionCall::getRuntimeDefinition();
return CXXInstanceCall::getRuntimeDefinition();
}
ArrayRef<ParmVarDecl*> ObjCMethodCall::parameters() const {
const ObjCMethodDecl *D = getDecl();
if (!D)
return None;
return D->parameters();
}
void ObjCMethodCall::getExtraInvalidatedValues(
ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const {
if (const ObjCPropertyDecl *PropDecl = getAccessedProperty()) {
if (const ObjCIvarDecl *PropIvar = PropDecl->getPropertyIvarDecl()) {
SVal IvarLVal = getState()->getLValue(PropIvar, getReceiverSVal());
if (const MemRegion *IvarRegion = IvarLVal.getAsRegion()) {
ETraits->setTrait(
IvarRegion,
RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
ETraits->setTrait(
IvarRegion,
RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
Values.push_back(IvarLVal);
}
return;
}
}
Values.push_back(getReceiverSVal());
}
SVal ObjCMethodCall::getReceiverSVal() const {
if (!isInstanceMessage())
return UnknownVal();
if (const Expr *RecE = getOriginExpr()->getInstanceReceiver())
return getSVal(RecE);
assert(getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperInstance);
SVal SelfVal = getState()->getSelfSVal(getLocationContext());
assert(SelfVal.isValid() && "Calling super but not in ObjC method");
return SelfVal;
}
bool ObjCMethodCall::isReceiverSelfOrSuper() const {
if (getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperInstance ||
getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperClass)
return true;
if (!isInstanceMessage())
return false;
SVal RecVal = getSVal(getOriginExpr()->getInstanceReceiver());
SVal SelfVal = getState()->getSelfSVal(getLocationContext());
return (RecVal == SelfVal);
}
SourceRange ObjCMethodCall::getSourceRange() const {
switch (getMessageKind()) {
case OCM_Message:
return getOriginExpr()->getSourceRange();
case OCM_PropertyAccess:
case OCM_Subscript:
return getContainingPseudoObjectExpr()->getSourceRange();
}
llvm_unreachable("unknown message kind");
}
using ObjCMessageDataTy = llvm::PointerIntPair<const PseudoObjectExpr *, 2>;
const PseudoObjectExpr *ObjCMethodCall::getContainingPseudoObjectExpr() const {
assert(Data && "Lazy lookup not yet performed.");
assert(getMessageKind() != OCM_Message && "Explicit message send.");
return ObjCMessageDataTy::getFromOpaqueValue(Data).getPointer();
}
static const Expr *
getSyntacticFromForPseudoObjectExpr(const PseudoObjectExpr *POE) {
const Expr *Syntactic = POE->getSyntacticForm()->IgnoreParens();
if (const auto *BO = dyn_cast<BinaryOperator>(Syntactic))
Syntactic = BO->getLHS()->IgnoreParens();
return Syntactic;
}
ObjCMessageKind ObjCMethodCall::getMessageKind() const {
if (!Data) {
const ParentMap &PM = getLocationContext()->getParentMap();
const Stmt *S = PM.getParentIgnoreParenCasts(getOriginExpr());
if (const auto *POE = dyn_cast_or_null<PseudoObjectExpr>(S)) {
const Expr *Syntactic = getSyntacticFromForPseudoObjectExpr(POE);
ObjCMessageKind K;
switch (Syntactic->getStmtClass()) {
case Stmt::ObjCPropertyRefExprClass:
K = OCM_PropertyAccess;
break;
case Stmt::ObjCSubscriptRefExprClass:
K = OCM_Subscript;
break;
default:
K = OCM_Message;
break;
}
if (K != OCM_Message) {
const_cast<ObjCMethodCall *>(this)->Data
= ObjCMessageDataTy(POE, K).getOpaqueValue();
assert(getMessageKind() == K);
return K;
}
}
const_cast<ObjCMethodCall *>(this)->Data
= ObjCMessageDataTy(nullptr, 1).getOpaqueValue();
assert(getMessageKind() == OCM_Message);
return OCM_Message;
}
ObjCMessageDataTy Info = ObjCMessageDataTy::getFromOpaqueValue(Data);
if (!Info.getPointer())
return OCM_Message;
return static_cast<ObjCMessageKind>(Info.getInt());
}
const ObjCPropertyDecl *ObjCMethodCall::getAccessedProperty() const {
if (getMessageKind() == OCM_PropertyAccess) {
const PseudoObjectExpr *POE = getContainingPseudoObjectExpr();
assert(POE && "Property access without PseudoObjectExpr?");
const Expr *Syntactic = getSyntacticFromForPseudoObjectExpr(POE);
auto *RefExpr = cast<ObjCPropertyRefExpr>(Syntactic);
if (RefExpr->isExplicitProperty())
return RefExpr->getExplicitProperty();
}
const ObjCMethodDecl *MD = getDecl();
if (!MD || !MD->isPropertyAccessor())
return nullptr;
return MD->findPropertyDecl();
}
bool ObjCMethodCall::canBeOverridenInSubclass(ObjCInterfaceDecl *IDecl,
Selector Sel) const {
assert(IDecl);
AnalysisManager &AMgr =
getState()->getStateManager().getOwningEngine().getAnalysisManager();
SourceLocation InterfLoc = IDecl->getEndOfDefinitionLoc();
if (InterfLoc.isValid() && AMgr.isInCodeFile(InterfLoc))
return false;
if (getMessageKind() == OCM_PropertyAccess)
return false;
ObjCMethodDecl *D = nullptr;
while (true) {
D = IDecl->lookupMethod(Sel, true);
if (!D)
return false;
if (D->getLocation().isValid() && !AMgr.isInCodeFile(D->getLocation()))
return true;
if (D->isOverriding()) {
IDecl = D->getClassInterface();
if (!IDecl)
return false;
IDecl = IDecl->getSuperClass();
if (!IDecl)
return false;
continue;
}
return false;
};
llvm_unreachable("The while loop should always terminate.");
}
static const ObjCMethodDecl *findDefiningRedecl(const ObjCMethodDecl *MD) {
if (!MD)
return MD;
if (!MD->hasBody()) {
for (auto I : MD->redecls())
if (I->hasBody())
MD = cast<ObjCMethodDecl>(I);
}
return MD;
}
struct PrivateMethodKey {
const ObjCInterfaceDecl *Interface;
Selector LookupSelector;
bool IsClassMethod;
};
namespace llvm {
template <> struct DenseMapInfo<PrivateMethodKey> {
using InterfaceInfo = DenseMapInfo<const ObjCInterfaceDecl *>;
using SelectorInfo = DenseMapInfo<Selector>;
static inline PrivateMethodKey getEmptyKey() {
return {InterfaceInfo::getEmptyKey(), SelectorInfo::getEmptyKey(), false};
}
static inline PrivateMethodKey getTombstoneKey() {
return {InterfaceInfo::getTombstoneKey(), SelectorInfo::getTombstoneKey(),
true};
}
static unsigned getHashValue(const PrivateMethodKey &Key) {
return llvm::hash_combine(
llvm::hash_code(InterfaceInfo::getHashValue(Key.Interface)),
llvm::hash_code(SelectorInfo::getHashValue(Key.LookupSelector)),
Key.IsClassMethod);
}
static bool isEqual(const PrivateMethodKey &LHS,
const PrivateMethodKey &RHS) {
return InterfaceInfo::isEqual(LHS.Interface, RHS.Interface) &&
SelectorInfo::isEqual(LHS.LookupSelector, RHS.LookupSelector) &&
LHS.IsClassMethod == RHS.IsClassMethod;
}
};
}
static const ObjCMethodDecl *
lookupRuntimeDefinition(const ObjCInterfaceDecl *Interface,
Selector LookupSelector, bool InstanceMethod) {
using PrivateMethodCache =
llvm::DenseMap<PrivateMethodKey, Optional<const ObjCMethodDecl *>>;
static PrivateMethodCache PMC;
Optional<const ObjCMethodDecl *> &Val =
PMC[{Interface, LookupSelector, InstanceMethod}];
if (!Val) {
Val = Interface->lookupPrivateMethod(LookupSelector, InstanceMethod);
if (!*Val) {
Val = Interface->lookupMethod(LookupSelector, InstanceMethod);
}
}
return *Val;
}
RuntimeDefinition ObjCMethodCall::getRuntimeDefinition() const {
const ObjCMessageExpr *E = getOriginExpr();
assert(E);
Selector Sel = E->getSelector();
if (E->isInstanceMessage()) {
const ObjCObjectType *ReceiverT = nullptr;
bool CanBeSubClassed = false;
bool LookingForInstanceMethod = true;
QualType SupersType = E->getSuperType();
const MemRegion *Receiver = nullptr;
if (!SupersType.isNull()) {
ReceiverT = cast<ObjCObjectPointerType>(SupersType)->getObjectType();
} else {
Receiver = getReceiverSVal().getAsRegion();
if (!Receiver)
return {};
DynamicTypeInfo DTI = getDynamicTypeInfo(getState(), Receiver);
if (!DTI.isValid()) {
assert(isa<AllocaRegion>(Receiver) &&
"Unhandled untyped region class!");
return {};
}
QualType DynType = DTI.getType();
CanBeSubClassed = DTI.canBeASubClass();
const auto *ReceiverDynT =
dyn_cast<ObjCObjectPointerType>(DynType.getCanonicalType());
if (ReceiverDynT) {
ReceiverT = ReceiverDynT->getObjectType();
if (ReceiverT->isObjCClass()) {
SVal SelfVal = getState()->getSelfSVal(getLocationContext());
if (Receiver == SelfVal.getAsRegion()) {
return RuntimeDefinition(findDefiningRedecl(E->getMethodDecl()));
}
if (SymbolRef ReceiverSym = getReceiverSVal().getAsSymbol()) {
DynamicTypeInfo DTI =
getClassObjectDynamicTypeInfo(getState(), ReceiverSym);
if (DTI.isValid()) {
ReceiverT =
cast<ObjCObjectType>(DTI.getType().getCanonicalType());
CanBeSubClassed = DTI.canBeASubClass();
LookingForInstanceMethod = false;
}
}
}
if (CanBeSubClassed)
if (ObjCInterfaceDecl *IDecl = ReceiverT->getInterface())
CanBeSubClassed = canBeOverridenInSubclass(IDecl, Sel);
}
}
if (ReceiverT)
if (ObjCInterfaceDecl *IDecl = ReceiverT->getInterface()) {
const ObjCMethodDecl *MD =
lookupRuntimeDefinition(IDecl, Sel, LookingForInstanceMethod);
if (MD && !MD->hasBody())
MD = MD->getCanonicalDecl();
if (CanBeSubClassed)
return RuntimeDefinition(MD, Receiver);
else
return RuntimeDefinition(MD, nullptr);
}
} else {
if (ObjCInterfaceDecl *IDecl = E->getReceiverInterface()) {
return RuntimeDefinition(IDecl->lookupPrivateClassMethod(Sel));
}
}
return {};
}
bool ObjCMethodCall::argumentsMayEscape() const {
if (isInSystemHeader() && !isInstanceMessage()) {
Selector Sel = getSelector();
if (Sel.getNumArgs() == 1 &&
Sel.getIdentifierInfoForSlot(0)->isStr("valueWithPointer"))
return true;
}
return CallEvent::argumentsMayEscape();
}
void ObjCMethodCall::getInitialStackFrameContents(
const StackFrameContext *CalleeCtx,
BindingsTy &Bindings) const {
const auto *D = cast<ObjCMethodDecl>(CalleeCtx->getDecl());
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
addParameterValuesToBindings(CalleeCtx, Bindings, SVB, *this,
D->parameters());
SVal SelfVal = getReceiverSVal();
if (!SelfVal.isUnknown()) {
const VarDecl *SelfD = CalleeCtx->getAnalysisDeclContext()->getSelfDecl();
MemRegionManager &MRMgr = SVB.getRegionManager();
Loc SelfLoc = SVB.makeLoc(MRMgr.getVarRegion(SelfD, CalleeCtx));
Bindings.push_back(std::make_pair(SelfLoc, SelfVal));
}
}
CallEventRef<>
CallEventManager::getSimpleCall(const CallExpr *CE, ProgramStateRef State,
const LocationContext *LCtx) {
if (const auto *MCE = dyn_cast<CXXMemberCallExpr>(CE))
return create<CXXMemberCall>(MCE, State, LCtx);
if (const auto *OpCE = dyn_cast<CXXOperatorCallExpr>(CE)) {
const FunctionDecl *DirectCallee = OpCE->getDirectCallee();
if (const auto *MD = dyn_cast<CXXMethodDecl>(DirectCallee))
if (MD->isInstance())
return create<CXXMemberOperatorCall>(OpCE, State, LCtx);
} else if (CE->getCallee()->getType()->isBlockPointerType()) {
return create<BlockCall>(CE, State, LCtx);
}
return create<SimpleFunctionCall>(CE, State, LCtx);
}
CallEventRef<>
CallEventManager::getCaller(const StackFrameContext *CalleeCtx,
ProgramStateRef State) {
const LocationContext *ParentCtx = CalleeCtx->getParent();
const LocationContext *CallerCtx = ParentCtx->getStackFrame();
assert(CallerCtx && "This should not be used for top-level stack frames");
const Stmt *CallSite = CalleeCtx->getCallSite();
if (CallSite) {
if (CallEventRef<> Out = getCall(CallSite, State, CallerCtx))
return Out;
SValBuilder &SVB = State->getStateManager().getSValBuilder();
const auto *Ctor = cast<CXXMethodDecl>(CalleeCtx->getDecl());
Loc ThisPtr = SVB.getCXXThis(Ctor, CalleeCtx);
SVal ThisVal = State->getSVal(ThisPtr);
if (const auto *CE = dyn_cast<CXXConstructExpr>(CallSite))
return getCXXConstructorCall(CE, ThisVal.getAsRegion(), State, CallerCtx);
else if (const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(CallSite))
return getCXXInheritedConstructorCall(CIE, ThisVal.getAsRegion(), State,
CallerCtx);
else {
llvm_unreachable("This is not an inlineable statement");
}
}
const CFGBlock *B = CalleeCtx->getCallSiteBlock();
CFGElement E = (*B)[CalleeCtx->getIndex()];
assert((E.getAs<CFGImplicitDtor>() || E.getAs<CFGTemporaryDtor>()) &&
"All other CFG elements should have exprs");
SValBuilder &SVB = State->getStateManager().getSValBuilder();
const auto *Dtor = cast<CXXDestructorDecl>(CalleeCtx->getDecl());
Loc ThisPtr = SVB.getCXXThis(Dtor, CalleeCtx);
SVal ThisVal = State->getSVal(ThisPtr);
const Stmt *Trigger;
if (Optional<CFGAutomaticObjDtor> AutoDtor = E.getAs<CFGAutomaticObjDtor>())
Trigger = AutoDtor->getTriggerStmt();
else if (Optional<CFGDeleteDtor> DeleteDtor = E.getAs<CFGDeleteDtor>())
Trigger = DeleteDtor->getDeleteExpr();
else
Trigger = Dtor->getBody();
return getCXXDestructorCall(Dtor, Trigger, ThisVal.getAsRegion(),
E.getAs<CFGBaseDtor>().has_value(), State,
CallerCtx);
}
CallEventRef<> CallEventManager::getCall(const Stmt *S, ProgramStateRef State,
const LocationContext *LC) {
if (const auto *CE = dyn_cast<CallExpr>(S)) {
return getSimpleCall(CE, State, LC);
} else if (const auto *NE = dyn_cast<CXXNewExpr>(S)) {
return getCXXAllocatorCall(NE, State, LC);
} else if (const auto *DE = dyn_cast<CXXDeleteExpr>(S)) {
return getCXXDeallocatorCall(DE, State, LC);
} else if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
return getObjCMethodCall(ME, State, LC);
} else {
return nullptr;
}
}