clang API Documentation
00001 //== Store.cpp - Interface for maps from Locations to Values ----*- C++ -*--==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defined the types Store and StoreManager. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h" 00015 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 00016 #include "clang/AST/CharUnits.h" 00017 #include "clang/AST/DeclObjC.h" 00018 00019 using namespace clang; 00020 using namespace ento; 00021 00022 StoreManager::StoreManager(ProgramStateManager &stateMgr) 00023 : svalBuilder(stateMgr.getSValBuilder()), StateMgr(stateMgr), 00024 MRMgr(svalBuilder.getRegionManager()), Ctx(stateMgr.getContext()) {} 00025 00026 StoreRef StoreManager::enterStackFrame(ProgramStateRef state, 00027 const LocationContext *callerCtx, 00028 const StackFrameContext *calleeCtx) { 00029 return StoreRef(state->getStore(), *this); 00030 } 00031 00032 const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base, 00033 QualType EleTy, uint64_t index) { 00034 NonLoc idx = svalBuilder.makeArrayIndex(index); 00035 return MRMgr.getElementRegion(EleTy, idx, Base, svalBuilder.getContext()); 00036 } 00037 00038 // FIXME: Merge with the implementation of the same method in MemRegion.cpp 00039 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) { 00040 if (const RecordType *RT = Ty->getAs<RecordType>()) { 00041 const RecordDecl *D = RT->getDecl(); 00042 if (!D->getDefinition()) 00043 return false; 00044 } 00045 00046 return true; 00047 } 00048 00049 StoreRef StoreManager::BindDefault(Store store, const MemRegion *R, SVal V) { 00050 return StoreRef(store, *this); 00051 } 00052 00053 const ElementRegion *StoreManager::GetElementZeroRegion(const MemRegion *R, 00054 QualType T) { 00055 NonLoc idx = svalBuilder.makeZeroArrayIndex(); 00056 assert(!T.isNull()); 00057 return MRMgr.getElementRegion(T, idx, R, Ctx); 00058 } 00059 00060 const MemRegion *StoreManager::castRegion(const MemRegion *R, QualType CastToTy) { 00061 00062 ASTContext &Ctx = StateMgr.getContext(); 00063 00064 // Handle casts to Objective-C objects. 00065 if (CastToTy->isObjCObjectPointerType()) 00066 return R->StripCasts(); 00067 00068 if (CastToTy->isBlockPointerType()) { 00069 // FIXME: We may need different solutions, depending on the symbol 00070 // involved. Blocks can be casted to/from 'id', as they can be treated 00071 // as Objective-C objects. This could possibly be handled by enhancing 00072 // our reasoning of downcasts of symbolic objects. 00073 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R)) 00074 return R; 00075 00076 // We don't know what to make of it. Return a NULL region, which 00077 // will be interpretted as UnknownVal. 00078 return NULL; 00079 } 00080 00081 // Now assume we are casting from pointer to pointer. Other cases should 00082 // already be handled. 00083 QualType PointeeTy = CastToTy->getPointeeType(); 00084 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy); 00085 00086 // Handle casts to void*. We just pass the region through. 00087 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy) 00088 return R; 00089 00090 // Handle casts from compatible types. 00091 if (R->isBoundable()) 00092 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { 00093 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType()); 00094 if (CanonPointeeTy == ObjTy) 00095 return R; 00096 } 00097 00098 // Process region cast according to the kind of the region being cast. 00099 switch (R->getKind()) { 00100 case MemRegion::CXXThisRegionKind: 00101 case MemRegion::GenericMemSpaceRegionKind: 00102 case MemRegion::StackLocalsSpaceRegionKind: 00103 case MemRegion::StackArgumentsSpaceRegionKind: 00104 case MemRegion::HeapSpaceRegionKind: 00105 case MemRegion::UnknownSpaceRegionKind: 00106 case MemRegion::StaticGlobalSpaceRegionKind: 00107 case MemRegion::GlobalInternalSpaceRegionKind: 00108 case MemRegion::GlobalSystemSpaceRegionKind: 00109 case MemRegion::GlobalImmutableSpaceRegionKind: { 00110 llvm_unreachable("Invalid region cast"); 00111 } 00112 00113 case MemRegion::FunctionTextRegionKind: 00114 case MemRegion::BlockTextRegionKind: 00115 case MemRegion::BlockDataRegionKind: 00116 case MemRegion::StringRegionKind: 00117 // FIXME: Need to handle arbitrary downcasts. 00118 case MemRegion::SymbolicRegionKind: 00119 case MemRegion::AllocaRegionKind: 00120 case MemRegion::CompoundLiteralRegionKind: 00121 case MemRegion::FieldRegionKind: 00122 case MemRegion::ObjCIvarRegionKind: 00123 case MemRegion::ObjCStringRegionKind: 00124 case MemRegion::VarRegionKind: 00125 case MemRegion::CXXTempObjectRegionKind: 00126 case MemRegion::CXXBaseObjectRegionKind: 00127 return MakeElementRegion(R, PointeeTy); 00128 00129 case MemRegion::ElementRegionKind: { 00130 // If we are casting from an ElementRegion to another type, the 00131 // algorithm is as follows: 00132 // 00133 // (1) Compute the "raw offset" of the ElementRegion from the 00134 // base region. This is done by calling 'getAsRawOffset()'. 00135 // 00136 // (2a) If we get a 'RegionRawOffset' after calling 00137 // 'getAsRawOffset()', determine if the absolute offset 00138 // can be exactly divided into chunks of the size of the 00139 // casted-pointee type. If so, create a new ElementRegion with 00140 // the pointee-cast type as the new ElementType and the index 00141 // being the offset divded by the chunk size. If not, create 00142 // a new ElementRegion at offset 0 off the raw offset region. 00143 // 00144 // (2b) If we don't a get a 'RegionRawOffset' after calling 00145 // 'getAsRawOffset()', it means that we are at offset 0. 00146 // 00147 // FIXME: Handle symbolic raw offsets. 00148 00149 const ElementRegion *elementR = cast<ElementRegion>(R); 00150 const RegionRawOffset &rawOff = elementR->getAsArrayOffset(); 00151 const MemRegion *baseR = rawOff.getRegion(); 00152 00153 // If we cannot compute a raw offset, throw up our hands and return 00154 // a NULL MemRegion*. 00155 if (!baseR) 00156 return NULL; 00157 00158 CharUnits off = rawOff.getOffset(); 00159 00160 if (off.isZero()) { 00161 // Edge case: we are at 0 bytes off the beginning of baseR. We 00162 // check to see if type we are casting to is the same as the base 00163 // region. If so, just return the base region. 00164 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(baseR)) { 00165 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType()); 00166 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy); 00167 if (CanonPointeeTy == ObjTy) 00168 return baseR; 00169 } 00170 00171 // Otherwise, create a new ElementRegion at offset 0. 00172 return MakeElementRegion(baseR, PointeeTy); 00173 } 00174 00175 // We have a non-zero offset from the base region. We want to determine 00176 // if the offset can be evenly divided by sizeof(PointeeTy). If so, 00177 // we create an ElementRegion whose index is that value. Otherwise, we 00178 // create two ElementRegions, one that reflects a raw offset and the other 00179 // that reflects the cast. 00180 00181 // Compute the index for the new ElementRegion. 00182 int64_t newIndex = 0; 00183 const MemRegion *newSuperR = 0; 00184 00185 // We can only compute sizeof(PointeeTy) if it is a complete type. 00186 if (IsCompleteType(Ctx, PointeeTy)) { 00187 // Compute the size in **bytes**. 00188 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy); 00189 if (!pointeeTySize.isZero()) { 00190 // Is the offset a multiple of the size? If so, we can layer the 00191 // ElementRegion (with elementType == PointeeTy) directly on top of 00192 // the base region. 00193 if (off % pointeeTySize == 0) { 00194 newIndex = off / pointeeTySize; 00195 newSuperR = baseR; 00196 } 00197 } 00198 } 00199 00200 if (!newSuperR) { 00201 // Create an intermediate ElementRegion to represent the raw byte. 00202 // This will be the super region of the final ElementRegion. 00203 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity()); 00204 } 00205 00206 return MakeElementRegion(newSuperR, PointeeTy, newIndex); 00207 } 00208 } 00209 00210 llvm_unreachable("unreachable"); 00211 } 00212 00213 00214 /// CastRetrievedVal - Used by subclasses of StoreManager to implement 00215 /// implicit casts that arise from loads from regions that are reinterpreted 00216 /// as another region. 00217 SVal StoreManager::CastRetrievedVal(SVal V, const TypedValueRegion *R, 00218 QualType castTy, bool performTestOnly) { 00219 00220 if (castTy.isNull() || V.isUnknownOrUndef()) 00221 return V; 00222 00223 ASTContext &Ctx = svalBuilder.getContext(); 00224 00225 if (performTestOnly) { 00226 // Automatically translate references to pointers. 00227 QualType T = R->getValueType(); 00228 if (const ReferenceType *RT = T->getAs<ReferenceType>()) 00229 T = Ctx.getPointerType(RT->getPointeeType()); 00230 00231 assert(svalBuilder.getContext().hasSameUnqualifiedType(castTy, T)); 00232 return V; 00233 } 00234 00235 return svalBuilder.dispatchCast(V, castTy); 00236 } 00237 00238 SVal StoreManager::getLValueFieldOrIvar(const Decl *D, SVal Base) { 00239 if (Base.isUnknownOrUndef()) 00240 return Base; 00241 00242 Loc BaseL = cast<Loc>(Base); 00243 const MemRegion* BaseR = 0; 00244 00245 switch (BaseL.getSubKind()) { 00246 case loc::MemRegionKind: 00247 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion(); 00248 break; 00249 00250 case loc::GotoLabelKind: 00251 // These are anormal cases. Flag an undefined value. 00252 return UndefinedVal(); 00253 00254 case loc::ConcreteIntKind: 00255 // While these seem funny, this can happen through casts. 00256 // FIXME: What we should return is the field offset. For example, 00257 // add the field offset to the integer value. That way funny things 00258 // like this work properly: &(((struct foo *) 0xa)->f) 00259 return Base; 00260 00261 default: 00262 llvm_unreachable("Unhandled Base."); 00263 } 00264 00265 // NOTE: We must have this check first because ObjCIvarDecl is a subclass 00266 // of FieldDecl. 00267 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D)) 00268 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR)); 00269 00270 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR)); 00271 } 00272 00273 SVal StoreManager::getLValueIvar(const ObjCIvarDecl *decl, SVal base) { 00274 return getLValueFieldOrIvar(decl, base); 00275 } 00276 00277 SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset, 00278 SVal Base) { 00279 00280 // If the base is an unknown or undefined value, just return it back. 00281 // FIXME: For absolute pointer addresses, we just return that value back as 00282 // well, although in reality we should return the offset added to that 00283 // value. 00284 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base)) 00285 return Base; 00286 00287 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion(); 00288 00289 // Pointer of any type can be cast and used as array base. 00290 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion); 00291 00292 // Convert the offset to the appropriate size and signedness. 00293 Offset = cast<NonLoc>(svalBuilder.convertToArrayIndex(Offset)); 00294 00295 if (!ElemR) { 00296 // 00297 // If the base region is not an ElementRegion, create one. 00298 // This can happen in the following example: 00299 // 00300 // char *p = __builtin_alloc(10); 00301 // p[1] = 8; 00302 // 00303 // Observe that 'p' binds to an AllocaRegion. 00304 // 00305 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset, 00306 BaseRegion, Ctx)); 00307 } 00308 00309 SVal BaseIdx = ElemR->getIndex(); 00310 00311 if (!isa<nonloc::ConcreteInt>(BaseIdx)) 00312 return UnknownVal(); 00313 00314 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue(); 00315 00316 // Only allow non-integer offsets if the base region has no offset itself. 00317 // FIXME: This is a somewhat arbitrary restriction. We should be using 00318 // SValBuilder here to add the two offsets without checking their types. 00319 if (!isa<nonloc::ConcreteInt>(Offset)) { 00320 if (isa<ElementRegion>(BaseRegion->StripCasts())) 00321 return UnknownVal(); 00322 00323 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset, 00324 ElemR->getSuperRegion(), 00325 Ctx)); 00326 } 00327 00328 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue(); 00329 assert(BaseIdxI.isSigned()); 00330 00331 // Compute the new index. 00332 nonloc::ConcreteInt NewIdx(svalBuilder.getBasicValueFactory().getValue(BaseIdxI + 00333 OffI)); 00334 00335 // Construct the new ElementRegion. 00336 const MemRegion *ArrayR = ElemR->getSuperRegion(); 00337 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR, 00338 Ctx)); 00339 } 00340 00341 StoreManager::BindingsHandler::~BindingsHandler() {} 00342 00343 bool StoreManager::FindUniqueBinding::HandleBinding(StoreManager& SMgr, 00344 Store store, 00345 const MemRegion* R, 00346 SVal val) { 00347 SymbolRef SymV = val.getAsLocSymbol(); 00348 if (!SymV || SymV != Sym) 00349 return true; 00350 00351 if (Binding) { 00352 First = false; 00353 return false; 00354 } 00355 else 00356 Binding = R; 00357 00358 return true; 00359 } 00360 00361 void SubRegionMap::anchor() { } 00362 void SubRegionMap::Visitor::anchor() { }