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