blob: e2412f6e2806515712f745a6808e05325e1e68fe [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
23StoreManager::CastResult
Ted Kremenek411af402009-07-06 22:23:45 +000024StoreManager::MakeElementRegion(const GRState *state, const MemRegion *region,
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000025 QualType pointeeTy, QualType castToTy,
26 uint64_t index) {
27 // Create a new ElementRegion.
28 SVal idx = ValMgr.makeArrayIndex(index);
Ted Kremenek411af402009-07-06 22:23:45 +000029 return CastResult(state, MRMgr.getElementRegion(pointeeTy, idx, region,
30 ValMgr.getContext()));
31}
32
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000033// FIXME: Merge with the implementation of the same method in MemRegion.cpp
Ted Kremenek169077d2009-07-06 23:47:19 +000034static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
Ted Kremenek6217b802009-07-29 21:53:49 +000035 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Ted Kremenek169077d2009-07-06 23:47:19 +000036 const RecordDecl *D = RT->getDecl();
37 if (!D->getDefinition(Ctx))
38 return false;
39 }
40
41 return true;
42}
43
Ted Kremenek411af402009-07-06 22:23:45 +000044StoreManager::CastResult
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000045StoreManager::CastRegion(const GRState *state, const MemRegion* R,
46 QualType CastToTy) {
Ted Kremenek48ce7de2009-07-06 20:21:51 +000047
48 ASTContext& Ctx = StateMgr.getContext();
49
Ted Kremenekb9a44252009-07-06 22:39:40 +000050 // Handle casts to Objective-C objects.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000051 if (CastToTy->isObjCObjectPointerType())
52 return CastResult(state, R->getBaseRegion());
53
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000054 if (CastToTy->isBlockPointerType()) {
55 if (isa<CodeTextRegion>(R))
56 return CastResult(state, R);
57
58 // FIXME: This may not be the right approach, depending on the symbol
59 // involved. Blocks can be casted to/from 'id', as they can be treated
60 // as Objective-C objects.
61 if (SymbolRef sym = loc::MemRegionVal(R).getAsSymbol()) {
62 R = MRMgr.getCodeTextRegion(sym, CastToTy);
63 return CastResult(state, R);
64 }
65
66 // We don't know what to make of it. Return a NULL region, which
67 // will be interpretted as UnknownVal.
68 return CastResult(state, NULL);
69 }
Ted Kremenek411af402009-07-06 22:23:45 +000070
Ted Kremenek48ce7de2009-07-06 20:21:51 +000071 // Now assume we are casting from pointer to pointer. Other cases should
72 // already be handled.
Ted Kremenek6217b802009-07-29 21:53:49 +000073 QualType PointeeTy = CastToTy->getAs<PointerType>()->getPointeeType();
Ted Kremenek9a108eb2009-08-02 04:12:53 +000074 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
75
76 // Handle casts to void*. We just pass the region through.
77 if (CanonPointeeTy.getUnqualifiedType() == Ctx.VoidTy)
78 return CastResult(state, R);
Ted Kremenek169077d2009-07-06 23:47:19 +000079
Ted Kremenek9a108eb2009-08-02 04:12:53 +000080 // Handle casts from compatible types.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000081 if (R->isBoundable())
82 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
83 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
Ted Kremenek9a108eb2009-08-02 04:12:53 +000084 if (CanonPointeeTy == ObjTy)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000085 return CastResult(state, R);
86 }
87
Ted Kremenek48ce7de2009-07-06 20:21:51 +000088 // Process region cast according to the kind of the region being cast.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000089 switch (R->getKind()) {
90 case MemRegion::BEG_TYPED_REGIONS:
91 case MemRegion::MemSpaceRegionKind:
92 case MemRegion::BEG_DECL_REGIONS:
93 case MemRegion::END_DECL_REGIONS:
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000094 case MemRegion::END_TYPED_REGIONS: {
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000095 assert(0 && "Invalid region cast");
96 break;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000097 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +000098 case MemRegion::CodeTextRegionKind: {
Ted Kremenek63b9cfe2009-07-18 06:27:51 +000099 // CodeTextRegion should be cast to only a function or block pointer type,
100 // although they can in practice be casted to anything, e.g, void*,
101 // char*, etc.
Ted Kremenek8d344ae2009-07-10 21:24:45 +0000102 // Just pass the region through.
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000103 break;
104 }
105
106 case MemRegion::StringRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000107 case MemRegion::ObjCObjectRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000108 // FIXME: Need to handle arbitrary downcasts.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000109 case MemRegion::SymbolicRegionKind:
110 case MemRegion::AllocaRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000111 case MemRegion::CompoundLiteralRegionKind:
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000112 case MemRegion::FieldRegionKind:
113 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000114 case MemRegion::VarRegionKind:
115 return MakeElementRegion(state, R, PointeeTy, CastToTy);
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000116
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000117 case MemRegion::ElementRegionKind: {
118 // If we are casting from an ElementRegion to another type, the
119 // algorithm is as follows:
120 //
121 // (1) Compute the "raw offset" of the ElementRegion from the
122 // base region. This is done by calling 'getAsRawOffset()'.
123 //
124 // (2a) If we get a 'RegionRawOffset' after calling
125 // 'getAsRawOffset()', determine if the absolute offset
126 // can be exactly divided into chunks of the size of the
127 // casted-pointee type. If so, create a new ElementRegion with
128 // the pointee-cast type as the new ElementType and the index
129 // being the offset divded by the chunk size. If not, create
130 // a new ElementRegion at offset 0 off the raw offset region.
131 //
132 // (2b) If we don't a get a 'RegionRawOffset' after calling
133 // 'getAsRawOffset()', it means that we are at offset 0.
134 //
135 // FIXME: Handle symbolic raw offsets.
Ted Kremenek169077d2009-07-06 23:47:19 +0000136
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137 const ElementRegion *elementR = cast<ElementRegion>(R);
138 const RegionRawOffset &rawOff = elementR->getAsRawOffset();
139 const MemRegion *baseR = rawOff.getRegion();
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000140
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000141 // If we cannot compute a raw offset, throw up our hands and return
142 // a NULL MemRegion*.
143 if (!baseR)
144 return CastResult(state, NULL);
145
146 int64_t off = rawOff.getByteOffset();
147
148 if (off == 0) {
149 // Edge case: we are at 0 bytes off the beginning of baseR. We
150 // check to see if type we are casting to is the same as the base
151 // region. If so, just return the base region.
152 if (const TypedRegion *TR = dyn_cast<TypedRegion>(baseR)) {
153 QualType ObjTy = Ctx.getCanonicalType(TR->getValueType(Ctx));
154 QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
155 if (CanonPointeeTy == ObjTy)
156 return CastResult(state, baseR);
157 }
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000158
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000159 // Otherwise, create a new ElementRegion at offset 0.
160 return MakeElementRegion(state, baseR, PointeeTy, CastToTy, 0);
161 }
162
163 // We have a non-zero offset from the base region. We want to determine
164 // if the offset can be evenly divided by sizeof(PointeeTy). If so,
165 // we create an ElementRegion whose index is that value. Otherwise, we
166 // create two ElementRegions, one that reflects a raw offset and the other
167 // that reflects the cast.
168
169 // Compute the index for the new ElementRegion.
170 int64_t newIndex = 0;
171 const MemRegion *newSuperR = 0;
172
173 // We can only compute sizeof(PointeeTy) if it is a complete type.
174 if (IsCompleteType(Ctx, PointeeTy)) {
175 // Compute the size in **bytes**.
176 int64_t pointeeTySize = (int64_t) (Ctx.getTypeSize(PointeeTy) / 8);
177
178 // Is the offset a multiple of the size? If so, we can layer the
179 // ElementRegion (with elementType == PointeeTy) directly on top of
180 // the base region.
181 if (off % pointeeTySize == 0) {
182 newIndex = off / pointeeTySize;
183 newSuperR = baseR;
184 }
185 }
186
187 if (!newSuperR) {
188 // Create an intermediate ElementRegion to represent the raw byte.
189 // This will be the super region of the final ElementRegion.
190 SVal idx = ValMgr.makeArrayIndex(off);
191 newSuperR = MRMgr.getElementRegion(Ctx.CharTy, idx, baseR, Ctx);
192 }
193
194 return MakeElementRegion(state, newSuperR, PointeeTy, CastToTy, newIndex);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000195 }
196 }
197
Ted Kremenekfc8f57c2009-07-06 22:56:37 +0000198 return CastResult(state, R);
Ted Kremenek48ce7de2009-07-06 20:21:51 +0000199}
Ted Kremenek1894dce2009-08-25 20:51:30 +0000200
201
202/// CastRetrievedVal - Used by subclasses of StoreManager to implement
203/// implicit casts that arise from loads from regions that are reinterpreted
204/// as another region.
205SValuator::CastResult StoreManager::CastRetrievedVal(SVal V,
206 const GRState *state,
207 const TypedRegion *R,
208 QualType castTy) {
209 if (castTy.isNull())
210 return SValuator::CastResult(state, V);
211
212 ASTContext &Ctx = ValMgr.getContext();
213 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
214}
215