blob: 22df07b716445c9dd91ca22a2c4f1da28b872c26 [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 Xudc0a25d2008-11-16 04:07:26 +000047void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
48 const MemRegion* superRegion) {
49 ID.AddInteger((unsigned) AnonTypedRegionKind);
50 ID.Add(T);
51 ID.AddPointer(superRegion);
52}
53
Zhongxing Xu817c67d2008-11-03 04:12:24 +000054QualType AnonPointeeRegion::getType(ASTContext& C) const {
Zhongxing Xu3bb662a2008-12-11 02:27:34 +000055 QualType T = C.getCanonicalType(Pointer->getType(C));
Zhongxing Xu817c67d2008-11-03 04:12:24 +000056 PointerType* PTy = cast<PointerType>(T.getTypePtr());
57
58 QualType PointeeTy = C.getCanonicalType(PTy->getPointeeType());
59 return PointeeTy;
Ted Kremenek9e240492008-10-04 05:50:14 +000060}
61
Zhongxing Xu17892752008-10-08 02:50:44 +000062void AnonPointeeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
Zhongxing Xu3bb662a2008-12-11 02:27:34 +000063 const TypedRegion* R,
Zhongxing Xu17892752008-10-08 02:50:44 +000064 const MemRegion* superRegion) {
65 ID.AddInteger((unsigned) AnonPointeeRegionKind);
Zhongxing Xu3bb662a2008-12-11 02:27:34 +000066 ID.AddPointer(R);
Zhongxing Xu17892752008-10-08 02:50:44 +000067 ID.AddPointer(superRegion);
68}
69
Ted Kremenek329d6fd2008-10-27 20:57:58 +000070void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
71 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
72}
73
74void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
75 const CompoundLiteralExpr* CL,
76 const MemRegion* superRegion) {
77 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
78 ID.AddPointer(CL);
79 ID.AddPointer(superRegion);
80}
81
Ted Kremenek9e240492008-10-04 05:50:14 +000082void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
83 const MemRegion* superRegion, Kind k) {
84 ID.AddInteger((unsigned) k);
85 ID.AddPointer(D);
86 ID.AddPointer(superRegion);
87}
88
89void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
90 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
91}
92
Ted Kremenek2dabd432008-12-05 02:27:51 +000093void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) {
Ted Kremenek993f1c72008-10-17 20:28:54 +000094 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremenek6d0e2d22008-12-05 02:39:38 +000095 ID.Add(sym);
Ted Kremenek993f1c72008-10-17 20:28:54 +000096}
97
98void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
99 SymbolicRegion::ProfileRegion(ID, sym);
100}
101
Zhongxing Xu511191c2008-10-21 05:27:10 +0000102void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx,
103 const MemRegion* superRegion) {
104 ID.AddInteger(MemRegion::ElementRegionKind);
105 ID.AddPointer(superRegion);
106 Idx.Profile(ID);
107}
108
109void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
110 ElementRegion::ProfileRegion(ID, Index, superRegion);
111}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000112
113QualType ElementRegion::getType(ASTContext& C) const {
114 QualType T = cast<TypedRegion>(superRegion)->getType(C);
Zhongxing Xu56af9772008-11-13 07:30:58 +0000115
116 if (isa<ArrayType>(T.getTypePtr())) {
117 ArrayType* AT = cast<ArrayType>(T.getTypePtr());
118 return AT->getElementType();
119 }
Ted Kremenek34265e72008-11-17 22:55:12 +0000120 else {
121 assert (isa<AnonTypedRegion>(superRegion));
Zhongxing Xu56af9772008-11-13 07:30:58 +0000122 PointerType* PtrT = cast<PointerType>(T.getTypePtr());
123 QualType PTy = PtrT->getPointeeType();
124 return C.getCanonicalType(PTy);
125 }
Zhongxing Xu27b57062008-10-27 13:17:02 +0000126}
127
Ted Kremenek9e240492008-10-04 05:50:14 +0000128//===----------------------------------------------------------------------===//
129// Region pretty-printing.
130//===----------------------------------------------------------------------===//
131
132std::string MemRegion::getString() const {
133 std::string s;
134 llvm::raw_string_ostream os(s);
135 print(os);
136 return os.str();
137}
138
139void MemRegion::print(llvm::raw_ostream& os) const {
140 os << "<Unknown Region>";
141}
142
Ted Kremenek7090ae12008-11-02 00:34:33 +0000143void AllocaRegion::print(llvm::raw_ostream& os) const {
144 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
145}
146
Ted Kremenek9e240492008-10-04 05:50:14 +0000147void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000148 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenek9e240492008-10-04 05:50:14 +0000149}
150
Ted Kremenek993f1c72008-10-17 20:28:54 +0000151void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek562731e2008-12-05 02:45:20 +0000152 os << "SymRegion-";
153 sym.print(os);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000154}
155
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000156void FieldRegion::print(llvm::raw_ostream& os) const {
157 superRegion->print(os);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000158 os << "->" << getDecl()->getNameAsString();
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000159}
160
Zhongxing Xub21ff772008-10-24 06:30:07 +0000161void ElementRegion::print(llvm::raw_ostream& os) const {
162 superRegion->print(os);
163 os << '['; Index.print(os); os << ']';
164}
165
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000166void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
167 // FIXME: More elaborate pretty-printing.
168 os << "{ " << (void*) CL << " }";
169}
170
Zhongxing Xucc128b32008-11-10 13:05:26 +0000171void StringRegion::print(llvm::raw_ostream& os) const {
Zhongxing Xud2f016f2008-11-11 01:36:32 +0000172 if (Str->isWide()) os << 'L';
173 os << '"';
174
175 // FIXME: this doesn't print wstrings right.
176 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
177 switch (Str->getStrData()[i]) {
178 default: os << Str->getStrData()[i]; break;
179 // Handle some common ones to make dumps prettier.
180 case '\\': os << "\\\\"; break;
181 case '"': os << "\\\""; break;
182 case '\n': os << "\\n"; break;
183 case '\t': os << "\\t"; break;
184 case '\a': os << "\\a"; break;
185 case '\b': os << "\\b"; break;
186 }
187 }
188 os << '"';
Zhongxing Xucc128b32008-11-10 13:05:26 +0000189}
190
Ted Kremenek9e240492008-10-04 05:50:14 +0000191//===----------------------------------------------------------------------===//
192// MemRegionManager methods.
193//===----------------------------------------------------------------------===//
194
195MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
196
197 if (!region) {
198 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
199 new (region) MemSpaceRegion();
200 }
201
202 return region;
203}
204
205MemSpaceRegion* MemRegionManager::getStackRegion() {
206 return LazyAllocate(stack);
207}
208
209MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
210 return LazyAllocate(globals);
211}
212
213MemSpaceRegion* MemRegionManager::getHeapRegion() {
214 return LazyAllocate(heap);
215}
216
Zhongxing Xu17892752008-10-08 02:50:44 +0000217MemSpaceRegion* MemRegionManager::getUnknownRegion() {
218 return LazyAllocate(unknown);
219}
220
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000221StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
222 llvm::FoldingSetNodeID ID;
223 MemSpaceRegion* GlobalsR = getGlobalsRegion();
224
225 StringRegion::ProfileRegion(ID, Str, GlobalsR);
226
227 void* InsertPos;
228 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
229 StringRegion* R = cast_or_null<StringRegion>(data);
230
231 if (!R) {
232 R = (StringRegion*) A.Allocate<StringRegion>();
233 new (R) StringRegion(Str, GlobalsR);
234 Regions.InsertNode(R, InsertPos);
235 }
236
237 return R;
238}
239
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000240VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
241
242 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
243 : getGlobalsRegion();
244
Ted Kremenek9e240492008-10-04 05:50:14 +0000245 llvm::FoldingSetNodeID ID;
246 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
247
248 void* InsertPos;
249 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
250 VarRegion* R = cast_or_null<VarRegion>(data);
251
252 if (!R) {
253 R = (VarRegion*) A.Allocate<VarRegion>();
254 new (R) VarRegion(d, superRegion);
255 Regions.InsertNode(R, InsertPos);
256 }
257
258 return R;
259}
260
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000261CompoundLiteralRegion*
262MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
263 // Is this compound literal allocated on the stack or is part of the
264 // global constant pool?
265 const MemRegion* superRegion = CL->isFileScope() ?
266 getGlobalsRegion() : getStackRegion();
267
268 // Profile the compound literal.
269 llvm::FoldingSetNodeID ID;
270 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
271
272 void* InsertPos;
273 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
274 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
275
276 if (!R) {
277 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
278 new (R) CompoundLiteralRegion(CL, superRegion);
279 Regions.InsertNode(R, InsertPos);
280 }
281
282 return R;
283}
284
Zhongxing Xu511191c2008-10-21 05:27:10 +0000285ElementRegion* MemRegionManager::getElementRegion(SVal Idx,
286 const MemRegion* superRegion){
287 llvm::FoldingSetNodeID ID;
288 ElementRegion::ProfileRegion(ID, Idx, superRegion);
289
290 void* InsertPos;
291 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
292 ElementRegion* R = cast_or_null<ElementRegion>(data);
293
294 if (!R) {
295 R = (ElementRegion*) A.Allocate<ElementRegion>();
296 new (R) ElementRegion(Idx, superRegion);
297 Regions.InsertNode(R, InsertPos);
298 }
299
300 return R;
301}
302
Ted Kremenek993f1c72008-10-17 20:28:54 +0000303/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenek2dabd432008-12-05 02:27:51 +0000304SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000305
306 llvm::FoldingSetNodeID ID;
307 SymbolicRegion::ProfileRegion(ID, sym);
308
309 void* InsertPos;
310 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
311 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
312
313 if (!R) {
314 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
315 new (R) SymbolicRegion(sym);
316 Regions.InsertNode(R, InsertPos);
317 }
318
319 return R;
320}
321
Ted Kremenek9e240492008-10-04 05:50:14 +0000322FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000323 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000324 llvm::FoldingSetNodeID ID;
325 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
326
327 void* InsertPos;
328 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
329 FieldRegion* R = cast_or_null<FieldRegion>(data);
330
331 if (!R) {
332 R = (FieldRegion*) A.Allocate<FieldRegion>();
333 new (R) FieldRegion(d, superRegion);
334 Regions.InsertNode(R, InsertPos);
335 }
336
337 return R;
338}
339
Ted Kremenek993f1c72008-10-17 20:28:54 +0000340ObjCIvarRegion*
341MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
342 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000343 llvm::FoldingSetNodeID ID;
344 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
345
346 void* InsertPos;
347 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
348 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
349
350 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000351 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
352 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000353 Regions.InsertNode(R, InsertPos);
354 }
355
356 return R;
357}
358
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000359ObjCObjectRegion*
360MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
361 const MemRegion* superRegion) {
362 llvm::FoldingSetNodeID ID;
363 DeclRegion::ProfileRegion(ID, d, superRegion,
364 MemRegion::ObjCObjectRegionKind);
365
366 void* InsertPos;
367 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
368 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
369
370 if (!R) {
371 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
372 new (R) ObjCObjectRegion(d, superRegion);
373 Regions.InsertNode(R, InsertPos);
374 }
375
376 return R;
377}
378
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000379AnonTypedRegion*
380MemRegionManager::getAnonTypedRegion(QualType t, const MemRegion* superRegion) {
381 llvm::FoldingSetNodeID ID;
382 AnonTypedRegion::ProfileRegion(ID, t, superRegion);
383
384 void* InsertPos;
385 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
386 AnonTypedRegion* R = cast_or_null<AnonTypedRegion>(data);
387
388 if (!R) {
389 R = (AnonTypedRegion*) A.Allocate<AnonTypedRegion>();
390 new (R) AnonTypedRegion(t, superRegion);
391 Regions.InsertNode(R, InsertPos);
392 }
393
394 return R;
395}
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000396
Zhongxing Xu3bb662a2008-12-11 02:27:34 +0000397AnonPointeeRegion* MemRegionManager::getAnonPointeeRegion(const TypedRegion* r) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000398 llvm::FoldingSetNodeID ID;
Zhongxing Xu17892752008-10-08 02:50:44 +0000399 MemRegion* superRegion = getUnknownRegion();
400
Zhongxing Xu3bb662a2008-12-11 02:27:34 +0000401 AnonPointeeRegion::ProfileRegion(ID, r, superRegion);
Zhongxing Xu17892752008-10-08 02:50:44 +0000402
403 void* InsertPos;
404 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
405 AnonPointeeRegion* R = cast_or_null<AnonPointeeRegion>(data);
406
407 if (!R) {
408 R = (AnonPointeeRegion*) A.Allocate<AnonPointeeRegion>();
Zhongxing Xu3bb662a2008-12-11 02:27:34 +0000409 new (R) AnonPointeeRegion(r, superRegion);
Zhongxing Xu17892752008-10-08 02:50:44 +0000410 Regions.InsertNode(R, InsertPos);
411 }
412
413 return R;
414}
415
Ted Kremenek7090ae12008-11-02 00:34:33 +0000416AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
417 llvm::FoldingSetNodeID ID;
418 AllocaRegion::ProfileRegion(ID, E, cnt);
419
420 void* InsertPos;
421 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
422 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
423
424 if (!R) {
425 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
426 new (R) AllocaRegion(E, cnt, getStackRegion());
427 Regions.InsertNode(R, InsertPos);
428 }
429
430 return R;
431}
432
Ted Kremenek9e240492008-10-04 05:50:14 +0000433bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000434
435 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000436 const SubRegion* SR = dyn_cast<SubRegion>(R);
437
Ted Kremenek993f1c72008-10-17 20:28:54 +0000438 if (!SR)
439 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000440
Ted Kremenek9e240492008-10-04 05:50:14 +0000441 MemSpaceRegion* S = getStackRegion();
442
Ted Kremenek993f1c72008-10-17 20:28:54 +0000443 while (SR) {
444 R = SR->getSuperRegion();
445 if (R == S)
446 return true;
447
448 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000449 }
450
451 return false;
452}