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/StaticAnalyzer/Core/PathSensitive/Store.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
16 #include "clang/AST/CharUnits.h"
18 using namespace clang;
21 StoreManager::StoreManager(ProgramStateManager &stateMgr)
22 : svalBuilder(stateMgr.getSValBuilder()), StateMgr(stateMgr),
23 MRMgr(svalBuilder.getRegionManager()), Ctx(stateMgr.getContext()) {}
25 StoreRef StoreManager::enterStackFrame(const ProgramState *state,
26 const StackFrameContext *frame) {
27 return StoreRef(state->getStore(), *this);
30 const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
31 QualType EleTy, uint64_t index) {
32 NonLoc idx = svalBuilder.makeArrayIndex(index);
33 return MRMgr.getElementRegion(EleTy, idx, Base, svalBuilder.getContext());
36 // FIXME: Merge with the implementation of the same method in MemRegion.cpp
37 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
38 if (const RecordType *RT = Ty->getAs<RecordType>()) {
39 const RecordDecl *D = RT->getDecl();
40 if (!D->getDefinition())
47 StoreRef StoreManager::BindDefault(Store store, const MemRegion *R, SVal V) {
48 return StoreRef(store, *this);
51 const ElementRegion *StoreManager::GetElementZeroRegion(const MemRegion *R,
53 NonLoc idx = svalBuilder.makeZeroArrayIndex();
55 return MRMgr.getElementRegion(T, idx, R, Ctx);
58 const MemRegion *StoreManager::castRegion(const MemRegion *R, QualType CastToTy) {
60 ASTContext &Ctx = StateMgr.getContext();
62 // Handle casts to Objective-C objects.
63 if (CastToTy->isObjCObjectPointerType())
64 return R->StripCasts();
66 if (CastToTy->isBlockPointerType()) {
67 // FIXME: We may need different solutions, depending on the symbol
68 // involved. Blocks can be casted to/from 'id', as they can be treated
69 // as Objective-C objects. This could possibly be handled by enhancing
70 // our reasoning of downcasts of symbolic objects.
71 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
74 // We don't know what to make of it. Return a NULL region, which
75 // will be interpretted as UnknownVal.
79 // Now assume we are casting from pointer to pointer. Other cases should
80 // already be handled.
81 QualType PointeeTy = CastToTy->getPointeeType();
82 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
84 // Handle casts to void*. We just pass the region through.
85 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
88 // Handle casts from compatible types.
90 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
91 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
92 if (CanonPointeeTy == ObjTy)
96 // Process region cast according to the kind of the region being cast.
97 switch (R->getKind()) {
98 case MemRegion::CXXThisRegionKind:
99 case MemRegion::GenericMemSpaceRegionKind:
100 case MemRegion::StackLocalsSpaceRegionKind:
101 case MemRegion::StackArgumentsSpaceRegionKind:
102 case MemRegion::HeapSpaceRegionKind:
103 case MemRegion::UnknownSpaceRegionKind:
104 case MemRegion::NonStaticGlobalSpaceRegionKind:
105 case MemRegion::StaticGlobalSpaceRegionKind: {
106 llvm_unreachable("Invalid region cast");
109 case MemRegion::FunctionTextRegionKind:
110 case MemRegion::BlockTextRegionKind:
111 case MemRegion::BlockDataRegionKind:
112 case MemRegion::StringRegionKind:
113 // FIXME: Need to handle arbitrary downcasts.
114 case MemRegion::SymbolicRegionKind:
115 case MemRegion::AllocaRegionKind:
116 case MemRegion::CompoundLiteralRegionKind:
117 case MemRegion::FieldRegionKind:
118 case MemRegion::ObjCIvarRegionKind:
119 case MemRegion::VarRegionKind:
120 case MemRegion::CXXTempObjectRegionKind:
121 case MemRegion::CXXBaseObjectRegionKind:
122 return MakeElementRegion(R, PointeeTy);
124 case MemRegion::ElementRegionKind: {
125 // If we are casting from an ElementRegion to another type, the
126 // algorithm is as follows:
128 // (1) Compute the "raw offset" of the ElementRegion from the
129 // base region. This is done by calling 'getAsRawOffset()'.
131 // (2a) If we get a 'RegionRawOffset' after calling
132 // 'getAsRawOffset()', determine if the absolute offset
133 // can be exactly divided into chunks of the size of the
134 // casted-pointee type. If so, create a new ElementRegion with
135 // the pointee-cast type as the new ElementType and the index
136 // being the offset divded by the chunk size. If not, create
137 // a new ElementRegion at offset 0 off the raw offset region.
139 // (2b) If we don't a get a 'RegionRawOffset' after calling
140 // 'getAsRawOffset()', it means that we are at offset 0.
142 // FIXME: Handle symbolic raw offsets.
144 const ElementRegion *elementR = cast<ElementRegion>(R);
145 const RegionRawOffset &rawOff = elementR->getAsArrayOffset();
146 const MemRegion *baseR = rawOff.getRegion();
148 // If we cannot compute a raw offset, throw up our hands and return
149 // a NULL MemRegion*.
153 CharUnits off = rawOff.getOffset();
156 // Edge case: we are at 0 bytes off the beginning of baseR. We
157 // check to see if type we are casting to is the same as the base
158 // region. If so, just return the base region.
159 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(baseR)) {
160 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
161 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
162 if (CanonPointeeTy == ObjTy)
166 // Otherwise, create a new ElementRegion at offset 0.
167 return MakeElementRegion(baseR, PointeeTy);
170 // We have a non-zero offset from the base region. We want to determine
171 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
172 // we create an ElementRegion whose index is that value. Otherwise, we
173 // create two ElementRegions, one that reflects a raw offset and the other
174 // that reflects the cast.
176 // Compute the index for the new ElementRegion.
177 int64_t newIndex = 0;
178 const MemRegion *newSuperR = 0;
180 // We can only compute sizeof(PointeeTy) if it is a complete type.
181 if (IsCompleteType(Ctx, PointeeTy)) {
182 // Compute the size in **bytes**.
183 CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
184 if (!pointeeTySize.isZero()) {
185 // Is the offset a multiple of the size? If so, we can layer the
186 // ElementRegion (with elementType == PointeeTy) directly on top of
188 if (off % pointeeTySize == 0) {
189 newIndex = off / pointeeTySize;
196 // Create an intermediate ElementRegion to represent the raw byte.
197 // This will be the super region of the final ElementRegion.
198 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
201 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
205 llvm_unreachable("unreachable");
209 /// CastRetrievedVal - Used by subclasses of StoreManager to implement
210 /// implicit casts that arise from loads from regions that are reinterpreted
211 /// as another region.
212 SVal StoreManager::CastRetrievedVal(SVal V, const TypedValueRegion *R,
213 QualType castTy, bool performTestOnly) {
218 ASTContext &Ctx = svalBuilder.getContext();
220 if (performTestOnly) {
221 // Automatically translate references to pointers.
222 QualType T = R->getValueType();
223 if (const ReferenceType *RT = T->getAs<ReferenceType>())
224 T = Ctx.getPointerType(RT->getPointeeType());
226 assert(svalBuilder.getContext().hasSameUnqualifiedType(castTy, T));
230 if (const Loc *L = dyn_cast<Loc>(&V))
231 return svalBuilder.evalCastFromLoc(*L, castTy);
232 else if (const NonLoc *NL = dyn_cast<NonLoc>(&V))
233 return svalBuilder.evalCastFromNonLoc(*NL, castTy);
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 llvm_unreachable("Unhandled Base.");
265 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
267 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
268 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
270 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
273 SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset,
276 // If the base is an unknown or undefined value, just return it back.
277 // FIXME: For absolute pointer addresses, we just return that value back as
278 // well, although in reality we should return the offset added to that
280 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
283 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
285 // Pointer of any type can be cast and used as array base.
286 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
288 // Convert the offset to the appropriate size and signedness.
289 Offset = cast<NonLoc>(svalBuilder.convertToArrayIndex(Offset));
293 // If the base region is not an ElementRegion, create one.
294 // This can happen in the following example:
296 // char *p = __builtin_alloc(10);
299 // Observe that 'p' binds to an AllocaRegion.
301 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
305 SVal BaseIdx = ElemR->getIndex();
307 if (!isa<nonloc::ConcreteInt>(BaseIdx))
310 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
312 // Only allow non-integer offsets if the base region has no offset itself.
313 // FIXME: This is a somewhat arbitrary restriction. We should be using
314 // SValBuilder here to add the two offsets without checking their types.
315 if (!isa<nonloc::ConcreteInt>(Offset)) {
316 if (isa<ElementRegion>(BaseRegion->StripCasts()))
319 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
320 ElemR->getSuperRegion(),
324 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
325 assert(BaseIdxI.isSigned());
327 // Compute the new index.
328 nonloc::ConcreteInt NewIdx(svalBuilder.getBasicValueFactory().getValue(BaseIdxI +
331 // Construct the new ElementRegion.
332 const MemRegion *ArrayR = ElemR->getSuperRegion();
333 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
337 StoreManager::BindingsHandler::~BindingsHandler() {}