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