blob: c1b6ed32d5858d3d51438a6e49e5faf06f82a032 [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
Zhongxing Xu7e5d6ed2009-01-08 13:17:14 +000024bool SubRegion::isSubRegionOf(const MemRegion* R) const {
25 const MemRegion* r = getSuperRegion();
26 while (r != 0) {
27 if (r == R)
28 return true;
29 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
30 r = sr->getSuperRegion();
31 else
32 break;
33 }
34 return false;
35}
36
Ted Kremenek9e240492008-10-04 05:50:14 +000037void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
38 ID.AddInteger((unsigned)getKind());
39}
40
Zhongxing Xue9f4e542008-10-25 14:13:41 +000041void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
42 const StringLiteral* Str,
43 const MemRegion* superRegion) {
44 ID.AddInteger((unsigned) StringRegionKind);
45 ID.AddPointer(Str);
46 ID.AddPointer(superRegion);
47}
48
Ted Kremenek7090ae12008-11-02 00:34:33 +000049void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
50 const Expr* Ex, unsigned cnt) {
51 ID.AddInteger((unsigned) AllocaRegionKind);
52 ID.AddPointer(Ex);
53 ID.AddInteger(cnt);
54}
55
56void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
57 ProfileRegion(ID, Ex, Cnt);
58}
59
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000060void AnonTypedRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
61 const MemRegion* superRegion) {
62 ID.AddInteger((unsigned) AnonTypedRegionKind);
63 ID.Add(T);
64 ID.AddPointer(superRegion);
65}
66
Ted Kremenek329d6fd2008-10-27 20:57:58 +000067void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
68 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
69}
70
71void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
72 const CompoundLiteralExpr* CL,
73 const MemRegion* superRegion) {
74 ID.AddInteger((unsigned) CompoundLiteralRegionKind);
75 ID.AddPointer(CL);
76 ID.AddPointer(superRegion);
77}
78
Ted Kremenek9e240492008-10-04 05:50:14 +000079void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl* D,
80 const MemRegion* superRegion, Kind k) {
81 ID.AddInteger((unsigned) k);
82 ID.AddPointer(D);
83 ID.AddPointer(superRegion);
84}
85
86void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
87 DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
88}
89
Ted Kremenek2dabd432008-12-05 02:27:51 +000090void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) {
Ted Kremenek993f1c72008-10-17 20:28:54 +000091 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremenek6d0e2d22008-12-05 02:39:38 +000092 ID.Add(sym);
Ted Kremenek993f1c72008-10-17 20:28:54 +000093}
94
95void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
96 SymbolicRegion::ProfileRegion(ID, sym);
97}
98
Zhongxing Xu511191c2008-10-21 05:27:10 +000099void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SVal Idx,
100 const MemRegion* superRegion) {
101 ID.AddInteger(MemRegion::ElementRegionKind);
102 ID.AddPointer(superRegion);
103 Idx.Profile(ID);
104}
105
106void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
107 ElementRegion::ProfileRegion(ID, Index, superRegion);
108}
Zhongxing Xu27b57062008-10-27 13:17:02 +0000109
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000110QualType ElementRegion::getRValueType(ASTContext& C) const {
111 QualType T = getArrayRegion()->getLValueType(C);
112 // FIXME: Should ArrayType be considered an LValue or RValue type?
Zhongxing Xu56af9772008-11-13 07:30:58 +0000113 if (isa<ArrayType>(T.getTypePtr())) {
114 ArrayType* AT = cast<ArrayType>(T.getTypePtr());
115 return AT->getElementType();
116 }
Ted Kremenek34265e72008-11-17 22:55:12 +0000117 else {
Zhongxing Xu56af9772008-11-13 07:30:58 +0000118 PointerType* PtrT = cast<PointerType>(T.getTypePtr());
119 QualType PTy = PtrT->getPointeeType();
120 return C.getCanonicalType(PTy);
121 }
Zhongxing Xu27b57062008-10-27 13:17:02 +0000122}
123
Ted Kremenek9e240492008-10-04 05:50:14 +0000124//===----------------------------------------------------------------------===//
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000125// getLValueType() and getRValueType()
126//===----------------------------------------------------------------------===//
127
128QualType StringRegion::getRValueType(ASTContext& C) const {
129 return Str->getType();
130}
131
132//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000133// Region pretty-printing.
134//===----------------------------------------------------------------------===//
135
136std::string MemRegion::getString() const {
137 std::string s;
138 llvm::raw_string_ostream os(s);
139 print(os);
140 return os.str();
141}
142
143void MemRegion::print(llvm::raw_ostream& os) const {
144 os << "<Unknown Region>";
145}
146
Ted Kremenek7090ae12008-11-02 00:34:33 +0000147void AllocaRegion::print(llvm::raw_ostream& os) const {
148 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
149}
150
Ted Kremenek500d2ee2008-12-17 19:25:50 +0000151void AnonTypedRegion::print(llvm::raw_ostream& os) const {
152 os << "anon_type{" << T.getAsString() << ',';
153 getSuperRegion()->print(os);
154 os << '}';
155}
156
Ted Kremenek9e240492008-10-04 05:50:14 +0000157void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000158 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenek9e240492008-10-04 05:50:14 +0000159}
160
Ted Kremenek993f1c72008-10-17 20:28:54 +0000161void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek562731e2008-12-05 02:45:20 +0000162 os << "SymRegion-";
163 sym.print(os);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000164}
165
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000166void FieldRegion::print(llvm::raw_ostream& os) const {
167 superRegion->print(os);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000168 os << "->" << getDecl()->getNameAsString();
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000169}
170
Zhongxing Xub21ff772008-10-24 06:30:07 +0000171void ElementRegion::print(llvm::raw_ostream& os) const {
172 superRegion->print(os);
173 os << '['; Index.print(os); os << ']';
174}
175
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000176void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
177 // FIXME: More elaborate pretty-printing.
178 os << "{ " << (void*) CL << " }";
179}
180
Zhongxing Xucc128b32008-11-10 13:05:26 +0000181void StringRegion::print(llvm::raw_ostream& os) const {
Zhongxing Xud2f016f2008-11-11 01:36:32 +0000182 if (Str->isWide()) os << 'L';
183 os << '"';
184
185 // FIXME: this doesn't print wstrings right.
186 for (unsigned i = 0, e = Str->getByteLength(); i != e; ++i) {
187 switch (Str->getStrData()[i]) {
188 default: os << Str->getStrData()[i]; break;
189 // Handle some common ones to make dumps prettier.
190 case '\\': os << "\\\\"; break;
191 case '"': os << "\\\""; break;
192 case '\n': os << "\\n"; break;
193 case '\t': os << "\\t"; break;
194 case '\a': os << "\\a"; break;
195 case '\b': os << "\\b"; break;
196 }
197 }
198 os << '"';
Zhongxing Xucc128b32008-11-10 13:05:26 +0000199}
200
Ted Kremenek9e240492008-10-04 05:50:14 +0000201//===----------------------------------------------------------------------===//
202// MemRegionManager methods.
203//===----------------------------------------------------------------------===//
204
205MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
206
207 if (!region) {
208 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
209 new (region) MemSpaceRegion();
210 }
211
212 return region;
213}
214
215MemSpaceRegion* MemRegionManager::getStackRegion() {
216 return LazyAllocate(stack);
217}
218
219MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
220 return LazyAllocate(globals);
221}
222
223MemSpaceRegion* MemRegionManager::getHeapRegion() {
224 return LazyAllocate(heap);
225}
226
Zhongxing Xu17892752008-10-08 02:50:44 +0000227MemSpaceRegion* MemRegionManager::getUnknownRegion() {
228 return LazyAllocate(unknown);
229}
230
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000231bool MemRegionManager::onStack(const MemRegion* R) {
232 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
233 R = SR->getSuperRegion();
234
235 return (R != 0) && (R == stack);
236}
237
238bool MemRegionManager::onHeap(const MemRegion* R) {
239 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
240 R = SR->getSuperRegion();
241
242 return (R != 0) && (R == heap);
243}
244
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000245StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
246 llvm::FoldingSetNodeID ID;
247 MemSpaceRegion* GlobalsR = getGlobalsRegion();
248
249 StringRegion::ProfileRegion(ID, Str, GlobalsR);
250
251 void* InsertPos;
252 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
253 StringRegion* R = cast_or_null<StringRegion>(data);
254
255 if (!R) {
256 R = (StringRegion*) A.Allocate<StringRegion>();
257 new (R) StringRegion(Str, GlobalsR);
258 Regions.InsertNode(R, InsertPos);
259 }
260
261 return R;
262}
263
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000264VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
265
266 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
267 : getGlobalsRegion();
268
Ted Kremenek9e240492008-10-04 05:50:14 +0000269 llvm::FoldingSetNodeID ID;
270 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
271
272 void* InsertPos;
273 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
274 VarRegion* R = cast_or_null<VarRegion>(data);
275
276 if (!R) {
277 R = (VarRegion*) A.Allocate<VarRegion>();
278 new (R) VarRegion(d, superRegion);
279 Regions.InsertNode(R, InsertPos);
280 }
281
282 return R;
283}
284
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000285CompoundLiteralRegion*
286MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
287 // Is this compound literal allocated on the stack or is part of the
288 // global constant pool?
289 const MemRegion* superRegion = CL->isFileScope() ?
290 getGlobalsRegion() : getStackRegion();
291
292 // Profile the compound literal.
293 llvm::FoldingSetNodeID ID;
294 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
295
296 void* InsertPos;
297 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
298 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
299
300 if (!R) {
301 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
302 new (R) CompoundLiteralRegion(CL, superRegion);
303 Regions.InsertNode(R, InsertPos);
304 }
305
306 return R;
307}
308
Ted Kremenekabb042f2008-12-13 19:24:37 +0000309ElementRegion*
310MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
311
Zhongxing Xu511191c2008-10-21 05:27:10 +0000312 llvm::FoldingSetNodeID ID;
313 ElementRegion::ProfileRegion(ID, Idx, superRegion);
314
315 void* InsertPos;
316 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
317 ElementRegion* R = cast_or_null<ElementRegion>(data);
318
319 if (!R) {
320 R = (ElementRegion*) A.Allocate<ElementRegion>();
321 new (R) ElementRegion(Idx, superRegion);
322 Regions.InsertNode(R, InsertPos);
323 }
324
325 return R;
326}
327
Ted Kremenek993f1c72008-10-17 20:28:54 +0000328/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenek2dabd432008-12-05 02:27:51 +0000329SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000330
331 llvm::FoldingSetNodeID ID;
332 SymbolicRegion::ProfileRegion(ID, sym);
333
334 void* InsertPos;
335 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
336 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
337
338 if (!R) {
339 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
340 new (R) SymbolicRegion(sym);
341 Regions.InsertNode(R, InsertPos);
342 }
343
344 return R;
345}
346
Ted Kremenek9e240492008-10-04 05:50:14 +0000347FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000348 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000349 llvm::FoldingSetNodeID ID;
350 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
351
352 void* InsertPos;
353 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
354 FieldRegion* R = cast_or_null<FieldRegion>(data);
355
356 if (!R) {
357 R = (FieldRegion*) A.Allocate<FieldRegion>();
358 new (R) FieldRegion(d, superRegion);
359 Regions.InsertNode(R, InsertPos);
360 }
361
362 return R;
363}
364
Ted Kremenek993f1c72008-10-17 20:28:54 +0000365ObjCIvarRegion*
366MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
367 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000368 llvm::FoldingSetNodeID ID;
369 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
370
371 void* InsertPos;
372 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
373 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
374
375 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000376 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
377 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000378 Regions.InsertNode(R, InsertPos);
379 }
380
381 return R;
382}
383
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000384ObjCObjectRegion*
385MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
386 const MemRegion* superRegion) {
387 llvm::FoldingSetNodeID ID;
388 DeclRegion::ProfileRegion(ID, d, superRegion,
389 MemRegion::ObjCObjectRegionKind);
390
391 void* InsertPos;
392 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
393 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
394
395 if (!R) {
396 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
397 new (R) ObjCObjectRegion(d, superRegion);
398 Regions.InsertNode(R, InsertPos);
399 }
400
401 return R;
402}
403
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000404AnonTypedRegion*
405MemRegionManager::getAnonTypedRegion(QualType t, const MemRegion* superRegion) {
406 llvm::FoldingSetNodeID ID;
407 AnonTypedRegion::ProfileRegion(ID, t, superRegion);
408
409 void* InsertPos;
410 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
411 AnonTypedRegion* R = cast_or_null<AnonTypedRegion>(data);
412
413 if (!R) {
414 R = (AnonTypedRegion*) A.Allocate<AnonTypedRegion>();
415 new (R) AnonTypedRegion(t, superRegion);
416 Regions.InsertNode(R, InsertPos);
417 }
418
419 return R;
420}
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000421
Ted Kremenek7090ae12008-11-02 00:34:33 +0000422AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
423 llvm::FoldingSetNodeID ID;
424 AllocaRegion::ProfileRegion(ID, E, cnt);
425
426 void* InsertPos;
427 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
428 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
429
430 if (!R) {
431 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
432 new (R) AllocaRegion(E, cnt, getStackRegion());
433 Regions.InsertNode(R, InsertPos);
434 }
435
436 return R;
437}
438
Ted Kremenek9e240492008-10-04 05:50:14 +0000439bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000440
441 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000442 const SubRegion* SR = dyn_cast<SubRegion>(R);
443
Ted Kremenek993f1c72008-10-17 20:28:54 +0000444 if (!SR)
445 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000446
Ted Kremenek9e240492008-10-04 05:50:14 +0000447 MemSpaceRegion* S = getStackRegion();
448
Ted Kremenek993f1c72008-10-17 20:28:54 +0000449 while (SR) {
450 R = SR->getSuperRegion();
451 if (R == S)
452 return true;
453
454 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000455 }
456
457 return false;
458}