1 //== Store.cpp - Interface for maps from Locations to Values ----*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defined the types Store and StoreManager.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Checker/PathSensitive/Store.h"
15 #include "clang/Checker/PathSensitive/GRState.h"
16 #include "clang/AST/CharUnits.h"
18 using namespace clang;
20 StoreManager::StoreManager(GRStateManager &stateMgr)
21 : ValMgr(stateMgr.getValueManager()), StateMgr(stateMgr),
22 MRMgr(ValMgr.getRegionManager()), Ctx(stateMgr.getContext()) {}
24 const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
25 QualType EleTy, uint64_t index) {
26 SVal idx = ValMgr.makeArrayIndex(index);
27 return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext());
30 // FIXME: Merge with the implementation of the same method in MemRegion.cpp
31 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
32 if (const RecordType *RT = Ty->getAs<RecordType>()) {
33 const RecordDecl *D = RT->getDecl();
34 if (!D->getDefinition())
41 const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
43 ASTContext& Ctx = StateMgr.getContext();
45 // Handle casts to Objective-C objects.
46 if (CastToTy->isObjCObjectPointerType())
47 return R->StripCasts();
49 if (CastToTy->isBlockPointerType()) {
50 // FIXME: We may need different solutions, depending on the symbol
51 // involved. Blocks can be casted to/from 'id', as they can be treated
52 // as Objective-C objects. This could possibly be handled by enhancing
53 // our reasoning of downcasts of symbolic objects.
54 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
57 // We don't know what to make of it. Return a NULL region, which
58 // will be interpretted as UnknownVal.
62 // Now assume we are casting from pointer to pointer. Other cases should
63 // already be handled.
64 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
65 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
67 // Handle casts to void*. We just pass the region through.
68 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
71 // Handle casts from compatible types.
73 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
74 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
75 if (CanonPointeeTy == ObjTy)
79 // Process region cast according to the kind of the region being cast.
80 switch (R->getKind()) {
81 case MemRegion::CXXThisRegionKind:
82 case MemRegion::GenericMemSpaceRegionKind:
83 case MemRegion::StackLocalsSpaceRegionKind:
84 case MemRegion::StackArgumentsSpaceRegionKind:
85 case MemRegion::HeapSpaceRegionKind:
86 case MemRegion::UnknownSpaceRegionKind:
87 case MemRegion::GlobalsSpaceRegionKind: {
88 assert(0 && "Invalid region cast");
92 case MemRegion::FunctionTextRegionKind:
93 case MemRegion::BlockTextRegionKind:
94 case MemRegion::BlockDataRegionKind: {
95 // CodeTextRegion should be cast to only a function or block pointer type,
96 // although they can in practice be casted to anything, e.g, void*, char*,
98 // Just return the region.
102 case MemRegion::StringRegionKind:
103 // FIXME: Need to handle arbitrary downcasts.
104 case MemRegion::SymbolicRegionKind:
105 case MemRegion::AllocaRegionKind:
106 case MemRegion::CompoundLiteralRegionKind:
107 case MemRegion::FieldRegionKind:
108 case MemRegion::ObjCIvarRegionKind:
109 case MemRegion::VarRegionKind:
110 case MemRegion::CXXObjectRegionKind:
111 return MakeElementRegion(R, PointeeTy);
113 case MemRegion::ElementRegionKind: {
114 // If we are casting from an ElementRegion to another type, the
115 // algorithm is as follows:
117 // (1) Compute the "raw offset" of the ElementRegion from the
118 // base region. This is done by calling 'getAsRawOffset()'.
120 // (2a) If we get a 'RegionRawOffset' after calling
121 // 'getAsRawOffset()', determine if the absolute offset
122 // can be exactly divided into chunks of the size of the
123 // casted-pointee type. If so, create a new ElementRegion with
124 // the pointee-cast type as the new ElementType and the index
125 // being the offset divded by the chunk size. If not, create
126 // a new ElementRegion at offset 0 off the raw offset region.
128 // (2b) If we don't a get a 'RegionRawOffset' after calling
129 // 'getAsRawOffset()', it means that we are at offset 0.
131 // FIXME: Handle symbolic raw offsets.
133 const ElementRegion *elementR = cast<ElementRegion>(R);
134 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
135 const MemRegion *baseR = rawOff.getRegion();
137 // If we cannot compute a raw offset, throw up our hands and return
138 // a NULL MemRegion*.
142 CharUnits off = CharUnits::fromQuantity(rawOff.getByteOffset());
145 // Edge case: we are at 0 bytes off the beginning of baseR. We
146 // check to see if type we are casting to is the same as the base
147 // region. If so, just return the base region.
148 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
149 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
150 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
151 if (CanonPointeeTy == ObjTy)
155 // Otherwise, create a new ElementRegion at offset 0.
156 return MakeElementRegion(baseR, PointeeTy);
159 // We have a non-zero offset from the base region. We want to determine
160 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
161 // we create an ElementRegion whose index is that value. Otherwise, we
162 // create two ElementRegions, one that reflects a raw offset and the other
163 // that reflects the cast.
165 // Compute the index for the new ElementRegion.
166 int64_t newIndex = 0;
167 const MemRegion *newSuperR = 0;
169 // We can only compute sizeof(PointeeTy) if it is a complete type.
170 if (IsCompleteType(Ctx, PointeeTy)) {
171 // Compute the size in **bytes**.
172 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
174 // Is the offset a multiple of the size? If so, we can layer the
175 // ElementRegion (with elementType == PointeeTy) directly on top of
177 if (off % pointeeTySize == 0) {
178 newIndex = off / pointeeTySize;
184 // Create an intermediate ElementRegion to represent the raw byte.
185 // This will be the super region of the final ElementRegion.
186 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
189 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
193 assert(0 && "unreachable");
198 /// CastRetrievedVal - Used by subclasses of StoreManager to implement
199 /// implicit casts that arise from loads from regions that are reinterpreted
200 /// as another region.
201 SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
202 QualType castTy, bool performTestOnly) {
207 ASTContext &Ctx = ValMgr.getContext();
209 if (performTestOnly) {
210 // Automatically translate references to pointers.
211 QualType T = R->getValueType(Ctx);
212 if (const ReferenceType *RT = T->getAs<ReferenceType>())
213 T = Ctx.getPointerType(RT->getPointeeType());
215 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy, T));
219 if (const Loc *L = dyn_cast<Loc>(&V))
220 return ValMgr.getSValuator().EvalCastL(*L, castTy);
221 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
222 return ValMgr.getSValuator().EvalCastNL(*NL, castTy);
227 Store StoreManager::InvalidateRegions(Store store,
228 const MemRegion * const *I,
229 const MemRegion * const *End,
230 const Expr *E, unsigned Count,
231 InvalidatedSymbols *IS) {
232 for ( ; I != End ; ++I)
233 store = InvalidateRegion(store, *I, E, Count, IS);
238 SVal StoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
239 if (Base.isUnknownOrUndef())
242 Loc BaseL = cast<Loc>(Base);
243 const MemRegion* BaseR = 0;
245 switch (BaseL.getSubKind()) {
246 case loc::MemRegionKind:
247 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
250 case loc::GotoLabelKind:
251 // These are anormal cases. Flag an undefined value.
252 return UndefinedVal();
254 case loc::ConcreteIntKind:
255 // While these seem funny, this can happen through casts.
256 // FIXME: What we should return is the field offset. For example,
257 // add the field offset to the integer value. That way funny things
258 // like this work properly: &(((struct foo *) 0xa)->f)
262 assert(0 && "Unhandled Base.");
266 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
268 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
269 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
271 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
274 SVal StoreManager::getLValueElement(QualType elementType, SVal Offset,
277 // If the base is an unknown or undefined value, just return it back.
278 // FIXME: For absolute pointer addresses, we just return that value back as
279 // well, although in reality we should return the offset added to that
281 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
284 // Only handle integer offsets... for now.
285 if (!isa<nonloc::ConcreteInt>(Offset))
288 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
290 // Pointer of any type can be cast and used as array base.
291 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
293 // Convert the offset to the appropriate size and signedness.
294 Offset = ValMgr.convertToArrayIndex(Offset);
298 // If the base region is not an ElementRegion, create one.
299 // This can happen in the following example:
301 // char *p = __builtin_alloc(10);
304 // Observe that 'p' binds to an AllocaRegion.
306 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
310 SVal BaseIdx = ElemR->getIndex();
312 if (!isa<nonloc::ConcreteInt>(BaseIdx))
315 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
316 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
317 assert(BaseIdxI.isSigned());
319 // Compute the new index.
320 SVal NewIdx = nonloc::ConcreteInt(
321 ValMgr.getBasicValueFactory().getValue(BaseIdxI + OffI));
323 // Construct the new ElementRegion.
324 const MemRegion *ArrayR = ElemR->getSuperRegion();
325 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,