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