blob: 81855ba61b59d3e372282902d1629cafe108c846 [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 Xuec13d922009-04-10 08:45:10 +0000110void CodeTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const void* data,
111 QualType t) {
112 ID.AddInteger(MemRegion::CodeTextRegionKind);
113 ID.AddPointer(data);
114 ID.Add(t);
115}
116
117void CodeTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
118 CodeTextRegion::ProfileRegion(ID, Data, LocationType);
119}
120
Zhongxing Xu026c6632009-02-05 06:57:29 +0000121//===----------------------------------------------------------------------===//
122// getLValueType() and getRValueType()
123//===----------------------------------------------------------------------===//
124
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000125QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83183042009-01-24 06:11:36 +0000126 // Strip off typedefs from the ArrayRegion's RvalueType.
127 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xuc496f142009-01-23 10:19:29 +0000128
Ted Kremenek83183042009-01-24 06:11:36 +0000129 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu56af9772008-11-13 07:30:58 +0000130 return AT->getElementType();
Ted Kremenek83183042009-01-24 06:11:36 +0000131
Ted Kremenek14553ab2009-01-30 00:08:43 +0000132 // If the RValueType of the array region isn't an ArrayType, then essentially
133 // the element's
134 return T;
Zhongxing Xu27b57062008-10-27 13:17:02 +0000135}
136
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000137QualType StringRegion::getRValueType(ASTContext& C) const {
138 return Str->getType();
139}
140
141//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000142// Region pretty-printing.
143//===----------------------------------------------------------------------===//
144
145std::string MemRegion::getString() const {
146 std::string s;
147 llvm::raw_string_ostream os(s);
148 print(os);
149 return os.str();
150}
151
152void MemRegion::print(llvm::raw_ostream& os) const {
153 os << "<Unknown Region>";
154}
155
Ted Kremenek7090ae12008-11-02 00:34:33 +0000156void AllocaRegion::print(llvm::raw_ostream& os) const {
157 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
158}
159
Ted Kremenek72e03202009-04-21 19:56:58 +0000160void CodeTextRegion::print(llvm::raw_ostream& os) const {
161 os << "code{";
162 if (isDeclared())
Ted Kremenek82539b02009-04-29 15:37:24 +0000163 os << getDecl()->getDeclName().getAsString();
Ted Kremenek72e03202009-04-21 19:56:58 +0000164 else
165 os << '$' << getSymbol();
166
167 os << '}';
168}
169
Ted Kremenek5639a3e2009-04-21 18:09:22 +0000170void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
171 // FIXME: More elaborate pretty-printing.
172 os << "{ " << (void*) CL << " }";
173}
174
175void ElementRegion::print(llvm::raw_ostream& os) const {
176 superRegion->print(os);
177 os << '['; Index.print(os); os << ']';
178}
179
180void FieldRegion::print(llvm::raw_ostream& os) const {
181 superRegion->print(os);
182 os << "->" << getDecl()->getNameAsString();
183}
184
185void StringRegion::print(llvm::raw_ostream& os) const {
186 Str->printPretty(os);
187}
188
189void SymbolicRegion::print(llvm::raw_ostream& os) const {
190 os << "SymRegion-" << sym;
191}
192
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000193void TypedViewRegion::print(llvm::raw_ostream& os) const {
Ted Kremenekb2dea732009-03-11 21:57:34 +0000194 os << "typed_view{" << LValueType.getAsString() << ',';
Ted Kremenek500d2ee2008-12-17 19:25:50 +0000195 getSuperRegion()->print(os);
196 os << '}';
197}
198
Ted Kremenek9e240492008-10-04 05:50:14 +0000199void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000200 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenek9e240492008-10-04 05:50:14 +0000201}
202
203//===----------------------------------------------------------------------===//
204// MemRegionManager methods.
205//===----------------------------------------------------------------------===//
206
207MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
208
209 if (!region) {
210 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
211 new (region) MemSpaceRegion();
212 }
213
214 return region;
215}
216
217MemSpaceRegion* MemRegionManager::getStackRegion() {
218 return LazyAllocate(stack);
219}
220
221MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
222 return LazyAllocate(globals);
223}
224
225MemSpaceRegion* MemRegionManager::getHeapRegion() {
226 return LazyAllocate(heap);
227}
228
Zhongxing Xu17892752008-10-08 02:50:44 +0000229MemSpaceRegion* MemRegionManager::getUnknownRegion() {
230 return LazyAllocate(unknown);
231}
232
Zhongxing Xuec13d922009-04-10 08:45:10 +0000233MemSpaceRegion* MemRegionManager::getCodeRegion() {
234 return LazyAllocate(code);
235}
236
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000237bool MemRegionManager::onStack(const MemRegion* R) {
238 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
239 R = SR->getSuperRegion();
240
241 return (R != 0) && (R == stack);
242}
243
244bool MemRegionManager::onHeap(const MemRegion* R) {
245 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
246 R = SR->getSuperRegion();
247
248 return (R != 0) && (R == heap);
249}
250
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000251StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
252 llvm::FoldingSetNodeID ID;
253 MemSpaceRegion* GlobalsR = getGlobalsRegion();
254
255 StringRegion::ProfileRegion(ID, Str, GlobalsR);
256
257 void* InsertPos;
258 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
259 StringRegion* R = cast_or_null<StringRegion>(data);
260
261 if (!R) {
262 R = (StringRegion*) A.Allocate<StringRegion>();
263 new (R) StringRegion(Str, GlobalsR);
264 Regions.InsertNode(R, InsertPos);
265 }
266
267 return R;
268}
269
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000270VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
271
272 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
273 : getGlobalsRegion();
274
Ted Kremenek9e240492008-10-04 05:50:14 +0000275 llvm::FoldingSetNodeID ID;
276 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
277
278 void* InsertPos;
279 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
280 VarRegion* R = cast_or_null<VarRegion>(data);
281
282 if (!R) {
283 R = (VarRegion*) A.Allocate<VarRegion>();
284 new (R) VarRegion(d, superRegion);
285 Regions.InsertNode(R, InsertPos);
286 }
287
288 return R;
289}
290
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000291CompoundLiteralRegion*
292MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
293 // Is this compound literal allocated on the stack or is part of the
294 // global constant pool?
295 const MemRegion* superRegion = CL->isFileScope() ?
296 getGlobalsRegion() : getStackRegion();
297
298 // Profile the compound literal.
299 llvm::FoldingSetNodeID ID;
300 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
301
302 void* InsertPos;
303 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
304 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
305
306 if (!R) {
307 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
308 new (R) CompoundLiteralRegion(CL, superRegion);
309 Regions.InsertNode(R, InsertPos);
310 }
311
312 return R;
313}
314
Ted Kremenekabb042f2008-12-13 19:24:37 +0000315ElementRegion*
316MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
317
Zhongxing Xu511191c2008-10-21 05:27:10 +0000318 llvm::FoldingSetNodeID ID;
319 ElementRegion::ProfileRegion(ID, Idx, superRegion);
320
321 void* InsertPos;
322 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
323 ElementRegion* R = cast_or_null<ElementRegion>(data);
324
325 if (!R) {
326 R = (ElementRegion*) A.Allocate<ElementRegion>();
327 new (R) ElementRegion(Idx, superRegion);
328 Regions.InsertNode(R, InsertPos);
329 }
330
331 return R;
332}
333
Zhongxing Xuec13d922009-04-10 08:45:10 +0000334CodeTextRegion* MemRegionManager::getCodeTextRegion(const FunctionDecl* fd,
335 QualType t) {
336 llvm::FoldingSetNodeID ID;
337 CodeTextRegion::ProfileRegion(ID, fd, t);
338 void* InsertPos;
339 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
340 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
341
342 if (!R) {
343 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
344 new (R) CodeTextRegion(fd, t, getCodeRegion());
345 Regions.InsertNode(R, InsertPos);
346 }
347
348 return R;
349}
350
351CodeTextRegion* MemRegionManager::getCodeTextRegion(SymbolRef sym, QualType t) {
352 llvm::FoldingSetNodeID ID;
353 CodeTextRegion::ProfileRegion(ID, sym, t);
354 void* InsertPos;
355 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
356 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
357
358 if (!R) {
359 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
360 new (R) CodeTextRegion(sym, t, getCodeRegion());
361 Regions.InsertNode(R, InsertPos);
362 }
363
364 return R;
365}
366
Ted Kremenek993f1c72008-10-17 20:28:54 +0000367/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000368SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000369 llvm::FoldingSetNodeID ID;
370 SymbolicRegion::ProfileRegion(ID, sym);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000371 void* InsertPos;
372 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
373 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
374
375 if (!R) {
376 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu026c6632009-02-05 06:57:29 +0000377 // SymbolicRegion's storage class is usually unknown.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000378 new (R) SymbolicRegion(sym, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000379 Regions.InsertNode(R, InsertPos);
380 }
381
382 return R;
383}
384
Ted Kremenek9e240492008-10-04 05:50:14 +0000385FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000386 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000387 llvm::FoldingSetNodeID ID;
388 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
389
390 void* InsertPos;
391 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
392 FieldRegion* R = cast_or_null<FieldRegion>(data);
393
394 if (!R) {
395 R = (FieldRegion*) A.Allocate<FieldRegion>();
396 new (R) FieldRegion(d, superRegion);
397 Regions.InsertNode(R, InsertPos);
398 }
399
400 return R;
401}
402
Ted Kremenek993f1c72008-10-17 20:28:54 +0000403ObjCIvarRegion*
404MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
405 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000406 llvm::FoldingSetNodeID ID;
407 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
408
409 void* InsertPos;
410 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
411 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
412
413 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000414 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
415 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000416 Regions.InsertNode(R, InsertPos);
417 }
418
419 return R;
420}
421
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000422ObjCObjectRegion*
423MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
424 const MemRegion* superRegion) {
425 llvm::FoldingSetNodeID ID;
426 DeclRegion::ProfileRegion(ID, d, superRegion,
427 MemRegion::ObjCObjectRegionKind);
428
429 void* InsertPos;
430 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
431 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
432
433 if (!R) {
434 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
435 new (R) ObjCObjectRegion(d, superRegion);
436 Regions.InsertNode(R, InsertPos);
437 }
438
439 return R;
440}
441
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000442TypedViewRegion*
443MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000444 llvm::FoldingSetNodeID ID;
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000445 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000446
447 void* InsertPos;
448 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000449 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000450
451 if (!R) {
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000452 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
453 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000454 Regions.InsertNode(R, InsertPos);
455 }
456
457 return R;
458}
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000459
Ted Kremenek7090ae12008-11-02 00:34:33 +0000460AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
461 llvm::FoldingSetNodeID ID;
462 AllocaRegion::ProfileRegion(ID, E, cnt);
463
464 void* InsertPos;
465 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
466 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
467
468 if (!R) {
469 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
470 new (R) AllocaRegion(E, cnt, getStackRegion());
471 Regions.InsertNode(R, InsertPos);
472 }
473
474 return R;
475}
476
Ted Kremenek9e240492008-10-04 05:50:14 +0000477bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000478
479 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000480 const SubRegion* SR = dyn_cast<SubRegion>(R);
481
Ted Kremenek993f1c72008-10-17 20:28:54 +0000482 if (!SR)
483 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000484
Ted Kremenek9e240492008-10-04 05:50:14 +0000485 MemSpaceRegion* S = getStackRegion();
486
Ted Kremenek993f1c72008-10-17 20:28:54 +0000487 while (SR) {
488 R = SR->getSuperRegion();
489 if (R == S)
490 return true;
491
492 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000493 }
Ted Kremenek1670e402009-04-11 00:11:10 +0000494
Ted Kremenek9e240492008-10-04 05:50:14 +0000495 return false;
496}
Ted Kremenek1670e402009-04-11 00:11:10 +0000497
498
499//===----------------------------------------------------------------------===//
500// View handling.
501//===----------------------------------------------------------------------===//
502
503const MemRegion *TypedViewRegion::removeViews() const {
504 const SubRegion *SR = this;
505 const MemRegion *R = SR;
506 while (SR && isa<TypedViewRegion>(SR)) {
507 R = SR->getSuperRegion();
508 SR = dyn_cast<SubRegion>(R);
509 }
510 return R;
511}