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