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" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame^] | 16 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/Checker/PathSensitive/GRState.h" |
| 18 | #include "clang/Checker/PathSensitive/Checker.h" |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 19 | |
| 20 | namespace clang { |
| 21 | |
| 22 | SimpleConstraintManager::~SimpleConstraintManager() {} |
| 23 | |
Ted Kremenek | 66b5271 | 2009-03-11 02:22:59 +0000 | [diff] [blame] | 24 | bool SimpleConstraintManager::canReasonAbout(SVal X) const { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 25 | if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) { |
| 26 | const SymExpr *SE = SymVal->getSymbolicExpression(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 28 | if (isa<SymbolData>(SE)) |
| 29 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 31 | if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) { |
| 32 | switch (SIE->getOpcode()) { |
| 33 | // We don't reason yet about bitwise-constraints on symbolic values. |
| 34 | case BinaryOperator::And: |
| 35 | case BinaryOperator::Or: |
| 36 | case BinaryOperator::Xor: |
| 37 | return false; |
| 38 | // We don't reason yet about arithmetic constraints on symbolic values. |
| 39 | case BinaryOperator::Mul: |
| 40 | case BinaryOperator::Div: |
| 41 | case BinaryOperator::Rem: |
| 42 | case BinaryOperator::Add: |
| 43 | case BinaryOperator::Sub: |
| 44 | case BinaryOperator::Shl: |
| 45 | case BinaryOperator::Shr: |
| 46 | return false; |
| 47 | // All other cases. |
| 48 | default: |
| 49 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | } |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return false; |
Ted Kremenek | 7de20fe | 2009-03-11 02:29:48 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | 66b5271 | 2009-03-11 02:22:59 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 59 | const GRState *SimpleConstraintManager::Assume(const GRState *state, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 60 | DefinedSVal Cond, |
| 61 | bool Assumption) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 62 | if (isa<NonLoc>(Cond)) |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 63 | return Assume(state, cast<NonLoc>(Cond), Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 64 | else |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 65 | return Assume(state, cast<Loc>(Cond), Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 68 | const GRState *SimpleConstraintManager::Assume(const GRState *state, Loc cond, |
| 69 | bool assumption) { |
| 70 | state = AssumeAux(state, cond, assumption); |
| 71 | return SU.ProcessAssume(state, cond, assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 74 | const GRState *SimpleConstraintManager::AssumeAux(const GRState *state, |
| 75 | Loc Cond, bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 77 | BasicValueFactory &BasicVals = state->getBasicVals(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 78 | |
| 79 | switch (Cond.getSubKind()) { |
| 80 | default: |
| 81 | assert (false && "'Assume' not implemented for this Loc."); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 82 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 84 | case loc::MemRegionKind: { |
| 85 | // FIXME: Should this go into the storemanager? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 87 | const MemRegion *R = cast<loc::MemRegionVal>(Cond).getRegion(); |
| 88 | const SubRegion *SubR = dyn_cast<SubRegion>(R); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 89 | |
| 90 | while (SubR) { |
| 91 | // FIXME: now we only find the first symbolic region. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 92 | if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) { |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame] | 93 | if (Assumption) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | return AssumeSymNE(state, SymR->getSymbol(), |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 95 | BasicVals.getZeroWithPtrWidth()); |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame] | 96 | else |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 97 | return AssumeSymEQ(state, SymR->getSymbol(), |
| 98 | BasicVals.getZeroWithPtrWidth()); |
Zhongxing Xu | 3330dcb | 2009-04-10 06:06:13 +0000 | [diff] [blame] | 99 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 100 | SubR = dyn_cast<SubRegion>(SubR->getSuperRegion()); |
| 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 103 | // FALL-THROUGH. |
| 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 106 | case loc::GotoLabelKind: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 107 | return Assumption ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 108 | |
| 109 | case loc::ConcreteIntKind: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 111 | bool isFeasible = b ? Assumption : !Assumption; |
| 112 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 113 | } |
| 114 | } // end switch |
| 115 | } |
| 116 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 117 | const GRState *SimpleConstraintManager::Assume(const GRState *state, |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 118 | NonLoc cond, |
| 119 | bool assumption) { |
| 120 | state = AssumeAux(state, cond, assumption); |
| 121 | return SU.ProcessAssume(state, cond, assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 124 | const GRState *SimpleConstraintManager::AssumeAux(const GRState *state, |
| 125 | NonLoc Cond, |
| 126 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 128 | // We cannot reason about SymIntExpr and SymSymExpr. |
| 129 | if (!canReasonAbout(Cond)) { |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 130 | // Just return the current state indicating that the path is feasible. |
| 131 | // This may be an over-approximation of what is possible. |
| 132 | return state; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | } |
Zhongxing Xu | a129eb9 | 2009-03-25 05:58:37 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 135 | BasicValueFactory &BasicVals = state->getBasicVals(); |
| 136 | SymbolManager &SymMgr = state->getSymbolManager(); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 137 | |
| 138 | switch (Cond.getSubKind()) { |
| 139 | default: |
| 140 | assert(false && "'Assume' not implemented for this NonLoc"); |
| 141 | |
| 142 | case nonloc::SymbolValKind: { |
| 143 | nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond); |
| 144 | SymbolRef sym = SV.getSymbol(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | QualType T = SymMgr.getType(sym); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 146 | const llvm::APSInt &zero = BasicVals.getValue(0, T); |
| 147 | |
| 148 | return Assumption ? AssumeSymNE(state, sym, zero) |
| 149 | : AssumeSymEQ(state, sym, zero); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 152 | case nonloc::SymExprValKind: { |
| 153 | nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond); |
Ted Kremenek | 8041747 | 2009-09-25 00:18:15 +0000 | [diff] [blame] | 154 | if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression())){ |
| 155 | // FIXME: This is a hack. It silently converts the RHS integer to be |
| 156 | // of the same type as on the left side. This should be removed once |
| 157 | // we support truncation/extension of symbolic values. |
| 158 | GRStateManager &StateMgr = state->getStateManager(); |
| 159 | ASTContext &Ctx = StateMgr.getContext(); |
| 160 | QualType LHSType = SE->getLHS()->getType(Ctx); |
| 161 | BasicValueFactory &BasicVals = StateMgr.getBasicVals(); |
| 162 | const llvm::APSInt &RHS = BasicVals.Convert(LHSType, SE->getRHS()); |
| 163 | SymIntExpr SENew(SE->getLHS(), SE->getOpcode(), RHS, SE->getType(Ctx)); |
| 164 | |
| 165 | return AssumeSymInt(state, Assumption, &SENew); |
| 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 168 | // For all other symbolic expressions, over-approximate and consider |
| 169 | // the constraint feasible. |
| 170 | return state; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 171 | } |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 172 | |
| 173 | case nonloc::ConcreteIntKind: { |
| 174 | bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0; |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 175 | bool isFeasible = b ? Assumption : !Assumption; |
| 176 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | case nonloc::LocAsIntegerKind: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 180 | return AssumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(), |
| 181 | Assumption); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 182 | } // end switch |
| 183 | } |
| 184 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 185 | const GRState *SimpleConstraintManager::AssumeSymInt(const GRState *state, |
| 186 | bool Assumption, |
| 187 | const SymIntExpr *SE) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 189 | |
| 190 | // Here we assume that LHS is a symbol. This is consistent with the |
| 191 | // rest of the constraint manager logic. |
| 192 | SymbolRef Sym = cast<SymbolData>(SE->getLHS()); |
| 193 | const llvm::APSInt &Int = SE->getRHS(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 195 | switch (SE->getOpcode()) { |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 196 | default: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 197 | // No logic yet for other operators. Assume the constraint is feasible. |
| 198 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 199 | |
| 200 | case BinaryOperator::EQ: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 201 | return Assumption ? AssumeSymEQ(state, Sym, Int) |
| 202 | : AssumeSymNE(state, Sym, Int); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 203 | |
| 204 | case BinaryOperator::NE: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 205 | return Assumption ? AssumeSymNE(state, Sym, Int) |
| 206 | : AssumeSymEQ(state, Sym, Int); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 207 | case BinaryOperator::GT: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 208 | return Assumption ? AssumeSymGT(state, Sym, Int) |
| 209 | : AssumeSymLE(state, Sym, Int); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 210 | |
| 211 | case BinaryOperator::GE: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 212 | return Assumption ? AssumeSymGE(state, Sym, Int) |
| 213 | : AssumeSymLT(state, Sym, Int); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 214 | |
| 215 | case BinaryOperator::LT: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 216 | return Assumption ? AssumeSymLT(state, Sym, Int) |
| 217 | : AssumeSymGE(state, Sym, Int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 219 | case BinaryOperator::LE: |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 220 | return Assumption ? AssumeSymLE(state, Sym, Int) |
| 221 | : AssumeSymGT(state, Sym, Int); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 222 | } // end switch |
| 223 | } |
| 224 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 225 | const GRState *SimpleConstraintManager::AssumeInBound(const GRState *state, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 226 | DefinedSVal Idx, |
| 227 | DefinedSVal UpperBound, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | bool Assumption) { |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 230 | // Only support ConcreteInt for now. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 231 | if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))) |
| 232 | return state; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | f1b8227 | 2009-06-18 23:20:05 +0000 | [diff] [blame] | 234 | const llvm::APSInt& Zero = state->getBasicVals().getZeroWithPtrWidth(false); |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 235 | llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue(); |
| 236 | // IdxV might be too narrow. |
| 237 | if (IdxV.getBitWidth() < Zero.getBitWidth()) |
| 238 | IdxV.extend(Zero.getBitWidth()); |
| 239 | // UBV might be too narrow, too. |
| 240 | llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue(); |
| 241 | if (UBV.getBitWidth() < Zero.getBitWidth()) |
| 242 | UBV.extend(Zero.getBitWidth()); |
| 243 | |
| 244 | bool InBound = (Zero <= IdxV) && (IdxV < UBV); |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 245 | bool isFeasible = Assumption ? InBound : !InBound; |
| 246 | return isFeasible ? state : NULL; |
Ted Kremenek | 4502195 | 2009-02-14 17:08:39 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | } // end of namespace clang |