blob: b2d7ad2d8965a67128f817d81360f7e7c11163ab [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
Ted Kremenek0312c0e2009-03-01 05:44:08 +000060void TypedViewRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000061 const MemRegion* superRegion) {
Ted Kremenek0312c0e2009-03-01 05:44:08 +000062 ID.AddInteger((unsigned) TypedViewRegionKind);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000063 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
Zhongxing Xua48f7372009-02-06 08:44:27 +0000117 // Get the type of the symbol.
118 QualType T = data.getType(C);
Zhongxing Xu026c6632009-02-05 06:57:29 +0000119
Zhongxing Xua48f7372009-02-06 08:44:27 +0000120 // Only when the symbol has pointer type it can have a symbolic region
121 // associated with it.
122 PointerType* PTy = cast<PointerType>(T.getTypePtr()->getDesugaredType());
Zhongxing Xu026c6632009-02-05 06:57:29 +0000123
Zhongxing Xua48f7372009-02-06 08:44:27 +0000124 // The type of the symbolic region is the pointee type of the symbol.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000125 return PTy->getPointeeType();
126}
127
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000128QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83183042009-01-24 06:11:36 +0000129 // Strip off typedefs from the ArrayRegion's RvalueType.
130 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xuc496f142009-01-23 10:19:29 +0000131
Ted Kremenek83183042009-01-24 06:11:36 +0000132 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu56af9772008-11-13 07:30:58 +0000133 return AT->getElementType();
Ted Kremenek83183042009-01-24 06:11:36 +0000134
Ted Kremenek14553ab2009-01-30 00:08:43 +0000135 // If the RValueType of the array region isn't an ArrayType, then essentially
136 // the element's
137 return T;
Zhongxing Xu27b57062008-10-27 13:17:02 +0000138}
139
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000140QualType StringRegion::getRValueType(ASTContext& C) const {
141 return Str->getType();
142}
143
144//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000145// Region pretty-printing.
146//===----------------------------------------------------------------------===//
147
148std::string MemRegion::getString() const {
149 std::string s;
150 llvm::raw_string_ostream os(s);
151 print(os);
152 return os.str();
153}
154
155void MemRegion::print(llvm::raw_ostream& os) const {
156 os << "<Unknown Region>";
157}
158
Ted Kremenek7090ae12008-11-02 00:34:33 +0000159void AllocaRegion::print(llvm::raw_ostream& os) const {
160 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
161}
162
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000163void TypedViewRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek500d2ee2008-12-17 19:25:50 +0000164 os << "anon_type{" << T.getAsString() << ',';
165 getSuperRegion()->print(os);
166 os << '}';
167}
168
Ted Kremenek9e240492008-10-04 05:50:14 +0000169void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000170 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenek9e240492008-10-04 05:50:14 +0000171}
172
Ted Kremenek993f1c72008-10-17 20:28:54 +0000173void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek562731e2008-12-05 02:45:20 +0000174 os << "SymRegion-";
175 sym.print(os);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000176}
177
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000178void FieldRegion::print(llvm::raw_ostream& os) const {
179 superRegion->print(os);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000180 os << "->" << getDecl()->getNameAsString();
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000181}
182
Zhongxing Xub21ff772008-10-24 06:30:07 +0000183void ElementRegion::print(llvm::raw_ostream& os) const {
184 superRegion->print(os);
185 os << '['; Index.print(os); os << ']';
186}
187
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000188void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
189 // FIXME: More elaborate pretty-printing.
190 os << "{ " << (void*) CL << " }";
191}
192
Zhongxing Xucc128b32008-11-10 13:05:26 +0000193void StringRegion::print(llvm::raw_ostream& os) const {
Ted Kremeneke2916d62009-01-16 19:26:50 +0000194 Str->printPretty(os);
Zhongxing Xucc128b32008-11-10 13:05:26 +0000195}
196
Ted Kremenek9e240492008-10-04 05:50:14 +0000197//===----------------------------------------------------------------------===//
198// MemRegionManager methods.
199//===----------------------------------------------------------------------===//
200
201MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
202
203 if (!region) {
204 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
205 new (region) MemSpaceRegion();
206 }
207
208 return region;
209}
210
211MemSpaceRegion* MemRegionManager::getStackRegion() {
212 return LazyAllocate(stack);
213}
214
215MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
216 return LazyAllocate(globals);
217}
218
219MemSpaceRegion* MemRegionManager::getHeapRegion() {
220 return LazyAllocate(heap);
221}
222
Zhongxing Xu17892752008-10-08 02:50:44 +0000223MemSpaceRegion* MemRegionManager::getUnknownRegion() {
224 return LazyAllocate(unknown);
225}
226
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000227bool MemRegionManager::onStack(const MemRegion* R) {
228 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
229 R = SR->getSuperRegion();
230
231 return (R != 0) && (R == stack);
232}
233
234bool MemRegionManager::onHeap(const MemRegion* R) {
235 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
236 R = SR->getSuperRegion();
237
238 return (R != 0) && (R == heap);
239}
240
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000241StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
242 llvm::FoldingSetNodeID ID;
243 MemSpaceRegion* GlobalsR = getGlobalsRegion();
244
245 StringRegion::ProfileRegion(ID, Str, GlobalsR);
246
247 void* InsertPos;
248 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
249 StringRegion* R = cast_or_null<StringRegion>(data);
250
251 if (!R) {
252 R = (StringRegion*) A.Allocate<StringRegion>();
253 new (R) StringRegion(Str, GlobalsR);
254 Regions.InsertNode(R, InsertPos);
255 }
256
257 return R;
258}
259
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000260VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
261
262 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
263 : getGlobalsRegion();
264
Ted Kremenek9e240492008-10-04 05:50:14 +0000265 llvm::FoldingSetNodeID ID;
266 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
267
268 void* InsertPos;
269 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
270 VarRegion* R = cast_or_null<VarRegion>(data);
271
272 if (!R) {
273 R = (VarRegion*) A.Allocate<VarRegion>();
274 new (R) VarRegion(d, superRegion);
275 Regions.InsertNode(R, InsertPos);
276 }
277
278 return R;
279}
280
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000281CompoundLiteralRegion*
282MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
283 // Is this compound literal allocated on the stack or is part of the
284 // global constant pool?
285 const MemRegion* superRegion = CL->isFileScope() ?
286 getGlobalsRegion() : getStackRegion();
287
288 // Profile the compound literal.
289 llvm::FoldingSetNodeID ID;
290 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
291
292 void* InsertPos;
293 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
294 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
295
296 if (!R) {
297 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
298 new (R) CompoundLiteralRegion(CL, superRegion);
299 Regions.InsertNode(R, InsertPos);
300 }
301
302 return R;
303}
304
Ted Kremenekabb042f2008-12-13 19:24:37 +0000305ElementRegion*
306MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
307
Zhongxing Xu511191c2008-10-21 05:27:10 +0000308 llvm::FoldingSetNodeID ID;
309 ElementRegion::ProfileRegion(ID, Idx, superRegion);
310
311 void* InsertPos;
312 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
313 ElementRegion* R = cast_or_null<ElementRegion>(data);
314
315 if (!R) {
316 R = (ElementRegion*) A.Allocate<ElementRegion>();
317 new (R) ElementRegion(Idx, superRegion);
318 Regions.InsertNode(R, InsertPos);
319 }
320
321 return R;
322}
323
Ted Kremenek993f1c72008-10-17 20:28:54 +0000324/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Zhongxing Xu026c6632009-02-05 06:57:29 +0000325SymbolicRegion* MemRegionManager::getSymbolicRegion(const SymbolRef sym,
326 const SymbolManager& mgr) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000327
328 llvm::FoldingSetNodeID ID;
329 SymbolicRegion::ProfileRegion(ID, sym);
330
331 void* InsertPos;
332 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
333 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
334
335 if (!R) {
336 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu026c6632009-02-05 06:57:29 +0000337 // SymbolicRegion's storage class is usually unknown.
338 new (R) SymbolicRegion(sym, mgr, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000339 Regions.InsertNode(R, InsertPos);
340 }
341
342 return R;
343}
344
Ted Kremenek9e240492008-10-04 05:50:14 +0000345FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000346 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000347 llvm::FoldingSetNodeID ID;
348 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
349
350 void* InsertPos;
351 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
352 FieldRegion* R = cast_or_null<FieldRegion>(data);
353
354 if (!R) {
355 R = (FieldRegion*) A.Allocate<FieldRegion>();
356 new (R) FieldRegion(d, superRegion);
357 Regions.InsertNode(R, InsertPos);
358 }
359
360 return R;
361}
362
Ted Kremenek993f1c72008-10-17 20:28:54 +0000363ObjCIvarRegion*
364MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
365 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000366 llvm::FoldingSetNodeID ID;
367 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
368
369 void* InsertPos;
370 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
371 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
372
373 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000374 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
375 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000376 Regions.InsertNode(R, InsertPos);
377 }
378
379 return R;
380}
381
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000382ObjCObjectRegion*
383MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
384 const MemRegion* superRegion) {
385 llvm::FoldingSetNodeID ID;
386 DeclRegion::ProfileRegion(ID, d, superRegion,
387 MemRegion::ObjCObjectRegionKind);
388
389 void* InsertPos;
390 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
391 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
392
393 if (!R) {
394 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
395 new (R) ObjCObjectRegion(d, superRegion);
396 Regions.InsertNode(R, InsertPos);
397 }
398
399 return R;
400}
401
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000402TypedViewRegion*
403MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000404 llvm::FoldingSetNodeID ID;
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000405 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000406
407 void* InsertPos;
408 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000409 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000410
411 if (!R) {
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000412 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
413 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000414 Regions.InsertNode(R, InsertPos);
415 }
416
417 return R;
418}
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000419
Ted Kremenek7090ae12008-11-02 00:34:33 +0000420AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
421 llvm::FoldingSetNodeID ID;
422 AllocaRegion::ProfileRegion(ID, E, cnt);
423
424 void* InsertPos;
425 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
426 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
427
428 if (!R) {
429 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
430 new (R) AllocaRegion(E, cnt, getStackRegion());
431 Regions.InsertNode(R, InsertPos);
432 }
433
434 return R;
435}
436
Ted Kremenek9e240492008-10-04 05:50:14 +0000437bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000438
439 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000440 const SubRegion* SR = dyn_cast<SubRegion>(R);
441
Ted Kremenek993f1c72008-10-17 20:28:54 +0000442 if (!SR)
443 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000444
Ted Kremenek9e240492008-10-04 05:50:14 +0000445 MemSpaceRegion* S = getStackRegion();
446
Ted Kremenek993f1c72008-10-17 20:28:54 +0000447 while (SR) {
448 R = SR->getSuperRegion();
449 if (R == S)
450 return true;
451
452 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000453 }
454
455 return false;
456}