blob: e591842838428eb184b81b4c1d3f66613a05b652 [file] [log] [blame]
Ted Kremenekc62abc12009-04-21 21:51:34 +00001//== Store.cpp - Interface for maps 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 types Store and StoreManager.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/Store.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
16
17using namespace clang;
18
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000019StoreManager::StoreManager(GRStateManager &stateMgr)
20 : ValMgr(stateMgr.getValueManager()), StateMgr(stateMgr),
Ted Kremenekc62abc12009-04-21 21:51:34 +000021 MRMgr(ValMgr.getRegionManager()) {}
22
Zhongxing Xu09270cc2009-10-14 06:55:01 +000023const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
Zhongxing Xu652be342009-11-16 04:49:44 +000024 QualType EleTy, uint64_t index) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000025 SVal idx = ValMgr.makeArrayIndex(index);
Zhongxing Xu09270cc2009-10-14 06:55:01 +000026 return MRMgr.getElementRegion(EleTy, idx, Base, ValMgr.getContext());
Ted Kremenek411af402009-07-06 22:23:45 +000027}
28
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000029// FIXME: Merge with the implementation of the same method in MemRegion.cpp
Ted Kremenek169077d2009-07-06 23:47:19 +000030static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek6217b802009-07-29 21:53:49 +000031 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000032 const RecordDecl *D = RT->getDecl();
33 if (!D->getDefinition(Ctx))
34 return false;
35 }
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenek169077d2009-07-06 23:47:19 +000037 return true;
38}
39
Zhongxing Xu09270cc2009-10-14 06:55:01 +000040const MemRegion *StoreManager::CastRegion(const MemRegion *R, QualType CastToTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenek48ce7de2009-07-06 20:21:51 +000042 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000043
Ted Kremenekb9a44252009-07-06 22:39:40 +000044 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000045 if (CastToTy->isObjCObjectPointerType())
Zhongxing Xu479529e2009-11-10 02:17:20 +000046 return R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000047
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000048 if (CastToTy->isBlockPointerType()) {
Ted Kremenekabd46e12009-08-28 04:49:15 +000049 // FIXME: We may need different solutions, depending on the symbol
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000050 // involved. Blocks can be casted to/from 'id', as they can be treated
Ted Kremenekabd46e12009-08-28 04:49:15 +000051 // as Objective-C objects. This could possibly be handled by enhancing
Mike Stump1eb44332009-09-09 15:08:12 +000052 // our reasoning of downcasts of symbolic objects.
Ted Kremenekabd46e12009-08-28 04:49:15 +000053 if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
Zhongxing Xu09270cc2009-10-14 06:55:01 +000054 return R;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000055
56 // We don't know what to make of it. Return a NULL region, which
57 // will be interpretted as UnknownVal.
Zhongxing Xu09270cc2009-10-14 06:55:01 +000058 return NULL;
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000059 }
Ted Kremenek411af402009-07-06 22:23:45 +000060
Ted Kremenek48ce7de2009-07-06 20:21:51 +000061 // Now assume we are casting from pointer to pointer. Other cases should
62 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000063 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000064 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
65
66 // Handle casts to void*. We just pass the region through.
Douglas Gregora4923eb2009-11-16 21:35:15 +000067 if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000068 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenek9a108eb2009-08-02 04:12:53 +000070 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000071 if (R->isBoundable())
72 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
73 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
Ted Kremenek9a108eb2009-08-02 04:12:53 +000074 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +000075 return R;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000076 }
77
Ted Kremenek48ce7de2009-07-06 20:21:51 +000078 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000079 switch (R->getKind()) {
Ted Kremenek67d12872009-12-07 22:05:27 +000080 case MemRegion::GenericMemSpaceRegionKind:
81 case MemRegion::StackLocalsSpaceRegionKind:
82 case MemRegion::StackArgumentsSpaceRegionKind:
83 case MemRegion::HeapSpaceRegionKind:
84 case MemRegion::GlobalsSpaceRegionKind: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000085 assert(0 && "Invalid region cast");
86 break;
Mike Stump1eb44332009-09-09 15:08:12 +000087 }
Ted Kremenekeb1c7a02009-11-25 01:32:22 +000088
89 case MemRegion::FunctionTextRegionKind:
Ted Kremenekbf0fe6c2009-11-25 23:58:21 +000090 case MemRegion::BlockTextRegionKind:
91 case MemRegion::BlockDataRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000092 // CodeTextRegion should be cast to only a function or block pointer type,
Zhongxing Xu09270cc2009-10-14 06:55:01 +000093 // although they can in practice be casted to anything, e.g, void*, char*,
94 // etc.
95 // Just return the region.
96 return R;
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000099 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000100 case MemRegion::ObjCObjectRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000101 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000102 case MemRegion::SymbolicRegionKind:
103 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000104 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000105 case MemRegion::FieldRegionKind:
106 case MemRegion::ObjCIvarRegionKind:
Mike Stump1eb44332009-09-09 15:08:12 +0000107 case MemRegion::VarRegionKind:
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000108 return MakeElementRegion(R, PointeeTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000110 case MemRegion::ElementRegionKind: {
111 // If we are casting from an ElementRegion to another type, the
112 // algorithm is as follows:
113 //
114 // (1) Compute the "raw offset" of the ElementRegion from the
115 // base region. This is done by calling 'getAsRawOffset()'.
116 //
Mike Stump1eb44332009-09-09 15:08:12 +0000117 // (2a) If we get a 'RegionRawOffset' after calling
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000118 // 'getAsRawOffset()', determine if the absolute offset
Mike Stump1eb44332009-09-09 15:08:12 +0000119 // can be exactly divided into chunks of the size of the
120 // casted-pointee type. If so, create a new ElementRegion with
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121 // the pointee-cast type as the new ElementType and the index
122 // being the offset divded by the chunk size. If not, create
123 // a new ElementRegion at offset 0 off the raw offset region.
124 //
125 // (2b) If we don't a get a 'RegionRawOffset' after calling
126 // 'getAsRawOffset()', it means that we are at offset 0.
Mike Stump1eb44332009-09-09 15:08:12 +0000127 //
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000128 // FIXME: Handle symbolic raw offsets.
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000130 const ElementRegion *elementR = cast<ElementRegion>(R);
131 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
132 const MemRegion *baseR = rawOff.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000134 // If we cannot compute a raw offset, throw up our hands and return
135 // a NULL MemRegion*.
136 if (!baseR)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000137 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000139 int64_t off = rawOff.getByteOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000141 if (off == 0) {
142 // Edge case: we are at 0 bytes off the beginning of baseR. We
143 // check to see if type we are casting to is the same as the base
Mike Stump1eb44332009-09-09 15:08:12 +0000144 // region. If so, just return the base region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000145 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
146 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
147 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
148 if (CanonPointeeTy == ObjTy)
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000149 return baseR;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000152 // Otherwise, create a new ElementRegion at offset 0.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000153 return MakeElementRegion(baseR, PointeeTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000156 // We have a non-zero offset from the base region. We want to determine
157 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
158 // we create an ElementRegion whose index is that value. Otherwise, we
159 // create two ElementRegions, one that reflects a raw offset and the other
160 // that reflects the cast.
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000162 // Compute the index for the new ElementRegion.
163 int64_t newIndex = 0;
164 const MemRegion *newSuperR = 0;
165
166 // We can only compute sizeof(PointeeTy) if it is a complete type.
167 if (IsCompleteType(Ctx, PointeeTy)) {
168 // Compute the size in **bytes**.
169 int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8);
170
171 // Is the offset a multiple of the size? If so, we can layer the
172 // ElementRegion (with elementType == PointeeTy) directly on top of
173 // the base region.
174 if (off % pointeeTySize == 0) {
175 newIndex = off / pointeeTySize;
176 newSuperR = baseR;
177 }
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000180 if (!newSuperR) {
181 // Create an intermediate ElementRegion to represent the raw byte.
182 // This will be the super region of the final ElementRegion.
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000183 newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000186 return MakeElementRegion(newSuperR, PointeeTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000187 }
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000190 assert(0 && "unreachable");
191 return 0;
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000192}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000193
194
195/// CastRetrievedVal - Used by subclasses of StoreManager to implement
196/// implicit casts that arise from loads from regions that are reinterpreted
197/// as another region.
Zhongxing Xu652be342009-11-16 04:49:44 +0000198SVal StoreManager::CastRetrievedVal(SVal V, const TypedRegion *R,
199 QualType castTy) {
Zhongxing Xu652be342009-11-16 04:49:44 +0000200 if (castTy.isNull())
201 return V;
202
Ted Kremenek27e05ed2009-11-19 19:04:08 +0000203 assert(ValMgr.getContext().hasSameUnqualifiedType(castTy,
204 R->getValueType(ValMgr.getContext())));
Zhongxing Xu652be342009-11-16 04:49:44 +0000205 return V;
Ted Kremenek1894dce2009-08-25 20:51:30 +0000206}
207
Ted Kremenek81a95832009-12-03 03:27:11 +0000208const GRState *StoreManager::InvalidateRegions(const GRState *state,
209 const MemRegion * const *I,
210 const MemRegion * const *End,
211 const Expr *E,
212 unsigned Count,
213 InvalidatedSymbols *IS) {
214 for ( ; I != End ; ++I)
215 state = InvalidateRegion(state, *I, E, Count, IS);
216
217 return state;
218}
Ted Kremenek67d12872009-12-07 22:05:27 +0000219
220//===----------------------------------------------------------------------===//
221// Common getLValueXXX methods.
222//===----------------------------------------------------------------------===//
223
224/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
225/// of a compound literal. Within RegionStore a compound literal
226/// has an associated region, and the lvalue of the compound literal
227/// is the lvalue of that region.
228SVal StoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL,
229 const LocationContext *LC) {
230 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC));
231}