SimpleConstraintManager.cpp revision 8569281fb7ce9b5ca164a0528b876acbb45eb989
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/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 18#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 19 20namespace clang { 21 22namespace ento { 23 24SimpleConstraintManager::~SimpleConstraintManager() {} 25 26bool SimpleConstraintManager::canReasonAbout(SVal X) const { 27 Optional<nonloc::SymbolVal> SymVal = X.getAs<nonloc::SymbolVal>(); 28 if (SymVal && SymVal->isExpression()) { 29 const SymExpr *SE = SymVal->getSymbol(); 30 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 BO_And: 35 case BO_Or: 36 case BO_Xor: 37 return false; 38 // We don't reason yet about these arithmetic constraints on 39 // symbolic values. 40 case BO_Mul: 41 case BO_Div: 42 case BO_Rem: 43 case BO_Shl: 44 case BO_Shr: 45 return false; 46 // All other cases. 47 default: 48 return true; 49 } 50 } 51 52 if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) { 53 if (SSE->getOpcode() == BO_EQ || SSE->getOpcode() == BO_NE) 54 return true; 55 } 56 57 return false; 58 } 59 60 return true; 61} 62 63ProgramStateRef SimpleConstraintManager::assume(ProgramStateRef state, 64 DefinedSVal Cond, 65 bool Assumption) { 66 if (Optional<NonLoc> NV = Cond.getAs<NonLoc>()) 67 return assume(state, *NV, Assumption); 68 return assume(state, Cond.castAs<Loc>(), Assumption); 69} 70 71ProgramStateRef SimpleConstraintManager::assume(ProgramStateRef state, Loc cond, 72 bool assumption) { 73 state = assumeAux(state, cond, assumption); 74 if (NotifyAssumeClients && SU) 75 return SU->processAssume(state, cond, assumption); 76 return state; 77} 78 79ProgramStateRef SimpleConstraintManager::assumeAux(ProgramStateRef state, 80 Loc Cond, bool Assumption) { 81 switch (Cond.getSubKind()) { 82 default: 83 assert (false && "'Assume' not implemented for this Loc."); 84 return state; 85 86 case loc::MemRegionKind: { 87 // FIXME: Should this go into the storemanager? 88 89 const MemRegion *R = Cond.castAs<loc::MemRegionVal>().getRegion(); 90 const SubRegion *SubR = dyn_cast<SubRegion>(R); 91 92 while (SubR) { 93 // FIXME: now we only find the first symbolic region. 94 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) { 95 const llvm::APSInt &zero = getBasicVals().getZeroWithPtrWidth(); 96 if (Assumption) 97 return assumeSymNE(state, SymR->getSymbol(), zero, zero); 98 else 99 return assumeSymEQ(state, SymR->getSymbol(), zero, zero); 100 } 101 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion()); 102 } 103 104 // FALL-THROUGH. 105 } 106 107 case loc::GotoLabelKind: 108 return Assumption ? state : NULL; 109 110 case loc::ConcreteIntKind: { 111 bool b = Cond.castAs<loc::ConcreteInt>().getValue() != 0; 112 bool isFeasible = b ? Assumption : !Assumption; 113 return isFeasible ? state : NULL; 114 } 115 } // end switch 116} 117 118ProgramStateRef SimpleConstraintManager::assume(ProgramStateRef state, 119 NonLoc cond, 120 bool assumption) { 121 state = assumeAux(state, cond, assumption); 122 if (NotifyAssumeClients && SU) 123 return SU->processAssume(state, cond, assumption); 124 return state; 125} 126 127 128ProgramStateRef 129SimpleConstraintManager::assumeAuxForSymbol(ProgramStateRef State, 130 SymbolRef Sym, bool Assumption) { 131 BasicValueFactory &BVF = getBasicVals(); 132 QualType T = Sym->getType(); 133 134 // None of the constraint solvers currently support non-integer types. 135 if (!T->isIntegerType()) 136 return State; 137 138 const llvm::APSInt &zero = BVF.getValue(0, T); 139 if (Assumption) 140 return assumeSymNE(State, Sym, zero, zero); 141 else 142 return assumeSymEQ(State, Sym, zero, zero); 143} 144 145ProgramStateRef SimpleConstraintManager::assumeAux(ProgramStateRef state, 146 NonLoc Cond, 147 bool Assumption) { 148 149 // We cannot reason about SymSymExprs, and can only reason about some 150 // SymIntExprs. 151 if (!canReasonAbout(Cond)) { 152 // Just add the constraint to the expression without trying to simplify. 153 SymbolRef sym = Cond.getAsSymExpr(); 154 return assumeAuxForSymbol(state, sym, Assumption); 155 } 156 157 switch (Cond.getSubKind()) { 158 default: 159 llvm_unreachable("'Assume' not implemented for this NonLoc"); 160 161 case nonloc::SymbolValKind: { 162 nonloc::SymbolVal SV = Cond.castAs<nonloc::SymbolVal>(); 163 SymbolRef sym = SV.getSymbol(); 164 assert(sym); 165 166 // Handle SymbolData. 167 if (!SV.isExpression()) { 168 return assumeAuxForSymbol(state, sym, Assumption); 169 170 // Handle symbolic expression. 171 } else if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(sym)) { 172 // We can only simplify expressions whose RHS is an integer. 173 174 BinaryOperator::Opcode op = SE->getOpcode(); 175 if (BinaryOperator::isComparisonOp(op)) { 176 if (!Assumption) 177 op = BinaryOperator::negateComparisonOp(op); 178 179 return assumeSymRel(state, SE->getLHS(), op, SE->getRHS()); 180 } 181 182 } else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(sym)) { 183 BinaryOperator::Opcode Op = SSE->getOpcode(); 184 185 // Translate "a != b" to "(b - a) != 0". 186 // We invert the order of the operands as a heuristic for how loop 187 // conditions are usually written ("begin != end") as compared to length 188 // calculations ("end - begin"). The more correct thing to do would be to 189 // canonicalize "a - b" and "b - a", which would allow us to treat 190 // "a != b" and "b != a" the same. 191 if (BinaryOperator::isEqualityOp(Op)) { 192 SymbolManager &SymMgr = getSymbolManager(); 193 194 assert(Loc::isLocType(SSE->getLHS()->getType())); 195 assert(Loc::isLocType(SSE->getRHS()->getType())); 196 QualType DiffTy = SymMgr.getContext().getPointerDiffType(); 197 SymbolRef Subtraction = SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, 198 SSE->getLHS(), DiffTy); 199 200 Assumption ^= (SSE->getOpcode() == BO_EQ); 201 return assumeAuxForSymbol(state, Subtraction, Assumption); 202 } 203 } 204 205 // If we get here, there's nothing else we can do but treat the symbol as 206 // opaque. 207 return assumeAuxForSymbol(state, sym, Assumption); 208 } 209 210 case nonloc::ConcreteIntKind: { 211 bool b = Cond.castAs<nonloc::ConcreteInt>().getValue() != 0; 212 bool isFeasible = b ? Assumption : !Assumption; 213 return isFeasible ? state : NULL; 214 } 215 216 case nonloc::LocAsIntegerKind: 217 return assumeAux(state, Cond.castAs<nonloc::LocAsInteger>().getLoc(), 218 Assumption); 219 } // end switch 220} 221 222static void computeAdjustment(SymbolRef &Sym, llvm::APSInt &Adjustment) { 223 // Is it a "($sym+constant1)" expression? 224 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) { 225 BinaryOperator::Opcode Op = SE->getOpcode(); 226 if (Op == BO_Add || Op == BO_Sub) { 227 Sym = SE->getLHS(); 228 Adjustment = APSIntType(Adjustment).convert(SE->getRHS()); 229 230 // Don't forget to negate the adjustment if it's being subtracted. 231 // This should happen /after/ promotion, in case the value being 232 // subtracted is, say, CHAR_MIN, and the promoted type is 'int'. 233 if (Op == BO_Sub) 234 Adjustment = -Adjustment; 235 } 236 } 237} 238 239ProgramStateRef SimpleConstraintManager::assumeSymRel(ProgramStateRef state, 240 const SymExpr *LHS, 241 BinaryOperator::Opcode op, 242 const llvm::APSInt& Int) { 243 assert(BinaryOperator::isComparisonOp(op) && 244 "Non-comparison ops should be rewritten as comparisons to zero."); 245 246 // Get the type used for calculating wraparound. 247 BasicValueFactory &BVF = getBasicVals(); 248 APSIntType WraparoundType = BVF.getAPSIntType(LHS->getType()); 249 250 // We only handle simple comparisons of the form "$sym == constant" 251 // or "($sym+constant1) == constant2". 252 // The adjustment is "constant1" in the above expression. It's used to 253 // "slide" the solution range around for modular arithmetic. For example, 254 // x < 4 has the solution [0, 3]. x+2 < 4 has the solution [0-2, 3-2], which 255 // in modular arithmetic is [0, 1] U [UINT_MAX-1, UINT_MAX]. It's up to 256 // the subclasses of SimpleConstraintManager to handle the adjustment. 257 SymbolRef Sym = LHS; 258 llvm::APSInt Adjustment = WraparoundType.getZeroValue(); 259 computeAdjustment(Sym, Adjustment); 260 261 // Convert the right-hand side integer as necessary. 262 APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int)); 263 llvm::APSInt ConvertedInt = ComparisonType.convert(Int); 264 265 switch (op) { 266 default: 267 // No logic yet for other operators. assume the constraint is feasible. 268 return state; 269 270 case BO_EQ: 271 return assumeSymEQ(state, Sym, ConvertedInt, Adjustment); 272 273 case BO_NE: 274 return assumeSymNE(state, Sym, ConvertedInt, Adjustment); 275 276 case BO_GT: 277 return assumeSymGT(state, Sym, ConvertedInt, Adjustment); 278 279 case BO_GE: 280 return assumeSymGE(state, Sym, ConvertedInt, Adjustment); 281 282 case BO_LT: 283 return assumeSymLT(state, Sym, ConvertedInt, Adjustment); 284 285 case BO_LE: 286 return assumeSymLE(state, Sym, ConvertedInt, Adjustment); 287 } // end switch 288} 289 290} // end of namespace ento 291 292} // end of namespace clang 293