Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1 | //== RegionStore.cpp - Field-sensitive store model --------------*- 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 a basic region store model. In this model, we do have field |
| 11 | // sensitivity. But we assume nothing about the heap shape. So recursive data |
| 12 | // structures are largely ignored. Basically we do 1-limiting analysis. |
| 13 | // Parameter pointers are assumed with no aliasing. Pointee objects of |
| 14 | // parameters are created lazily. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
| 19 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 20 | |
| 21 | #include "llvm/ADT/ImmutableMap.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 26 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 31 | RegionBindingsTy::Factory RBFactory; |
| 32 | GRStateManager& StateMgr; |
| 33 | MemRegionManager MRMgr; |
| 34 | |
| 35 | public: |
| 36 | RegionStoreManager(GRStateManager& mgr) |
| 37 | : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {} |
| 38 | |
| 39 | virtual ~RegionStoreManager() {} |
| 40 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame^] | 41 | MemRegionManager& getRegionManager() { return MRMgr; } |
| 42 | |
| 43 | // FIXME: Is this function necessary? |
| 44 | SVal GetRegionSVal(Store St, const MemRegion* R) { |
| 45 | return Retrieve(St, loc::MemRegionVal(R)); |
| 46 | } |
| 47 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 48 | SVal getLValueVar(const GRState* St, const VarDecl* VD); |
| 49 | |
| 50 | SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base); |
| 51 | |
| 52 | SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D); |
| 53 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 54 | SVal getLValueElement(const GRState* St, SVal Base, SVal Offset); |
| 55 | |
| 56 | SVal ArrayToPointer(SVal Array); |
| 57 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame^] | 58 | SVal Retrieve(Store S, Loc L, QualType T = QualType()); |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 59 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 60 | Store Bind(Store St, Loc LV, SVal V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 61 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame^] | 62 | Store Remove(Store store, Loc LV) { |
| 63 | // FIXME: Implement. |
| 64 | return store; |
| 65 | } |
| 66 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 67 | Store getInitialStore(); |
| 68 | |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame^] | 69 | Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live, |
| 70 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots, |
| 71 | LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) { |
| 72 | // FIXME: Implement this. |
| 73 | return store; |
| 74 | } |
| 75 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 76 | Store AddDecl(Store store, const VarDecl* VD, Expr* Ex, SVal InitVal, |
| 77 | unsigned Count); |
| 78 | |
| 79 | Loc getVarLoc(const VarDecl* VD) { |
| 80 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 81 | } |
| 82 | |
| 83 | Loc getElementLoc(const VarDecl* VD, SVal Idx); |
| 84 | |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 85 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 86 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 87 | } |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame^] | 88 | |
| 89 | void print(Store store, std::ostream& Out, const char* nl, const char *sep) { |
| 90 | // FIXME: Implement. |
| 91 | } |
| 92 | |
| 93 | void iterBindings(Store store, BindingsHandler& f) { |
| 94 | // FIXME: Implement. |
| 95 | } |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // end anonymous namespace |
| 99 | |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 100 | StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) { |
Zhongxing Xu | e4b6fc2 | 2008-10-24 01:38:55 +0000 | [diff] [blame^] | 101 | return new RegionStoreManager(StMgr); |
Ted Kremenek | c380399 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 104 | Loc RegionStoreManager::getElementLoc(const VarDecl* VD, SVal Idx) { |
| 105 | MemRegion* R = MRMgr.getVarRegion(VD); |
| 106 | ElementRegion* ER = MRMgr.getElementRegion(Idx, R); |
| 107 | return loc::MemRegionVal(ER); |
| 108 | } |
| 109 | |
Zhongxing Xu | 6f1b515 | 2008-10-22 13:44:38 +0000 | [diff] [blame] | 110 | SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) { |
| 111 | return loc::MemRegionVal(MRMgr.getVarRegion(VD)); |
| 112 | } |
| 113 | |
| 114 | SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D, |
| 115 | SVal Base) { |
| 116 | return UnknownVal(); |
| 117 | } |
| 118 | |
| 119 | SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base, |
| 120 | const FieldDecl* D) { |
| 121 | if (Base.isUnknownOrUndef()) |
| 122 | return Base; |
| 123 | |
| 124 | Loc BaseL = cast<Loc>(Base); |
| 125 | const MemRegion* BaseR = 0; |
| 126 | |
| 127 | switch (BaseL.getSubKind()) { |
| 128 | case loc::MemRegionKind: |
| 129 | BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); |
| 130 | break; |
| 131 | |
| 132 | case loc::SymbolValKind: |
| 133 | BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol()); |
| 134 | break; |
| 135 | |
| 136 | case loc::GotoLabelKind: |
| 137 | case loc::FuncValKind: |
| 138 | // These are anormal cases. Flag an undefined value. |
| 139 | return UndefinedVal(); |
| 140 | |
| 141 | case loc::ConcreteIntKind: |
| 142 | case loc::StringLiteralValKind: |
| 143 | // While these seem funny, this can happen through casts. |
| 144 | // FIXME: What we should return is the field offset. For example, |
| 145 | // add the field offset to the integer value. That way funny things |
| 146 | // like this work properly: &(((struct foo *) 0xa)->f) |
| 147 | return Base; |
| 148 | |
| 149 | default: |
| 150 | assert("Unhandled Base."); |
| 151 | return Base; |
| 152 | } |
| 153 | |
| 154 | return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR)); |
| 155 | } |
| 156 | |
Zhongxing Xu | 0972d0a | 2008-10-24 01:09:32 +0000 | [diff] [blame] | 157 | SVal RegionStoreManager::getLValueElement(const GRState* St, |
| 158 | SVal Base, SVal Offset) { |
| 159 | if (Base.isUnknownOrUndef()) |
| 160 | return Base; |
| 161 | |
| 162 | loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base); |
| 163 | |
| 164 | // We expect BaseR is an ElementRegion, not a base VarRegion. |
| 165 | |
| 166 | const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion()); |
| 167 | |
| 168 | SVal Idx = ElemR->getIndex(); |
| 169 | |
| 170 | nonloc::ConcreteInt *CI1, *CI2; |
| 171 | |
| 172 | // Only handle integer indices for now. |
| 173 | if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) && |
| 174 | (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) { |
| 175 | SVal NewIdx = CI1->EvalBinOp(StateMgr.getBasicVals(), BinaryOperator::Add, |
| 176 | *CI2); |
| 177 | return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx, |
| 178 | ElemR->getSuperRegion())); |
| 179 | } |
| 180 | |
| 181 | return UnknownVal(); |
| 182 | } |
| 183 | |
| 184 | // Cast 'pointer to array' to 'pointer to the first element of array'. |
| 185 | |
| 186 | SVal RegionStoreManager::ArrayToPointer(SVal Array) { |
| 187 | const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion(); |
| 188 | |
| 189 | const VarDecl* D = cast<VarRegion>(ArrayR)->getDecl(); |
| 190 | |
| 191 | if (const ConstantArrayType* CAT = |
| 192 | dyn_cast<ConstantArrayType>(D->getType().getTypePtr())) { |
| 193 | |
| 194 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 195 | |
| 196 | nonloc::ConcreteInt Idx(BasicVals.getValue(0, CAT->getSize().getBitWidth(), |
| 197 | false)); |
| 198 | |
| 199 | ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR); |
| 200 | |
| 201 | return loc::MemRegionVal(ER); |
| 202 | } |
| 203 | |
| 204 | return Array; |
| 205 | } |
| 206 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 207 | SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) { |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 208 | assert(!isa<UnknownVal>(L) && "location unknown"); |
| 209 | assert(!isa<UndefinedVal>(L) && "location undefined"); |
| 210 | |
| 211 | switch (L.getSubKind()) { |
| 212 | case loc::MemRegionKind: { |
| 213 | const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion(); |
| 214 | assert(R && "bad region"); |
| 215 | |
| 216 | RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S)); |
| 217 | RegionBindingsTy::data_type* V = B.lookup(R); |
| 218 | return V ? *V : UnknownVal(); |
| 219 | } |
| 220 | |
| 221 | case loc::SymbolValKind: |
| 222 | return UnknownVal(); |
| 223 | |
| 224 | case loc::ConcreteIntKind: |
| 225 | return UndefinedVal(); // As in BasicStoreManager. |
| 226 | |
| 227 | case loc::FuncValKind: |
| 228 | return L; |
| 229 | |
| 230 | case loc::StringLiteralValKind: |
| 231 | return UnknownVal(); |
| 232 | |
| 233 | default: |
| 234 | assert(false && "Invalid Location"); |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 239 | Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) { |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 240 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 241 | |
Ted Kremenek | 38a4b4b | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 242 | const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 243 | |
| 244 | if (!R) |
| 245 | return store; |
| 246 | |
| 247 | RegionBindingsTy B = GetRegionBindings(store); |
| 248 | return V.isUnknown() |
| 249 | ? RBFactory.Remove(B, R).getRoot() |
| 250 | : RBFactory.Add(B, R, V).getRoot(); |
| 251 | } |
| 252 | |
| 253 | Store RegionStoreManager::getInitialStore() { |
| 254 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 255 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 256 | |
| 257 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 258 | |
| 259 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 260 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 262 | if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 263 | // Punt on static variables for now. |
| 264 | if (VD->getStorageClass() == VarDecl::Static) |
| 265 | continue; |
| 266 | |
| 267 | QualType T = VD->getType(); |
| 268 | // Only handle pointers and integers for now. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 269 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 270 | // Initialize globals and parameters to symbolic values. |
| 271 | // Initialize local variables to undefined. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 272 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 273 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 274 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 275 | : UndefinedVal(); |
| 276 | |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 277 | St = Bind(St, getVarLoc(VD), X); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | return St; |
| 282 | } |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 283 | |
| 284 | Store RegionStoreManager::AddDecl(Store store, |
| 285 | const VarDecl* VD, Expr* Ex, |
| 286 | SVal InitVal, unsigned Count) { |
| 287 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 288 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 289 | |
| 290 | if (VD->hasGlobalStorage()) { |
| 291 | // Static global variables should not be visited here. |
| 292 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 293 | VD->isFileVarDecl())); |
| 294 | // Process static variables. |
| 295 | if (VD->getStorageClass() == VarDecl::Static) { |
| 296 | if (!Ex) { |
| 297 | // Only handle pointer and integer static variables. |
| 298 | |
| 299 | QualType T = VD->getType(); |
| 300 | |
| 301 | if (Loc::IsLocType(T)) |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 302 | store = Bind(store, getVarLoc(VD), |
| 303 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 304 | |
| 305 | else if (T->isIntegerType()) |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 306 | store = Bind(store, getVarLoc(VD), |
| 307 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 308 | else |
| 309 | assert("ignore other types of variables"); |
| 310 | } else { |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 311 | store = Bind(store, getVarLoc(VD), InitVal); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } else { |
| 315 | // Process local variables. |
| 316 | |
| 317 | QualType T = VD->getType(); |
| 318 | |
| 319 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
| 320 | SVal V = Ex ? InitVal : UndefinedVal(); |
| 321 | if (Ex && InitVal.isUnknown()) { |
| 322 | // "Conjured" symbols. |
| 323 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 324 | V = Loc::IsLocType(Ex->getType()) |
| 325 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 326 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
| 327 | } |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 328 | store = Bind(store, getVarLoc(VD), V); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 329 | |
| 330 | } else if (T->isArrayType()) { |
| 331 | // Only handle constant size array. |
| 332 | if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) { |
| 333 | |
| 334 | llvm::APInt Size = CAT->getSize(); |
| 335 | |
| 336 | for (llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth()); |
| 337 | i != Size; ++i) { |
| 338 | nonloc::ConcreteInt Idx(BasicVals.getValue(llvm::APSInt(i))); |
Zhongxing Xu | 7324932 | 2008-10-21 06:27:32 +0000 | [diff] [blame] | 339 | store = Bind(store, getElementLoc(VD, Idx), UndefinedVal()); |
Zhongxing Xu | e3954d1 | 2008-10-21 05:29:26 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | } else if (T->isStructureType()) { |
| 343 | // FIXME: Implement struct initialization. |
| 344 | } |
| 345 | } |
| 346 | return store; |
| 347 | } |
| 348 | |