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