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 | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 18 | |
| 19 | #include "llvm/Support/Streams.h" |
Ted Kremenek | b387a3f | 2008-02-14 22:16:04 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using llvm::dyn_cast; |
| 23 | using llvm::cast; |
| 24 | using llvm::APSInt; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 26 | GRExprEngine::StateTy |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 27 | GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal& V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 29 | if (!StateCleaned) { |
| 30 | St = RemoveDeadBindings(CurrentStmt, St); |
| 31 | StateCleaned = true; |
| 32 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 34 | bool isBlkExpr = false; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 36 | if (Ex == CurrentStmt) { |
| 37 | isBlkExpr = getCFG().isBlkExpr(Ex); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 38 | |
| 39 | if (!isBlkExpr) |
| 40 | return St; |
| 41 | } |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 43 | return StateMgr.SetRVal(St, Ex, isBlkExpr, V); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 46 | const GRExprEngine::StateTy::BufferTy& |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 47 | GRExprEngine::SetRVal(StateTy St, Expr* Ex, const RVal::BufferTy& RB, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 48 | StateTy::BufferTy& RetBuf) { |
| 49 | |
| 50 | assert (RetBuf.empty()); |
| 51 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 52 | for (RVal::BufferTy::const_iterator I = RB.begin(), E = RB.end(); I!=E; ++I) |
| 53 | RetBuf.push_back(SetRVal(St, Ex, *I)); |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 54 | |
| 55 | return RetBuf; |
| 56 | } |
| 57 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 58 | GRExprEngine::StateTy |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 59 | GRExprEngine::SetRVal(StateTy St, const LVal& LV, const RVal& RV) { |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 60 | |
| 61 | if (!StateCleaned) { |
| 62 | St = RemoveDeadBindings(CurrentStmt, St); |
| 63 | StateCleaned = true; |
| 64 | } |
| 65 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 66 | return StateMgr.SetRVal(St, LV, RV); |
Ted Kremenek | e070a1d | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 69 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 70 | BranchNodeBuilder& builder) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 72 | // Remove old bindings for subexpressions. |
| 73 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 75 | // Check for NULL conditions; e.g. "for(;;)" |
| 76 | if (!Condition) { |
| 77 | builder.markInfeasible(false); |
| 78 | |
| 79 | // Get the current block counter. |
| 80 | GRBlockCounter BC = builder.getBlockCounter(); |
| 81 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 82 | unsigned NumVisited = BC.getNumVisited(BlockID); |
| 83 | |
| 84 | if (NumVisited < 1) builder.generateNode(PrevState, true); |
| 85 | else builder.markInfeasible(true); |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 90 | RVal V = GetRVal(PrevState, Condition); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 91 | |
| 92 | switch (V.getBaseKind()) { |
| 93 | default: |
| 94 | break; |
| 95 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 96 | case RVal::UnknownKind: |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 97 | builder.generateNode(PrevState, true); |
| 98 | builder.generateNode(PrevState, false); |
| 99 | return; |
| 100 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 101 | case RVal::UninitializedKind: { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 102 | NodeTy* N = builder.generateNode(PrevState, true); |
| 103 | |
| 104 | if (N) { |
| 105 | N->markAsSink(); |
| 106 | UninitBranches.insert(N); |
| 107 | } |
| 108 | |
| 109 | builder.markInfeasible(false); |
| 110 | return; |
| 111 | } |
| 112 | } |
Ted Kremenek | b233183 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 114 | // Get the current block counter. |
| 115 | GRBlockCounter BC = builder.getBlockCounter(); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 116 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 117 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 119 | if (isa<nonlval::ConcreteInt>(V) || |
| 120 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 121 | |
| 122 | // Process the true branch. |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 124 | bool isFeasible = true; |
| 125 | |
| 126 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 127 | |
| 128 | if (isFeasible) |
| 129 | builder.generateNode(St, true); |
| 130 | else |
| 131 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 132 | } |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 133 | else |
| 134 | builder.markInfeasible(true); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 136 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 137 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 139 | if (isa<nonlval::ConcreteInt>(V) || |
| 140 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 141 | |
| 142 | // Process the false branch. |
| 143 | |
| 144 | bool isFeasible = false; |
| 145 | |
| 146 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 147 | |
| 148 | if (isFeasible) |
| 149 | builder.generateNode(St, false); |
| 150 | else |
| 151 | builder.markInfeasible(false); |
| 152 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 153 | else |
| 154 | builder.markInfeasible(false); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 157 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 158 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 159 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 160 | |
| 161 | StateTy St = builder.getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 162 | RVal V = GetRVal(St, builder.getTarget()); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 163 | |
| 164 | // Three possibilities: |
| 165 | // |
| 166 | // (1) We know the computed label. |
| 167 | // (2) The label is NULL (or some other constant), or Uninitialized. |
| 168 | // (3) We have no clue about the label. Dispatch to all targets. |
| 169 | // |
| 170 | |
| 171 | typedef IndirectGotoNodeBuilder::iterator iterator; |
| 172 | |
| 173 | if (isa<lval::GotoLabel>(V)) { |
| 174 | LabelStmt* L = cast<lval::GotoLabel>(V).getLabel(); |
| 175 | |
| 176 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 177 | if (I.getLabel() == L) { |
| 178 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | assert (false && "No block with label."); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) { |
| 188 | // Dispatch to the first target and mark it as a sink. |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 189 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 190 | UninitBranches.insert(N); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // This is really a catch-all. We don't support symbolics yet. |
| 195 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 196 | assert (V.isUnknown()); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 197 | |
| 198 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 24f1a96 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 199 | builder.generateNode(I, St); |
Ted Kremenek | 754607e | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 200 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 202 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 203 | /// nodes by processing the 'effects' of a switch statement. |
| 204 | void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) { |
| 205 | |
| 206 | typedef SwitchNodeBuilder::iterator iterator; |
| 207 | |
| 208 | StateTy St = builder.getState(); |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 209 | Expr* CondE = builder.getCondition(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 210 | RVal CondV = GetRVal(St, CondE); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 212 | if (CondV.isUninit()) { |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 213 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 214 | UninitBranches.insert(N); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | StateTy DefaultSt = St; |
| 219 | |
| 220 | // While most of this can be assumed (such as the signedness), having it |
| 221 | // just computed makes sure everything makes the same assumptions end-to-end. |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 222 | |
| 223 | unsigned bits = getContext().getTypeSize(CondE->getType(), |
| 224 | CondE->getExprLoc()); |
| 225 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 226 | APSInt V1(bits, false); |
| 227 | APSInt V2 = V1; |
| 228 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 229 | for (iterator I = builder.begin(), EI = builder.end(); I != EI; ++I) { |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 230 | |
| 231 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 232 | |
| 233 | // Evaluate the case. |
| 234 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 235 | assert (false && "Case condition must evaluate to an integer constant."); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | // Get the RHS of the case, if it exists. |
| 240 | |
| 241 | if (Expr* E = Case->getRHS()) { |
| 242 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 243 | assert (false && |
| 244 | "Case condition (RHS) must evaluate to an integer constant."); |
| 245 | return ; |
| 246 | } |
| 247 | |
| 248 | assert (V1 <= V2); |
| 249 | } |
| 250 | else V2 = V1; |
| 251 | |
| 252 | // FIXME: Eventually we should replace the logic below with a range |
| 253 | // comparison, rather than concretize the values within the range. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 254 | // This should be easy once we have "ranges" for NonLVals. |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 255 | |
| 256 | do { |
| 257 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 258 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 259 | RVal Res = EvalBinOp(BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 260 | |
| 261 | // Now "assume" that the case matches. |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 262 | bool isFeasible = false; |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 264 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 265 | |
| 266 | if (isFeasible) { |
| 267 | builder.generateCaseStmtNode(I, StNew); |
| 268 | |
| 269 | // If CondV evaluates to a constant, then we know that this |
| 270 | // is the *only* case that we can take, so stop evaluating the |
| 271 | // others. |
| 272 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // Now "assume" that the case doesn't match. Add this state |
| 277 | // to the default state (if it is feasible). |
| 278 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 279 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 280 | |
| 281 | if (isFeasible) |
| 282 | DefaultSt = StNew; |
| 283 | |
| 284 | // Concretize the next value in the range. |
| 285 | ++V1; |
| 286 | |
| 287 | } while (V1 < V2); |
| 288 | } |
| 289 | |
| 290 | // If we reach here, than we know that the default branch is |
| 291 | // possible. |
| 292 | builder.generateDefaultCaseNode(DefaultSt); |
| 293 | } |
| 294 | |
| 295 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 296 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 297 | NodeSet& Dst) { |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 298 | |
| 299 | bool hasR2; |
| 300 | StateTy PrevState = Pred->getState(); |
| 301 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 302 | RVal R1 = GetRVal(PrevState, B->getLHS()); |
| 303 | RVal R2 = GetRVal(PrevState, B->getRHS(), hasR2); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 304 | |
| 305 | if (hasR2) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 306 | if (R2.isUnknownOrUninit()) { |
| 307 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, R2)); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 311 | else if (R1.isUnknownOrUninit()) { |
| 312 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, R1)); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 317 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 318 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 319 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 320 | if (hasR2 == false) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 321 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | } |
| 325 | else { |
| 326 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 327 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 328 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 329 | if (hasR2 == false) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 330 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 336 | // R2 == false. |
| 337 | |
| 338 | bool isFeasible; |
| 339 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 340 | |
| 341 | if (isFeasible) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 342 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(1U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 343 | |
| 344 | St = Assume(PrevState, R2, false, isFeasible); |
| 345 | |
| 346 | if (isFeasible) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 347 | Nodify(Dst, B, Pred, SetRVal(PrevState, B, MakeConstantVal(0U, B))); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 352 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 354 | Builder = &builder; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 355 | StmtEntryNode = builder.getLastNode(); |
| 356 | CurrentStmt = S; |
| 357 | NodeSet Dst; |
| 358 | StateCleaned = false; |
| 359 | |
| 360 | Visit(S, StmtEntryNode, Dst); |
| 361 | |
| 362 | // If no nodes were generated, generate a new node that has all the |
| 363 | // dead mappings removed. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 364 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 365 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 366 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 367 | builder.generateNode(S, St, StmtEntryNode); |
| 368 | } |
Ted Kremenek | f84469b | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 370 | // For safety, NULL out these variables. |
| 371 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 372 | CurrentStmt = NULL; |
| 373 | StmtEntryNode = NULL; |
| 374 | Builder = NULL; |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 377 | GRExprEngine::NodeTy* |
| 378 | GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, StateTy St) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 379 | |
| 380 | // If the state hasn't changed, don't generate a new node. |
Ted Kremenek | 7e59336 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 381 | if (St == Pred->getState()) |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 382 | return NULL; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 384 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 385 | Dst.Add(N); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 387 | return N; |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 388 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 390 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 391 | const StateTy::BufferTy& SB) { |
| 392 | |
| 393 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 394 | Nodify(Dst, S, Pred, *I); |
| 395 | } |
| 396 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 397 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 399 | if (D != CurrentStmt) { |
| 400 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | // If we are here, we are loading the value of the decl and binding |
| 405 | // it to the block-level expression. |
| 406 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 407 | StateTy St = Pred->getState(); |
| 408 | Nodify(Dst, D, Pred, SetRVal(St, D, GetRVal(St, D))); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 411 | void GRExprEngine::VisitCall(CallExpr* CE, NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 412 | CallExpr::arg_iterator AI, |
| 413 | CallExpr::arg_iterator AE, |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 414 | NodeSet& Dst) { |
| 415 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 416 | // Process the arguments. |
| 417 | |
| 418 | if (AI != AE) { |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 420 | NodeSet DstTmp; |
| 421 | Visit(*AI, Pred, DstTmp); |
| 422 | ++AI; |
| 423 | |
| 424 | for (NodeSet::iterator DI=DstTmp.begin(), DE=DstTmp.end(); DI != DE; ++DI) |
| 425 | VisitCall(CE, *DI, AI, AE, Dst); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 426 | |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | // If we reach here we have processed all of the arguments. Evaluate |
| 431 | // the callee expression. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 432 | NodeSet DstTmp; |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 433 | Visit(CE->getCallee(), Pred, DstTmp); |
| 434 | |
| 435 | // Finally, evaluate the function call. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 436 | for (NodeSet::iterator DI = DstTmp.begin(), DE = DstTmp.end(); DI!=DE; ++DI) { |
| 437 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 438 | StateTy St = (*DI)->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 439 | RVal L = GetLVal(St, CE->getCallee()); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 440 | |
| 441 | // Check for uninitialized control-flow. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 442 | |
| 443 | if (L.isUninit()) { |
| 444 | |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 445 | NodeTy* N = Builder->generateNode(CE, St, *DI); |
| 446 | N->markAsSink(); |
| 447 | UninitBranches.insert(N); |
| 448 | continue; |
| 449 | } |
| 450 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 451 | // FIXME: EvalCall must handle the case where the callee is Unknown. |
| 452 | assert (!L.isUnknown()); |
| 453 | |
| 454 | Nodify(Dst, CE, *DI, EvalCall(CE, cast<LVal>(L), (*DI)->getState())); |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 458 | void GRExprEngine::VisitCast(Expr* CastE, Expr* Ex, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 460 | NodeSet S1; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 461 | Visit(Ex, Pred, S1); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 463 | QualType T = CastE->getType(); |
| 464 | |
Ted Kremenek | 402563b | 2008-02-19 18:47:04 +0000 | [diff] [blame] | 465 | // Check for redundant casts or casting to "void" |
| 466 | if (T->isVoidType() || |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 467 | Ex->getType() == T || |
| 468 | (T->isPointerType() && Ex->getType()->isFunctionType())) { |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 470 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) |
Ted Kremenek | 5d3003a | 2008-02-19 18:52:54 +0000 | [diff] [blame] | 471 | Dst.Add(*I1); |
| 472 | |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 476 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 477 | NodeTy* N = *I1; |
| 478 | StateTy St = N->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 479 | RVal V = GetRVal(St, Ex); |
| 480 | Nodify(Dst, CastE, N, SetRVal(St, CastE, EvalCast(ValMgr, V, CastE))); |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 481 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 484 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 485 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 486 | |
| 487 | StateTy St = Pred->getState(); |
| 488 | |
| 489 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 490 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | c2c95b0 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 491 | |
| 492 | // FIXME: Add support for local arrays. |
| 493 | if (VD->getType()->isArrayType()) |
| 494 | continue; |
| 495 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 496 | const Expr* Ex = VD->getInit(); |
| 497 | |
| 498 | St = SetRVal(St, lval::DeclVal(VD), |
| 499 | Ex ? GetRVal(St, Ex) : UninitializedVal()); |
Ted Kremenek | 403c181 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 500 | } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 501 | |
| 502 | Nodify(Dst, DS, Pred, St); |
| 503 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 504 | if (Dst.empty()) { Dst.Add(Pred); } |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 505 | } |
Ted Kremenek | 874d63f | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 507 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 508 | void GRExprEngine::VisitGuardedExpr(Expr* Ex, Expr* L, Expr* R, |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 509 | NodeTy* Pred, NodeSet& Dst) { |
| 510 | |
| 511 | StateTy St = Pred->getState(); |
| 512 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 513 | RVal V = GetRVal(St, L); |
| 514 | if (isa<UnknownVal>(V)) V = GetRVal(St, R); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 516 | Nodify(Dst, Ex, Pred, SetRVal(St, Ex, V)); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 519 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 520 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* Ex, |
| 521 | NodeTy* Pred, |
| 522 | NodeSet& Dst) { |
| 523 | |
| 524 | assert (Ex->isSizeOf() && "AlignOf(Expr) not yet implemented."); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 525 | |
| 526 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 527 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 528 | QualType T = Ex->getArgumentType(); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 529 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 530 | // FIXME: Implement alignof |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | 297d0d7 | 2008-02-21 18:15:29 +0000 | [diff] [blame^] | 532 | // FIXME: Add support for VLAs. |
Eli Friedman | d868856 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 533 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 534 | return; |
| 535 | |
Ted Kremenek | 297d0d7 | 2008-02-21 18:15:29 +0000 | [diff] [blame^] | 536 | |
| 537 | uint64_t size = 1; // Handle sizeof(void) |
| 538 | |
| 539 | if (T != getContext().VoidTy) { |
| 540 | SourceLocation Loc = Ex->getExprLoc(); |
| 541 | size = getContext().getTypeSize(T, Loc) / 8; |
| 542 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 544 | Nodify(Dst, Ex, Pred, |
| 545 | SetRVal(Pred->getState(), Ex, |
Ted Kremenek | 297d0d7 | 2008-02-21 18:15:29 +0000 | [diff] [blame^] | 546 | NonLVal::MakeVal(ValMgr, size, Ex->getType()))); |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 547 | |
| 548 | } |
| 549 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 550 | void GRExprEngine::VisitDeref(UnaryOperator* U, NodeTy* Pred, NodeSet& Dst) { |
| 551 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 552 | Expr* Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 553 | |
| 554 | NodeSet DstTmp; |
| 555 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 556 | if (!isa<DeclRefExpr>(Ex)) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 557 | DstTmp.Add(Pred); |
| 558 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 559 | Visit(Ex, Pred, DstTmp); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 561 | for (NodeSet::iterator I = DstTmp.begin(), DE = DstTmp.end(); I != DE; ++I) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 562 | |
| 563 | NodeTy* N = *I; |
| 564 | StateTy St = N->getState(); |
| 565 | |
| 566 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 567 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 568 | RVal V = GetRVal(St, Ex); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 569 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 570 | // Check for dereferences of uninitialized values. |
| 571 | |
| 572 | if (V.isUninit()) { |
| 573 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 574 | NodeTy* Succ = Builder->generateNode(U, St, N); |
| 575 | |
| 576 | if (Succ) { |
| 577 | Succ->markAsSink(); |
| 578 | UninitDeref.insert(Succ); |
| 579 | } |
| 580 | |
| 581 | continue; |
| 582 | } |
| 583 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 584 | // Check for dereferences of unknown values. Treat as No-Ops. |
| 585 | |
| 586 | if (V.isUnknown()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 587 | Dst.Add(N); |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | // After a dereference, one of two possible situations arise: |
| 592 | // (1) A crash, because the pointer was NULL. |
| 593 | // (2) The pointer is not NULL, and the dereference works. |
| 594 | // |
| 595 | // We add these assumptions. |
| 596 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 597 | LVal LV = cast<LVal>(V); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 598 | bool isFeasibleNotNull; |
| 599 | |
| 600 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 601 | |
| 602 | StateTy StNotNull = Assume(St, LV, true, isFeasibleNotNull); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 603 | |
| 604 | if (isFeasibleNotNull) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 605 | |
| 606 | // FIXME: Currently symbolic analysis "generates" new symbols |
| 607 | // for the contents of values. We need a better approach. |
| 608 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 609 | Nodify(Dst, U, N, SetRVal(StNotNull, U, |
| 610 | GetRVal(StNotNull, LV, U->getType()))); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | bool isFeasibleNull; |
| 614 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 615 | // Now "assume" that the pointer is NULL. |
| 616 | |
| 617 | StateTy StNull = Assume(St, LV, false, isFeasibleNull); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 618 | |
| 619 | if (isFeasibleNull) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 620 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 621 | // We don't use "Nodify" here because the node will be a sink |
| 622 | // and we have no intention of processing it later. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 623 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 624 | NodeTy* NullNode = Builder->generateNode(U, StNull, N); |
| 625 | |
| 626 | if (NullNode) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 627 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 628 | NullNode->markAsSink(); |
| 629 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 630 | if (isFeasibleNotNull) ImplicitNullDeref.insert(NullNode); |
| 631 | else ExplicitNullDeref.insert(NullNode); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 637 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, NodeTy* Pred, |
| 638 | NodeSet& Dst) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 640 | NodeSet S1; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 641 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 642 | assert (U->getOpcode() != UnaryOperator::Deref); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 643 | assert (U->getOpcode() != UnaryOperator::SizeOf); |
| 644 | assert (U->getOpcode() != UnaryOperator::AlignOf); |
| 645 | |
| 646 | bool use_GetLVal = false; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 647 | |
| 648 | switch (U->getOpcode()) { |
| 649 | case UnaryOperator::PostInc: |
| 650 | case UnaryOperator::PostDec: |
| 651 | case UnaryOperator::PreInc: |
| 652 | case UnaryOperator::PreDec: |
| 653 | case UnaryOperator::AddrOf: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 654 | // Evalue subexpression as an LVal. |
| 655 | use_GetLVal = true; |
| 656 | VisitLVal(U->getSubExpr(), Pred, S1); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 657 | break; |
| 658 | |
| 659 | default: |
| 660 | Visit(U->getSubExpr(), Pred, S1); |
| 661 | break; |
| 662 | } |
| 663 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 664 | for (NodeSet::iterator I1 = S1.begin(), E1 = S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 665 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 666 | NodeTy* N1 = *I1; |
| 667 | StateTy St = N1->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 668 | |
| 669 | RVal SubV = use_GetLVal ? GetLVal(St, U->getSubExpr()) : |
| 670 | GetRVal(St, U->getSubExpr()); |
| 671 | |
| 672 | if (SubV.isUnknown()) { |
| 673 | Dst.Add(N1); |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | if (SubV.isUninit()) { |
| 678 | Nodify(Dst, U, N1, SetRVal(St, U, SubV)); |
| 679 | continue; |
| 680 | } |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 681 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 682 | if (U->isIncrementDecrementOp()) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 683 | |
| 684 | // Handle ++ and -- (both pre- and post-increment). |
| 685 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 686 | LVal SubLV = cast<LVal>(SubV); |
| 687 | RVal V = GetRVal(St, SubLV, U->getType()); |
| 688 | |
| 689 | // An LVal should never bind to UnknownVal. |
| 690 | assert (!V.isUnknown()); |
| 691 | |
| 692 | // Propagate uninitialized values. |
| 693 | if (V.isUninit()) { |
| 694 | Nodify(Dst, U, N1, SetRVal(St, U, V)); |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | // Handle all NonLVals. |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 699 | |
| 700 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 701 | : BinaryOperator::Sub; |
| 702 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 703 | RVal Result = EvalBinOp(Op, cast<NonLVal>(V), MakeConstantVal(1U, U)); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 704 | |
| 705 | if (U->isPostfix()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 706 | St = SetRVal(SetRVal(St, U, V), SubLV, Result); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 707 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 708 | St = SetRVal(SetRVal(St, U, Result), SubLV, Result); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 709 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 710 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 711 | continue; |
| 712 | } |
| 713 | |
| 714 | // Handle all other unary operators. |
| 715 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 716 | switch (U->getOpcode()) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 717 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 718 | case UnaryOperator::Minus: |
| 719 | St = SetRVal(St, U, EvalMinus(U, cast<NonLVal>(SubV))); |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 720 | break; |
Ted Kremenek | dacbb4f | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 721 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 722 | case UnaryOperator::Not: |
| 723 | St = SetRVal(St, U, EvalComplement(cast<NonLVal>(SubV))); |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 724 | break; |
Ted Kremenek | 90e4203 | 2008-02-20 04:12:31 +0000 | [diff] [blame] | 725 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 726 | case UnaryOperator::LNot: |
Ted Kremenek | c5d3b4c | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 727 | |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 728 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 729 | // |
| 730 | // Note: technically we do "E == 0", but this is the same in the |
| 731 | // transfer functions as "0 == E". |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 732 | |
| 733 | if (isa<LVal>(SubV)) { |
| 734 | lval::ConcreteInt V(ValMgr.getZeroWithPtrWidth()); |
| 735 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<LVal>(SubV), V); |
| 736 | St = SetRVal(St, U, Result); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 737 | } |
| 738 | else { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 739 | nonlval::ConcreteInt V(ValMgr.getZeroWithPtrWidth()); |
| 740 | RVal Result = EvalBinOp(BinaryOperator::EQ, cast<NonLVal>(SubV), V); |
| 741 | St = SetRVal(St, U, Result); |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | break; |
Ted Kremenek | c60f0f7 | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 745 | |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 746 | case UnaryOperator::AddrOf: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 747 | assert (isa<LVal>(SubV)); |
| 748 | St = SetRVal(St, U, SubV); |
Ted Kremenek | 6492485 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 749 | break; |
| 750 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 751 | |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 752 | default: ; |
| 753 | assert (false && "Not implemented."); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | Nodify(Dst, U, N1, St); |
Ted Kremenek | 7b8009a | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 760 | void GRExprEngine::VisitSizeOfExpr(UnaryOperator* U, NodeTy* Pred, |
| 761 | NodeSet& Dst) { |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 762 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 763 | QualType T = U->getSubExpr()->getType(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 764 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 765 | // FIXME: Add support for VLAs. |
| 766 | if (!T.getTypePtr()->isConstantSizeType()) |
| 767 | return; |
| 768 | |
| 769 | SourceLocation Loc = U->getExprLoc(); |
| 770 | uint64_t size = getContext().getTypeSize(T, Loc) / 8; |
| 771 | StateTy St = Pred->getState(); |
| 772 | St = SetRVal(St, U, NonLVal::MakeVal(ValMgr, size, U->getType(), Loc)); |
| 773 | |
| 774 | Nodify(Dst, U, Pred, St); |
| 775 | } |
| 776 | |
| 777 | void GRExprEngine::VisitLVal(Expr* Ex, NodeTy* Pred, NodeSet& Dst) { |
| 778 | |
| 779 | assert (Ex != CurrentStmt && !getCFG().isBlkExpr(Ex)); |
| 780 | |
| 781 | Ex = Ex->IgnoreParens(); |
| 782 | |
| 783 | if (isa<DeclRefExpr>(Ex)) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 784 | Dst.Add(Pred); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 785 | return; |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 786 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 787 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 788 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(Ex)) { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 789 | if (U->getOpcode() == UnaryOperator::Deref) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 790 | Ex = U->getSubExpr()->IgnoreParens(); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 791 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 792 | if (isa<DeclRefExpr>(Ex)) |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 793 | Dst.Add(Pred); |
| 794 | else |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 795 | Visit(Ex, Pred, Dst); |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | } |
| 800 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 801 | Visit(Ex, Pred, Dst); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 804 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 805 | GRExprEngine::NodeTy* Pred, |
| 806 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 807 | NodeSet S1; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 808 | |
| 809 | if (B->isAssignmentOp()) |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 810 | VisitLVal(B->getLHS(), Pred, S1); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 811 | else |
| 812 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 813 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 814 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 815 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 816 | NodeTy* N1 = *I1; |
Ted Kremenek | e00fe3f | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 817 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 818 | // When getting the value for the LHS, check if we are in an assignment. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 819 | // In such cases, we want to (initially) treat the LHS as an LVal, |
| 820 | // so we use GetLVal instead of GetRVal so that DeclRefExpr's are |
| 821 | // evaluated to LValDecl's instead of to an NonLVal. |
| 822 | |
| 823 | RVal LeftV = B->isAssignmentOp() ? GetLVal(N1->getState(), B->getLHS()) |
| 824 | : GetRVal(N1->getState(), B->getLHS()); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 825 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 826 | // Visit the RHS... |
| 827 | |
| 828 | NodeSet S2; |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 829 | Visit(B->getRHS(), N1, S2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 830 | |
| 831 | // Process the binary operator. |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 832 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 833 | for (NodeSet::iterator I2 = S2.begin(), E2 = S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 834 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 835 | NodeTy* N2 = *I2; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 836 | StateTy St = N2->getState(); |
| 837 | RVal RightV = GetRVal(St, B->getRHS()); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 838 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 839 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 840 | |
| 841 | if (Op <= BinaryOperator::Or) { |
| 842 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 843 | // Process non-assignements except commas or short-circuited |
| 844 | // logical expressions (LAnd and LOr). |
| 845 | |
| 846 | RVal Result = EvalBinOp(Op, LeftV, RightV); |
| 847 | |
| 848 | if (Result.isUnknown()) { |
| 849 | Dst.Add(N2); |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 850 | continue; |
| 851 | } |
| 852 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 853 | Nodify(Dst, B, N2, SetRVal(St, B, Result)); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 854 | continue; |
| 855 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 856 | |
| 857 | // Process assignments. |
| 858 | |
| 859 | switch (Op) { |
| 860 | |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 861 | case BinaryOperator::Assign: { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 862 | |
| 863 | // Simple assignments. |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 864 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 865 | if (LeftV.isUninit()) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 866 | HandleUninitializedStore(B, N2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 867 | continue; |
| 868 | } |
| 869 | |
| 870 | if (LeftV.isUnknown()) { |
| 871 | St = SetRVal(St, B, RightV); |
| 872 | break; |
| 873 | } |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 874 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 875 | St = SetRVal(SetRVal(St, B, RightV), cast<LVal>(LeftV), RightV); |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 879 | // Compound assignment operators. |
Ted Kremenek | 687af80 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 880 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 881 | default: { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 882 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 883 | assert (B->isCompoundAssignmentOp()); |
| 884 | |
| 885 | if (LeftV.isUninit()) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 886 | HandleUninitializedStore(B, N2); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 887 | continue; |
| 888 | } |
| 889 | |
| 890 | if (LeftV.isUnknown()) { |
| 891 | |
| 892 | // While we do not know the location to store RightV, |
| 893 | // the entire expression does evaluate to RightV. |
| 894 | |
| 895 | if (RightV.isUnknown()) { |
| 896 | Dst.Add(N2); |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | St = SetRVal(St, B, RightV); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 901 | break; |
| 902 | } |
| 903 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 904 | // At this pointer we know that the LHS evaluates to an LVal |
| 905 | // that is neither "Unknown" or "Unintialized." |
| 906 | |
| 907 | LVal LeftLV = cast<LVal>(LeftV); |
| 908 | |
| 909 | // Propagate uninitialized values (right-side). |
| 910 | |
| 911 | if (RightV.isUninit()) { |
| 912 | St = SetRVal(SetRVal(St, B, RightV), LeftLV, RightV); |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 913 | break; |
| 914 | } |
| 915 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 916 | // Fetch the value of the LHS (the value of the variable, etc.). |
| 917 | |
| 918 | RVal V = GetRVal(N1->getState(), LeftLV, B->getLHS()->getType()); |
| 919 | |
| 920 | // Propagate uninitialized value (left-side). |
| 921 | |
| 922 | if (V.isUninit()) { |
| 923 | St = SetRVal(St, B, V); |
| 924 | break; |
| 925 | } |
| 926 | |
| 927 | // Propagate unknown values. |
| 928 | |
| 929 | assert (!V.isUnknown() && |
| 930 | "An LVal should never bind to UnknownVal"); |
| 931 | |
| 932 | if (RightV.isUnknown()) { |
| 933 | St = SetRVal(SetRVal(St, LeftLV, RightV), B, RightV); |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | // Neither the LHS or the RHS have Unknown/Uninit values. Process |
| 938 | // the operation and store the result. |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 939 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 940 | if (Op >= BinaryOperator::AndAssign) |
| 941 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 942 | else |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 943 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 944 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 945 | RVal Result = EvalBinOp(Op, V, RightV); |
| 946 | St = SetRVal(SetRVal(St, B, Result), LeftLV, Result); |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 947 | } |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 948 | } |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 949 | |
| 950 | Nodify(Dst, B, N2, St); |
Ted Kremenek | cb448ca | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 951 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 952 | } |
Ted Kremenek | d27f816 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 953 | } |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 954 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 955 | void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 956 | NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred); |
| 957 | N->markAsSink(); |
| 958 | UninitStores.insert(N); |
| 959 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 960 | |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 961 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 962 | |
| 963 | // FIXME: add metadata to the CFG so that we can disable |
| 964 | // this check when we KNOW that there is no block-level subexpression. |
| 965 | // The motivation is that this check requires a hashtable lookup. |
| 966 | |
| 967 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 968 | Dst.Add(Pred); |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | switch (S->getStmtClass()) { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 973 | |
| 974 | default: |
| 975 | // Cases we intentionally have "default" handle: |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 976 | // AddrLabelExpr |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 977 | |
| 978 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 979 | break; |
| 980 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 981 | case Stmt::BinaryOperatorClass: { |
| 982 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 984 | if (B->isLogicalOp()) { |
| 985 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 986 | break; |
| 987 | } |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 988 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 989 | StateTy St = Pred->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 990 | Nodify(Dst, B, Pred, SetRVal(St, B, GetRVal(St, B->getRHS()))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 993 | |
| 994 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 995 | break; |
| 996 | } |
Ted Kremenek | de43424 | 2008-02-19 01:44:53 +0000 | [diff] [blame] | 997 | |
| 998 | case Stmt::CallExprClass: { |
| 999 | CallExpr* C = cast<CallExpr>(S); |
| 1000 | VisitCall(C, Pred, C->arg_begin(), C->arg_end(), Dst); |
| 1001 | break; |
| 1002 | } |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1003 | |
| 1004 | case Stmt::CastExprClass: { |
| 1005 | CastExpr* C = cast<CastExpr>(S); |
| 1006 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1007 | break; |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1008 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1009 | |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1010 | // While explicitly creating a node+state for visiting a CharacterLiteral |
| 1011 | // seems wasteful, it also solves a bunch of problems when handling |
| 1012 | // the ?, &&, and ||. |
| 1013 | |
| 1014 | case Stmt::CharacterLiteralClass: { |
| 1015 | CharacterLiteral* C = cast<CharacterLiteral>(S); |
| 1016 | StateTy St = Pred->getState(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1017 | NonLVal X = NonLVal::MakeVal(ValMgr, C->getValue(), C->getType(), |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1018 | C->getLoc()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1019 | Nodify(Dst, C, Pred, SetRVal(St, C, X)); |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1023 | // FIXME: ChooseExpr is really a constant. We need to fix |
| 1024 | // the CFG do not model them as explicit control-flow. |
| 1025 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1026 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 1027 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 1028 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1029 | break; |
| 1030 | } |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | b4ae33f | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 1032 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | ab2b8c5 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 1033 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 1034 | break; |
| 1035 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1036 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 1037 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 1038 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | case Stmt::DeclRefExprClass: |
| 1043 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 1044 | break; |
| 1045 | |
| 1046 | case Stmt::DeclStmtClass: |
| 1047 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 1048 | break; |
| 1049 | |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1050 | // While explicitly creating a node+state for visiting an IntegerLiteral |
| 1051 | // seems wasteful, it also solves a bunch of problems when handling |
| 1052 | // the ?, &&, and ||. |
| 1053 | |
| 1054 | case Stmt::IntegerLiteralClass: { |
| 1055 | StateTy St = Pred->getState(); |
| 1056 | IntegerLiteral* I = cast<IntegerLiteral>(S); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1057 | NonLVal X = NonLVal::MakeVal(ValMgr, I); |
| 1058 | Nodify(Dst, I, Pred, SetRVal(St, I, X)); |
Ted Kremenek | 7263910 | 2008-02-19 02:01:16 +0000 | [diff] [blame] | 1059 | break; |
| 1060 | } |
| 1061 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1062 | case Stmt::ImplicitCastExprClass: { |
| 1063 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 1064 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | case Stmt::ParenExprClass: |
| 1069 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 1070 | break; |
| 1071 | |
| 1072 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 1073 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 1074 | break; |
| 1075 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1076 | case Stmt::StmtExprClass: { |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1077 | StmtExpr* SE = cast<StmtExpr>(S); |
| 1078 | |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1079 | StateTy St = Pred->getState(); |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 1080 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1081 | Nodify(Dst, SE, Pred, SetRVal(St, SE, GetRVal(St, LastExpr))); |
Ted Kremenek | da9bd09 | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1085 | // FIXME: We may wish to always bind state to ReturnStmts so |
| 1086 | // that users can quickly query what was the state at the |
| 1087 | // exit points of a function. |
| 1088 | |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1089 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1090 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 1091 | Visit(R, Pred, Dst); |
| 1092 | else |
| 1093 | Dst.Add(Pred); |
| 1094 | |
| 1095 | break; |
Ted Kremenek | d9435bf | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 1096 | } |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 1097 | |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1098 | case Stmt::UnaryOperatorClass: { |
| 1099 | UnaryOperator* U = cast<UnaryOperator>(S); |
| 1100 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1101 | switch (U->getOpcode()) { |
| 1102 | case UnaryOperator::Deref: VisitDeref(U, Pred, Dst); break; |
| 1103 | case UnaryOperator::Plus: Visit(U->getSubExpr(), Pred, Dst); break; |
| 1104 | case UnaryOperator::SizeOf: VisitSizeOfExpr(U, Pred, Dst); break; |
| 1105 | default: VisitUnaryOperator(U, Pred, Dst); break; |
| 1106 | } |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1107 | |
Ted Kremenek | 9de04c4 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 1108 | break; |
Ted Kremenek | d8e9f0d | 2008-02-20 04:02:35 +0000 | [diff] [blame] | 1109 | } |
Ted Kremenek | 79649df | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 1110 | } |
Ted Kremenek | 1ccd31c | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1113 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1114 | // "Assume" logic. |
| 1115 | //===----------------------------------------------------------------------===// |
| 1116 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1117 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LVal Cond, |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1118 | bool Assumption, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1119 | bool& isFeasible) { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1120 | switch (Cond.getSubKind()) { |
| 1121 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1122 | assert (false && "'Assume' not implemented for this LVal."); |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1123 | return St; |
| 1124 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1125 | case lval::SymbolValKind: |
| 1126 | if (Assumption) |
| 1127 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1128 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1129 | else |
| 1130 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 1131 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 1132 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1133 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1134 | case lval::DeclValKind: |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1135 | isFeasible = Assumption; |
| 1136 | return St; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1137 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1138 | case lval::ConcreteIntKind: { |
| 1139 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 1140 | isFeasible = b ? Assumption : !Assumption; |
| 1141 | return St; |
| 1142 | } |
| 1143 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1146 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLVal Cond, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1147 | bool Assumption, |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1148 | bool& isFeasible) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1149 | switch (Cond.getSubKind()) { |
| 1150 | default: |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1151 | assert (false && "'Assume' not implemented for this NonLVal."); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1152 | return St; |
| 1153 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1154 | |
| 1155 | case nonlval::SymbolValKind: { |
Ted Kremenek | 230aaab | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 1156 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 1157 | SymbolID sym = SV.getSymbol(); |
| 1158 | |
| 1159 | if (Assumption) |
| 1160 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1161 | isFeasible); |
| 1162 | else |
| 1163 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 1164 | isFeasible); |
| 1165 | } |
| 1166 | |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1167 | case nonlval::SymIntConstraintValKind: |
| 1168 | return |
| 1169 | AssumeSymInt(St, Assumption, |
| 1170 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 1171 | isFeasible); |
| 1172 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 1173 | case nonlval::ConcreteIntKind: { |
| 1174 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1175 | isFeasible = b ? Assumption : !Assumption; |
| 1176 | return St; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1181 | GRExprEngine::StateTy |
| 1182 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1183 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 1184 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1185 | // First, determine if sym == X, where X != V. |
| 1186 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1187 | isFeasible = *X != V; |
| 1188 | return St; |
| 1189 | } |
| 1190 | |
| 1191 | // Second, determine if sym != V. |
| 1192 | if (St.isNotEqual(sym, V)) { |
| 1193 | isFeasible = true; |
| 1194 | return St; |
| 1195 | } |
| 1196 | |
| 1197 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 1198 | // Make that assumption. |
| 1199 | |
| 1200 | isFeasible = true; |
| 1201 | return StateMgr.AddNE(St, sym, V); |
| 1202 | } |
| 1203 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1204 | GRExprEngine::StateTy |
| 1205 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1206 | const llvm::APSInt& V, bool& isFeasible) { |
| 1207 | |
| 1208 | // First, determine if sym == X, where X != V. |
| 1209 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1210 | isFeasible = *X == V; |
| 1211 | return St; |
| 1212 | } |
| 1213 | |
| 1214 | // Second, determine if sym != V. |
| 1215 | if (St.isNotEqual(sym, V)) { |
| 1216 | isFeasible = false; |
| 1217 | return St; |
| 1218 | } |
| 1219 | |
| 1220 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1221 | // Make that assumption. |
| 1222 | |
| 1223 | isFeasible = true; |
| 1224 | return StateMgr.AddEQ(St, sym, V); |
| 1225 | } |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1226 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1227 | GRExprEngine::StateTy |
| 1228 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 08b6625 | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1229 | const SymIntConstraint& C, bool& isFeasible) { |
| 1230 | |
| 1231 | switch (C.getOpcode()) { |
| 1232 | default: |
| 1233 | // No logic yet for other operators. |
| 1234 | return St; |
| 1235 | |
| 1236 | case BinaryOperator::EQ: |
| 1237 | if (Assumption) |
| 1238 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1239 | else |
| 1240 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1241 | |
| 1242 | case BinaryOperator::NE: |
| 1243 | if (Assumption) |
| 1244 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1245 | else |
| 1246 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1247 | } |
| 1248 | } |
| 1249 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1250 | //===----------------------------------------------------------------------===// |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1251 | // Visualization. |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1252 | //===----------------------------------------------------------------------===// |
| 1253 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1254 | #ifndef NDEBUG |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1255 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1256 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1257 | namespace llvm { |
| 1258 | template<> |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1259 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1260 | public DefaultDOTGraphTraits { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1261 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1262 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1263 | |
| 1264 | Out << "Variables:\\l"; |
| 1265 | |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1266 | bool isFirst = true; |
| 1267 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1268 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1269 | E=St.vb_end(); I!=E;++I) { |
| 1270 | |
| 1271 | if (isFirst) |
| 1272 | isFirst = false; |
| 1273 | else |
| 1274 | Out << "\\l"; |
| 1275 | |
| 1276 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1277 | I.getData().print(Out); |
| 1278 | } |
| 1279 | |
| 1280 | } |
| 1281 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1282 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1283 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1284 | |
| 1285 | bool isFirst = true; |
| 1286 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1287 | for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1288 | I != E;++I) { |
| 1289 | |
| 1290 | if (isFirst) { |
| 1291 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1292 | isFirst = false; |
| 1293 | } |
| 1294 | else |
| 1295 | Out << "\\l"; |
| 1296 | |
| 1297 | Out << " (" << (void*) I.getKey() << ") "; |
| 1298 | I.getKey()->printPretty(Out); |
| 1299 | Out << " : "; |
| 1300 | I.getData().print(Out); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Ted Kremenek | 44842c2 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1304 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1305 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1306 | bool isFirst = true; |
| 1307 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1308 | for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1309 | I != E; ++I) { |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1310 | if (isFirst) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1311 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1312 | isFirst = false; |
| 1313 | } |
| 1314 | else |
| 1315 | Out << "\\l"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1316 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1317 | Out << " (" << (void*) I.getKey() << ") "; |
| 1318 | I.getKey()->printPretty(Out); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1319 | Out << " : "; |
| 1320 | I.getData().print(Out); |
| 1321 | } |
| 1322 | } |
| 1323 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1324 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1325 | ValueState::ConstEqTy CE = St.getImpl()->ConstEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1326 | |
| 1327 | if (CE.isEmpty()) |
| 1328 | return; |
| 1329 | |
| 1330 | Out << "\\l\\|'==' constraints:"; |
| 1331 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1332 | for (ValueState::ConstEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1333 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1334 | } |
| 1335 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1336 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1337 | ValueState::ConstNotEqTy NE = St.getImpl()->ConstNotEq; |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1338 | |
| 1339 | if (NE.isEmpty()) |
| 1340 | return; |
| 1341 | |
| 1342 | Out << "\\l\\|'!=' constraints:"; |
| 1343 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1344 | for (ValueState::ConstNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1345 | I != EI; ++I){ |
| 1346 | |
| 1347 | Out << "\\l $" << I.getKey() << " : "; |
| 1348 | bool isFirst = true; |
| 1349 | |
| 1350 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1351 | EJ=I.getData().end(); |
| 1352 | for ( ; J != EJ; ++J) { |
| 1353 | if (isFirst) isFirst = false; |
| 1354 | else Out << ", "; |
| 1355 | |
| 1356 | Out << (*J)->toString(); |
| 1357 | } |
| 1358 | } |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1362 | |
| 1363 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1364 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1365 | GraphPrintCheckerState->isUninitDeref(N) || |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1366 | GraphPrintCheckerState->isUninitStore(N) || |
| 1367 | GraphPrintCheckerState->isUninitControlFlow(N)) |
Ted Kremenek | a3fadfc | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1368 | return "color=\"red\",style=\"filled\""; |
| 1369 | |
| 1370 | return ""; |
| 1371 | } |
Ted Kremenek | ed4de31 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1372 | |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1373 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1374 | std::ostringstream Out; |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Program Location. |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1377 | ProgramPoint Loc = N->getLocation(); |
| 1378 | |
| 1379 | switch (Loc.getKind()) { |
| 1380 | case ProgramPoint::BlockEntranceKind: |
| 1381 | Out << "Block Entrance: B" |
| 1382 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1383 | break; |
| 1384 | |
| 1385 | case ProgramPoint::BlockExitKind: |
| 1386 | assert (false); |
| 1387 | break; |
| 1388 | |
| 1389 | case ProgramPoint::PostStmtKind: { |
| 1390 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 9ff731d | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1391 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1392 | << (void*) L.getStmt() << ' '; |
| 1393 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1394 | L.getStmt()->printPretty(Out); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1395 | |
| 1396 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1397 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1398 | } |
Ted Kremenek | 63a4f69 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1399 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1400 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1401 | } |
Ted Kremenek | b533912 | 2008-02-19 20:53:06 +0000 | [diff] [blame] | 1402 | else if (GraphPrintCheckerState->isUninitDeref(N)) { |
| 1403 | Out << "\\|Dereference of uninitialied value.\\l"; |
| 1404 | } |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1405 | else if (GraphPrintCheckerState->isUninitStore(N)) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 1406 | Out << "\\|Store to Uninitialized LVal."; |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1407 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1408 | |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | } |
| 1411 | |
| 1412 | default: { |
| 1413 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1414 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1415 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1418 | Out << "\\|Terminator: "; |
| 1419 | E.getSrc()->printTerminator(Out); |
| 1420 | |
Ted Kremenek | daeb9a7 | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1421 | if (isa<SwitchStmt>(T)) { |
| 1422 | Stmt* Label = E.getDst()->getLabel(); |
| 1423 | |
| 1424 | if (Label) { |
| 1425 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1426 | Out << "\\lcase "; |
| 1427 | C->getLHS()->printPretty(Out); |
| 1428 | |
| 1429 | if (Stmt* RHS = C->getRHS()) { |
| 1430 | Out << " .. "; |
| 1431 | RHS->printPretty(Out); |
| 1432 | } |
| 1433 | |
| 1434 | Out << ":"; |
| 1435 | } |
| 1436 | else { |
| 1437 | assert (isa<DefaultStmt>(Label)); |
| 1438 | Out << "\\ldefault:"; |
| 1439 | } |
| 1440 | } |
| 1441 | else |
| 1442 | Out << "\\l(implicit) default:"; |
| 1443 | } |
| 1444 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1445 | // FIXME |
| 1446 | } |
| 1447 | else { |
| 1448 | Out << "\\lCondition: "; |
| 1449 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1450 | Out << "true"; |
| 1451 | else |
| 1452 | Out << "false"; |
| 1453 | } |
| 1454 | |
| 1455 | Out << "\\l"; |
| 1456 | } |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1459 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1460 | } |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1464 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1465 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1466 | N->getState().printDOT(Out); |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1467 | |
Ted Kremenek | 803c9ed | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1468 | Out << "\\l"; |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1469 | return Out.str(); |
| 1470 | } |
| 1471 | }; |
| 1472 | } // end llvm namespace |
| 1473 | #endif |
| 1474 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1475 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | aa66a32 | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1476 | #ifndef NDEBUG |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1477 | GraphPrintCheckerState = this; |
| 1478 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1479 | GraphPrintCheckerState = NULL; |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1480 | #endif |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1481 | } |