Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1 | //== BasicStore.cpp - Basic map from Locations to Values --------*- 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 defined the BasicStore and BasicStoreManager classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ImmutableMap.h" |
| 17 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 21 | using store::Region; |
| 22 | using store::RegionExtent; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager { |
| 27 | typedef llvm::ImmutableMap<VarDecl*,RVal> VarBindingsTy; |
| 28 | VarBindingsTy::Factory VBFactory; |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 29 | GRStateManager& StMgr; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 30 | |
| 31 | public: |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 32 | BasicStoreManager(GRStateManager& mgr) : StMgr(mgr) {} |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 34 | virtual ~BasicStoreManager() {} |
| 35 | |
| 36 | virtual RVal GetRVal(Store St, LVal LV, QualType T); |
| 37 | virtual Store SetRVal(Store St, LVal LV, RVal V); |
| 38 | virtual Store Remove(Store St, LVal LV); |
| 39 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 40 | virtual Store getInitialStore(GRStateManager& StateMgr); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 41 | |
| 42 | virtual Store RemoveDeadBindings(Store store, Stmt* Loc, |
| 43 | const LiveVariables& Live, |
| 44 | DeclRootsTy& DRoots, LiveSymbolsTy& LSymbols, |
| 45 | DeadSymbolsTy& DSymbols); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 47 | virtual Store AddDecl(Store store, GRStateManager& StateMgr, |
| 48 | const VarDecl* VD, Expr* Ex, |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 49 | RVal InitVal = UndefinedVal(), unsigned Count = 0); |
| 50 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 51 | static inline VarBindingsTy GetVarBindings(Store store) { |
| 52 | return VarBindingsTy(static_cast<const VarBindingsTy::TreeTy*>(store)); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | virtual void print(Store store, std::ostream& Out, |
| 56 | const char* nl, const char *sep); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 58 | virtual RegionExtent getExtent(Region R); |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 59 | |
| 60 | /// getBindings - Returns all bindings in the specified store that bind |
| 61 | /// to the specified symbolic value. |
| 62 | virtual void getBindings(llvm::SmallVectorImpl<store::Binding>& bindings, |
| 63 | Store store, SymbolID Sym); |
| 64 | |
| 65 | /// BindingAsString - Returns a string representing the given binding. |
| 66 | virtual std::string BindingAsString(store::Binding binding); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // end anonymous namespace |
| 70 | |
| 71 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 72 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 73 | return new BasicStoreManager(StMgr); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 76 | RegionExtent BasicStoreManager::getExtent(Region R) { |
| 77 | VarDecl* VD = (VarDecl*) R; |
| 78 | QualType T = VD->getType(); |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 80 | // FIXME: Add support for VLAs. This may require passing in additional |
| 81 | // information, or tracking a different region type. |
| 82 | if (!T.getTypePtr()->isConstantSizeType()) |
| 83 | return store::UnknownExtent(); |
| 84 | |
| 85 | ASTContext& C = StMgr.getContext(); |
| 86 | assert (!T->isObjCInterfaceType()); // @interface not a possible VarDecl type. |
| 87 | assert (T != C.VoidTy); // void not a possible VarDecl type. |
| 88 | return store::IntExtent(StMgr.getBasicVals().getValue(C.getTypeSize(T), |
| 89 | C.VoidPtrTy)); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ted Kremenek | 5f81c44 | 2008-08-28 23:31:31 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 93 | RVal BasicStoreManager::GetRVal(Store St, LVal LV, QualType T) { |
| 94 | |
| 95 | if (isa<UnknownVal>(LV)) |
| 96 | return UnknownVal(); |
| 97 | |
| 98 | assert (!isa<UndefinedVal>(LV)); |
| 99 | |
| 100 | switch (LV.getSubKind()) { |
| 101 | |
| 102 | case lval::DeclValKind: { |
| 103 | VarBindingsTy B(static_cast<const VarBindingsTy::TreeTy*>(St)); |
| 104 | VarBindingsTy::data_type* T = B.lookup(cast<lval::DeclVal>(LV).getDecl()); |
| 105 | return T ? *T : UnknownVal(); |
| 106 | } |
| 107 | |
Ted Kremenek | d0c4b28 | 2008-08-25 19:33:03 +0000 | [diff] [blame] | 108 | case lval::SymbolValKind: |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 109 | return UnknownVal(); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 110 | |
| 111 | case lval::ConcreteIntKind: |
| 112 | // Some clients may call GetRVal with such an option simply because |
| 113 | // they are doing a quick scan through their LVals (potentially to |
| 114 | // invalidate their bindings). Just return Undefined. |
| 115 | return UndefinedVal(); |
| 116 | |
| 117 | case lval::ArrayOffsetKind: |
| 118 | case lval::FieldOffsetKind: |
| 119 | return UnknownVal(); |
| 120 | |
| 121 | case lval::FuncValKind: |
| 122 | return LV; |
| 123 | |
| 124 | case lval::StringLiteralValKind: |
| 125 | // FIXME: Implement better support for fetching characters from strings. |
| 126 | return UnknownVal(); |
| 127 | |
| 128 | default: |
| 129 | assert (false && "Invalid LVal."); |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return UnknownVal(); |
| 134 | } |
| 135 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 136 | Store BasicStoreManager::SetRVal(Store store, LVal LV, RVal V) { |
| 137 | switch (LV.getSubKind()) { |
| 138 | case lval::DeclValKind: { |
| 139 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 140 | return V.isUnknown() |
| 141 | ? VBFactory.Remove(B,cast<lval::DeclVal>(LV).getDecl()).getRoot() |
| 142 | : VBFactory.Add(B, cast<lval::DeclVal>(LV).getDecl(), V).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 143 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 144 | default: |
| 145 | assert ("SetRVal for given LVal type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 146 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 150 | Store BasicStoreManager::Remove(Store store, LVal LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 151 | switch (LV.getSubKind()) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 152 | case lval::DeclValKind: { |
| 153 | VarBindingsTy B = GetVarBindings(store); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 154 | return VBFactory.Remove(B,cast<lval::DeclVal>(LV).getDecl()).getRoot(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 155 | } |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 156 | default: |
| 157 | assert ("Remove for given LVal type not yet implemented."); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 158 | return store; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 161 | |
| 162 | Store BasicStoreManager::RemoveDeadBindings(Store store, |
| 163 | Stmt* Loc, |
| 164 | const LiveVariables& Liveness, |
| 165 | DeclRootsTy& DRoots, |
| 166 | LiveSymbolsTy& LSymbols, |
| 167 | DeadSymbolsTy& DSymbols) { |
| 168 | |
| 169 | VarBindingsTy B = GetVarBindings(store); |
| 170 | typedef RVal::symbol_iterator symbol_iterator; |
| 171 | |
| 172 | // Iterate over the variable bindings. |
| 173 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) |
| 174 | if (Liveness.isLive(Loc, I.getKey())) { |
| 175 | DRoots.push_back(I.getKey()); |
| 176 | RVal X = I.getData(); |
| 177 | |
| 178 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 179 | LSymbols.insert(*SI); |
| 180 | } |
| 181 | |
| 182 | // Scan for live variables and live symbols. |
| 183 | llvm::SmallPtrSet<ValueDecl*, 10> Marked; |
| 184 | |
| 185 | while (!DRoots.empty()) { |
| 186 | ValueDecl* V = DRoots.back(); |
| 187 | DRoots.pop_back(); |
| 188 | |
| 189 | if (Marked.count(V)) |
| 190 | continue; |
| 191 | |
| 192 | Marked.insert(V); |
| 193 | |
| 194 | RVal X = GetRVal(store, lval::DeclVal(cast<VarDecl>(V)), QualType()); |
| 195 | |
| 196 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 197 | LSymbols.insert(*SI); |
| 198 | |
| 199 | if (!isa<lval::DeclVal>(X)) |
| 200 | continue; |
| 201 | |
| 202 | const lval::DeclVal& LVD = cast<lval::DeclVal>(X); |
| 203 | DRoots.push_back(LVD.getDecl()); |
| 204 | } |
| 205 | |
| 206 | // Remove dead variable bindings. |
| 207 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) |
| 208 | if (!Marked.count(I.getKey())) { |
| 209 | store = Remove(store, lval::DeclVal(I.getKey())); |
| 210 | RVal X = I.getData(); |
| 211 | |
| 212 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 213 | if (!LSymbols.count(*SI)) DSymbols.insert(*SI); |
| 214 | } |
| 215 | |
| 216 | return store; |
| 217 | } |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 218 | |
| 219 | Store BasicStoreManager::getInitialStore(GRStateManager& StateMgr) { |
| 220 | // The LiveVariables information already has a compilation of all VarDecls |
| 221 | // used in the function. Iterate through this set, and "symbolicate" |
| 222 | // any VarDecl whose value originally comes from outside the function. |
| 223 | |
| 224 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 225 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 226 | |
| 227 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 228 | |
| 229 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
| 230 | ScopedDecl* SD = const_cast<ScopedDecl*>(I->first); |
| 231 | |
| 232 | if (VarDecl* VD = dyn_cast<VarDecl>(SD)) { |
| 233 | // Punt on static variables for now. |
| 234 | if (VD->getStorageClass() == VarDecl::Static) |
| 235 | continue; |
| 236 | |
| 237 | // Only handle pointers and integers for now. |
| 238 | QualType T = VD->getType(); |
| 239 | if (LVal::IsLValType(T) || T->isIntegerType()) { |
| 240 | // Initialize globals and parameters to symbolic values. |
| 241 | // Initialize local variables to undefined. |
| 242 | RVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
| 243 | isa<ImplicitParamDecl>(VD)) |
| 244 | ? RVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
| 245 | : UndefinedVal(); |
| 246 | |
| 247 | St = SetRVal(St, lval::DeclVal(VD), X); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | return St; |
| 252 | } |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 254 | Store BasicStoreManager::AddDecl(Store store, GRStateManager& StateMgr, |
| 255 | const VarDecl* VD, Expr* Ex, |
| 256 | RVal InitVal, unsigned Count) { |
| 257 | |
| 258 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 259 | SymbolManager& SymMgr = StateMgr.getSymbolManager(); |
| 260 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 261 | // BasicStore does not model arrays and structs. |
| 262 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 263 | return store; |
| 264 | |
| 265 | if (VD->hasGlobalStorage()) { |
| 266 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 267 | |
| 268 | // FIXME:: static variables may have an initializer, but the second time a |
| 269 | // function is called those values may not be current. Currently, a function |
| 270 | // will not be called more than once. |
| 271 | |
| 272 | // Static global variables should not be visited here. |
| 273 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 274 | VD->isFileVarDecl())); |
| 275 | |
| 276 | // Process static variables. |
| 277 | if (VD->getStorageClass() == VarDecl::Static) { |
| 278 | // C99: 6.7.8 Initialization |
| 279 | // If an object that has static storage duration is not initialized |
| 280 | // explicitly, then: |
| 281 | // —if it has pointer type, it is initialized to a null pointer; |
| 282 | // —if it has arithmetic type, it is initialized to (positive or |
| 283 | // unsigned) zero; |
| 284 | if (!Ex) { |
| 285 | QualType T = VD->getType(); |
| 286 | if (LVal::IsLValType(T)) |
| 287 | store = SetRVal(store, lval::DeclVal(VD), |
| 288 | lval::ConcreteInt(BasicVals.getValue(0, T))); |
| 289 | else if (T->isIntegerType()) |
| 290 | store = SetRVal(store, lval::DeclVal(VD), |
| 291 | nonlval::ConcreteInt(BasicVals.getValue(0, T))); |
| 292 | else { |
| 293 | // assert(0 && "ignore other types of variables"); |
| 294 | } |
| 295 | } else { |
| 296 | store = SetRVal(store, lval::DeclVal(VD), InitVal); |
| 297 | } |
| 298 | } |
| 299 | } else { |
| 300 | // Process local scalar variables. |
| 301 | QualType T = VD->getType(); |
| 302 | if (LVal::IsLValType(T) || T->isIntegerType()) { |
| 303 | RVal V = Ex ? InitVal : UndefinedVal(); |
| 304 | |
| 305 | if (Ex && InitVal.isUnknown()) { |
| 306 | // EXPERIMENTAL: "Conjured" symbols. |
| 307 | SymbolID Sym = SymMgr.getConjuredSymbol(Ex, Count); |
| 308 | |
| 309 | V = LVal::IsLValType(Ex->getType()) |
| 310 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 311 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
| 312 | } |
| 313 | |
| 314 | store = SetRVal(store, lval::DeclVal(VD), V); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | return store; |
| 319 | } |
| 320 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 321 | void BasicStoreManager::print(Store store, std::ostream& Out, |
| 322 | const char* nl, const char *sep) { |
| 323 | |
| 324 | VarBindingsTy B = GetVarBindings(store); |
| 325 | Out << "Variables:" << nl; |
| 326 | |
| 327 | bool isFirst = true; |
| 328 | |
| 329 | for (VarBindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
| 330 | if (isFirst) isFirst = false; |
| 331 | else Out << nl; |
| 332 | |
| 333 | Out << ' ' << I.getKey()->getName() << " : "; |
| 334 | I.getData().print(Out); |
| 335 | } |
| 336 | } |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 337 | |
| 338 | void |
| 339 | BasicStoreManager::getBindings(llvm::SmallVectorImpl<store::Binding>& bindings, |
| 340 | Store store, SymbolID Sym) { |
| 341 | |
| 342 | VarBindingsTy VB((VarBindingsTy::TreeTy*) store); |
| 343 | |
| 344 | for (VarBindingsTy::iterator I=VB.begin(), E=VB.end(); I!=E; ++I) { |
| 345 | if (const lval::SymbolVal* SV=dyn_cast<lval::SymbolVal>(&I->second)) { |
| 346 | if (SV->getSymbol() == Sym) |
| 347 | bindings.push_back(I->first); |
| 348 | |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | if (const nonlval::SymbolVal* SV=dyn_cast<nonlval::SymbolVal>(&I->second)){ |
| 353 | if (SV->getSymbol() == Sym) |
| 354 | bindings.push_back(I->first); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | std::string BasicStoreManager::BindingAsString(store::Binding binding) { |
| 360 | // A binding is just an VarDecl*. |
| 361 | return ((VarDecl*) binding)->getName(); |
| 362 | } |