blob: 7c13f0701e7e4a374458c031352ee25a32cc3bad [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
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000114QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83183042009-01-24 06:11:36 +0000115 // Strip off typedefs from the ArrayRegion's RvalueType.
116 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xuc496f142009-01-23 10:19:29 +0000117
Ted Kremenek83183042009-01-24 06:11:36 +0000118 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu56af9772008-11-13 07:30:58 +0000119 return AT->getElementType();
Ted Kremenek83183042009-01-24 06:11:36 +0000120
Ted Kremenek14553ab2009-01-30 00:08:43 +0000121 // If the RValueType of the array region isn't an ArrayType, then essentially
122 // the element's
123 return T;
Zhongxing Xu27b57062008-10-27 13:17:02 +0000124}
125
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000126QualType StringRegion::getRValueType(ASTContext& C) const {
127 return Str->getType();
128}
129
130//===----------------------------------------------------------------------===//
Ted Kremenek9e240492008-10-04 05:50:14 +0000131// Region pretty-printing.
132//===----------------------------------------------------------------------===//
133
134std::string MemRegion::getString() const {
135 std::string s;
136 llvm::raw_string_ostream os(s);
137 print(os);
138 return os.str();
139}
140
141void MemRegion::print(llvm::raw_ostream& os) const {
142 os << "<Unknown Region>";
143}
144
Ted Kremenek7090ae12008-11-02 00:34:33 +0000145void AllocaRegion::print(llvm::raw_ostream& os) const {
146 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
147}
148
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000149void TypedViewRegion::print(llvm::raw_ostream& os) const {
Ted Kremenekb2dea732009-03-11 21:57:34 +0000150 os << "typed_view{" << LValueType.getAsString() << ',';
Ted Kremenek500d2ee2008-12-17 19:25:50 +0000151 getSuperRegion()->print(os);
152 os << '}';
153}
154
Ted Kremenek9e240492008-10-04 05:50:14 +0000155void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000156 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenek9e240492008-10-04 05:50:14 +0000157}
158
Ted Kremenek993f1c72008-10-17 20:28:54 +0000159void SymbolicRegion::print(llvm::raw_ostream& os) const {
Ted Kremenek94c96982009-03-03 22:06:47 +0000160 os << "SymRegion-" << sym;
Ted Kremenek993f1c72008-10-17 20:28:54 +0000161}
162
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000163void FieldRegion::print(llvm::raw_ostream& os) const {
164 superRegion->print(os);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000165 os << "->" << getDecl()->getNameAsString();
Ted Kremenek4bd1eef2008-10-17 21:05:44 +0000166}
167
Zhongxing Xub21ff772008-10-24 06:30:07 +0000168void ElementRegion::print(llvm::raw_ostream& os) const {
169 superRegion->print(os);
170 os << '['; Index.print(os); os << ']';
171}
172
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000173void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
174 // FIXME: More elaborate pretty-printing.
175 os << "{ " << (void*) CL << " }";
176}
177
Zhongxing Xucc128b32008-11-10 13:05:26 +0000178void StringRegion::print(llvm::raw_ostream& os) const {
Ted Kremeneke2916d62009-01-16 19:26:50 +0000179 Str->printPretty(os);
Zhongxing Xucc128b32008-11-10 13:05:26 +0000180}
181
Ted Kremenek9e240492008-10-04 05:50:14 +0000182//===----------------------------------------------------------------------===//
183// MemRegionManager methods.
184//===----------------------------------------------------------------------===//
185
186MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
187
188 if (!region) {
189 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
190 new (region) MemSpaceRegion();
191 }
192
193 return region;
194}
195
196MemSpaceRegion* MemRegionManager::getStackRegion() {
197 return LazyAllocate(stack);
198}
199
200MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
201 return LazyAllocate(globals);
202}
203
204MemSpaceRegion* MemRegionManager::getHeapRegion() {
205 return LazyAllocate(heap);
206}
207
Zhongxing Xu17892752008-10-08 02:50:44 +0000208MemSpaceRegion* MemRegionManager::getUnknownRegion() {
209 return LazyAllocate(unknown);
210}
211
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000212bool MemRegionManager::onStack(const MemRegion* R) {
213 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
214 R = SR->getSuperRegion();
215
216 return (R != 0) && (R == stack);
217}
218
219bool MemRegionManager::onHeap(const MemRegion* R) {
220 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
221 R = SR->getSuperRegion();
222
223 return (R != 0) && (R == heap);
224}
225
Zhongxing Xue9f4e542008-10-25 14:13:41 +0000226StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
227 llvm::FoldingSetNodeID ID;
228 MemSpaceRegion* GlobalsR = getGlobalsRegion();
229
230 StringRegion::ProfileRegion(ID, Str, GlobalsR);
231
232 void* InsertPos;
233 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
234 StringRegion* R = cast_or_null<StringRegion>(data);
235
236 if (!R) {
237 R = (StringRegion*) A.Allocate<StringRegion>();
238 new (R) StringRegion(Str, GlobalsR);
239 Regions.InsertNode(R, InsertPos);
240 }
241
242 return R;
243}
244
Ted Kremenek9a1f03a2008-10-27 21:01:26 +0000245VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
246
247 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
248 : getGlobalsRegion();
249
Ted Kremenek9e240492008-10-04 05:50:14 +0000250 llvm::FoldingSetNodeID ID;
251 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
252
253 void* InsertPos;
254 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
255 VarRegion* R = cast_or_null<VarRegion>(data);
256
257 if (!R) {
258 R = (VarRegion*) A.Allocate<VarRegion>();
259 new (R) VarRegion(d, superRegion);
260 Regions.InsertNode(R, InsertPos);
261 }
262
263 return R;
264}
265
Ted Kremenek329d6fd2008-10-27 20:57:58 +0000266CompoundLiteralRegion*
267MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
268 // Is this compound literal allocated on the stack or is part of the
269 // global constant pool?
270 const MemRegion* superRegion = CL->isFileScope() ?
271 getGlobalsRegion() : getStackRegion();
272
273 // Profile the compound literal.
274 llvm::FoldingSetNodeID ID;
275 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
276
277 void* InsertPos;
278 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
279 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
280
281 if (!R) {
282 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
283 new (R) CompoundLiteralRegion(CL, superRegion);
284 Regions.InsertNode(R, InsertPos);
285 }
286
287 return R;
288}
289
Ted Kremenekabb042f2008-12-13 19:24:37 +0000290ElementRegion*
291MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
292
Zhongxing Xu511191c2008-10-21 05:27:10 +0000293 llvm::FoldingSetNodeID ID;
294 ElementRegion::ProfileRegion(ID, Idx, superRegion);
295
296 void* InsertPos;
297 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
298 ElementRegion* R = cast_or_null<ElementRegion>(data);
299
300 if (!R) {
301 R = (ElementRegion*) A.Allocate<ElementRegion>();
302 new (R) ElementRegion(Idx, superRegion);
303 Regions.InsertNode(R, InsertPos);
304 }
305
306 return R;
307}
308
Ted Kremenek993f1c72008-10-17 20:28:54 +0000309/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000310SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000311 llvm::FoldingSetNodeID ID;
312 SymbolicRegion::ProfileRegion(ID, sym);
Ted Kremenek993f1c72008-10-17 20:28:54 +0000313 void* InsertPos;
314 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
315 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
316
317 if (!R) {
318 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu026c6632009-02-05 06:57:29 +0000319 // SymbolicRegion's storage class is usually unknown.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000320 new (R) SymbolicRegion(sym, getUnknownRegion());
Ted Kremenek993f1c72008-10-17 20:28:54 +0000321 Regions.InsertNode(R, InsertPos);
322 }
323
324 return R;
325}
326
Ted Kremenek9e240492008-10-04 05:50:14 +0000327FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek993f1c72008-10-17 20:28:54 +0000328 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000329 llvm::FoldingSetNodeID ID;
330 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
331
332 void* InsertPos;
333 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
334 FieldRegion* R = cast_or_null<FieldRegion>(data);
335
336 if (!R) {
337 R = (FieldRegion*) A.Allocate<FieldRegion>();
338 new (R) FieldRegion(d, superRegion);
339 Regions.InsertNode(R, InsertPos);
340 }
341
342 return R;
343}
344
Ted Kremenek993f1c72008-10-17 20:28:54 +0000345ObjCIvarRegion*
346MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
347 const MemRegion* superRegion) {
Ted Kremenek9e240492008-10-04 05:50:14 +0000348 llvm::FoldingSetNodeID ID;
349 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
350
351 void* InsertPos;
352 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
353 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
354
355 if (!R) {
Zhongxing Xu722c2882008-10-06 03:03:33 +0000356 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
357 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenek9e240492008-10-04 05:50:14 +0000358 Regions.InsertNode(R, InsertPos);
359 }
360
361 return R;
362}
363
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000364ObjCObjectRegion*
365MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
366 const MemRegion* superRegion) {
367 llvm::FoldingSetNodeID ID;
368 DeclRegion::ProfileRegion(ID, d, superRegion,
369 MemRegion::ObjCObjectRegionKind);
370
371 void* InsertPos;
372 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
373 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
374
375 if (!R) {
376 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
377 new (R) ObjCObjectRegion(d, superRegion);
378 Regions.InsertNode(R, InsertPos);
379 }
380
381 return R;
382}
383
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000384TypedViewRegion*
385MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000386 llvm::FoldingSetNodeID ID;
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000387 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000388
389 void* InsertPos;
390 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000391 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000392
393 if (!R) {
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000394 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
395 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000396 Regions.InsertNode(R, InsertPos);
397 }
398
399 return R;
400}
Ted Kremeneka7f1b9e2008-10-24 20:30:08 +0000401
Ted Kremenek7090ae12008-11-02 00:34:33 +0000402AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
403 llvm::FoldingSetNodeID ID;
404 AllocaRegion::ProfileRegion(ID, E, cnt);
405
406 void* InsertPos;
407 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
408 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
409
410 if (!R) {
411 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
412 new (R) AllocaRegion(E, cnt, getStackRegion());
413 Regions.InsertNode(R, InsertPos);
414 }
415
416 return R;
417}
418
Ted Kremenek9e240492008-10-04 05:50:14 +0000419bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000420
421 // Only subregions can have stack storage.
Ted Kremenek7090ae12008-11-02 00:34:33 +0000422 const SubRegion* SR = dyn_cast<SubRegion>(R);
423
Ted Kremenek993f1c72008-10-17 20:28:54 +0000424 if (!SR)
425 return false;
Ted Kremenek7090ae12008-11-02 00:34:33 +0000426
Ted Kremenek9e240492008-10-04 05:50:14 +0000427 MemSpaceRegion* S = getStackRegion();
428
Ted Kremenek993f1c72008-10-17 20:28:54 +0000429 while (SR) {
430 R = SR->getSuperRegion();
431 if (R == S)
432 return true;
433
434 SR = dyn_cast<SubRegion>(R);
Ted Kremenek9e240492008-10-04 05:50:14 +0000435 }
436
437 return false;
438}