Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 1 | //== SimpleConstraintManager.cpp --------------------------------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines SimpleConstraintManager, a class that holds code shared |
| 11 | // between BasicConstraintManager and RangeConstraintManager. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SimpleConstraintManager.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/Analysis/PathSensitive/GRState.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | |
| 21 | SimpleConstraintManager::~SimpleConstraintManager() {} |
| 22 | |
Ted Kremenek | 66b5271 | 2009-03-11 02:22:59 +0000 | [diff] [blame] | 23 | bool SimpleConstraintManager::canReasonAbout(SVal X) const { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 24 | if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) { |
| 25 | const SymExpr *SE = SymVal->getSymbolicExpression(); |
| 26 | |
| 27 | if (isa<SymbolData>(SE)) |
| 28 | return true; |
| 29 | |
| 30 | if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) { |
| 31 | switch (SIE->getOpcode()) { |
| 32 | // We don't reason yet about bitwise-constraints on symbolic values. |
| 33 | case BinaryOperator::And: |
| 34 | case BinaryOperator::Or: |
| 35 | case BinaryOperator::Xor: |
| 36 | return false; |
| 37 | // We don't reason yet about arithmetic constraints on symbolic values. |
| 38 | case BinaryOperator::Mul: |
| 39 | case BinaryOperator::Div: |
| 40 | case BinaryOperator::Rem: |
| 41 | case BinaryOperator::Add: |
| 42 | case BinaryOperator::Sub: |
| 43 | case BinaryOperator::Shl: |
| 44 | case BinaryOperator::Shr: |
| 45 | return false; |
| 46 | // All other cases. |
| 47 | default: |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return false; |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ted Kremenek | 66b5271 | 2009-03-11 02:22:59 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 58 | const GRState* |
| 59 | SimpleConstraintManager::Assume(const GRState* St, SVal Cond, bool Assumption, |
| 60 | bool& isFeasible) { |
| 61 | if (Cond.isUnknown()) { |
| 62 | isFeasible = true; |
| 63 | return St; |
| 64 | } |
| 65 | |
| 66 | if (isa<NonLoc>(Cond)) |
| 67 | return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible); |
| 68 | else |
| 69 | return Assume(St, cast<Loc>(Cond), Assumption, isFeasible); |
| 70 | } |
| 71 | |
| 72 | const GRState* |
| 73 | SimpleConstraintManager::Assume(const GRState* St, Loc Cond, bool Assumption, |
| 74 | bool& isFeasible) { |
| 75 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 76 | |
| 77 | if (!isFeasible) |
| 78 | return St; |
| 79 | |
| 80 | // EvalAssume is used to call into the GRTransferFunction object to perform |
| 81 | // any checker-specific update of the state based on this assumption being |
| 82 | // true or false. |
| 83 | return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption, |
| 84 | isFeasible); |
| 85 | } |
| 86 | |
| 87 | const GRState* |
| 88 | SimpleConstraintManager::AssumeAux(const GRState* St, Loc Cond, bool Assumption, |
| 89 | bool& isFeasible) { |
| 90 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 91 | |
| 92 | switch (Cond.getSubKind()) { |
| 93 | default: |
| 94 | assert (false && "'Assume' not implemented for this Loc."); |
| 95 | return St; |
| 96 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 97 | case loc::MemRegionKind: { |
| 98 | // FIXME: Should this go into the storemanager? |
| 99 | |
| 100 | const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion(); |
| 101 | const SubRegion* SubR = dyn_cast<SubRegion>(R); |
| 102 | |
| 103 | while (SubR) { |
| 104 | // FIXME: now we only find the first symbolic region. |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame^] | 105 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(SubR)) { |
| 106 | if (Assumption) |
| 107 | return AssumeSymNE(St, SymR->getSymbol(), |
| 108 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 109 | else |
| 110 | return AssumeSymEQ(St, SymR->getSymbol(), |
| 111 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 112 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 113 | SubR = dyn_cast<SubRegion>(SubR->getSuperRegion()); |
| 114 | } |
| 115 | |
| 116 | // FALL-THROUGH. |
| 117 | } |
| 118 | |
| 119 | case loc::FuncValKind: |
| 120 | case loc::GotoLabelKind: |
| 121 | isFeasible = Assumption; |
| 122 | return St; |
| 123 | |
| 124 | case loc::ConcreteIntKind: { |
| 125 | bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0; |
| 126 | isFeasible = b ? Assumption : !Assumption; |
| 127 | return St; |
| 128 | } |
| 129 | } // end switch |
| 130 | } |
| 131 | |
| 132 | const GRState* |
| 133 | SimpleConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption, |
| 134 | bool& isFeasible) { |
| 135 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 136 | |
| 137 | if (!isFeasible) |
| 138 | return St; |
| 139 | |
| 140 | // EvalAssume is used to call into the GRTransferFunction object to perform |
| 141 | // any checker-specific update of the state based on this assumption being |
| 142 | // true or false. |
| 143 | return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption, |
| 144 | isFeasible); |
| 145 | } |
| 146 | |
| 147 | const GRState* |
| 148 | SimpleConstraintManager::AssumeAux(const GRState* St,NonLoc Cond, |
| 149 | bool Assumption, bool& isFeasible) { |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 150 | // We cannot reason about SymIntExpr and SymSymExpr. |
| 151 | if (!canReasonAbout(Cond)) { |
| 152 | isFeasible = true; |
| 153 | return St; |
| 154 | } |
| 155 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 156 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 157 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 158 | |
| 159 | switch (Cond.getSubKind()) { |
| 160 | default: |
| 161 | assert(false && "'Assume' not implemented for this NonLoc"); |
| 162 | |
| 163 | case nonloc::SymbolValKind: { |
| 164 | nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond); |
| 165 | SymbolRef sym = SV.getSymbol(); |
| 166 | QualType T = SymMgr.getType(sym); |
| 167 | |
| 168 | if (Assumption) |
| 169 | return AssumeSymNE(St, sym, BasicVals.getValue(0, T), isFeasible); |
| 170 | else |
| 171 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, T), isFeasible); |
| 172 | } |
| 173 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 174 | case nonloc::SymExprValKind: { |
| 175 | nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond); |
| 176 | if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression())) |
| 177 | return AssumeSymInt(St, Assumption, SE, isFeasible); |
| 178 | |
| 179 | isFeasible = true; |
| 180 | return St; |
| 181 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 182 | |
| 183 | case nonloc::ConcreteIntKind: { |
| 184 | bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0; |
| 185 | isFeasible = b ? Assumption : !Assumption; |
| 186 | return St; |
| 187 | } |
| 188 | |
| 189 | case nonloc::LocAsIntegerKind: |
| 190 | return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(), |
| 191 | Assumption, isFeasible); |
| 192 | } // end switch |
| 193 | } |
| 194 | |
| 195 | const GRState* |
| 196 | SimpleConstraintManager::AssumeSymInt(const GRState* St, bool Assumption, |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 197 | const SymIntExpr *SE, bool& isFeasible) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 199 | |
| 200 | // Here we assume that LHS is a symbol. This is consistent with the |
| 201 | // rest of the constraint manager logic. |
| 202 | SymbolRef Sym = cast<SymbolData>(SE->getLHS()); |
| 203 | const llvm::APSInt &Int = SE->getRHS(); |
| 204 | |
| 205 | switch (SE->getOpcode()) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 206 | default: |
| 207 | // No logic yet for other operators. |
| 208 | isFeasible = true; |
| 209 | return St; |
| 210 | |
| 211 | case BinaryOperator::EQ: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 212 | return Assumption ? AssumeSymEQ(St, Sym, Int, isFeasible) |
| 213 | : AssumeSymNE(St, Sym, Int, isFeasible); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 214 | |
| 215 | case BinaryOperator::NE: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 216 | return Assumption ? AssumeSymNE(St, Sym, Int, isFeasible) |
| 217 | : AssumeSymEQ(St, Sym, Int, isFeasible); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 218 | |
| 219 | case BinaryOperator::GT: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 220 | return Assumption ? AssumeSymGT(St, Sym, Int, isFeasible) |
| 221 | : AssumeSymLE(St, Sym, Int, isFeasible); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 222 | |
| 223 | case BinaryOperator::GE: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 224 | return Assumption ? AssumeSymGE(St, Sym, Int, isFeasible) |
| 225 | : AssumeSymLT(St, Sym, Int, isFeasible); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 226 | |
| 227 | case BinaryOperator::LT: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 228 | return Assumption ? AssumeSymLT(St, Sym, Int, isFeasible) |
| 229 | : AssumeSymGE(St, Sym, Int, isFeasible); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 230 | |
| 231 | case BinaryOperator::LE: |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 232 | return Assumption ? AssumeSymLE(St, Sym, Int, isFeasible) |
| 233 | : AssumeSymGT(St, Sym, Int, isFeasible); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 234 | } // end switch |
| 235 | } |
| 236 | |
| 237 | const GRState* |
| 238 | SimpleConstraintManager::AssumeInBound(const GRState* St, SVal Idx, |
| 239 | SVal UpperBound, bool Assumption, |
| 240 | bool& isFeasible) { |
| 241 | // Only support ConcreteInt for now. |
| 242 | if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))){ |
| 243 | isFeasible = true; |
| 244 | return St; |
| 245 | } |
| 246 | |
| 247 | const llvm::APSInt& Zero = getBasicVals().getZeroWithPtrWidth(false); |
| 248 | llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue(); |
| 249 | // IdxV might be too narrow. |
| 250 | if (IdxV.getBitWidth() < Zero.getBitWidth()) |
| 251 | IdxV.extend(Zero.getBitWidth()); |
| 252 | // UBV might be too narrow, too. |
| 253 | llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue(); |
| 254 | if (UBV.getBitWidth() < Zero.getBitWidth()) |
| 255 | UBV.extend(Zero.getBitWidth()); |
| 256 | |
| 257 | bool InBound = (Zero <= IdxV) && (IdxV < UBV); |
| 258 | |
| 259 | isFeasible = Assumption ? InBound : !InBound; |
| 260 | |
| 261 | return St; |
| 262 | } |
| 263 | |
| 264 | } // end of namespace clang |