ExprEngine.h revision 253955ca25c7e7049963b5db613c0cd15d66e4f8
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 93public: 94 ExprEngine(AnalysisManager &mgr, bool gcEnabled, SetOfDecls *VisitedCallees); 95 96 ~ExprEngine(); 97 98 /// Returns true if there is still simulation state on the worklist. 99 bool ExecuteWorkList(const LocationContext *L, unsigned Steps = 150000) { 100 return Engine.ExecuteWorkList(L, Steps, 0); 101 } 102 103 /// Execute the work list with an initial state. Nodes that reaches the exit 104 /// of the function are added into the Dst set, which represent the exit 105 /// state of the function call. Returns true if there is still simulation 106 /// state on the worklist. 107 bool ExecuteWorkListWithInitialState(const LocationContext *L, unsigned Steps, 108 ProgramStateRef InitState, 109 ExplodedNodeSet &Dst) { 110 return Engine.ExecuteWorkListWithInitialState(L, Steps, InitState, Dst); 111 } 112 113 /// getContext - Return the ASTContext associated with this analysis. 114 ASTContext &getContext() const { return AMgr.getASTContext(); } 115 116 virtual AnalysisManager &getAnalysisManager() { return AMgr; } 117 118 CheckerManager &getCheckerManager() const { 119 return *AMgr.getCheckerManager(); 120 } 121 122 SValBuilder &getSValBuilder() { return svalBuilder; } 123 124 BugReporter& getBugReporter() { return BR; } 125 126 const NodeBuilderContext &getBuilderContext() { 127 assert(currentBuilderContext); 128 return *currentBuilderContext; 129 } 130 131 bool isObjCGCEnabled() { return ObjCGCEnabled; } 132 133 const Stmt *getStmt() const; 134 135 void GenerateAutoTransition(ExplodedNode *N); 136 void enqueueEndOfPath(ExplodedNodeSet &S); 137 void GenerateCallExitNode(ExplodedNode *N); 138 139 /// ViewGraph - Visualize the ExplodedGraph created by executing the 140 /// simulation. 141 void ViewGraph(bool trim = false); 142 143 void ViewGraph(ExplodedNode** Beg, ExplodedNode** End); 144 145 /// getInitialState - Return the initial state used for the root vertex 146 /// in the ExplodedGraph. 147 ProgramStateRef getInitialState(const LocationContext *InitLoc); 148 149 ExplodedGraph& getGraph() { return G; } 150 const ExplodedGraph& getGraph() const { return G; } 151 152 /// processCFGElement - Called by CoreEngine. Used to generate new successor 153 /// nodes by processing the 'effects' of a CFG element. 154 void processCFGElement(const CFGElement E, ExplodedNode *Pred, 155 unsigned StmtIdx, NodeBuilderContext *Ctx); 156 157 void ProcessStmt(const CFGStmt S, ExplodedNode *Pred); 158 159 void ProcessInitializer(const CFGInitializer I, ExplodedNode *Pred); 160 161 void ProcessImplicitDtor(const CFGImplicitDtor D, ExplodedNode *Pred); 162 163 void ProcessAutomaticObjDtor(const CFGAutomaticObjDtor D, 164 ExplodedNode *Pred, ExplodedNodeSet &Dst); 165 void ProcessBaseDtor(const CFGBaseDtor D, 166 ExplodedNode *Pred, ExplodedNodeSet &Dst); 167 void ProcessMemberDtor(const CFGMemberDtor D, 168 ExplodedNode *Pred, ExplodedNodeSet &Dst); 169 void ProcessTemporaryDtor(const CFGTemporaryDtor D, 170 ExplodedNode *Pred, ExplodedNodeSet &Dst); 171 172 /// Called by CoreEngine when processing the entrance of a CFGBlock. 173 virtual void processCFGBlockEntrance(const BlockEdge &L, 174 NodeBuilderWithSinks &nodeBuilder); 175 176 /// ProcessBranch - Called by CoreEngine. Used to generate successor 177 /// nodes by processing the 'effects' of a branch condition. 178 void processBranch(const Stmt *Condition, const Stmt *Term, 179 NodeBuilderContext& BuilderCtx, 180 ExplodedNode *Pred, 181 ExplodedNodeSet &Dst, 182 const CFGBlock *DstT, 183 const CFGBlock *DstF); 184 185 /// processIndirectGoto - Called by CoreEngine. Used to generate successor 186 /// nodes by processing the 'effects' of a computed goto jump. 187 void processIndirectGoto(IndirectGotoNodeBuilder& builder); 188 189 /// ProcessSwitch - Called by CoreEngine. Used to generate successor 190 /// nodes by processing the 'effects' of a switch statement. 191 void processSwitch(SwitchNodeBuilder& builder); 192 193 /// ProcessEndPath - Called by CoreEngine. Used to generate end-of-path 194 /// nodes when the control reaches the end of a function. 195 void processEndOfFunction(NodeBuilderContext& BC); 196 197 /// Generate the entry node of the callee. 198 void processCallEnter(CallEnter CE, ExplodedNode *Pred); 199 200 /// Generate the first post callsite node. 201 void processCallExit(ExplodedNode *Pred); 202 203 /// Called by CoreEngine when the analysis worklist has terminated. 204 void processEndWorklist(bool hasWorkRemaining); 205 206 /// evalAssume - Callback function invoked by the ConstraintManager when 207 /// making assumptions about state values. 208 ProgramStateRef processAssume(ProgramStateRef state, SVal cond,bool assumption); 209 210 /// wantsRegionChangeUpdate - Called by ProgramStateManager to determine if a 211 /// region change should trigger a processRegionChanges update. 212 bool wantsRegionChangeUpdate(ProgramStateRef state); 213 214 /// processRegionChanges - Called by ProgramStateManager whenever a change is made 215 /// to the store. Used to update checkers that track region values. 216 ProgramStateRef 217 processRegionChanges(ProgramStateRef state, 218 const StoreManager::InvalidatedSymbols *invalidated, 219 ArrayRef<const MemRegion *> ExplicitRegions, 220 ArrayRef<const MemRegion *> Regions, 221 const CallOrObjCMessage *Call); 222 223 /// printState - Called by ProgramStateManager to print checker-specific data. 224 void printState(raw_ostream &Out, ProgramStateRef State, 225 const char *NL, const char *Sep); 226 227 virtual ProgramStateManager& getStateManager() { return StateMgr; } 228 229 StoreManager& getStoreManager() { return StateMgr.getStoreManager(); } 230 231 ConstraintManager& getConstraintManager() { 232 return StateMgr.getConstraintManager(); 233 } 234 235 // FIXME: Remove when we migrate over to just using SValBuilder. 236 BasicValueFactory& getBasicVals() { 237 return StateMgr.getBasicVals(); 238 } 239 const BasicValueFactory& getBasicVals() const { 240 return StateMgr.getBasicVals(); 241 } 242 243 // FIXME: Remove when we migrate over to just using ValueManager. 244 SymbolManager& getSymbolManager() { return SymMgr; } 245 const SymbolManager& getSymbolManager() const { return SymMgr; } 246 247 // Functions for external checking of whether we have unfinished work 248 bool wasBlocksExhausted() const { return Engine.wasBlocksExhausted(); } 249 bool hasEmptyWorkList() const { return !Engine.getWorkList()->hasWork(); } 250 bool hasWorkRemaining() const { return Engine.hasWorkRemaining(); } 251 252 const CoreEngine &getCoreEngine() const { return Engine; } 253 254public: 255 /// Visit - Transfer function logic for all statements. Dispatches to 256 /// other functions that handle specific kinds of statements. 257 void Visit(const Stmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst); 258 259 /// VisitArraySubscriptExpr - Transfer function for array accesses. 260 void VisitLvalArraySubscriptExpr(const ArraySubscriptExpr *Ex, 261 ExplodedNode *Pred, 262 ExplodedNodeSet &Dst); 263 264 /// VisitAsmStmt - Transfer function logic for inline asm. 265 void VisitAsmStmt(const AsmStmt *A, ExplodedNode *Pred, ExplodedNodeSet &Dst); 266 267 /// VisitBlockExpr - Transfer function logic for BlockExprs. 268 void VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred, 269 ExplodedNodeSet &Dst); 270 271 /// VisitBinaryOperator - Transfer function logic for binary operators. 272 void VisitBinaryOperator(const BinaryOperator* B, ExplodedNode *Pred, 273 ExplodedNodeSet &Dst); 274 275 276 /// VisitCall - Transfer function for function calls. 277 void VisitCallExpr(const CallExpr *CE, ExplodedNode *Pred, 278 ExplodedNodeSet &Dst); 279 280 /// VisitCast - Transfer function logic for all casts (implicit and explicit). 281 void VisitCast(const CastExpr *CastE, const Expr *Ex, ExplodedNode *Pred, 282 ExplodedNodeSet &Dst); 283 284 /// VisitCompoundLiteralExpr - Transfer function logic for compound literals. 285 void VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL, 286 ExplodedNode *Pred, ExplodedNodeSet &Dst); 287 288 /// Transfer function logic for DeclRefExprs and BlockDeclRefExprs. 289 void VisitCommonDeclRefExpr(const Expr *DR, const NamedDecl *D, 290 ExplodedNode *Pred, ExplodedNodeSet &Dst); 291 292 /// VisitDeclStmt - Transfer function logic for DeclStmts. 293 void VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred, 294 ExplodedNodeSet &Dst); 295 296 /// VisitGuardedExpr - Transfer function logic for ?, __builtin_choose 297 void VisitGuardedExpr(const Expr *Ex, const Expr *L, const Expr *R, 298 ExplodedNode *Pred, ExplodedNodeSet &Dst); 299 300 void VisitInitListExpr(const InitListExpr *E, ExplodedNode *Pred, 301 ExplodedNodeSet &Dst); 302 303 /// VisitLogicalExpr - Transfer function logic for '&&', '||' 304 void VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred, 305 ExplodedNodeSet &Dst); 306 307 /// VisitMemberExpr - Transfer function for member expressions. 308 void VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred, 309 ExplodedNodeSet &Dst); 310 311 /// Transfer function logic for ObjCAtSynchronizedStmts. 312 void VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S, 313 ExplodedNode *Pred, ExplodedNodeSet &Dst); 314 315 /// Transfer function logic for computing the lvalue of an Objective-C ivar. 316 void VisitLvalObjCIvarRefExpr(const ObjCIvarRefExpr *DR, ExplodedNode *Pred, 317 ExplodedNodeSet &Dst); 318 319 /// VisitObjCForCollectionStmt - Transfer function logic for 320 /// ObjCForCollectionStmt. 321 void VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S, 322 ExplodedNode *Pred, ExplodedNodeSet &Dst); 323 324 void VisitObjCMessage(const ObjCMessage &msg, ExplodedNode *Pred, 325 ExplodedNodeSet &Dst); 326 327 /// VisitReturnStmt - Transfer function logic for return statements. 328 void VisitReturnStmt(const ReturnStmt *R, ExplodedNode *Pred, 329 ExplodedNodeSet &Dst); 330 331 /// VisitOffsetOfExpr - Transfer function for offsetof. 332 void VisitOffsetOfExpr(const OffsetOfExpr *Ex, ExplodedNode *Pred, 333 ExplodedNodeSet &Dst); 334 335 /// VisitUnaryExprOrTypeTraitExpr - Transfer function for sizeof. 336 void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex, 337 ExplodedNode *Pred, ExplodedNodeSet &Dst); 338 339 /// VisitUnaryOperator - Transfer function logic for unary operators. 340 void VisitUnaryOperator(const UnaryOperator* B, ExplodedNode *Pred, 341 ExplodedNodeSet &Dst); 342 343 /// Handle ++ and -- (both pre- and post-increment). 344 void VisitIncrementDecrementOperator(const UnaryOperator* U, 345 ExplodedNode *Pred, 346 ExplodedNodeSet &Dst); 347 348 void VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred, 349 ExplodedNodeSet &Dst); 350 351 void VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 352 ExplodedNodeSet & Dst); 353 354 void VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *expr, 355 ExplodedNode *Pred, ExplodedNodeSet &Dst); 356 357 void VisitCXXConstructExpr(const CXXConstructExpr *E, const MemRegion *Dest, 358 ExplodedNode *Pred, ExplodedNodeSet &Dst); 359 360 void VisitCXXDestructor(const CXXDestructorDecl *DD, 361 const MemRegion *Dest, const Stmt *S, 362 ExplodedNode *Pred, ExplodedNodeSet &Dst); 363 364 void VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 365 ExplodedNodeSet &Dst); 366 367 void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 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 /// evalEagerlyAssume - Given the nodes in 'Src', eagerly assume symbolic 383 /// expressions of the form 'x != 0' and generate new nodes (stored in Dst) 384 /// with those assumptions. 385 void evalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, 386 const Expr *Ex); 387 388 std::pair<const ProgramPointTag *, const ProgramPointTag*> 389 getEagerlyAssumeTags(); 390 391 SVal evalMinus(SVal X) { 392 return X.isValid() ? svalBuilder.evalMinus(cast<NonLoc>(X)) : X; 393 } 394 395 SVal evalComplement(SVal X) { 396 return X.isValid() ? svalBuilder.evalComplement(cast<NonLoc>(X)) : X; 397 } 398 399public: 400 401 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 402 NonLoc L, NonLoc R, QualType T) { 403 return svalBuilder.evalBinOpNN(state, op, L, R, T); 404 } 405 406 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 407 NonLoc L, SVal R, QualType T) { 408 return R.isValid() ? svalBuilder.evalBinOpNN(state,op,L, cast<NonLoc>(R), T) : R; 409 } 410 411 SVal evalBinOp(ProgramStateRef ST, BinaryOperator::Opcode Op, 412 SVal LHS, SVal RHS, QualType T) { 413 return svalBuilder.evalBinOp(ST, Op, LHS, RHS, T); 414 } 415 416protected: 417 void evalObjCMessage(StmtNodeBuilder &Bldr, const ObjCMessage &msg, 418 ExplodedNode *Pred, ProgramStateRef state, 419 bool GenSink); 420 421 ProgramStateRef invalidateArguments(ProgramStateRef State, 422 const CallOrObjCMessage &Call, 423 const LocationContext *LC); 424 425 ProgramStateRef MarkBranch(ProgramStateRef state, 426 const Stmt *Terminator, 427 const LocationContext *LCtx, 428 bool branchTaken); 429 430 /// evalBind - Handle the semantics of binding a value to a specific location. 431 /// This method is used by evalStore, VisitDeclStmt, and others. 432 void evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE, ExplodedNode *Pred, 433 SVal location, SVal Val, bool atDeclInit = false); 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 shouldInlineDecl(const FunctionDecl *FD, ExplodedNode *Pred); 463 bool InlineCall(ExplodedNodeSet &Dst, const CallExpr *CE, ExplodedNode *Pred); 464 465 bool replayWithoutInlining(ExplodedNode *P, const LocationContext *CalleeLC); 466}; 467 468/// Traits for storing the call processing policy inside GDM. 469/// The GDM stores the corresponding CallExpr pointer. 470struct ReplayWithoutInlining{}; 471template <> 472struct ProgramStateTrait<ReplayWithoutInlining> : 473 public ProgramStatePartialTrait<void*> { 474 static void *GDMIndex() { static int index = 0; return &index; } 475}; 476 477} // end ento namespace 478 479} // end clang namespace 480 481#endif 482