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