ExprEngine.h revision ec8605f1d7ec846dbf51047bfd5c56d32d1ff91c
1//===-- ExprEngine.h - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines a meta-engine for path-sensitive dataflow analysis that 11// is built on CoreEngine, but provides the boilerplate to execute transfer 12// functions and build the ExplodedGraph at the expression level. 13// 14//===----------------------------------------------------------------------===// 15 16#ifndef LLVM_CLANG_GR_EXPRENGINE 17#define LLVM_CLANG_GR_EXPRENGINE 18 19#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 20#include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h" 21#include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h" 22#include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h" 23#include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h" 24#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 25#include "clang/AST/Type.h" 26#include "clang/AST/ExprObjC.h" 27#include "clang/AST/ExprCXX.h" 28#include "clang/AST/StmtObjC.h" 29 30namespace clang { 31 32class ObjCForCollectionStmt; 33 34namespace ento { 35 36class AnalysisManager; 37 38class ExprEngine : public SubEngine { 39 AnalysisManager &AMgr; 40 41 CoreEngine Engine; 42 43 /// G - the simulation graph. 44 ExplodedGraph& G; 45 46 /// Builder - The current StmtNodeBuilder which is used when building the 47 /// nodes for a given statement. 48 StmtNodeBuilder* Builder; 49 50 /// StateMgr - Object that manages the data for all created states. 51 GRStateManager StateMgr; 52 53 /// SymMgr - Object that manages the symbol information. 54 SymbolManager& SymMgr; 55 56 /// svalBuilder - SValBuilder object that creates SVals from expressions. 57 SValBuilder &svalBuilder; 58 59 /// EntryNode - The immediate predecessor node. 60 ExplodedNode* EntryNode; 61 62 /// CleanedState - The state for EntryNode "cleaned" of all dead 63 /// variables and symbols (as determined by a liveness analysis). 64 const GRState* CleanedState; 65 66 /// currentStmt - The current block-level statement. 67 const Stmt* currentStmt; 68 69 // Obj-C Class Identifiers. 70 IdentifierInfo* NSExceptionII; 71 72 // Obj-C Selectors. 73 Selector* NSExceptionInstanceRaiseSelectors; 74 Selector RaiseSel; 75 76 /// The BugReporter associated with this engine. It is important that 77 /// this object be placed at the very end of member variables so that its 78 /// destructor is called before the rest of the ExprEngine is destroyed. 79 GRBugReporter BR; 80 81 llvm::OwningPtr<TransferFuncs> TF; 82 83public: 84 ExprEngine(AnalysisManager &mgr, TransferFuncs *tf); 85 86 ~ExprEngine(); 87 88 void ExecuteWorkList(const LocationContext *L, unsigned Steps = 150000) { 89 Engine.ExecuteWorkList(L, Steps, 0); 90 } 91 92 /// Execute the work list with an initial state. Nodes that reaches the exit 93 /// of the function are added into the Dst set, which represent the exit 94 /// state of the function call. 95 void ExecuteWorkListWithInitialState(const LocationContext *L, unsigned Steps, 96 const GRState *InitState, 97 ExplodedNodeSet &Dst) { 98 Engine.ExecuteWorkListWithInitialState(L, Steps, InitState, Dst); 99 } 100 101 /// getContext - Return the ASTContext associated with this analysis. 102 ASTContext& getContext() const { return AMgr.getASTContext(); } 103 104 virtual AnalysisManager &getAnalysisManager() { return AMgr; } 105 106 CheckerManager &getCheckerManager() const { 107 return *AMgr.getCheckerManager(); 108 } 109 110 SValBuilder &getSValBuilder() { return svalBuilder; } 111 112 TransferFuncs& getTF() { return *TF; } 113 114 BugReporter& getBugReporter() { return BR; } 115 116 StmtNodeBuilder &getBuilder() { assert(Builder); return *Builder; } 117 118 // FIXME: Remove once TransferFuncs is no longer referenced. 119 void setTransferFunction(TransferFuncs* tf); 120 121 /// ViewGraph - Visualize the ExplodedGraph created by executing the 122 /// simulation. 123 void ViewGraph(bool trim = false); 124 125 void ViewGraph(ExplodedNode** Beg, ExplodedNode** End); 126 127 /// getInitialState - Return the initial state used for the root vertex 128 /// in the ExplodedGraph. 129 const GRState* getInitialState(const LocationContext *InitLoc); 130 131 ExplodedGraph& getGraph() { return G; } 132 const ExplodedGraph& getGraph() const { return G; } 133 134 /// processCFGElement - Called by CoreEngine. Used to generate new successor 135 /// nodes by processing the 'effects' of a CFG element. 136 void processCFGElement(const CFGElement E, StmtNodeBuilder& builder); 137 138 void ProcessStmt(const CFGStmt S, StmtNodeBuilder &builder); 139 140 void ProcessInitializer(const CFGInitializer I, StmtNodeBuilder &builder); 141 142 void ProcessImplicitDtor(const CFGImplicitDtor D, StmtNodeBuilder &builder); 143 144 void ProcessAutomaticObjDtor(const CFGAutomaticObjDtor D, 145 StmtNodeBuilder &builder); 146 void ProcessBaseDtor(const CFGBaseDtor D, StmtNodeBuilder &builder); 147 void ProcessMemberDtor(const CFGMemberDtor D, StmtNodeBuilder &builder); 148 void ProcessTemporaryDtor(const CFGTemporaryDtor D, 149 StmtNodeBuilder &builder); 150 151 /// Called by CoreEngine when processing the entrance of a CFGBlock. 152 virtual void processCFGBlockEntrance(ExplodedNodeSet &dstNodes, 153 GenericNodeBuilder<BlockEntrance> &nodeBuilder); 154 155 /// ProcessBranch - Called by CoreEngine. Used to generate successor 156 /// nodes by processing the 'effects' of a branch condition. 157 void processBranch(const Stmt* Condition, const Stmt* Term, 158 BranchNodeBuilder& builder); 159 160 /// processIndirectGoto - Called by CoreEngine. Used to generate successor 161 /// nodes by processing the 'effects' of a computed goto jump. 162 void processIndirectGoto(IndirectGotoNodeBuilder& builder); 163 164 /// ProcessSwitch - Called by CoreEngine. Used to generate successor 165 /// nodes by processing the 'effects' of a switch statement. 166 void processSwitch(SwitchNodeBuilder& builder); 167 168 /// ProcessEndPath - Called by CoreEngine. Used to generate end-of-path 169 /// nodes when the control reaches the end of a function. 170 void processEndOfFunction(EndOfFunctionNodeBuilder& builder); 171 172 /// Generate the entry node of the callee. 173 void processCallEnter(CallEnterNodeBuilder &builder); 174 175 /// Generate the first post callsite node. 176 void processCallExit(CallExitNodeBuilder &builder); 177 178 /// Called by CoreEngine when the analysis worklist has terminated. 179 void processEndWorklist(bool hasWorkRemaining); 180 181 /// evalAssume - Callback function invoked by the ConstraintManager when 182 /// making assumptions about state values. 183 const GRState *processAssume(const GRState *state, SVal cond,bool assumption); 184 185 /// wantsRegionChangeUpdate - Called by GRStateManager to determine if a 186 /// region change should trigger a processRegionChanges update. 187 bool wantsRegionChangeUpdate(const GRState* state); 188 189 /// processRegionChanges - Called by GRStateManager whenever a change is made 190 /// to the store. Used to update checkers that track region values. 191 const GRState* processRegionChanges(const GRState *state, 192 const MemRegion * const *Begin, 193 const MemRegion * const *End); 194 195 virtual GRStateManager& getStateManager() { return StateMgr; } 196 197 StoreManager& getStoreManager() { return StateMgr.getStoreManager(); } 198 199 ConstraintManager& getConstraintManager() { 200 return StateMgr.getConstraintManager(); 201 } 202 203 // FIXME: Remove when we migrate over to just using SValBuilder. 204 BasicValueFactory& getBasicVals() { 205 return StateMgr.getBasicVals(); 206 } 207 const BasicValueFactory& getBasicVals() const { 208 return StateMgr.getBasicVals(); 209 } 210 211 // FIXME: Remove when we migrate over to just using ValueManager. 212 SymbolManager& getSymbolManager() { return SymMgr; } 213 const SymbolManager& getSymbolManager() const { return SymMgr; } 214 215 // Functions for external checking of whether we have unfinished work 216 bool wasBlockAborted() const { return Engine.wasBlockAborted(); } 217 bool hasEmptyWorkList() const { return !Engine.getWorkList()->hasWork(); } 218 bool hasWorkRemaining() const { 219 return wasBlockAborted() || Engine.getWorkList()->hasWork(); 220 } 221 222 const CoreEngine &getCoreEngine() const { return Engine; } 223 224protected: 225 const GRState* GetState(ExplodedNode* N) { 226 return N == EntryNode ? CleanedState : N->getState(); 227 } 228 229public: 230 ExplodedNode* MakeNode(ExplodedNodeSet& Dst, const Stmt* S, 231 ExplodedNode* Pred, const GRState* St, 232 ProgramPoint::Kind K = ProgramPoint::PostStmtKind, 233 const void *tag = 0); 234 235 /// Visit - Transfer function logic for all statements. Dispatches to 236 /// other functions that handle specific kinds of statements. 237 void Visit(const Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst); 238 239 /// VisitArraySubscriptExpr - Transfer function for array accesses. 240 void VisitLvalArraySubscriptExpr(const ArraySubscriptExpr* Ex, 241 ExplodedNode* Pred, 242 ExplodedNodeSet& Dst); 243 244 /// VisitAsmStmt - Transfer function logic for inline asm. 245 void VisitAsmStmt(const AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst); 246 247 void VisitAsmStmtHelperOutputs(const AsmStmt* A, 248 AsmStmt::const_outputs_iterator I, 249 AsmStmt::const_outputs_iterator E, 250 ExplodedNode* Pred, ExplodedNodeSet& Dst); 251 252 void VisitAsmStmtHelperInputs(const AsmStmt* A, 253 AsmStmt::const_inputs_iterator I, 254 AsmStmt::const_inputs_iterator E, 255 ExplodedNode* Pred, ExplodedNodeSet& Dst); 256 257 /// VisitBlockExpr - Transfer function logic for BlockExprs. 258 void VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred, 259 ExplodedNodeSet &Dst); 260 261 /// VisitBinaryOperator - Transfer function logic for binary operators. 262 void VisitBinaryOperator(const BinaryOperator* B, ExplodedNode* Pred, 263 ExplodedNodeSet& Dst); 264 265 266 /// VisitCall - Transfer function for function calls. 267 void VisitCall(const CallExpr* CE, ExplodedNode* Pred, 268 CallExpr::const_arg_iterator AI, 269 CallExpr::const_arg_iterator AE, 270 ExplodedNodeSet& Dst); 271 272 /// VisitCast - Transfer function logic for all casts (implicit and explicit). 273 void VisitCast(const CastExpr *CastE, const Expr *Ex, ExplodedNode *Pred, 274 ExplodedNodeSet &Dst); 275 276 /// VisitCompoundLiteralExpr - Transfer function logic for compound literals. 277 void VisitCompoundLiteralExpr(const CompoundLiteralExpr* CL, 278 ExplodedNode* Pred, ExplodedNodeSet& Dst); 279 280 /// Transfer function logic for DeclRefExprs and BlockDeclRefExprs. 281 void VisitCommonDeclRefExpr(const Expr* DR, const NamedDecl *D, 282 ExplodedNode* Pred, ExplodedNodeSet& Dst); 283 284 /// VisitDeclStmt - Transfer function logic for DeclStmts. 285 void VisitDeclStmt(const DeclStmt* DS, ExplodedNode* Pred, 286 ExplodedNodeSet& Dst); 287 288 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose 289 void VisitGuardedExpr(const Expr* Ex, const Expr* L, const Expr* R, 290 ExplodedNode* Pred, ExplodedNodeSet& Dst); 291 292 /// VisitCondInit - Transfer function for handling the initialization 293 /// of a condition variable in an IfStmt, SwitchStmt, etc. 294 void VisitCondInit(const VarDecl *VD, const Stmt *S, ExplodedNode *Pred, 295 ExplodedNodeSet& Dst); 296 297 void VisitInitListExpr(const InitListExpr* E, ExplodedNode* Pred, 298 ExplodedNodeSet& Dst); 299 300 /// VisitLogicalExpr - Transfer function logic for '&&', '||' 301 void VisitLogicalExpr(const BinaryOperator* B, ExplodedNode* Pred, 302 ExplodedNodeSet& Dst); 303 304 /// VisitMemberExpr - Transfer function for member expressions. 305 void VisitMemberExpr(const MemberExpr* M, ExplodedNode* Pred, 306 ExplodedNodeSet& Dst); 307 308 /// Transfer function logic for ObjCAtSynchronizedStmts. 309 void VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, 310 ExplodedNode *Pred, ExplodedNodeSet &Dst); 311 312 void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E, 313 ExplodedNode *Pred, ExplodedNodeSet &Dst); 314 315 /// Transfer function logic for computing the lvalue of an Objective-C ivar. 316 void VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr* DR, ExplodedNode* Pred, 317 ExplodedNodeSet& Dst); 318 319 /// VisitObjCForCollectionStmt - Transfer function logic for 320 /// ObjCForCollectionStmt. 321 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt* S, 322 ExplodedNode* Pred, ExplodedNodeSet& Dst); 323 324 void VisitObjCForCollectionStmtAux(const ObjCForCollectionStmt* S, 325 ExplodedNode* Pred, 326 ExplodedNodeSet& Dst, SVal ElementV); 327 328 /// VisitObjCMessageExpr - Transfer function for ObjC message expressions. 329 void VisitObjCMessageExpr(const ObjCMessageExpr* ME, ExplodedNode* Pred, 330 ExplodedNodeSet& Dst); 331 void VisitObjCMessage(const ObjCMessage &msg, ExplodedNodeSet &Src, 332 ExplodedNodeSet& Dst); 333 334 /// VisitReturnStmt - Transfer function logic for return statements. 335 void VisitReturnStmt(const ReturnStmt* R, ExplodedNode* Pred, 336 ExplodedNodeSet& Dst); 337 338 /// VisitOffsetOfExpr - Transfer function for offsetof. 339 void VisitOffsetOfExpr(const OffsetOfExpr* Ex, ExplodedNode* Pred, 340 ExplodedNodeSet& Dst); 341 342 /// VisitSizeOfAlignOfExpr - Transfer function for sizeof. 343 void VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr* Ex, ExplodedNode* Pred, 344 ExplodedNodeSet& Dst); 345 346 /// VisitUnaryOperator - Transfer function logic for unary operators. 347 void VisitUnaryOperator(const UnaryOperator* B, ExplodedNode* Pred, 348 ExplodedNodeSet& Dst); 349 350 void VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 351 ExplodedNodeSet & Dst); 352 353 void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *expr, 354 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 355 VisitCXXConstructExpr(expr, 0, Pred, Dst); 356 } 357 358 void VisitCXXConstructExpr(const CXXConstructExpr *E, const MemRegion *Dest, 359 ExplodedNode *Pred, ExplodedNodeSet &Dst); 360 361 void VisitCXXDestructor(const CXXDestructorDecl *DD, 362 const MemRegion *Dest, const Stmt *S, 363 ExplodedNode *Pred, ExplodedNodeSet &Dst); 364 365 void VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE, ExplodedNode *Pred, 366 ExplodedNodeSet &Dst); 367 368 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *C, 369 ExplodedNode *Pred, ExplodedNodeSet &Dst); 370 371 void VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 372 ExplodedNodeSet &Dst); 373 374 void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, ExplodedNode *Pred, 375 ExplodedNodeSet &Dst); 376 377 void VisitAggExpr(const Expr *E, const MemRegion *Dest, ExplodedNode *Pred, 378 ExplodedNodeSet &Dst); 379 380 /// Create a C++ temporary object for an rvalue. 381 void CreateCXXTemporaryObject(const Expr *Ex, ExplodedNode *Pred, 382 ExplodedNodeSet &Dst); 383 384 /// Synthesize CXXThisRegion. 385 const CXXThisRegion *getCXXThisRegion(const CXXRecordDecl *RD, 386 const StackFrameContext *SFC); 387 388 const CXXThisRegion *getCXXThisRegion(const CXXMethodDecl *decl, 389 const StackFrameContext *frameCtx); 390 391 /// Evaluate arguments with a work list algorithm. 392 void evalArguments(ConstExprIterator AI, ConstExprIterator AE, 393 const FunctionProtoType *FnType, 394 ExplodedNode *Pred, ExplodedNodeSet &Dst, 395 bool FstArgAsLValue = false); 396 397 /// Evaluate method call itself. Used for CXXMethodCallExpr and 398 /// CXXOperatorCallExpr. 399 void evalMethodCall(const CallExpr *MCE, const CXXMethodDecl *MD, 400 const Expr *ThisExpr, ExplodedNode *Pred, 401 ExplodedNodeSet &Src, ExplodedNodeSet &Dst); 402 403 /// evalEagerlyAssume - Given the nodes in 'Src', eagerly assume symbolic 404 /// expressions of the form 'x != 0' and generate new nodes (stored in Dst) 405 /// with those assumptions. 406 void evalEagerlyAssume(ExplodedNodeSet& Dst, ExplodedNodeSet& Src, 407 const Expr *Ex); 408 409 SVal evalMinus(SVal X) { 410 return X.isValid() ? svalBuilder.evalMinus(cast<NonLoc>(X)) : X; 411 } 412 413 SVal evalComplement(SVal X) { 414 return X.isValid() ? svalBuilder.evalComplement(cast<NonLoc>(X)) : X; 415 } 416 417public: 418 419 SVal evalBinOp(const GRState *state, BinaryOperator::Opcode op, 420 NonLoc L, NonLoc R, QualType T) { 421 return svalBuilder.evalBinOpNN(state, op, L, R, T); 422 } 423 424 SVal evalBinOp(const GRState *state, BinaryOperator::Opcode op, 425 NonLoc L, SVal R, QualType T) { 426 return R.isValid() ? svalBuilder.evalBinOpNN(state,op,L, cast<NonLoc>(R), T) : R; 427 } 428 429 SVal evalBinOp(const GRState *ST, BinaryOperator::Opcode Op, 430 SVal LHS, SVal RHS, QualType T) { 431 return svalBuilder.evalBinOp(ST, Op, LHS, RHS, T); 432 } 433 434protected: 435 void evalObjCMessage(ExplodedNodeSet& Dst, const ObjCMessage &msg, 436 ExplodedNode* Pred, const GRState *state) { 437 assert (Builder && "StmtNodeBuilder must be defined."); 438 getTF().evalObjCMessage(Dst, *this, *Builder, msg, Pred, state); 439 } 440 441 const GRState* MarkBranch(const GRState* St, const Stmt* Terminator, 442 bool branchTaken); 443 444 /// evalBind - Handle the semantics of binding a value to a specific location. 445 /// This method is used by evalStore, VisitDeclStmt, and others. 446 void evalBind(ExplodedNodeSet& Dst, const Stmt* StoreE, ExplodedNode* Pred, 447 const GRState* St, SVal location, SVal Val, 448 bool atDeclInit = false); 449 450public: 451 // FIXME: 'tag' should be removed, and a LocationContext should be used 452 // instead. 453 // FIXME: Comment on the meaning of the arguments, when 'St' may not 454 // be the same as Pred->state, and when 'location' may not be the 455 // same as state->getLValue(Ex). 456 /// Simulate a read of the result of Ex. 457 void evalLoad(ExplodedNodeSet& Dst, const Expr* Ex, ExplodedNode* Pred, 458 const GRState* St, SVal location, const void *tag = 0, 459 QualType LoadTy = QualType()); 460 461 // FIXME: 'tag' should be removed, and a LocationContext should be used 462 // instead. 463 void evalStore(ExplodedNodeSet& Dst, const Expr* AssignE, const Expr* StoreE, 464 ExplodedNode* Pred, const GRState* St, SVal TargetLV, SVal Val, 465 const void *tag = 0); 466private: 467 void evalLoadCommon(ExplodedNodeSet& Dst, const Expr* Ex, ExplodedNode* Pred, 468 const GRState* St, SVal location, const void *tag, 469 QualType LoadTy); 470 471 // FIXME: 'tag' should be removed, and a LocationContext should be used 472 // instead. 473 void evalLocation(ExplodedNodeSet &Dst, const Stmt *S, ExplodedNode* Pred, 474 const GRState* St, SVal location, 475 const void *tag, bool isLoad); 476 477 bool InlineCall(ExplodedNodeSet &Dst, const CallExpr *CE, ExplodedNode *Pred); 478}; 479 480} // end ento namespace 481 482} // end clang namespace 483 484#endif 485