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