Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 1 | //=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- C++ -*-// |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 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 | // |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 10 | // This file defines BasicValueFactory, a class that manages the lifetime |
| 11 | // of APSInt objects and symbolic constraints used by GRExprEngine |
| 12 | // and related classes. |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/BasicValueFactory.h" |
Zhongxing Xu | 22ab7a4 | 2008-10-21 05:41:03 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/SVals.h" |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 21 | CompoundValData::CompoundValData(QualType t, const SVal* vals, unsigned n, |
| 22 | llvm::BumpPtrAllocator& A) |
| 23 | : T(t), NumVals(n) { |
| 24 | |
| 25 | Vals = (SVal*) A.Allocate<SVal>(n); |
| 26 | |
| 27 | new (Vals) SVal[n]; |
| 28 | |
| 29 | for (unsigned i = 0; i < n; ++i) |
| 30 | Vals[i] = vals[i]; |
| 31 | } |
| 32 | |
| 33 | void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T, |
| 34 | unsigned N, const SVal* Vals) { |
| 35 | T.Profile(ID); |
| 36 | ID.AddInteger(N); |
| 37 | for (unsigned i = 0; i < N; ++i) |
| 38 | Vals[i].Profile(ID); |
| 39 | } |
| 40 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 41 | typedef std::pair<SVal, uintptr_t> SValData; |
| 42 | typedef std::pair<SVal, SVal> SValPair; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 44 | |
| 45 | namespace llvm { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 46 | template<> struct FoldingSetTrait<SValData> { |
| 47 | static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) { |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 48 | X.first.Profile(ID); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 49 | ID.AddPointer( (void*) X.second); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 50 | } |
| 51 | }; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 52 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 53 | template<> struct FoldingSetTrait<SValPair> { |
| 54 | static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) { |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 55 | X.first.Profile(ID); |
| 56 | X.second.Profile(ID); |
| 57 | } |
| 58 | }; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 61 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> > |
| 62 | PersistentSValsTy; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 63 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 64 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> > |
| 65 | PersistentSValPairsTy; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 67 | BasicValueFactory::~BasicValueFactory() { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 68 | // Note that the dstor for the contents of APSIntSet will never be called, |
| 69 | // so we iterate over the set and invoke the dstor for each APSInt. This |
| 70 | // frees an aux. memory allocated to represent very large constants. |
| 71 | for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I) |
| 72 | I->getValue().~APSInt(); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 73 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 74 | delete (PersistentSValsTy*) PersistentSVals; |
| 75 | delete (PersistentSValPairsTy*) PersistentSValPairs; |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 78 | const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 79 | llvm::FoldingSetNodeID ID; |
| 80 | void* InsertPos; |
| 81 | typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy; |
| 82 | |
| 83 | X.Profile(ID); |
| 84 | FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos); |
| 85 | |
| 86 | if (!P) { |
| 87 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 88 | new (P) FoldNodeTy(X); |
| 89 | APSIntSet.InsertNode(P, InsertPos); |
| 90 | } |
| 91 | |
| 92 | return *P; |
| 93 | } |
| 94 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 95 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 96 | bool isUnsigned) { |
| 97 | llvm::APSInt V(BitWidth, isUnsigned); |
| 98 | V = X; |
| 99 | return getValue(V); |
| 100 | } |
| 101 | |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 102 | const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) { |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 104 | unsigned bits = Ctx.getTypeSize(T); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 105 | llvm::APSInt V(bits, T->isUnsignedIntegerType()); |
| 106 | V = X; |
| 107 | return getValue(V); |
| 108 | } |
| 109 | |
| 110 | const SymIntConstraint& |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 111 | BasicValueFactory::getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 112 | const llvm::APSInt& V) { |
| 113 | |
| 114 | llvm::FoldingSetNodeID ID; |
| 115 | SymIntConstraint::Profile(ID, sym, Op, V); |
| 116 | void* InsertPos; |
| 117 | |
| 118 | SymIntConstraint* C = SymIntCSet.FindNodeOrInsertPos(ID, InsertPos); |
| 119 | |
| 120 | if (!C) { |
| 121 | C = (SymIntConstraint*) BPAlloc.Allocate<SymIntConstraint>(); |
| 122 | new (C) SymIntConstraint(sym, Op, V); |
| 123 | SymIntCSet.InsertNode(C, InsertPos); |
| 124 | } |
| 125 | |
| 126 | return *C; |
| 127 | } |
| 128 | |
Zhongxing Xu | 6764b72 | 2008-10-30 04:58:00 +0000 | [diff] [blame] | 129 | const CompoundValData* |
| 130 | BasicValueFactory::getCompoundValData(QualType T, const SVal* Vals, |
| 131 | unsigned NumVals) { |
| 132 | llvm::FoldingSetNodeID ID; |
| 133 | CompoundValData::Profile(ID, T, NumVals, Vals); |
| 134 | void* InsertPos; |
| 135 | |
| 136 | CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos); |
| 137 | |
| 138 | if (!D) { |
| 139 | D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>(); |
| 140 | new (D) CompoundValData(T, Vals, NumVals, BPAlloc); |
| 141 | CompoundValDataSet.InsertNode(D, InsertPos); |
| 142 | } |
| 143 | |
| 144 | return D; |
| 145 | } |
| 146 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 147 | const llvm::APSInt* |
Ted Kremenek | 240f1f0 | 2008-03-07 20:13:31 +0000 | [diff] [blame] | 148 | BasicValueFactory::EvaluateAPSInt(BinaryOperator::Opcode Op, |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 149 | const llvm::APSInt& V1, const llvm::APSInt& V2) { |
| 150 | |
| 151 | switch (Op) { |
| 152 | default: |
| 153 | assert (false && "Invalid Opcode."); |
| 154 | |
| 155 | case BinaryOperator::Mul: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 156 | return &getValue( V1 * V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 157 | |
| 158 | case BinaryOperator::Div: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 159 | return &getValue( V1 / V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 160 | |
| 161 | case BinaryOperator::Rem: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 162 | return &getValue( V1 % V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 163 | |
| 164 | case BinaryOperator::Add: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 165 | return &getValue( V1 + V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 166 | |
| 167 | case BinaryOperator::Sub: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 168 | return &getValue( V1 - V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 170 | case BinaryOperator::Shl: { |
| 171 | |
| 172 | // FIXME: This logic should probably go higher up, where we can |
| 173 | // test these conditions symbolically. |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 175 | // FIXME: Expand these checks to include all undefined behavior. |
| 176 | |
| 177 | if (V2.isSigned() && V2.isNegative()) |
| 178 | return NULL; |
| 179 | |
| 180 | uint64_t Amt = V2.getZExtValue(); |
| 181 | |
| 182 | if (Amt > V1.getBitWidth()) |
| 183 | return NULL; |
| 184 | |
| 185 | return &getValue( V1.operator<<( (unsigned) Amt )); |
| 186 | } |
| 187 | |
| 188 | case BinaryOperator::Shr: { |
| 189 | |
| 190 | // FIXME: This logic should probably go higher up, where we can |
| 191 | // test these conditions symbolically. |
| 192 | |
| 193 | // FIXME: Expand these checks to include all undefined behavior. |
| 194 | |
| 195 | if (V2.isSigned() && V2.isNegative()) |
| 196 | return NULL; |
| 197 | |
| 198 | uint64_t Amt = V2.getZExtValue(); |
| 199 | |
| 200 | if (Amt > V1.getBitWidth()) |
| 201 | return NULL; |
| 202 | |
| 203 | return &getValue( V1.operator>>( (unsigned) Amt )); |
| 204 | } |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 205 | |
| 206 | case BinaryOperator::LT: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 207 | return &getTruthValue( V1 < V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 208 | |
| 209 | case BinaryOperator::GT: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 210 | return &getTruthValue( V1 > V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 211 | |
| 212 | case BinaryOperator::LE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 213 | return &getTruthValue( V1 <= V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 214 | |
| 215 | case BinaryOperator::GE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 216 | return &getTruthValue( V1 >= V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 217 | |
| 218 | case BinaryOperator::EQ: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 219 | return &getTruthValue( V1 == V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 220 | |
| 221 | case BinaryOperator::NE: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 222 | return &getTruthValue( V1 != V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 223 | |
| 224 | // Note: LAnd, LOr, Comma are handled specially by higher-level logic. |
| 225 | |
| 226 | case BinaryOperator::And: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 227 | return &getValue( V1 & V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 228 | |
| 229 | case BinaryOperator::Or: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 230 | return &getValue( V1 | V2 ); |
Ted Kremenek | 1caf26a | 2008-02-19 20:53:37 +0000 | [diff] [blame] | 231 | |
| 232 | case BinaryOperator::Xor: |
Ted Kremenek | 8cc13ea | 2008-02-28 20:32:03 +0000 | [diff] [blame] | 233 | return &getValue( V1 ^ V2 ); |
Ted Kremenek | d70d0b0 | 2008-02-16 01:12:31 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 236 | |
| 237 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 238 | const std::pair<SVal, uintptr_t>& |
| 239 | BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) { |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 240 | |
| 241 | // Lazily create the folding set. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 242 | if (!PersistentSVals) PersistentSVals = new PersistentSValsTy(); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 243 | |
| 244 | llvm::FoldingSetNodeID ID; |
| 245 | void* InsertPos; |
| 246 | V.Profile(ID); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 247 | ID.AddPointer((void*) Data); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 248 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 249 | PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 250 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 251 | typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 252 | FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos); |
| 253 | |
| 254 | if (!P) { |
| 255 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 256 | new (P) FoldNodeTy(std::make_pair(V, Data)); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 257 | Map.InsertNode(P, InsertPos); |
| 258 | } |
| 259 | |
Ted Kremenek | 718c4f7 | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 260 | return P->getValue(); |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 261 | } |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 262 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 263 | const std::pair<SVal, SVal>& |
| 264 | BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) { |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 265 | |
| 266 | // Lazily create the folding set. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 267 | if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy(); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 268 | |
| 269 | llvm::FoldingSetNodeID ID; |
| 270 | void* InsertPos; |
| 271 | V1.Profile(ID); |
| 272 | V2.Profile(ID); |
| 273 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 274 | PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs); |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 275 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 276 | typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy; |
Ted Kremenek | 4d0348b | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 277 | FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos); |
| 278 | |
| 279 | if (!P) { |
| 280 | P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>(); |
| 281 | new (P) FoldNodeTy(std::make_pair(V1, V2)); |
| 282 | Map.InsertNode(P, InsertPos); |
| 283 | } |
| 284 | |
| 285 | return P->getValue(); |
| 286 | } |
| 287 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 288 | const SVal* BasicValueFactory::getPersistentSVal(SVal X) { |
| 289 | return &getPersistentSValWithData(X, 0).first; |
Ted Kremenek | 7360fda | 2008-09-18 23:09:54 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |