blob: 85ad3d87fa086e4a56c4a18e1996292bb36a9fc5 [file] [log] [blame]
Ted Kremenek9e240492008-10-04 05:50:14 +00001//== MemRegion.cpp - Abstract memory regions for static analysis --*- 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 defines MemRegion and its subclasses. MemRegion defines a
11// partially-typed abstraction of memory useful for path-sensitive dataflow
12// analyses.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Support/raw_ostream.h"
17#include "clang/Analysis/PathSensitive/MemRegion.h"
18
19using namespace clang;
20
21
22MemRegion::~MemRegion() {}
23
24void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
25 ID.AddInteger((unsigned)getKind());
26}
27
Zhongxing Xue9f4e542008-10-25 14:13:41 +000028void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
29 const StringLiteral* Str,
30 const MemRegion* superRegion) {
31 ID.AddInteger((unsigned) StringRegionKind);
32 ID.AddPointer(Str);
33 ID.AddPointer(superRegion);
34}
35
Ted Kremenek7090ae12008-11-02 00:34:33 +000036void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
37 const Expr* Ex, unsigned cnt) {
38 ID.AddInteger((unsigned) AllocaRegionKind);
39 ID.AddPointer(Ex);
40 ID.AddInteger(cnt);
41}
42
43void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
44 ProfileRegion(ID, Ex, Cnt);
45}
46
Zhongxing Xu817c67d2008-11-03 04:12:24 +000047QualType AnonPointeeRegion::getType(ASTContext& C) const {
48 QualType T = C.getCanonicalType(Pointer->getType());
49 PointerType* PTy = cast<PointerType>(T.getTypePtr());
50
51 QualType PointeeTy = C.getCanonicalType(PTy->getPointeeType());
52 return PointeeTy;
Ted Kremenek9e240492008-10-04 05:50:14 +000053}
54
Zhongxing Xu17892752008-10-08 02:50:44 +000055void AnonPointeeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Zhongxing Xu817c67d2008-11-03 04:12:24 +000056 const VarDecl* VD,
Zhongxing Xu17892752008-10-08 02:50:44 +000057 const MemRegion* superRegion) {
58 ID.AddInteger((unsigned) AnonPointeeRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +000059 ID.AddPointer(VD);
60 ID.AddPointer(superRegion);
61}
62
Ted Kremenek329d6fd2008-10-27 20:57:58 +000063void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
64 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
65}
66
67void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
68 const CompoundLiteralExpr* CL,
69 const MemRegion* superRegion) {
70 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
71 ID.AddPointer(CL);
72 ID.AddPointer(superRegion);
73}
74
Ted Kremenek9e240492008-10-04 05:50:14 +000075void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
76 const MemRegion* superRegion, Kind k) {
77 ID.AddInteger((unsigned) k);
78 ID.AddPointer(D);
79 ID.AddPointer(superRegion);
80}
81
82void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
83 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
84}
85
Ted Kremenek993f1c72008-10-17 20:28:54 +000086void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolID sym) {
87 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
88 ID.AddInteger(sym.getNumber());
89}
90
91void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
92 SymbolicRegion::ProfileRegion(ID, sym);
93}
94
Zhongxing Xu511191c2008-10-21 05:27:10 +000095void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx,
96 const MemRegion* superRegion) {
97 ID.AddInteger(MemRegion::ElementRegionKind);
98 ID.AddPointer(superRegion);
99 Idx.Profile(ID);
100}
101
102void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
103 ElementRegion::ProfileRegion(ID, Index, superRegion);
104}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000105
106QualType ElementRegion::getType(ASTContext& C) const {
107 QualType T = cast<TypedRegion>(superRegion)->getType(C);
108 ArrayType* AT = cast<ArrayType>(T.getTypePtr());
109 return AT->getElementType();
110}
111
Ted Kremenek9e240492008-10-04 05:50:14 +0000112//===----------------------------------------------------------------------===//
113// Region pretty-printing.
114//===----------------------------------------------------------------------===//
115
116std::string MemRegion::getString() const {
117 std::string s;
118 llvm::raw_string_ostream os(s);
119 print(os);
120 return os.str();
121}
122
123void MemRegion::print(llvm::raw_ostream& os) const {
124 os << "<Unknown Region>";
125}
126
Ted Kremenek7090ae12008-11-02 00:34:33 +0000127void AllocaRegion::print(llvm::raw_ostream& os) const {
128 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
129}
130
Ted Kremenek9e240492008-10-04 05:50:14 +0000131void VarRegion::print(llvm::raw_ostream& os) const {
132 os << cast<VarDecl>(D)->getName();
133}
134
Ted Kremenek993f1c72008-10-17 20:28:54 +0000135void SymbolicRegion::print(llvm::raw_ostream& os) const {
136 os << "$" << sym.getNumber();
137}
138
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000139void FieldRegion::print(llvm::raw_ostream& os) const {
140 superRegion->print(os);
141 os << "->" << getDecl()->getName();
142}
143
Zhongxing Xub21ff772008-10-24 06:30:07 +0000144void ElementRegion::print(llvm::raw_ostream& os) const {
145 superRegion->print(os);
146 os << '['; Index.print(os); os << ']';
147}
148
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000149void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
150 // FIXME: More elaborate pretty-printing.
151 os << "{ " << (void*) CL << " }";
152}
153
Ted Kremenek9e240492008-10-04 05:50:14 +0000154//===----------------------------------------------------------------------===//
155// MemRegionManager methods.
156//===----------------------------------------------------------------------===//
157
158MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
159
160 if (!region) {
161 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
162 new (region) MemSpaceRegion();
163 }
164
165 return region;
166}
167
168MemSpaceRegion* MemRegionManager::getStackRegion() {
169 return LazyAllocate(stack);
170}
171
172MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
173 return LazyAllocate(globals);
174}
175
176MemSpaceRegion* MemRegionManager::getHeapRegion() {
177 return LazyAllocate(heap);
178}
179
Zhongxing Xu17892752008-10-08 02:50:44 +0000180MemSpaceRegion* MemRegionManager::getUnknownRegion() {
181 return LazyAllocate(unknown);
182}
183
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000184StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
185 llvm::FoldingSetNodeID ID;
186 MemSpaceRegion* GlobalsR = getGlobalsRegion();
187
188 StringRegion::ProfileRegion(ID, Str, GlobalsR);
189
190 void* InsertPos;
191 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
192 StringRegion* R = cast_or_null<StringRegion>(data);
193
194 if (!R) {
195 R = (StringRegion*) A.Allocate<StringRegion>();
196 new (R) StringRegion(Str, GlobalsR);
197 Regions.InsertNode(R, InsertPos);
198 }
199
200 return R;
201}
202
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000203VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
204
205 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
206 : getGlobalsRegion();
207
Ted Kremenek9e240492008-10-04 05:50:14 +0000208 llvm::FoldingSetNodeID ID;
209 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
210
211 void* InsertPos;
212 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
213 VarRegion* R = cast_or_null<VarRegion>(data);
214
215 if (!R) {
216 R = (VarRegion*) A.Allocate<VarRegion>();
217 new (R) VarRegion(d, superRegion);
218 Regions.InsertNode(R, InsertPos);
219 }
220
221 return R;
222}
223
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000224CompoundLiteralRegion*
225MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
226 // Is this compound literal allocated on the stack or is part of the
227 // global constant pool?
228 const MemRegion* superRegion = CL->isFileScope() ?
229 getGlobalsRegion() : getStackRegion();
230
231 // Profile the compound literal.
232 llvm::FoldingSetNodeID ID;
233 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
234
235 void* InsertPos;
236 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
237 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
238
239 if (!R) {
240 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
241 new (R) CompoundLiteralRegion(CL, superRegion);
242 Regions.InsertNode(R, InsertPos);
243 }
244
245 return R;
246}
247
Zhongxing Xu511191c2008-10-21 05:27:10 +0000248ElementRegion* MemRegionManager::getElementRegion(SVal Idx,
249 const MemRegion* superRegion){
250 llvm::FoldingSetNodeID ID;
251 ElementRegion::ProfileRegion(ID, Idx, superRegion);
252
253 void* InsertPos;
254 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
255 ElementRegion* R = cast_or_null<ElementRegion>(data);
256
257 if (!R) {
258 R = (ElementRegion*) A.Allocate<ElementRegion>();
259 new (R) ElementRegion(Idx, superRegion);
260 Regions.InsertNode(R, InsertPos);
261 }
262
263 return R;
264}
265
Ted Kremenek993f1c72008-10-17 20:28:54 +0000266/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
267SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolID sym) {
268
269 llvm::FoldingSetNodeID ID;
270 SymbolicRegion::ProfileRegion(ID, sym);
271
272 void* InsertPos;
273 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
274 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
275
276 if (!R) {
277 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
278 new (R) SymbolicRegion(sym);
279 Regions.InsertNode(R, InsertPos);
280 }
281
282 return R;
283}
284
Ted Kremenek9e240492008-10-04 05:50:14 +0000285FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000286 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000287 llvm::FoldingSetNodeID ID;
288 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
289
290 void* InsertPos;
291 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
292 FieldRegion* R = cast_or_null<FieldRegion>(data);
293
294 if (!R) {
295 R = (FieldRegion*) A.Allocate<FieldRegion>();
296 new (R) FieldRegion(d, superRegion);
297 Regions.InsertNode(R, InsertPos);
298 }
299
300 return R;
301}
302
Ted Kremenek993f1c72008-10-17 20:28:54 +0000303ObjCIvarRegion*
304MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
305 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000306 llvm::FoldingSetNodeID ID;
307 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
308
309 void* InsertPos;
310 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
311 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
312
313 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000314 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
315 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000316 Regions.InsertNode(R, InsertPos);
317 }
318
319 return R;
320}
321
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000322ObjCObjectRegion*
323MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
324 const MemRegion* superRegion) {
325 llvm::FoldingSetNodeID ID;
326 DeclRegion::ProfileRegion(ID, d, superRegion,
327 MemRegion::ObjCObjectRegionKind);
328
329 void* InsertPos;
330 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
331 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
332
333 if (!R) {
334 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
335 new (R) ObjCObjectRegion(d, superRegion);
336 Regions.InsertNode(R, InsertPos);
337 }
338
339 return R;
340}
341
342
Zhongxing Xu17892752008-10-08 02:50:44 +0000343AnonPointeeRegion* MemRegionManager::getAnonPointeeRegion(const VarDecl* d) {
344 llvm::FoldingSetNodeID ID;
Zhongxing Xu17892752008-10-08 02:50:44 +0000345 MemRegion* superRegion = getUnknownRegion();
346
Zhongxing Xu817c67d2008-11-03 04:12:24 +0000347 AnonPointeeRegion::ProfileRegion(ID, d, superRegion);
Zhongxing Xu17892752008-10-08 02:50:44 +0000348
349 void* InsertPos;
350 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
351 AnonPointeeRegion* R = cast_or_null<AnonPointeeRegion>(data);
352
353 if (!R) {
354 R = (AnonPointeeRegion*) A.Allocate<AnonPointeeRegion>();
Zhongxing Xu817c67d2008-11-03 04:12:24 +0000355 new (R) AnonPointeeRegion(d, superRegion);
Zhongxing Xu17892752008-10-08 02:50:44 +0000356 Regions.InsertNode(R, InsertPos);
357 }
358
359 return R;
360}
361
Ted Kremenek7090ae12008-11-02 00:34:33 +0000362AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
363 llvm::FoldingSetNodeID ID;
364 AllocaRegion::ProfileRegion(ID, E, cnt);
365
366 void* InsertPos;
367 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
368 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
369
370 if (!R) {
371 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
372 new (R) AllocaRegion(E, cnt, getStackRegion());
373 Regions.InsertNode(R, InsertPos);
374 }
375
376 return R;
377}
378
Ted Kremenek9e240492008-10-04 05:50:14 +0000379bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000380
381 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000382 const SubRegion* SR = dyn_cast<SubRegion>(R);
383
Ted Kremenek993f1c72008-10-17 20:28:54 +0000384 if (!SR)
385 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000386
Ted Kremenek9e240492008-10-04 05:50:14 +0000387 MemSpaceRegion* S = getStackRegion();
388
Ted Kremenek993f1c72008-10-17 20:28:54 +0000389 while (SR) {
390 R = SR->getSuperRegion();
391 if (R == S)
392 return true;
393
394 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000395 }
396
397 return false;
398}