Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 1 | // GRSimpleVals.cpp - Transfer functions for tracking simple values -*- C++ -*-- |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This files defines GRSimpleVals, a sub-class of GRTransferFuncs that |
| 11 | // provides transfer functions for performing simple value tracking with |
| 12 | // limited support for symbolics. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "GRSimpleVals.h" |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 21 | namespace clang { |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame^] | 22 | unsigned RunGRSimpleVals(CFG& cfg, FunctionDecl& FD, ASTContext& Ctx, |
| 23 | Diagnostic& Diag, bool Visualize) { |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 24 | |
Ted Kremenek | 546bded | 2008-02-14 22:54:17 +0000 | [diff] [blame] | 25 | if (Diag.hasErrorOccurred()) |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame^] | 26 | return 0; |
Ted Kremenek | 546bded | 2008-02-14 22:54:17 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 28 | GRCoreEngine<GRExprEngine> Engine(cfg, FD, Ctx); |
| 29 | GRExprEngine* CheckerState = &Engine.getCheckerState(); |
| 30 | GRSimpleVals GRSV; |
| 31 | CheckerState->setTransferFunctions(GRSV); |
| 32 | |
| 33 | // Execute the worklist algorithm. |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame^] | 34 | Engine.ExecuteWorkList(200); |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 35 | |
| 36 | // Look for explicit-Null dereferences and warn about them. |
| 37 | for (GRExprEngine::null_iterator I=CheckerState->null_begin(), |
| 38 | E=CheckerState->null_end(); I!=E; ++I) { |
| 39 | |
| 40 | const PostStmt& L = cast<PostStmt>((*I)->getLocation()); |
Eli Friedman | 7df3d71 | 2008-02-16 23:17:23 +0000 | [diff] [blame] | 41 | Expr* Exp = cast<Expr>(L.getStmt()); |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 42 | |
Eli Friedman | 7df3d71 | 2008-02-16 23:17:23 +0000 | [diff] [blame] | 43 | Diag.Report(FullSourceLoc(Exp->getExprLoc(), Ctx.getSourceManager()), |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 44 | diag::chkr_null_deref_after_check); |
| 45 | } |
| 46 | |
| 47 | #ifndef NDEBUG |
Ted Kremenek | d55fe52 | 2008-02-15 00:35:38 +0000 | [diff] [blame] | 48 | if (Visualize) CheckerState->ViewGraph(); |
Ted Kremenek | 9dca062 | 2008-02-19 00:22:37 +0000 | [diff] [blame^] | 49 | #endif |
| 50 | |
| 51 | return Engine.getGraph().size(); |
Ted Kremenek | e01c987 | 2008-02-14 22:36:46 +0000 | [diff] [blame] | 52 | } |
| 53 | } // end clang namespace |
| 54 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
| 56 | // Transfer function for Casts. |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | RValue GRSimpleVals::EvalCast(ValueManager& ValMgr, NonLValue X, |
| 60 | Expr* CastExpr) { |
| 61 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 62 | assert (!isa<UnknownVal>(X) && !isa<UninitializedVal>(X)); |
| 63 | |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 64 | if (!isa<nonlval::ConcreteInt>(X)) |
| 65 | return UnknownVal(); |
| 66 | |
| 67 | llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue(); |
| 68 | QualType T = CastExpr->getType(); |
| 69 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
| 70 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
| 71 | |
| 72 | if (CastExpr->getType()->isPointerType()) |
| 73 | return lval::ConcreteInt(ValMgr.getValue(V)); |
| 74 | else |
| 75 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
| 76 | } |
| 77 | |
| 78 | // Casts. |
| 79 | |
| 80 | RValue GRSimpleVals::EvalCast(ValueManager& ValMgr, LValue X, Expr* CastExpr) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 81 | |
| 82 | assert (!isa<UnknownVal>(X) && !isa<UninitializedVal>(X)); |
Ted Kremenek | d59cccc | 2008-02-14 18:28:23 +0000 | [diff] [blame] | 83 | |
| 84 | if (CastExpr->getType()->isPointerType()) |
| 85 | return X; |
| 86 | |
| 87 | assert (CastExpr->getType()->isIntegerType()); |
| 88 | |
| 89 | if (!isa<lval::ConcreteInt>(X)) |
| 90 | return UnknownVal(); |
| 91 | |
| 92 | llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue(); |
| 93 | QualType T = CastExpr->getType(); |
| 94 | V.setIsUnsigned(T->isUnsignedIntegerType() || T->isPointerType()); |
| 95 | V.extOrTrunc(ValMgr.getContext().getTypeSize(T, CastExpr->getLocStart())); |
| 96 | |
| 97 | return nonlval::ConcreteInt(ValMgr.getValue(V)); |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Unary operators. |
| 101 | |
| 102 | NonLValue GRSimpleVals::EvalMinus(ValueManager& ValMgr, UnaryOperator* U, |
| 103 | NonLValue X) { |
| 104 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 105 | assert (!isa<UnknownVal>(X) && !isa<UninitializedVal>(X)); |
| 106 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 107 | switch (X.getSubKind()) { |
| 108 | case nonlval::ConcreteIntKind: |
| 109 | return cast<nonlval::ConcreteInt>(X).EvalMinus(ValMgr, U); |
| 110 | default: |
| 111 | return cast<NonLValue>(UnknownVal()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | NonLValue GRSimpleVals::EvalComplement(ValueManager& ValMgr, NonLValue X) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 116 | |
| 117 | assert (!isa<UnknownVal>(X) && !isa<UninitializedVal>(X)); |
| 118 | |
Ted Kremenek | c3f261d | 2008-02-14 18:40:24 +0000 | [diff] [blame] | 119 | switch (X.getSubKind()) { |
| 120 | case nonlval::ConcreteIntKind: |
| 121 | return cast<nonlval::ConcreteInt>(X).EvalComplement(ValMgr); |
| 122 | default: |
| 123 | return cast<NonLValue>(UnknownVal()); |
| 124 | } |
| 125 | } |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 126 | |
| 127 | // Binary operators. |
| 128 | |
| 129 | NonLValue GRSimpleVals::EvalBinaryOp(ValueManager& ValMgr, |
| 130 | BinaryOperator::Opcode Op, |
| 131 | NonLValue LHS, NonLValue RHS) { |
| 132 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 133 | assert (!isa<UnknownVal>(LHS) && !isa<UninitializedVal>(LHS)); |
| 134 | assert (!isa<UnknownVal>(RHS) && !isa<UninitializedVal>(RHS)); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 135 | |
| 136 | while(1) { |
| 137 | |
| 138 | switch (LHS.getSubKind()) { |
| 139 | default: |
| 140 | return cast<NonLValue>(UnknownVal()); |
| 141 | |
| 142 | case nonlval::ConcreteIntKind: |
| 143 | |
| 144 | if (isa<nonlval::ConcreteInt>(RHS)) { |
| 145 | const nonlval::ConcreteInt& LHS_CI = cast<nonlval::ConcreteInt>(LHS); |
| 146 | const nonlval::ConcreteInt& RHS_CI = cast<nonlval::ConcreteInt>(RHS); |
| 147 | return LHS_CI.EvalBinaryOp(ValMgr, Op, RHS_CI); |
| 148 | } |
| 149 | else if(isa<UnknownVal>(RHS)) |
| 150 | return cast<NonLValue>(UnknownVal()); |
| 151 | else { |
| 152 | NonLValue tmp = RHS; |
| 153 | RHS = LHS; |
| 154 | LHS = tmp; |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | case nonlval::SymbolValKind: { |
| 159 | if (isa<nonlval::ConcreteInt>(RHS)) { |
| 160 | const SymIntConstraint& C = |
| 161 | ValMgr.getConstraint(cast<nonlval::SymbolVal>(LHS).getSymbol(), Op, |
| 162 | cast<nonlval::ConcreteInt>(RHS).getValue()); |
| 163 | |
| 164 | return nonlval::SymIntConstraintVal(C); |
| 165 | } |
| 166 | else |
| 167 | return cast<NonLValue>(UnknownVal()); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 174 | // Binary Operators (except assignments and comma). |
| 175 | |
| 176 | RValue GRSimpleVals::EvalBinaryOp(ValueManager& ValMgr, |
| 177 | BinaryOperator::Opcode Op, |
| 178 | LValue LHS, LValue RHS) { |
| 179 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 180 | assert (!isa<UnknownVal>(LHS) && !isa<UninitializedVal>(LHS)); |
| 181 | assert (!isa<UnknownVal>(RHS) && !isa<UninitializedVal>(RHS)); |
| 182 | |
Ted Kremenek | c6fbdcd | 2008-02-15 23:15:23 +0000 | [diff] [blame] | 183 | switch (Op) { |
| 184 | default: |
| 185 | return UnknownVal(); |
| 186 | |
| 187 | case BinaryOperator::EQ: |
| 188 | return EvalEQ(ValMgr, LHS, RHS); |
| 189 | |
| 190 | case BinaryOperator::NE: |
| 191 | return EvalNE(ValMgr, LHS, RHS); |
| 192 | } |
| 193 | } |
| 194 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 195 | // Pointer arithmetic. |
| 196 | |
| 197 | LValue GRSimpleVals::EvalBinaryOp(ValueManager& ValMgr, |
| 198 | BinaryOperator::Opcode Op, |
| 199 | LValue LHS, NonLValue RHS) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 200 | |
| 201 | assert (!isa<UnknownVal>(LHS) && !isa<UninitializedVal>(LHS)); |
| 202 | assert (!isa<UnknownVal>(RHS) && !isa<UninitializedVal>(RHS)); |
| 203 | |
Ted Kremenek | b640b3b | 2008-02-15 00:52:26 +0000 | [diff] [blame] | 204 | return cast<LValue>(UnknownVal()); |
| 205 | } |
| 206 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 207 | // Equality operators for LValues. |
| 208 | |
| 209 | |
| 210 | NonLValue GRSimpleVals::EvalEQ(ValueManager& ValMgr, LValue LHS, LValue RHS) { |
| 211 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 212 | assert (!isa<UnknownVal>(LHS) && !isa<UninitializedVal>(LHS)); |
| 213 | assert (!isa<UnknownVal>(RHS) && !isa<UninitializedVal>(RHS)); |
| 214 | |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 215 | switch (LHS.getSubKind()) { |
| 216 | default: |
| 217 | assert(false && "EQ not implemented for this LValue."); |
| 218 | return cast<NonLValue>(UnknownVal()); |
| 219 | |
| 220 | case lval::ConcreteIntKind: |
| 221 | if (isa<lval::ConcreteInt>(RHS)) { |
| 222 | bool b = cast<lval::ConcreteInt>(LHS).getValue() == |
| 223 | cast<lval::ConcreteInt>(RHS).getValue(); |
| 224 | |
| 225 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 226 | } |
| 227 | else if (isa<lval::SymbolVal>(RHS)) { |
| 228 | |
| 229 | const SymIntConstraint& C = |
| 230 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 231 | BinaryOperator::EQ, |
| 232 | cast<lval::ConcreteInt>(LHS).getValue()); |
| 233 | |
| 234 | return nonlval::SymIntConstraintVal(C); |
| 235 | } |
| 236 | |
| 237 | break; |
| 238 | |
| 239 | case lval::SymbolValKind: { |
| 240 | if (isa<lval::ConcreteInt>(RHS)) { |
| 241 | const SymIntConstraint& C = |
| 242 | ValMgr.getConstraint(cast<lval::SymbolVal>(LHS).getSymbol(), |
| 243 | BinaryOperator::EQ, |
| 244 | cast<lval::ConcreteInt>(RHS).getValue()); |
| 245 | |
| 246 | return nonlval::SymIntConstraintVal(C); |
| 247 | } |
| 248 | |
Ted Kremenek | f0c8ef0 | 2008-02-18 21:19:49 +0000 | [diff] [blame] | 249 | // FIXME: Implement unification |
| 250 | return cast<NonLValue>(UnknownVal()); |
| 251 | //assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement unification."); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 252 | |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | case lval::DeclValKind: |
| 257 | |
| 258 | if (isa<lval::DeclVal>(RHS)) { |
| 259 | bool b = cast<lval::DeclVal>(LHS) == cast<lval::DeclVal>(RHS); |
| 260 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 261 | } |
| 262 | |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | return NonLValue::GetIntTruthValue(ValMgr, false); |
| 267 | } |
| 268 | |
| 269 | NonLValue GRSimpleVals::EvalNE(ValueManager& ValMgr, LValue LHS, LValue RHS) { |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 270 | |
| 271 | assert (!isa<UnknownVal>(LHS) && !isa<UninitializedVal>(LHS)); |
| 272 | assert (!isa<UnknownVal>(RHS) && !isa<UninitializedVal>(RHS)); |
Ted Kremenek | 6cb0b54 | 2008-02-14 19:37:24 +0000 | [diff] [blame] | 273 | |
| 274 | switch (LHS.getSubKind()) { |
| 275 | default: |
| 276 | assert(false && "NE not implemented for this LValue."); |
| 277 | return cast<NonLValue>(UnknownVal()); |
| 278 | |
| 279 | case lval::ConcreteIntKind: |
| 280 | if (isa<lval::ConcreteInt>(RHS)) { |
| 281 | bool b = cast<lval::ConcreteInt>(LHS).getValue() != |
| 282 | cast<lval::ConcreteInt>(RHS).getValue(); |
| 283 | |
| 284 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 285 | } |
| 286 | else if (isa<lval::SymbolVal>(RHS)) { |
| 287 | const SymIntConstraint& C = |
| 288 | ValMgr.getConstraint(cast<lval::SymbolVal>(RHS).getSymbol(), |
| 289 | BinaryOperator::NE, |
| 290 | cast<lval::ConcreteInt>(LHS).getValue()); |
| 291 | |
| 292 | return nonlval::SymIntConstraintVal(C); |
| 293 | } |
| 294 | |
| 295 | break; |
| 296 | |
| 297 | case lval::SymbolValKind: { |
| 298 | if (isa<lval::ConcreteInt>(RHS)) { |
| 299 | const SymIntConstraint& C = |
| 300 | ValMgr.getConstraint(cast<lval::SymbolVal>(LHS).getSymbol(), |
| 301 | BinaryOperator::NE, |
| 302 | cast<lval::ConcreteInt>(RHS).getValue()); |
| 303 | |
| 304 | return nonlval::SymIntConstraintVal(C); |
| 305 | } |
| 306 | |
| 307 | assert (!isa<lval::SymbolVal>(RHS) && "FIXME: Implement sym !=."); |
| 308 | |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | case lval::DeclValKind: |
| 313 | if (isa<lval::DeclVal>(RHS)) { |
| 314 | bool b = cast<lval::DeclVal>(LHS) == cast<lval::DeclVal>(RHS); |
| 315 | return NonLValue::GetIntTruthValue(ValMgr, b); |
| 316 | } |
| 317 | |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | return NonLValue::GetIntTruthValue(ValMgr, true); |
| 322 | } |