Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [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 | |
| 14 | #include "clang/AST/ExprObjC.h" |
| 15 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 16 | #include "clang/Analysis/AnalysisContext.h" |
| 17 | #include "clang/Checker/PathSensitive/GRState.h" |
| 18 | #include "llvm/ADT/ImmutableMap.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class BasicStoreSubRegionMap : public SubRegionMap { |
| 27 | public: |
| 28 | BasicStoreSubRegionMap() {} |
| 29 | |
| 30 | bool iterSubRegions(const MemRegion* R, Visitor& V) const { |
| 31 | return true; // Do nothing. No subregions. |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | class BasicStoreManager : public StoreManager { |
| 36 | BindingsTy::Factory VBFactory; |
| 37 | public: |
| 38 | BasicStoreManager(GRStateManager& mgr) |
| 39 | : StoreManager(mgr), VBFactory(mgr.getAllocator()) {} |
| 40 | |
| 41 | ~BasicStoreManager() {} |
| 42 | |
| 43 | SubRegionMap *getSubRegionMap(Store store) { |
| 44 | return new BasicStoreSubRegionMap(); |
| 45 | } |
| 46 | |
| 47 | SVal Retrieve(Store store, Loc loc, QualType T = QualType()); |
| 48 | |
| 49 | Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E, |
| 50 | unsigned Count, InvalidatedSymbols *IS); |
| 51 | |
| 52 | Store scanForIvars(Stmt *B, const Decl* SelfDecl, |
| 53 | const MemRegion *SelfRegion, Store St); |
| 54 | |
| 55 | Store Bind(Store St, Loc loc, SVal V); |
| 56 | Store Remove(Store St, Loc loc); |
| 57 | Store getInitialStore(const LocationContext *InitLoc); |
| 58 | |
| 59 | // FIXME: Investigate what is using this. This method should be removed. |
| 60 | virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) { |
| 61 | return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC)); |
| 62 | } |
| 63 | |
| 64 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr*, |
| 65 | const LocationContext*, SVal val) { |
| 66 | return store; |
| 67 | } |
| 68 | |
| 69 | /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit |
| 70 | /// conversions between arrays and pointers. |
| 71 | SVal ArrayToPointer(Loc Array) { return Array; } |
| 72 | |
| 73 | /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values. |
| 74 | /// It updatees the GRState object in place with the values removed. |
| 75 | Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper, |
| 76 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots); |
| 77 | |
| 78 | void iterBindings(Store store, BindingsHandler& f); |
| 79 | |
| 80 | Store BindDecl(Store store, const VarRegion *VR, SVal InitVal) { |
| 81 | return BindDeclInternal(store, VR, &InitVal); |
| 82 | } |
| 83 | |
| 84 | Store BindDeclWithNoInit(Store store, const VarRegion *VR) { |
| 85 | return BindDeclInternal(store, VR, 0); |
| 86 | } |
| 87 | |
| 88 | Store BindDeclInternal(Store store, const VarRegion *VR, SVal *InitVal); |
| 89 | |
| 90 | static inline BindingsTy GetBindings(Store store) { |
| 91 | return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store)); |
| 92 | } |
| 93 | |
| 94 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 95 | const char *sep); |
| 96 | |
| 97 | private: |
| 98 | ASTContext& getContext() { return StateMgr.getContext(); } |
| 99 | }; |
| 100 | |
| 101 | } // end anonymous namespace |
| 102 | |
| 103 | |
| 104 | StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) { |
| 105 | return new BasicStoreManager(StMgr); |
| 106 | } |
| 107 | |
| 108 | static bool isHigherOrderRawPtr(QualType T, ASTContext &C) { |
| 109 | bool foundPointer = false; |
| 110 | while (1) { |
| 111 | const PointerType *PT = T->getAs<PointerType>(); |
| 112 | if (!PT) { |
| 113 | if (!foundPointer) |
| 114 | return false; |
| 115 | |
| 116 | // intptr_t* or intptr_t**, etc? |
| 117 | if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy)) |
| 118 | return true; |
| 119 | |
| 120 | QualType X = C.getCanonicalType(T).getUnqualifiedType(); |
| 121 | return X == C.VoidTy; |
| 122 | } |
| 123 | |
| 124 | foundPointer = true; |
| 125 | T = PT->getPointeeType(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | SVal BasicStoreManager::Retrieve(Store store, Loc loc, QualType T) { |
| 130 | if (isa<UnknownVal>(loc)) |
| 131 | return UnknownVal(); |
| 132 | |
| 133 | assert(!isa<UndefinedVal>(loc)); |
| 134 | |
| 135 | switch (loc.getSubKind()) { |
| 136 | |
| 137 | case loc::MemRegionKind: { |
| 138 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
| 139 | |
| 140 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
| 141 | return UnknownVal(); |
| 142 | |
| 143 | BindingsTy B = GetBindings(store); |
| 144 | BindingsTy::data_type *Val = B.lookup(R); |
| 145 | |
| 146 | if (!Val) |
| 147 | break; |
| 148 | |
| 149 | return CastRetrievedVal(*Val, cast<TypedRegion>(R), T); |
| 150 | } |
| 151 | |
| 152 | case loc::ConcreteIntKind: |
| 153 | // Some clients may call GetSVal with such an option simply because |
| 154 | // they are doing a quick scan through their Locs (potentially to |
| 155 | // invalidate their bindings). Just return Undefined. |
| 156 | return UndefinedVal(); |
| 157 | |
| 158 | default: |
| 159 | assert (false && "Invalid Loc."); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return UnknownVal(); |
| 164 | } |
| 165 | |
| 166 | Store BasicStoreManager::Bind(Store store, Loc loc, SVal V) { |
| 167 | if (isa<loc::ConcreteInt>(loc)) |
| 168 | return store; |
| 169 | |
| 170 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
| 171 | ASTContext &C = StateMgr.getContext(); |
| 172 | |
| 173 | // Special case: handle store of pointer values (Loc) to pointers via |
| 174 | // a cast to intXX_t*, void*, etc. This is needed to handle |
| 175 | // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier. |
| 176 | if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V)) |
| 177 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 178 | // FIXME: Should check for index 0. |
| 179 | QualType T = ER->getLocationType(C); |
| 180 | |
| 181 | if (isHigherOrderRawPtr(T, C)) |
| 182 | R = ER->getSuperRegion(); |
| 183 | } |
| 184 | |
| 185 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
| 186 | return store; |
| 187 | |
| 188 | const TypedRegion *TyR = cast<TypedRegion>(R); |
| 189 | |
| 190 | // Do not bind to arrays. We need to explicitly check for this so that |
| 191 | // we do not encounter any weirdness of trying to load/store from arrays. |
| 192 | if (TyR->isBoundable() && TyR->getValueType(C)->isArrayType()) |
| 193 | return store; |
| 194 | |
| 195 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) { |
| 196 | // Only convert 'V' to a location iff the underlying region type |
| 197 | // is a location as well. |
| 198 | // FIXME: We are allowing a store of an arbitrary location to |
| 199 | // a pointer. We may wish to flag a type error here if the types |
| 200 | // are incompatible. This may also cause lots of breakage |
| 201 | // elsewhere. Food for thought. |
| 202 | if (TyR->isBoundable() && Loc::IsLocType(TyR->getValueType(C))) |
| 203 | V = X->getLoc(); |
| 204 | } |
| 205 | |
| 206 | BindingsTy B = GetBindings(store); |
| 207 | return V.isUnknown() |
| 208 | ? VBFactory.Remove(B, R).getRoot() |
| 209 | : VBFactory.Add(B, R, V).getRoot(); |
| 210 | } |
| 211 | |
| 212 | Store BasicStoreManager::Remove(Store store, Loc loc) { |
| 213 | switch (loc.getSubKind()) { |
| 214 | case loc::MemRegionKind: { |
| 215 | const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion(); |
| 216 | |
| 217 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
| 218 | return store; |
| 219 | |
| 220 | return VBFactory.Remove(GetBindings(store), R).getRoot(); |
| 221 | } |
| 222 | default: |
| 223 | assert ("Remove for given Loc type not yet implemented."); |
| 224 | return store; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | Store BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc, |
| 229 | SymbolReaper& SymReaper, |
| 230 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots) |
| 231 | { |
| 232 | BindingsTy B = GetBindings(store); |
| 233 | typedef SVal::symbol_iterator symbol_iterator; |
| 234 | |
| 235 | // Iterate over the variable bindings. |
| 236 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
| 237 | if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) { |
| 238 | if (SymReaper.isLive(Loc, VR)) |
| 239 | RegionRoots.push_back(VR); |
| 240 | else |
| 241 | continue; |
| 242 | } |
| 243 | else if (isa<ObjCIvarRegion>(I.getKey())) { |
| 244 | RegionRoots.push_back(I.getKey()); |
| 245 | } |
| 246 | else |
| 247 | continue; |
| 248 | |
| 249 | // Mark the bindings in the data as live. |
| 250 | SVal X = I.getData(); |
| 251 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 252 | SymReaper.markLive(*SI); |
| 253 | } |
| 254 | |
| 255 | // Scan for live variables and live symbols. |
| 256 | llvm::SmallPtrSet<const MemRegion*, 10> Marked; |
| 257 | |
| 258 | while (!RegionRoots.empty()) { |
| 259 | const MemRegion* MR = RegionRoots.back(); |
| 260 | RegionRoots.pop_back(); |
| 261 | |
| 262 | while (MR) { |
| 263 | if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) { |
| 264 | SymReaper.markLive(SymR->getSymbol()); |
| 265 | break; |
| 266 | } |
| 267 | else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) { |
| 268 | if (Marked.count(MR)) |
| 269 | break; |
| 270 | |
| 271 | Marked.insert(MR); |
| 272 | SVal X = Retrieve(store, loc::MemRegionVal(MR)); |
| 273 | |
| 274 | // FIXME: We need to handle symbols nested in region definitions. |
| 275 | for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI) |
| 276 | SymReaper.markLive(*SI); |
| 277 | |
| 278 | if (!isa<loc::MemRegionVal>(X)) |
| 279 | break; |
| 280 | |
| 281 | const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X); |
| 282 | RegionRoots.push_back(LVD.getRegion()); |
| 283 | break; |
| 284 | } |
| 285 | else if (const SubRegion* R = dyn_cast<SubRegion>(MR)) |
| 286 | MR = R->getSuperRegion(); |
| 287 | else |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Remove dead variable bindings. |
| 293 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) { |
| 294 | const MemRegion* R = I.getKey(); |
| 295 | |
| 296 | if (!Marked.count(R)) { |
| 297 | store = Remove(store, ValMgr.makeLoc(R)); |
| 298 | SVal X = I.getData(); |
| 299 | |
| 300 | for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI) |
| 301 | SymReaper.maybeDead(*SI); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return store; |
| 306 | } |
| 307 | |
| 308 | Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, |
| 309 | const MemRegion *SelfRegion, Store St) { |
| 310 | for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end(); |
| 311 | CI != CE; ++CI) { |
| 312 | |
| 313 | if (!*CI) |
| 314 | continue; |
| 315 | |
| 316 | // Check if the statement is an ivar reference. We only |
| 317 | // care about self.ivar. |
| 318 | if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) { |
| 319 | const Expr *Base = IV->getBase()->IgnoreParenCasts(); |
| 320 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) { |
| 321 | if (DR->getDecl() == SelfDecl) { |
| 322 | const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(), |
| 323 | SelfRegion); |
| 324 | SVal X = ValMgr.getRegionValueSymbolVal(IVR); |
| 325 | St = Bind(St, ValMgr.makeLoc(IVR), X); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | else |
| 330 | St = scanForIvars(*CI, SelfDecl, SelfRegion, St); |
| 331 | } |
| 332 | |
| 333 | return St; |
| 334 | } |
| 335 | |
| 336 | Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) { |
| 337 | // The LiveVariables information already has a compilation of all VarDecls |
| 338 | // used in the function. Iterate through this set, and "symbolicate" |
| 339 | // any VarDecl whose value originally comes from outside the function. |
| 340 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 341 | LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData(); |
| 342 | Store St = VBFactory.GetEmptyMap().getRoot(); |
| 343 | |
| 344 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
| 345 | NamedDecl* ND = const_cast<NamedDecl*>(I->first); |
| 346 | |
| 347 | // Handle implicit parameters. |
| 348 | if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) { |
| 349 | const Decl& CD = *InitLoc->getDecl(); |
| 350 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) { |
| 351 | if (MD->getSelfDecl() == PD) { |
| 352 | // FIXME: Add type constraints (when they become available) to |
| 353 | // SelfRegion? (i.e., it implements MD->getClassInterface()). |
| 354 | const MemRegion *VR = MRMgr.getVarRegion(PD, InitLoc); |
| 355 | const MemRegion *SelfRegion = |
| 356 | ValMgr.getRegionValueSymbolVal(VR).getAsRegion(); |
| 357 | assert(SelfRegion); |
| 358 | St = Bind(St, ValMgr.makeLoc(VR), loc::MemRegionVal(SelfRegion)); |
| 359 | // Scan the method for ivar references. While this requires an |
| 360 | // entire AST scan, the cost should not be high in practice. |
| 361 | St = scanForIvars(MD->getBody(), PD, SelfRegion, St); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) { |
| 366 | // Only handle simple types that we can symbolicate. |
| 367 | if (!SymbolManager::canSymbolicate(VD->getType())) |
| 368 | continue; |
| 369 | |
| 370 | // Initialize globals and parameters to symbolic values. |
| 371 | // Initialize local variables to undefined. |
| 372 | const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc); |
| 373 | SVal X = UndefinedVal(); |
| 374 | if (R->hasGlobalsOrParametersStorage()) |
| 375 | X = ValMgr.getRegionValueSymbolVal(R); |
| 376 | |
| 377 | St = Bind(St, ValMgr.makeLoc(R), X); |
| 378 | } |
| 379 | } |
| 380 | return St; |
| 381 | } |
| 382 | |
| 383 | Store BasicStoreManager::BindDeclInternal(Store store, const VarRegion* VR, |
| 384 | SVal* InitVal) { |
| 385 | |
| 386 | BasicValueFactory& BasicVals = StateMgr.getBasicVals(); |
| 387 | const VarDecl *VD = VR->getDecl(); |
| 388 | |
| 389 | // BasicStore does not model arrays and structs. |
| 390 | if (VD->getType()->isArrayType() || VD->getType()->isStructureType()) |
| 391 | return store; |
| 392 | |
| 393 | if (VD->hasGlobalStorage()) { |
| 394 | // Handle variables with global storage: extern, static, PrivateExtern. |
| 395 | |
| 396 | // FIXME:: static variables may have an initializer, but the second time a |
| 397 | // function is called those values may not be current. Currently, a function |
| 398 | // will not be called more than once. |
| 399 | |
| 400 | // Static global variables should not be visited here. |
| 401 | assert(!(VD->getStorageClass() == VarDecl::Static && |
| 402 | VD->isFileVarDecl())); |
| 403 | |
| 404 | // Process static variables. |
| 405 | if (VD->getStorageClass() == VarDecl::Static) { |
| 406 | // C99: 6.7.8 Initialization |
| 407 | // If an object that has static storage duration is not initialized |
| 408 | // explicitly, then: |
| 409 | // —if it has pointer type, it is initialized to a null pointer; |
| 410 | // —if it has arithmetic type, it is initialized to (positive or |
| 411 | // unsigned) zero; |
| 412 | if (!InitVal) { |
| 413 | QualType T = VD->getType(); |
| 414 | if (Loc::IsLocType(T)) |
| 415 | store = Bind(store, loc::MemRegionVal(VR), |
| 416 | loc::ConcreteInt(BasicVals.getValue(0, T))); |
| 417 | else if (T->isIntegerType()) |
| 418 | store = Bind(store, loc::MemRegionVal(VR), |
| 419 | nonloc::ConcreteInt(BasicVals.getValue(0, T))); |
| 420 | else { |
| 421 | // assert(0 && "ignore other types of variables"); |
| 422 | } |
| 423 | } else { |
| 424 | store = Bind(store, loc::MemRegionVal(VR), *InitVal); |
| 425 | } |
| 426 | } |
| 427 | } else { |
| 428 | // Process local scalar variables. |
| 429 | QualType T = VD->getType(); |
| 430 | if (ValMgr.getSymbolManager().canSymbolicate(T)) { |
| 431 | SVal V = InitVal ? *InitVal : UndefinedVal(); |
| 432 | store = Bind(store, loc::MemRegionVal(VR), V); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return store; |
| 437 | } |
| 438 | |
| 439 | void BasicStoreManager::print(Store store, llvm::raw_ostream& Out, |
| 440 | const char* nl, const char *sep) { |
| 441 | |
| 442 | BindingsTy B = GetBindings(store); |
| 443 | Out << "Variables:" << nl; |
| 444 | |
| 445 | bool isFirst = true; |
| 446 | |
| 447 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) { |
| 448 | if (isFirst) |
| 449 | isFirst = false; |
| 450 | else |
| 451 | Out << nl; |
| 452 | |
| 453 | Out << ' ' << I.getKey() << " : " << I.getData(); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) { |
| 459 | BindingsTy B = GetBindings(store); |
| 460 | |
| 461 | for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) |
| 462 | f.HandleBinding(*this, store, I.getKey(), I.getData()); |
| 463 | |
| 464 | } |
| 465 | |
| 466 | StoreManager::BindingsHandler::~BindingsHandler() {} |
| 467 | |
| 468 | //===----------------------------------------------------------------------===// |
| 469 | // Binding invalidation. |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | |
| 472 | Store BasicStoreManager::InvalidateRegion(Store store, |
| 473 | const MemRegion *R, |
| 474 | const Expr *E, |
| 475 | unsigned Count, |
| 476 | InvalidatedSymbols *IS) { |
| 477 | R = R->StripCasts(); |
| 478 | |
| 479 | if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R))) |
| 480 | return store; |
| 481 | |
| 482 | if (IS) { |
| 483 | BindingsTy B = GetBindings(store); |
| 484 | if (BindingsTy::data_type *Val = B.lookup(R)) { |
| 485 | if (SymbolRef Sym = Val->getAsSymbol()) |
| 486 | IS->insert(Sym); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | QualType T = cast<TypedRegion>(R)->getValueType(R->getContext()); |
| 491 | SVal V = ValMgr.getConjuredSymbolVal(R, E, T, Count); |
| 492 | return Bind(store, loc::MemRegionVal(R), V); |
| 493 | } |
| 494 | |