1 //== 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/StaticAnalyzer/Core/PathSensitive/Store.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/DeclObjC.h"
19 
20 using namespace clang;
21 using namespace ento;
22 
23 StoreManager::StoreManager(ProgramStateManager &stateMgr)
24   : svalBuilder(stateMgr.getSValBuilder()), StateMgr(stateMgr),
25     MRMgr(svalBuilder.getRegionManager()), Ctx(stateMgr.getContext()) {}
26 
27 StoreRef StoreManager::enterStackFrame(Store OldStore,
28                                        const CallEvent &Call,
29                                        const StackFrameContext *LCtx) {
30   StoreRef Store = StoreRef(OldStore, *this);
31 
32   SmallVector<CallEvent::FrameBindingTy, 16> InitialBindings;
33   Call.getInitialStackFrameContents(LCtx, InitialBindings);
34 
35   for (CallEvent::BindingsTy::iterator I = InitialBindings.begin(),
36                                        E = InitialBindings.end();
37        I != E; ++I) {
38     Store = Bind(Store.getStore(), I->first, I->second);
39   }
40 
41   return Store;
42 }
43 
44 const MemRegion *StoreManager::MakeElementRegion(const MemRegion *Base,
45                                               QualType EleTy, uint64_t index) {
46   NonLoc idx = svalBuilder.makeArrayIndex(index);
47   return MRMgr.getElementRegion(EleTy, idx, Base, svalBuilder.getContext());
48 }
49 
50 // FIXME: Merge with the implementation of the same method in MemRegion.cpp
51 static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
52   if (const RecordType *RT = Ty->getAs<RecordType>()) {
53     const RecordDecl *D = RT->getDecl();
54     if (!D->getDefinition())
55       return false;
56   }
57 
58   return true;
59 }
60 
61 StoreRef StoreManager::BindDefault(Store store, const MemRegion *R, SVal V) {
62   return StoreRef(store, *this);
63 }
64 
65 const ElementRegion *StoreManager::GetElementZeroRegion(const MemRegion *R,
66                                                         QualType T) {
67   NonLoc idx = svalBuilder.makeZeroArrayIndex();
68   assert(!T.isNull());
69   return MRMgr.getElementRegion(T, idx, R, Ctx);
70 }
71 
72 const MemRegion *StoreManager::castRegion(const MemRegion *R, QualType CastToTy) {
73 
74   ASTContext &Ctx = StateMgr.getContext();
75 
76   // Handle casts to Objective-C objects.
77   if (CastToTy->isObjCObjectPointerType())
78     return R->StripCasts();
79 
80   if (CastToTy->isBlockPointerType()) {
81     // FIXME: We may need different solutions, depending on the symbol
82     // involved.  Blocks can be casted to/from 'id', as they can be treated
83     // as Objective-C objects.  This could possibly be handled by enhancing
84     // our reasoning of downcasts of symbolic objects.
85     if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
86       return R;
87 
88     // We don't know what to make of it.  Return a NULL region, which
89     // will be interpretted as UnknownVal.
90     return NULL;
91   }
92 
93   // Now assume we are casting from pointer to pointer. Other cases should
94   // already be handled.
95   QualType PointeeTy = CastToTy->getPointeeType();
96   QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
97 
98   // Handle casts to void*.  We just pass the region through.
99   if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
100     return R;
101 
102   // Handle casts from compatible types.
103   if (R->isBoundable())
104     if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
105       QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
106       if (CanonPointeeTy == ObjTy)
107         return R;
108     }
109 
110   // Process region cast according to the kind of the region being cast.
111   switch (R->getKind()) {
112     case MemRegion::CXXThisRegionKind:
113     case MemRegion::GenericMemSpaceRegionKind:
114     case MemRegion::StackLocalsSpaceRegionKind:
115     case MemRegion::StackArgumentsSpaceRegionKind:
116     case MemRegion::HeapSpaceRegionKind:
117     case MemRegion::UnknownSpaceRegionKind:
118     case MemRegion::StaticGlobalSpaceRegionKind:
119     case MemRegion::GlobalInternalSpaceRegionKind:
120     case MemRegion::GlobalSystemSpaceRegionKind:
121     case MemRegion::GlobalImmutableSpaceRegionKind: {
122       llvm_unreachable("Invalid region cast");
123     }
124 
125     case MemRegion::FunctionTextRegionKind:
126     case MemRegion::BlockTextRegionKind:
127     case MemRegion::BlockDataRegionKind:
128     case MemRegion::StringRegionKind:
129       // FIXME: Need to handle arbitrary downcasts.
130     case MemRegion::SymbolicRegionKind:
131     case MemRegion::AllocaRegionKind:
132     case MemRegion::CompoundLiteralRegionKind:
133     case MemRegion::FieldRegionKind:
134     case MemRegion::ObjCIvarRegionKind:
135     case MemRegion::ObjCStringRegionKind:
136     case MemRegion::VarRegionKind:
137     case MemRegion::CXXTempObjectRegionKind:
138     case MemRegion::CXXBaseObjectRegionKind:
139       return MakeElementRegion(R, PointeeTy);
140 
141     case MemRegion::ElementRegionKind: {
142       // If we are casting from an ElementRegion to another type, the
143       // algorithm is as follows:
144       //
145       // (1) Compute the "raw offset" of the ElementRegion from the
146       //     base region.  This is done by calling 'getAsRawOffset()'.
147       //
148       // (2a) If we get a 'RegionRawOffset' after calling
149       //      'getAsRawOffset()', determine if the absolute offset
150       //      can be exactly divided into chunks of the size of the
151       //      casted-pointee type.  If so, create a new ElementRegion with
152       //      the pointee-cast type as the new ElementType and the index
153       //      being the offset divded by the chunk size.  If not, create
154       //      a new ElementRegion at offset 0 off the raw offset region.
155       //
156       // (2b) If we don't a get a 'RegionRawOffset' after calling
157       //      'getAsRawOffset()', it means that we are at offset 0.
158       //
159       // FIXME: Handle symbolic raw offsets.
160 
161       const ElementRegion *elementR = cast<ElementRegion>(R);
162       const RegionRawOffset &rawOff = elementR->getAsArrayOffset();
163       const MemRegion *baseR = rawOff.getRegion();
164 
165       // If we cannot compute a raw offset, throw up our hands and return
166       // a NULL MemRegion*.
167       if (!baseR)
168         return NULL;
169 
170       CharUnits off = rawOff.getOffset();
171 
172       if (off.isZero()) {
173         // Edge case: we are at 0 bytes off the beginning of baseR.  We
174         // check to see if type we are casting to is the same as the base
175         // region.  If so, just return the base region.
176         if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(baseR)) {
177           QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
178           QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
179           if (CanonPointeeTy == ObjTy)
180             return baseR;
181         }
182 
183         // Otherwise, create a new ElementRegion at offset 0.
184         return MakeElementRegion(baseR, PointeeTy);
185       }
186 
187       // We have a non-zero offset from the base region.  We want to determine
188       // if the offset can be evenly divided by sizeof(PointeeTy).  If so,
189       // we create an ElementRegion whose index is that value.  Otherwise, we
190       // create two ElementRegions, one that reflects a raw offset and the other
191       // that reflects the cast.
192 
193       // Compute the index for the new ElementRegion.
194       int64_t newIndex = 0;
195       const MemRegion *newSuperR = 0;
196 
197       // We can only compute sizeof(PointeeTy) if it is a complete type.
198       if (IsCompleteType(Ctx, PointeeTy)) {
199         // Compute the size in **bytes**.
200         CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
201         if (!pointeeTySize.isZero()) {
202           // Is the offset a multiple of the size?  If so, we can layer the
203           // ElementRegion (with elementType == PointeeTy) directly on top of
204           // the base region.
205           if (off % pointeeTySize == 0) {
206             newIndex = off / pointeeTySize;
207             newSuperR = baseR;
208           }
209         }
210       }
211 
212       if (!newSuperR) {
213         // Create an intermediate ElementRegion to represent the raw byte.
214         // This will be the super region of the final ElementRegion.
215         newSuperR = MakeElementRegion(baseR, Ctx.CharTy, off.getQuantity());
216       }
217 
218       return MakeElementRegion(newSuperR, PointeeTy, newIndex);
219     }
220   }
221 
222   llvm_unreachable("unreachable");
223 }
224 
225 SVal StoreManager::evalDerivedToBase(SVal Derived, const CastExpr *Cast) {
226   // Walk through the cast path to create nested CXXBaseRegions.
227   SVal Result = Derived;
228   for (CastExpr::path_const_iterator I = Cast->path_begin(),
229                                      E = Cast->path_end();
230        I != E; ++I) {
231     Result = evalDerivedToBase(Result, (*I)->getType());
232   }
233   return Result;
234 }
235 
236 
237 /// CastRetrievedVal - Used by subclasses of StoreManager to implement
238 ///  implicit casts that arise from loads from regions that are reinterpreted
239 ///  as another region.
240 SVal StoreManager::CastRetrievedVal(SVal V, const TypedValueRegion *R,
241                                     QualType castTy, bool performTestOnly) {
242 
243   if (castTy.isNull() || V.isUnknownOrUndef())
244     return V;
245 
246   ASTContext &Ctx = svalBuilder.getContext();
247 
248   if (performTestOnly) {
249     // Automatically translate references to pointers.
250     QualType T = R->getValueType();
251     if (const ReferenceType *RT = T->getAs<ReferenceType>())
252       T = Ctx.getPointerType(RT->getPointeeType());
253 
254     assert(svalBuilder.getContext().hasSameUnqualifiedType(castTy, T));
255     return V;
256   }
257 
258   return svalBuilder.dispatchCast(V, castTy);
259 }
260 
261 SVal StoreManager::getLValueFieldOrIvar(const Decl *D, SVal Base) {
262   if (Base.isUnknownOrUndef())
263     return Base;
264 
265   Loc BaseL = cast<Loc>(Base);
266   const MemRegion* BaseR = 0;
267 
268   switch (BaseL.getSubKind()) {
269   case loc::MemRegionKind:
270     BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
271     break;
272 
273   case loc::GotoLabelKind:
274     // These are anormal cases. Flag an undefined value.
275     return UndefinedVal();
276 
277   case loc::ConcreteIntKind:
278     // While these seem funny, this can happen through casts.
279     // FIXME: What we should return is the field offset.  For example,
280     //  add the field offset to the integer value.  That way funny things
281     //  like this work properly:  &(((struct foo *) 0xa)->f)
282     return Base;
283 
284   default:
285     llvm_unreachable("Unhandled Base.");
286   }
287 
288   // NOTE: We must have this check first because ObjCIvarDecl is a subclass
289   // of FieldDecl.
290   if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
291     return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
292 
293   return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
294 }
295 
296 SVal StoreManager::getLValueIvar(const ObjCIvarDecl *decl, SVal base) {
297   return getLValueFieldOrIvar(decl, base);
298 }
299 
300 SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset,
301                                     SVal Base) {
302 
303   // If the base is an unknown or undefined value, just return it back.
304   // FIXME: For absolute pointer addresses, we just return that value back as
305   //  well, although in reality we should return the offset added to that
306   //  value.
307   if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
308     return Base;
309 
310   const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
311 
312   // Pointer of any type can be cast and used as array base.
313   const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
314 
315   // Convert the offset to the appropriate size and signedness.
316   Offset = cast<NonLoc>(svalBuilder.convertToArrayIndex(Offset));
317 
318   if (!ElemR) {
319     //
320     // If the base region is not an ElementRegion, create one.
321     // This can happen in the following example:
322     //
323     //   char *p = __builtin_alloc(10);
324     //   p[1] = 8;
325     //
326     //  Observe that 'p' binds to an AllocaRegion.
327     //
328     return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
329                                                     BaseRegion, Ctx));
330   }
331 
332   SVal BaseIdx = ElemR->getIndex();
333 
334   if (!isa<nonloc::ConcreteInt>(BaseIdx))
335     return UnknownVal();
336 
337   const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
338 
339   // Only allow non-integer offsets if the base region has no offset itself.
340   // FIXME: This is a somewhat arbitrary restriction. We should be using
341   // SValBuilder here to add the two offsets without checking their types.
342   if (!isa<nonloc::ConcreteInt>(Offset)) {
343     if (isa<ElementRegion>(BaseRegion->StripCasts()))
344       return UnknownVal();
345 
346     return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
347                                                     ElemR->getSuperRegion(),
348                                                     Ctx));
349   }
350 
351   const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
352   assert(BaseIdxI.isSigned());
353 
354   // Compute the new index.
355   nonloc::ConcreteInt NewIdx(svalBuilder.getBasicValueFactory().getValue(BaseIdxI +
356                                                                     OffI));
357 
358   // Construct the new ElementRegion.
359   const MemRegion *ArrayR = ElemR->getSuperRegion();
360   return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
361                                                   Ctx));
362 }
363 
364 StoreManager::BindingsHandler::~BindingsHandler() {}
365 
366 bool StoreManager::FindUniqueBinding::HandleBinding(StoreManager& SMgr,
367                                                     Store store,
368                                                     const MemRegion* R,
369                                                     SVal val) {
370   SymbolRef SymV = val.getAsLocSymbol();
371   if (!SymV || SymV != Sym)
372     return true;
373 
374   if (Binding) {
375     First = false;
376     return false;
377   }
378   else
379     Binding = R;
380 
381   return true;
382 }
383