blob: bda7a632e24b1bab766b6c71e63c1072366a353d [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
10#include "clang/Checker/PathSensitive/GRState.h"
11#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;
Zhongxing Xu36d02e02010-02-08 05:40:07 +000015using llvm::Interval;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000016
17// The actual store type.
18typedef llvm::ImmutableIntervalMap<SVal> BindingVal;
19typedef llvm::ImmutableMap<const MemRegion *, BindingVal> RegionBindings;
20
21namespace {
22class FlatStoreManager : public StoreManager {
23 RegionBindings::Factory RBFactory;
24 BindingVal::Factory BVFactory;
25
26public:
27 FlatStoreManager(GRStateManager &mgr)
28 : StoreManager(mgr),
29 RBFactory(mgr.getAllocator()),
30 BVFactory(mgr.getAllocator()) {}
31
Zhongxing Xu36d02e02010-02-08 05:40:07 +000032 SVal Retrieve(Store store, Loc L, QualType T);
33 Store Bind(Store store, Loc L, SVal val);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000034 Store Remove(Store St, Loc L);
Zhongxing Xub4a9c612010-02-05 05:06:13 +000035 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl,
36 const LocationContext *LC, SVal v);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000037
38 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek3baf6722010-11-24 00:54:37 +000039 return RBFactory.getEmptyMap().getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000040 }
41
Zhongxing Xuf5416bd2010-02-05 05:18:47 +000042 SubRegionMap *getSubRegionMap(Store store) {
43 return 0;
44 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000045
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000046 SVal ArrayToPointer(Loc Array);
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000047 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000048 SymbolReaper& SymReaper,
Zhongxing Xu72119c42010-02-05 05:34:29 +000049 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000050 return store;
Zhongxing Xu72119c42010-02-05 05:34:29 +000051 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000052
Zhongxing Xub4a9c612010-02-05 05:06:13 +000053 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000054
Zhongxing Xub4a9c612010-02-05 05:06:13 +000055 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000056
57 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
58
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000059 Store InvalidateRegions(Store store, const MemRegion * const *I,
60 const MemRegion * const *E, const Expr *Ex,
61 unsigned Count, InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +000062 bool invalidateGlobals, InvalidatedRegions *Regions);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000063
64 void print(Store store, llvm::raw_ostream& Out, const char* nl,
65 const char *sep);
66 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000067
68private:
69 static RegionBindings getRegionBindings(Store store) {
70 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
71 }
72
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000073 class RegionInterval {
74 public:
75 const MemRegion *R;
76 Interval I;
Zhongxing Xue3273e72010-08-03 06:34:25 +000077 RegionInterval(const MemRegion *r, int64_t s, int64_t e) : R(r), I(s, e){}
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000078 };
79
80 RegionInterval RegionToInterval(const MemRegion *R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000081
82 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000083};
84} // end anonymous namespace
85
86StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
87 return new FlatStoreManager(StMgr);
88}
89
Zhongxing Xu36d02e02010-02-08 05:40:07 +000090SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
91 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000092 RegionInterval RI = RegionToInterval(R);
Jordy Rosee7011172010-08-16 01:15:17 +000093 // FIXME: FlatStore should handle regions with unknown intervals.
94 if (!RI.R)
95 return UnknownVal();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000096
Zhongxing Xu36d02e02010-02-08 05:40:07 +000097 RegionBindings B = getRegionBindings(store);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000098 const BindingVal *BV = B.lookup(RI.R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000099 if (BV) {
Ted Kremenek3baf6722010-11-24 00:54:37 +0000100 const SVal *V = BVFactory.lookup(*BV, RI.I);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000101 if (V)
102 return *V;
103 else
104 return RetrieveRegionWithNoBinding(R, T);
105 }
106 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000107}
108
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000109SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
110 QualType T) {
111 if (R->hasStackNonParametersStorage())
112 return UndefinedVal();
113 else
Zhongxing Xu14d23282010-03-01 06:56:52 +0000114 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000115}
116
117Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
118 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
119 RegionBindings B = getRegionBindings(store);
120 const BindingVal *V = B.lookup(R);
121
Ted Kremenek3baf6722010-11-24 00:54:37 +0000122 BindingVal BV = BVFactory.getEmptyMap();
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000123 if (V)
124 BV = *V;
125
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000126 RegionInterval RI = RegionToInterval(R);
Jordy Rosee7011172010-08-16 01:15:17 +0000127 // FIXME: FlatStore should handle regions with unknown intervals.
128 if (!RI.R)
129 return B.getRoot();
Ted Kremenek3baf6722010-11-24 00:54:37 +0000130 BV = BVFactory.add(BV, RI.I, val);
131 B = RBFactory.add(B, RI.R, BV);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000132 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000133}
134
135Store FlatStoreManager::Remove(Store store, Loc L) {
136 return store;
137}
138
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000139Store FlatStoreManager::BindCompoundLiteral(Store store,
140 const CompoundLiteralExpr* cl,
141 const LocationContext *LC,
142 SVal v) {
143 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000144}
145
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000146SVal FlatStoreManager::ArrayToPointer(Loc Array) {
147 return Array;
148}
149
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000150Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
151 SVal initVal) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000152 return Bind(store, ValMgr.makeLoc(VR), initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000153}
154
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000155Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
156 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000157}
158
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000159Store FlatStoreManager::InvalidateRegions(Store store,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000160 const MemRegion * const *I,
161 const MemRegion * const *E,
162 const Expr *Ex, unsigned Count,
163 InvalidatedSymbols *IS,
164 bool invalidateGlobals,
165 InvalidatedRegions *Regions) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000166 assert(false && "Not implemented");
167 return store;
168}
169
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000170void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
171 const char* nl, const char *sep) {
172}
173
174void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
175}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000176
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000177FlatStoreManager::RegionInterval
178FlatStoreManager::RegionToInterval(const MemRegion *R) {
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000179 switch (R->getKind()) {
180 case MemRegion::VarRegionKind: {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000181 QualType T = cast<VarRegion>(R)->getValueType();
Zhongxing Xue3273e72010-08-03 06:34:25 +0000182 int64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000183 return RegionInterval(R, 0, Size-1);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000184 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000185
186 case MemRegion::ElementRegionKind:
187 case MemRegion::FieldRegionKind: {
Zhongxing Xue8882332010-08-03 04:52:05 +0000188 RegionOffset Offset = R->getAsOffset();
189 // We cannot compute offset for all regions, for example, elements
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000190 // with symbolic offsets.
191 if (!Offset.getRegion())
192 return RegionInterval(0, 0, 0);
Zhongxing Xue3273e72010-08-03 06:34:25 +0000193 int64_t Start = Offset.getOffset();
Zhongxing Xu018220c2010-08-11 06:10:55 +0000194 int64_t Size = Ctx.getTypeSize(cast<TypedRegion>(R)->getValueType());
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000195 return RegionInterval(Offset.getRegion(), Start, Start+Size);
196 }
197
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000198 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000199 llvm_unreachable("Region kind unhandled.");
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000200 return RegionInterval(0, 0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000201 }
202}