blob: e6cb58d819c384a614f84809fb64b189072d42ce [file] [log] [blame]
Zhongxing Xu5d26bc02010-02-03 09:10:32 +00001//=== FlatStore.cpp - Flat region-based store model -------------*- 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
Ted Kremenek21142582010-12-23 19:38:26 +000010#include "clang/StaticAnalyzer/PathSensitive/GRState.h"
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000011#include "llvm/ADT/ImmutableIntervalMap.h"
Daniel Dunbar135da712010-02-08 20:24:21 +000012#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000013
14using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000015using namespace ento;
Zhongxing Xu36d02e02010-02-08 05:40:07 +000016using llvm::Interval;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000017
18// The actual store type.
19typedef llvm::ImmutableIntervalMap<SVal> BindingVal;
20typedef llvm::ImmutableMap<const MemRegion *, BindingVal> RegionBindings;
21
22namespace {
23class FlatStoreManager : public StoreManager {
24 RegionBindings::Factory RBFactory;
25 BindingVal::Factory BVFactory;
26
27public:
28 FlatStoreManager(GRStateManager &mgr)
29 : StoreManager(mgr),
30 RBFactory(mgr.getAllocator()),
31 BVFactory(mgr.getAllocator()) {}
32
Zhongxing Xu36d02e02010-02-08 05:40:07 +000033 SVal Retrieve(Store store, Loc L, QualType T);
34 Store Bind(Store store, Loc L, SVal val);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000035 Store Remove(Store St, Loc L);
Zhongxing Xub4a9c612010-02-05 05:06:13 +000036 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl,
37 const LocationContext *LC, SVal v);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000038
39 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek3baf6722010-11-24 00:54:37 +000040 return RBFactory.getEmptyMap().getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000041 }
42
Zhongxing Xuf5416bd2010-02-05 05:18:47 +000043 SubRegionMap *getSubRegionMap(Store store) {
44 return 0;
45 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000046
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000047 SVal ArrayToPointer(Loc Array);
Ted Kremenekdb0594b2011-01-14 20:34:15 +000048 Store removeDeadBindings(Store store, const StackFrameContext *LCtx,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000049 SymbolReaper& SymReaper,
Zhongxing Xu72119c42010-02-05 05:34:29 +000050 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000051 return store;
Zhongxing Xu72119c42010-02-05 05:34:29 +000052 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000053
Zhongxing Xub4a9c612010-02-05 05:06:13 +000054 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000055
Zhongxing Xub4a9c612010-02-05 05:06:13 +000056 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000057
58 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
59
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000060 Store InvalidateRegions(Store store, const MemRegion * const *I,
61 const MemRegion * const *E, const Expr *Ex,
62 unsigned Count, InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +000063 bool invalidateGlobals, InvalidatedRegions *Regions);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000064
65 void print(Store store, llvm::raw_ostream& Out, const char* nl,
66 const char *sep);
67 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000068
69private:
70 static RegionBindings getRegionBindings(Store store) {
71 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
72 }
73
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000074 class RegionInterval {
75 public:
76 const MemRegion *R;
77 Interval I;
Zhongxing Xue3273e72010-08-03 06:34:25 +000078 RegionInterval(const MemRegion *r, int64_t s, int64_t e) : R(r), I(s, e){}
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000079 };
80
81 RegionInterval RegionToInterval(const MemRegion *R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000082
83 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000084};
85} // end anonymous namespace
86
Ted Kremenek9ef65372010-12-23 07:20:52 +000087StoreManager *ento::CreateFlatStoreManager(GRStateManager &StMgr) {
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000088 return new FlatStoreManager(StMgr);
89}
90
Zhongxing Xu36d02e02010-02-08 05:40:07 +000091SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
92 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000093 RegionInterval RI = RegionToInterval(R);
Jordy Rosee7011172010-08-16 01:15:17 +000094 // FIXME: FlatStore should handle regions with unknown intervals.
95 if (!RI.R)
96 return UnknownVal();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000097
Zhongxing Xu36d02e02010-02-08 05:40:07 +000098 RegionBindings B = getRegionBindings(store);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000099 const BindingVal *BV = B.lookup(RI.R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000100 if (BV) {
Ted Kremenek3baf6722010-11-24 00:54:37 +0000101 const SVal *V = BVFactory.lookup(*BV, RI.I);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000102 if (V)
103 return *V;
104 else
105 return RetrieveRegionWithNoBinding(R, T);
106 }
107 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000108}
109
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000110SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
111 QualType T) {
112 if (R->hasStackNonParametersStorage())
113 return UndefinedVal();
114 else
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000115 return svalBuilder.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000116}
117
118Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
119 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
120 RegionBindings B = getRegionBindings(store);
121 const BindingVal *V = B.lookup(R);
122
Ted Kremenek3baf6722010-11-24 00:54:37 +0000123 BindingVal BV = BVFactory.getEmptyMap();
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000124 if (V)
125 BV = *V;
126
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000127 RegionInterval RI = RegionToInterval(R);
Jordy Rosee7011172010-08-16 01:15:17 +0000128 // FIXME: FlatStore should handle regions with unknown intervals.
129 if (!RI.R)
130 return B.getRoot();
Ted Kremenek3baf6722010-11-24 00:54:37 +0000131 BV = BVFactory.add(BV, RI.I, val);
132 B = RBFactory.add(B, RI.R, BV);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000133 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000134}
135
136Store FlatStoreManager::Remove(Store store, Loc L) {
137 return store;
138}
139
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000140Store FlatStoreManager::BindCompoundLiteral(Store store,
141 const CompoundLiteralExpr* cl,
142 const LocationContext *LC,
143 SVal v) {
144 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000145}
146
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000147SVal FlatStoreManager::ArrayToPointer(Loc Array) {
148 return Array;
149}
150
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000151Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
152 SVal initVal) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000153 return Bind(store, svalBuilder.makeLoc(VR), initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000154}
155
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000156Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
157 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000158}
159
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000160Store FlatStoreManager::InvalidateRegions(Store store,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000161 const MemRegion * const *I,
162 const MemRegion * const *E,
163 const Expr *Ex, unsigned Count,
164 InvalidatedSymbols *IS,
165 bool invalidateGlobals,
166 InvalidatedRegions *Regions) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000167 assert(false && "Not implemented");
168 return store;
169}
170
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000171void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
172 const char* nl, const char *sep) {
173}
174
175void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
176}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000177
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000178FlatStoreManager::RegionInterval
179FlatStoreManager::RegionToInterval(const MemRegion *R) {
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000180 switch (R->getKind()) {
181 case MemRegion::VarRegionKind: {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000182 QualType T = cast<VarRegion>(R)->getValueType();
Zhongxing Xue3273e72010-08-03 06:34:25 +0000183 int64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000184 return RegionInterval(R, 0, Size-1);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000185 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000186
187 case MemRegion::ElementRegionKind:
188 case MemRegion::FieldRegionKind: {
Zhongxing Xue8882332010-08-03 04:52:05 +0000189 RegionOffset Offset = R->getAsOffset();
190 // We cannot compute offset for all regions, for example, elements
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000191 // with symbolic offsets.
192 if (!Offset.getRegion())
193 return RegionInterval(0, 0, 0);
Zhongxing Xue3273e72010-08-03 06:34:25 +0000194 int64_t Start = Offset.getOffset();
Zhongxing Xu018220c2010-08-11 06:10:55 +0000195 int64_t Size = Ctx.getTypeSize(cast<TypedRegion>(R)->getValueType());
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000196 return RegionInterval(Offset.getRegion(), Start, Start+Size);
197 }
198
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000199 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000200 llvm_unreachable("Region kind unhandled.");
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000201 return RegionInterval(0, 0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000202 }
203}