blob: e41c5f937b632226098e88e49ca810cd86e7a165 [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
Zhongxing Xu026c6632009-02-05 06:57:29 +0000110//===----------------------------------------------------------------------===//
111// getLValueType() and getRValueType()
112//===----------------------------------------------------------------------===//
113
114QualType SymbolicRegion::getRValueType(ASTContext& C) const {
115 const SymbolData& data = SymMgr.getSymbolData(sym);
116
117 // FIXME: We could use the SymbolManager::getType() directly. But that
118 // would hide the assumptions we made here. What is the type of a symbolic
119 // region is unclear for other cases.
120
121 // For now we assume the symbol is a typed region rvalue.
122 const TypedRegion* R
123 = cast<TypedRegion>(cast<SymbolRegionRValue>(data).getRegion());
124
125 // Assume the region rvalue has a pointer type, only then we could have a
126 // symbolic region associated with it.
127 PointerType* PTy = cast<PointerType>(R->getRValueType(C).getTypePtr());
128
129 return PTy->getPointeeType();
130}
131
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000132QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83183042009-01-24 06:11:36 +0000133 // Strip off typedefs from the ArrayRegion's RvalueType.
134 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xuc496f142009-01-23 10:19:29 +0000135
Ted Kremenek83183042009-01-24 06:11:36 +0000136 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu56af9772008-11-13 07:30:58 +0000137 return AT->getElementType();
Ted Kremenek83183042009-01-24 06:11:36 +0000138
Ted Kremenek14553ab2009-01-30 00:08:43 +0000139 // If the RValueType of the array region isn't an ArrayType, then essentially
140 // the element's
141 return T;
Zhongxing Xu27b57062008-10-27 13:17:02 +0000142}
143
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000144QualType StringRegion::getRValueType(ASTContext& C) const {
145 return Str->getType();
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000149// Region pretty-printing.
150//===----------------------------------------------------------------------===//
151
152std::string MemRegion::getString() const {
153 std::string s;
154 llvm::raw_string_ostream os(s);
155 print(os);
156 return os.str();
157}
158
159void MemRegion::print(llvm::raw_ostream& os) const {
160 os << "<Unknown Region>";
161}
162
Ted Kremenek7090ae12008-11-02 00:34:33 +0000163void AllocaRegion::print(llvm::raw_ostream& os) const {
164 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
165}
166
Ted Kremenek500d2ee2008-12-17 19:25:50 +0000167void AnonTypedRegion::print(llvm::raw_ostream& os) const {
168 os << "anon_type{" << T.getAsString() << ',';
169 getSuperRegion()->print(os);
170 os << '}';
171}
172
Ted Kremenek9e240492008-10-04 05:50:14 +0000173void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000174 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenek9e240492008-10-04 05:50:14 +0000175}
176
Ted Kremenek993f1c72008-10-17 20:28:54 +0000177void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek562731e2008-12-05 02:45:20 +0000178 os << "SymRegion-";
179 sym.print(os);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000180}
181
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000182void FieldRegion::print(llvm::raw_ostream& os) const {
183 superRegion->print(os);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000184 os << "->" << getDecl()->getNameAsString();
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000185}
186
Zhongxing Xub21ff772008-10-24 06:30:07 +0000187void ElementRegion::print(llvm::raw_ostream& os) const {
188 superRegion->print(os);
189 os << '['; Index.print(os); os << ']';
190}
191
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000192void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
193 // FIXME: More elaborate pretty-printing.
194 os << "{ " << (void*) CL << " }";
195}
196
Zhongxing Xucc128b32008-11-10 13:05:26 +0000197void StringRegion::print(llvm::raw_ostream& os) const {
Ted Kremeneke2916d62009-01-16 19:26:50 +0000198 Str->printPretty(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.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000329SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym,
330 const SymbolManager& mgr) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000331
332 llvm::FoldingSetNodeID ID;
333 SymbolicRegion::ProfileRegion(ID, sym);
334
335 void* InsertPos;
336 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
337 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
338
339 if (!R) {
340 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu026c6632009-02-05 06:57:29 +0000341 // SymbolicRegion's storage class is usually unknown.
342 new (R) SymbolicRegion(sym, mgr, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000343 Regions.InsertNode(R, InsertPos);
344 }
345
346 return R;
347}
348
Ted Kremenek9e240492008-10-04 05:50:14 +0000349FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000350 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000351 llvm::FoldingSetNodeID ID;
352 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
353
354 void* InsertPos;
355 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
356 FieldRegion* R = cast_or_null<FieldRegion>(data);
357
358 if (!R) {
359 R = (FieldRegion*) A.Allocate<FieldRegion>();
360 new (R) FieldRegion(d, superRegion);
361 Regions.InsertNode(R, InsertPos);
362 }
363
364 return R;
365}
366
Ted Kremenek993f1c72008-10-17 20:28:54 +0000367ObjCIvarRegion*
368MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
369 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000370 llvm::FoldingSetNodeID ID;
371 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
372
373 void* InsertPos;
374 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
375 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
376
377 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000378 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
379 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000380 Regions.InsertNode(R, InsertPos);
381 }
382
383 return R;
384}
385
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000386ObjCObjectRegion*
387MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
388 const MemRegion* superRegion) {
389 llvm::FoldingSetNodeID ID;
390 DeclRegion::ProfileRegion(ID, d, superRegion,
391 MemRegion::ObjCObjectRegionKind);
392
393 void* InsertPos;
394 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
395 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
396
397 if (!R) {
398 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
399 new (R) ObjCObjectRegion(d, superRegion);
400 Regions.InsertNode(R, InsertPos);
401 }
402
403 return R;
404}
405
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000406AnonTypedRegion*
407MemRegionManager::getAnonTypedRegion(QualType t, const MemRegion* superRegion) {
408 llvm::FoldingSetNodeID ID;
409 AnonTypedRegion::ProfileRegion(ID, t, superRegion);
410
411 void* InsertPos;
412 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
413 AnonTypedRegion* R = cast_or_null<AnonTypedRegion>(data);
414
415 if (!R) {
416 R = (AnonTypedRegion*) A.Allocate<AnonTypedRegion>();
417 new (R) AnonTypedRegion(t, superRegion);
418 Regions.InsertNode(R, InsertPos);
419 }
420
421 return R;
422}
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000423
Ted Kremenek7090ae12008-11-02 00:34:33 +0000424AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
425 llvm::FoldingSetNodeID ID;
426 AllocaRegion::ProfileRegion(ID, E, cnt);
427
428 void* InsertPos;
429 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
430 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
431
432 if (!R) {
433 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
434 new (R) AllocaRegion(E, cnt, getStackRegion());
435 Regions.InsertNode(R, InsertPos);
436 }
437
438 return R;
439}
440
Ted Kremenek9e240492008-10-04 05:50:14 +0000441bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000442
443 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000444 const SubRegion* SR = dyn_cast<SubRegion>(R);
445
Ted Kremenek993f1c72008-10-17 20:28:54 +0000446 if (!SR)
447 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000448
Ted Kremenek9e240492008-10-04 05:50:14 +0000449 MemSpaceRegion* S = getStackRegion();
450
Ted Kremenek993f1c72008-10-17 20:28:54 +0000451 while (SR) {
452 R = SR->getSuperRegion();
453 if (R == S)
454 return true;
455
456 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000457 }
458
459 return false;
460}