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