Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 4af8431 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 10 | // This file defines a meta-engine for path-sensitive dataflow analysis that |
| 11 | // is built on GREngine, but provides the boilerplate to execute transfer |
| 12 | // functions and build the ExplodedGraph at the expression level. |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 77349cb | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h" |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/Checker.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
| 20 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Builtins.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 0bed8a1 | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PrettyStackTrace.h" |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/ImmutableList.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 0f5f059 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 29 | #ifndef NDEBUG |
| 30 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | 0f5f059 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Ted Kremenek | b387a3f | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | using llvm::dyn_cast; |
| 35 | using llvm::cast; |
| 36 | using llvm::APSInt; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 39 | // Batch auditor. DEPRECATED. |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | class VISIBILITY_HIDDEN MappedBatchAuditor : public GRSimpleAPICheck { |
| 45 | typedef llvm::ImmutableList<GRSimpleAPICheck*> Checks; |
| 46 | typedef llvm::DenseMap<void*,Checks> MapTy; |
| 47 | |
| 48 | MapTy M; |
| 49 | Checks::Factory F; |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 50 | Checks AllStmts; |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 51 | |
| 52 | public: |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 53 | MappedBatchAuditor(llvm::BumpPtrAllocator& Alloc) : |
| 54 | F(Alloc), AllStmts(F.GetEmptyList()) {} |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 55 | |
| 56 | virtual ~MappedBatchAuditor() { |
| 57 | llvm::DenseSet<GRSimpleAPICheck*> AlreadyVisited; |
| 58 | |
| 59 | for (MapTy::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
| 60 | for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E;++I){ |
| 61 | |
| 62 | GRSimpleAPICheck* check = *I; |
| 63 | |
| 64 | if (AlreadyVisited.count(check)) |
| 65 | continue; |
| 66 | |
| 67 | AlreadyVisited.insert(check); |
| 68 | delete check; |
| 69 | } |
| 70 | } |
| 71 | |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 72 | void AddCheck(GRSimpleAPICheck *A, Stmt::StmtClass C) { |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 73 | assert (A && "Check cannot be null."); |
| 74 | void* key = reinterpret_cast<void*>((uintptr_t) C); |
| 75 | MapTy::iterator I = M.find(key); |
| 76 | M[key] = F.Concat(A, I == M.end() ? F.GetEmptyList() : I->second); |
| 77 | } |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 78 | |
| 79 | void AddCheck(GRSimpleAPICheck *A) { |
| 80 | assert (A && "Check cannot be null."); |
| 81 | AllStmts = F.Concat(A, AllStmts); |
| 82 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 83 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 84 | virtual bool Audit(ExplodedNode* N, GRStateManager& VMgr) { |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 85 | // First handle the auditors that accept all statements. |
| 86 | bool isSink = false; |
| 87 | for (Checks::iterator I = AllStmts.begin(), E = AllStmts.end(); I!=E; ++I) |
| 88 | isSink |= (*I)->Audit(N, VMgr); |
| 89 | |
| 90 | // Next handle the auditors that accept only specific statements. |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 91 | const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 92 | void* key = reinterpret_cast<void*>((uintptr_t) S->getStmtClass()); |
| 93 | MapTy::iterator MI = M.find(key); |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 94 | if (MI != M.end()) { |
| 95 | for (Checks::iterator I=MI->second.begin(), E=MI->second.end(); I!=E; ++I) |
| 96 | isSink |= (*I)->Audit(N, VMgr); |
| 97 | } |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 99 | return isSink; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | } // end anonymous namespace |
| 104 | |
| 105 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 106 | // Checker worklist routines. |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 109 | void GRExprEngine::CheckerVisit(Stmt *S, ExplodedNodeSet &Dst, |
| 110 | ExplodedNodeSet &Src, bool isPrevisit) { |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 111 | |
| 112 | if (Checkers.empty()) { |
| 113 | Dst = Src; |
| 114 | return; |
| 115 | } |
| 116 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 117 | ExplodedNodeSet Tmp; |
| 118 | ExplodedNodeSet *PrevSet = &Src; |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 119 | |
| 120 | for (std::vector<Checker*>::iterator I = Checkers.begin(), E = Checkers.end(); |
| 121 | I != E; ++I) { |
| 122 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 123 | ExplodedNodeSet *CurrSet = (I+1 == E) ? &Dst |
| 124 | : (PrevSet == &Tmp) ? &Src : &Tmp; |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 125 | CurrSet->clear(); |
| 126 | Checker *checker = *I; |
| 127 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 128 | for (ExplodedNodeSet::iterator NI = PrevSet->begin(), NE = PrevSet->end(); |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 129 | NI != NE; ++NI) |
| 130 | checker->GR_Visit(*CurrSet, *Builder, *this, S, *NI, isPrevisit); |
| 131 | |
| 132 | // Update which NodeSet is the current one. |
| 133 | PrevSet = CurrSet; |
| 134 | } |
| 135 | |
| 136 | // Don't autotransition. The CheckerContext objects should do this |
| 137 | // automatically. |
| 138 | } |
| 139 | |
| 140 | //===----------------------------------------------------------------------===// |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 141 | // Engine construction and deletion. |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 144 | static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { |
| 145 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 146 | return Ctx.Selectors.getSelector(0, &II); |
| 147 | } |
| 148 | |
Ted Kremenek | daa497e | 2008-03-09 18:05:48 +0000 | [diff] [blame] | 149 | |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 150 | GRExprEngine::GRExprEngine(AnalysisManager &mgr) |
Zhongxing Xu | 25e695b | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 151 | : AMgr(mgr), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 152 | CoreEngine(mgr.getASTContext(), *this), |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 153 | G(CoreEngine.getGraph()), |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 154 | Builder(NULL), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 155 | StateMgr(G.getContext(), mgr.getStoreManagerCreator(), |
| 156 | mgr.getConstraintManagerCreator(), G.getAllocator()), |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 157 | SymMgr(StateMgr.getSymbolManager()), |
Ted Kremenek | 8e5fb28 | 2009-04-09 16:46:55 +0000 | [diff] [blame] | 158 | ValMgr(StateMgr.getValueManager()), |
Ted Kremenek | 06669c8 | 2009-07-16 01:32:00 +0000 | [diff] [blame] | 159 | SVator(ValMgr.getSValuator()), |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 160 | CurrentStmt(NULL), |
Zhongxing Xu | a5a4166 | 2008-12-22 08:30:52 +0000 | [diff] [blame] | 161 | NSExceptionII(NULL), NSExceptionInstanceRaiseSelectors(NULL), |
| 162 | RaiseSel(GetNullarySelector("raise", G.getContext())), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 163 | BR(mgr, *this) {} |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 1a654b6 | 2008-06-20 21:45:25 +0000 | [diff] [blame] | 165 | GRExprEngine::~GRExprEngine() { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 166 | BR.FlushReports(); |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 167 | delete [] NSExceptionInstanceRaiseSelectors; |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 168 | for (std::vector<Checker*>::iterator I=Checkers.begin(), E=Checkers.end(); |
| 169 | I!=E; ++I) |
| 170 | delete *I; |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 173 | //===----------------------------------------------------------------------===// |
| 174 | // Utility methods. |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 178 | void GRExprEngine::setTransferFunctions(GRTransferFuncs* tf) { |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 179 | StateMgr.TF = tf; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 180 | tf->RegisterChecks(getBugReporter()); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 181 | tf->RegisterPrinters(getStateManager().Printers); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 184 | void GRExprEngine::AddCheck(GRSimpleAPICheck* A, Stmt::StmtClass C) { |
| 185 | if (!BatchAuditor) |
| 186 | BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator())); |
| 187 | |
| 188 | ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A, C); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Ted Kremenek | 536aa02 | 2009-03-30 17:53:05 +0000 | [diff] [blame] | 191 | void GRExprEngine::AddCheck(GRSimpleAPICheck *A) { |
| 192 | if (!BatchAuditor) |
| 193 | BatchAuditor.reset(new MappedBatchAuditor(getGraph().getAllocator())); |
| 194 | |
| 195 | ((MappedBatchAuditor*) BatchAuditor.get())->AddCheck(A); |
| 196 | } |
| 197 | |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 198 | const GRState* GRExprEngine::getInitialState(const LocationContext *InitLoc) { |
| 199 | const GRState *state = StateMgr.getInitialState(InitLoc); |
Ted Kremenek | 52e5602 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 200 | |
| 201 | // Precondition: the first argument of 'main' is an integer guaranteed |
| 202 | // to be > 0. |
| 203 | // FIXME: It would be nice if we had a more general mechanism to add |
| 204 | // such preconditions. Some day. |
Zhongxing Xu | b8310ea | 2009-08-21 03:05:36 +0000 | [diff] [blame] | 205 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(InitLoc->getDecl())) |
Ted Kremenek | 52e5602 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 206 | if (strcmp(FD->getIdentifier()->getName(), "main") == 0 && |
| 207 | FD->getNumParams() > 0) { |
| 208 | const ParmVarDecl *PD = FD->getParamDecl(0); |
| 209 | QualType T = PD->getType(); |
| 210 | if (T->isIntegerType()) |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 211 | if (const MemRegion *R = state->getRegion(PD, InitLoc)) { |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 212 | SVal V = state->getSVal(loc::MemRegionVal(R)); |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 213 | SVal Constraint = EvalBinOp(state, BinaryOperator::GT, V, |
Ted Kremenek | 52e5602 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 214 | ValMgr.makeZeroVal(T), |
| 215 | getContext().IntTy); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 216 | |
| 217 | if (const GRState *newState = state->assume(Constraint, true)) |
| 218 | state = newState; |
Ted Kremenek | 52e5602 | 2009-04-10 00:59:50 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | return state; |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 225 | //===----------------------------------------------------------------------===// |
| 226 | // Top-level transfer function logic (Dispatcher). |
| 227 | //===----------------------------------------------------------------------===// |
| 228 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 229 | void GRExprEngine::ProcessStmt(Stmt* S, GRStmtNodeBuilder& builder) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | 0bed8a1 | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 231 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), |
| 232 | S->getLocStart(), |
| 233 | "Error evaluating statement"); |
| 234 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 235 | Builder = &builder; |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 236 | EntryNode = builder.getLastNode(); |
Ted Kremenek | df7533b | 2008-07-17 21:27:31 +0000 | [diff] [blame] | 237 | |
| 238 | // FIXME: Consolidate. |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 239 | CurrentStmt = S; |
Ted Kremenek | df7533b | 2008-07-17 21:27:31 +0000 | [diff] [blame] | 240 | StateMgr.CurrentStmt = S; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 241 | |
| 242 | // Set up our simple checks. |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 243 | if (BatchAuditor) |
| 244 | Builder->setAuditor(BatchAuditor.get()); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | bdb435d | 2008-07-11 18:37:32 +0000 | [diff] [blame] | 246 | // Create the cleaned state. |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 247 | SymbolReaper SymReaper(*AMgr.getLiveVariables(), SymMgr); |
Zhongxing Xu | c999496 | 2009-08-27 06:55:26 +0000 | [diff] [blame] | 248 | CleanedState = AMgr.shouldPurgeDead() |
| 249 | ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper) |
| 250 | : EntryNode->getState(); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 252 | // Process any special transfer function for dead symbols. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 253 | ExplodedNodeSet Tmp; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 255 | if (!SymReaper.hasDeadSymbols()) |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 256 | Tmp.Add(EntryNode); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 257 | else { |
| 258 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 259 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
| 260 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 261 | SaveAndRestore<bool> OldPurgeDeadSymbols(Builder->PurgingDeadSymbols); |
| 262 | Builder->PurgingDeadSymbols = true; |
| 263 | |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 264 | getTF().EvalDeadSymbols(Tmp, *this, *Builder, EntryNode, S, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 265 | CleanedState, SymReaper); |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 266 | |
| 267 | if (!Builder->BuildSinks && !Builder->HasGeneratedNode) |
| 268 | Tmp.Add(EntryNode); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 269 | } |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 270 | |
| 271 | bool HasAutoGenerated = false; |
| 272 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 273 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 274 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 275 | ExplodedNodeSet Dst; |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 277 | // Set the cleaned state. |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 278 | Builder->SetCleanedState(*I == EntryNode ? CleanedState : GetState(*I)); |
| 279 | |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 280 | // Visit the statement. |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 281 | Visit(S, *I, Dst); |
| 282 | |
| 283 | // Do we need to auto-generate a node? We only need to do this to generate |
| 284 | // a node with a "cleaned" state; GRCoreEngine will actually handle |
| 285 | // auto-transitions for other cases. |
| 286 | if (Dst.size() == 1 && *Dst.begin() == EntryNode |
| 287 | && !Builder->HasGeneratedNode && !HasAutoGenerated) { |
| 288 | HasAutoGenerated = true; |
| 289 | builder.generateNode(S, GetState(EntryNode), *I); |
| 290 | } |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 291 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 293 | // NULL out these variables to cleanup. |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 294 | CleanedState = NULL; |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 295 | EntryNode = NULL; |
Ted Kremenek | df7533b | 2008-07-17 21:27:31 +0000 | [diff] [blame] | 296 | |
| 297 | // FIXME: Consolidate. |
| 298 | StateMgr.CurrentStmt = 0; |
| 299 | CurrentStmt = 0; |
| 300 | |
Ted Kremenek | 846d4e9 | 2008-04-24 23:35:58 +0000 | [diff] [blame] | 301 | Builder = NULL; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 304 | void GRExprEngine::Visit(Stmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 0bed8a1 | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 305 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), |
| 306 | S->getLocStart(), |
| 307 | "Error evaluating statement"); |
| 308 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 309 | // FIXME: add metadata to the CFG so that we can disable |
| 310 | // this check when we KNOW that there is no block-level subexpression. |
| 311 | // The motivation is that this check requires a hashtable lookup. |
| 312 | |
Zhongxing Xu | cc02553 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 313 | if (S != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S)) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 314 | Dst.Add(Pred); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | switch (S->getStmtClass()) { |
| 319 | |
| 320 | default: |
| 321 | // Cases we intentionally have "default" handle: |
| 322 | // AddrLabelExpr, IntegerLiteral, CharacterLiteral |
| 323 | |
| 324 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 325 | break; |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 326 | |
| 327 | case Stmt::ArraySubscriptExprClass: |
| 328 | VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst, false); |
| 329 | break; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 330 | |
| 331 | case Stmt::AsmStmtClass: |
| 332 | VisitAsmStmt(cast<AsmStmt>(S), Pred, Dst); |
| 333 | break; |
| 334 | |
| 335 | case Stmt::BinaryOperatorClass: { |
| 336 | BinaryOperator* B = cast<BinaryOperator>(S); |
| 337 | |
| 338 | if (B->isLogicalOp()) { |
| 339 | VisitLogicalExpr(B, Pred, Dst); |
| 340 | break; |
| 341 | } |
| 342 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 343 | const GRState* state = GetState(Pred); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 344 | MakeNode(Dst, B, Pred, state->BindExpr(B, state->getSVal(B->getRHS()))); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 345 | break; |
| 346 | } |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 347 | |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 348 | if (AMgr.shouldEagerlyAssume() && (B->isRelationalOp() || B->isEqualityOp())) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 349 | ExplodedNodeSet Tmp; |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 350 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp); |
Ted Kremenek | b293902 | 2009-02-25 23:32:10 +0000 | [diff] [blame] | 351 | EvalEagerlyAssume(Dst, Tmp, cast<Expr>(S)); |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 355 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 356 | break; |
| 357 | } |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 359 | case Stmt::CallExprClass: |
| 360 | case Stmt::CXXOperatorCallExprClass: { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 361 | CallExpr* C = cast<CallExpr>(S); |
| 362 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 363 | break; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 364 | } |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 366 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 367 | // the CFG do not model them as explicit control-flow. |
| 368 | |
| 369 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 370 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 371 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | case Stmt::CompoundAssignOperatorClass: |
| 376 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 377 | break; |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 378 | |
| 379 | case Stmt::CompoundLiteralExprClass: |
| 380 | VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst, false); |
| 381 | break; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 382 | |
| 383 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 384 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 385 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 390 | case Stmt::QualifiedDeclRefExprClass: |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 391 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst, false); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case Stmt::DeclStmtClass: |
| 395 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 396 | break; |
| 397 | |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 398 | case Stmt::ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 399 | case Stmt::CStyleCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 400 | CastExpr* C = cast<CastExpr>(S); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 401 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 402 | break; |
| 403 | } |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 404 | |
| 405 | case Stmt::InitListExprClass: |
| 406 | VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst); |
| 407 | break; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 409 | case Stmt::MemberExprClass: |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 410 | VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst, false); |
| 411 | break; |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 412 | |
| 413 | case Stmt::ObjCIvarRefExprClass: |
| 414 | VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst, false); |
| 415 | break; |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 416 | |
| 417 | case Stmt::ObjCForCollectionStmtClass: |
| 418 | VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst); |
| 419 | break; |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 420 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 421 | case Stmt::ObjCMessageExprClass: { |
| 422 | VisitObjCMessageExpr(cast<ObjCMessageExpr>(S), Pred, Dst); |
| 423 | break; |
| 424 | } |
| 425 | |
Ted Kremenek | bbfd07a | 2008-12-09 20:18:58 +0000 | [diff] [blame] | 426 | case Stmt::ObjCAtThrowStmtClass: { |
| 427 | // FIXME: This is not complete. We basically treat @throw as |
| 428 | // an abort. |
| 429 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 430 | Builder->BuildSinks = true; |
| 431 | MakeNode(Dst, S, Pred, GetState(Pred)); |
| 432 | break; |
| 433 | } |
| 434 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 435 | case Stmt::ParenExprClass: |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 436 | Visit(cast<ParenExpr>(S)->getSubExpr()->IgnoreParens(), Pred, Dst); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 437 | break; |
| 438 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 439 | case Stmt::ReturnStmtClass: |
| 440 | VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst); |
| 441 | break; |
| 442 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 443 | case Stmt::SizeOfAlignOfExprClass: |
| 444 | VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), Pred, Dst); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 445 | break; |
| 446 | |
| 447 | case Stmt::StmtExprClass: { |
| 448 | StmtExpr* SE = cast<StmtExpr>(S); |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 449 | |
| 450 | if (SE->getSubStmt()->body_empty()) { |
| 451 | // Empty statement expression. |
| 452 | assert(SE->getType() == getContext().VoidTy |
| 453 | && "Empty statement expression must have void type."); |
| 454 | Dst.Add(Pred); |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | if (Expr* LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) { |
| 459 | const GRState* state = GetState(Pred); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 460 | MakeNode(Dst, SE, Pred, state->BindExpr(SE, state->getSVal(LastExpr))); |
Ted Kremenek | a3d1eb8 | 2009-02-14 05:55:08 +0000 | [diff] [blame] | 461 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 462 | else |
| 463 | Dst.Add(Pred); |
| 464 | |
| 465 | break; |
| 466 | } |
Zhongxing Xu | 6987c7b | 2008-11-30 05:49:49 +0000 | [diff] [blame] | 467 | |
| 468 | case Stmt::StringLiteralClass: |
| 469 | VisitLValue(cast<StringLiteral>(S), Pred, Dst); |
| 470 | break; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 471 | |
Ted Kremenek | 7237459 | 2009-03-18 23:49:26 +0000 | [diff] [blame] | 472 | case Stmt::UnaryOperatorClass: { |
| 473 | UnaryOperator *U = cast<UnaryOperator>(S); |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 474 | if (AMgr.shouldEagerlyAssume() && (U->getOpcode() == UnaryOperator::LNot)) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 475 | ExplodedNodeSet Tmp; |
Ted Kremenek | 7237459 | 2009-03-18 23:49:26 +0000 | [diff] [blame] | 476 | VisitUnaryOperator(U, Pred, Tmp, false); |
| 477 | EvalEagerlyAssume(Dst, Tmp, U); |
| 478 | } |
| 479 | else |
| 480 | VisitUnaryOperator(U, Pred, Dst, false); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 481 | break; |
Ted Kremenek | 7237459 | 2009-03-18 23:49:26 +0000 | [diff] [blame] | 482 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 486 | void GRExprEngine::VisitLValue(Expr* Ex, ExplodedNode* Pred, |
| 487 | ExplodedNodeSet& Dst) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 488 | |
| 489 | Ex = Ex->IgnoreParens(); |
| 490 | |
Zhongxing Xu | cc02553 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 491 | if (Ex != CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 492 | Dst.Add(Pred); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | switch (Ex->getStmtClass()) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 497 | |
| 498 | case Stmt::ArraySubscriptExprClass: |
| 499 | VisitArraySubscriptExpr(cast<ArraySubscriptExpr>(Ex), Pred, Dst, true); |
| 500 | return; |
| 501 | |
| 502 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 503 | case Stmt::QualifiedDeclRefExprClass: |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 504 | VisitDeclRefExpr(cast<DeclRefExpr>(Ex), Pred, Dst, true); |
| 505 | return; |
| 506 | |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 507 | case Stmt::ObjCIvarRefExprClass: |
| 508 | VisitObjCIvarRefExpr(cast<ObjCIvarRefExpr>(Ex), Pred, Dst, true); |
| 509 | return; |
| 510 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 511 | case Stmt::UnaryOperatorClass: |
| 512 | VisitUnaryOperator(cast<UnaryOperator>(Ex), Pred, Dst, true); |
| 513 | return; |
| 514 | |
| 515 | case Stmt::MemberExprClass: |
| 516 | VisitMemberExpr(cast<MemberExpr>(Ex), Pred, Dst, true); |
| 517 | return; |
Ted Kremenek | b6b81d1 | 2008-10-17 17:24:14 +0000 | [diff] [blame] | 518 | |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 519 | case Stmt::CompoundLiteralExprClass: |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 520 | VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(Ex), Pred, Dst, true); |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 521 | return; |
| 522 | |
Ted Kremenek | b6b81d1 | 2008-10-17 17:24:14 +0000 | [diff] [blame] | 523 | case Stmt::ObjCPropertyRefExprClass: |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 524 | case Stmt::ObjCImplicitSetterGetterRefExprClass: |
Ted Kremenek | b6b81d1 | 2008-10-17 17:24:14 +0000 | [diff] [blame] | 525 | // FIXME: Property assignments are lvalues, but not really "locations". |
| 526 | // e.g.: self.x = something; |
| 527 | // Here the "self.x" really can translate to a method call (setter) when |
| 528 | // the assignment is made. Moreover, the entire assignment expression |
| 529 | // evaluate to whatever "something" is, not calling the "getter" for |
| 530 | // the property (which would make sense since it can have side effects). |
| 531 | // We'll probably treat this as a location, but not one that we can |
| 532 | // take the address of. Perhaps we need a new SVal class for cases |
| 533 | // like thsis? |
| 534 | // Note that we have a similar problem for bitfields, since they don't |
| 535 | // have "locations" in the sense that we can take their address. |
| 536 | Dst.Add(Pred); |
Ted Kremenek | c7df6d2 | 2008-10-18 04:08:49 +0000 | [diff] [blame] | 537 | return; |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 538 | |
| 539 | case Stmt::StringLiteralClass: { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 540 | const GRState* state = GetState(Pred); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 541 | SVal V = state->getLValue(cast<StringLiteral>(Ex)); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 542 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V)); |
Zhongxing Xu | 143bf82 | 2008-10-25 14:18:57 +0000 | [diff] [blame] | 543 | return; |
| 544 | } |
Ted Kremenek | c7df6d2 | 2008-10-18 04:08:49 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | f8cd1b2 | 2008-10-18 04:15:35 +0000 | [diff] [blame] | 546 | default: |
| 547 | // Arbitrary subexpressions can return aggregate temporaries that |
| 548 | // can be used in a lvalue context. We need to enhance our support |
| 549 | // of such temporaries in both the environment and the store, so right |
| 550 | // now we just do a regular visit. |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 551 | assert ((Ex->getType()->isAggregateType()) && |
Ted Kremenek | 5b2316a | 2008-10-25 20:09:21 +0000 | [diff] [blame] | 552 | "Other kinds of expressions with non-aggregate/union types do" |
| 553 | " not have lvalues."); |
Ted Kremenek | c7df6d2 | 2008-10-18 04:08:49 +0000 | [diff] [blame] | 554 | |
Ted Kremenek | f8cd1b2 | 2008-10-18 04:15:35 +0000 | [diff] [blame] | 555 | Visit(Ex, Pred, Dst); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | //===----------------------------------------------------------------------===// |
| 560 | // Block entrance. (Update counters). |
| 561 | //===----------------------------------------------------------------------===// |
| 562 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 563 | bool GRExprEngine::ProcessBlockEntrance(CFGBlock* B, const GRState*, |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 564 | GRBlockCounter BC) { |
| 565 | |
| 566 | return BC.getNumVisited(B->getBlockID()) < 3; |
| 567 | } |
| 568 | |
| 569 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 570 | // Generic node creation. |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 573 | ExplodedNode* GRExprEngine::MakeNode(ExplodedNodeSet& Dst, Stmt* S, |
| 574 | ExplodedNode* Pred, const GRState* St, |
| 575 | ProgramPoint::Kind K, const void *tag) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 576 | assert (Builder && "GRStmtNodeBuilder not present."); |
| 577 | SaveAndRestore<const void*> OldTag(Builder->Tag); |
| 578 | Builder->Tag = tag; |
| 579 | return Builder->MakeNode(Dst, S, Pred, St, K); |
| 580 | } |
| 581 | |
| 582 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 583 | // Branch processing. |
| 584 | //===----------------------------------------------------------------------===// |
| 585 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 586 | const GRState* GRExprEngine::MarkBranch(const GRState* state, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 587 | Stmt* Terminator, |
| 588 | bool branchTaken) { |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 589 | |
| 590 | switch (Terminator->getStmtClass()) { |
| 591 | default: |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 592 | return state; |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 593 | |
| 594 | case Stmt::BinaryOperatorClass: { // '&&' and '||' |
| 595 | |
| 596 | BinaryOperator* B = cast<BinaryOperator>(Terminator); |
| 597 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 598 | |
| 599 | assert (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr); |
| 600 | |
| 601 | // For &&, if we take the true branch, then the value of the whole |
| 602 | // expression is that of the RHS expression. |
| 603 | // |
| 604 | // For ||, if we take the false branch, then the value of the whole |
| 605 | // expression is that of the RHS expression. |
| 606 | |
| 607 | Expr* Ex = (Op == BinaryOperator::LAnd && branchTaken) || |
| 608 | (Op == BinaryOperator::LOr && !branchTaken) |
| 609 | ? B->getRHS() : B->getLHS(); |
| 610 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 611 | return state->BindExpr(B, UndefinedVal(Ex)); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | case Stmt::ConditionalOperatorClass: { // ?: |
| 615 | |
| 616 | ConditionalOperator* C = cast<ConditionalOperator>(Terminator); |
| 617 | |
| 618 | // For ?, if branchTaken == true then the value is either the LHS or |
| 619 | // the condition itself. (GNU extension). |
| 620 | |
| 621 | Expr* Ex; |
| 622 | |
| 623 | if (branchTaken) |
| 624 | Ex = C->getLHS() ? C->getLHS() : C->getCond(); |
| 625 | else |
| 626 | Ex = C->getRHS(); |
| 627 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 628 | return state->BindExpr(C, UndefinedVal(Ex)); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | case Stmt::ChooseExprClass: { // ?: |
| 632 | |
| 633 | ChooseExpr* C = cast<ChooseExpr>(Terminator); |
| 634 | |
| 635 | Expr* Ex = branchTaken ? C->getLHS() : C->getRHS(); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 636 | return state->BindExpr(C, UndefinedVal(Ex)); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
Ted Kremenek | 6ae8a36 | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 641 | /// RecoverCastedSymbol - A helper function for ProcessBranch that is used |
| 642 | /// to try to recover some path-sensitivity for casts of symbolic |
| 643 | /// integers that promote their values (which are currently not tracked well). |
| 644 | /// This function returns the SVal bound to Condition->IgnoreCasts if all the |
| 645 | // cast(s) did was sign-extend the original value. |
| 646 | static SVal RecoverCastedSymbol(GRStateManager& StateMgr, const GRState* state, |
| 647 | Stmt* Condition, ASTContext& Ctx) { |
| 648 | |
| 649 | Expr *Ex = dyn_cast<Expr>(Condition); |
| 650 | if (!Ex) |
| 651 | return UnknownVal(); |
| 652 | |
| 653 | uint64_t bits = 0; |
| 654 | bool bitsInit = false; |
| 655 | |
| 656 | while (CastExpr *CE = dyn_cast<CastExpr>(Ex)) { |
| 657 | QualType T = CE->getType(); |
| 658 | |
| 659 | if (!T->isIntegerType()) |
| 660 | return UnknownVal(); |
| 661 | |
| 662 | uint64_t newBits = Ctx.getTypeSize(T); |
| 663 | if (!bitsInit || newBits < bits) { |
| 664 | bitsInit = true; |
| 665 | bits = newBits; |
| 666 | } |
| 667 | |
| 668 | Ex = CE->getSubExpr(); |
| 669 | } |
| 670 | |
| 671 | // We reached a non-cast. Is it a symbolic value? |
| 672 | QualType T = Ex->getType(); |
| 673 | |
| 674 | if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits) |
| 675 | return UnknownVal(); |
| 676 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 677 | return state->getSVal(Ex); |
Ted Kremenek | 6ae8a36 | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 680 | void GRExprEngine::ProcessBranch(Stmt* Condition, Stmt* Term, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 681 | GRBranchNodeBuilder& builder) { |
Ted Kremenek | 0bed8a1 | 2009-03-11 02:41:36 +0000 | [diff] [blame] | 682 | |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 683 | // Check for NULL conditions; e.g. "for(;;)" |
| 684 | if (!Condition) { |
| 685 | builder.markInfeasible(false); |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
| 688 | |
Ted Kremenek | 21028dd | 2009-03-11 03:54:24 +0000 | [diff] [blame] | 689 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), |
| 690 | Condition->getLocStart(), |
| 691 | "Error evaluating branch"); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 692 | |
| 693 | const GRState* PrevState = builder.getState(); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 694 | SVal V = PrevState->getSVal(Condition); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 695 | |
| 696 | switch (V.getBaseKind()) { |
| 697 | default: |
| 698 | break; |
| 699 | |
Ted Kremenek | 6ae8a36 | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 700 | case SVal::UnknownKind: { |
| 701 | if (Expr *Ex = dyn_cast<Expr>(Condition)) { |
| 702 | if (Ex->getType()->isIntegerType()) { |
| 703 | // Try to recover some path-sensitivity. Right now casts of symbolic |
| 704 | // integers that promote their values are currently not tracked well. |
| 705 | // If 'Condition' is such an expression, try and recover the |
| 706 | // underlying value and use that instead. |
| 707 | SVal recovered = RecoverCastedSymbol(getStateManager(), |
| 708 | builder.getState(), Condition, |
| 709 | getContext()); |
| 710 | |
| 711 | if (!recovered.isUnknown()) { |
| 712 | V = recovered; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
Ted Kremenek | 58b3321 | 2008-02-26 19:40:44 +0000 | [diff] [blame] | 718 | builder.generateNode(MarkBranch(PrevState, Term, true), true); |
| 719 | builder.generateNode(MarkBranch(PrevState, Term, false), false); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 720 | return; |
Ted Kremenek | 6ae8a36 | 2009-03-13 16:32:54 +0000 | [diff] [blame] | 721 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 722 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 723 | case SVal::UndefinedKind: { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 724 | ExplodedNode* N = builder.generateNode(PrevState, true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 725 | |
| 726 | if (N) { |
| 727 | N->markAsSink(); |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 728 | UndefBranches.insert(N); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | builder.markInfeasible(false); |
| 732 | return; |
| 733 | } |
| 734 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 735 | |
Ted Kremenek | 6a6719a | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 736 | // Process the true branch. |
Ted Kremenek | 5200354 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 737 | if (builder.isFeasible(true)) { |
| 738 | if (const GRState *state = PrevState->assume(V, true)) |
| 739 | builder.generateNode(MarkBranch(state, Term, true), true); |
| 740 | else |
| 741 | builder.markInfeasible(true); |
| 742 | } |
Ted Kremenek | 6a6719a | 2008-02-29 20:27:50 +0000 | [diff] [blame] | 743 | |
| 744 | // Process the false branch. |
Ted Kremenek | 5200354 | 2009-07-20 18:44:36 +0000 | [diff] [blame] | 745 | if (builder.isFeasible(false)) { |
| 746 | if (const GRState *state = PrevState->assume(V, false)) |
| 747 | builder.generateNode(MarkBranch(state, Term, false), false); |
| 748 | else |
| 749 | builder.markInfeasible(false); |
| 750 | } |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 753 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 754 | /// nodes by processing the 'effects' of a computed goto jump. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 755 | void GRExprEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 757 | const GRState *state = builder.getState(); |
| 758 | SVal V = state->getSVal(builder.getTarget()); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 759 | |
| 760 | // Three possibilities: |
| 761 | // |
| 762 | // (1) We know the computed label. |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 763 | // (2) The label is NULL (or some other constant), or Undefined. |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 764 | // (3) We have no clue about the label. Dispatch to all targets. |
| 765 | // |
| 766 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 767 | typedef GRIndirectGotoNodeBuilder::iterator iterator; |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 768 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 769 | if (isa<loc::GotoLabel>(V)) { |
| 770 | LabelStmt* L = cast<loc::GotoLabel>(V).getLabel(); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 771 | |
| 772 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 773 | if (I.getLabel() == L) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 774 | builder.generateNode(I, state); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 775 | return; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | assert (false && "No block with label."); |
| 780 | return; |
| 781 | } |
| 782 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 783 | if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 784 | // Dispatch to the first target and mark it as a sink. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 785 | ExplodedNode* N = builder.generateNode(builder.begin(), state, true); |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 786 | UndefBranches.insert(N); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | |
| 790 | // This is really a catch-all. We don't support symbolics yet. |
Ted Kremenek | b3cfd58 | 2009-04-23 17:49:43 +0000 | [diff] [blame] | 791 | // FIXME: Implement dispatch for symbolic pointers. |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 792 | |
| 793 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 794 | builder.generateNode(I, state); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 795 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 797 | |
| 798 | void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 799 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 800 | |
Zhongxing Xu | cc02553 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 801 | assert (Ex == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(Ex)); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 802 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 803 | const GRState* state = GetState(Pred); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 804 | SVal X = state->getSVal(Ex); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 805 | |
| 806 | assert (X.isUndef()); |
| 807 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 808 | Expr *SE = (Expr*) cast<UndefinedVal>(X).getData(); |
| 809 | assert(SE); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 810 | X = state->getSVal(SE); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 811 | |
| 812 | // Make sure that we invalidate the previous binding. |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 813 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, X, true)); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 816 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 817 | /// nodes by processing the 'effects' of a switch statement. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 818 | void GRExprEngine::ProcessSwitch(GRSwitchNodeBuilder& builder) { |
| 819 | typedef GRSwitchNodeBuilder::iterator iterator; |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 820 | const GRState* state = builder.getState(); |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 821 | Expr* CondE = builder.getCondition(); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 822 | SVal CondV = state->getSVal(CondE); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 824 | if (CondV.isUndef()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 825 | ExplodedNode* N = builder.generateDefaultCaseNode(state, true); |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 826 | UndefBranches.insert(N); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 827 | return; |
| 828 | } |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 829 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 830 | const GRState* DefaultSt = state; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 831 | bool defaultIsFeasible = false; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 832 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 833 | for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) { |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 834 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 835 | |
| 836 | // Evaluate the LHS of the case value. |
| 837 | Expr::EvalResult V1; |
| 838 | bool b = Case->getLHS()->Evaluate(V1, getContext()); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 839 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 840 | // Sanity checks. These go away in Release builds. |
| 841 | assert(b && V1.Val.isInt() && !V1.HasSideEffects |
| 842 | && "Case condition must evaluate to an integer constant."); |
| 843 | b = b; // silence unused variable warning |
| 844 | assert(V1.Val.getInt().getBitWidth() == |
| 845 | getContext().getTypeSize(CondE->getType())); |
| 846 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 847 | // Get the RHS of the case, if it exists. |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 848 | Expr::EvalResult V2; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 849 | |
| 850 | if (Expr* E = Case->getRHS()) { |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 851 | b = E->Evaluate(V2, getContext()); |
| 852 | assert(b && V2.Val.isInt() && !V2.HasSideEffects |
| 853 | && "Case condition must evaluate to an integer constant."); |
| 854 | b = b; // silence unused variable warning |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 855 | } |
Ted Kremenek | 14a1140 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 856 | else |
| 857 | V2 = V1; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 858 | |
| 859 | // FIXME: Eventually we should replace the logic below with a range |
| 860 | // comparison, rather than concretize the values within the range. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 861 | // This should be easy once we have "ranges" for NonLVals. |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 862 | |
Ted Kremenek | 14a1140 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 863 | do { |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 864 | nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1.Val.getInt())); |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 865 | SVal Res = EvalBinOp(DefaultSt, BinaryOperator::EQ, CondV, CaseVal, |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 866 | getContext().IntTy); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 867 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 868 | // Now "assume" that the case matches. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 869 | if (const GRState* stateNew = state->assume(Res, true)) { |
| 870 | builder.generateCaseStmtNode(I, stateNew); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 871 | |
| 872 | // If CondV evaluates to a constant, then we know that this |
| 873 | // is the *only* case that we can take, so stop evaluating the |
| 874 | // others. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 875 | if (isa<nonloc::ConcreteInt>(CondV)) |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 876 | return; |
| 877 | } |
| 878 | |
| 879 | // Now "assume" that the case doesn't match. Add this state |
| 880 | // to the default state (if it is feasible). |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 881 | if (const GRState *stateNew = DefaultSt->assume(Res, false)) { |
| 882 | defaultIsFeasible = true; |
| 883 | DefaultSt = stateNew; |
Ted Kremenek | 5014ab1 | 2008-04-23 05:03:18 +0000 | [diff] [blame] | 884 | } |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 885 | |
Ted Kremenek | 14a1140 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 886 | // Concretize the next value in the range. |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 887 | if (V1.Val.getInt() == V2.Val.getInt()) |
Ted Kremenek | 14a1140 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 888 | break; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 889 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 890 | ++V1.Val.getInt(); |
| 891 | assert (V1.Val.getInt() <= V2.Val.getInt()); |
Ted Kremenek | 14a1140 | 2008-03-17 22:17:56 +0000 | [diff] [blame] | 892 | |
| 893 | } while (true); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | // If we reach here, than we know that the default branch is |
| 897 | // possible. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 898 | if (defaultIsFeasible) builder.generateDefaultCaseNode(DefaultSt); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 901 | //===----------------------------------------------------------------------===// |
| 902 | // Transfer functions: logical operations ('&&', '||'). |
| 903 | //===----------------------------------------------------------------------===// |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 904 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 905 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, ExplodedNode* Pred, |
| 906 | ExplodedNodeSet& Dst) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 907 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 908 | assert(B->getOpcode() == BinaryOperator::LAnd || |
| 909 | B->getOpcode() == BinaryOperator::LOr); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 910 | |
Zhongxing Xu | cc02553 | 2009-08-25 03:33:41 +0000 | [diff] [blame] | 911 | assert(B == CurrentStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(B)); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 912 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 913 | const GRState* state = GetState(Pred); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 914 | SVal X = state->getSVal(B); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 915 | assert(X.isUndef()); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 916 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 917 | Expr* Ex = (Expr*) cast<UndefinedVal>(X).getData(); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 918 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 919 | assert(Ex); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 920 | |
| 921 | if (Ex == B->getRHS()) { |
| 922 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 923 | X = state->getSVal(Ex); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 924 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 925 | // Handle undefined values. |
Ted Kremenek | 58b3321 | 2008-02-26 19:40:44 +0000 | [diff] [blame] | 926 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 927 | if (X.isUndef()) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 928 | MakeNode(Dst, B, Pred, state->BindExpr(B, X)); |
Ted Kremenek | 58b3321 | 2008-02-26 19:40:44 +0000 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 932 | // We took the RHS. Because the value of the '&&' or '||' expression must |
| 933 | // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0 |
| 934 | // or 1. Alternatively, we could take a lazy approach, and calculate this |
| 935 | // value later when necessary. We don't have the machinery in place for |
| 936 | // this right now, and since most logical expressions are used for branches, |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 937 | // the payoff is not likely to be large. Instead, we do eager evaluation. |
| 938 | if (const GRState *newState = state->assume(X, true)) |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 939 | MakeNode(Dst, B, Pred, |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 940 | newState->BindExpr(B, ValMgr.makeIntVal(1U, B->getType()))); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 941 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 942 | if (const GRState *newState = state->assume(X, false)) |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 943 | MakeNode(Dst, B, Pred, |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 944 | newState->BindExpr(B, ValMgr.makeIntVal(0U, B->getType()))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 945 | } |
| 946 | else { |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 947 | // We took the LHS expression. Depending on whether we are '&&' or |
| 948 | // '||' we know what the value of the expression is via properties of |
| 949 | // the short-circuiting. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 950 | X = ValMgr.makeIntVal(B->getOpcode() == BinaryOperator::LAnd ? 0U : 1U, |
| 951 | B->getType()); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 952 | MakeNode(Dst, B, Pred, state->BindExpr(B, X)); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 953 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 954 | } |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 955 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 956 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 957 | // Transfer functions: Loads and stores. |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 958 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 959 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 960 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr *Ex, ExplodedNode *Pred, |
| 961 | ExplodedNodeSet &Dst, bool asLValue) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 962 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 963 | const GRState* state = GetState(Pred); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 965 | const NamedDecl* D = Ex->getDecl(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 966 | |
| 967 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 968 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 969 | SVal V = state->getLValue(VD, Pred->getLocationContext()); |
Zhongxing Xu | a758173 | 2008-10-17 02:20:14 +0000 | [diff] [blame] | 970 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 971 | if (asLValue) |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 972 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), |
Ted Kremenek | 7090d54 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 973 | ProgramPoint::PostLValueKind); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 974 | else |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 975 | EvalLoad(Dst, Ex, Pred, state, V); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 976 | return; |
| 977 | |
| 978 | } else if (const EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) { |
| 979 | assert(!asLValue && "EnumConstantDecl does not have lvalue."); |
| 980 | |
Zhongxing Xu | 3038c5a | 2009-06-23 06:13:19 +0000 | [diff] [blame] | 981 | SVal V = ValMgr.makeIntVal(ED->getInitVal()); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 982 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V)); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 983 | return; |
| 984 | |
| 985 | } else if (const FunctionDecl* FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | 5631a73 | 2008-11-15 02:35:08 +0000 | [diff] [blame] | 986 | assert(asLValue); |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 987 | SVal V = ValMgr.getFunctionPointer(FD); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 988 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), |
Ted Kremenek | 7090d54 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 989 | ProgramPoint::PostLValueKind); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 990 | return; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 991 | } |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 992 | |
| 993 | assert (false && |
| 994 | "ValueDecl support for this ValueDecl not implemented."); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 997 | /// VisitArraySubscriptExpr - Transfer function for array accesses |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 998 | void GRExprEngine::VisitArraySubscriptExpr(ArraySubscriptExpr* A, |
| 999 | ExplodedNode* Pred, |
| 1000 | ExplodedNodeSet& Dst, bool asLValue){ |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 1001 | |
| 1002 | Expr* Base = A->getBase()->IgnoreParens(); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1003 | Expr* Idx = A->getIdx()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1004 | ExplodedNodeSet Tmp; |
Ted Kremenek | 265a305 | 2009-02-24 02:23:11 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (Base->getType()->isVectorType()) { |
| 1007 | // For vector types get its lvalue. |
| 1008 | // FIXME: This may not be correct. Is the rvalue of a vector its location? |
| 1009 | // In fact, I think this is just a hack. We need to get the right |
| 1010 | // semantics. |
| 1011 | VisitLValue(Base, Pred, Tmp); |
| 1012 | } |
| 1013 | else |
| 1014 | Visit(Base, Pred, Tmp); // Get Base's rvalue, which should be an LocVal. |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1015 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1016 | for (ExplodedNodeSet::iterator I1=Tmp.begin(), E1=Tmp.end(); I1!=E1; ++I1) { |
| 1017 | ExplodedNodeSet Tmp2; |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 1018 | Visit(Idx, *I1, Tmp2); // Evaluate the index. |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1019 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1020 | for (ExplodedNodeSet::iterator I2=Tmp2.begin(),E2=Tmp2.end();I2!=E2; ++I2) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1021 | const GRState* state = GetState(*I2); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1022 | SVal V = state->getLValue(A->getType(), state->getSVal(Base), |
| 1023 | state->getSVal(Idx)); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1024 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1025 | if (asLValue) |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1026 | MakeNode(Dst, A, *I2, state->BindExpr(A, V), |
Ted Kremenek | 7090d54 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 1027 | ProgramPoint::PostLValueKind); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1028 | else |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1029 | EvalLoad(Dst, A, *I2, state, V); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 1030 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1031 | } |
Ted Kremenek | 540cbe2 | 2008-04-22 04:56:29 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1034 | /// VisitMemberExpr - Transfer function for member expressions. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1035 | void GRExprEngine::VisitMemberExpr(MemberExpr* M, ExplodedNode* Pred, |
| 1036 | ExplodedNodeSet& Dst, bool asLValue) { |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1037 | |
| 1038 | Expr* Base = M->getBase()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1039 | ExplodedNodeSet Tmp; |
Ted Kremenek | 5c456fe | 2008-10-18 03:28:48 +0000 | [diff] [blame] | 1040 | |
| 1041 | if (M->isArrow()) |
| 1042 | Visit(Base, Pred, Tmp); // p->f = ... or ... = p->f |
| 1043 | else |
| 1044 | VisitLValue(Base, Pred, Tmp); // x.f = ... or ... = x.f |
| 1045 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1046 | FieldDecl *Field = dyn_cast<FieldDecl>(M->getMemberDecl()); |
| 1047 | if (!Field) // FIXME: skipping member expressions for non-fields |
| 1048 | return; |
| 1049 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1050 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1051 | const GRState* state = GetState(*I); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 1052 | // FIXME: Should we insert some assumption logic in here to determine |
| 1053 | // if "Base" is a valid piece of memory? Before we put this assumption |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1054 | // later when using FieldOffset lvals (which we no longer have). |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1055 | SVal L = state->getLValue(state->getSVal(Base), Field); |
Ted Kremenek | d9bc33e | 2008-10-17 00:51:01 +0000 | [diff] [blame] | 1056 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1057 | if (asLValue) |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1058 | MakeNode(Dst, M, *I, state->BindExpr(M, L), |
Ted Kremenek | 7090d54 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 1059 | ProgramPoint::PostLValueKind); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 1060 | else |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1061 | EvalLoad(Dst, M, *I, state, L); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1062 | } |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1065 | /// EvalBind - Handle the semantics of binding a value to a specific location. |
| 1066 | /// This method is used by EvalStore and (soon) VisitDeclStmt, and others. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1067 | void GRExprEngine::EvalBind(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred, |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1068 | const GRState* state, SVal location, SVal Val) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1069 | |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1070 | const GRState* newState = 0; |
| 1071 | |
| 1072 | if (location.isUnknown()) { |
| 1073 | // We know that the new state will be the same as the old state since |
| 1074 | // the location of the binding is "unknown". Consequently, there |
| 1075 | // is no reason to just create a new node. |
| 1076 | newState = state; |
| 1077 | } |
| 1078 | else { |
| 1079 | // We are binding to a value other than 'unknown'. Perform the binding |
| 1080 | // using the StoreManager. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1081 | newState = state->bindLoc(cast<Loc>(location), Val); |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1082 | } |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1083 | |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1084 | // The next thing to do is check if the GRTransferFuncs object wants to |
| 1085 | // update the state based on the new binding. If the GRTransferFunc object |
| 1086 | // doesn't do anything, just auto-propagate the current state. |
| 1087 | GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, Pred, newState, Ex, |
| 1088 | newState != state); |
| 1089 | |
| 1090 | getTF().EvalBind(BuilderRef, location, Val); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /// EvalStore - Handle the semantics of a store via an assignment. |
| 1094 | /// @param Dst The node set to store generated state nodes |
| 1095 | /// @param Ex The expression representing the location of the store |
| 1096 | /// @param state The current simulation state |
| 1097 | /// @param location The location to store the value |
| 1098 | /// @param Val The value to be stored |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1099 | void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred, |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1100 | const GRState* state, SVal location, SVal Val, |
| 1101 | const void *tag) { |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 1102 | |
| 1103 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
| 1104 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1105 | // Evaluate the location (checks for bad dereferences). |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1106 | Pred = EvalLocation(Ex, Pred, state, location, tag); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1107 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1108 | if (!Pred) |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1109 | return; |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 1110 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1111 | assert (!location.isUndef()); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1112 | state = GetState(Pred); |
| 1113 | |
| 1114 | // Proceed with the store. |
| 1115 | SaveAndRestore<ProgramPoint::Kind> OldSPointKind(Builder->PointKind); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1116 | SaveAndRestore<const void*> OldTag(Builder->Tag); |
| 1117 | Builder->PointKind = ProgramPoint::PostStoreKind; |
| 1118 | Builder->Tag = tag; |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1119 | EvalBind(Dst, Ex, Pred, state, location, Val); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1122 | void GRExprEngine::EvalLoad(ExplodedNodeSet& Dst, Expr* Ex, ExplodedNode* Pred, |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1123 | const GRState* state, SVal location, |
| 1124 | const void *tag) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1125 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1126 | // Evaluate the location (checks for bad dereferences). |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1127 | Pred = EvalLocation(Ex, Pred, state, location, tag); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1128 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1129 | if (!Pred) |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1130 | return; |
| 1131 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1132 | state = GetState(Pred); |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1133 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1134 | // Proceed with the load. |
Ted Kremenek | 982e674 | 2008-08-28 18:43:46 +0000 | [diff] [blame] | 1135 | ProgramPoint::Kind K = ProgramPoint::PostLoadKind; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1136 | |
| 1137 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 1138 | // for the contents of values. We need a better approach. |
| 1139 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1140 | if (location.isUnknown()) { |
Ted Kremenek | 436f2b9 | 2008-04-30 04:23:07 +0000 | [diff] [blame] | 1141 | // This is important. We must nuke the old binding. |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1142 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, UnknownVal()), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 1143 | K, tag); |
Ted Kremenek | 436f2b9 | 2008-04-30 04:23:07 +0000 | [diff] [blame] | 1144 | } |
Zhongxing Xu | d5b499d | 2008-11-28 08:34:30 +0000 | [diff] [blame] | 1145 | else { |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1146 | SVal V = state->getSVal(cast<Loc>(location), Ex->getType()); |
Ted Kremenek | 43d74a5 | 2009-07-11 04:38:49 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Casts can create weird scenarios where a location must be implicitly |
| 1149 | // converted to something else. For example: |
| 1150 | // |
| 1151 | // void *x; |
| 1152 | // int *y = (int*) &x; // void** -> int* cast. |
| 1153 | // invalidate(y); // 'x' now binds to a symbolic region |
| 1154 | // int z = *y; |
| 1155 | // |
Zhongxing Xu | 8203725 | 2009-07-14 01:12:46 +0000 | [diff] [blame] | 1156 | //if (isa<Loc>(V) && !Loc::IsLocType(Ex->getType())) { |
| 1157 | // V = EvalCast(V, Ex->getType()); |
| 1158 | //} |
Ted Kremenek | 43d74a5 | 2009-07-11 04:38:49 +0000 | [diff] [blame] | 1159 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1160 | MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, V), K, tag); |
Zhongxing Xu | d5b499d | 2008-11-28 08:34:30 +0000 | [diff] [blame] | 1161 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1164 | void GRExprEngine::EvalStore(ExplodedNodeSet& Dst, Expr* Ex, Expr* StoreE, |
| 1165 | ExplodedNode* Pred, const GRState* state, |
| 1166 | SVal location, SVal Val, const void *tag) { |
Ted Kremenek | 82bae3f | 2008-09-20 01:50:34 +0000 | [diff] [blame] | 1167 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1168 | ExplodedNodeSet TmpDst; |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1169 | EvalStore(TmpDst, StoreE, Pred, state, location, Val, tag); |
Ted Kremenek | 82bae3f | 2008-09-20 01:50:34 +0000 | [diff] [blame] | 1170 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1171 | for (ExplodedNodeSet::iterator I=TmpDst.begin(), E=TmpDst.end(); I!=E; ++I) |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1172 | MakeNode(Dst, Ex, *I, (*I)->getState(), ProgramPoint::PostStmtKind, tag); |
Ted Kremenek | 82bae3f | 2008-09-20 01:50:34 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1175 | ExplodedNode* GRExprEngine::EvalLocation(Stmt* Ex, ExplodedNode* Pred, |
| 1176 | const GRState* state, SVal location, |
| 1177 | const void *tag) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1178 | |
| 1179 | SaveAndRestore<const void*> OldTag(Builder->Tag); |
| 1180 | Builder->Tag = tag; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Check for loads/stores from/to undefined values. |
| 1183 | if (location.isUndef()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1184 | ExplodedNode* N = |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1185 | Builder->generateNode(Ex, state, Pred, |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1186 | ProgramPoint::PostUndefLocationCheckFailedKind); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1187 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1188 | if (N) { |
| 1189 | N->markAsSink(); |
| 1190 | UndefDeref.insert(N); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1193 | return 0; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | // Check for loads/stores from/to unknown locations. Treat as No-Ops. |
| 1197 | if (location.isUnknown()) |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1198 | return Pred; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1199 | |
| 1200 | // During a load, one of two possible situations arise: |
| 1201 | // (1) A crash, because the location (pointer) was NULL. |
| 1202 | // (2) The location (pointer) is not NULL, and the dereference works. |
| 1203 | // |
| 1204 | // We add these assumptions. |
| 1205 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1206 | Loc LV = cast<Loc>(location); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1207 | |
| 1208 | // "Assume" that the pointer is not NULL. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1209 | const GRState *StNotNull = state->assume(LV, true); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1210 | |
| 1211 | // "Assume" that the pointer is NULL. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1212 | const GRState *StNull = state->assume(LV, false); |
Zhongxing Xu | a1718c7 | 2009-04-03 07:33:13 +0000 | [diff] [blame] | 1213 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1214 | if (StNull) { |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 1215 | // Use the Generic Data Map to mark in the state what lval was null. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1216 | const SVal* PersistentLV = getBasicVals().getPersistentSVal(LV); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1217 | StNull = StNull->set<GRState::NullDerefTag>(PersistentLV); |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 1218 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1219 | // We don't use "MakeNode" here because the node will be a sink |
| 1220 | // and we have no intention of processing it later. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1221 | ExplodedNode* NullNode = |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1222 | Builder->generateNode(Ex, StNull, Pred, |
| 1223 | ProgramPoint::PostNullCheckFailedKind); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1224 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1225 | if (NullNode) { |
| 1226 | NullNode->markAsSink(); |
| 1227 | if (StNotNull) ImplicitNullDeref.insert(NullNode); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1228 | else ExplicitNullDeref.insert(NullNode); |
| 1229 | } |
| 1230 | } |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1231 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1232 | if (!StNotNull) |
| 1233 | return NULL; |
Zhongxing Xu | 60156f0 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1234 | |
Ted Kremenek | 39abcdf | 2009-08-01 05:59:39 +0000 | [diff] [blame] | 1235 | // FIXME: Temporarily disable out-of-bounds checking until we make |
| 1236 | // the logic reflect recent changes to CastRegion and friends. |
| 1237 | #if 0 |
Zhongxing Xu | 60156f0 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1238 | // Check for out-of-bound array access. |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1239 | if (isa<loc::MemRegionVal>(LV)) { |
Zhongxing Xu | 60156f0 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1240 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
| 1241 | if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) { |
| 1242 | // Get the index of the accessed element. |
| 1243 | SVal Idx = ER->getIndex(); |
| 1244 | // Get the extent of the array. |
Zhongxing Xu | 1ed8d4b | 2008-11-24 07:02:06 +0000 | [diff] [blame] | 1245 | SVal NumElements = getStoreManager().getSizeInElements(StNotNull, |
| 1246 | ER->getSuperRegion()); |
Zhongxing Xu | 60156f0 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1247 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1248 | const GRState * StInBound = StNotNull->assumeInBound(Idx, NumElements, |
| 1249 | true); |
| 1250 | const GRState* StOutBound = StNotNull->assumeInBound(Idx, NumElements, |
| 1251 | false); |
Zhongxing Xu | 60156f0 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1252 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1253 | if (StOutBound) { |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1254 | // Report warning. Make sink node manually. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1255 | ExplodedNode* OOBNode = |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1256 | Builder->generateNode(Ex, StOutBound, Pred, |
| 1257 | ProgramPoint::PostOutOfBoundsCheckFailedKind); |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (OOBNode) { |
| 1260 | OOBNode->markAsSink(); |
| 1261 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1262 | if (StInBound) |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 1263 | ImplicitOOBMemAccesses.insert(OOBNode); |
| 1264 | else |
| 1265 | ExplicitOOBMemAccesses.insert(OOBNode); |
| 1266 | } |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1269 | if (!StInBound) |
| 1270 | return NULL; |
| 1271 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1272 | StNotNull = StInBound; |
Zhongxing Xu | 60156f0 | 2008-11-08 03:45:42 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
Ted Kremenek | 39abcdf | 2009-08-01 05:59:39 +0000 | [diff] [blame] | 1275 | #endif |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 1276 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1277 | // Generate a new node indicating the checks succeed. |
| 1278 | return Builder->generateNode(Ex, StNotNull, Pred, |
| 1279 | ProgramPoint::PostLocationChecksSucceedKind); |
Ted Kremenek | ec96a2d | 2008-04-16 18:39:06 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1282 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1283 | // Transfer function: OSAtomics. |
| 1284 | // |
| 1285 | // FIXME: Eventually refactor into a more "plugin" infrastructure. |
| 1286 | //===----------------------------------------------------------------------===// |
| 1287 | |
| 1288 | // Mac OS X: |
| 1289 | // http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3 |
| 1290 | // atomic.3.html |
| 1291 | // |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1292 | static bool EvalOSAtomicCompareAndSwap(ExplodedNodeSet& Dst, |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1293 | GRExprEngine& Engine, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1294 | GRStmtNodeBuilder& Builder, |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1295 | CallExpr* CE, SVal L, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1296 | ExplodedNode* Pred) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1297 | |
| 1298 | // Not enough arguments to match OSAtomicCompareAndSwap? |
| 1299 | if (CE->getNumArgs() != 3) |
| 1300 | return false; |
| 1301 | |
| 1302 | ASTContext &C = Engine.getContext(); |
| 1303 | Expr *oldValueExpr = CE->getArg(0); |
| 1304 | QualType oldValueType = C.getCanonicalType(oldValueExpr->getType()); |
| 1305 | |
| 1306 | Expr *newValueExpr = CE->getArg(1); |
| 1307 | QualType newValueType = C.getCanonicalType(newValueExpr->getType()); |
| 1308 | |
| 1309 | // Do the types of 'oldValue' and 'newValue' match? |
| 1310 | if (oldValueType != newValueType) |
| 1311 | return false; |
| 1312 | |
| 1313 | Expr *theValueExpr = CE->getArg(2); |
Ted Kremenek | 39abcdf | 2009-08-01 05:59:39 +0000 | [diff] [blame] | 1314 | const PointerType *theValueType = |
| 1315 | theValueExpr->getType()->getAs<PointerType>(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1316 | |
| 1317 | // theValueType not a pointer? |
| 1318 | if (!theValueType) |
| 1319 | return false; |
| 1320 | |
| 1321 | QualType theValueTypePointee = |
| 1322 | C.getCanonicalType(theValueType->getPointeeType()).getUnqualifiedType(); |
| 1323 | |
| 1324 | // The pointee must match newValueType and oldValueType. |
| 1325 | if (theValueTypePointee != newValueType) |
| 1326 | return false; |
| 1327 | |
| 1328 | static unsigned magic_load = 0; |
| 1329 | static unsigned magic_store = 0; |
| 1330 | |
| 1331 | const void *OSAtomicLoadTag = &magic_load; |
| 1332 | const void *OSAtomicStoreTag = &magic_store; |
| 1333 | |
| 1334 | // Load 'theValue'. |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1335 | const GRState *state = Pred->getState(); |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1336 | ExplodedNodeSet Tmp; |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 1337 | SVal location = state->getSVal(theValueExpr); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1338 | Engine.EvalLoad(Tmp, theValueExpr, Pred, state, location, OSAtomicLoadTag); |
| 1339 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1340 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1341 | I != E; ++I) { |
| 1342 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1343 | ExplodedNode *N = *I; |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1344 | const GRState *stateLoad = N->getState(); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1345 | SVal theValueVal = stateLoad->getSVal(theValueExpr); |
| 1346 | SVal oldValueVal = stateLoad->getSVal(oldValueExpr); |
Ted Kremenek | 1ff8339 | 2009-07-20 20:38:59 +0000 | [diff] [blame] | 1347 | |
| 1348 | // FIXME: Issue an error. |
| 1349 | if (theValueVal.isUndef() || oldValueVal.isUndef()) { |
| 1350 | return false; |
| 1351 | } |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 1352 | |
| 1353 | SValuator &SVator = Engine.getSValuator(); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Perform the comparison. |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 1356 | SVal Cmp = SVator.EvalBinOp(stateLoad, BinaryOperator::EQ, theValueVal, |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 1357 | oldValueVal, Engine.getContext().IntTy); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1358 | |
| 1359 | const GRState *stateEqual = stateLoad->assume(Cmp, true); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1360 | |
| 1361 | // Were they equal? |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1362 | if (stateEqual) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1363 | // Perform the store. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1364 | ExplodedNodeSet TmpStore; |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1365 | SVal val = stateEqual->getSVal(newValueExpr); |
| 1366 | |
| 1367 | // Handle implicit value casts. |
| 1368 | if (const TypedRegion *R = |
| 1369 | dyn_cast_or_null<TypedRegion>(location.getAsRegion())) { |
Ted Kremenek | ff4264d | 2009-08-25 18:44:25 +0000 | [diff] [blame] | 1370 | llvm::tie(state, val) = SVator.EvalCast(val, state, R->getValueType(C), |
| 1371 | newValueExpr->getType()); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1374 | Engine.EvalStore(TmpStore, theValueExpr, N, stateEqual, location, |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 1375 | val, OSAtomicStoreTag); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1376 | |
| 1377 | // Now bind the result of the comparison. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1378 | for (ExplodedNodeSet::iterator I2 = TmpStore.begin(), |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1379 | E2 = TmpStore.end(); I2 != E2; ++I2) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1380 | ExplodedNode *predNew = *I2; |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1381 | const GRState *stateNew = predNew->getState(); |
| 1382 | SVal Res = Engine.getValueManager().makeTruthVal(true, CE->getType()); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1383 | Engine.MakeNode(Dst, CE, predNew, stateNew->BindExpr(CE, Res)); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // Were they not equal? |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1388 | if (const GRState *stateNotEqual = stateLoad->assume(Cmp, false)) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1389 | SVal Res = Engine.getValueManager().makeTruthVal(false, CE->getType()); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1390 | Engine.MakeNode(Dst, CE, N, stateNotEqual->BindExpr(CE, Res)); |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1397 | static bool EvalOSAtomic(ExplodedNodeSet& Dst, |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1398 | GRExprEngine& Engine, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1399 | GRStmtNodeBuilder& Builder, |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1400 | CallExpr* CE, SVal L, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 1401 | ExplodedNode* Pred) { |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1402 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
| 1403 | if (!FD) |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1404 | return false; |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1405 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1406 | const char *FName = FD->getNameAsCString(); |
| 1407 | |
| 1408 | // Check for compare and swap. |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 1409 | if (strncmp(FName, "OSAtomicCompareAndSwap", 22) == 0 || |
| 1410 | strncmp(FName, "objc_atomicCompareAndSwap", 25) == 0) |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1411 | return EvalOSAtomicCompareAndSwap(Dst, Engine, Builder, CE, L, Pred); |
Ted Kremenek | b3bf76f | 2009-04-11 00:54:13 +0000 | [diff] [blame] | 1412 | |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1413 | // FIXME: Other atomics. |
| 1414 | return false; |
| 1415 | } |
| 1416 | |
| 1417 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1418 | // Transfer function: Function calls. |
| 1419 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1420 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1421 | void GRExprEngine::EvalCall(ExplodedNodeSet& Dst, CallExpr* CE, SVal L, |
| 1422 | ExplodedNode* Pred) { |
Ted Kremenek | 1670e40 | 2009-04-11 00:11:10 +0000 | [diff] [blame] | 1423 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
| 1424 | |
| 1425 | // FIXME: Allow us to chain together transfer functions. |
| 1426 | if (EvalOSAtomic(Dst, *this, *Builder, CE, L, Pred)) |
| 1427 | return; |
| 1428 | |
| 1429 | getTF().EvalCall(Dst, *this, *Builder, CE, L, Pred); |
| 1430 | } |
| 1431 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1432 | void GRExprEngine::VisitCall(CallExpr* CE, ExplodedNode* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1433 | CallExpr::arg_iterator AI, |
| 1434 | CallExpr::arg_iterator AE, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1435 | ExplodedNodeSet& Dst) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1436 | { |
| 1437 | // Determine the type of function we're calling (if available). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1438 | const FunctionProtoType *Proto = NULL; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1439 | QualType FnType = CE->getCallee()->IgnoreParens()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1440 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1441 | Proto = FnTypePtr->getPointeeType()->getAsFunctionProtoType(); |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1442 | |
| 1443 | VisitCallRec(CE, Pred, AI, AE, Dst, Proto, /*ParamIdx=*/0); |
| 1444 | } |
| 1445 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1446 | void GRExprEngine::VisitCallRec(CallExpr* CE, ExplodedNode* Pred, |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1447 | CallExpr::arg_iterator AI, |
| 1448 | CallExpr::arg_iterator AE, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1449 | ExplodedNodeSet& Dst, |
| 1450 | const FunctionProtoType *Proto, |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1451 | unsigned ParamIdx) { |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1452 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1453 | // Process the arguments. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1454 | if (AI != AE) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1455 | // If the call argument is being bound to a reference parameter, |
| 1456 | // visit it as an lvalue, not an rvalue. |
| 1457 | bool VisitAsLvalue = false; |
| 1458 | if (Proto && ParamIdx < Proto->getNumArgs()) |
| 1459 | VisitAsLvalue = Proto->getArgType(ParamIdx)->isReferenceType(); |
| 1460 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1461 | ExplodedNodeSet DstTmp; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1462 | if (VisitAsLvalue) |
| 1463 | VisitLValue(*AI, Pred, DstTmp); |
| 1464 | else |
| 1465 | Visit(*AI, Pred, DstTmp); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1466 | ++AI; |
| 1467 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1468 | for (ExplodedNodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; |
| 1469 | ++DI) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1470 | VisitCallRec(CE, *DI, AI, AE, Dst, Proto, ParamIdx + 1); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1471 | |
| 1472 | return; |
| 1473 | } |
| 1474 | |
| 1475 | // If we reach here we have processed all of the arguments. Evaluate |
| 1476 | // the callee expression. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1477 | ExplodedNodeSet DstTmp; |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1478 | Expr* Callee = CE->getCallee()->IgnoreParens(); |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 1479 | |
| 1480 | { // Enter new scope to make the lifetime of 'DstTmp2' bounded. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1481 | ExplodedNodeSet DstTmp2; |
Ted Kremenek | 5a5d98b | 2009-07-22 21:43:51 +0000 | [diff] [blame] | 1482 | Visit(Callee, Pred, DstTmp2); |
| 1483 | |
| 1484 | // Perform the previsit of the CallExpr, storing the results in DstTmp. |
| 1485 | CheckerVisit(CE, DstTmp, DstTmp2, true); |
| 1486 | } |
Ted Kremenek | a1354a5 | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1487 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1488 | // Finally, evaluate the function call. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1489 | for (ExplodedNodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1490 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1491 | const GRState* state = GetState(*DI); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1492 | SVal L = state->getSVal(Callee); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1493 | |
Ted Kremenek | a1354a5 | 2008-03-03 16:47:31 +0000 | [diff] [blame] | 1494 | // FIXME: Add support for symbolic function calls (calls involving |
| 1495 | // function pointer values that are symbolic). |
Zhongxing Xu | d99f361 | 2009-09-02 08:10:35 +0000 | [diff] [blame] | 1496 | |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1497 | // Check for the "noreturn" attribute. |
| 1498 | |
| 1499 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1500 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
| 1501 | if (FD) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1502 | if (FD->getAttr<NoReturnAttr>() || |
| 1503 | FD->getAttr<AnalyzerNoReturnAttr>()) |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1504 | Builder->BuildSinks = true; |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1505 | else { |
| 1506 | // HACK: Some functions are not marked noreturn, and don't return. |
| 1507 | // Here are a few hardwired ones. If this takes too long, we can |
| 1508 | // potentially cache these results. |
| 1509 | const char* s = FD->getIdentifier()->getName(); |
| 1510 | unsigned n = strlen(s); |
| 1511 | |
| 1512 | switch (n) { |
| 1513 | default: |
| 1514 | break; |
Ted Kremenek | 76fdbde | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 1515 | |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1516 | case 4: |
Ted Kremenek | 76fdbde | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 1517 | if (!memcmp(s, "exit", 4)) Builder->BuildSinks = true; |
| 1518 | break; |
| 1519 | |
| 1520 | case 5: |
| 1521 | if (!memcmp(s, "panic", 5)) Builder->BuildSinks = true; |
Zhongxing Xu | bb316c5 | 2008-10-07 10:06:03 +0000 | [diff] [blame] | 1522 | else if (!memcmp(s, "error", 5)) { |
Zhongxing Xu | a90d56e | 2008-10-09 03:19:06 +0000 | [diff] [blame] | 1523 | if (CE->getNumArgs() > 0) { |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1524 | SVal X = state->getSVal(*CE->arg_begin()); |
Zhongxing Xu | a90d56e | 2008-10-09 03:19:06 +0000 | [diff] [blame] | 1525 | // FIXME: use Assume to inspect the possible symbolic value of |
| 1526 | // X. Also check the specific signature of error(). |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1527 | nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&X); |
Zhongxing Xu | a90d56e | 2008-10-09 03:19:06 +0000 | [diff] [blame] | 1528 | if (CI && CI->getValue() != 0) |
Zhongxing Xu | bb316c5 | 2008-10-07 10:06:03 +0000 | [diff] [blame] | 1529 | Builder->BuildSinks = true; |
Zhongxing Xu | a90d56e | 2008-10-09 03:19:06 +0000 | [diff] [blame] | 1530 | } |
Zhongxing Xu | bb316c5 | 2008-10-07 10:06:03 +0000 | [diff] [blame] | 1531 | } |
Ted Kremenek | 76fdbde | 2008-03-14 23:25:49 +0000 | [diff] [blame] | 1532 | break; |
Ted Kremenek | 29ba6b4 | 2009-02-17 17:48:52 +0000 | [diff] [blame] | 1533 | |
Ted Kremenek | 9a094cb | 2008-04-22 05:37:33 +0000 | [diff] [blame] | 1534 | case 6: |
Ted Kremenek | 489ecd5 | 2008-05-17 00:42:01 +0000 | [diff] [blame] | 1535 | if (!memcmp(s, "Assert", 6)) { |
| 1536 | Builder->BuildSinks = true; |
| 1537 | break; |
| 1538 | } |
Ted Kremenek | c7122d5 | 2008-05-01 15:55:59 +0000 | [diff] [blame] | 1539 | |
| 1540 | // FIXME: This is just a wrapper around throwing an exception. |
| 1541 | // Eventually inter-procedural analysis should handle this easily. |
| 1542 | if (!memcmp(s, "ziperr", 6)) Builder->BuildSinks = true; |
| 1543 | |
Ted Kremenek | 9a094cb | 2008-04-22 05:37:33 +0000 | [diff] [blame] | 1544 | break; |
Ted Kremenek | 688738f | 2008-04-23 00:41:25 +0000 | [diff] [blame] | 1545 | |
| 1546 | case 7: |
| 1547 | if (!memcmp(s, "assfail", 7)) Builder->BuildSinks = true; |
| 1548 | break; |
Ted Kremenek | 9a108ae | 2008-04-22 06:09:33 +0000 | [diff] [blame] | 1549 | |
Ted Kremenek | f47bb78 | 2008-04-30 17:54:04 +0000 | [diff] [blame] | 1550 | case 8: |
Ted Kremenek | 29ba6b4 | 2009-02-17 17:48:52 +0000 | [diff] [blame] | 1551 | if (!memcmp(s ,"db_error", 8) || |
| 1552 | !memcmp(s, "__assert", 8)) |
| 1553 | Builder->BuildSinks = true; |
Ted Kremenek | f47bb78 | 2008-04-30 17:54:04 +0000 | [diff] [blame] | 1554 | break; |
Ted Kremenek | 24cb8a2 | 2008-05-01 17:52:49 +0000 | [diff] [blame] | 1555 | |
| 1556 | case 12: |
| 1557 | if (!memcmp(s, "__assert_rtn", 12)) Builder->BuildSinks = true; |
| 1558 | break; |
Ted Kremenek | f47bb78 | 2008-04-30 17:54:04 +0000 | [diff] [blame] | 1559 | |
Ted Kremenek | f968308 | 2008-09-19 02:30:47 +0000 | [diff] [blame] | 1560 | case 13: |
| 1561 | if (!memcmp(s, "__assert_fail", 13)) Builder->BuildSinks = true; |
| 1562 | break; |
| 1563 | |
Ted Kremenek | 9a108ae | 2008-04-22 06:09:33 +0000 | [diff] [blame] | 1564 | case 14: |
Ted Kremenek | 2598b57 | 2008-10-30 00:00:57 +0000 | [diff] [blame] | 1565 | if (!memcmp(s, "dtrace_assfail", 14) || |
| 1566 | !memcmp(s, "yy_fatal_error", 14)) |
| 1567 | Builder->BuildSinks = true; |
Ted Kremenek | 9a108ae | 2008-04-22 06:09:33 +0000 | [diff] [blame] | 1568 | break; |
Ted Kremenek | ec8a1cb | 2008-05-17 00:33:23 +0000 | [diff] [blame] | 1569 | |
| 1570 | case 26: |
Ted Kremenek | 7386d77 | 2008-07-18 16:28:33 +0000 | [diff] [blame] | 1571 | if (!memcmp(s, "_XCAssertionFailureHandler", 26) || |
Ted Kremenek | 40bbff0 | 2009-02-17 23:27:17 +0000 | [diff] [blame] | 1572 | !memcmp(s, "_DTAssertionFailureHandler", 26) || |
| 1573 | !memcmp(s, "_TSAssertionFailureHandler", 26)) |
Ted Kremenek | 05a9112 | 2008-05-17 00:40:45 +0000 | [diff] [blame] | 1574 | Builder->BuildSinks = true; |
Ted Kremenek | 7386d77 | 2008-07-18 16:28:33 +0000 | [diff] [blame] | 1575 | |
Ted Kremenek | ec8a1cb | 2008-05-17 00:33:23 +0000 | [diff] [blame] | 1576 | break; |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1577 | } |
Ted Kremenek | 9a108ae | 2008-04-22 06:09:33 +0000 | [diff] [blame] | 1578 | |
Ted Kremenek | 636e6ba | 2008-03-14 21:58:42 +0000 | [diff] [blame] | 1579 | } |
| 1580 | } |
Ted Kremenek | 4bf38da | 2008-03-05 21:15:02 +0000 | [diff] [blame] | 1581 | |
| 1582 | // Evaluate the call. |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1583 | |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1584 | if (FD) { |
Ted Kremenek | d753f3c | 2008-03-04 22:01:56 +0000 | [diff] [blame] | 1585 | |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 1586 | if (unsigned id = FD->getBuiltinID(getContext())) |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1587 | switch (id) { |
| 1588 | case Builtin::BI__builtin_expect: { |
| 1589 | // For __builtin_expect, just return the value of the subexpression. |
| 1590 | assert (CE->arg_begin() != CE->arg_end()); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1591 | SVal X = state->getSVal(*(CE->arg_begin())); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1592 | MakeNode(Dst, CE, *DI, state->BindExpr(CE, X)); |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1593 | continue; |
| 1594 | } |
| 1595 | |
Ted Kremenek | b302133 | 2008-11-02 00:35:01 +0000 | [diff] [blame] | 1596 | case Builtin::BI__builtin_alloca: { |
Ted Kremenek | b302133 | 2008-11-02 00:35:01 +0000 | [diff] [blame] | 1597 | // FIXME: Refactor into StoreManager itself? |
| 1598 | MemRegionManager& RM = getStateManager().getRegionManager(); |
| 1599 | const MemRegion* R = |
Zhongxing Xu | 6d82f9d | 2008-11-13 07:58:20 +0000 | [diff] [blame] | 1600 | RM.getAllocaRegion(CE, Builder->getCurrentBlockCount()); |
Zhongxing Xu | baf03a7 | 2008-11-24 09:44:56 +0000 | [diff] [blame] | 1601 | |
| 1602 | // Set the extent of the region in bytes. This enables us to use the |
| 1603 | // SVal of the argument directly. If we save the extent in bits, we |
| 1604 | // cannot represent values like symbol*8. |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1605 | SVal Extent = state->getSVal(*(CE->arg_begin())); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1606 | state = getStoreManager().setExtent(state, R, Extent); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1607 | MakeNode(Dst, CE, *DI, state->BindExpr(CE, loc::MemRegionVal(R))); |
Ted Kremenek | b302133 | 2008-11-02 00:35:01 +0000 | [diff] [blame] | 1608 | continue; |
| 1609 | } |
| 1610 | |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1611 | default: |
Ted Kremenek | 55aea31 | 2008-03-05 22:59:42 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | } |
Zhongxing Xu | 9a5bca3 | 2009-08-29 02:11:01 +0000 | [diff] [blame] | 1614 | } |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 1615 | |
| 1616 | // Dispatch to the plug-in transfer function. |
| 1617 | |
| 1618 | unsigned size = Dst.size(); |
| 1619 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
| 1620 | EvalCall(Dst, CE, L, *DI); |
| 1621 | |
| 1622 | // Handle the case where no nodes where generated. Auto-generate that |
| 1623 | // contains the updated state if we aren't generating sinks. |
| 1624 | |
| 1625 | if (!Builder->BuildSinks && Dst.size() == size && |
| 1626 | !Builder->HasGeneratedNode) |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1627 | MakeNode(Dst, CE, *DI, state); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1631 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1632 | // Transfer function: Objective-C ivar references. |
| 1633 | //===----------------------------------------------------------------------===// |
| 1634 | |
Ted Kremenek | f5cae63 | 2009-02-28 20:50:43 +0000 | [diff] [blame] | 1635 | static std::pair<const void*,const void*> EagerlyAssumeTag |
| 1636 | = std::pair<const void*,const void*>(&EagerlyAssumeTag,0); |
| 1637 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1638 | void GRExprEngine::EvalEagerlyAssume(ExplodedNodeSet &Dst, ExplodedNodeSet &Src, Expr *Ex) { |
| 1639 | for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) { |
| 1640 | ExplodedNode *Pred = *I; |
Ted Kremenek | b293902 | 2009-02-25 23:32:10 +0000 | [diff] [blame] | 1641 | |
| 1642 | // Test if the previous node was as the same expression. This can happen |
| 1643 | // when the expression fails to evaluate to anything meaningful and |
| 1644 | // (as an optimization) we don't generate a node. |
| 1645 | ProgramPoint P = Pred->getLocation(); |
| 1646 | if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) { |
| 1647 | Dst.Add(Pred); |
| 1648 | continue; |
| 1649 | } |
| 1650 | |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1651 | const GRState* state = Pred->getState(); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1652 | SVal V = state->getSVal(Ex); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 1653 | if (isa<nonloc::SymExprVal>(V)) { |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1654 | // First assume that the condition is true. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1655 | if (const GRState *stateTrue = state->assume(V, true)) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1656 | stateTrue = stateTrue->BindExpr(Ex, |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 1657 | ValMgr.makeIntVal(1U, Ex->getType())); |
Zhongxing Xu | 25e695b | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 1658 | Dst.Add(Builder->generateNode(PostStmtCustom(Ex, |
| 1659 | &EagerlyAssumeTag, Pred->getLocationContext()), |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1660 | stateTrue, Pred)); |
| 1661 | } |
| 1662 | |
| 1663 | // Next, assume that the condition is false. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1664 | if (const GRState *stateFalse = state->assume(V, false)) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1665 | stateFalse = stateFalse->BindExpr(Ex, |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 1666 | ValMgr.makeIntVal(0U, Ex->getType())); |
Zhongxing Xu | 25e695b | 2009-08-15 03:17:38 +0000 | [diff] [blame] | 1667 | Dst.Add(Builder->generateNode(PostStmtCustom(Ex, &EagerlyAssumeTag, |
| 1668 | Pred->getLocationContext()), |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 1669 | stateFalse, Pred)); |
| 1670 | } |
| 1671 | } |
| 1672 | else |
| 1673 | Dst.Add(Pred); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | //===----------------------------------------------------------------------===// |
| 1678 | // Transfer function: Objective-C ivar references. |
| 1679 | //===----------------------------------------------------------------------===// |
| 1680 | |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1681 | void GRExprEngine::VisitObjCIvarRefExpr(ObjCIvarRefExpr* Ex, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1682 | ExplodedNode* Pred, ExplodedNodeSet& Dst, |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1683 | bool asLValue) { |
| 1684 | |
| 1685 | Expr* Base = cast<Expr>(Ex->getBase()); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1686 | ExplodedNodeSet Tmp; |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1687 | Visit(Base, Pred, Tmp); |
| 1688 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1689 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1690 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1691 | SVal BaseVal = state->getSVal(Base); |
| 1692 | SVal location = state->getLValue(Ex->getDecl(), BaseVal); |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1693 | |
| 1694 | if (asLValue) |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1695 | MakeNode(Dst, Ex, *I, state->BindExpr(Ex, location)); |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1696 | else |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1697 | EvalLoad(Dst, Ex, *I, state, location); |
Ted Kremenek | 97ed4f6 | 2008-10-17 00:03:18 +0000 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | //===----------------------------------------------------------------------===// |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1702 | // Transfer function: Objective-C fast enumeration 'for' statements. |
| 1703 | //===----------------------------------------------------------------------===// |
| 1704 | |
| 1705 | void GRExprEngine::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1706 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1707 | |
| 1708 | // ObjCForCollectionStmts are processed in two places. This method |
| 1709 | // handles the case where an ObjCForCollectionStmt* occurs as one of the |
| 1710 | // statements within a basic block. This transfer function does two things: |
| 1711 | // |
| 1712 | // (1) binds the next container value to 'element'. This creates a new |
| 1713 | // node in the ExplodedGraph. |
| 1714 | // |
| 1715 | // (2) binds the value 0/1 to the ObjCForCollectionStmt* itself, indicating |
| 1716 | // whether or not the container has any more elements. This value |
| 1717 | // will be tested in ProcessBranch. We need to explicitly bind |
| 1718 | // this value because a container can contain nil elements. |
| 1719 | // |
| 1720 | // FIXME: Eventually this logic should actually do dispatches to |
| 1721 | // 'countByEnumeratingWithState:objects:count:' (NSFastEnumeration). |
| 1722 | // This will require simulating a temporary NSFastEnumerationState, either |
| 1723 | // through an SVal or through the use of MemRegions. This value can |
| 1724 | // be affixed to the ObjCForCollectionStmt* instead of 0/1; when the loop |
| 1725 | // terminates we reclaim the temporary (it goes out of scope) and we |
| 1726 | // we can test if the SVal is 0 or if the MemRegion is null (depending |
| 1727 | // on what approach we take). |
| 1728 | // |
| 1729 | // For now: simulate (1) by assigning either a symbol or nil if the |
| 1730 | // container is empty. Thus this transfer function will by default |
| 1731 | // result in state splitting. |
| 1732 | |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1733 | Stmt* elem = S->getElement(); |
| 1734 | SVal ElementV; |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1735 | |
| 1736 | if (DeclStmt* DS = dyn_cast<DeclStmt>(elem)) { |
Chris Lattner | 7e24e82 | 2009-03-28 06:33:19 +0000 | [diff] [blame] | 1737 | VarDecl* ElemD = cast<VarDecl>(DS->getSingleDecl()); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1738 | assert (ElemD->getInit() == 0); |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 1739 | ElementV = GetState(Pred)->getLValue(ElemD, Pred->getLocationContext()); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1740 | VisitObjCForCollectionStmtAux(S, Pred, Dst, ElementV); |
| 1741 | return; |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1742 | } |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1743 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1744 | ExplodedNodeSet Tmp; |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1745 | VisitLValue(cast<Expr>(elem), Pred, Tmp); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1746 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1747 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1748 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1749 | VisitObjCForCollectionStmtAux(S, *I, Dst, state->getSVal(elem)); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | void GRExprEngine::VisitObjCForCollectionStmtAux(ObjCForCollectionStmt* S, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1754 | ExplodedNode* Pred, ExplodedNodeSet& Dst, |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1755 | SVal ElementV) { |
| 1756 | |
| 1757 | |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1758 | |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1759 | // Get the current state. Use 'EvalLocation' to determine if it is a null |
| 1760 | // pointer, etc. |
| 1761 | Stmt* elem = S->getElement(); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1762 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1763 | Pred = EvalLocation(elem, Pred, GetState(Pred), ElementV); |
| 1764 | if (!Pred) |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1765 | return; |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 1766 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1767 | const GRState *state = GetState(Pred); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1768 | |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1769 | // Handle the case where the container still has elements. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1770 | SVal TrueV = ValMgr.makeTruthVal(1); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1771 | const GRState *hasElems = state->BindExpr(S, TrueV); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1772 | |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1773 | // Handle the case where the container has no elements. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1774 | SVal FalseV = ValMgr.makeTruthVal(0); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1775 | const GRState *noElems = state->BindExpr(S, FalseV); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1776 | |
| 1777 | if (loc::MemRegionVal* MV = dyn_cast<loc::MemRegionVal>(&ElementV)) |
| 1778 | if (const TypedRegion* R = dyn_cast<TypedRegion>(MV->getRegion())) { |
| 1779 | // FIXME: The proper thing to do is to really iterate over the |
| 1780 | // container. We will do this with dispatch logic to the store. |
| 1781 | // For now, just 'conjure' up a symbolic value. |
Zhongxing Xu | a82d8aa | 2009-05-09 03:57:34 +0000 | [diff] [blame] | 1782 | QualType T = R->getValueType(getContext()); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1783 | assert (Loc::IsLocType(T)); |
| 1784 | unsigned Count = Builder->getCurrentBlockCount(); |
Zhongxing Xu | ea7c5ce | 2009-04-09 06:49:52 +0000 | [diff] [blame] | 1785 | SymbolRef Sym = SymMgr.getConjuredSymbol(elem, T, Count); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1786 | SVal V = ValMgr.makeLoc(Sym); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1787 | hasElems = hasElems->bindLoc(ElementV, V); |
Ted Kremenek | 116ed0a | 2008-11-12 21:12:46 +0000 | [diff] [blame] | 1788 | |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1789 | // Bind the location to 'nil' on the false branch. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 1790 | SVal nilV = ValMgr.makeIntVal(0, T); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1791 | noElems = noElems->bindLoc(ElementV, nilV); |
Ted Kremenek | 06fb99f | 2008-11-14 19:47:18 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Ted Kremenek | 116ed0a | 2008-11-12 21:12:46 +0000 | [diff] [blame] | 1794 | // Create the new nodes. |
| 1795 | MakeNode(Dst, S, Pred, hasElems); |
| 1796 | MakeNode(Dst, S, Pred, noElems); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1800 | // Transfer function: Objective-C message expressions. |
| 1801 | //===----------------------------------------------------------------------===// |
| 1802 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1803 | void GRExprEngine::VisitObjCMessageExpr(ObjCMessageExpr* ME, ExplodedNode* Pred, |
| 1804 | ExplodedNodeSet& Dst){ |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1805 | |
| 1806 | VisitObjCMessageExprArgHelper(ME, ME->arg_begin(), ME->arg_end(), |
| 1807 | Pred, Dst); |
| 1808 | } |
| 1809 | |
| 1810 | void GRExprEngine::VisitObjCMessageExprArgHelper(ObjCMessageExpr* ME, |
Zhongxing Xu | d3118bd | 2008-10-31 07:26:14 +0000 | [diff] [blame] | 1811 | ObjCMessageExpr::arg_iterator AI, |
| 1812 | ObjCMessageExpr::arg_iterator AE, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1813 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1814 | if (AI == AE) { |
| 1815 | |
| 1816 | // Process the receiver. |
| 1817 | |
| 1818 | if (Expr* Receiver = ME->getReceiver()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1819 | ExplodedNodeSet Tmp; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1820 | Visit(Receiver, Pred, Tmp); |
| 1821 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1822 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
Daniel Dunbar | 3629255 | 2009-07-23 04:41:06 +0000 | [diff] [blame] | 1823 | VisitObjCMessageExprDispatchHelper(ME, *NI, Dst); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1824 | |
| 1825 | return; |
| 1826 | } |
| 1827 | |
Daniel Dunbar | 3629255 | 2009-07-23 04:41:06 +0000 | [diff] [blame] | 1828 | VisitObjCMessageExprDispatchHelper(ME, Pred, Dst); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1829 | return; |
| 1830 | } |
| 1831 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1832 | ExplodedNodeSet Tmp; |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1833 | Visit(*AI, Pred, Tmp); |
| 1834 | |
| 1835 | ++AI; |
| 1836 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1837 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1838 | VisitObjCMessageExprArgHelper(ME, AI, AE, *NI, Dst); |
| 1839 | } |
| 1840 | |
| 1841 | void GRExprEngine::VisitObjCMessageExprDispatchHelper(ObjCMessageExpr* ME, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1842 | ExplodedNode* Pred, |
| 1843 | ExplodedNodeSet& Dst) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1844 | |
| 1845 | // FIXME: More logic for the processing the method call. |
| 1846 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 1847 | const GRState* state = GetState(Pred); |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1848 | bool RaisesException = false; |
| 1849 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (Expr* Receiver = ME->getReceiver()) { |
| 1852 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1853 | SVal L = state->getSVal(Receiver); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1854 | |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1855 | // Check for undefined control-flow. |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1856 | if (L.isUndef()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1857 | ExplodedNode* N = Builder->generateNode(ME, state, Pred); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1858 | |
| 1859 | if (N) { |
| 1860 | N->markAsSink(); |
| 1861 | UndefReceivers.insert(N); |
| 1862 | } |
| 1863 | |
| 1864 | return; |
| 1865 | } |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1866 | |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1867 | // "Assume" that the receiver is not NULL. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1868 | const GRState *StNotNull = state->assume(L, true); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1869 | |
| 1870 | // "Assume" that the receiver is NULL. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1871 | const GRState *StNull = state->assume(L, false); |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1872 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1873 | if (StNull) { |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1874 | QualType RetTy = ME->getType(); |
| 1875 | |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1876 | // Check if the receiver was nil and the return value a struct. |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1877 | if(RetTy->isRecordType()) { |
Ted Kremenek | e8dbf06 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1878 | if (BR.getParentMap().isConsumedExpr(ME)) { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1879 | // The [0 ...] expressions will return garbage. Flag either an |
| 1880 | // explicit or implicit error. Because of the structure of this |
| 1881 | // function we currently do not bifurfacte the state graph at |
| 1882 | // this point. |
| 1883 | // FIXME: We should bifurcate and fill the returned struct with |
| 1884 | // garbage. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1885 | if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) { |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1886 | N->markAsSink(); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1887 | if (StNotNull) |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1888 | NilReceiverStructRetImplicit.insert(N); |
Ted Kremenek | f8769c8 | 2009-04-09 06:02:06 +0000 | [diff] [blame] | 1889 | else |
Ted Kremenek | e644939 | 2009-04-09 04:06:51 +0000 | [diff] [blame] | 1890 | NilReceiverStructRetExplicit.insert(N); |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1891 | } |
| 1892 | } |
Ted Kremenek | e8dbf06 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1893 | } |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1894 | else { |
Ted Kremenek | e8dbf06 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1895 | ASTContext& Ctx = getContext(); |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1896 | if (RetTy != Ctx.VoidTy) { |
| 1897 | if (BR.getParentMap().isConsumedExpr(ME)) { |
| 1898 | // sizeof(void *) |
| 1899 | const uint64_t voidPtrSize = Ctx.getTypeSize(Ctx.VoidPtrTy); |
| 1900 | // sizeof(return type) |
| 1901 | const uint64_t returnTypeSize = Ctx.getTypeSize(ME->getType()); |
Ted Kremenek | e8dbf06 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1902 | |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1903 | if(voidPtrSize < returnTypeSize) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 1904 | if (ExplodedNode* N = Builder->generateNode(ME, StNull, Pred)) { |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1905 | N->markAsSink(); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1906 | if(StNotNull) |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1907 | NilReceiverLargerThanVoidPtrRetImplicit.insert(N); |
Ted Kremenek | f8769c8 | 2009-04-09 06:02:06 +0000 | [diff] [blame] | 1908 | else |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1909 | NilReceiverLargerThanVoidPtrRetExplicit.insert(N); |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1912 | else if (!StNotNull) { |
Ted Kremenek | fe630b9 | 2009-04-09 05:45:56 +0000 | [diff] [blame] | 1913 | // Handle the safe cases where the return value is 0 if the |
| 1914 | // receiver is nil. |
| 1915 | // |
| 1916 | // FIXME: For now take the conservative approach that we only |
| 1917 | // return null values if we *know* that the receiver is nil. |
| 1918 | // This is because we can have surprises like: |
| 1919 | // |
| 1920 | // ... = [[NSScreens screens] objectAtIndex:0]; |
| 1921 | // |
| 1922 | // What can happen is that [... screens] could return nil, but |
| 1923 | // it most likely isn't nil. We should assume the semantics |
| 1924 | // of this case unless we have *a lot* more knowledge. |
| 1925 | // |
Ted Kremenek | 8e5fb28 | 2009-04-09 16:46:55 +0000 | [diff] [blame] | 1926 | SVal V = ValMgr.makeZeroVal(ME->getType()); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 1927 | MakeNode(Dst, ME, Pred, StNull->BindExpr(ME, V)); |
Ted Kremenek | e644939 | 2009-04-09 04:06:51 +0000 | [diff] [blame] | 1928 | return; |
| 1929 | } |
Ted Kremenek | 899b3de | 2009-04-08 03:07:17 +0000 | [diff] [blame] | 1930 | } |
Ted Kremenek | e8dbf06 | 2009-04-09 00:00:02 +0000 | [diff] [blame] | 1931 | } |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1932 | } |
Ted Kremenek | da9ae60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 1933 | // We have handled the cases where the receiver is nil. The remainder |
Ted Kremenek | f8769c8 | 2009-04-09 06:02:06 +0000 | [diff] [blame] | 1934 | // of this method should assume that the receiver is not nil. |
| 1935 | if (!StNotNull) |
| 1936 | return; |
| 1937 | |
Ted Kremenek | da9ae60 | 2009-04-08 18:51:08 +0000 | [diff] [blame] | 1938 | state = StNotNull; |
Ted Kremenek | 21fe837 | 2009-02-19 04:06:22 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1941 | // Check if the "raise" message was sent. |
| 1942 | if (ME->getSelector() == RaiseSel) |
| 1943 | RaisesException = true; |
| 1944 | } |
| 1945 | else { |
| 1946 | |
| 1947 | IdentifierInfo* ClsName = ME->getClassName(); |
| 1948 | Selector S = ME->getSelector(); |
| 1949 | |
| 1950 | // Check for special instance methods. |
| 1951 | |
| 1952 | if (!NSExceptionII) { |
| 1953 | ASTContext& Ctx = getContext(); |
| 1954 | |
| 1955 | NSExceptionII = &Ctx.Idents.get("NSException"); |
| 1956 | } |
| 1957 | |
| 1958 | if (ClsName == NSExceptionII) { |
| 1959 | |
| 1960 | enum { NUM_RAISE_SELECTORS = 2 }; |
| 1961 | |
| 1962 | // Lazily create a cache of the selectors. |
| 1963 | |
| 1964 | if (!NSExceptionInstanceRaiseSelectors) { |
| 1965 | |
| 1966 | ASTContext& Ctx = getContext(); |
| 1967 | |
| 1968 | NSExceptionInstanceRaiseSelectors = new Selector[NUM_RAISE_SELECTORS]; |
| 1969 | |
| 1970 | llvm::SmallVector<IdentifierInfo*, NUM_RAISE_SELECTORS> II; |
| 1971 | unsigned idx = 0; |
| 1972 | |
| 1973 | // raise:format: |
Ted Kremenek | 6ff6f8b | 2008-05-02 17:12:56 +0000 | [diff] [blame] | 1974 | II.push_back(&Ctx.Idents.get("raise")); |
| 1975 | II.push_back(&Ctx.Idents.get("format")); |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1976 | NSExceptionInstanceRaiseSelectors[idx++] = |
| 1977 | Ctx.Selectors.getSelector(II.size(), &II[0]); |
| 1978 | |
| 1979 | // raise:format::arguments: |
Ted Kremenek | 6ff6f8b | 2008-05-02 17:12:56 +0000 | [diff] [blame] | 1980 | II.push_back(&Ctx.Idents.get("arguments")); |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 1981 | NSExceptionInstanceRaiseSelectors[idx++] = |
| 1982 | Ctx.Selectors.getSelector(II.size(), &II[0]); |
| 1983 | } |
| 1984 | |
| 1985 | for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) |
| 1986 | if (S == NSExceptionInstanceRaiseSelectors[i]) { |
| 1987 | RaisesException = true; break; |
| 1988 | } |
| 1989 | } |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | // Check for any arguments that are uninitialized/undefined. |
| 1993 | |
| 1994 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 1995 | I != E; ++I) { |
| 1996 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 1997 | if (state->getSVal(*I).isUndef()) { |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 1998 | |
| 1999 | // Generate an error node for passing an uninitialized/undefined value |
| 2000 | // as an argument to a message expression. This node is a sink. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2001 | ExplodedNode* N = Builder->generateNode(ME, state, Pred); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 2002 | |
| 2003 | if (N) { |
| 2004 | N->markAsSink(); |
| 2005 | MsgExprUndefArgs[N] = *I; |
| 2006 | } |
| 2007 | |
| 2008 | return; |
| 2009 | } |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 2010 | } |
Daniel Dunbar | 3629255 | 2009-07-23 04:41:06 +0000 | [diff] [blame] | 2011 | |
Ted Kremenek | e448ab4 | 2008-05-01 18:33:28 +0000 | [diff] [blame] | 2012 | // Check if we raise an exception. For now treat these as sinks. Eventually |
| 2013 | // we will want to handle exceptions properly. |
| 2014 | |
| 2015 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 2016 | |
| 2017 | if (RaisesException) |
| 2018 | Builder->BuildSinks = true; |
| 2019 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 2020 | // Dispatch to plug-in transfer function. |
| 2021 | |
| 2022 | unsigned size = Dst.size(); |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 2023 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2024 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 2025 | EvalObjCMessageExpr(Dst, ME, Pred); |
| 2026 | |
| 2027 | // Handle the case where no nodes where generated. Auto-generate that |
| 2028 | // contains the updated state if we aren't generating sinks. |
| 2029 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2030 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2031 | MakeNode(Dst, ME, Pred, state); |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | //===----------------------------------------------------------------------===// |
| 2035 | // Transfer functions: Miscellaneous statements. |
| 2036 | //===----------------------------------------------------------------------===// |
| 2037 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2038 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, ExplodedNode* Pred, ExplodedNodeSet& Dst){ |
| 2039 | ExplodedNodeSet S1; |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 2040 | QualType T = CastE->getType(); |
Zhongxing Xu | 933c3e1 | 2008-10-21 06:54:23 +0000 | [diff] [blame] | 2041 | QualType ExTy = Ex->getType(); |
Zhongxing Xu | ed340f7 | 2008-10-22 08:02:16 +0000 | [diff] [blame] | 2042 | |
Zhongxing Xu | d3118bd | 2008-10-31 07:26:14 +0000 | [diff] [blame] | 2043 | if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE)) |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 2044 | T = ExCast->getTypeAsWritten(); |
| 2045 | |
Zhongxing Xu | ed340f7 | 2008-10-22 08:02:16 +0000 | [diff] [blame] | 2046 | if (ExTy->isArrayType() || ExTy->isFunctionType() || T->isReferenceType()) |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2047 | VisitLValue(Ex, Pred, S1); |
Ted Kremenek | 65cfb73 | 2008-03-04 22:16:08 +0000 | [diff] [blame] | 2048 | else |
| 2049 | Visit(Ex, Pred, S1); |
| 2050 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 2051 | // Check for casting to "void". |
Ted Kremenek | dc40290 | 2009-03-04 00:14:35 +0000 | [diff] [blame] | 2052 | if (T->isVoidType()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2053 | for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 2054 | Dst.Add(*I1); |
| 2055 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 2056 | return; |
| 2057 | } |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2058 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2059 | for (ExplodedNodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
| 2060 | ExplodedNode* N = *I1; |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2061 | const GRState* state = GetState(N); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2062 | SVal V = state->getSVal(Ex); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2063 | const SValuator::CastResult &Res = SVator.EvalCast(V, state, T, ExTy); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2064 | state = Res.getState()->BindExpr(CastE, Res.getSVal()); |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2065 | MakeNode(Dst, CastE, N, state); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 2066 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2069 | void GRExprEngine::VisitCompoundLiteralExpr(CompoundLiteralExpr* CL, |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 2070 | ExplodedNode* Pred, |
| 2071 | ExplodedNodeSet& Dst, |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 2072 | bool asLValue) { |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2073 | InitListExpr* ILE = cast<InitListExpr>(CL->getInitializer()->IgnoreParens()); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2074 | ExplodedNodeSet Tmp; |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2075 | Visit(ILE, Pred, Tmp); |
| 2076 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2077 | for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I!=EI; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2078 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2079 | SVal ILV = state->getSVal(ILE); |
| 2080 | state = state->bindCompoundLiteral(CL, ILV); |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2081 | |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 2082 | if (asLValue) |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2083 | MakeNode(Dst, CL, *I, state->BindExpr(CL, state->getLValue(CL))); |
Zhongxing Xu | f22679e | 2008-11-07 10:38:33 +0000 | [diff] [blame] | 2084 | else |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2085 | MakeNode(Dst, CL, *I, state->BindExpr(CL, ILV)); |
Ted Kremenek | 4f09027 | 2008-10-27 21:54:31 +0000 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2089 | void GRExprEngine::VisitDeclStmt(DeclStmt *DS, ExplodedNode *Pred, |
| 2090 | ExplodedNodeSet& Dst) { |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2091 | |
Ted Kremenek | 8369a8b | 2008-10-06 18:43:53 +0000 | [diff] [blame] | 2092 | // The CFG has one DeclStmt per Decl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2093 | Decl* D = *DS->decl_begin(); |
Ted Kremenek | e6c62e3 | 2008-08-28 18:34:26 +0000 | [diff] [blame] | 2094 | |
| 2095 | if (!D || !isa<VarDecl>(D)) |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2096 | return; |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 2097 | |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 2098 | const VarDecl* VD = dyn_cast<VarDecl>(D); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2099 | Expr* InitEx = const_cast<Expr*>(VD->getInit()); |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2100 | |
| 2101 | // FIXME: static variables may have an initializer, but the second |
| 2102 | // time a function is called those values may not be current. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2103 | ExplodedNodeSet Tmp; |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2104 | |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2105 | if (InitEx) |
| 2106 | Visit(InitEx, Pred, Tmp); |
Ted Kremenek | 1152fc0 | 2009-07-17 23:48:26 +0000 | [diff] [blame] | 2107 | else |
Ted Kremenek | e6c62e3 | 2008-08-28 18:34:26 +0000 | [diff] [blame] | 2108 | Tmp.Add(Pred); |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2109 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2110 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2111 | const GRState* state = GetState(*I); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2112 | unsigned Count = Builder->getCurrentBlockCount(); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 2113 | |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2114 | // Check if 'VD' is a VLA and if so check if has a non-zero size. |
| 2115 | QualType T = getContext().getCanonicalType(VD->getType()); |
| 2116 | if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) { |
| 2117 | // FIXME: Handle multi-dimensional VLAs. |
| 2118 | |
| 2119 | Expr* SE = VLA->getSizeExpr(); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2120 | SVal Size = state->getSVal(SE); |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (Size.isUndef()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2123 | if (ExplodedNode* N = Builder->generateNode(DS, state, Pred)) { |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2124 | N->markAsSink(); |
| 2125 | ExplicitBadSizedVLA.insert(N); |
| 2126 | } |
| 2127 | continue; |
| 2128 | } |
| 2129 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2130 | const GRState* zeroState = state->assume(Size, false); |
| 2131 | state = state->assume(Size, true); |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2132 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2133 | if (zeroState) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2134 | if (ExplodedNode* N = Builder->generateNode(DS, zeroState, Pred)) { |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2135 | N->markAsSink(); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2136 | if (state) |
| 2137 | ImplicitBadSizedVLA.insert(N); |
| 2138 | else |
| 2139 | ExplicitBadSizedVLA.insert(N); |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2143 | if (!state) |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2144 | continue; |
| 2145 | } |
| 2146 | |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 2147 | // Decls without InitExpr are not initialized explicitly. |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2148 | const LocationContext *LC = (*I)->getLocationContext(); |
| 2149 | |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2150 | if (InitEx) { |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2151 | SVal InitVal = state->getSVal(InitEx); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2152 | QualType T = VD->getType(); |
| 2153 | |
| 2154 | // Recover some path-sensitivity if a scalar value evaluated to |
| 2155 | // UnknownVal. |
Ted Kremenek | 276c6ac | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2156 | if (InitVal.isUnknown() || |
| 2157 | !getConstraintManager().canReasonAbout(InitVal)) { |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2158 | InitVal = ValMgr.getConjuredSymbolVal(InitEx, Count); |
Ted Kremenek | af33741 | 2008-11-12 19:24:17 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2161 | state = state->bindDecl(VD, LC, InitVal); |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2162 | |
| 2163 | // The next thing to do is check if the GRTransferFuncs object wants to |
| 2164 | // update the state based on the new binding. If the GRTransferFunc |
| 2165 | // object doesn't do anything, just auto-propagate the current state. |
| 2166 | GRStmtNodeBuilderRef BuilderRef(Dst, *Builder, *this, *I, state, DS,true); |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2167 | getTF().EvalBind(BuilderRef, loc::MemRegionVal(state->getRegion(VD, LC)), |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2168 | InitVal); |
| 2169 | } |
| 2170 | else { |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 2171 | state = state->bindDeclWithNoInit(VD, LC); |
Ted Kremenek | 5b8d901 | 2009-02-14 01:54:57 +0000 | [diff] [blame] | 2172 | MakeNode(Dst, DS, *I, state); |
Ted Kremenek | efd5994 | 2008-12-08 22:47:34 +0000 | [diff] [blame] | 2173 | } |
Ted Kremenek | 5b7dcce | 2008-04-22 22:25:27 +0000 | [diff] [blame] | 2174 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 2175 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 2176 | |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2177 | namespace { |
| 2178 | // This class is used by VisitInitListExpr as an item in a worklist |
| 2179 | // for processing the values contained in an InitListExpr. |
| 2180 | class VISIBILITY_HIDDEN InitListWLItem { |
| 2181 | public: |
| 2182 | llvm::ImmutableList<SVal> Vals; |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2183 | ExplodedNode* N; |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2184 | InitListExpr::reverse_iterator Itr; |
| 2185 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2186 | InitListWLItem(ExplodedNode* n, llvm::ImmutableList<SVal> vals, |
| 2187 | InitListExpr::reverse_iterator itr) |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2188 | : Vals(vals), N(n), Itr(itr) {} |
| 2189 | }; |
| 2190 | } |
| 2191 | |
| 2192 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2193 | void GRExprEngine::VisitInitListExpr(InitListExpr* E, ExplodedNode* Pred, |
| 2194 | ExplodedNodeSet& Dst) { |
Ted Kremenek | a49e367 | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2195 | |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2196 | const GRState* state = GetState(Pred); |
Ted Kremenek | 76dba7b | 2008-11-13 05:05:34 +0000 | [diff] [blame] | 2197 | QualType T = getContext().getCanonicalType(E->getType()); |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2198 | unsigned NumInitElements = E->getNumInits(); |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2199 | |
Ted Kremenek | f3bfa21 | 2009-07-28 20:46:55 +0000 | [diff] [blame] | 2200 | if (T->isArrayType() || T->isStructureType() || |
| 2201 | T->isUnionType() || T->isVectorType()) { |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2202 | |
Ted Kremenek | a49e367 | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2203 | llvm::ImmutableList<SVal> StartVals = getBasicVals().getEmptySValList(); |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2204 | |
Ted Kremenek | a49e367 | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2205 | // Handle base case where the initializer has no elements. |
| 2206 | // e.g: static int* myArray[] = {}; |
| 2207 | if (NumInitElements == 0) { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2208 | SVal V = ValMgr.makeCompoundVal(T, StartVals); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2209 | MakeNode(Dst, E, Pred, state->BindExpr(E, V)); |
Ted Kremenek | a49e367 | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2210 | return; |
| 2211 | } |
| 2212 | |
| 2213 | // Create a worklist to process the initializers. |
| 2214 | llvm::SmallVector<InitListWLItem, 10> WorkList; |
| 2215 | WorkList.reserve(NumInitElements); |
| 2216 | WorkList.push_back(InitListWLItem(Pred, StartVals, E->rbegin())); |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2217 | InitListExpr::reverse_iterator ItrEnd = E->rend(); |
| 2218 | |
Ted Kremenek | a49e367 | 2008-10-30 23:14:36 +0000 | [diff] [blame] | 2219 | // Process the worklist until it is empty. |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2220 | while (!WorkList.empty()) { |
| 2221 | InitListWLItem X = WorkList.back(); |
| 2222 | WorkList.pop_back(); |
| 2223 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2224 | ExplodedNodeSet Tmp; |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2225 | Visit(*X.Itr, X.N, Tmp); |
| 2226 | |
| 2227 | InitListExpr::reverse_iterator NewItr = X.Itr + 1; |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2228 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2229 | for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) { |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2230 | // Get the last initializer value. |
| 2231 | state = GetState(*NI); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2232 | SVal InitV = state->getSVal(cast<Expr>(*X.Itr)); |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2233 | |
| 2234 | // Construct the new list of values by prepending the new value to |
| 2235 | // the already constructed list. |
| 2236 | llvm::ImmutableList<SVal> NewVals = |
| 2237 | getBasicVals().consVals(InitV, X.Vals); |
| 2238 | |
| 2239 | if (NewItr == ItrEnd) { |
Zhongxing Xu | a189dca | 2008-10-31 03:01:26 +0000 | [diff] [blame] | 2240 | // Now we have a list holding all init values. Make CompoundValData. |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2241 | SVal V = ValMgr.makeCompoundVal(T, NewVals); |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2242 | |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2243 | // Make final state and node. |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2244 | MakeNode(Dst, E, *NI, state->BindExpr(E, V)); |
Ted Kremenek | f75b186 | 2008-10-30 17:47:32 +0000 | [diff] [blame] | 2245 | } |
| 2246 | else { |
| 2247 | // Still some initializer values to go. Push them onto the worklist. |
| 2248 | WorkList.push_back(InitListWLItem(*NI, NewVals, NewItr)); |
| 2249 | } |
| 2250 | } |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2251 | } |
Ted Kremenek | 8790307 | 2008-10-30 18:34:31 +0000 | [diff] [blame] | 2252 | |
| 2253 | return; |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2256 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 2257 | assert (E->getNumInits() == 1); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2258 | ExplodedNodeSet Tmp; |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2259 | Expr* Init = E->getInit(0); |
| 2260 | Visit(Init, Pred, Tmp); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2261 | for (ExplodedNodeSet::iterator I = Tmp.begin(), EI = Tmp.end(); I != EI; ++I) { |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2262 | state = GetState(*I); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2263 | MakeNode(Dst, E, *I, state->BindExpr(E, state->getSVal(Init))); |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2264 | } |
| 2265 | return; |
| 2266 | } |
| 2267 | |
Zhongxing Xu | c4f8706 | 2008-10-30 05:02:23 +0000 | [diff] [blame] | 2268 | |
| 2269 | printf("InitListExpr type = %s\n", T.getAsString().c_str()); |
| 2270 | assert(0 && "unprocessed InitListExpr type"); |
| 2271 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 2272 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2273 | /// VisitSizeOfAlignOfExpr - Transfer function for sizeof(type). |
| 2274 | void GRExprEngine::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr* Ex, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2275 | ExplodedNode* Pred, |
| 2276 | ExplodedNodeSet& Dst) { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2277 | QualType T = Ex->getTypeOfArgument(); |
Ted Kremenek | 87e8034 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2278 | uint64_t amt; |
| 2279 | |
| 2280 | if (Ex->isSizeOf()) { |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2281 | if (T == getContext().VoidTy) { |
| 2282 | // sizeof(void) == 1 byte. |
| 2283 | amt = 1; |
| 2284 | } |
| 2285 | else if (!T.getTypePtr()->isConstantSizeType()) { |
| 2286 | // FIXME: Add support for VLAs. |
Ted Kremenek | 87e8034 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2287 | return; |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2288 | } |
| 2289 | else if (T->isObjCInterfaceType()) { |
| 2290 | // Some code tries to take the sizeof an ObjCInterfaceType, relying that |
| 2291 | // the compiler has laid out its representation. Just report Unknown |
| 2292 | // for these. |
Ted Kremenek | f342d18 | 2008-04-30 21:31:12 +0000 | [diff] [blame] | 2293 | return; |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2294 | } |
| 2295 | else { |
| 2296 | // All other cases. |
Ted Kremenek | 87e8034 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2297 | amt = getContext().getTypeSize(T) / 8; |
Ted Kremenek | 55f7bcb | 2008-12-15 18:51:00 +0000 | [diff] [blame] | 2298 | } |
Ted Kremenek | 87e8034 | 2008-03-15 03:13:20 +0000 | [diff] [blame] | 2299 | } |
| 2300 | else // Get alignment of the type. |
Ted Kremenek | 897781a | 2008-03-15 03:13:55 +0000 | [diff] [blame] | 2301 | amt = getContext().getTypeAlign(T) / 8; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 2302 | |
Ted Kremenek | 0e561a3 | 2008-03-21 21:30:14 +0000 | [diff] [blame] | 2303 | MakeNode(Dst, Ex, Pred, |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2304 | GetState(Pred)->BindExpr(Ex, ValMgr.makeIntVal(amt, Ex->getType()))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2307 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2308 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, ExplodedNode* Pred, |
| 2309 | ExplodedNodeSet& Dst, bool asLValue) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2310 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2311 | switch (U->getOpcode()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2312 | |
| 2313 | default: |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2314 | break; |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2315 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2316 | case UnaryOperator::Deref: { |
| 2317 | |
| 2318 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2319 | ExplodedNodeSet Tmp; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2320 | Visit(Ex, Pred, Tmp); |
| 2321 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2322 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2323 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2324 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2325 | SVal location = state->getSVal(Ex); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2326 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2327 | if (asLValue) |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2328 | MakeNode(Dst, U, *I, state->BindExpr(U, location), |
Ted Kremenek | 7090d54 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 2329 | ProgramPoint::PostLValueKind); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2330 | else |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2331 | EvalLoad(Dst, U, *I, state, location); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | return; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2335 | } |
Ted Kremenek | a084bb6 | 2008-04-30 21:45:55 +0000 | [diff] [blame] | 2336 | |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2337 | case UnaryOperator::Real: { |
| 2338 | |
| 2339 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2340 | ExplodedNodeSet Tmp; |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2341 | Visit(Ex, Pred, Tmp); |
| 2342 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2343 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2344 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2345 | // FIXME: We don't have complex SValues yet. |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2346 | if (Ex->getType()->isAnyComplexType()) { |
| 2347 | // Just report "Unknown." |
| 2348 | Dst.Add(*I); |
| 2349 | continue; |
| 2350 | } |
| 2351 | |
| 2352 | // For all other types, UnaryOperator::Real is an identity operation. |
| 2353 | assert (U->getType() == Ex->getType()); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2354 | const GRState* state = GetState(*I); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2355 | MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex))); |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | return; |
| 2359 | } |
| 2360 | |
| 2361 | case UnaryOperator::Imag: { |
| 2362 | |
| 2363 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2364 | ExplodedNodeSet Tmp; |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2365 | Visit(Ex, Pred, Tmp); |
| 2366 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2367 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2368 | // FIXME: We don't have complex SValues yet. |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2369 | if (Ex->getType()->isAnyComplexType()) { |
| 2370 | // Just report "Unknown." |
| 2371 | Dst.Add(*I); |
| 2372 | continue; |
| 2373 | } |
| 2374 | |
| 2375 | // For all other types, UnaryOperator::Float returns 0. |
| 2376 | assert (Ex->getType()->isIntegerType()); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2377 | const GRState* state = GetState(*I); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2378 | SVal X = ValMgr.makeZeroVal(Ex->getType()); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2379 | MakeNode(Dst, U, *I, state->BindExpr(U, X)); |
Ted Kremenek | b8e26e6 | 2008-06-19 17:55:38 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | return; |
| 2383 | } |
| 2384 | |
| 2385 | // FIXME: Just report "Unknown" for OffsetOf. |
Ted Kremenek | a084bb6 | 2008-04-30 21:45:55 +0000 | [diff] [blame] | 2386 | case UnaryOperator::OffsetOf: |
Ted Kremenek | a084bb6 | 2008-04-30 21:45:55 +0000 | [diff] [blame] | 2387 | Dst.Add(Pred); |
| 2388 | return; |
| 2389 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2390 | case UnaryOperator::Plus: assert (!asLValue); // FALL-THROUGH. |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2391 | case UnaryOperator::Extension: { |
| 2392 | |
| 2393 | // Unary "+" is a no-op, similar to a parentheses. We still have places |
| 2394 | // where it may be a block-level expression, so we need to |
| 2395 | // generate an extra node that just propagates the value of the |
| 2396 | // subexpression. |
| 2397 | |
| 2398 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2399 | ExplodedNodeSet Tmp; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2400 | Visit(Ex, Pred, Tmp); |
| 2401 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2402 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2403 | const GRState* state = GetState(*I); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2404 | MakeNode(Dst, U, *I, state->BindExpr(U, state->getSVal(Ex))); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | return; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2408 | } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 2409 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2410 | case UnaryOperator::AddrOf: { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 2411 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2412 | assert(!asLValue); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2413 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2414 | ExplodedNodeSet Tmp; |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2415 | VisitLValue(Ex, Pred, Tmp); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2416 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2417 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2418 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2419 | SVal V = state->getSVal(Ex); |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2420 | state = state->BindExpr(U, V); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2421 | MakeNode(Dst, U, *I, state); |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 2422 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2423 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2424 | return; |
| 2425 | } |
| 2426 | |
| 2427 | case UnaryOperator::LNot: |
| 2428 | case UnaryOperator::Minus: |
| 2429 | case UnaryOperator::Not: { |
| 2430 | |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2431 | assert (!asLValue); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2432 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2433 | ExplodedNodeSet Tmp; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2434 | Visit(Ex, Pred, Tmp); |
| 2435 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2436 | for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end(); I!=E; ++I) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2437 | const GRState* state = GetState(*I); |
Ted Kremenek | 855cd90 | 2008-09-30 05:32:44 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Get the value of the subexpression. |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2440 | SVal V = state->getSVal(Ex); |
Ted Kremenek | 855cd90 | 2008-09-30 05:32:44 +0000 | [diff] [blame] | 2441 | |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 2442 | if (V.isUnknownOrUndef()) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2443 | MakeNode(Dst, U, *I, state->BindExpr(U, V)); |
Ted Kremenek | e04a5cb | 2008-11-15 00:20:05 +0000 | [diff] [blame] | 2444 | continue; |
| 2445 | } |
| 2446 | |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2447 | // QualType DstT = getContext().getCanonicalType(U->getType()); |
| 2448 | // QualType SrcT = getContext().getCanonicalType(Ex->getType()); |
| 2449 | // |
| 2450 | // if (DstT != SrcT) // Perform promotions. |
| 2451 | // V = EvalCast(V, DstT); |
| 2452 | // |
| 2453 | // if (V.isUnknownOrUndef()) { |
| 2454 | // MakeNode(Dst, U, *I, BindExpr(St, U, V)); |
| 2455 | // continue; |
| 2456 | // } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2457 | |
| 2458 | switch (U->getOpcode()) { |
| 2459 | default: |
| 2460 | assert(false && "Invalid Opcode."); |
| 2461 | break; |
| 2462 | |
| 2463 | case UnaryOperator::Not: |
Ted Kremenek | 60a6e0c | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 2464 | // FIXME: Do we need to handle promotions? |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2465 | state = state->BindExpr(U, EvalComplement(cast<NonLoc>(V))); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2466 | break; |
| 2467 | |
| 2468 | case UnaryOperator::Minus: |
Ted Kremenek | 60a6e0c | 2008-10-01 00:21:14 +0000 | [diff] [blame] | 2469 | // FIXME: Do we need to handle promotions? |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2470 | state = state->BindExpr(U, EvalMinus(cast<NonLoc>(V))); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2471 | break; |
| 2472 | |
| 2473 | case UnaryOperator::LNot: |
| 2474 | |
| 2475 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 2476 | // |
| 2477 | // Note: technically we do "E == 0", but this is the same in the |
| 2478 | // transfer functions as "0 == E". |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2479 | SVal Result; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2480 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2481 | if (isa<Loc>(V)) { |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2482 | Loc X = ValMgr.makeNull(); |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2483 | Result = EvalBinOp(state, BinaryOperator::EQ, cast<Loc>(V), X, |
| 2484 | U->getType()); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2485 | } |
| 2486 | else { |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2487 | nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType())); |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2488 | Result = EvalBinOp(BinaryOperator::EQ, cast<NonLoc>(V), X, |
| 2489 | U->getType()); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2492 | state = state->BindExpr(U, Result); |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2493 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2494 | break; |
| 2495 | } |
| 2496 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2497 | MakeNode(Dst, U, *I, state); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | return; |
| 2501 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | // Handle ++ and -- (both pre- and post-increment). |
| 2505 | |
| 2506 | assert (U->isIncrementDecrementOp()); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2507 | ExplodedNodeSet Tmp; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2508 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2509 | VisitLValue(Ex, Pred, Tmp); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2510 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2511 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I!=E; ++I) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2512 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2513 | const GRState* state = GetState(*I); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2514 | SVal V1 = state->getSVal(Ex); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2515 | |
| 2516 | // Perform a load. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2517 | ExplodedNodeSet Tmp2; |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2518 | EvalLoad(Tmp2, Ex, *I, state, V1); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2519 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2520 | for (ExplodedNodeSet::iterator I2 = Tmp2.begin(), E2 = Tmp2.end(); I2!=E2; ++I2) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2521 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2522 | state = GetState(*I2); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2523 | SVal V2 = state->getSVal(Ex); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Propagate unknown and undefined values. |
| 2526 | if (V2.isUnknownOrUndef()) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2527 | MakeNode(Dst, U, *I2, state->BindExpr(U, V2)); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2528 | continue; |
| 2529 | } |
| 2530 | |
Ted Kremenek | 21028dd | 2009-03-11 03:54:24 +0000 | [diff] [blame] | 2531 | // Handle all other values. |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 2532 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 2533 | : BinaryOperator::Sub; |
Ted Kremenek | 21028dd | 2009-03-11 03:54:24 +0000 | [diff] [blame] | 2534 | |
Zhongxing Xu | a52ad4e | 2009-08-05 02:51:59 +0000 | [diff] [blame] | 2535 | // If the UnaryOperator has non-location type, use its type to create the |
| 2536 | // constant value. If the UnaryOperator has location type, create the |
| 2537 | // constant with int type and pointer width. |
| 2538 | SVal RHS; |
| 2539 | |
| 2540 | if (U->getType()->isAnyPointerType()) |
| 2541 | RHS = ValMgr.makeIntValWithPtrWidth(1, false); |
| 2542 | else |
| 2543 | RHS = ValMgr.makeIntVal(1, U->getType()); |
| 2544 | |
| 2545 | SVal Result = EvalBinOp(state, Op, V2, RHS, U->getType()); |
Ted Kremenek | bb9b271 | 2009-03-20 20:10:45 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Conjure a new symbol if necessary to recover precision. |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2548 | if (Result.isUnknown() || !getConstraintManager().canReasonAbout(Result)){ |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2549 | Result = ValMgr.getConjuredSymbolVal(Ex, |
| 2550 | Builder->getCurrentBlockCount()); |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2551 | |
| 2552 | // If the value is a location, ++/-- should always preserve |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2553 | // non-nullness. Check if the original value was non-null, and if so |
| 2554 | // propagate that constraint. |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2555 | if (Loc::IsLocType(U->getType())) { |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 2556 | SVal Constraint = EvalBinOp(state, BinaryOperator::EQ, V2, |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2557 | ValMgr.makeZeroVal(U->getType()), |
| 2558 | getContext().IntTy); |
| 2559 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2560 | if (!state->assume(Constraint, true)) { |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2561 | // It isn't feasible for the original value to be null. |
| 2562 | // Propagate this constraint. |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 2563 | Constraint = EvalBinOp(state, BinaryOperator::EQ, Result, |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2564 | ValMgr.makeZeroVal(U->getType()), |
| 2565 | getContext().IntTy); |
| 2566 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 2567 | state = state->assume(Constraint, false); |
| 2568 | assert(state); |
Ted Kremenek | af48fdd | 2009-04-21 22:38:05 +0000 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | } |
Ted Kremenek | bb9b271 | 2009-03-20 20:10:45 +0000 | [diff] [blame] | 2572 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2573 | state = state->BindExpr(U, U->isPostfix() ? V2 : Result); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 2574 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2575 | // Perform the store. |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2576 | EvalStore(Dst, U, *I2, state, V1, Result); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2577 | } |
Ted Kremenek | 469ecbd | 2008-04-21 23:43:38 +0000 | [diff] [blame] | 2578 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2581 | void GRExprEngine::VisitAsmStmt(AsmStmt* A, ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2582 | VisitAsmStmtHelperOutputs(A, A->begin_outputs(), A->end_outputs(), Pred, Dst); |
| 2583 | } |
| 2584 | |
| 2585 | void GRExprEngine::VisitAsmStmtHelperOutputs(AsmStmt* A, |
| 2586 | AsmStmt::outputs_iterator I, |
| 2587 | AsmStmt::outputs_iterator E, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2588 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2589 | if (I == E) { |
| 2590 | VisitAsmStmtHelperInputs(A, A->begin_inputs(), A->end_inputs(), Pred, Dst); |
| 2591 | return; |
| 2592 | } |
| 2593 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2594 | ExplodedNodeSet Tmp; |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2595 | VisitLValue(*I, Pred, Tmp); |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2596 | |
| 2597 | ++I; |
| 2598 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2599 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2600 | VisitAsmStmtHelperOutputs(A, I, E, *NI, Dst); |
| 2601 | } |
| 2602 | |
| 2603 | void GRExprEngine::VisitAsmStmtHelperInputs(AsmStmt* A, |
| 2604 | AsmStmt::inputs_iterator I, |
| 2605 | AsmStmt::inputs_iterator E, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2606 | ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2607 | if (I == E) { |
| 2608 | |
| 2609 | // We have processed both the inputs and the outputs. All of the outputs |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2610 | // should evaluate to Locs. Nuke all of their values. |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2611 | |
| 2612 | // FIXME: Some day in the future it would be nice to allow a "plug-in" |
| 2613 | // which interprets the inline asm and stores proper results in the |
| 2614 | // outputs. |
| 2615 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2616 | const GRState* state = GetState(Pred); |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2617 | |
| 2618 | for (AsmStmt::outputs_iterator OI = A->begin_outputs(), |
| 2619 | OE = A->end_outputs(); OI != OE; ++OI) { |
| 2620 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2621 | SVal X = state->getSVal(*OI); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2622 | assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef. |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2623 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2624 | if (isa<Loc>(X)) |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2625 | state = state->bindLoc(cast<Loc>(X), UnknownVal()); |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2628 | MakeNode(Dst, A, Pred, state); |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2629 | return; |
| 2630 | } |
| 2631 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2632 | ExplodedNodeSet Tmp; |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2633 | Visit(*I, Pred, Tmp); |
| 2634 | |
| 2635 | ++I; |
| 2636 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2637 | for (ExplodedNodeSet::iterator NI = Tmp.begin(), NE = Tmp.end(); NI != NE; ++NI) |
Ted Kremenek | ef44bfb | 2008-03-17 21:11:24 +0000 | [diff] [blame] | 2638 | VisitAsmStmtHelperInputs(A, I, E, *NI, Dst); |
| 2639 | } |
| 2640 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2641 | void GRExprEngine::EvalReturn(ExplodedNodeSet& Dst, ReturnStmt* S, ExplodedNode* Pred) { |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2642 | assert (Builder && "GRStmtNodeBuilder must be defined."); |
| 2643 | |
| 2644 | unsigned size = Dst.size(); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2645 | |
Ted Kremenek | 186350f | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 2646 | SaveAndRestore<bool> OldSink(Builder->BuildSinks); |
| 2647 | SaveOr OldHasGen(Builder->HasGeneratedNode); |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2648 | |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 2649 | getTF().EvalReturn(Dst, *this, *Builder, S, Pred); |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2650 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2651 | // Handle the case where no nodes where generated. |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2652 | |
Ted Kremenek | b053396 | 2008-04-18 20:35:30 +0000 | [diff] [blame] | 2653 | if (!Builder->BuildSinks && Dst.size() == size && !Builder->HasGeneratedNode) |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2654 | MakeNode(Dst, S, Pred, GetState(Pred)); |
| 2655 | } |
| 2656 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2657 | void GRExprEngine::VisitReturnStmt(ReturnStmt* S, ExplodedNode* Pred, ExplodedNodeSet& Dst) { |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2658 | |
| 2659 | Expr* R = S->getRetValue(); |
| 2660 | |
| 2661 | if (!R) { |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2662 | EvalReturn(Dst, S, Pred); |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2663 | return; |
| 2664 | } |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2665 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2666 | ExplodedNodeSet Tmp; |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2667 | Visit(R, Pred, Tmp); |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2668 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2669 | for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) { |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2670 | SVal X = (*I)->getState()->getSVal(R); |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2671 | |
| 2672 | // Check if we return the address of a stack variable. |
| 2673 | if (isa<loc::MemRegionVal>(X)) { |
| 2674 | // Determine if the value is on the stack. |
| 2675 | const MemRegion* R = cast<loc::MemRegionVal>(&X)->getRegion(); |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2676 | |
Ted Kremenek | ea20cd7 | 2009-06-23 18:05:21 +0000 | [diff] [blame] | 2677 | if (R && R->hasStackStorage()) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2678 | // Create a special node representing the error. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2679 | if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2680 | N->markAsSink(); |
| 2681 | RetsStackAddr.insert(N); |
| 2682 | } |
| 2683 | continue; |
| 2684 | } |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2685 | } |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2686 | // Check if we return an undefined value. |
| 2687 | else if (X.isUndef()) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2688 | if (ExplodedNode* N = Builder->generateNode(S, GetState(*I), *I)) { |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2689 | N->markAsSink(); |
| 2690 | RetsUndef.insert(N); |
| 2691 | } |
| 2692 | continue; |
| 2693 | } |
| 2694 | |
Ted Kremenek | 6b31e8e | 2008-04-16 23:05:51 +0000 | [diff] [blame] | 2695 | EvalReturn(Dst, S, *I); |
Ted Kremenek | 5917d78 | 2008-11-21 00:27:44 +0000 | [diff] [blame] | 2696 | } |
Ted Kremenek | 02737ed | 2008-03-31 15:02:58 +0000 | [diff] [blame] | 2697 | } |
Ted Kremenek | 55deb97 | 2008-03-25 00:34:37 +0000 | [diff] [blame] | 2698 | |
Ted Kremenek | e695e1c | 2008-04-15 23:06:53 +0000 | [diff] [blame] | 2699 | //===----------------------------------------------------------------------===// |
| 2700 | // Transfer functions: Binary operators. |
| 2701 | //===----------------------------------------------------------------------===// |
| 2702 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2703 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2704 | ExplodedNode* Pred, |
| 2705 | ExplodedNodeSet& Dst) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2706 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2707 | ExplodedNodeSet Tmp1; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2708 | Expr* LHS = B->getLHS()->IgnoreParens(); |
| 2709 | Expr* RHS = B->getRHS()->IgnoreParens(); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2710 | |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2711 | // FIXME: Add proper support for ObjCImplicitSetterGetterRefExpr. |
| 2712 | if (isa<ObjCImplicitSetterGetterRefExpr>(LHS)) { |
Ted Kremenek | 759623e | 2008-12-06 02:39:30 +0000 | [diff] [blame] | 2713 | Visit(RHS, Pred, Dst); |
| 2714 | return; |
| 2715 | } |
| 2716 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2717 | if (B->isAssignmentOp()) |
Zhongxing Xu | 6d69b5d | 2008-10-16 06:09:51 +0000 | [diff] [blame] | 2718 | VisitLValue(LHS, Pred, Tmp1); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 2719 | else |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2720 | Visit(LHS, Pred, Tmp1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 2721 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2722 | for (ExplodedNodeSet::iterator I1=Tmp1.begin(), E1=Tmp1.end(); I1 != E1; ++I1) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2723 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2724 | SVal LeftV = (*I1)->getState()->getSVal(LHS); |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 2725 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2726 | // Process the RHS. |
| 2727 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2728 | ExplodedNodeSet Tmp2; |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2729 | Visit(RHS, *I1, Tmp2); |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame^] | 2730 | |
| 2731 | ExplodedNodeSet CheckedSet; |
| 2732 | CheckerVisit(B, CheckedSet, Tmp2, true); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2733 | |
| 2734 | // With both the LHS and RHS evaluated, process the operation itself. |
| 2735 | |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame^] | 2736 | for (ExplodedNodeSet::iterator I2=CheckedSet.begin(), E2=CheckedSet.end(); |
| 2737 | I2 != E2; ++I2) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2738 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2739 | const GRState* state = GetState(*I2); |
| 2740 | const GRState* OldSt = state; |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 2741 | |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2742 | SVal RightV = state->getSVal(RHS); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 2743 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 2744 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2745 | switch (Op) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2746 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 2747 | case BinaryOperator::Assign: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2748 | |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 2749 | // EXPERIMENTAL: "Conjured" symbols. |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2750 | // FIXME: Handle structs. |
| 2751 | QualType T = RHS->getType(); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 2752 | |
Ted Kremenek | 276c6ac | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2753 | if ((RightV.isUnknown() || |
| 2754 | !getConstraintManager().canReasonAbout(RightV)) |
| 2755 | && (Loc::IsLocType(T) || |
| 2756 | (T->isScalarType() && T->isIntegerType()))) { |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2757 | unsigned Count = Builder->getCurrentBlockCount(); |
| 2758 | RightV = ValMgr.getConjuredSymbolVal(B->getRHS(), Count); |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Ted Kremenek | 361fa8e | 2008-03-12 21:45:47 +0000 | [diff] [blame] | 2761 | // Simulate the effects of a "store": bind the value of the RHS |
Ted Kremenek | 276c6ac | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2762 | // to the L-Value represented by the LHS. |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2763 | EvalStore(Dst, B, LHS, *I2, state->BindExpr(B, RightV), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 2764 | LeftV, RightV); |
Ted Kremenek | e38718e | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 2765 | continue; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 2766 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2767 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2768 | // FALL-THROUGH. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 2769 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2770 | default: { |
| 2771 | |
| 2772 | if (B->isAssignmentOp()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2773 | break; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2774 | |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2775 | // Process non-assignments except commas or short-circuited |
| 2776 | // logical expressions (LAnd and LOr). |
Zhongxing Xu | 262fd03 | 2009-05-20 09:00:16 +0000 | [diff] [blame] | 2777 | SVal Result = EvalBinOp(state, Op, LeftV, RightV, B->getType()); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2778 | |
| 2779 | if (Result.isUnknown()) { |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2780 | if (OldSt != state) { |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 2781 | // Generate a new node if we have already created a new state. |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2782 | MakeNode(Dst, B, *I2, state); |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 2783 | } |
| 2784 | else |
| 2785 | Dst.Add(*I2); |
| 2786 | |
Ted Kremenek | 89063af | 2008-02-21 19:15:37 +0000 | [diff] [blame] | 2787 | continue; |
| 2788 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2789 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2790 | if (Result.isUndef() && !LeftV.isUndef() && !RightV.isUndef()) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2791 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2792 | // The operands were *not* undefined, but the result is undefined. |
| 2793 | // This is a special node that should be flagged as an error. |
Ted Kremenek | 3c8d0c5 | 2008-02-25 18:42:54 +0000 | [diff] [blame] | 2794 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 2795 | if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I2)){ |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 2796 | UndefNode->markAsSink(); |
| 2797 | UndefResults.insert(UndefNode); |
| 2798 | } |
| 2799 | |
| 2800 | continue; |
| 2801 | } |
| 2802 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2803 | // Otherwise, create a new node. |
| 2804 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2805 | MakeNode(Dst, B, *I2, state->BindExpr(B, Result)); |
Ted Kremenek | e38718e | 2008-04-16 18:21:25 +0000 | [diff] [blame] | 2806 | continue; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 2807 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 2808 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 2809 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2810 | assert (B->isCompoundAssignmentOp()); |
| 2811 | |
Ted Kremenek | cad2996 | 2009-02-07 00:52:24 +0000 | [diff] [blame] | 2812 | switch (Op) { |
| 2813 | default: |
| 2814 | assert(0 && "Invalid opcode for compound assignment."); |
| 2815 | case BinaryOperator::MulAssign: Op = BinaryOperator::Mul; break; |
| 2816 | case BinaryOperator::DivAssign: Op = BinaryOperator::Div; break; |
| 2817 | case BinaryOperator::RemAssign: Op = BinaryOperator::Rem; break; |
| 2818 | case BinaryOperator::AddAssign: Op = BinaryOperator::Add; break; |
| 2819 | case BinaryOperator::SubAssign: Op = BinaryOperator::Sub; break; |
| 2820 | case BinaryOperator::ShlAssign: Op = BinaryOperator::Shl; break; |
| 2821 | case BinaryOperator::ShrAssign: Op = BinaryOperator::Shr; break; |
| 2822 | case BinaryOperator::AndAssign: Op = BinaryOperator::And; break; |
| 2823 | case BinaryOperator::XorAssign: Op = BinaryOperator::Xor; break; |
| 2824 | case BinaryOperator::OrAssign: Op = BinaryOperator::Or; break; |
Ted Kremenek | 934e3e9 | 2008-10-27 23:02:39 +0000 | [diff] [blame] | 2825 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2826 | |
| 2827 | // Perform a load (the LHS). This performs the checks for |
| 2828 | // null dereferences, and so on. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2829 | ExplodedNodeSet Tmp3; |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2830 | SVal location = state->getSVal(LHS); |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2831 | EvalLoad(Tmp3, LHS, *I2, state, location); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2832 | |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame^] | 2833 | for (ExplodedNodeSet::iterator I3=Tmp3.begin(), E3=Tmp3.end(); I3!=E3; |
| 2834 | ++I3) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2835 | |
Ted Kremenek | a8538d9 | 2009-02-13 01:45:31 +0000 | [diff] [blame] | 2836 | state = GetState(*I3); |
Ted Kremenek | 3ff1259 | 2009-06-19 17:10:32 +0000 | [diff] [blame] | 2837 | SVal V = state->getSVal(LHS); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2838 | |
| 2839 | // Propagate undefined values (left-side). |
| 2840 | if (V.isUndef()) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2841 | EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, V), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 2842 | location, V); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2843 | continue; |
| 2844 | } |
| 2845 | |
| 2846 | // Propagate unknown values (left and right-side). |
| 2847 | if (RightV.isUnknown() || V.isUnknown()) { |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2848 | EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, UnknownVal()), |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 2849 | location, UnknownVal()); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2850 | continue; |
| 2851 | } |
| 2852 | |
| 2853 | // At this point: |
| 2854 | // |
| 2855 | // The LHS is not Undef/Unknown. |
| 2856 | // The RHS is not Unknown. |
| 2857 | |
| 2858 | // Get the computation type. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2859 | QualType CTy = |
| 2860 | cast<CompoundAssignOperator>(B)->getComputationResultType(); |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2861 | CTy = getContext().getCanonicalType(CTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2862 | |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2863 | QualType CLHSTy = |
| 2864 | cast<CompoundAssignOperator>(B)->getComputationLHSType(); |
| 2865 | CLHSTy = getContext().getCanonicalType(CLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2866 | |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2867 | QualType LTy = getContext().getCanonicalType(LHS->getType()); |
| 2868 | QualType RTy = getContext().getCanonicalType(RHS->getType()); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Promote LHS. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2871 | llvm::tie(state, V) = SVator.EvalCast(V, state, CLHSTy, LTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 2872 | |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2873 | // Evaluate operands and promote to result type. |
Ted Kremenek | c13b6e2 | 2008-10-20 23:40:25 +0000 | [diff] [blame] | 2874 | if (RightV.isUndef()) { |
Ted Kremenek | 82bae3f | 2008-09-20 01:50:34 +0000 | [diff] [blame] | 2875 | // Propagate undefined values (right-side). |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2876 | EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, RightV), location, |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 2877 | RightV); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2878 | continue; |
| 2879 | } |
| 2880 | |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2881 | // Compute the result of the operation. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2882 | SVal Result; |
| 2883 | llvm::tie(state, Result) = SVator.EvalCast(EvalBinOp(state, Op, V, |
| 2884 | RightV, CTy), |
| 2885 | state, B->getType(), CTy); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2886 | |
| 2887 | if (Result.isUndef()) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2888 | // The operands were not undefined, but the result is undefined. |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2889 | if (ExplodedNode* UndefNode = Builder->generateNode(B, state, *I3)) { |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2890 | UndefNode->markAsSink(); |
| 2891 | UndefResults.insert(UndefNode); |
| 2892 | } |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2893 | continue; |
| 2894 | } |
Ted Kremenek | 9ff267d | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2895 | |
| 2896 | // EXPERIMENTAL: "Conjured" symbols. |
| 2897 | // FIXME: Handle structs. |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2898 | |
| 2899 | SVal LHSVal; |
| 2900 | |
Ted Kremenek | 276c6ac | 2009-03-11 02:24:48 +0000 | [diff] [blame] | 2901 | if ((Result.isUnknown() || |
| 2902 | !getConstraintManager().canReasonAbout(Result)) |
| 2903 | && (Loc::IsLocType(CTy) |
| 2904 | || (CTy->isScalarType() && CTy->isIntegerType()))) { |
Ted Kremenek | 0944ccc | 2008-10-21 19:49:01 +0000 | [diff] [blame] | 2905 | |
Ted Kremenek | 9ff267d | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2906 | unsigned Count = Builder->getCurrentBlockCount(); |
Ted Kremenek | 9ff267d | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2907 | |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2908 | // The symbolic value is actually for the type of the left-hand side |
| 2909 | // expression, not the computation type, as this is the value the |
| 2910 | // LValue on the LHS will bind to. |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2911 | LHSVal = ValMgr.getConjuredSymbolVal(B->getRHS(), LTy, Count); |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2912 | |
Zhongxing Xu | 1c0c233 | 2008-11-23 05:52:28 +0000 | [diff] [blame] | 2913 | // However, we need to convert the symbol to the computation type. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2914 | llvm::tie(state, Result) = SVator.EvalCast(LHSVal, state, CTy, LTy); |
Ted Kremenek | 9ff267d | 2008-10-20 23:13:25 +0000 | [diff] [blame] | 2915 | } |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2916 | else { |
| 2917 | // The left-hand side may bind to a different value then the |
| 2918 | // computation type. |
Ted Kremenek | 32c3fa4 | 2009-07-21 21:03:30 +0000 | [diff] [blame] | 2919 | llvm::tie(state, LHSVal) = SVator.EvalCast(Result, state, LTy, CTy); |
Ted Kremenek | 60595da | 2008-11-15 04:01:56 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 2922 | EvalStore(Dst, B, LHS, *I3, state->BindExpr(B, Result), |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 2923 | location, LHSVal); |
Ted Kremenek | 1b8bd4d | 2008-04-29 21:04:26 +0000 | [diff] [blame] | 2924 | } |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 2925 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 2926 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 2927 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2928 | |
| 2929 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 2930 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 2931 | //===----------------------------------------------------------------------===// |
| 2932 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2933 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 2934 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 2935 | static SourceManager* GraphPrintSourceManager; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 2936 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2937 | namespace llvm { |
| 2938 | template<> |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2939 | struct VISIBILITY_HIDDEN DOTGraphTraits<ExplodedNode*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2940 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 2941 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2942 | static std::string getNodeAttributes(const ExplodedNode* N, void*) { |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2943 | |
| 2944 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 2945 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 2946 | GraphPrintCheckerState->isUndefDeref(N) || |
| 2947 | GraphPrintCheckerState->isUndefStore(N) || |
| 2948 | GraphPrintCheckerState->isUndefControlFlow(N) || |
Ted Kremenek | 4d839b4 | 2008-03-07 19:04:53 +0000 | [diff] [blame] | 2949 | GraphPrintCheckerState->isExplicitBadDivide(N) || |
| 2950 | GraphPrintCheckerState->isImplicitBadDivide(N) || |
Ted Kremenek | 5e03fcb | 2008-02-29 23:14:48 +0000 | [diff] [blame] | 2951 | GraphPrintCheckerState->isUndefResult(N) || |
Ted Kremenek | 2ded35a | 2008-02-29 23:53:11 +0000 | [diff] [blame] | 2952 | GraphPrintCheckerState->isBadCall(N) || |
| 2953 | GraphPrintCheckerState->isUndefArg(N)) |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2954 | return "color=\"red\",style=\"filled\""; |
| 2955 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 2956 | if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 2957 | return "color=\"blue\",style=\"filled\""; |
| 2958 | |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 2959 | return ""; |
| 2960 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 2961 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 2962 | static std::string getNodeLabel(const ExplodedNode* N, void*,bool ShortNames){ |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 2963 | |
| 2964 | std::string sbuf; |
| 2965 | llvm::raw_string_ostream Out(sbuf); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 2966 | |
| 2967 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2968 | ProgramPoint Loc = N->getLocation(); |
| 2969 | |
| 2970 | switch (Loc.getKind()) { |
| 2971 | case ProgramPoint::BlockEntranceKind: |
| 2972 | Out << "Block Entrance: B" |
| 2973 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 2974 | break; |
| 2975 | |
| 2976 | case ProgramPoint::BlockExitKind: |
| 2977 | assert (false); |
| 2978 | break; |
| 2979 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 2980 | default: { |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 2981 | if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) { |
| 2982 | const Stmt* S = L->getStmt(); |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2983 | SourceLocation SLoc = S->getLocStart(); |
| 2984 | |
| 2985 | Out << S->getStmtClassName() << ' ' << (void*) S << ' '; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2986 | LangOptions LO; // FIXME. |
| 2987 | S->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2988 | |
| 2989 | if (SLoc.isFileID()) { |
| 2990 | Out << "\\lline=" |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 2991 | << GraphPrintSourceManager->getInstantiationLineNumber(SLoc) |
| 2992 | << " col=" |
| 2993 | << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc) |
| 2994 | << "\\l"; |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 2997 | if (isa<PreStmt>(Loc)) |
| 2998 | Out << "\\lPreStmt\\l;"; |
| 2999 | else if (isa<PostLoad>(Loc)) |
Ted Kremenek | 7090d54 | 2009-05-07 18:27:16 +0000 | [diff] [blame] | 3000 | Out << "\\lPostLoad\\l;"; |
| 3001 | else if (isa<PostStore>(Loc)) |
| 3002 | Out << "\\lPostStore\\l"; |
| 3003 | else if (isa<PostLValue>(Loc)) |
| 3004 | Out << "\\lPostLValue\\l"; |
| 3005 | else if (isa<PostLocationChecksSucceed>(Loc)) |
| 3006 | Out << "\\lPostLocationChecksSucceed\\l"; |
| 3007 | else if (isa<PostNullCheckFailed>(Loc)) |
| 3008 | Out << "\\lPostNullCheckFailed\\l"; |
| 3009 | |
Ted Kremenek | 8c35475 | 2008-12-16 22:02:27 +0000 | [diff] [blame] | 3010 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) |
| 3011 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 3012 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) |
| 3013 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 3014 | else if (GraphPrintCheckerState->isUndefDeref(N)) |
| 3015 | Out << "\\|Dereference of undefialied value.\\l"; |
| 3016 | else if (GraphPrintCheckerState->isUndefStore(N)) |
| 3017 | Out << "\\|Store to Undefined Loc."; |
| 3018 | else if (GraphPrintCheckerState->isExplicitBadDivide(N)) |
| 3019 | Out << "\\|Explicit divide-by zero or undefined value."; |
| 3020 | else if (GraphPrintCheckerState->isImplicitBadDivide(N)) |
| 3021 | Out << "\\|Implicit divide-by zero or undefined value."; |
| 3022 | else if (GraphPrintCheckerState->isUndefResult(N)) |
| 3023 | Out << "\\|Result of operation is undefined."; |
| 3024 | else if (GraphPrintCheckerState->isNoReturnCall(N)) |
| 3025 | Out << "\\|Call to function marked \"noreturn\"."; |
| 3026 | else if (GraphPrintCheckerState->isBadCall(N)) |
| 3027 | Out << "\\|Call to NULL/Undefined."; |
| 3028 | else if (GraphPrintCheckerState->isUndefArg(N)) |
| 3029 | Out << "\\|Argument in call is undefined"; |
| 3030 | |
| 3031 | break; |
| 3032 | } |
| 3033 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 3034 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 3035 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 3036 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 3037 | |
| 3038 | if (Stmt* T = E.getSrc()->getTerminator()) { |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 3039 | |
| 3040 | SourceLocation SLoc = T->getLocStart(); |
| 3041 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 3042 | Out << "\\|Terminator: "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 3043 | LangOptions LO; // FIXME. |
| 3044 | E.getSrc()->printTerminator(Out, LO); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 3045 | |
Ted Kremenek | 9b5551d | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 3046 | if (SLoc.isFileID()) { |
| 3047 | Out << "\\lline=" |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 3048 | << GraphPrintSourceManager->getInstantiationLineNumber(SLoc) |
| 3049 | << " col=" |
| 3050 | << GraphPrintSourceManager->getInstantiationColumnNumber(SLoc); |
Ted Kremenek | 9b5551d | 2008-03-09 03:30:59 +0000 | [diff] [blame] | 3051 | } |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 3052 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 3053 | if (isa<SwitchStmt>(T)) { |
| 3054 | Stmt* Label = E.getDst()->getLabel(); |
| 3055 | |
| 3056 | if (Label) { |
| 3057 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 3058 | Out << "\\lcase "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 3059 | LangOptions LO; // FIXME. |
| 3060 | C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 3061 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 3062 | if (Stmt* RHS = C->getRHS()) { |
| 3063 | Out << " .. "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 3064 | RHS->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | Out << ":"; |
| 3068 | } |
| 3069 | else { |
| 3070 | assert (isa<DefaultStmt>(Label)); |
| 3071 | Out << "\\ldefault:"; |
| 3072 | } |
| 3073 | } |
| 3074 | else |
| 3075 | Out << "\\l(implicit) default:"; |
| 3076 | } |
| 3077 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 3078 | // FIXME |
| 3079 | } |
| 3080 | else { |
| 3081 | Out << "\\lCondition: "; |
| 3082 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 3083 | Out << "true"; |
| 3084 | else |
| 3085 | Out << "false"; |
| 3086 | } |
| 3087 | |
| 3088 | Out << "\\l"; |
| 3089 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 3090 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 3091 | if (GraphPrintCheckerState->isUndefControlFlow(N)) { |
| 3092 | Out << "\\|Control-flow based on\\lUndefined value.\\l"; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 3093 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 3097 | Out << "\\|StateID: " << (void*) N->getState() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 3098 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3099 | const GRState *state = N->getState(); |
| 3100 | state->printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 3101 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 3102 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 3103 | return Out.str(); |
| 3104 | } |
| 3105 | }; |
| 3106 | } // end llvm namespace |
| 3107 | #endif |
| 3108 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3109 | #ifndef NDEBUG |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3110 | template <typename ITERATOR> |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3111 | ExplodedNode* GetGraphNode(ITERATOR I) { return *I; } |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3112 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3113 | template <> ExplodedNode* |
| 3114 | GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator> |
| 3115 | (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) { |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3116 | return I->first; |
| 3117 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3118 | #endif |
| 3119 | |
| 3120 | void GRExprEngine::ViewGraph(bool trim) { |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3121 | #ifndef NDEBUG |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3122 | if (trim) { |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3123 | std::vector<ExplodedNode*> Src; |
Ted Kremenek | 940abcc | 2009-03-11 01:41:22 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Flush any outstanding reports to make sure we cover all the nodes. |
| 3126 | // This does not cause them to get displayed. |
| 3127 | for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) |
| 3128 | const_cast<BugType*>(*I)->FlushReports(BR); |
| 3129 | |
| 3130 | // Iterate through the reports and get their nodes. |
| 3131 | for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I) { |
| 3132 | for (BugType::const_iterator I2=(*I)->begin(), E2=(*I)->end(); I2!=E2; ++I2) { |
| 3133 | const BugReportEquivClass& EQ = *I2; |
| 3134 | const BugReport &R = **EQ.begin(); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3135 | ExplodedNode *N = const_cast<ExplodedNode*>(R.getEndNode()); |
Ted Kremenek | 940abcc | 2009-03-11 01:41:22 +0000 | [diff] [blame] | 3136 | if (N) Src.push_back(N); |
| 3137 | } |
| 3138 | } |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3139 | |
Ted Kremenek | 7ec07fd | 2008-03-12 17:18:20 +0000 | [diff] [blame] | 3140 | ViewGraph(&Src[0], &Src[0]+Src.size()); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3141 | } |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3142 | else { |
| 3143 | GraphPrintCheckerState = this; |
| 3144 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 3145 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3146 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3147 | |
| 3148 | GraphPrintCheckerState = NULL; |
| 3149 | GraphPrintSourceManager = NULL; |
| 3150 | } |
| 3151 | #endif |
| 3152 | } |
| 3153 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3154 | void GRExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) { |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3155 | #ifndef NDEBUG |
| 3156 | GraphPrintCheckerState = this; |
| 3157 | GraphPrintSourceManager = &getContext().getSourceManager(); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 3158 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 3159 | std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first); |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3160 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 3161 | if (!TrimmedG.get()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 3162 | llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n"; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 3163 | else |
Ted Kremenek | 493d7a2 | 2008-03-11 18:25:33 +0000 | [diff] [blame] | 3164 | llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedGRExprEngine"); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 3165 | |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 3166 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e97ca06 | 2008-03-07 20:57:30 +0000 | [diff] [blame] | 3167 | GraphPrintSourceManager = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 3168 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 3169 | } |