Ted Kremenek | 50df4f4 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 1 | //=-- GRExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-= |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 2e16060 | 2008-01-31 06:49:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Ted Kremenek | 68d70a8 | 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 | 50df4f4 | 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 | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 50df4f4 | 2008-02-14 22:13:12 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 3862eb1 | 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 | d446743 | 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 | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 26 | GRExprEngine::StateTy |
| 27 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue& V) { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 29 | if (!StateCleaned) { |
| 30 | St = RemoveDeadBindings(CurrentStmt, St); |
| 31 | StateCleaned = true; |
| 32 | } |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 34 | bool isBlkExpr = false; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 35 | |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 36 | if (S == CurrentStmt) { |
| 37 | isBlkExpr = getCFG().isBlkExpr(S); |
| 38 | |
| 39 | if (!isBlkExpr) |
| 40 | return St; |
| 41 | } |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 43 | return StateMgr.SetValue(St, S, isBlkExpr, V); |
| 44 | } |
| 45 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 46 | const GRExprEngine::StateTy::BufferTy& |
| 47 | GRExprEngine::SetValue(StateTy St, Expr* S, const RValue::BufferTy& RB, |
Ted Kremenek | f20c201 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 48 | StateTy::BufferTy& RetBuf) { |
| 49 | |
| 50 | assert (RetBuf.empty()); |
| 51 | |
| 52 | for (RValue::BufferTy::const_iterator I=RB.begin(), E=RB.end(); I!=E; ++I) |
| 53 | RetBuf.push_back(SetValue(St, S, *I)); |
| 54 | |
| 55 | return RetBuf; |
| 56 | } |
| 57 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 58 | GRExprEngine::StateTy |
| 59 | GRExprEngine::SetValue(StateTy St, const LValue& LV, const RValue& V) { |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 0428e02 | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 61 | if (LV.isUnknown()) |
Ted Kremenek | 7f5ebc7 | 2008-02-04 21:59:01 +0000 | [diff] [blame] | 62 | return St; |
| 63 | |
| 64 | if (!StateCleaned) { |
| 65 | St = RemoveDeadBindings(CurrentStmt, St); |
| 66 | StateCleaned = true; |
| 67 | } |
| 68 | |
| 69 | return StateMgr.SetValue(St, LV, V); |
| 70 | } |
| 71 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 72 | void GRExprEngine::ProcessBranch(Expr* Condition, Stmt* Term, |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 73 | BranchNodeBuilder& builder) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 75 | // Remove old bindings for subexpressions. |
| 76 | StateTy PrevState = StateMgr.RemoveSubExprBindings(builder.getState()); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 78 | // Check for NULL conditions; e.g. "for(;;)" |
| 79 | if (!Condition) { |
| 80 | builder.markInfeasible(false); |
| 81 | |
| 82 | // Get the current block counter. |
| 83 | GRBlockCounter BC = builder.getBlockCounter(); |
| 84 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 85 | unsigned NumVisited = BC.getNumVisited(BlockID); |
| 86 | |
| 87 | if (NumVisited < 1) builder.generateNode(PrevState, true); |
| 88 | else builder.markInfeasible(true); |
| 89 | |
| 90 | return; |
| 91 | } |
| 92 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 93 | RValue V = GetValue(PrevState, Condition); |
| 94 | |
| 95 | switch (V.getBaseKind()) { |
| 96 | default: |
| 97 | break; |
| 98 | |
Ted Kremenek | 0428e02 | 2008-02-08 03:02:48 +0000 | [diff] [blame] | 99 | case RValue::UnknownKind: |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 100 | builder.generateNode(PrevState, true); |
| 101 | builder.generateNode(PrevState, false); |
| 102 | return; |
| 103 | |
| 104 | case RValue::UninitializedKind: { |
| 105 | NodeTy* N = builder.generateNode(PrevState, true); |
| 106 | |
| 107 | if (N) { |
| 108 | N->markAsSink(); |
| 109 | UninitBranches.insert(N); |
| 110 | } |
| 111 | |
| 112 | builder.markInfeasible(false); |
| 113 | return; |
| 114 | } |
| 115 | } |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 117 | // Get the current block counter. |
| 118 | GRBlockCounter BC = builder.getBlockCounter(); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 119 | unsigned BlockID = builder.getTargetBlock(true)->getBlockID(); |
| 120 | unsigned NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 122 | if (isa<nonlval::ConcreteInt>(V) || |
| 123 | BC.getNumVisited(builder.getTargetBlock(true)->getBlockID()) < 1) { |
| 124 | |
| 125 | // Process the true branch. |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 127 | bool isFeasible = true; |
| 128 | |
| 129 | StateTy St = Assume(PrevState, V, true, isFeasible); |
| 130 | |
| 131 | if (isFeasible) |
| 132 | builder.generateNode(St, true); |
| 133 | else |
| 134 | builder.markInfeasible(true); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 135 | } |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 136 | else |
| 137 | builder.markInfeasible(true); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 139 | BlockID = builder.getTargetBlock(false)->getBlockID(); |
| 140 | NumVisited = BC.getNumVisited(BlockID); |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 4b170e5 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 142 | if (isa<nonlval::ConcreteInt>(V) || |
| 143 | BC.getNumVisited(builder.getTargetBlock(false)->getBlockID()) < 1) { |
| 144 | |
| 145 | // Process the false branch. |
| 146 | |
| 147 | bool isFeasible = false; |
| 148 | |
| 149 | StateTy St = Assume(PrevState, V, false, isFeasible); |
| 150 | |
| 151 | if (isFeasible) |
| 152 | builder.generateNode(St, false); |
| 153 | else |
| 154 | builder.markInfeasible(false); |
| 155 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 156 | else |
| 157 | builder.markInfeasible(false); |
Ted Kremenek | 6ff3cea | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 160 | /// ProcessIndirectGoto - Called by GRCoreEngine. Used to generate successor |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 161 | /// nodes by processing the 'effects' of a computed goto jump. |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 162 | void GRExprEngine::ProcessIndirectGoto(IndirectGotoNodeBuilder& builder) { |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 163 | |
| 164 | StateTy St = builder.getState(); |
| 165 | LValue V = cast<LValue>(GetValue(St, builder.getTarget())); |
| 166 | |
| 167 | // Three possibilities: |
| 168 | // |
| 169 | // (1) We know the computed label. |
| 170 | // (2) The label is NULL (or some other constant), or Uninitialized. |
| 171 | // (3) We have no clue about the label. Dispatch to all targets. |
| 172 | // |
| 173 | |
| 174 | typedef IndirectGotoNodeBuilder::iterator iterator; |
| 175 | |
| 176 | if (isa<lval::GotoLabel>(V)) { |
| 177 | LabelStmt* L = cast<lval::GotoLabel>(V).getLabel(); |
| 178 | |
| 179 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) { |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 180 | if (I.getLabel() == L) { |
| 181 | builder.generateNode(I, St); |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | assert (false && "No block with label."); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (isa<lval::ConcreteInt>(V) || isa<UninitializedVal>(V)) { |
| 191 | // Dispatch to the first target and mark it as a sink. |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 192 | NodeTy* N = builder.generateNode(builder.begin(), St, true); |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 193 | UninitBranches.insert(N); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | // This is really a catch-all. We don't support symbolics yet. |
| 198 | |
| 199 | assert (isa<UnknownVal>(V)); |
| 200 | |
| 201 | for (iterator I=builder.begin(), E=builder.end(); I != E; ++I) |
Ted Kremenek | 79f63f5 | 2008-02-13 17:27:37 +0000 | [diff] [blame] | 202 | builder.generateNode(I, St); |
Ted Kremenek | 677f4ef | 2008-02-13 00:24:44 +0000 | [diff] [blame] | 203 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 205 | /// ProcessSwitch - Called by GRCoreEngine. Used to generate successor |
| 206 | /// nodes by processing the 'effects' of a switch statement. |
| 207 | void GRExprEngine::ProcessSwitch(SwitchNodeBuilder& builder) { |
| 208 | |
| 209 | typedef SwitchNodeBuilder::iterator iterator; |
| 210 | |
| 211 | StateTy St = builder.getState(); |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 212 | Expr* CondE = builder.getCondition(); |
| 213 | NonLValue CondV = cast<NonLValue>(GetValue(St, CondE)); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 214 | |
| 215 | if (isa<UninitializedVal>(CondV)) { |
| 216 | NodeTy* N = builder.generateDefaultCaseNode(St, true); |
| 217 | UninitBranches.insert(N); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | StateTy DefaultSt = St; |
| 222 | |
| 223 | // While most of this can be assumed (such as the signedness), having it |
| 224 | // just computed makes sure everything makes the same assumptions end-to-end. |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 225 | |
| 226 | unsigned bits = getContext().getTypeSize(CondE->getType(), |
| 227 | CondE->getExprLoc()); |
| 228 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 229 | APSInt V1(bits, false); |
| 230 | APSInt V2 = V1; |
| 231 | |
| 232 | for (iterator I=builder.begin(), E=builder.end(); I!=E; ++I) { |
| 233 | |
| 234 | CaseStmt* Case = cast<CaseStmt>(I.getCase()); |
| 235 | |
| 236 | // Evaluate the case. |
| 237 | if (!Case->getLHS()->isIntegerConstantExpr(V1, getContext(), 0, true)) { |
| 238 | assert (false && "Case condition must evaluate to an integer constant."); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | // Get the RHS of the case, if it exists. |
| 243 | |
| 244 | if (Expr* E = Case->getRHS()) { |
| 245 | if (!E->isIntegerConstantExpr(V2, getContext(), 0, true)) { |
| 246 | assert (false && |
| 247 | "Case condition (RHS) must evaluate to an integer constant."); |
| 248 | return ; |
| 249 | } |
| 250 | |
| 251 | assert (V1 <= V2); |
| 252 | } |
| 253 | else V2 = V1; |
| 254 | |
| 255 | // FIXME: Eventually we should replace the logic below with a range |
| 256 | // comparison, rather than concretize the values within the range. |
| 257 | // This should be easy once we have "ranges" for NonLValues. |
| 258 | |
| 259 | do { |
| 260 | nonlval::ConcreteInt CaseVal(ValMgr.getValue(V1)); |
| 261 | |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 262 | NonLValue Res = EvalBinaryOp(ValMgr, BinaryOperator::EQ, CondV, CaseVal); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 263 | |
| 264 | // Now "assume" that the case matches. |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 265 | bool isFeasible = false; |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 267 | StateTy StNew = Assume(St, Res, true, isFeasible); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 268 | |
| 269 | if (isFeasible) { |
| 270 | builder.generateCaseStmtNode(I, StNew); |
| 271 | |
| 272 | // If CondV evaluates to a constant, then we know that this |
| 273 | // is the *only* case that we can take, so stop evaluating the |
| 274 | // others. |
| 275 | if (isa<nonlval::ConcreteInt>(CondV)) |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // Now "assume" that the case doesn't match. Add this state |
| 280 | // to the default state (if it is feasible). |
| 281 | |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 282 | StNew = Assume(DefaultSt, Res, false, isFeasible); |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 283 | |
| 284 | if (isFeasible) |
| 285 | DefaultSt = StNew; |
| 286 | |
| 287 | // Concretize the next value in the range. |
| 288 | ++V1; |
| 289 | |
| 290 | } while (V1 < V2); |
| 291 | } |
| 292 | |
| 293 | // If we reach here, than we know that the default branch is |
| 294 | // possible. |
| 295 | builder.generateDefaultCaseNode(DefaultSt); |
| 296 | } |
| 297 | |
| 298 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 299 | void GRExprEngine::VisitLogicalExpr(BinaryOperator* B, NodeTy* Pred, |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 300 | NodeSet& Dst) { |
| 301 | |
| 302 | bool hasR2; |
| 303 | StateTy PrevState = Pred->getState(); |
| 304 | |
| 305 | RValue R1 = GetValue(PrevState, B->getLHS()); |
| 306 | RValue R2 = GetValue(PrevState, B->getRHS(), hasR2); |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 307 | |
| 308 | if (hasR2) { |
| 309 | if (isa<UninitializedVal>(R2) || isa<UnknownVal>(R2)) { |
| 310 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R2)); |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | else if (isa<UninitializedVal>(R1) || isa<UnknownVal>(R1)) { |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 315 | Nodify(Dst, B, Pred, SetValue(PrevState, B, R1)); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | // R1 is an expression that can evaluate to either 'true' or 'false'. |
| 320 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 321 | // hasR2 == 'false' means that LHS evaluated to 'false' and that |
| 322 | // we short-circuited, leading to a value of '0' for the '&&' expression. |
| 323 | if (hasR2 == false) { |
| 324 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | else { |
| 329 | assert (B->getOpcode() == BinaryOperator::LOr); |
| 330 | // hasR2 == 'false' means that the LHS evaluate to 'true' and that |
| 331 | // we short-circuited, leading to a value of '1' for the '||' expression. |
| 332 | if (hasR2 == false) { |
| 333 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 334 | return; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // If we reach here we did not short-circuit. Assume R2 == true and |
| 339 | // R2 == false. |
| 340 | |
| 341 | bool isFeasible; |
| 342 | StateTy St = Assume(PrevState, R2, true, isFeasible); |
| 343 | |
| 344 | if (isFeasible) |
| 345 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(1U, B))); |
| 346 | |
| 347 | St = Assume(PrevState, R2, false, isFeasible); |
| 348 | |
| 349 | if (isFeasible) |
| 350 | Nodify(Dst, B, Pred, SetValue(PrevState, B, GetRValueConstant(0U, B))); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 355 | void GRExprEngine::ProcessStmt(Stmt* S, StmtNodeBuilder& builder) { |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 356 | Builder = &builder; |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 357 | |
| 358 | StmtEntryNode = builder.getLastNode(); |
| 359 | CurrentStmt = S; |
| 360 | NodeSet Dst; |
| 361 | StateCleaned = false; |
| 362 | |
| 363 | Visit(S, StmtEntryNode, Dst); |
| 364 | |
| 365 | // If no nodes were generated, generate a new node that has all the |
| 366 | // dead mappings removed. |
| 367 | if (Dst.size() == 1 && *Dst.begin() == StmtEntryNode) { |
| 368 | StateTy St = RemoveDeadBindings(S, StmtEntryNode->getState()); |
| 369 | builder.generateNode(S, St, StmtEntryNode); |
| 370 | } |
Ted Kremenek | a57214f | 2008-01-18 00:41:32 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 372 | CurrentStmt = NULL; |
| 373 | StmtEntryNode = NULL; |
| 374 | Builder = NULL; |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Ted Kremenek | 30fa28b | 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 | f031b87 | 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 | 02b5b40 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 381 | if (St == Pred->getState()) |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 382 | return NULL; |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 384 | NodeTy* N = Builder->generateNode(S, St, Pred); |
| 385 | Dst.Add(N); |
| 386 | return N; |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 387 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 389 | void GRExprEngine::Nodify(NodeSet& Dst, Stmt* S, NodeTy* Pred, |
Ted Kremenek | f20c201 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 390 | const StateTy::BufferTy& SB) { |
| 391 | |
| 392 | for (StateTy::BufferTy::const_iterator I=SB.begin(), E=SB.end(); I!=E; ++I) |
| 393 | Nodify(Dst, S, Pred, *I); |
| 394 | } |
| 395 | |
Ted Kremenek | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 396 | void GRExprEngine::VisitDeclRefExpr(DeclRefExpr* D, NodeTy* Pred, NodeSet& Dst){ |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 397 | if (D != CurrentStmt) { |
| 398 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | // If we are here, we are loading the value of the decl and binding |
| 403 | // it to the block-level expression. |
| 404 | |
| 405 | StateTy St = Pred->getState(); |
| 406 | |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 407 | Nodify(Dst, D, Pred, SetValue(St, D, GetValue(St, D))); |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 410 | void GRExprEngine::VisitCast(Expr* CastE, Expr* E, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 411 | |
| 412 | QualType T = CastE->getType(); |
| 413 | |
| 414 | // Check for redundant casts. |
| 415 | if (E->getType() == T) { |
| 416 | Dst.Add(Pred); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | NodeSet S1; |
| 421 | Visit(E, Pred, S1); |
| 422 | |
| 423 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 424 | NodeTy* N = *I1; |
| 425 | StateTy St = N->getState(); |
Ted Kremenek | 0922ec8 | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 426 | const RValue& V = GetValue(St, E); |
Ted Kremenek | 25a484d | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 427 | Nodify(Dst, CastE, N, SetValue(St, CastE, EvalCast(ValMgr, V, CastE))); |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 428 | } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 431 | void GRExprEngine::VisitDeclStmt(DeclStmt* DS, GRExprEngine::NodeTy* Pred, |
| 432 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 433 | |
| 434 | StateTy St = Pred->getState(); |
| 435 | |
| 436 | for (const ScopedDecl* D = DS->getDecl(); D; D = D->getNextDeclarator()) |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 437 | if (const VarDecl* VD = dyn_cast<VarDecl>(D)) { |
Ted Kremenek | e025800 | 2008-02-19 00:29:51 +0000 | [diff] [blame] | 438 | |
| 439 | // FIXME: Add support for local arrays. |
| 440 | if (VD->getType()->isArrayType()) |
| 441 | continue; |
| 442 | |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 443 | const Expr* E = VD->getInit(); |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 444 | St = SetValue(St, lval::DeclVal(VD), |
Ted Kremenek | adec14b | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 445 | E ? GetValue(St, E) : UninitializedVal()); |
Ted Kremenek | 8c9bc34 | 2008-01-28 22:51:57 +0000 | [diff] [blame] | 446 | } |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 447 | |
| 448 | Nodify(Dst, DS, Pred, St); |
| 449 | |
| 450 | if (Dst.empty()) |
| 451 | Dst.Add(Pred); |
| 452 | } |
Ted Kremenek | 54eddae | 2008-01-24 02:02:54 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 455 | void GRExprEngine::VisitGuardedExpr(Expr* S, Expr* LHS, Expr* RHS, |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 456 | NodeTy* Pred, NodeSet& Dst) { |
| 457 | |
| 458 | StateTy St = Pred->getState(); |
| 459 | |
| 460 | RValue R = GetValue(St, LHS); |
Ted Kremenek | adec14b | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 461 | if (isa<UnknownVal>(R)) R = GetValue(St, RHS); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 462 | |
| 463 | Nodify(Dst, S, Pred, SetValue(St, S, R)); |
| 464 | } |
| 465 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 466 | /// VisitSizeOfAlignOfTypeExpr - Transfer function for sizeof(type). |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 467 | void GRExprEngine::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr* S, |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 468 | NodeTy* Pred, |
| 469 | NodeSet& Dst) { |
| 470 | |
| 471 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 472 | |
| 473 | QualType T = S->getArgumentType(); |
| 474 | |
| 475 | // FIXME: Add support for VLAs. |
Eli Friedman | 82ce9a8 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 476 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 477 | return; |
| 478 | |
| 479 | SourceLocation L = S->getExprLoc(); |
| 480 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 481 | |
| 482 | Nodify(Dst, S, Pred, |
| 483 | SetValue(Pred->getState(), S, |
Ted Kremenek | 521d972 | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 484 | NonLValue::GetValue(ValMgr, size, S->getType(), L))); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 485 | |
| 486 | } |
| 487 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 488 | void GRExprEngine::VisitUnaryOperator(UnaryOperator* U, |
| 489 | GRExprEngine::NodeTy* Pred, |
| 490 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 492 | NodeSet S1; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 493 | UnaryOperator::Opcode Op = U->getOpcode(); |
| 494 | |
| 495 | // FIXME: This is a hack so that for '*' and '&' we don't recurse |
| 496 | // on visiting the subexpression if it is a DeclRefExpr. We should |
| 497 | // probably just handle AddrOf and Deref in their own methods to make |
| 498 | // this cleaner. |
| 499 | if ((Op == UnaryOperator::Deref || Op == UnaryOperator::AddrOf) && |
| 500 | isa<DeclRefExpr>(U->getSubExpr())) |
| 501 | S1.Add(Pred); |
| 502 | else |
| 503 | Visit(U->getSubExpr(), Pred, S1); |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 504 | |
| 505 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 506 | NodeTy* N1 = *I1; |
| 507 | StateTy St = N1->getState(); |
| 508 | |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 509 | // Handle ++ and -- (both pre- and post-increment). |
| 510 | |
| 511 | if (U->isIncrementDecrementOp()) { |
| 512 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 513 | RValue R1 = GetValue(St, L1); |
| 514 | |
| 515 | BinaryOperator::Opcode Op = U->isIncrementOp() ? BinaryOperator::Add |
| 516 | : BinaryOperator::Sub; |
| 517 | |
| 518 | RValue Result = EvalBinaryOp(ValMgr, Op, R1, GetRValueConstant(1U, U)); |
| 519 | |
| 520 | if (U->isPostfix()) |
| 521 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, R1), L1, Result)); |
| 522 | else |
| 523 | Nodify(Dst, U, N1, SetValue(SetValue(St, U, Result), L1, Result)); |
| 524 | |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | // Handle all other unary operators. |
| 529 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 530 | switch (U->getOpcode()) { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | cacd736 | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 532 | case UnaryOperator::Minus: { |
Ted Kremenek | 0922ec8 | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 533 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c9922fd | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 534 | Nodify(Dst, U, N1, SetValue(St, U, EvalMinus(ValMgr, U, R1))); |
Ted Kremenek | cacd736 | 2008-01-24 08:20:02 +0000 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | |
Ted Kremenek | 2cb4664 | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 538 | case UnaryOperator::Not: { |
| 539 | const NonLValue& R1 = cast<NonLValue>(GetValue(St, U->getSubExpr())); |
Ted Kremenek | c9922fd | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 540 | Nodify(Dst, U, N1, SetValue(St, U, EvalComplement(ValMgr, R1))); |
Ted Kremenek | 2cb4664 | 2008-02-04 16:58:30 +0000 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 544 | case UnaryOperator::LNot: { |
| 545 | // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." |
| 546 | // |
| 547 | // Note: technically we do "E == 0", but this is the same in the |
| 548 | // transfer functions as "0 == E". |
| 549 | |
| 550 | RValue V1 = GetValue(St, U->getSubExpr()); |
| 551 | |
| 552 | if (isa<LValue>(V1)) { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 553 | const LValue& L1 = cast<LValue>(V1); |
| 554 | lval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
| 555 | Nodify(Dst, U, N1, |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 556 | SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 557 | L1, V2))); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 558 | } |
| 559 | else { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 560 | const NonLValue& R1 = cast<NonLValue>(V1); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 561 | nonlval::ConcreteInt V2(ValMgr.getZeroWithPtrWidth()); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 562 | Nodify(Dst, U, N1, |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 563 | SetValue(St, U, EvalBinaryOp(ValMgr, BinaryOperator::EQ, |
| 564 | R1, V2))); |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | break; |
| 568 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 569 | |
| 570 | case UnaryOperator::SizeOf: { |
| 571 | // 6.5.3.4 sizeof: "The result type is an integer." |
| 572 | |
| 573 | QualType T = U->getSubExpr()->getType(); |
| 574 | |
| 575 | // FIXME: Add support for VLAs. |
Eli Friedman | 82ce9a8 | 2008-02-15 12:28:27 +0000 | [diff] [blame] | 576 | if (!T.getTypePtr()->isConstantSizeType()) |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 577 | return; |
| 578 | |
| 579 | SourceLocation L = U->getExprLoc(); |
| 580 | uint64_t size = getContext().getTypeSize(T, L) / 8; |
| 581 | |
| 582 | Nodify(Dst, U, N1, |
| 583 | SetValue(St, U, NonLValue::GetValue(ValMgr, size, |
Ted Kremenek | 521d972 | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 584 | U->getType(), L))); |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 585 | |
| 586 | break; |
| 587 | } |
Ted Kremenek | a6683bf | 2008-02-06 17:56:00 +0000 | [diff] [blame] | 588 | |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 589 | case UnaryOperator::AddrOf: { |
| 590 | const LValue& L1 = GetLValue(St, U->getSubExpr()); |
| 591 | Nodify(Dst, U, N1, SetValue(St, U, L1)); |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | case UnaryOperator::Deref: { |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 596 | // FIXME: Stop when dereferencing an uninitialized value. |
| 597 | // FIXME: Bifurcate when dereferencing a symbolic with no constraints? |
| 598 | |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 599 | const RValue& V = GetValue(St, U->getSubExpr()); |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 600 | const LValue& L1 = cast<LValue>(V); |
| 601 | |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 602 | // After a dereference, one of two possible situations arise: |
| 603 | // (1) A crash, because the pointer was NULL. |
| 604 | // (2) The pointer is not NULL, and the dereference works. |
| 605 | // |
| 606 | // We add these assumptions. |
| 607 | |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 608 | bool isFeasibleNotNull; |
| 609 | |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 610 | // "Assume" that the pointer is Not-NULL. |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 611 | StateTy StNotNull = Assume(St, L1, true, isFeasibleNotNull); |
| 612 | |
| 613 | if (isFeasibleNotNull) { |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 614 | QualType T = U->getType(); |
| 615 | Nodify(Dst, U, N1, SetValue(StNotNull, U, |
| 616 | GetValue(StNotNull, L1, &T))); |
| 617 | } |
| 618 | |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 619 | if (V.isUnknown()) |
| 620 | return; |
| 621 | |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 622 | bool isFeasibleNull; |
| 623 | |
| 624 | // "Assume" that the pointer is NULL. |
| 625 | StateTy StNull = Assume(St, L1, false, isFeasibleNull); |
| 626 | |
| 627 | if (isFeasibleNull) { |
Ted Kremenek | 02b5b40 | 2008-02-07 15:20:13 +0000 | [diff] [blame] | 628 | // We don't use "Nodify" here because the node will be a sink |
| 629 | // and we have no intention of processing it later. |
| 630 | NodeTy* NullNode = Builder->generateNode(U, StNull, N1); |
| 631 | |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 632 | if (NullNode) { |
| 633 | NullNode->markAsSink(); |
| 634 | |
| 635 | if (isFeasibleNotNull) |
| 636 | ImplicitNullDeref.insert(NullNode); |
| 637 | else |
| 638 | ExplicitNullDeref.insert(NullNode); |
| 639 | } |
| 640 | } |
| 641 | |
Ted Kremenek | c48b8e4 | 2008-01-31 02:35:41 +0000 | [diff] [blame] | 642 | break; |
| 643 | } |
| 644 | |
Ted Kremenek | 1be5eb9 | 2008-01-24 02:28:56 +0000 | [diff] [blame] | 645 | default: ; |
| 646 | assert (false && "Not implemented."); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 651 | void GRExprEngine::VisitAssignmentLHS(Expr* E, GRExprEngine::NodeTy* Pred, |
| 652 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 653 | |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 654 | if (isa<DeclRefExpr>(E)) { |
| 655 | Dst.Add(Pred); |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 656 | return; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 657 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 658 | |
| 659 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) { |
| 660 | if (U->getOpcode() == UnaryOperator::Deref) { |
| 661 | Visit(U->getSubExpr(), Pred, Dst); |
| 662 | return; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | Visit(E, Pred, Dst); |
| 667 | } |
| 668 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 669 | void GRExprEngine::VisitBinaryOperator(BinaryOperator* B, |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 670 | GRExprEngine::NodeTy* Pred, |
| 671 | GRExprEngine::NodeSet& Dst) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 672 | NodeSet S1; |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 673 | |
| 674 | if (B->isAssignmentOp()) |
| 675 | VisitAssignmentLHS(B->getLHS(), Pred, S1); |
| 676 | else |
| 677 | Visit(B->getLHS(), Pred, S1); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 678 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 679 | for (NodeSet::iterator I1=S1.begin(), E1=S1.end(); I1 != E1; ++I1) { |
| 680 | NodeTy* N1 = *I1; |
Ted Kremenek | e860db8 | 2008-01-17 00:52:48 +0000 | [diff] [blame] | 681 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 682 | // When getting the value for the LHS, check if we are in an assignment. |
| 683 | // In such cases, we want to (initially) treat the LHS as an LValue, |
| 684 | // so we use GetLValue instead of GetValue so that DeclRefExpr's are |
Ted Kremenek | 0922ec8 | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 685 | // evaluated to LValueDecl's instead of to an NonLValue. |
| 686 | const RValue& V1 = |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 687 | B->isAssignmentOp() ? GetLValue(N1->getState(), B->getLHS()) |
| 688 | : GetValue(N1->getState(), B->getLHS()); |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 689 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 690 | NodeSet S2; |
| 691 | Visit(B->getRHS(), N1, S2); |
| 692 | |
| 693 | for (NodeSet::iterator I2=S2.begin(), E2=S2.end(); I2 != E2; ++I2) { |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 694 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 695 | NodeTy* N2 = *I2; |
| 696 | StateTy St = N2->getState(); |
Ted Kremenek | 0922ec8 | 2008-01-28 22:09:13 +0000 | [diff] [blame] | 697 | const RValue& V2 = GetValue(St, B->getRHS()); |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 698 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 699 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 700 | |
| 701 | if (Op <= BinaryOperator::Or) { |
| 702 | |
Ted Kremenek | adec14b | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 703 | if (isa<UnknownVal>(V1) || isa<UninitializedVal>(V1)) { |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 704 | Nodify(Dst, B, N2, SetValue(St, B, V1)); |
| 705 | continue; |
| 706 | } |
| 707 | |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 708 | Nodify(Dst, B, N2, SetValue(St, B, EvalBinaryOp(ValMgr, Op, V1, V2))); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 709 | continue; |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 710 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | switch (Op) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 714 | case BinaryOperator::Assign: { |
| 715 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 716 | |
| 717 | if (isa<UninitializedVal>(L1)) |
| 718 | HandleUninitializedStore(B, N2); |
| 719 | else |
| 720 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, V2), L1, V2)); |
| 721 | |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 722 | break; |
| 723 | } |
| 724 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 725 | default: { // Compound assignment operators. |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 726 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 727 | assert (B->isCompoundAssignmentOp()); |
| 728 | |
| 729 | const LValue& L1 = cast<LValue>(V1); |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 730 | |
| 731 | if (isa<UninitializedVal>(L1)) { |
| 732 | HandleUninitializedStore(B, N2); |
| 733 | break; |
| 734 | } |
| 735 | |
Ted Kremenek | adec14b | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 736 | RValue Result = cast<NonLValue>(UnknownVal()); |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 737 | |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 738 | if (Op >= BinaryOperator::AndAssign) |
| 739 | ((int&) Op) -= (BinaryOperator::AndAssign - BinaryOperator::And); |
| 740 | else |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 741 | ((int&) Op) -= BinaryOperator::MulAssign; |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 742 | |
Ted Kremenek | 22640ce | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 743 | if (B->getType()->isPointerType()) { // Perform pointer arithmetic. |
| 744 | const NonLValue& R2 = cast<NonLValue>(V2); |
| 745 | Result = EvalBinaryOp(ValMgr, Op, L1, R2); |
| 746 | } |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 747 | else if (isa<LValue>(V2)) { |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 748 | const LValue& L2 = cast<LValue>(V2); |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 749 | |
| 750 | if (B->getRHS()->getType()->isPointerType()) { |
| 751 | // LValue comparison. |
| 752 | Result = EvalBinaryOp(ValMgr, Op, L1, L2); |
| 753 | } |
| 754 | else { |
Ted Kremenek | 521d972 | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 755 | QualType T1 = B->getLHS()->getType(); |
| 756 | QualType T2 = B->getRHS()->getType(); |
| 757 | |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 758 | // An operation between two variables of a non-lvalue type. |
| 759 | Result = |
| 760 | EvalBinaryOp(ValMgr, Op, |
Ted Kremenek | 521d972 | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 761 | cast<NonLValue>(GetValue(N1->getState(), L1, &T1)), |
| 762 | cast<NonLValue>(GetValue(N2->getState(), L2, &T2))); |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 763 | } |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 764 | } |
Ted Kremenek | 022b605 | 2008-02-15 22:29:00 +0000 | [diff] [blame] | 765 | else { // Any other operation between two Non-LValues. |
Ted Kremenek | 521d972 | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 766 | QualType T = B->getLHS()->getType(); |
| 767 | const NonLValue& R1 = cast<NonLValue>(GetValue(N1->getState(), |
| 768 | L1, &T)); |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 769 | const NonLValue& R2 = cast<NonLValue>(V2); |
Ted Kremenek | b193413 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 770 | Result = EvalBinaryOp(ValMgr, Op, R1, R2); |
Ted Kremenek | 19072e0 | 2008-01-29 19:43:15 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 773 | Nodify(Dst, B, N2, SetValue(SetValue(St, B, Result), L1, Result)); |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 774 | break; |
Ted Kremenek | 15cb078 | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 775 | } |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 776 | } |
Ted Kremenek | afba4b2 | 2008-01-16 00:53:15 +0000 | [diff] [blame] | 777 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 778 | } |
Ted Kremenek | 68d70a8 | 2008-01-15 23:55:06 +0000 | [diff] [blame] | 779 | } |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 780 | |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 781 | void GRExprEngine::HandleUninitializedStore(Stmt* S, NodeTy* Pred) { |
| 782 | |
| 783 | NodeTy* N = Builder->generateNode(S, Pred->getState(), Pred); |
| 784 | N->markAsSink(); |
| 785 | UninitStores.insert(N); |
| 786 | } |
Ted Kremenek | be96245 | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 787 | |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 788 | void GRExprEngine::Visit(Stmt* S, NodeTy* Pred, NodeSet& Dst) { |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 789 | |
| 790 | // FIXME: add metadata to the CFG so that we can disable |
| 791 | // this check when we KNOW that there is no block-level subexpression. |
| 792 | // The motivation is that this check requires a hashtable lookup. |
| 793 | |
| 794 | if (S != CurrentStmt && getCFG().isBlkExpr(S)) { |
| 795 | Dst.Add(Pred); |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | switch (S->getStmtClass()) { |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 800 | |
| 801 | default: |
| 802 | // Cases we intentionally have "default" handle: |
| 803 | // AddrLabelExpr, CharacterLiteral, IntegerLiteral |
| 804 | |
| 805 | Dst.Add(Pred); // No-op. Simply propagate the current state unchanged. |
| 806 | break; |
| 807 | |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 808 | case Stmt::BinaryOperatorClass: { |
| 809 | BinaryOperator* B = cast<BinaryOperator>(S); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 810 | |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 811 | if (B->isLogicalOp()) { |
| 812 | VisitLogicalExpr(B, Pred, Dst); |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 813 | break; |
| 814 | } |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 815 | else if (B->getOpcode() == BinaryOperator::Comma) { |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 816 | StateTy St = Pred->getState(); |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 817 | Nodify(Dst, B, Pred, SetValue(St, B, GetValue(St, B->getRHS()))); |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 820 | |
| 821 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | case Stmt::CastExprClass: { |
| 826 | CastExpr* C = cast<CastExpr>(S); |
| 827 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 828 | break; |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 829 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 830 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 831 | case Stmt::ChooseExprClass: { // __builtin_choose_expr |
| 832 | ChooseExpr* C = cast<ChooseExpr>(S); |
| 833 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 834 | break; |
| 835 | } |
Ted Kremenek | 1f0eb99 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 836 | |
Ted Kremenek | 9914e9c | 2008-01-23 23:38:00 +0000 | [diff] [blame] | 837 | case Stmt::CompoundAssignOperatorClass: |
Ted Kremenek | f031b87 | 2008-01-23 19:59:44 +0000 | [diff] [blame] | 838 | VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst); |
| 839 | break; |
| 840 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 841 | case Stmt::ConditionalOperatorClass: { // '?' operator |
| 842 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 843 | VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst); |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | case Stmt::DeclRefExprClass: |
| 848 | VisitDeclRefExpr(cast<DeclRefExpr>(S), Pred, Dst); |
| 849 | break; |
| 850 | |
| 851 | case Stmt::DeclStmtClass: |
| 852 | VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst); |
| 853 | break; |
| 854 | |
| 855 | case Stmt::ImplicitCastExprClass: { |
| 856 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(S); |
| 857 | VisitCast(C, C->getSubExpr(), Pred, Dst); |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | case Stmt::ParenExprClass: |
| 862 | Visit(cast<ParenExpr>(S)->getSubExpr(), Pred, Dst); |
| 863 | break; |
| 864 | |
| 865 | case Stmt::SizeOfAlignOfTypeExprClass: |
| 866 | VisitSizeOfAlignOfTypeExpr(cast<SizeOfAlignOfTypeExpr>(S), Pred, Dst); |
| 867 | break; |
| 868 | |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 869 | case Stmt::StmtExprClass: { |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 870 | StmtExpr* SE = cast<StmtExpr>(S); |
| 871 | |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 872 | StateTy St = Pred->getState(); |
Ted Kremenek | 744a786 | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 873 | Expr* LastExpr = cast<Expr>(*SE->getSubStmt()->body_rbegin()); |
| 874 | Nodify(Dst, SE, Pred, SetValue(St, SE, GetValue(St, LastExpr))); |
Ted Kremenek | 106f37c | 2008-02-08 07:05:39 +0000 | [diff] [blame] | 875 | break; |
| 876 | } |
| 877 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 878 | case Stmt::ReturnStmtClass: { |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 879 | if (Expr* R = cast<ReturnStmt>(S)->getRetValue()) |
| 880 | Visit(R, Pred, Dst); |
| 881 | else |
| 882 | Dst.Add(Pred); |
| 883 | |
| 884 | break; |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 885 | } |
Ted Kremenek | e1f38b6 | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 886 | |
Ted Kremenek | fd85f29 | 2008-02-12 19:49:57 +0000 | [diff] [blame] | 887 | case Stmt::UnaryOperatorClass: |
| 888 | VisitUnaryOperator(cast<UnaryOperator>(S), Pred, Dst); |
Ted Kremenek | b9c30e3 | 2008-01-24 20:55:43 +0000 | [diff] [blame] | 889 | break; |
Ted Kremenek | ab8ed95 | 2008-01-17 18:25:22 +0000 | [diff] [blame] | 890 | } |
Ted Kremenek | be96245 | 2008-01-16 19:42:59 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 893 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 894 | // "Assume" logic. |
| 895 | //===----------------------------------------------------------------------===// |
| 896 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 897 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, LValue Cond, |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 898 | bool Assumption, |
| 899 | bool& isFeasible) { |
| 900 | |
| 901 | assert (!isa<UninitializedVal>(Cond)); |
| 902 | |
| 903 | if (isa<UnknownVal>(Cond)) { |
| 904 | isFeasible = true; |
| 905 | return St; |
| 906 | } |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 907 | |
| 908 | switch (Cond.getSubKind()) { |
| 909 | default: |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 910 | assert (false && "'Assume' not implemented for this LValue."); |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 911 | return St; |
| 912 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 913 | case lval::SymbolValKind: |
| 914 | if (Assumption) |
| 915 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 916 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 917 | else |
| 918 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 919 | ValMgr.getZeroWithPtrWidth(), isFeasible); |
| 920 | |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 921 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 922 | case lval::DeclValKind: |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 923 | isFeasible = Assumption; |
| 924 | return St; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 925 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 926 | case lval::ConcreteIntKind: { |
| 927 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | 6e24a80 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 928 | isFeasible = b ? Assumption : !Assumption; |
| 929 | return St; |
| 930 | } |
| 931 | } |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 934 | GRExprEngine::StateTy GRExprEngine::Assume(StateTy St, NonLValue Cond, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 935 | bool Assumption, |
Ted Kremenek | 7219790 | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 936 | bool& isFeasible) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 937 | |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 938 | assert (!isa<UninitializedVal>(Cond)); |
| 939 | |
| 940 | if (isa<UnknownVal>(Cond)) { |
| 941 | isFeasible = true; |
| 942 | return St; |
| 943 | } |
| 944 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 945 | switch (Cond.getSubKind()) { |
| 946 | default: |
| 947 | assert (false && "'Assume' not implemented for this NonLValue."); |
| 948 | return St; |
| 949 | |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 950 | |
| 951 | case nonlval::SymbolValKind: { |
Ted Kremenek | 5bde36b | 2008-02-12 21:37:25 +0000 | [diff] [blame] | 952 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
Ted Kremenek | ab359c1 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 953 | SymbolID sym = SV.getSymbol(); |
| 954 | |
| 955 | if (Assumption) |
| 956 | return AssumeSymNE(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 957 | isFeasible); |
| 958 | else |
| 959 | return AssumeSymEQ(St, sym, ValMgr.getValue(0, SymMgr.getType(sym)), |
| 960 | isFeasible); |
| 961 | } |
| 962 | |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 963 | case nonlval::SymIntConstraintValKind: |
| 964 | return |
| 965 | AssumeSymInt(St, Assumption, |
| 966 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 967 | isFeasible); |
| 968 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 969 | case nonlval::ConcreteIntKind: { |
| 970 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 971 | isFeasible = b ? Assumption : !Assumption; |
| 972 | return St; |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 977 | GRExprEngine::StateTy |
| 978 | GRExprEngine::AssumeSymNE(StateTy St, SymbolID sym, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 979 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 980 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 981 | // First, determine if sym == X, where X != V. |
| 982 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 983 | isFeasible = *X != V; |
| 984 | return St; |
| 985 | } |
| 986 | |
| 987 | // Second, determine if sym != V. |
| 988 | if (St.isNotEqual(sym, V)) { |
| 989 | isFeasible = true; |
| 990 | return St; |
| 991 | } |
| 992 | |
| 993 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 994 | // Make that assumption. |
| 995 | |
| 996 | isFeasible = true; |
| 997 | return StateMgr.AddNE(St, sym, V); |
| 998 | } |
| 999 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1000 | GRExprEngine::StateTy |
| 1001 | GRExprEngine::AssumeSymEQ(StateTy St, SymbolID sym, |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 1002 | const llvm::APSInt& V, bool& isFeasible) { |
| 1003 | |
| 1004 | // First, determine if sym == X, where X != V. |
| 1005 | if (const llvm::APSInt* X = St.getSymVal(sym)) { |
| 1006 | isFeasible = *X == V; |
| 1007 | return St; |
| 1008 | } |
| 1009 | |
| 1010 | // Second, determine if sym != V. |
| 1011 | if (St.isNotEqual(sym, V)) { |
| 1012 | isFeasible = false; |
| 1013 | return St; |
| 1014 | } |
| 1015 | |
| 1016 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 1017 | // Make that assumption. |
| 1018 | |
| 1019 | isFeasible = true; |
| 1020 | return StateMgr.AddEQ(St, sym, V); |
| 1021 | } |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1022 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1023 | GRExprEngine::StateTy |
| 1024 | GRExprEngine::AssumeSymInt(StateTy St, bool Assumption, |
Ted Kremenek | 0033fbb | 2008-02-06 04:31:33 +0000 | [diff] [blame] | 1025 | const SymIntConstraint& C, bool& isFeasible) { |
| 1026 | |
| 1027 | switch (C.getOpcode()) { |
| 1028 | default: |
| 1029 | // No logic yet for other operators. |
| 1030 | return St; |
| 1031 | |
| 1032 | case BinaryOperator::EQ: |
| 1033 | if (Assumption) |
| 1034 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1035 | else |
| 1036 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1037 | |
| 1038 | case BinaryOperator::NE: |
| 1039 | if (Assumption) |
| 1040 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1041 | else |
| 1042 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1046 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1047 | // Visualization. |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1048 | //===----------------------------------------------------------------------===// |
| 1049 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1050 | #ifndef NDEBUG |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1051 | static GRExprEngine* GraphPrintCheckerState; |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1053 | namespace llvm { |
| 1054 | template<> |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1055 | struct VISIBILITY_HIDDEN DOTGraphTraits<GRExprEngine::NodeTy*> : |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1056 | public DefaultDOTGraphTraits { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1057 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1058 | static void PrintVarBindings(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1059 | |
| 1060 | Out << "Variables:\\l"; |
| 1061 | |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1062 | bool isFirst = true; |
| 1063 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1064 | for (GRExprEngine::StateTy::vb_iterator I=St.vb_begin(), |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1065 | E=St.vb_end(); I!=E;++I) { |
| 1066 | |
| 1067 | if (isFirst) |
| 1068 | isFirst = false; |
| 1069 | else |
| 1070 | Out << "\\l"; |
| 1071 | |
| 1072 | Out << ' ' << I.getKey()->getName() << " : "; |
| 1073 | I.getData().print(Out); |
| 1074 | } |
| 1075 | |
| 1076 | } |
| 1077 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1078 | |
Ted Kremenek | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1079 | static void PrintSubExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1080 | |
| 1081 | bool isFirst = true; |
| 1082 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1083 | for (GRExprEngine::StateTy::seb_iterator I=St.seb_begin(), E=St.seb_end(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1084 | I != E;++I) { |
| 1085 | |
| 1086 | if (isFirst) { |
| 1087 | Out << "\\l\\lSub-Expressions:\\l"; |
| 1088 | isFirst = false; |
| 1089 | } |
| 1090 | else |
| 1091 | Out << "\\l"; |
| 1092 | |
| 1093 | Out << " (" << (void*) I.getKey() << ") "; |
| 1094 | I.getKey()->printPretty(Out); |
| 1095 | Out << " : "; |
| 1096 | I.getData().print(Out); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Ted Kremenek | 6d40992 | 2008-02-13 18:06:44 +0000 | [diff] [blame] | 1100 | static void PrintBlkExprBindings(std::ostream& Out, GRExprEngine::StateTy St){ |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1101 | |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1102 | bool isFirst = true; |
| 1103 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1104 | for (GRExprEngine::StateTy::beb_iterator I=St.beb_begin(), E=St.beb_end(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1105 | I != E; ++I) { |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1106 | if (isFirst) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1107 | Out << "\\l\\lBlock-level Expressions:\\l"; |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1108 | isFirst = false; |
| 1109 | } |
| 1110 | else |
| 1111 | Out << "\\l"; |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1112 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1113 | Out << " (" << (void*) I.getKey() << ") "; |
| 1114 | I.getKey()->printPretty(Out); |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1115 | Out << " : "; |
| 1116 | I.getData().print(Out); |
| 1117 | } |
| 1118 | } |
| 1119 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1120 | static void PrintEQ(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1121 | ValueState::ConstantEqTy CE = St.getImpl()->ConstantEq; |
| 1122 | |
| 1123 | if (CE.isEmpty()) |
| 1124 | return; |
| 1125 | |
| 1126 | Out << "\\l\\|'==' constraints:"; |
| 1127 | |
| 1128 | for (ValueState::ConstantEqTy::iterator I=CE.begin(), E=CE.end(); I!=E;++I) |
| 1129 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 1130 | } |
| 1131 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1132 | static void PrintNE(std::ostream& Out, GRExprEngine::StateTy St) { |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1133 | ValueState::ConstantNotEqTy NE = St.getImpl()->ConstantNotEq; |
| 1134 | |
| 1135 | if (NE.isEmpty()) |
| 1136 | return; |
| 1137 | |
| 1138 | Out << "\\l\\|'!=' constraints:"; |
| 1139 | |
| 1140 | for (ValueState::ConstantNotEqTy::iterator I=NE.begin(), EI=NE.end(); |
| 1141 | I != EI; ++I){ |
| 1142 | |
| 1143 | Out << "\\l $" << I.getKey() << " : "; |
| 1144 | bool isFirst = true; |
| 1145 | |
| 1146 | ValueState::IntSetTy::iterator J=I.getData().begin(), |
| 1147 | EJ=I.getData().end(); |
| 1148 | for ( ; J != EJ; ++J) { |
| 1149 | if (isFirst) isFirst = false; |
| 1150 | else Out << ", "; |
| 1151 | |
| 1152 | Out << (*J)->toString(); |
| 1153 | } |
| 1154 | } |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | static std::string getNodeAttributes(const GRExprEngine::NodeTy* N, void*) { |
| 1158 | |
| 1159 | if (GraphPrintCheckerState->isImplicitNullDeref(N) || |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1160 | GraphPrintCheckerState->isExplicitNullDeref(N) || |
| 1161 | GraphPrintCheckerState->isUninitStore(N) || |
| 1162 | GraphPrintCheckerState->isUninitControlFlow(N)) |
Ted Kremenek | a853de6 | 2008-02-14 22:54:53 +0000 | [diff] [blame] | 1163 | return "color=\"red\",style=\"filled\""; |
| 1164 | |
| 1165 | return ""; |
| 1166 | } |
Ted Kremenek | e653669 | 2008-02-06 03:56:15 +0000 | [diff] [blame] | 1167 | |
Ted Kremenek | 30fa28b | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 1168 | static std::string getNodeLabel(const GRExprEngine::NodeTy* N, void*) { |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1169 | std::ostringstream Out; |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1170 | |
| 1171 | // Program Location. |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1172 | ProgramPoint Loc = N->getLocation(); |
| 1173 | |
| 1174 | switch (Loc.getKind()) { |
| 1175 | case ProgramPoint::BlockEntranceKind: |
| 1176 | Out << "Block Entrance: B" |
| 1177 | << cast<BlockEntrance>(Loc).getBlock()->getBlockID(); |
| 1178 | break; |
| 1179 | |
| 1180 | case ProgramPoint::BlockExitKind: |
| 1181 | assert (false); |
| 1182 | break; |
| 1183 | |
| 1184 | case ProgramPoint::PostStmtKind: { |
| 1185 | const PostStmt& L = cast<PostStmt>(Loc); |
Ted Kremenek | 3ec7be9 | 2008-01-24 22:27:20 +0000 | [diff] [blame] | 1186 | Out << L.getStmt()->getStmtClassName() << ':' |
| 1187 | << (void*) L.getStmt() << ' '; |
| 1188 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1189 | L.getStmt()->printPretty(Out); |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (GraphPrintCheckerState->isImplicitNullDeref(N)) { |
| 1192 | Out << "\\|Implicit-Null Dereference.\\l"; |
| 1193 | } |
Ted Kremenek | ae7bdc1 | 2008-02-07 06:04:18 +0000 | [diff] [blame] | 1194 | else if (GraphPrintCheckerState->isExplicitNullDeref(N)) { |
| 1195 | Out << "\\|Explicit-Null Dereference.\\l"; |
| 1196 | } |
Ted Kremenek | bf988d0 | 2008-02-19 00:22:37 +0000 | [diff] [blame] | 1197 | else if (GraphPrintCheckerState->isUninitStore(N)) { |
| 1198 | Out << "\\|Store to Uninitialized LValue."; |
| 1199 | } |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 1200 | |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | } |
| 1203 | |
| 1204 | default: { |
| 1205 | const BlockEdge& E = cast<BlockEdge>(Loc); |
| 1206 | Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B" |
| 1207 | << E.getDst()->getBlockID() << ')'; |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (Stmt* T = E.getSrc()->getTerminator()) { |
| 1210 | Out << "\\|Terminator: "; |
| 1211 | E.getSrc()->printTerminator(Out); |
| 1212 | |
Ted Kremenek | aee121c | 2008-02-13 23:08:21 +0000 | [diff] [blame] | 1213 | if (isa<SwitchStmt>(T)) { |
| 1214 | Stmt* Label = E.getDst()->getLabel(); |
| 1215 | |
| 1216 | if (Label) { |
| 1217 | if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) { |
| 1218 | Out << "\\lcase "; |
| 1219 | C->getLHS()->printPretty(Out); |
| 1220 | |
| 1221 | if (Stmt* RHS = C->getRHS()) { |
| 1222 | Out << " .. "; |
| 1223 | RHS->printPretty(Out); |
| 1224 | } |
| 1225 | |
| 1226 | Out << ":"; |
| 1227 | } |
| 1228 | else { |
| 1229 | assert (isa<DefaultStmt>(Label)); |
| 1230 | Out << "\\ldefault:"; |
| 1231 | } |
| 1232 | } |
| 1233 | else |
| 1234 | Out << "\\l(implicit) default:"; |
| 1235 | } |
| 1236 | else if (isa<IndirectGotoStmt>(T)) { |
Ted Kremenek | 9096097 | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 1237 | // FIXME |
| 1238 | } |
| 1239 | else { |
| 1240 | Out << "\\lCondition: "; |
| 1241 | if (*E.getSrc()->succ_begin() == E.getDst()) |
| 1242 | Out << "true"; |
| 1243 | else |
| 1244 | Out << "false"; |
| 1245 | } |
| 1246 | |
| 1247 | Out << "\\l"; |
| 1248 | } |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1249 | |
| 1250 | if (GraphPrintCheckerState->isUninitControlFlow(N)) { |
| 1251 | Out << "\\|Control-flow based on\\lUninitialized value.\\l"; |
| 1252 | } |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1256 | Out << "\\|StateID: " << (void*) N->getState().getImpl() << "\\|"; |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 1257 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 1258 | N->getState().printDOT(Out); |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1259 | |
Ted Kremenek | bacd6cd | 2008-01-23 22:30:44 +0000 | [diff] [blame] | 1260 | Out << "\\l"; |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1261 | return Out.str(); |
| 1262 | } |
| 1263 | }; |
| 1264 | } // end llvm namespace |
| 1265 | #endif |
| 1266 | |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1267 | void GRExprEngine::ViewGraph() { |
Ted Kremenek | dd9e97d | 2008-01-16 21:46:15 +0000 | [diff] [blame] | 1268 | #ifndef NDEBUG |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1269 | GraphPrintCheckerState = this; |
| 1270 | llvm::ViewGraph(*G.roots_begin(), "GRExprEngine"); |
Ted Kremenek | 428d39e | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 1271 | GraphPrintCheckerState = NULL; |
Ted Kremenek | 3862eb1 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 1272 | #endif |
Ted Kremenek | d2500ab | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 1273 | } |