MemRegion.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. //== 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. #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
  16. #include "clang/AST/Attr.h"
  17. #include "clang/AST/CharUnits.h"
  18. #include "clang/AST/DeclObjC.h"
  19. #include "clang/AST/RecordLayout.h"
  20. #include "clang/Analysis/AnalysisContext.h"
  21. #include "clang/Analysis/Support/BumpVector.h"
  22. #include "clang/Basic/SourceManager.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace clang;
  26. using namespace ento;
  27. //===----------------------------------------------------------------------===//
  28. // MemRegion Construction.
  29. //===----------------------------------------------------------------------===//
  30. template<typename RegionTy> struct MemRegionManagerTrait;
  31. template <typename RegionTy, typename A1>
  32. RegionTy* MemRegionManager::getRegion(const A1 a1) {
  33. const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
  34. MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1);
  35. llvm::FoldingSetNodeID ID;
  36. RegionTy::ProfileRegion(ID, a1, superRegion);
  37. void *InsertPos;
  38. RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
  39. InsertPos));
  40. if (!R) {
  41. R = (RegionTy*) A.Allocate<RegionTy>();
  42. new (R) RegionTy(a1, superRegion);
  43. Regions.InsertNode(R, InsertPos);
  44. }
  45. return R;
  46. }
  47. template <typename RegionTy, typename A1>
  48. RegionTy* MemRegionManager::getSubRegion(const A1 a1,
  49. const MemRegion *superRegion) {
  50. llvm::FoldingSetNodeID ID;
  51. RegionTy::ProfileRegion(ID, a1, superRegion);
  52. void *InsertPos;
  53. RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
  54. InsertPos));
  55. if (!R) {
  56. R = (RegionTy*) A.Allocate<RegionTy>();
  57. new (R) RegionTy(a1, superRegion);
  58. Regions.InsertNode(R, InsertPos);
  59. }
  60. return R;
  61. }
  62. template <typename RegionTy, typename A1, typename A2>
  63. RegionTy* MemRegionManager::getRegion(const A1 a1, const A2 a2) {
  64. const typename MemRegionManagerTrait<RegionTy>::SuperRegionTy *superRegion =
  65. MemRegionManagerTrait<RegionTy>::getSuperRegion(*this, a1, a2);
  66. llvm::FoldingSetNodeID ID;
  67. RegionTy::ProfileRegion(ID, a1, a2, superRegion);
  68. void *InsertPos;
  69. RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
  70. InsertPos));
  71. if (!R) {
  72. R = (RegionTy*) A.Allocate<RegionTy>();
  73. new (R) RegionTy(a1, a2, superRegion);
  74. Regions.InsertNode(R, InsertPos);
  75. }
  76. return R;
  77. }
  78. template <typename RegionTy, typename A1, typename A2>
  79. RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2,
  80. const MemRegion *superRegion) {
  81. llvm::FoldingSetNodeID ID;
  82. RegionTy::ProfileRegion(ID, a1, a2, superRegion);
  83. void *InsertPos;
  84. RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
  85. InsertPos));
  86. if (!R) {
  87. R = (RegionTy*) A.Allocate<RegionTy>();
  88. new (R) RegionTy(a1, a2, superRegion);
  89. Regions.InsertNode(R, InsertPos);
  90. }
  91. return R;
  92. }
  93. template <typename RegionTy, typename A1, typename A2, typename A3>
  94. RegionTy* MemRegionManager::getSubRegion(const A1 a1, const A2 a2, const A3 a3,
  95. const MemRegion *superRegion) {
  96. llvm::FoldingSetNodeID ID;
  97. RegionTy::ProfileRegion(ID, a1, a2, a3, superRegion);
  98. void *InsertPos;
  99. RegionTy* R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID,
  100. InsertPos));
  101. if (!R) {
  102. R = (RegionTy*) A.Allocate<RegionTy>();
  103. new (R) RegionTy(a1, a2, a3, superRegion);
  104. Regions.InsertNode(R, InsertPos);
  105. }
  106. return R;
  107. }
  108. //===----------------------------------------------------------------------===//
  109. // Object destruction.
  110. //===----------------------------------------------------------------------===//
  111. MemRegion::~MemRegion() {}
  112. MemRegionManager::~MemRegionManager() {
  113. // All regions and their data are BumpPtrAllocated. No need to call
  114. // their destructors.
  115. }
  116. //===----------------------------------------------------------------------===//
  117. // Basic methods.
  118. //===----------------------------------------------------------------------===//
  119. bool SubRegion::isSubRegionOf(const MemRegion* R) const {
  120. const MemRegion* r = getSuperRegion();
  121. while (r != 0) {
  122. if (r == R)
  123. return true;
  124. if (const SubRegion* sr = dyn_cast<SubRegion>(r))
  125. r = sr->getSuperRegion();
  126. else
  127. break;
  128. }
  129. return false;
  130. }
  131. MemRegionManager* SubRegion::getMemRegionManager() const {
  132. const SubRegion* r = this;
  133. do {
  134. const MemRegion *superRegion = r->getSuperRegion();
  135. if (const SubRegion *sr = dyn_cast<SubRegion>(superRegion)) {
  136. r = sr;
  137. continue;
  138. }
  139. return superRegion->getMemRegionManager();
  140. } while (1);
  141. }
  142. const StackFrameContext *VarRegion::getStackFrame() const {
  143. const StackSpaceRegion *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
  144. return SSR ? SSR->getStackFrame() : NULL;
  145. }
  146. //===----------------------------------------------------------------------===//
  147. // Region extents.
  148. //===----------------------------------------------------------------------===//
  149. DefinedOrUnknownSVal TypedValueRegion::getExtent(SValBuilder &svalBuilder) const {
  150. ASTContext &Ctx = svalBuilder.getContext();
  151. QualType T = getDesugaredValueType(Ctx);
  152. if (isa<VariableArrayType>(T))
  153. return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
  154. if (isa<IncompleteArrayType>(T))
  155. return UnknownVal();
  156. CharUnits size = Ctx.getTypeSizeInChars(T);
  157. QualType sizeTy = svalBuilder.getArrayIndexType();
  158. return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
  159. }
  160. DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
  161. DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
  162. // A zero-length array at the end of a struct often stands for dynamically-
  163. // allocated extra memory.
  164. if (Extent.isZeroConstant()) {
  165. QualType T = getDesugaredValueType(svalBuilder.getContext());
  166. if (isa<ConstantArrayType>(T))
  167. return UnknownVal();
  168. }
  169. return Extent;
  170. }
  171. DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
  172. return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
  173. }
  174. DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
  175. return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
  176. }
  177. DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
  178. return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
  179. svalBuilder.getArrayIndexType());
  180. }
  181. ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const MemRegion* sReg)
  182. : DeclRegion(ivd, sReg, ObjCIvarRegionKind) {}
  183. const ObjCIvarDecl *ObjCIvarRegion::getDecl() const {
  184. return cast<ObjCIvarDecl>(D);
  185. }
  186. QualType ObjCIvarRegion::getValueType() const {
  187. return getDecl()->getType();
  188. }
  189. QualType CXXBaseObjectRegion::getValueType() const {
  190. return QualType(getDecl()->getTypeForDecl(), 0);
  191. }
  192. //===----------------------------------------------------------------------===//
  193. // FoldingSet profiling.
  194. //===----------------------------------------------------------------------===//
  195. void MemSpaceRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  196. ID.AddInteger((unsigned)getKind());
  197. }
  198. void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  199. ID.AddInteger((unsigned)getKind());
  200. ID.AddPointer(getStackFrame());
  201. }
  202. void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  203. ID.AddInteger((unsigned)getKind());
  204. ID.AddPointer(getCodeRegion());
  205. }
  206. void StringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  207. const StringLiteral* Str,
  208. const MemRegion* superRegion) {
  209. ID.AddInteger((unsigned) StringRegionKind);
  210. ID.AddPointer(Str);
  211. ID.AddPointer(superRegion);
  212. }
  213. void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  214. const ObjCStringLiteral* Str,
  215. const MemRegion* superRegion) {
  216. ID.AddInteger((unsigned) ObjCStringRegionKind);
  217. ID.AddPointer(Str);
  218. ID.AddPointer(superRegion);
  219. }
  220. void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  221. const Expr *Ex, unsigned cnt,
  222. const MemRegion *superRegion) {
  223. ID.AddInteger((unsigned) AllocaRegionKind);
  224. ID.AddPointer(Ex);
  225. ID.AddInteger(cnt);
  226. ID.AddPointer(superRegion);
  227. }
  228. void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  229. ProfileRegion(ID, Ex, Cnt, superRegion);
  230. }
  231. void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  232. CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
  233. }
  234. void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  235. const CompoundLiteralExpr *CL,
  236. const MemRegion* superRegion) {
  237. ID.AddInteger((unsigned) CompoundLiteralRegionKind);
  238. ID.AddPointer(CL);
  239. ID.AddPointer(superRegion);
  240. }
  241. void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  242. const PointerType *PT,
  243. const MemRegion *sRegion) {
  244. ID.AddInteger((unsigned) CXXThisRegionKind);
  245. ID.AddPointer(PT);
  246. ID.AddPointer(sRegion);
  247. }
  248. void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  249. CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
  250. }
  251. void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  252. const ObjCIvarDecl *ivd,
  253. const MemRegion* superRegion) {
  254. DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind);
  255. }
  256. void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
  257. const MemRegion* superRegion, Kind k) {
  258. ID.AddInteger((unsigned) k);
  259. ID.AddPointer(D);
  260. ID.AddPointer(superRegion);
  261. }
  262. void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  263. DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
  264. }
  265. void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  266. VarRegion::ProfileRegion(ID, getDecl(), superRegion);
  267. }
  268. void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
  269. const MemRegion *sreg) {
  270. ID.AddInteger((unsigned) MemRegion::SymbolicRegionKind);
  271. ID.Add(sym);
  272. ID.AddPointer(sreg);
  273. }
  274. void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  275. SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
  276. }
  277. void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  278. QualType ElementType, SVal Idx,
  279. const MemRegion* superRegion) {
  280. ID.AddInteger(MemRegion::ElementRegionKind);
  281. ID.Add(ElementType);
  282. ID.AddPointer(superRegion);
  283. Idx.Profile(ID);
  284. }
  285. void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  286. ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
  287. }
  288. void FunctionTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  289. const NamedDecl *FD,
  290. const MemRegion*) {
  291. ID.AddInteger(MemRegion::FunctionTextRegionKind);
  292. ID.AddPointer(FD);
  293. }
  294. void FunctionTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  295. FunctionTextRegion::ProfileRegion(ID, FD, superRegion);
  296. }
  297. void BlockTextRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  298. const BlockDecl *BD, CanQualType,
  299. const AnalysisDeclContext *AC,
  300. const MemRegion*) {
  301. ID.AddInteger(MemRegion::BlockTextRegionKind);
  302. ID.AddPointer(BD);
  303. }
  304. void BlockTextRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  305. BlockTextRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
  306. }
  307. void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  308. const BlockTextRegion *BC,
  309. const LocationContext *LC,
  310. const MemRegion *sReg) {
  311. ID.AddInteger(MemRegion::BlockDataRegionKind);
  312. ID.AddPointer(BC);
  313. ID.AddPointer(LC);
  314. ID.AddPointer(sReg);
  315. }
  316. void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  317. BlockDataRegion::ProfileRegion(ID, BC, LC, getSuperRegion());
  318. }
  319. void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  320. Expr const *Ex,
  321. const MemRegion *sReg) {
  322. ID.AddPointer(Ex);
  323. ID.AddPointer(sReg);
  324. }
  325. void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  326. ProfileRegion(ID, Ex, getSuperRegion());
  327. }
  328. void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  329. const CXXRecordDecl *RD,
  330. bool IsVirtual,
  331. const MemRegion *SReg) {
  332. ID.AddPointer(RD);
  333. ID.AddBoolean(IsVirtual);
  334. ID.AddPointer(SReg);
  335. }
  336. void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  337. ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
  338. }
  339. //===----------------------------------------------------------------------===//
  340. // Region anchors.
  341. //===----------------------------------------------------------------------===//
  342. void GlobalsSpaceRegion::anchor() { }
  343. void HeapSpaceRegion::anchor() { }
  344. void UnknownSpaceRegion::anchor() { }
  345. void StackLocalsSpaceRegion::anchor() { }
  346. void StackArgumentsSpaceRegion::anchor() { }
  347. void TypedRegion::anchor() { }
  348. void TypedValueRegion::anchor() { }
  349. void CodeTextRegion::anchor() { }
  350. void SubRegion::anchor() { }
  351. //===----------------------------------------------------------------------===//
  352. // Region pretty-printing.
  353. //===----------------------------------------------------------------------===//
  354. void MemRegion::dump() const {
  355. dumpToStream(llvm::errs());
  356. }
  357. std::string MemRegion::getString() const {
  358. std::string s;
  359. llvm::raw_string_ostream os(s);
  360. dumpToStream(os);
  361. return os.str();
  362. }
  363. void MemRegion::dumpToStream(raw_ostream &os) const {
  364. os << "<Unknown Region>";
  365. }
  366. void AllocaRegion::dumpToStream(raw_ostream &os) const {
  367. os << "alloca{" << (const void*) Ex << ',' << Cnt << '}';
  368. }
  369. void FunctionTextRegion::dumpToStream(raw_ostream &os) const {
  370. os << "code{" << getDecl()->getDeclName().getAsString() << '}';
  371. }
  372. void BlockTextRegion::dumpToStream(raw_ostream &os) const {
  373. os << "block_code{" << (const void*) this << '}';
  374. }
  375. void BlockDataRegion::dumpToStream(raw_ostream &os) const {
  376. os << "block_data{" << BC << '}';
  377. }
  378. void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
  379. // FIXME: More elaborate pretty-printing.
  380. os << "{ " << (const void*) CL << " }";
  381. }
  382. void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
  383. os << "temp_object{" << getValueType().getAsString() << ','
  384. << (const void*) Ex << '}';
  385. }
  386. void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
  387. os << "base{" << superRegion << ',' << getDecl()->getName() << '}';
  388. }
  389. void CXXThisRegion::dumpToStream(raw_ostream &os) const {
  390. os << "this";
  391. }
  392. void ElementRegion::dumpToStream(raw_ostream &os) const {
  393. os << "element{" << superRegion << ','
  394. << Index << ',' << getElementType().getAsString() << '}';
  395. }
  396. void FieldRegion::dumpToStream(raw_ostream &os) const {
  397. os << superRegion << "->" << *getDecl();
  398. }
  399. void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
  400. os << "ivar{" << superRegion << ',' << *getDecl() << '}';
  401. }
  402. void StringRegion::dumpToStream(raw_ostream &os) const {
  403. Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOpts()));
  404. }
  405. void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
  406. Str->printPretty(os, 0, PrintingPolicy(getContext().getLangOpts()));
  407. }
  408. void SymbolicRegion::dumpToStream(raw_ostream &os) const {
  409. os << "SymRegion{" << sym << '}';
  410. }
  411. void VarRegion::dumpToStream(raw_ostream &os) const {
  412. os << *cast<VarDecl>(D);
  413. }
  414. void RegionRawOffset::dump() const {
  415. dumpToStream(llvm::errs());
  416. }
  417. void RegionRawOffset::dumpToStream(raw_ostream &os) const {
  418. os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
  419. }
  420. void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
  421. os << "StaticGlobalsMemSpace{" << CR << '}';
  422. }
  423. void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
  424. os << "GlobalInternalSpaceRegion";
  425. }
  426. void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
  427. os << "GlobalSystemSpaceRegion";
  428. }
  429. void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
  430. os << "GlobalImmutableSpaceRegion";
  431. }
  432. void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
  433. os << "HeapSpaceRegion";
  434. }
  435. void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
  436. os << "UnknownSpaceRegion";
  437. }
  438. void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
  439. os << "StackArgumentsSpaceRegion";
  440. }
  441. void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
  442. os << "StackLocalsSpaceRegion";
  443. }
  444. bool MemRegion::canPrintPretty() const {
  445. return false;
  446. }
  447. void MemRegion::printPretty(raw_ostream &os) const {
  448. return;
  449. }
  450. bool VarRegion::canPrintPretty() const {
  451. return true;
  452. }
  453. void VarRegion::printPretty(raw_ostream &os) const {
  454. os << getDecl()->getName();
  455. }
  456. bool ObjCIvarRegion::canPrintPretty() const {
  457. return true;
  458. }
  459. void ObjCIvarRegion::printPretty(raw_ostream &os) const {
  460. os << getDecl()->getName();
  461. }
  462. bool FieldRegion::canPrintPretty() const {
  463. return superRegion->canPrintPretty();
  464. }
  465. void FieldRegion::printPretty(raw_ostream &os) const {
  466. superRegion->printPretty(os);
  467. os << "." << getDecl()->getName();
  468. }
  469. //===----------------------------------------------------------------------===//
  470. // MemRegionManager methods.
  471. //===----------------------------------------------------------------------===//
  472. template <typename REG>
  473. const REG *MemRegionManager::LazyAllocate(REG*& region) {
  474. if (!region) {
  475. region = (REG*) A.Allocate<REG>();
  476. new (region) REG(this);
  477. }
  478. return region;
  479. }
  480. template <typename REG, typename ARG>
  481. const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
  482. if (!region) {
  483. region = (REG*) A.Allocate<REG>();
  484. new (region) REG(this, a);
  485. }
  486. return region;
  487. }
  488. const StackLocalsSpaceRegion*
  489. MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
  490. assert(STC);
  491. StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
  492. if (R)
  493. return R;
  494. R = A.Allocate<StackLocalsSpaceRegion>();
  495. new (R) StackLocalsSpaceRegion(this, STC);
  496. return R;
  497. }
  498. const StackArgumentsSpaceRegion *
  499. MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
  500. assert(STC);
  501. StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
  502. if (R)
  503. return R;
  504. R = A.Allocate<StackArgumentsSpaceRegion>();
  505. new (R) StackArgumentsSpaceRegion(this, STC);
  506. return R;
  507. }
  508. const GlobalsSpaceRegion
  509. *MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
  510. const CodeTextRegion *CR) {
  511. if (!CR) {
  512. if (K == MemRegion::GlobalSystemSpaceRegionKind)
  513. return LazyAllocate(SystemGlobals);
  514. if (K == MemRegion::GlobalImmutableSpaceRegionKind)
  515. return LazyAllocate(ImmutableGlobals);
  516. assert(K == MemRegion::GlobalInternalSpaceRegionKind);
  517. return LazyAllocate(InternalGlobals);
  518. }
  519. assert(K == MemRegion::StaticGlobalSpaceRegionKind);
  520. StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
  521. if (R)
  522. return R;
  523. R = A.Allocate<StaticGlobalSpaceRegion>();
  524. new (R) StaticGlobalSpaceRegion(this, CR);
  525. return R;
  526. }
  527. const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
  528. return LazyAllocate(heap);
  529. }
  530. const MemSpaceRegion *MemRegionManager::getUnknownRegion() {
  531. return LazyAllocate(unknown);
  532. }
  533. const MemSpaceRegion *MemRegionManager::getCodeRegion() {
  534. return LazyAllocate(code);
  535. }
  536. //===----------------------------------------------------------------------===//
  537. // Constructing regions.
  538. //===----------------------------------------------------------------------===//
  539. const StringRegion* MemRegionManager::getStringRegion(const StringLiteral* Str){
  540. return getSubRegion<StringRegion>(Str, getGlobalsRegion());
  541. }
  542. const ObjCStringRegion *
  543. MemRegionManager::getObjCStringRegion(const ObjCStringLiteral* Str){
  544. return getSubRegion<ObjCStringRegion>(Str, getGlobalsRegion());
  545. }
  546. /// Look through a chain of LocationContexts to either find the
  547. /// StackFrameContext that matches a DeclContext, or find a VarRegion
  548. /// for a variable captured by a block.
  549. static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
  550. getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
  551. const DeclContext *DC,
  552. const VarDecl *VD) {
  553. while (LC) {
  554. if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
  555. if (cast<DeclContext>(SFC->getDecl()) == DC)
  556. return SFC;
  557. }
  558. if (const BlockInvocationContext *BC =
  559. dyn_cast<BlockInvocationContext>(LC)) {
  560. const BlockDataRegion *BR =
  561. static_cast<const BlockDataRegion*>(BC->getContextData());
  562. // FIXME: This can be made more efficient.
  563. for (BlockDataRegion::referenced_vars_iterator
  564. I = BR->referenced_vars_begin(),
  565. E = BR->referenced_vars_end(); I != E; ++I) {
  566. if (const VarRegion *VR = dyn_cast<VarRegion>(I.getOriginalRegion()))
  567. if (VR->getDecl() == VD)
  568. return cast<VarRegion>(I.getCapturedRegion());
  569. }
  570. }
  571. LC = LC->getParent();
  572. }
  573. return (const StackFrameContext*)0;
  574. }
  575. const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
  576. const LocationContext *LC) {
  577. const MemRegion *sReg = 0;
  578. if (D->hasGlobalStorage() && !D->isStaticLocal()) {
  579. // First handle the globals defined in system headers.
  580. if (C.getSourceManager().isInSystemHeader(D->getLocation())) {
  581. // Whitelist the system globals which often DO GET modified, assume the
  582. // rest are immutable.
  583. if (D->getName().find("errno") != StringRef::npos)
  584. sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
  585. else
  586. sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
  587. // Treat other globals as GlobalInternal unless they are constants.
  588. } else {
  589. QualType GQT = D->getType();
  590. const Type *GT = GQT.getTypePtrOrNull();
  591. // TODO: We could walk the complex types here and see if everything is
  592. // constified.
  593. if (GT && GQT.isConstQualified() && GT->isArithmeticType())
  594. sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
  595. else
  596. sReg = getGlobalsRegion();
  597. }
  598. // Finally handle static locals.
  599. } else {
  600. // FIXME: Once we implement scope handling, we will need to properly lookup
  601. // 'D' to the proper LocationContext.
  602. const DeclContext *DC = D->getDeclContext();
  603. llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
  604. getStackOrCaptureRegionForDeclContext(LC, DC, D);
  605. if (V.is<const VarRegion*>())
  606. return V.get<const VarRegion*>();
  607. const StackFrameContext *STC = V.get<const StackFrameContext*>();
  608. if (!STC)
  609. sReg = getUnknownRegion();
  610. else {
  611. if (D->hasLocalStorage()) {
  612. sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
  613. ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
  614. : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
  615. }
  616. else {
  617. assert(D->isStaticLocal());
  618. const Decl *STCD = STC->getDecl();
  619. if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD))
  620. sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
  621. getFunctionTextRegion(cast<NamedDecl>(STCD)));
  622. else if (const BlockDecl *BD = dyn_cast<BlockDecl>(STCD)) {
  623. const BlockTextRegion *BTR =
  624. getBlockTextRegion(BD,
  625. C.getCanonicalType(BD->getSignatureAsWritten()->getType()),
  626. STC->getAnalysisDeclContext());
  627. sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
  628. BTR);
  629. }
  630. else {
  631. sReg = getGlobalsRegion();
  632. }
  633. }
  634. }
  635. }
  636. return getSubRegion<VarRegion>(D, sReg);
  637. }
  638. const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
  639. const MemRegion *superR) {
  640. return getSubRegion<VarRegion>(D, superR);
  641. }
  642. const BlockDataRegion *
  643. MemRegionManager::getBlockDataRegion(const BlockTextRegion *BC,
  644. const LocationContext *LC) {
  645. const MemRegion *sReg = 0;
  646. const BlockDecl *BD = BC->getDecl();
  647. if (!BD->hasCaptures()) {
  648. // This handles 'static' blocks.
  649. sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
  650. }
  651. else {
  652. if (LC) {
  653. // FIXME: Once we implement scope handling, we want the parent region
  654. // to be the scope.
  655. const StackFrameContext *STC = LC->getCurrentStackFrame();
  656. assert(STC);
  657. sReg = getStackLocalsRegion(STC);
  658. }
  659. else {
  660. // We allow 'LC' to be NULL for cases where want BlockDataRegions
  661. // without context-sensitivity.
  662. sReg = getUnknownRegion();
  663. }
  664. }
  665. return getSubRegion<BlockDataRegion>(BC, LC, sReg);
  666. }
  667. const CompoundLiteralRegion*
  668. MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
  669. const LocationContext *LC) {
  670. const MemRegion *sReg = 0;
  671. if (CL->isFileScope())
  672. sReg = getGlobalsRegion();
  673. else {
  674. const StackFrameContext *STC = LC->getCurrentStackFrame();
  675. assert(STC);
  676. sReg = getStackLocalsRegion(STC);
  677. }
  678. return getSubRegion<CompoundLiteralRegion>(CL, sReg);
  679. }
  680. const ElementRegion*
  681. MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
  682. const MemRegion* superRegion,
  683. ASTContext &Ctx){
  684. QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
  685. llvm::FoldingSetNodeID ID;
  686. ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
  687. void *InsertPos;
  688. MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
  689. ElementRegion* R = cast_or_null<ElementRegion>(data);
  690. if (!R) {
  691. R = (ElementRegion*) A.Allocate<ElementRegion>();
  692. new (R) ElementRegion(T, Idx, superRegion);
  693. Regions.InsertNode(R, InsertPos);
  694. }
  695. return R;
  696. }
  697. const FunctionTextRegion *
  698. MemRegionManager::getFunctionTextRegion(const NamedDecl *FD) {
  699. return getSubRegion<FunctionTextRegion>(FD, getCodeRegion());
  700. }
  701. const BlockTextRegion *
  702. MemRegionManager::getBlockTextRegion(const BlockDecl *BD, CanQualType locTy,
  703. AnalysisDeclContext *AC) {
  704. return getSubRegion<BlockTextRegion>(BD, locTy, AC, getCodeRegion());
  705. }
  706. /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
  707. const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
  708. return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
  709. }
  710. const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
  711. return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
  712. }
  713. const FieldRegion*
  714. MemRegionManager::getFieldRegion(const FieldDecl *d,
  715. const MemRegion* superRegion){
  716. return getSubRegion<FieldRegion>(d, superRegion);
  717. }
  718. const ObjCIvarRegion*
  719. MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
  720. const MemRegion* superRegion) {
  721. return getSubRegion<ObjCIvarRegion>(d, superRegion);
  722. }
  723. const CXXTempObjectRegion*
  724. MemRegionManager::getCXXTempObjectRegion(Expr const *E,
  725. LocationContext const *LC) {
  726. const StackFrameContext *SFC = LC->getCurrentStackFrame();
  727. assert(SFC);
  728. return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
  729. }
  730. /// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
  731. /// class of the type of \p Super.
  732. static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
  733. const TypedValueRegion *Super,
  734. bool IsVirtual) {
  735. BaseClass = BaseClass->getCanonicalDecl();
  736. const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
  737. if (!Class)
  738. return true;
  739. if (IsVirtual)
  740. return Class->isVirtuallyDerivedFrom(BaseClass);
  741. for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
  742. E = Class->bases_end();
  743. I != E; ++I) {
  744. if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
  745. return true;
  746. }
  747. return false;
  748. }
  749. const CXXBaseObjectRegion *
  750. MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
  751. const MemRegion *Super,
  752. bool IsVirtual) {
  753. if (isa<TypedValueRegion>(Super)) {
  754. assert(isValidBaseClass(RD, dyn_cast<TypedValueRegion>(Super), IsVirtual));
  755. (void)isValidBaseClass;
  756. if (IsVirtual) {
  757. // Virtual base regions should not be layered, since the layout rules
  758. // are different.
  759. while (const CXXBaseObjectRegion *Base =
  760. dyn_cast<CXXBaseObjectRegion>(Super)) {
  761. Super = Base->getSuperRegion();
  762. }
  763. assert(Super && !isa<MemSpaceRegion>(Super));
  764. }
  765. }
  766. return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
  767. }
  768. const CXXThisRegion*
  769. MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
  770. const LocationContext *LC) {
  771. const StackFrameContext *STC = LC->getCurrentStackFrame();
  772. assert(STC);
  773. const PointerType *PT = thisPointerTy->getAs<PointerType>();
  774. assert(PT);
  775. return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
  776. }
  777. const AllocaRegion*
  778. MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
  779. const LocationContext *LC) {
  780. const StackFrameContext *STC = LC->getCurrentStackFrame();
  781. assert(STC);
  782. return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
  783. }
  784. const MemSpaceRegion *MemRegion::getMemorySpace() const {
  785. const MemRegion *R = this;
  786. const SubRegion* SR = dyn_cast<SubRegion>(this);
  787. while (SR) {
  788. R = SR->getSuperRegion();
  789. SR = dyn_cast<SubRegion>(R);
  790. }
  791. return dyn_cast<MemSpaceRegion>(R);
  792. }
  793. bool MemRegion::hasStackStorage() const {
  794. return isa<StackSpaceRegion>(getMemorySpace());
  795. }
  796. bool MemRegion::hasStackNonParametersStorage() const {
  797. return isa<StackLocalsSpaceRegion>(getMemorySpace());
  798. }
  799. bool MemRegion::hasStackParametersStorage() const {
  800. return isa<StackArgumentsSpaceRegion>(getMemorySpace());
  801. }
  802. bool MemRegion::hasGlobalsOrParametersStorage() const {
  803. const MemSpaceRegion *MS = getMemorySpace();
  804. return isa<StackArgumentsSpaceRegion>(MS) ||
  805. isa<GlobalsSpaceRegion>(MS);
  806. }
  807. // getBaseRegion strips away all elements and fields, and get the base region
  808. // of them.
  809. const MemRegion *MemRegion::getBaseRegion() const {
  810. const MemRegion *R = this;
  811. while (true) {
  812. switch (R->getKind()) {
  813. case MemRegion::ElementRegionKind:
  814. case MemRegion::FieldRegionKind:
  815. case MemRegion::ObjCIvarRegionKind:
  816. case MemRegion::CXXBaseObjectRegionKind:
  817. R = cast<SubRegion>(R)->getSuperRegion();
  818. continue;
  819. default:
  820. break;
  821. }
  822. break;
  823. }
  824. return R;
  825. }
  826. bool MemRegion::isSubRegionOf(const MemRegion *R) const {
  827. return false;
  828. }
  829. //===----------------------------------------------------------------------===//
  830. // View handling.
  831. //===----------------------------------------------------------------------===//
  832. const MemRegion *MemRegion::StripCasts(bool StripBaseCasts) const {
  833. const MemRegion *R = this;
  834. while (true) {
  835. switch (R->getKind()) {
  836. case ElementRegionKind: {
  837. const ElementRegion *ER = cast<ElementRegion>(R);
  838. if (!ER->getIndex().isZeroConstant())
  839. return R;
  840. R = ER->getSuperRegion();
  841. break;
  842. }
  843. case CXXBaseObjectRegionKind:
  844. if (!StripBaseCasts)
  845. return R;
  846. R = cast<CXXBaseObjectRegion>(R)->getSuperRegion();
  847. break;
  848. default:
  849. return R;
  850. }
  851. }
  852. }
  853. // FIXME: Merge with the implementation of the same method in Store.cpp
  854. static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
  855. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  856. const RecordDecl *D = RT->getDecl();
  857. if (!D->getDefinition())
  858. return false;
  859. }
  860. return true;
  861. }
  862. RegionRawOffset ElementRegion::getAsArrayOffset() const {
  863. CharUnits offset = CharUnits::Zero();
  864. const ElementRegion *ER = this;
  865. const MemRegion *superR = NULL;
  866. ASTContext &C = getContext();
  867. // FIXME: Handle multi-dimensional arrays.
  868. while (ER) {
  869. superR = ER->getSuperRegion();
  870. // FIXME: generalize to symbolic offsets.
  871. SVal index = ER->getIndex();
  872. if (Optional<nonloc::ConcreteInt> CI = index.getAs<nonloc::ConcreteInt>()) {
  873. // Update the offset.
  874. int64_t i = CI->getValue().getSExtValue();
  875. if (i != 0) {
  876. QualType elemType = ER->getElementType();
  877. // If we are pointing to an incomplete type, go no further.
  878. if (!IsCompleteType(C, elemType)) {
  879. superR = ER;
  880. break;
  881. }
  882. CharUnits size = C.getTypeSizeInChars(elemType);
  883. offset += (i * size);
  884. }
  885. // Go to the next ElementRegion (if any).
  886. ER = dyn_cast<ElementRegion>(superR);
  887. continue;
  888. }
  889. return NULL;
  890. }
  891. assert(superR && "super region cannot be NULL");
  892. return RegionRawOffset(superR, offset);
  893. }
  894. RegionOffset MemRegion::getAsOffset() const {
  895. const MemRegion *R = this;
  896. const MemRegion *SymbolicOffsetBase = 0;
  897. int64_t Offset = 0;
  898. while (1) {
  899. switch (R->getKind()) {
  900. case GenericMemSpaceRegionKind:
  901. case StackLocalsSpaceRegionKind:
  902. case StackArgumentsSpaceRegionKind:
  903. case HeapSpaceRegionKind:
  904. case UnknownSpaceRegionKind:
  905. case StaticGlobalSpaceRegionKind:
  906. case GlobalInternalSpaceRegionKind:
  907. case GlobalSystemSpaceRegionKind:
  908. case GlobalImmutableSpaceRegionKind:
  909. // Stores can bind directly to a region space to set a default value.
  910. assert(Offset == 0 && !SymbolicOffsetBase);
  911. goto Finish;
  912. case FunctionTextRegionKind:
  913. case BlockTextRegionKind:
  914. case BlockDataRegionKind:
  915. // These will never have bindings, but may end up having values requested
  916. // if the user does some strange casting.
  917. if (Offset != 0)
  918. SymbolicOffsetBase = R;
  919. goto Finish;
  920. case SymbolicRegionKind:
  921. case AllocaRegionKind:
  922. case CompoundLiteralRegionKind:
  923. case CXXThisRegionKind:
  924. case StringRegionKind:
  925. case ObjCStringRegionKind:
  926. case VarRegionKind:
  927. case CXXTempObjectRegionKind:
  928. // Usual base regions.
  929. goto Finish;
  930. case ObjCIvarRegionKind:
  931. // This is a little strange, but it's a compromise between
  932. // ObjCIvarRegions having unknown compile-time offsets (when using the
  933. // non-fragile runtime) and yet still being distinct, non-overlapping
  934. // regions. Thus we treat them as "like" base regions for the purposes
  935. // of computing offsets.
  936. goto Finish;
  937. case CXXBaseObjectRegionKind: {
  938. const CXXBaseObjectRegion *BOR = cast<CXXBaseObjectRegion>(R);
  939. R = BOR->getSuperRegion();
  940. QualType Ty;
  941. if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R)) {
  942. Ty = TVR->getDesugaredValueType(getContext());
  943. } else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
  944. // If our base region is symbolic, we don't know what type it really is.
  945. // Pretend the type of the symbol is the true dynamic type.
  946. // (This will at least be self-consistent for the life of the symbol.)
  947. Ty = SR->getSymbol()->getType()->getPointeeType();
  948. }
  949. const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
  950. if (!Child) {
  951. // We cannot compute the offset of the base class.
  952. SymbolicOffsetBase = R;
  953. }
  954. // Don't bother calculating precise offsets if we already have a
  955. // symbolic offset somewhere in the chain.
  956. if (SymbolicOffsetBase)
  957. continue;
  958. CharUnits BaseOffset;
  959. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Child);
  960. if (BOR->isVirtual())
  961. BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
  962. else
  963. BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
  964. // The base offset is in chars, not in bits.
  965. Offset += BaseOffset.getQuantity() * getContext().getCharWidth();
  966. break;
  967. }
  968. case ElementRegionKind: {
  969. const ElementRegion *ER = cast<ElementRegion>(R);
  970. R = ER->getSuperRegion();
  971. QualType EleTy = ER->getValueType();
  972. if (!IsCompleteType(getContext(), EleTy)) {
  973. // We cannot compute the offset of the base class.
  974. SymbolicOffsetBase = R;
  975. continue;
  976. }
  977. SVal Index = ER->getIndex();
  978. if (Optional<nonloc::ConcreteInt> CI =
  979. Index.getAs<nonloc::ConcreteInt>()) {
  980. // Don't bother calculating precise offsets if we already have a
  981. // symbolic offset somewhere in the chain.
  982. if (SymbolicOffsetBase)
  983. continue;
  984. int64_t i = CI->getValue().getSExtValue();
  985. // This type size is in bits.
  986. Offset += i * getContext().getTypeSize(EleTy);
  987. } else {
  988. // We cannot compute offset for non-concrete index.
  989. SymbolicOffsetBase = R;
  990. }
  991. break;
  992. }
  993. case FieldRegionKind: {
  994. const FieldRegion *FR = cast<FieldRegion>(R);
  995. R = FR->getSuperRegion();
  996. const RecordDecl *RD = FR->getDecl()->getParent();
  997. if (RD->isUnion() || !RD->isCompleteDefinition()) {
  998. // We cannot compute offset for incomplete type.
  999. // For unions, we could treat everything as offset 0, but we'd rather
  1000. // treat each field as a symbolic offset so they aren't stored on top
  1001. // of each other, since we depend on things in typed regions actually
  1002. // matching their types.
  1003. SymbolicOffsetBase = R;
  1004. }
  1005. // Don't bother calculating precise offsets if we already have a
  1006. // symbolic offset somewhere in the chain.
  1007. if (SymbolicOffsetBase)
  1008. continue;
  1009. // Get the field number.
  1010. unsigned idx = 0;
  1011. for (RecordDecl::field_iterator FI = RD->field_begin(),
  1012. FE = RD->field_end(); FI != FE; ++FI, ++idx)
  1013. if (FR->getDecl() == *FI)
  1014. break;
  1015. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  1016. // This is offset in bits.
  1017. Offset += Layout.getFieldOffset(idx);
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. Finish:
  1023. if (SymbolicOffsetBase)
  1024. return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
  1025. return RegionOffset(R, Offset);
  1026. }
  1027. //===----------------------------------------------------------------------===//
  1028. // BlockDataRegion
  1029. //===----------------------------------------------------------------------===//
  1030. std::pair<const VarRegion *, const VarRegion *>
  1031. BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
  1032. MemRegionManager &MemMgr = *getMemRegionManager();
  1033. const VarRegion *VR = 0;
  1034. const VarRegion *OriginalVR = 0;
  1035. if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage()) {
  1036. VR = MemMgr.getVarRegion(VD, this);
  1037. OriginalVR = MemMgr.getVarRegion(VD, LC);
  1038. }
  1039. else {
  1040. if (LC) {
  1041. VR = MemMgr.getVarRegion(VD, LC);
  1042. OriginalVR = VR;
  1043. }
  1044. else {
  1045. VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
  1046. OriginalVR = MemMgr.getVarRegion(VD, LC);
  1047. }
  1048. }
  1049. return std::make_pair(VR, OriginalVR);
  1050. }
  1051. void BlockDataRegion::LazyInitializeReferencedVars() {
  1052. if (ReferencedVars)
  1053. return;
  1054. AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
  1055. AnalysisDeclContext::referenced_decls_iterator I, E;
  1056. llvm::tie(I, E) = AC->getReferencedBlockVars(BC->getDecl());
  1057. if (I == E) {
  1058. ReferencedVars = (void*) 0x1;
  1059. return;
  1060. }
  1061. MemRegionManager &MemMgr = *getMemRegionManager();
  1062. llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
  1063. BumpVectorContext BC(A);
  1064. typedef BumpVector<const MemRegion*> VarVec;
  1065. VarVec *BV = (VarVec*) A.Allocate<VarVec>();
  1066. new (BV) VarVec(BC, E - I);
  1067. VarVec *BVOriginal = (VarVec*) A.Allocate<VarVec>();
  1068. new (BVOriginal) VarVec(BC, E - I);
  1069. for ( ; I != E; ++I) {
  1070. const VarRegion *VR = 0;
  1071. const VarRegion *OriginalVR = 0;
  1072. llvm::tie(VR, OriginalVR) = getCaptureRegions(*I);
  1073. assert(VR);
  1074. assert(OriginalVR);
  1075. BV->push_back(VR, BC);
  1076. BVOriginal->push_back(OriginalVR, BC);
  1077. }
  1078. ReferencedVars = BV;
  1079. OriginalVars = BVOriginal;
  1080. }
  1081. BlockDataRegion::referenced_vars_iterator
  1082. BlockDataRegion::referenced_vars_begin() const {
  1083. const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
  1084. BumpVector<const MemRegion*> *Vec =
  1085. static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
  1086. if (Vec == (void*) 0x1)
  1087. return BlockDataRegion::referenced_vars_iterator(0, 0);
  1088. BumpVector<const MemRegion*> *VecOriginal =
  1089. static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
  1090. return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
  1091. VecOriginal->begin());
  1092. }
  1093. BlockDataRegion::referenced_vars_iterator
  1094. BlockDataRegion::referenced_vars_end() const {
  1095. const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
  1096. BumpVector<const MemRegion*> *Vec =
  1097. static_cast<BumpVector<const MemRegion*>*>(ReferencedVars);
  1098. if (Vec == (void*) 0x1)
  1099. return BlockDataRegion::referenced_vars_iterator(0, 0);
  1100. BumpVector<const MemRegion*> *VecOriginal =
  1101. static_cast<BumpVector<const MemRegion*>*>(OriginalVars);
  1102. return BlockDataRegion::referenced_vars_iterator(Vec->end(),
  1103. VecOriginal->end());
  1104. }
  1105. const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
  1106. for (referenced_vars_iterator I = referenced_vars_begin(),
  1107. E = referenced_vars_end();
  1108. I != E; ++I) {
  1109. if (I.getCapturedRegion() == R)
  1110. return I.getOriginalRegion();
  1111. }
  1112. return 0;
  1113. }