blob: 5e71924bf9cedd029d22febcee0d615d8265921a [file] [log] [blame]
Ted Kremenekb15eba42008-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 Xub287b212009-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 Kremenekb15eba42008-10-04 05:50:14 +000037void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
38 ID.AddInteger((unsigned)getKind());
39}
40
Zhongxing Xu73507bd2008-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 Kremenekea9ca502008-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 Kremenek6a9b5352009-03-01 05:44:08 +000060void TypedViewRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, QualType T,
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000061 const MemRegion* superRegion) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +000062 ID.AddInteger((unsigned) TypedViewRegionKind);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +000063 ID.Add(T);
64 ID.AddPointer(superRegion);
65}
66
Ted Kremenek6bc91b92008-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 Kremenekb15eba42008-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 Kremenekb9cd9a72008-12-05 02:27:51 +000090void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +000091 ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
Ted Kremeneke8993f12008-12-05 02:39:38 +000092 ID.Add(sym);
Ted Kremenek38a4b4b2008-10-17 20:28:54 +000093}
94
95void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
96 SymbolicRegion::ProfileRegion(ID, sym);
97}
98
Zhongxing Xu54969732008-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 Xu1a563da2008-10-27 13:17:02 +0000109
Zhongxing Xuef3fb4c2009-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 Xu93b80662009-02-05 06:57:29 +0000121//===----------------------------------------------------------------------===//
122// getLValueType() and getRValueType()
123//===----------------------------------------------------------------------===//
124
Ted Kremenekf5da3252008-12-13 21:49:13 +0000125QualType ElementRegion::getRValueType(ASTContext& C) const {
Ted Kremenek83146912009-01-24 06:11:36 +0000126 // Strip off typedefs from the ArrayRegion's RvalueType.
127 QualType T = getArrayRegion()->getRValueType(C)->getDesugaredType();
Zhongxing Xu02357a42009-01-23 10:19:29 +0000128
Ted Kremenek83146912009-01-24 06:11:36 +0000129 if (ArrayType* AT = dyn_cast<ArrayType>(T.getTypePtr()))
Zhongxing Xu9f801532008-11-13 07:30:58 +0000130 return AT->getElementType();
Ted Kremenek83146912009-01-24 06:11:36 +0000131
Ted Kremeneka25cb052009-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 Xu1a563da2008-10-27 13:17:02 +0000135}
136
Ted Kremenekf5da3252008-12-13 21:49:13 +0000137QualType StringRegion::getRValueType(ASTContext& C) const {
138 return Str->getType();
139}
140
141//===----------------------------------------------------------------------===//
Ted Kremenekb15eba42008-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 Kremenekea9ca502008-11-02 00:34:33 +0000156void AllocaRegion::print(llvm::raw_ostream& os) const {
157 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
158}
159
Ted Kremenek3a4beb52009-04-21 18:09:22 +0000160void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
161 // FIXME: More elaborate pretty-printing.
162 os << "{ " << (void*) CL << " }";
163}
164
165void ElementRegion::print(llvm::raw_ostream& os) const {
166 superRegion->print(os);
167 os << '['; Index.print(os); os << ']';
168}
169
170void FieldRegion::print(llvm::raw_ostream& os) const {
171 superRegion->print(os);
172 os << "->" << getDecl()->getNameAsString();
173}
174
175void StringRegion::print(llvm::raw_ostream& os) const {
176 Str->printPretty(os);
177}
178
179void SymbolicRegion::print(llvm::raw_ostream& os) const {
180 os << "SymRegion-" << sym;
181}
182
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000183void TypedViewRegion::print(llvm::raw_ostream& os) const {
Ted Kremenekd71d13a2009-03-11 21:57:34 +0000184 os << "typed_view{" << LValueType.getAsString() << ',';
Ted Kremenek542c34d2008-12-17 19:25:50 +0000185 getSuperRegion()->print(os);
186 os << '}';
187}
188
Ted Kremenekb15eba42008-10-04 05:50:14 +0000189void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattner271d4c22008-11-24 05:29:24 +0000190 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000191}
192
193//===----------------------------------------------------------------------===//
194// MemRegionManager methods.
195//===----------------------------------------------------------------------===//
196
197MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
198
199 if (!region) {
200 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
201 new (region) MemSpaceRegion();
202 }
203
204 return region;
205}
206
207MemSpaceRegion* MemRegionManager::getStackRegion() {
208 return LazyAllocate(stack);
209}
210
211MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
212 return LazyAllocate(globals);
213}
214
215MemSpaceRegion* MemRegionManager::getHeapRegion() {
216 return LazyAllocate(heap);
217}
218
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000219MemSpaceRegion* MemRegionManager::getUnknownRegion() {
220 return LazyAllocate(unknown);
221}
222
Zhongxing Xuef3fb4c2009-04-10 08:45:10 +0000223MemSpaceRegion* MemRegionManager::getCodeRegion() {
224 return LazyAllocate(code);
225}
226
Zhongxing Xu5ea4ad02008-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 Xu73507bd2008-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 Kremenek81329ab2008-10-27 21:01:26 +0000260VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
261
262 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
263 : getGlobalsRegion();
264
Ted Kremenekb15eba42008-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 Kremenek6bc91b92008-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 Kremenek2c0de352008-12-13 19:24:37 +0000305ElementRegion*
306MemRegionManager::getElementRegion(SVal Idx, const TypedRegion* superRegion){
307
Zhongxing Xu54969732008-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
Zhongxing Xuef3fb4c2009-04-10 08:45:10 +0000324CodeTextRegion* MemRegionManager::getCodeTextRegion(const FunctionDecl* fd,
325 QualType t) {
326 llvm::FoldingSetNodeID ID;
327 CodeTextRegion::ProfileRegion(ID, fd, t);
328 void* InsertPos;
329 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
330 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
331
332 if (!R) {
333 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
334 new (R) CodeTextRegion(fd, t, getCodeRegion());
335 Regions.InsertNode(R, InsertPos);
336 }
337
338 return R;
339}
340
341CodeTextRegion* MemRegionManager::getCodeTextRegion(SymbolRef sym, QualType t) {
342 llvm::FoldingSetNodeID ID;
343 CodeTextRegion::ProfileRegion(ID, sym, t);
344 void* InsertPos;
345 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
346 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
347
348 if (!R) {
349 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
350 new (R) CodeTextRegion(sym, t, getCodeRegion());
351 Regions.InsertNode(R, InsertPos);
352 }
353
354 return R;
355}
356
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000357/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenek74556a12009-03-26 03:35:11 +0000358SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000359 llvm::FoldingSetNodeID ID;
360 SymbolicRegion::ProfileRegion(ID, sym);
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000361 void* InsertPos;
362 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
363 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
364
365 if (!R) {
366 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu93b80662009-02-05 06:57:29 +0000367 // SymbolicRegion's storage class is usually unknown.
Ted Kremenek74556a12009-03-26 03:35:11 +0000368 new (R) SymbolicRegion(sym, getUnknownRegion());
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000369 Regions.InsertNode(R, InsertPos);
370 }
371
372 return R;
373}
374
Ted Kremenekb15eba42008-10-04 05:50:14 +0000375FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000376 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000377 llvm::FoldingSetNodeID ID;
378 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
379
380 void* InsertPos;
381 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
382 FieldRegion* R = cast_or_null<FieldRegion>(data);
383
384 if (!R) {
385 R = (FieldRegion*) A.Allocate<FieldRegion>();
386 new (R) FieldRegion(d, superRegion);
387 Regions.InsertNode(R, InsertPos);
388 }
389
390 return R;
391}
392
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000393ObjCIvarRegion*
394MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
395 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000396 llvm::FoldingSetNodeID ID;
397 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
398
399 void* InsertPos;
400 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
401 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
402
403 if (!R) {
Zhongxing Xu7a861e82008-10-06 03:03:33 +0000404 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
405 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000406 Regions.InsertNode(R, InsertPos);
407 }
408
409 return R;
410}
411
Ted Kremenek2539c112008-10-24 20:30:08 +0000412ObjCObjectRegion*
413MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
414 const MemRegion* superRegion) {
415 llvm::FoldingSetNodeID ID;
416 DeclRegion::ProfileRegion(ID, d, superRegion,
417 MemRegion::ObjCObjectRegionKind);
418
419 void* InsertPos;
420 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
421 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
422
423 if (!R) {
424 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
425 new (R) ObjCObjectRegion(d, superRegion);
426 Regions.InsertNode(R, InsertPos);
427 }
428
429 return R;
430}
431
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000432TypedViewRegion*
433MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000434 llvm::FoldingSetNodeID ID;
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000435 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000436
437 void* InsertPos;
438 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000439 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000440
441 if (!R) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000442 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
443 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000444 Regions.InsertNode(R, InsertPos);
445 }
446
447 return R;
448}
Ted Kremenek2539c112008-10-24 20:30:08 +0000449
Ted Kremenekea9ca502008-11-02 00:34:33 +0000450AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
451 llvm::FoldingSetNodeID ID;
452 AllocaRegion::ProfileRegion(ID, E, cnt);
453
454 void* InsertPos;
455 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
456 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
457
458 if (!R) {
459 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
460 new (R) AllocaRegion(E, cnt, getStackRegion());
461 Regions.InsertNode(R, InsertPos);
462 }
463
464 return R;
465}
466
Ted Kremenekb15eba42008-10-04 05:50:14 +0000467bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000468
469 // Only subregions can have stack storage.
Ted Kremenekea9ca502008-11-02 00:34:33 +0000470 const SubRegion* SR = dyn_cast<SubRegion>(R);
471
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000472 if (!SR)
473 return false;
Ted Kremenekea9ca502008-11-02 00:34:33 +0000474
Ted Kremenekb15eba42008-10-04 05:50:14 +0000475 MemSpaceRegion* S = getStackRegion();
476
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000477 while (SR) {
478 R = SR->getSuperRegion();
479 if (R == S)
480 return true;
481
482 SR = dyn_cast<SubRegion>(R);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000483 }
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000484
Ted Kremenekb15eba42008-10-04 05:50:14 +0000485 return false;
486}
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000487
488
489//===----------------------------------------------------------------------===//
490// View handling.
491//===----------------------------------------------------------------------===//
492
493const MemRegion *TypedViewRegion::removeViews() const {
494 const SubRegion *SR = this;
495 const MemRegion *R = SR;
496 while (SR && isa<TypedViewRegion>(SR)) {
497 R = SR->getSuperRegion();
498 SR = dyn_cast<SubRegion>(R);
499 }
500 return R;
501}