Zhongxing Xu | d19e21b | 2008-08-29 15:09:12 +0000 | [diff] [blame] | 1 | //== BasicConstraintManager.cpp - Manage basic constraints.------*- 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 BasicConstraintManager, a class that tracks simple |
| 11 | // equality and inequality constraints on symbolic values of GRState. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/ConstraintManager.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRState.h" |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { class VISIBILITY_HIDDEN ConstNotEq {}; } |
| 26 | namespace { class VISIBILITY_HIDDEN ConstEq {}; } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 28 | typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy; |
| 29 | typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy; |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 30 | |
| 31 | static int ConstEqIndex = 0; |
| 32 | static int ConstNotEqIndex = 0; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 34 | namespace clang { |
| 35 | template<> |
| 36 | struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> { |
| 37 | static inline void* GDMIndex() { return &ConstNotEqIndex; } |
| 38 | }; |
| 39 | |
| 40 | template<> |
| 41 | struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> { |
| 42 | static inline void* GDMIndex() { return &ConstEqIndex; } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | namespace { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 47 | // BasicConstraintManager only tracks equality and inequality constraints of |
| 48 | // constants and integer variables. |
| 49 | class VISIBILITY_HIDDEN BasicConstraintManager : public ConstraintManager { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 50 | GRStateManager& StateMgr; |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 51 | GRState::IntSetTy::Factory ISetFactory; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 52 | public: |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 53 | BasicConstraintManager(GRStateManager& statemgr) |
| 54 | : StateMgr(statemgr), ISetFactory(statemgr.getAllocator()) {} |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 55 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 56 | virtual const GRState* Assume(const GRState* St, SVal Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 57 | bool Assumption, bool& isFeasible); |
| 58 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 59 | const GRState* Assume(const GRState* St, Loc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 60 | bool& isFeasible); |
| 61 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 62 | const GRState* AssumeAux(const GRState* St, Loc Cond,bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 63 | bool& isFeasible); |
| 64 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 65 | const GRState* Assume(const GRState* St, NonLoc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 66 | bool& isFeasible); |
| 67 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 68 | const GRState* AssumeAux(const GRState* St, NonLoc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 69 | bool& isFeasible); |
| 70 | |
| 71 | const GRState* AssumeSymInt(const GRState* St, bool Assumption, |
| 72 | const SymIntConstraint& C, bool& isFeasible); |
| 73 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 74 | const GRState* AssumeSymNE(const GRState* St, SymbolRef sym, |
Ted Kremenek | b2bf7cd | 2009-01-28 22:27:59 +0000 | [diff] [blame^] | 75 | const llvm::APSInt& V, bool& isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 77 | const GRState* AssumeSymEQ(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 78 | const llvm::APSInt& V, bool& isFeasible); |
| 79 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 80 | const GRState* AssumeSymLT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 81 | const llvm::APSInt& V, bool& isFeasible); |
| 82 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 83 | const GRState* AssumeSymGT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 84 | const llvm::APSInt& V, bool& isFeasible); |
| 85 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 86 | const GRState* AssumeSymGE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 87 | const llvm::APSInt& V, bool& isFeasible); |
| 88 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 89 | const GRState* AssumeSymLE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 90 | const llvm::APSInt& V, bool& isFeasible); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 91 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 92 | const GRState* AssumeInBound(const GRState* St, SVal Idx, SVal UpperBound, |
| 93 | bool Assumption, bool& isFeasible); |
| 94 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 95 | const GRState* AddEQ(const GRState* St, SymbolRef sym, const llvm::APSInt& V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 97 | const GRState* AddNE(const GRState* St, SymbolRef sym, const llvm::APSInt& V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 99 | const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym); |
| 100 | bool isNotEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) const; |
| 101 | bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) const; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 103 | const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper); |
| 104 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 105 | |
| 106 | void print(const GRState* St, std::ostream& Out, |
| 107 | const char* nl, const char *sep); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); } |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 111 | }; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 112 | |
| 113 | } // end anonymous namespace |
| 114 | |
| 115 | ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& StateMgr) |
| 116 | { |
| 117 | return new BasicConstraintManager(StateMgr); |
| 118 | } |
| 119 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 120 | const GRState* BasicConstraintManager::Assume(const GRState* St, SVal Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 121 | bool Assumption, bool& isFeasible) { |
| 122 | if (Cond.isUnknown()) { |
| 123 | isFeasible = true; |
| 124 | return St; |
| 125 | } |
| 126 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 127 | if (isa<NonLoc>(Cond)) |
| 128 | return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 129 | else |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 130 | return Assume(St, cast<Loc>(Cond), Assumption, isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 133 | const GRState* BasicConstraintManager::Assume(const GRState* St, Loc Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 134 | bool Assumption, bool& isFeasible) { |
| 135 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 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); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 147 | const GRState* BasicConstraintManager::AssumeAux(const GRState* St, Loc Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 148 | bool Assumption, bool& isFeasible) { |
| 149 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 150 | |
| 151 | switch (Cond.getSubKind()) { |
| 152 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 153 | assert (false && "'Assume' not implemented for this Loc."); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 154 | return St; |
| 155 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 156 | case loc::SymbolValKind: |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 157 | if (Assumption) |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 158 | return AssumeSymNE(St, cast<loc::SymbolVal>(Cond).getSymbol(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 159 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 160 | else |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 161 | return AssumeSymEQ(St, cast<loc::SymbolVal>(Cond).getSymbol(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 162 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 163 | |
Ted Kremenek | c523471 | 2008-10-17 21:22:20 +0000 | [diff] [blame] | 164 | case loc::MemRegionKind: { |
| 165 | // FIXME: Should this go into the storemanager? |
| 166 | |
| 167 | const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion(); |
| 168 | |
| 169 | while (R) { |
| 170 | if (const SubRegion* SubR = dyn_cast<SubRegion>(R)) { |
| 171 | R = SubR->getSuperRegion(); |
| 172 | continue; |
| 173 | } |
| 174 | else if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R)) |
| 175 | return AssumeAux(St, loc::SymbolVal(SymR->getSymbol()), Assumption, |
| 176 | isFeasible); |
| 177 | |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | // FALL-THROUGH. |
| 182 | } |
| 183 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 184 | case loc::FuncValKind: |
| 185 | case loc::GotoLabelKind: |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 186 | isFeasible = Assumption; |
| 187 | return St; |
| 188 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 189 | case loc::ConcreteIntKind: { |
| 190 | bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 191 | isFeasible = b ? Assumption : !Assumption; |
| 192 | return St; |
| 193 | } |
| 194 | } // end switch |
| 195 | } |
| 196 | |
| 197 | const GRState* |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 198 | BasicConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 199 | bool& isFeasible) { |
| 200 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
Ted Kremenek | 2fb78a7 | 2008-12-17 21:50:35 +0000 | [diff] [blame] | 201 | |
| 202 | if (!isFeasible) |
| 203 | return St; |
| 204 | |
| 205 | // EvalAssume is used to call into the GRTransferFunction object to perform |
| 206 | // any checker-specific update of the state based on this assumption being |
| 207 | // true or false. |
| 208 | return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption, |
| 209 | isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | const GRState* |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 213 | BasicConstraintManager::AssumeAux(const GRState* St,NonLoc Cond, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 214 | bool Assumption, bool& isFeasible) { |
| 215 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 216 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 217 | |
| 218 | switch (Cond.getSubKind()) { |
| 219 | default: |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 220 | assert(false && "'Assume' not implemented for this NonLoc"); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 221 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 222 | case nonloc::SymbolValKind: { |
| 223 | nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 224 | SymbolRef sym = SV.getSymbol(); |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 225 | QualType T = SymMgr.getType(sym); |
| 226 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 227 | if (Assumption) |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 228 | return AssumeSymNE(St, sym, BasicVals.getValue(0, T), isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 229 | else |
Ted Kremenek | 9ab6b9c | 2009-01-22 18:23:34 +0000 | [diff] [blame] | 230 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, T), isFeasible); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 233 | case nonloc::SymIntConstraintValKind: |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 234 | return |
| 235 | AssumeSymInt(St, Assumption, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 236 | cast<nonloc::SymIntConstraintVal>(Cond).getConstraint(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 237 | isFeasible); |
| 238 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 239 | case nonloc::ConcreteIntKind: { |
| 240 | bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 241 | isFeasible = b ? Assumption : !Assumption; |
| 242 | return St; |
| 243 | } |
| 244 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 245 | case nonloc::LocAsIntegerKind: |
| 246 | return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(), |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 247 | Assumption, isFeasible); |
| 248 | } // end switch |
| 249 | } |
| 250 | |
| 251 | const GRState* |
| 252 | BasicConstraintManager::AssumeSymInt(const GRState* St, bool Assumption, |
| 253 | const SymIntConstraint& C, bool& isFeasible) { |
| 254 | |
| 255 | switch (C.getOpcode()) { |
| 256 | default: |
| 257 | // No logic yet for other operators. |
| 258 | isFeasible = true; |
| 259 | return St; |
| 260 | |
| 261 | case BinaryOperator::EQ: |
| 262 | if (Assumption) |
| 263 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 264 | else |
| 265 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 266 | |
| 267 | case BinaryOperator::NE: |
| 268 | if (Assumption) |
| 269 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 270 | else |
| 271 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 272 | |
Zhongxing Xu | 94b8312 | 2008-09-19 06:07:59 +0000 | [diff] [blame] | 273 | case BinaryOperator::GT: |
| 274 | if (Assumption) |
| 275 | return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 276 | else |
| 277 | return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 278 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 279 | case BinaryOperator::GE: |
| 280 | if (Assumption) |
| 281 | return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 282 | else |
| 283 | return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 284 | |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 285 | case BinaryOperator::LT: |
| 286 | if (Assumption) |
| 287 | return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 288 | else |
| 289 | return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 290 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 291 | case BinaryOperator::LE: |
| 292 | if (Assumption) |
| 293 | return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 294 | else |
| 295 | return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 296 | } // end switch |
| 297 | } |
| 298 | |
| 299 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 300 | BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 301 | const llvm::APSInt& V, bool& isFeasible) { |
| 302 | // First, determine if sym == X, where X != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 303 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 304 | isFeasible = (*X != V); |
| 305 | return St; |
| 306 | } |
| 307 | |
| 308 | // Second, determine if sym != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 309 | if (isNotEqual(St, sym, V)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 310 | isFeasible = true; |
| 311 | return St; |
| 312 | } |
| 313 | |
| 314 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 315 | // Make that assumption. |
| 316 | isFeasible = true; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 317 | return AddNE(St, sym, V); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 321 | BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 322 | const llvm::APSInt& V, bool& isFeasible) { |
| 323 | // First, determine if sym == X, where X != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 324 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 325 | isFeasible = *X == V; |
| 326 | return St; |
| 327 | } |
| 328 | |
| 329 | // Second, determine if sym != V. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 330 | if (isNotEqual(St, sym, V)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 331 | isFeasible = false; |
| 332 | return St; |
| 333 | } |
| 334 | |
| 335 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 336 | // Make that assumption. |
| 337 | |
| 338 | isFeasible = true; |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 339 | return AddEQ(St, sym, V); |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // These logic will be handled in another ConstraintManager. |
| 343 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 344 | BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 345 | const llvm::APSInt& V, bool& isFeasible) { |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 346 | |
| 347 | // Is 'V' the smallest possible value? |
| 348 | if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isSigned())) { |
| 349 | // sym cannot be any value less than 'V'. This path is infeasible. |
| 350 | isFeasible = false; |
| 351 | return St; |
| 352 | } |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 353 | |
| 354 | // FIXME: For now have assuming x < y be the same as assuming sym != V; |
| 355 | return AssumeSymNE(St, sym, V, isFeasible); |
| 356 | } |
| 357 | |
| 358 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 359 | BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 360 | const llvm::APSInt& V, bool& isFeasible) { |
| 361 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 362 | // Is 'V' the largest possible value? |
| 363 | if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isSigned())) { |
| 364 | // sym cannot be any value greater than 'V'. This path is infeasible. |
| 365 | isFeasible = false; |
| 366 | return St; |
| 367 | } |
| 368 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 369 | // FIXME: For now have assuming x > y be the same as assuming sym != V; |
| 370 | return AssumeSymNE(St, sym, V, isFeasible); |
| 371 | } |
| 372 | |
| 373 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 374 | BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 375 | const llvm::APSInt& V, bool& isFeasible) { |
| 376 | |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 377 | // Reject a path if the value of sym is a constant X and !(X >= V). |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 378 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 379 | isFeasible = *X >= V; |
| 380 | return St; |
| 381 | } |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 382 | |
| 383 | // Sym is not a constant, but it is worth looking to see if V is the |
| 384 | // maximum integer value. |
| 385 | if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isSigned())) { |
| 386 | // If we know that sym != V, then this condition is infeasible since |
| 387 | // there is no other value greater than V. |
| 388 | isFeasible = !isNotEqual(St, sym, V); |
| 389 | |
| 390 | // If the path is still feasible then as a consequence we know that |
| 391 | // 'sym == V' because we cannot have 'sym > V' (no larger values). |
| 392 | // Add this constraint. |
| 393 | if (isFeasible) |
| 394 | return AddEQ(St, sym, V); |
| 395 | } |
| 396 | else |
| 397 | isFeasible = true; |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 398 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 399 | return St; |
| 400 | } |
| 401 | |
| 402 | const GRState* |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 403 | BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 404 | const llvm::APSInt& V, bool& isFeasible) { |
| 405 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 406 | // Reject a path if the value of sym is a constant X and !(X <= V). |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 407 | if (const llvm::APSInt* X = getSymVal(St, sym)) { |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 408 | isFeasible = *X <= V; |
| 409 | return St; |
| 410 | } |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 412 | // Sym is not a constant, but it is worth looking to see if V is the |
| 413 | // minimum integer value. |
| 414 | if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isSigned())) { |
| 415 | // If we know that sym != V, then this condition is infeasible since |
| 416 | // there is no other value less than V. |
| 417 | isFeasible = !isNotEqual(St, sym, V); |
| 418 | |
| 419 | // If the path is still feasible then as a consequence we know that |
| 420 | // 'sym == V' because we cannot have 'sym < V' (no smaller values). |
| 421 | // Add this constraint. |
| 422 | if (isFeasible) |
| 423 | return AddEQ(St, sym, V); |
| 424 | } |
| 425 | else |
| 426 | isFeasible = true; |
| 427 | |
Zhongxing Xu | 30ad167 | 2008-08-27 14:03:33 +0000 | [diff] [blame] | 428 | return St; |
| 429 | } |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 430 | |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 431 | const GRState* |
| 432 | BasicConstraintManager::AssumeInBound(const GRState* St, SVal Idx, |
| 433 | SVal UpperBound, bool Assumption, |
| 434 | bool& isFeasible) { |
| 435 | // Only support ConcreteInt for now. |
| 436 | if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))){ |
| 437 | isFeasible = true; |
| 438 | return St; |
| 439 | } |
| 440 | |
| 441 | const llvm::APSInt& Zero = getBasicVals().getZeroWithPtrWidth(false); |
Sebastian Redl | e95db4f | 2008-11-24 19:35:33 +0000 | [diff] [blame] | 442 | llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue(); |
| 443 | // IdxV might be too narrow. |
| 444 | if (IdxV.getBitWidth() < Zero.getBitWidth()) |
| 445 | IdxV.extend(Zero.getBitWidth()); |
| 446 | // UBV might be too narrow, too. |
| 447 | llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue(); |
| 448 | if (UBV.getBitWidth() < Zero.getBitWidth()) |
| 449 | UBV.extend(Zero.getBitWidth()); |
Zhongxing Xu | e8a964b | 2008-11-22 13:21:46 +0000 | [diff] [blame] | 450 | |
| 451 | bool InBound = (Zero <= IdxV) && (IdxV < UBV); |
| 452 | |
| 453 | isFeasible = Assumption ? InBound : !InBound; |
| 454 | |
| 455 | return St; |
| 456 | } |
| 457 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 458 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 459 | const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 460 | const llvm::APSInt& V) { |
| 461 | // Create a new state with the old binding replaced. |
| 462 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 463 | return state.set<ConstEq>(sym, &V); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 466 | const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 467 | const llvm::APSInt& V) { |
Zhongxing Xu | f0bc50e | 2008-11-27 06:08:40 +0000 | [diff] [blame] | 468 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 469 | GRStateRef state(St, StateMgr); |
| 470 | |
| 471 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 472 | ConstNotEqTy::data_type* T = state.get<ConstNotEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 473 | GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
| 474 | |
| 475 | |
| 476 | // Now add V to the NE set. |
| 477 | S = ISetFactory.Add(S, &V); |
| 478 | |
| 479 | // Create a new state with the old binding replaced. |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 480 | return state.set<ConstNotEq>(sym, S); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 484 | SymbolRef sym) { |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 485 | const ConstEqTy::data_type* T = St->get<ConstEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 486 | return T ? *T : NULL; |
| 487 | } |
| 488 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 489 | bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 490 | const llvm::APSInt& V) const { |
| 491 | |
| 492 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 493 | const ConstNotEqTy::data_type* T = St->get<ConstNotEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 494 | |
| 495 | // See if V is present in the NE-set. |
| 496 | return T ? T->contains(&V) : false; |
| 497 | } |
| 498 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 499 | bool BasicConstraintManager::isEqual(const GRState* St, SymbolRef sym, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 500 | const llvm::APSInt& V) const { |
| 501 | // Retrieve the EQ-set associated with the given symbol. |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 502 | const ConstEqTy::data_type* T = St->get<ConstEq>(sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 503 | // See if V is present in the EQ-set. |
| 504 | return T ? **T == V : false; |
| 505 | } |
| 506 | |
Zhongxing Xu | 8fd9b35 | 2008-11-27 02:39:34 +0000 | [diff] [blame] | 507 | /// Scan all symbols referenced by the constraints. If the symbol is not alive |
| 508 | /// as marked in LSymbols, mark it as dead in DSymbols. |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 509 | const GRState* |
| 510 | BasicConstraintManager::RemoveDeadBindings(const GRState* St, |
| 511 | SymbolReaper& SymReaper) { |
| 512 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 513 | GRStateRef state(St, StateMgr); |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 514 | ConstEqTy CE = state.get<ConstEq>(); |
| 515 | ConstEqTy::Factory& CEFactory = state.get_context<ConstEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 516 | |
| 517 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 518 | SymbolRef sym = I.getKey(); |
| 519 | if (SymReaper.maybeDead(sym)) CE = CEFactory.Remove(CE, sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 520 | } |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 521 | state = state.set<ConstEq>(CE); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 523 | ConstNotEqTy CNE = state.get<ConstNotEq>(); |
| 524 | ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 525 | |
| 526 | for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 527 | SymbolRef sym = I.getKey(); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 528 | if (SymReaper.maybeDead(sym)) CNE = CNEFactory.Remove(CNE, sym); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 531 | return state.set<ConstNotEq>(CNE); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void BasicConstraintManager::print(const GRState* St, std::ostream& Out, |
| 535 | const char* nl, const char *sep) { |
| 536 | // Print equality constraints. |
| 537 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 538 | ConstEqTy CE = St->get<ConstEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 539 | |
| 540 | if (!CE.isEmpty()) { |
| 541 | Out << nl << sep << "'==' constraints:"; |
| 542 | |
| 543 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 544 | Out << nl << " $" << I.getKey(); |
| 545 | llvm::raw_os_ostream OS(Out); |
| 546 | OS << " : " << *I.getData(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // Print != constraints. |
| 551 | |
Ted Kremenek | 8ee74d5 | 2009-01-26 06:04:53 +0000 | [diff] [blame] | 552 | ConstNotEqTy CNE = St->get<ConstNotEq>(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 553 | |
| 554 | if (!CNE.isEmpty()) { |
| 555 | Out << nl << sep << "'!=' constraints:"; |
| 556 | |
| 557 | for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) { |
| 558 | Out << nl << " $" << I.getKey() << " : "; |
| 559 | bool isFirst = true; |
| 560 | |
| 561 | GRState::IntSetTy::iterator J = I.getData().begin(), |
| 562 | EJ = I.getData().end(); |
| 563 | |
| 564 | for ( ; J != EJ; ++J) { |
| 565 | if (isFirst) isFirst = false; |
| 566 | else Out << ", "; |
| 567 | |
Zhongxing Xu | 7d94e26 | 2008-11-10 05:00:06 +0000 | [diff] [blame] | 568 | Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | } |
Daniel Dunbar | 0e194dd | 2008-08-30 02:06:22 +0000 | [diff] [blame] | 572 | } |