blob: 9e11a263536de3024d3476ad9d13170e49d2e9b0 [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
Ted Kremenekaf81ece2009-05-04 06:18:28 +000099void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
100 QualType ElementType, SVal Idx,
Zhongxing Xu54969732008-10-21 05:27:10 +0000101 const MemRegion* superRegion) {
102 ID.AddInteger(MemRegion::ElementRegionKind);
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000103 ID.Add(ElementType);
Zhongxing Xu54969732008-10-21 05:27:10 +0000104 ID.AddPointer(superRegion);
105 Idx.Profile(ID);
106}
107
108void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000109 ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
Zhongxing Xu54969732008-10-21 05:27:10 +0000110}
Zhongxing Xu1a563da2008-10-27 13:17:02 +0000111
Zhongxing Xuef3fb4c2009-04-10 08:45:10 +0000112void CodeTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const void* data,
113 QualType t) {
114 ID.AddInteger(MemRegion::CodeTextRegionKind);
115 ID.AddPointer(data);
116 ID.Add(t);
117}
118
119void CodeTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
120 CodeTextRegion::ProfileRegion(ID, Data, LocationType);
121}
122
Zhongxing Xu93b80662009-02-05 06:57:29 +0000123//===----------------------------------------------------------------------===//
Ted Kremenekb15eba42008-10-04 05:50:14 +0000124// Region pretty-printing.
125//===----------------------------------------------------------------------===//
126
127std::string MemRegion::getString() const {
128 std::string s;
129 llvm::raw_string_ostream os(s);
130 print(os);
131 return os.str();
132}
133
134void MemRegion::print(llvm::raw_ostream& os) const {
135 os << "<Unknown Region>";
136}
137
Ted Kremenekea9ca502008-11-02 00:34:33 +0000138void AllocaRegion::print(llvm::raw_ostream& os) const {
139 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
140}
141
Ted Kremenek398efb52009-04-21 19:56:58 +0000142void CodeTextRegion::print(llvm::raw_ostream& os) const {
143 os << "code{";
144 if (isDeclared())
Ted Kremenekb895ed22009-04-29 15:37:24 +0000145 os << getDecl()->getDeclName().getAsString();
Ted Kremenek398efb52009-04-21 19:56:58 +0000146 else
147 os << '$' << getSymbol();
148
149 os << '}';
150}
151
Ted Kremenek3a4beb52009-04-21 18:09:22 +0000152void CompoundLiteralRegion::print(llvm::raw_ostream& os) const {
153 // FIXME: More elaborate pretty-printing.
154 os << "{ " << (void*) CL << " }";
155}
156
157void ElementRegion::print(llvm::raw_ostream& os) const {
158 superRegion->print(os);
159 os << '['; Index.print(os); os << ']';
160}
161
162void FieldRegion::print(llvm::raw_ostream& os) const {
163 superRegion->print(os);
164 os << "->" << getDecl()->getNameAsString();
165}
166
167void StringRegion::print(llvm::raw_ostream& os) const {
168 Str->printPretty(os);
169}
170
171void SymbolicRegion::print(llvm::raw_ostream& os) const {
172 os << "SymRegion-" << sym;
173}
174
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000175void TypedViewRegion::print(llvm::raw_ostream& os) const {
Ted Kremenekd71d13a2009-03-11 21:57:34 +0000176 os << "typed_view{" << LValueType.getAsString() << ',';
Ted Kremenek542c34d2008-12-17 19:25:50 +0000177 getSuperRegion()->print(os);
178 os << '}';
179}
180
Ted Kremenekb15eba42008-10-04 05:50:14 +0000181void VarRegion::print(llvm::raw_ostream& os) const {
Chris Lattner271d4c22008-11-24 05:29:24 +0000182 os << cast<VarDecl>(D)->getNameAsString();
Ted Kremenekb15eba42008-10-04 05:50:14 +0000183}
184
185//===----------------------------------------------------------------------===//
186// MemRegionManager methods.
187//===----------------------------------------------------------------------===//
188
189MemSpaceRegion* MemRegionManager::LazyAllocate(MemSpaceRegion*& region) {
190
191 if (!region) {
192 region = (MemSpaceRegion*) A.Allocate<MemSpaceRegion>();
193 new (region) MemSpaceRegion();
194 }
195
196 return region;
197}
198
199MemSpaceRegion* MemRegionManager::getStackRegion() {
200 return LazyAllocate(stack);
201}
202
203MemSpaceRegion* MemRegionManager::getGlobalsRegion() {
204 return LazyAllocate(globals);
205}
206
207MemSpaceRegion* MemRegionManager::getHeapRegion() {
208 return LazyAllocate(heap);
209}
210
Zhongxing Xu79c57f82008-10-08 02:50:44 +0000211MemSpaceRegion* MemRegionManager::getUnknownRegion() {
212 return LazyAllocate(unknown);
213}
214
Zhongxing Xuef3fb4c2009-04-10 08:45:10 +0000215MemSpaceRegion* MemRegionManager::getCodeRegion() {
216 return LazyAllocate(code);
217}
218
Zhongxing Xu5ea4ad02008-12-20 06:32:12 +0000219bool MemRegionManager::onStack(const MemRegion* R) {
220 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
221 R = SR->getSuperRegion();
222
223 return (R != 0) && (R == stack);
224}
225
226bool MemRegionManager::onHeap(const MemRegion* R) {
227 while (const SubRegion* SR = dyn_cast<SubRegion>(R))
228 R = SR->getSuperRegion();
229
230 return (R != 0) && (R == heap);
231}
232
Zhongxing Xu73507bd2008-10-25 14:13:41 +0000233StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str) {
234 llvm::FoldingSetNodeID ID;
235 MemSpaceRegion* GlobalsR = getGlobalsRegion();
236
237 StringRegion::ProfileRegion(ID, Str, GlobalsR);
238
239 void* InsertPos;
240 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
241 StringRegion* R = cast_or_null<StringRegion>(data);
242
243 if (!R) {
244 R = (StringRegion*) A.Allocate<StringRegion>();
245 new (R) StringRegion(Str, GlobalsR);
246 Regions.InsertNode(R, InsertPos);
247 }
248
249 return R;
250}
251
Ted Kremenek81329ab2008-10-27 21:01:26 +0000252VarRegion* MemRegionManager::getVarRegion(const VarDecl* d) {
253
254 const MemRegion* superRegion = d->hasLocalStorage() ? getStackRegion()
255 : getGlobalsRegion();
256
Ted Kremenekb15eba42008-10-04 05:50:14 +0000257 llvm::FoldingSetNodeID ID;
258 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::VarRegionKind);
259
260 void* InsertPos;
261 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
262 VarRegion* R = cast_or_null<VarRegion>(data);
263
264 if (!R) {
265 R = (VarRegion*) A.Allocate<VarRegion>();
266 new (R) VarRegion(d, superRegion);
267 Regions.InsertNode(R, InsertPos);
268 }
269
270 return R;
271}
272
Ted Kremenek6bc91b92008-10-27 20:57:58 +0000273CompoundLiteralRegion*
274MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr* CL) {
275 // Is this compound literal allocated on the stack or is part of the
276 // global constant pool?
277 const MemRegion* superRegion = CL->isFileScope() ?
278 getGlobalsRegion() : getStackRegion();
279
280 // Profile the compound literal.
281 llvm::FoldingSetNodeID ID;
282 CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
283
284 void* InsertPos;
285 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
286 CompoundLiteralRegion* R = cast_or_null<CompoundLiteralRegion>(data);
287
288 if (!R) {
289 R = (CompoundLiteralRegion*) A.Allocate<CompoundLiteralRegion>();
290 new (R) CompoundLiteralRegion(CL, superRegion);
291 Regions.InsertNode(R, InsertPos);
292 }
293
294 return R;
295}
296
Ted Kremenek2c0de352008-12-13 19:24:37 +0000297ElementRegion*
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000298MemRegionManager::getElementRegion(QualType elementType, SVal Idx,
Zhongxing Xufe483ee2009-06-16 09:55:50 +0000299 const MemRegion* superRegion, ASTContext& Ctx){
300
301 QualType T = Ctx.getCanonicalType(elementType);
Ted Kremenek2c0de352008-12-13 19:24:37 +0000302
Zhongxing Xu54969732008-10-21 05:27:10 +0000303 llvm::FoldingSetNodeID ID;
Zhongxing Xufe483ee2009-06-16 09:55:50 +0000304 ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
Zhongxing Xu54969732008-10-21 05:27:10 +0000305
306 void* InsertPos;
307 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
308 ElementRegion* R = cast_or_null<ElementRegion>(data);
309
310 if (!R) {
311 R = (ElementRegion*) A.Allocate<ElementRegion>();
Zhongxing Xufe483ee2009-06-16 09:55:50 +0000312 new (R) ElementRegion(T, Idx, superRegion);
Zhongxing Xu54969732008-10-21 05:27:10 +0000313 Regions.InsertNode(R, InsertPos);
314 }
315
316 return R;
317}
318
Zhongxing Xuef3fb4c2009-04-10 08:45:10 +0000319CodeTextRegion* MemRegionManager::getCodeTextRegion(const FunctionDecl* fd,
320 QualType t) {
321 llvm::FoldingSetNodeID ID;
322 CodeTextRegion::ProfileRegion(ID, fd, t);
323 void* InsertPos;
324 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
325 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
326
327 if (!R) {
328 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
329 new (R) CodeTextRegion(fd, t, getCodeRegion());
330 Regions.InsertNode(R, InsertPos);
331 }
332
333 return R;
334}
335
336CodeTextRegion* MemRegionManager::getCodeTextRegion(SymbolRef sym, QualType t) {
337 llvm::FoldingSetNodeID ID;
338 CodeTextRegion::ProfileRegion(ID, sym, t);
339 void* InsertPos;
340 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
341 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
342
343 if (!R) {
344 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
345 new (R) CodeTextRegion(sym, t, getCodeRegion());
346 Regions.InsertNode(R, InsertPos);
347 }
348
349 return R;
350}
351
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000352/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenek74556a12009-03-26 03:35:11 +0000353SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000354 llvm::FoldingSetNodeID ID;
355 SymbolicRegion::ProfileRegion(ID, sym);
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000356 void* InsertPos;
357 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
358 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
359
360 if (!R) {
361 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu93b80662009-02-05 06:57:29 +0000362 // SymbolicRegion's storage class is usually unknown.
Ted Kremenek74556a12009-03-26 03:35:11 +0000363 new (R) SymbolicRegion(sym, getUnknownRegion());
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000364 Regions.InsertNode(R, InsertPos);
365 }
366
367 return R;
368}
369
Ted Kremenekb15eba42008-10-04 05:50:14 +0000370FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000371 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000372 llvm::FoldingSetNodeID ID;
373 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
374
375 void* InsertPos;
376 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
377 FieldRegion* R = cast_or_null<FieldRegion>(data);
378
379 if (!R) {
380 R = (FieldRegion*) A.Allocate<FieldRegion>();
381 new (R) FieldRegion(d, superRegion);
382 Regions.InsertNode(R, InsertPos);
383 }
384
385 return R;
386}
387
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000388ObjCIvarRegion*
389MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
390 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000391 llvm::FoldingSetNodeID ID;
392 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
393
394 void* InsertPos;
395 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
396 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
397
398 if (!R) {
Zhongxing Xu7a861e82008-10-06 03:03:33 +0000399 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
400 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000401 Regions.InsertNode(R, InsertPos);
402 }
403
404 return R;
405}
406
Ted Kremenek2539c112008-10-24 20:30:08 +0000407ObjCObjectRegion*
408MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
409 const MemRegion* superRegion) {
410 llvm::FoldingSetNodeID ID;
411 DeclRegion::ProfileRegion(ID, d, superRegion,
412 MemRegion::ObjCObjectRegionKind);
413
414 void* InsertPos;
415 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
416 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
417
418 if (!R) {
419 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
420 new (R) ObjCObjectRegion(d, superRegion);
421 Regions.InsertNode(R, InsertPos);
422 }
423
424 return R;
425}
426
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000427TypedViewRegion*
428MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000429 llvm::FoldingSetNodeID ID;
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000430 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000431
432 void* InsertPos;
433 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000434 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000435
436 if (!R) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000437 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
438 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000439 Regions.InsertNode(R, InsertPos);
440 }
441
442 return R;
443}
Ted Kremenek2539c112008-10-24 20:30:08 +0000444
Ted Kremenekea9ca502008-11-02 00:34:33 +0000445AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
446 llvm::FoldingSetNodeID ID;
447 AllocaRegion::ProfileRegion(ID, E, cnt);
448
449 void* InsertPos;
450 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
451 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
452
453 if (!R) {
454 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
455 new (R) AllocaRegion(E, cnt, getStackRegion());
456 Regions.InsertNode(R, InsertPos);
457 }
458
459 return R;
460}
461
Ted Kremenekb15eba42008-10-04 05:50:14 +0000462bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000463
464 // Only subregions can have stack storage.
Ted Kremenekea9ca502008-11-02 00:34:33 +0000465 const SubRegion* SR = dyn_cast<SubRegion>(R);
466
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000467 if (!SR)
468 return false;
Ted Kremenekea9ca502008-11-02 00:34:33 +0000469
Ted Kremenekb15eba42008-10-04 05:50:14 +0000470 MemSpaceRegion* S = getStackRegion();
471
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000472 while (SR) {
473 R = SR->getSuperRegion();
474 if (R == S)
475 return true;
476
477 SR = dyn_cast<SubRegion>(R);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000478 }
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000479
Ted Kremenekb15eba42008-10-04 05:50:14 +0000480 return false;
481}
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000482
483
484//===----------------------------------------------------------------------===//
485// View handling.
486//===----------------------------------------------------------------------===//
487
488const MemRegion *TypedViewRegion::removeViews() const {
489 const SubRegion *SR = this;
490 const MemRegion *R = SR;
491 while (SR && isa<TypedViewRegion>(SR)) {
492 R = SR->getSuperRegion();
493 SR = dyn_cast<SubRegion>(R);
494 }
495 return R;
496}