| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 15 | #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h" |
| 16 | |
| 17 | namespace clang { |
| 18 | |
| 19 | namespace ento { |
| 20 | |
| 21 | RangedConstraintManager::~RangedConstraintManager() {} |
| 22 | |
| 23 | ProgramStateRef RangedConstraintManager::assumeSym(ProgramStateRef State, |
| 24 | SymbolRef Sym, |
| 25 | bool Assumption) { |
| 26 | |
| 27 | if (isa<SymbolData>(Sym)) { |
| 28 | return assumeSymUnsupported(State, Sym, Assumption); |
| 29 | |
| 30 | |
| 31 | } else if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(Sym)) { |
| 32 | |
| 33 | |
| 34 | BinaryOperator::Opcode op = SIE->getOpcode(); |
| 35 | if (BinaryOperator::isComparisonOp(op) && op != BO_Cmp) { |
| 36 | if (!Assumption) |
| 37 | op = BinaryOperator::negateComparisonOp(op); |
| 38 | |
| 39 | return assumeSymRel(State, SIE->getLHS(), op, SIE->getRHS()); |
| 40 | } |
| 41 | |
| 42 | } else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(Sym)) { |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | SymbolManager &SymMgr = getSymbolManager(); |
| 50 | BinaryOperator::Opcode Op = SSE->getOpcode(); |
| 51 | assert(BinaryOperator::isComparisonOp(Op)); |
| 52 | |
| 53 | |
| 54 | if (Loc::isLocType(SSE->getLHS()->getType()) && |
| 55 | Loc::isLocType(SSE->getRHS()->getType())) { |
| 56 | QualType DiffTy = SymMgr.getContext().getPointerDiffType(); |
| 57 | SymbolRef Subtraction = |
| 58 | SymMgr.getSymSymExpr(SSE->getRHS(), BO_Sub, SSE->getLHS(), DiffTy); |
| 59 | |
| 60 | const llvm::APSInt &Zero = getBasicVals().getValue(0, DiffTy); |
| 61 | Op = BinaryOperator::reverseComparisonOp(Op); |
| 62 | if (!Assumption) |
| 63 | Op = BinaryOperator::negateComparisonOp(Op); |
| 64 | return assumeSymRel(State, Subtraction, Op, Zero); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | return assumeSymUnsupported(State, Sym, Assumption); |
| 71 | } |
| 72 | |
| 73 | ProgramStateRef RangedConstraintManager::assumeSymInclusiveRange( |
| 74 | ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From, |
| 75 | const llvm::APSInt &To, bool InRange) { |
| 76 | |
| 77 | BasicValueFactory &BVF = getBasicVals(); |
| 78 | APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType()); |
| 79 | |
| 80 | llvm::APSInt Adjustment = WraparoundType.getZeroValue(); |
| 81 | SymbolRef AdjustedSym = Sym; |
| 82 | computeAdjustment(AdjustedSym, Adjustment); |
| 83 | |
| 84 | |
| 85 | APSIntType ComparisonType = std::max(WraparoundType, APSIntType(From)); |
| 86 | llvm::APSInt ConvertedFrom = ComparisonType.convert(From); |
| 87 | llvm::APSInt ConvertedTo = ComparisonType.convert(To); |
| 88 | |
| 89 | |
| 90 | if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() && |
| 91 | ComparisonType.isUnsigned() && !WraparoundType.isUnsigned()) |
| 92 | Adjustment.setIsSigned(false); |
| 93 | |
| 94 | if (InRange) |
| 95 | return assumeSymWithinInclusiveRange(State, AdjustedSym, ConvertedFrom, |
| 96 | ConvertedTo, Adjustment); |
| 97 | return assumeSymOutsideInclusiveRange(State, AdjustedSym, ConvertedFrom, |
| 98 | ConvertedTo, Adjustment); |
| 99 | } |
| 100 | |
| 101 | ProgramStateRef |
| 102 | RangedConstraintManager::assumeSymUnsupported(ProgramStateRef State, |
| 103 | SymbolRef Sym, bool Assumption) { |
| 104 | BasicValueFactory &BVF = getBasicVals(); |
| 105 | QualType T = Sym->getType(); |
| 106 | |
| 107 | |
| 108 | if (!T->isIntegralOrEnumerationType()) |
| 109 | return State; |
| 110 | |
| 111 | |
| 112 | const llvm::APSInt &Zero = BVF.getValue(0, T); |
| 113 | if (Assumption) |
| 114 | return assumeSymNE(State, Sym, Zero, Zero); |
| 115 | else |
| 116 | return assumeSymEQ(State, Sym, Zero, Zero); |
| 117 | } |
| 118 | |
| 119 | ProgramStateRef RangedConstraintManager::assumeSymRel(ProgramStateRef State, |
| 120 | SymbolRef Sym, |
| 121 | BinaryOperator::Opcode Op, |
| 122 | const llvm::APSInt &Int) { |
| 123 | (0) . __assert_fail ("BinaryOperator..isComparisonOp(Op) && \"Non-comparison ops should be rewritten as comparisons to zero.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/RangedConstraintManager.cpp", 124, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(BinaryOperator::isComparisonOp(Op) && |
| 124 | (0) . __assert_fail ("BinaryOperator..isComparisonOp(Op) && \"Non-comparison ops should be rewritten as comparisons to zero.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/RangedConstraintManager.cpp", 124, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Non-comparison ops should be rewritten as comparisons to zero."); |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | if (Int == 0 && (Op == BO_EQ || Op == BO_NE)) { |
| 132 | if (const BinarySymExpr *SE = dyn_cast<BinarySymExpr>(Sym)) |
| 133 | if (BinaryOperator::isComparisonOp(SE->getOpcode())) |
| 134 | return assumeSym(State, Sym, (Op == BO_NE ? true : false)); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | BasicValueFactory &BVF = getBasicVals(); |
| 139 | APSIntType WraparoundType = BVF.getAPSIntType(Sym->getType()); |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | llvm::APSInt Adjustment = WraparoundType.getZeroValue(); |
| 149 | computeAdjustment(Sym, Adjustment); |
| 150 | |
| 151 | |
| 152 | APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int)); |
| 153 | llvm::APSInt ConvertedInt = ComparisonType.convert(Int); |
| 154 | |
| 155 | |
| 156 | if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() && |
| 157 | ComparisonType.isUnsigned() && !WraparoundType.isUnsigned()) |
| 158 | Adjustment.setIsSigned(false); |
| 159 | |
| 160 | switch (Op) { |
| 161 | default: |
| 162 | llvm_unreachable("invalid operation not caught by assertion above"); |
| 163 | |
| 164 | case BO_EQ: |
| 165 | return assumeSymEQ(State, Sym, ConvertedInt, Adjustment); |
| 166 | |
| 167 | case BO_NE: |
| 168 | return assumeSymNE(State, Sym, ConvertedInt, Adjustment); |
| 169 | |
| 170 | case BO_GT: |
| 171 | return assumeSymGT(State, Sym, ConvertedInt, Adjustment); |
| 172 | |
| 173 | case BO_GE: |
| 174 | return assumeSymGE(State, Sym, ConvertedInt, Adjustment); |
| 175 | |
| 176 | case BO_LT: |
| 177 | return assumeSymLT(State, Sym, ConvertedInt, Adjustment); |
| 178 | |
| 179 | case BO_LE: |
| 180 | return assumeSymLE(State, Sym, ConvertedInt, Adjustment); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void RangedConstraintManager::computeAdjustment(SymbolRef &Sym, |
| 185 | llvm::APSInt &Adjustment) { |
| 186 | |
| 187 | if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(Sym)) { |
| 188 | BinaryOperator::Opcode Op = SE->getOpcode(); |
| 189 | if (Op == BO_Add || Op == BO_Sub) { |
| 190 | Sym = SE->getLHS(); |
| 191 | Adjustment = APSIntType(Adjustment).convert(SE->getRHS()); |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | if (Op == BO_Sub) |
| 197 | Adjustment = -Adjustment; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void *ProgramStateTrait<ConstraintRange>::GDMIndex() { |
| 203 | static int Index; |
| 204 | return &Index; |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |