MemRegion.cpp 47 KB

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