blob: 84fd163093aa09bb97f771849e24c50a6c443f72 [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//===----------------------------------------------------------------------===//
124// getLValueType() and getRValueType()
125//===----------------------------------------------------------------------===//
126
Ted Kremenekf5da3252008-12-13 21:49:13 +0000127QualType StringRegion::getRValueType(ASTContext& C) const {
128 return Str->getType();
129}
130
131//===----------------------------------------------------------------------===//
Ted Kremenekb15eba42008-10-04 05:50:14 +0000132// Region pretty-printing.
133//===----------------------------------------------------------------------===//
134
135std::string MemRegion::getString() const {
136 std::string s;
137 llvm::raw_string_ostream os(s);
138 print(os);
139 return os.str();
140}
141
142void MemRegion::print(llvm::raw_ostream& os) const {
143 os << "<Unknown Region>";
144}
145
Ted Kremenekea9ca502008-11-02 00:34:33 +0000146void AllocaRegion::print(llvm::raw_ostream& os) const {
147 os << "alloca{" << (void*) Ex << ',' << Cnt << '}';
148}
149
Ted Kremenek398efb52009-04-21 19:56:58 +0000150void CodeTextRegion::print(llvm::raw_ostream& os) const {
151 os << "code{";
152 if (isDeclared())
Ted Kremenekb895ed22009-04-29 15:37:24 +0000153 os << getDecl()->getDeclName().getAsString();
Ted Kremenek398efb52009-04-21 19:56:58 +0000154 else
155 os << '$' << getSymbol();
156
157 os << '}';
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*
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000306MemRegionManager::getElementRegion(QualType elementType, SVal Idx,
Zhongxing Xu94f63372009-05-06 08:15:46 +0000307 const MemRegion* superRegion){
Ted Kremenek2c0de352008-12-13 19:24:37 +0000308
Zhongxing Xu54969732008-10-21 05:27:10 +0000309 llvm::FoldingSetNodeID ID;
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000310 ElementRegion::ProfileRegion(ID, elementType, Idx, superRegion);
Zhongxing Xu54969732008-10-21 05:27:10 +0000311
312 void* InsertPos;
313 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
314 ElementRegion* R = cast_or_null<ElementRegion>(data);
315
316 if (!R) {
317 R = (ElementRegion*) A.Allocate<ElementRegion>();
Ted Kremenekaf81ece2009-05-04 06:18:28 +0000318 new (R) ElementRegion(elementType, Idx, superRegion);
Zhongxing Xu54969732008-10-21 05:27:10 +0000319 Regions.InsertNode(R, InsertPos);
320 }
321
322 return R;
323}
324
Zhongxing Xuef3fb4c2009-04-10 08:45:10 +0000325CodeTextRegion* MemRegionManager::getCodeTextRegion(const FunctionDecl* fd,
326 QualType t) {
327 llvm::FoldingSetNodeID ID;
328 CodeTextRegion::ProfileRegion(ID, fd, t);
329 void* InsertPos;
330 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
331 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
332
333 if (!R) {
334 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
335 new (R) CodeTextRegion(fd, t, getCodeRegion());
336 Regions.InsertNode(R, InsertPos);
337 }
338
339 return R;
340}
341
342CodeTextRegion* MemRegionManager::getCodeTextRegion(SymbolRef sym, QualType t) {
343 llvm::FoldingSetNodeID ID;
344 CodeTextRegion::ProfileRegion(ID, sym, t);
345 void* InsertPos;
346 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
347 CodeTextRegion* R = cast_or_null<CodeTextRegion>(data);
348
349 if (!R) {
350 R = (CodeTextRegion*) A.Allocate<CodeTextRegion>();
351 new (R) CodeTextRegion(sym, t, getCodeRegion());
352 Regions.InsertNode(R, InsertPos);
353 }
354
355 return R;
356}
357
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000358/// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
Ted Kremenek74556a12009-03-26 03:35:11 +0000359SymbolicRegion* MemRegionManager::getSymbolicRegion(SymbolRef sym) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000360 llvm::FoldingSetNodeID ID;
361 SymbolicRegion::ProfileRegion(ID, sym);
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000362 void* InsertPos;
363 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
364 SymbolicRegion* R = cast_or_null<SymbolicRegion>(data);
365
366 if (!R) {
367 R = (SymbolicRegion*) A.Allocate<SymbolicRegion>();
Zhongxing Xu93b80662009-02-05 06:57:29 +0000368 // SymbolicRegion's storage class is usually unknown.
Ted Kremenek74556a12009-03-26 03:35:11 +0000369 new (R) SymbolicRegion(sym, getUnknownRegion());
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000370 Regions.InsertNode(R, InsertPos);
371 }
372
373 return R;
374}
375
Ted Kremenekb15eba42008-10-04 05:50:14 +0000376FieldRegion* MemRegionManager::getFieldRegion(const FieldDecl* d,
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000377 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000378 llvm::FoldingSetNodeID ID;
379 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::FieldRegionKind);
380
381 void* InsertPos;
382 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
383 FieldRegion* R = cast_or_null<FieldRegion>(data);
384
385 if (!R) {
386 R = (FieldRegion*) A.Allocate<FieldRegion>();
387 new (R) FieldRegion(d, superRegion);
388 Regions.InsertNode(R, InsertPos);
389 }
390
391 return R;
392}
393
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000394ObjCIvarRegion*
395MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl* d,
396 const MemRegion* superRegion) {
Ted Kremenekb15eba42008-10-04 05:50:14 +0000397 llvm::FoldingSetNodeID ID;
398 DeclRegion::ProfileRegion(ID, d, superRegion, MemRegion::ObjCIvarRegionKind);
399
400 void* InsertPos;
401 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
402 ObjCIvarRegion* R = cast_or_null<ObjCIvarRegion>(data);
403
404 if (!R) {
Zhongxing Xu7a861e82008-10-06 03:03:33 +0000405 R = (ObjCIvarRegion*) A.Allocate<ObjCIvarRegion>();
406 new (R) ObjCIvarRegion(d, superRegion);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000407 Regions.InsertNode(R, InsertPos);
408 }
409
410 return R;
411}
412
Ted Kremenek2539c112008-10-24 20:30:08 +0000413ObjCObjectRegion*
414MemRegionManager::getObjCObjectRegion(const ObjCInterfaceDecl* d,
415 const MemRegion* superRegion) {
416 llvm::FoldingSetNodeID ID;
417 DeclRegion::ProfileRegion(ID, d, superRegion,
418 MemRegion::ObjCObjectRegionKind);
419
420 void* InsertPos;
421 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
422 ObjCObjectRegion* R = cast_or_null<ObjCObjectRegion>(data);
423
424 if (!R) {
425 R = (ObjCObjectRegion*) A.Allocate<ObjCObjectRegion>();
426 new (R) ObjCObjectRegion(d, superRegion);
427 Regions.InsertNode(R, InsertPos);
428 }
429
430 return R;
431}
432
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000433TypedViewRegion*
434MemRegionManager::getTypedViewRegion(QualType t, const MemRegion* superRegion) {
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000435 llvm::FoldingSetNodeID ID;
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000436 TypedViewRegion::ProfileRegion(ID, t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000437
438 void* InsertPos;
439 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000440 TypedViewRegion* R = cast_or_null<TypedViewRegion>(data);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000441
442 if (!R) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +0000443 R = (TypedViewRegion*) A.Allocate<TypedViewRegion>();
444 new (R) TypedViewRegion(t, superRegion);
Zhongxing Xu8fbe7ae2008-11-16 04:07:26 +0000445 Regions.InsertNode(R, InsertPos);
446 }
447
448 return R;
449}
Ted Kremenek2539c112008-10-24 20:30:08 +0000450
Ted Kremenekea9ca502008-11-02 00:34:33 +0000451AllocaRegion* MemRegionManager::getAllocaRegion(const Expr* E, unsigned cnt) {
452 llvm::FoldingSetNodeID ID;
453 AllocaRegion::ProfileRegion(ID, E, cnt);
454
455 void* InsertPos;
456 MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
457 AllocaRegion* R = cast_or_null<AllocaRegion>(data);
458
459 if (!R) {
460 R = (AllocaRegion*) A.Allocate<AllocaRegion>();
461 new (R) AllocaRegion(E, cnt, getStackRegion());
462 Regions.InsertNode(R, InsertPos);
463 }
464
465 return R;
466}
467
Ted Kremenekb15eba42008-10-04 05:50:14 +0000468bool MemRegionManager::hasStackStorage(const MemRegion* R) {
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000469
470 // Only subregions can have stack storage.
Ted Kremenekea9ca502008-11-02 00:34:33 +0000471 const SubRegion* SR = dyn_cast<SubRegion>(R);
472
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000473 if (!SR)
474 return false;
Ted Kremenekea9ca502008-11-02 00:34:33 +0000475
Ted Kremenekb15eba42008-10-04 05:50:14 +0000476 MemSpaceRegion* S = getStackRegion();
477
Ted Kremenek38a4b4b2008-10-17 20:28:54 +0000478 while (SR) {
479 R = SR->getSuperRegion();
480 if (R == S)
481 return true;
482
483 SR = dyn_cast<SubRegion>(R);
Ted Kremenekb15eba42008-10-04 05:50:14 +0000484 }
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000485
Ted Kremenekb15eba42008-10-04 05:50:14 +0000486 return false;
487}
Ted Kremenek8765ebc2009-04-11 00:11:10 +0000488
489
490//===----------------------------------------------------------------------===//
491// View handling.
492//===----------------------------------------------------------------------===//
493
494const MemRegion *TypedViewRegion::removeViews() const {
495 const SubRegion *SR = this;
496 const MemRegion *R = SR;
497 while (SR && isa<TypedViewRegion>(SR)) {
498 R = SR->getSuperRegion();
499 SR = dyn_cast<SubRegion>(R);
500 }
501 return R;
502}