MemRegion.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. //===- MemRegion.cpp - Abstract memory regions for static analysis --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines MemRegion and its subclasses. MemRegion defines a
  10. // partially-typed abstraction of memory useful for path-sensitive dataflow
  11. // analyses.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Attr.h"
  17. #include "clang/AST/CharUnits.h"
  18. #include "clang/AST/Decl.h"
  19. #include "clang/AST/DeclCXX.h"
  20. #include "clang/AST/DeclObjC.h"
  21. #include "clang/AST/Expr.h"
  22. #include "clang/AST/PrettyPrinter.h"
  23. #include "clang/AST/RecordLayout.h"
  24. #include "clang/AST/Type.h"
  25. #include "clang/Analysis/AnalysisDeclContext.h"
  26. #include "clang/Analysis/Support/BumpVector.h"
  27. #include "clang/Basic/IdentifierTable.h"
  28. #include "clang/Basic/LLVM.h"
  29. #include "clang/Basic/SourceManager.h"
  30. #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
  31. #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
  32. #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
  33. #include "llvm/ADT/APInt.h"
  34. #include "llvm/ADT/FoldingSet.h"
  35. #include "llvm/ADT/Optional.h"
  36. #include "llvm/ADT/PointerUnion.h"
  37. #include "llvm/ADT/SmallString.h"
  38. #include "llvm/ADT/StringRef.h"
  39. #include "llvm/ADT/Twine.h"
  40. #include "llvm/Support/Allocator.h"
  41. #include "llvm/Support/Casting.h"
  42. #include "llvm/Support/CheckedArithmetic.h"
  43. #include "llvm/Support/Compiler.h"
  44. #include "llvm/Support/Debug.h"
  45. #include "llvm/Support/ErrorHandling.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include <cassert>
  48. #include <cstdint>
  49. #include <functional>
  50. #include <iterator>
  51. #include <string>
  52. #include <tuple>
  53. #include <utility>
  54. using namespace clang;
  55. using namespace ento;
  56. #define DEBUG_TYPE "MemRegion"
  57. //===----------------------------------------------------------------------===//
  58. // MemRegion Construction.
  59. //===----------------------------------------------------------------------===//
  60. template <typename RegionTy, typename SuperTy, typename Arg1Ty>
  61. RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1,
  62. const SuperTy *superRegion) {
  63. llvm::FoldingSetNodeID ID;
  64. RegionTy::ProfileRegion(ID, arg1, superRegion);
  65. void *InsertPos;
  66. auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
  67. if (!R) {
  68. R = A.Allocate<RegionTy>();
  69. new (R) RegionTy(arg1, superRegion);
  70. Regions.InsertNode(R, InsertPos);
  71. }
  72. return R;
  73. }
  74. template <typename RegionTy, typename SuperTy, typename Arg1Ty, typename Arg2Ty>
  75. RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
  76. const SuperTy *superRegion) {
  77. llvm::FoldingSetNodeID ID;
  78. RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
  79. void *InsertPos;
  80. auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
  81. if (!R) {
  82. R = A.Allocate<RegionTy>();
  83. new (R) RegionTy(arg1, arg2, superRegion);
  84. Regions.InsertNode(R, InsertPos);
  85. }
  86. return R;
  87. }
  88. template <typename RegionTy, typename SuperTy,
  89. typename Arg1Ty, typename Arg2Ty, typename Arg3Ty>
  90. RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
  91. const Arg3Ty arg3,
  92. const SuperTy *superRegion) {
  93. llvm::FoldingSetNodeID ID;
  94. RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
  95. void *InsertPos;
  96. auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
  97. if (!R) {
  98. R = A.Allocate<RegionTy>();
  99. new (R) RegionTy(arg1, arg2, arg3, superRegion);
  100. Regions.InsertNode(R, InsertPos);
  101. }
  102. return R;
  103. }
  104. //===----------------------------------------------------------------------===//
  105. // Object destruction.
  106. //===----------------------------------------------------------------------===//
  107. MemRegion::~MemRegion() = default;
  108. // All regions and their data are BumpPtrAllocated. No need to call their
  109. // destructors.
  110. MemRegionManager::~MemRegionManager() = default;
  111. //===----------------------------------------------------------------------===//
  112. // Basic methods.
  113. //===----------------------------------------------------------------------===//
  114. bool SubRegion::isSubRegionOf(const MemRegion* R) const {
  115. const MemRegion* r = this;
  116. do {
  117. if (r == R)
  118. return true;
  119. if (const auto *sr = dyn_cast<SubRegion>(r))
  120. r = sr->getSuperRegion();
  121. else
  122. break;
  123. } while (r != nullptr);
  124. return false;
  125. }
  126. MemRegionManager* SubRegion::getMemRegionManager() const {
  127. const SubRegion* r = this;
  128. do {
  129. const MemRegion *superRegion = r->getSuperRegion();
  130. if (const auto *sr = dyn_cast<SubRegion>(superRegion)) {
  131. r = sr;
  132. continue;
  133. }
  134. return superRegion->getMemRegionManager();
  135. } while (true);
  136. }
  137. const StackFrameContext *VarRegion::getStackFrame() const {
  138. const auto *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
  139. return SSR ? SSR->getStackFrame() : nullptr;
  140. }
  141. //===----------------------------------------------------------------------===//
  142. // Region extents.
  143. //===----------------------------------------------------------------------===//
  144. DefinedOrUnknownSVal TypedValueRegion::getExtent(SValBuilder &svalBuilder) const {
  145. ASTContext &Ctx = svalBuilder.getContext();
  146. QualType T = getDesugaredValueType(Ctx);
  147. if (isa<VariableArrayType>(T))
  148. return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
  149. if (T->isIncompleteType())
  150. return UnknownVal();
  151. CharUnits size = Ctx.getTypeSizeInChars(T);
  152. QualType sizeTy = svalBuilder.getArrayIndexType();
  153. return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
  154. }
  155. DefinedOrUnknownSVal FieldRegion::getExtent(SValBuilder &svalBuilder) const {
  156. // Force callers to deal with bitfields explicitly.
  157. if (getDecl()->isBitField())
  158. return UnknownVal();
  159. DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
  160. // A zero-length array at the end of a struct often stands for dynamically-
  161. // allocated extra memory.
  162. if (Extent.isZeroConstant()) {
  163. QualType T = getDesugaredValueType(svalBuilder.getContext());
  164. if (isa<ConstantArrayType>(T))
  165. return UnknownVal();
  166. }
  167. return Extent;
  168. }
  169. DefinedOrUnknownSVal AllocaRegion::getExtent(SValBuilder &svalBuilder) const {
  170. return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
  171. }
  172. DefinedOrUnknownSVal SymbolicRegion::getExtent(SValBuilder &svalBuilder) const {
  173. return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
  174. }
  175. DefinedOrUnknownSVal StringRegion::getExtent(SValBuilder &svalBuilder) const {
  176. return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
  177. svalBuilder.getArrayIndexType());
  178. }
  179. ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg)
  180. : DeclRegion(ivd, sReg, ObjCIvarRegionKind) {}
  181. const ObjCIvarDecl *ObjCIvarRegion::getDecl() const {
  182. return cast<ObjCIvarDecl>(D);
  183. }
  184. QualType ObjCIvarRegion::getValueType() const {
  185. return getDecl()->getType();
  186. }
  187. QualType CXXBaseObjectRegion::getValueType() const {
  188. return QualType(getDecl()->getTypeForDecl(), 0);
  189. }
  190. QualType CXXDerivedObjectRegion::getValueType() const {
  191. return QualType(getDecl()->getTypeForDecl(), 0);
  192. }
  193. //===----------------------------------------------------------------------===//
  194. // FoldingSet profiling.
  195. //===----------------------------------------------------------------------===//
  196. void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  197. ID.AddInteger(static_cast<unsigned>(getKind()));
  198. }
  199. void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  200. ID.AddInteger(static_cast<unsigned>(getKind()));
  201. ID.AddPointer(getStackFrame());
  202. }
  203. void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  204. ID.AddInteger(static_cast<unsigned>(getKind()));
  205. ID.AddPointer(getCodeRegion());
  206. }
  207. void StringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  208. const StringLiteral *Str,
  209. const MemRegion *superRegion) {
  210. ID.AddInteger(static_cast<unsigned>(StringRegionKind));
  211. ID.AddPointer(Str);
  212. ID.AddPointer(superRegion);
  213. }
  214. void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  215. const ObjCStringLiteral *Str,
  216. const MemRegion *superRegion) {
  217. ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind));
  218. ID.AddPointer(Str);
  219. ID.AddPointer(superRegion);
  220. }
  221. void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  222. const Expr *Ex, unsigned cnt,
  223. const MemRegion *superRegion) {
  224. ID.AddInteger(static_cast<unsigned>(AllocaRegionKind));
  225. ID.AddPointer(Ex);
  226. ID.AddInteger(cnt);
  227. ID.AddPointer(superRegion);
  228. }
  229. void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  230. ProfileRegion(ID, Ex, Cnt, superRegion);
  231. }
  232. void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  233. CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
  234. }
  235. void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  236. const CompoundLiteralExpr *CL,
  237. const MemRegion* superRegion) {
  238. ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind));
  239. ID.AddPointer(CL);
  240. ID.AddPointer(superRegion);
  241. }
  242. void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  243. const PointerType *PT,
  244. const MemRegion *sRegion) {
  245. ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind));
  246. ID.AddPointer(PT);
  247. ID.AddPointer(sRegion);
  248. }
  249. void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  250. CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
  251. }
  252. void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  253. const ObjCIvarDecl *ivd,
  254. const MemRegion* superRegion) {
  255. DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind);
  256. }
  257. void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
  258. const MemRegion* superRegion, Kind k) {
  259. ID.AddInteger(static_cast<unsigned>(k));
  260. ID.AddPointer(D);
  261. ID.AddPointer(superRegion);
  262. }
  263. void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  264. DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
  265. }
  266. void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  267. VarRegion::ProfileRegion(ID, getDecl(), superRegion);
  268. }
  269. void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
  270. const MemRegion *sreg) {
  271. ID.AddInteger(static_cast<unsigned>(MemRegion::SymbolicRegionKind));
  272. ID.Add(sym);
  273. ID.AddPointer(sreg);
  274. }
  275. void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  276. SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
  277. }
  278. void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  279. QualType ElementType, SVal Idx,
  280. const MemRegion* superRegion) {
  281. ID.AddInteger(MemRegion::ElementRegionKind);
  282. ID.Add(ElementType);
  283. ID.AddPointer(superRegion);
  284. Idx.Profile(ID);
  285. }
  286. void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  287. ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
  288. }
  289. void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  290. const NamedDecl *FD,
  291. const MemRegion*) {
  292. ID.AddInteger(MemRegion::FunctionCodeRegionKind);
  293. ID.AddPointer(FD);
  294. }
  295. void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  296. FunctionCodeRegion::ProfileRegion(ID, FD, superRegion);
  297. }
  298. void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  299. const BlockDecl *BD, CanQualType,
  300. const AnalysisDeclContext *AC,
  301. const MemRegion*) {
  302. ID.AddInteger(MemRegion::BlockCodeRegionKind);
  303. ID.AddPointer(BD);
  304. }
  305. void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  306. BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
  307. }
  308. void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
  309. const BlockCodeRegion *BC,
  310. const LocationContext *LC,
  311. unsigned BlkCount,
  312. const MemRegion *sReg) {
  313. ID.AddInteger(MemRegion::BlockDataRegionKind);
  314. ID.AddPointer(BC);
  315. ID.AddPointer(LC);
  316. ID.AddInteger(BlkCount);
  317. ID.AddPointer(sReg);
  318. }
  319. void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
  320. BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion());
  321. }
  322. void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  323. Expr const *Ex,
  324. const MemRegion *sReg) {
  325. ID.AddPointer(Ex);
  326. ID.AddPointer(sReg);
  327. }
  328. void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  329. ProfileRegion(ID, Ex, getSuperRegion());
  330. }
  331. void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  332. const CXXRecordDecl *RD,
  333. bool IsVirtual,
  334. const MemRegion *SReg) {
  335. ID.AddPointer(RD);
  336. ID.AddBoolean(IsVirtual);
  337. ID.AddPointer(SReg);
  338. }
  339. void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  340. ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
  341. }
  342. void CXXDerivedObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
  343. const CXXRecordDecl *RD,
  344. const MemRegion *SReg) {
  345. ID.AddPointer(RD);
  346. ID.AddPointer(SReg);
  347. }
  348. void CXXDerivedObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
  349. ProfileRegion(ID, getDecl(), superRegion);
  350. }
  351. //===----------------------------------------------------------------------===//
  352. // Region anchors.
  353. //===----------------------------------------------------------------------===//
  354. void GlobalsSpaceRegion::anchor() {}
  355. void NonStaticGlobalSpaceRegion::anchor() {}
  356. void StackSpaceRegion::anchor() {}
  357. void TypedRegion::anchor() {}
  358. void TypedValueRegion::anchor() {}
  359. void CodeTextRegion::anchor() {}
  360. void SubRegion::anchor() {}
  361. //===----------------------------------------------------------------------===//
  362. // Region pretty-printing.
  363. //===----------------------------------------------------------------------===//
  364. LLVM_DUMP_METHOD void MemRegion::dump() const {
  365. dumpToStream(llvm::errs());
  366. }
  367. std::string MemRegion::getString() const {
  368. std::string s;
  369. llvm::raw_string_ostream os(s);
  370. dumpToStream(os);
  371. return os.str();
  372. }
  373. void MemRegion::dumpToStream(raw_ostream &os) const {
  374. os << "<Unknown Region>";
  375. }
  376. void AllocaRegion::dumpToStream(raw_ostream &os) const {
  377. os << "alloca{S" << Ex->getID(getContext()) << ',' << Cnt << '}';
  378. }
  379. void FunctionCodeRegion::dumpToStream(raw_ostream &os) const {
  380. os << "code{" << getDecl()->getDeclName().getAsString() << '}';
  381. }
  382. void BlockCodeRegion::dumpToStream(raw_ostream &os) const {
  383. os << "block_code{" << static_cast<const void *>(this) << '}';
  384. }
  385. void BlockDataRegion::dumpToStream(raw_ostream &os) const {
  386. os << "block_data{" << BC;
  387. os << "; ";
  388. for (BlockDataRegion::referenced_vars_iterator
  389. I = referenced_vars_begin(),
  390. E = referenced_vars_end(); I != E; ++I)
  391. os << "(" << I.getCapturedRegion() << "<-" <<
  392. I.getOriginalRegion() << ") ";
  393. os << '}';
  394. }
  395. void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
  396. // FIXME: More elaborate pretty-printing.
  397. os << "{ S" << CL->getID(getContext()) << " }";
  398. }
  399. void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
  400. os << "temp_object{" << getValueType().getAsString() << ", "
  401. << "S" << Ex->getID(getContext()) << '}';
  402. }
  403. void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
  404. os << "Base{" << superRegion << ',' << getDecl()->getName() << '}';
  405. }
  406. void CXXDerivedObjectRegion::dumpToStream(raw_ostream &os) const {
  407. os << "Derived{" << superRegion << ',' << getDecl()->getName() << '}';
  408. }
  409. void CXXThisRegion::dumpToStream(raw_ostream &os) const {
  410. os << "this";
  411. }
  412. void ElementRegion::dumpToStream(raw_ostream &os) const {
  413. os << "Element{" << superRegion << ','
  414. << Index << ',' << getElementType().getAsString() << '}';
  415. }
  416. void FieldRegion::dumpToStream(raw_ostream &os) const {
  417. os << superRegion << "." << *getDecl();
  418. }
  419. void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
  420. os << "Ivar{" << superRegion << ',' << *getDecl() << '}';
  421. }
  422. void StringRegion::dumpToStream(raw_ostream &os) const {
  423. assert(Str != nullptr && "Expecting non-null StringLiteral");
  424. Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
  425. }
  426. void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
  427. assert(Str != nullptr && "Expecting non-null ObjCStringLiteral");
  428. Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
  429. }
  430. void SymbolicRegion::dumpToStream(raw_ostream &os) const {
  431. if (isa<HeapSpaceRegion>(getSuperRegion()))
  432. os << "Heap";
  433. os << "SymRegion{" << sym << '}';
  434. }
  435. void VarRegion::dumpToStream(raw_ostream &os) const {
  436. const auto *VD = cast<VarDecl>(D);
  437. if (const IdentifierInfo *ID = VD->getIdentifier())
  438. os << ID->getName();
  439. else
  440. os << "VarRegion{D" << VD->getID() << '}';
  441. }
  442. LLVM_DUMP_METHOD void RegionRawOffset::dump() const {
  443. dumpToStream(llvm::errs());
  444. }
  445. void RegionRawOffset::dumpToStream(raw_ostream &os) const {
  446. os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
  447. }
  448. void CodeSpaceRegion::dumpToStream(raw_ostream &os) const {
  449. os << "CodeSpaceRegion";
  450. }
  451. void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
  452. os << "StaticGlobalsMemSpace{" << CR << '}';
  453. }
  454. void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
  455. os << "GlobalInternalSpaceRegion";
  456. }
  457. void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
  458. os << "GlobalSystemSpaceRegion";
  459. }
  460. void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
  461. os << "GlobalImmutableSpaceRegion";
  462. }
  463. void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
  464. os << "HeapSpaceRegion";
  465. }
  466. void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
  467. os << "UnknownSpaceRegion";
  468. }
  469. void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
  470. os << "StackArgumentsSpaceRegion";
  471. }
  472. void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
  473. os << "StackLocalsSpaceRegion";
  474. }
  475. bool MemRegion::canPrintPretty() const {
  476. return canPrintPrettyAsExpr();
  477. }
  478. bool MemRegion::canPrintPrettyAsExpr() const {
  479. return false;
  480. }
  481. void MemRegion::printPretty(raw_ostream &os) const {
  482. assert(canPrintPretty() && "This region cannot be printed pretty.");
  483. os << "'";
  484. printPrettyAsExpr(os);
  485. os << "'";
  486. }
  487. void MemRegion::printPrettyAsExpr(raw_ostream &) const {
  488. llvm_unreachable("This region cannot be printed pretty.");
  489. }
  490. bool VarRegion::canPrintPrettyAsExpr() const {
  491. return true;
  492. }
  493. void VarRegion::printPrettyAsExpr(raw_ostream &os) const {
  494. os << getDecl()->getName();
  495. }
  496. bool ObjCIvarRegion::canPrintPrettyAsExpr() const {
  497. return true;
  498. }
  499. void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const {
  500. os << getDecl()->getName();
  501. }
  502. bool FieldRegion::canPrintPretty() const {
  503. return true;
  504. }
  505. bool FieldRegion::canPrintPrettyAsExpr() const {
  506. return superRegion->canPrintPrettyAsExpr();
  507. }
  508. void FieldRegion::printPrettyAsExpr(raw_ostream &os) const {
  509. assert(canPrintPrettyAsExpr());
  510. superRegion->printPrettyAsExpr(os);
  511. os << "." << getDecl()->getName();
  512. }
  513. void FieldRegion::printPretty(raw_ostream &os) const {
  514. if (canPrintPrettyAsExpr()) {
  515. os << "\'";
  516. printPrettyAsExpr(os);
  517. os << "'";
  518. } else {
  519. os << "field " << "\'" << getDecl()->getName() << "'";
  520. }
  521. }
  522. bool CXXBaseObjectRegion::canPrintPrettyAsExpr() const {
  523. return superRegion->canPrintPrettyAsExpr();
  524. }
  525. void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
  526. superRegion->printPrettyAsExpr(os);
  527. }
  528. bool CXXDerivedObjectRegion::canPrintPrettyAsExpr() const {
  529. return superRegion->canPrintPrettyAsExpr();
  530. }
  531. void CXXDerivedObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
  532. superRegion->printPrettyAsExpr(os);
  533. }
  534. std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
  535. std::string VariableName;
  536. std::string ArrayIndices;
  537. const MemRegion *R = this;
  538. SmallString<50> buf;
  539. llvm::raw_svector_ostream os(buf);
  540. // Obtain array indices to add them to the variable name.
  541. const ElementRegion *ER = nullptr;
  542. while ((ER = R->getAs<ElementRegion>())) {
  543. // Index is a ConcreteInt.
  544. if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) {
  545. llvm::SmallString<2> Idx;
  546. CI->getValue().toString(Idx);
  547. ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str();
  548. }
  549. // If not a ConcreteInt, try to obtain the variable
  550. // name by calling 'getDescriptiveName' recursively.
  551. else {
  552. std::string Idx = ER->getDescriptiveName(false);
  553. if (!Idx.empty()) {
  554. ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str();
  555. }
  556. }
  557. R = ER->getSuperRegion();
  558. }
  559. // Get variable name.
  560. if (R && R->canPrintPrettyAsExpr()) {
  561. R->printPrettyAsExpr(os);
  562. if (UseQuotes)
  563. return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str();
  564. else
  565. return (llvm::Twine(os.str()) + ArrayIndices).str();
  566. }
  567. return VariableName;
  568. }
  569. SourceRange MemRegion::sourceRange() const {
  570. const auto *const VR = dyn_cast<VarRegion>(this->getBaseRegion());
  571. const auto *const FR = dyn_cast<FieldRegion>(this);
  572. // Check for more specific regions first.
  573. // FieldRegion
  574. if (FR) {
  575. return FR->getDecl()->getSourceRange();
  576. }
  577. // VarRegion
  578. else if (VR) {
  579. return VR->getDecl()->getSourceRange();
  580. }
  581. // Return invalid source range (can be checked by client).
  582. else
  583. return {};
  584. }
  585. //===----------------------------------------------------------------------===//
  586. // MemRegionManager methods.
  587. //===----------------------------------------------------------------------===//
  588. template <typename REG>
  589. const REG *MemRegionManager::LazyAllocate(REG*& region) {
  590. if (!region) {
  591. region = A.Allocate<REG>();
  592. new (region) REG(this);
  593. }
  594. return region;
  595. }
  596. template <typename REG, typename ARG>
  597. const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
  598. if (!region) {
  599. region = A.Allocate<REG>();
  600. new (region) REG(this, a);
  601. }
  602. return region;
  603. }
  604. const StackLocalsSpaceRegion*
  605. MemRegionManager::getStackLocalsRegion(const StackFrameContext *STC) {
  606. assert(STC);
  607. StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
  608. if (R)
  609. return R;
  610. R = A.Allocate<StackLocalsSpaceRegion>();
  611. new (R) StackLocalsSpaceRegion(this, STC);
  612. return R;
  613. }
  614. const StackArgumentsSpaceRegion *
  615. MemRegionManager::getStackArgumentsRegion(const StackFrameContext *STC) {
  616. assert(STC);
  617. StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
  618. if (R)
  619. return R;
  620. R = A.Allocate<StackArgumentsSpaceRegion>();
  621. new (R) StackArgumentsSpaceRegion(this, STC);
  622. return R;
  623. }
  624. const GlobalsSpaceRegion
  625. *MemRegionManager::getGlobalsRegion(MemRegion::Kind K,
  626. const CodeTextRegion *CR) {
  627. if (!CR) {
  628. if (K == MemRegion::GlobalSystemSpaceRegionKind)
  629. return LazyAllocate(SystemGlobals);
  630. if (K == MemRegion::GlobalImmutableSpaceRegionKind)
  631. return LazyAllocate(ImmutableGlobals);
  632. assert(K == MemRegion::GlobalInternalSpaceRegionKind);
  633. return LazyAllocate(InternalGlobals);
  634. }
  635. assert(K == MemRegion::StaticGlobalSpaceRegionKind);
  636. StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
  637. if (R)
  638. return R;
  639. R = A.Allocate<StaticGlobalSpaceRegion>();
  640. new (R) StaticGlobalSpaceRegion(this, CR);
  641. return R;
  642. }
  643. const HeapSpaceRegion *MemRegionManager::getHeapRegion() {
  644. return LazyAllocate(heap);
  645. }
  646. const UnknownSpaceRegion *MemRegionManager::getUnknownRegion() {
  647. return LazyAllocate(unknown);
  648. }
  649. const CodeSpaceRegion *MemRegionManager::getCodeRegion() {
  650. return LazyAllocate(code);
  651. }
  652. //===----------------------------------------------------------------------===//
  653. // Constructing regions.
  654. //===----------------------------------------------------------------------===//
  655. const StringRegion *MemRegionManager::getStringRegion(const StringLiteral *Str){
  656. return getSubRegion<StringRegion>(
  657. Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
  658. }
  659. const ObjCStringRegion *
  660. MemRegionManager::getObjCStringRegion(const ObjCStringLiteral *Str){
  661. return getSubRegion<ObjCStringRegion>(
  662. Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
  663. }
  664. /// Look through a chain of LocationContexts to either find the
  665. /// StackFrameContext that matches a DeclContext, or find a VarRegion
  666. /// for a variable captured by a block.
  667. static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
  668. getStackOrCaptureRegionForDeclContext(const LocationContext *LC,
  669. const DeclContext *DC,
  670. const VarDecl *VD) {
  671. while (LC) {
  672. if (const auto *SFC = dyn_cast<StackFrameContext>(LC)) {
  673. if (cast<DeclContext>(SFC->getDecl()) == DC)
  674. return SFC;
  675. }
  676. if (const auto *BC = dyn_cast<BlockInvocationContext>(LC)) {
  677. const auto *BR =
  678. static_cast<const BlockDataRegion *>(BC->getContextData());
  679. // FIXME: This can be made more efficient.
  680. for (BlockDataRegion::referenced_vars_iterator
  681. I = BR->referenced_vars_begin(),
  682. E = BR->referenced_vars_end(); I != E; ++I) {
  683. const VarRegion *VR = I.getOriginalRegion();
  684. if (VR->getDecl() == VD)
  685. return cast<VarRegion>(I.getCapturedRegion());
  686. }
  687. }
  688. LC = LC->getParent();
  689. }
  690. return (const StackFrameContext *)nullptr;
  691. }
  692. const VarRegion* MemRegionManager::getVarRegion(const VarDecl *D,
  693. const LocationContext *LC) {
  694. D = D->getCanonicalDecl();
  695. const MemRegion *sReg = nullptr;
  696. if (D->hasGlobalStorage() && !D->isStaticLocal()) {
  697. // First handle the globals defined in system headers.
  698. if (C.getSourceManager().isInSystemHeader(D->getLocation())) {
  699. // Whitelist the system globals which often DO GET modified, assume the
  700. // rest are immutable.
  701. if (D->getName().find("errno") != StringRef::npos)
  702. sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
  703. else
  704. sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
  705. // Treat other globals as GlobalInternal unless they are constants.
  706. } else {
  707. QualType GQT = D->getType();
  708. const Type *GT = GQT.getTypePtrOrNull();
  709. // TODO: We could walk the complex types here and see if everything is
  710. // constified.
  711. if (GT && GQT.isConstQualified() && GT->isArithmeticType())
  712. sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
  713. else
  714. sReg = getGlobalsRegion();
  715. }
  716. // Finally handle static locals.
  717. } else {
  718. // FIXME: Once we implement scope handling, we will need to properly lookup
  719. // 'D' to the proper LocationContext.
  720. const DeclContext *DC = D->getDeclContext();
  721. llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
  722. getStackOrCaptureRegionForDeclContext(LC, DC, D);
  723. if (V.is<const VarRegion*>())
  724. return V.get<const VarRegion*>();
  725. const auto *STC = V.get<const StackFrameContext *>();
  726. if (!STC) {
  727. // FIXME: Assign a more sensible memory space to static locals
  728. // we see from within blocks that we analyze as top-level declarations.
  729. sReg = getUnknownRegion();
  730. } else {
  731. if (D->hasLocalStorage()) {
  732. sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
  733. ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
  734. : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
  735. }
  736. else {
  737. assert(D->isStaticLocal());
  738. const Decl *STCD = STC->getDecl();
  739. if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD))
  740. sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
  741. getFunctionCodeRegion(cast<NamedDecl>(STCD)));
  742. else if (const auto *BD = dyn_cast<BlockDecl>(STCD)) {
  743. // FIXME: The fallback type here is totally bogus -- though it should
  744. // never be queried, it will prevent uniquing with the real
  745. // BlockCodeRegion. Ideally we'd fix the AST so that we always had a
  746. // signature.
  747. QualType T;
  748. if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
  749. T = TSI->getType();
  750. if (T.isNull())
  751. T = getContext().VoidTy;
  752. if (!T->getAs<FunctionType>())
  753. T = getContext().getFunctionNoProtoType(T);
  754. T = getContext().getBlockPointerType(T);
  755. const BlockCodeRegion *BTR =
  756. getBlockCodeRegion(BD, C.getCanonicalType(T),
  757. STC->getAnalysisDeclContext());
  758. sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
  759. BTR);
  760. }
  761. else {
  762. sReg = getGlobalsRegion();
  763. }
  764. }
  765. }
  766. }
  767. return getSubRegion<VarRegion>(D, sReg);
  768. }
  769. const VarRegion *MemRegionManager::getVarRegion(const VarDecl *D,
  770. const MemRegion *superR) {
  771. D = D->getCanonicalDecl();
  772. return getSubRegion<VarRegion>(D, superR);
  773. }
  774. const BlockDataRegion *
  775. MemRegionManager::getBlockDataRegion(const BlockCodeRegion *BC,
  776. const LocationContext *LC,
  777. unsigned blockCount) {
  778. const MemSpaceRegion *sReg = nullptr;
  779. const BlockDecl *BD = BC->getDecl();
  780. if (!BD->hasCaptures()) {
  781. // This handles 'static' blocks.
  782. sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
  783. }
  784. else {
  785. if (LC) {
  786. // FIXME: Once we implement scope handling, we want the parent region
  787. // to be the scope.
  788. const StackFrameContext *STC = LC->getStackFrame();
  789. assert(STC);
  790. sReg = getStackLocalsRegion(STC);
  791. }
  792. else {
  793. // We allow 'LC' to be NULL for cases where want BlockDataRegions
  794. // without context-sensitivity.
  795. sReg = getUnknownRegion();
  796. }
  797. }
  798. return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg);
  799. }
  800. const CXXTempObjectRegion *
  801. MemRegionManager::getCXXStaticTempObjectRegion(const Expr *Ex) {
  802. return getSubRegion<CXXTempObjectRegion>(
  803. Ex, getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr));
  804. }
  805. const CompoundLiteralRegion*
  806. MemRegionManager::getCompoundLiteralRegion(const CompoundLiteralExpr *CL,
  807. const LocationContext *LC) {
  808. const MemSpaceRegion *sReg = nullptr;
  809. if (CL->isFileScope())
  810. sReg = getGlobalsRegion();
  811. else {
  812. const StackFrameContext *STC = LC->getStackFrame();
  813. assert(STC);
  814. sReg = getStackLocalsRegion(STC);
  815. }
  816. return getSubRegion<CompoundLiteralRegion>(CL, sReg);
  817. }
  818. const ElementRegion*
  819. MemRegionManager::getElementRegion(QualType elementType, NonLoc Idx,
  820. const SubRegion* superRegion,
  821. ASTContext &Ctx){
  822. QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
  823. llvm::FoldingSetNodeID ID;
  824. ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
  825. void *InsertPos;
  826. MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
  827. auto *R = cast_or_null<ElementRegion>(data);
  828. if (!R) {
  829. R = A.Allocate<ElementRegion>();
  830. new (R) ElementRegion(T, Idx, superRegion);
  831. Regions.InsertNode(R, InsertPos);
  832. }
  833. return R;
  834. }
  835. const FunctionCodeRegion *
  836. MemRegionManager::getFunctionCodeRegion(const NamedDecl *FD) {
  837. // To think: should we canonicalize the declaration here?
  838. return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion());
  839. }
  840. const BlockCodeRegion *
  841. MemRegionManager::getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy,
  842. AnalysisDeclContext *AC) {
  843. return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion());
  844. }
  845. /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
  846. const SymbolicRegion *MemRegionManager::getSymbolicRegion(SymbolRef sym) {
  847. return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
  848. }
  849. const SymbolicRegion *MemRegionManager::getSymbolicHeapRegion(SymbolRef Sym) {
  850. return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
  851. }
  852. const FieldRegion*
  853. MemRegionManager::getFieldRegion(const FieldDecl *d,
  854. const SubRegion* superRegion){
  855. return getSubRegion<FieldRegion>(d, superRegion);
  856. }
  857. const ObjCIvarRegion*
  858. MemRegionManager::getObjCIvarRegion(const ObjCIvarDecl *d,
  859. const SubRegion* superRegion) {
  860. return getSubRegion<ObjCIvarRegion>(d, superRegion);
  861. }
  862. const CXXTempObjectRegion*
  863. MemRegionManager::getCXXTempObjectRegion(Expr const *E,
  864. LocationContext const *LC) {
  865. const StackFrameContext *SFC = LC->getStackFrame();
  866. assert(SFC);
  867. return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
  868. }
  869. /// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
  870. /// class of the type of \p Super.
  871. static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
  872. const TypedValueRegion *Super,
  873. bool IsVirtual) {
  874. BaseClass = BaseClass->getCanonicalDecl();
  875. const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
  876. if (!Class)
  877. return true;
  878. if (IsVirtual)
  879. return Class->isVirtuallyDerivedFrom(BaseClass);
  880. for (const auto &I : Class->bases()) {
  881. if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
  882. return true;
  883. }
  884. return false;
  885. }
  886. const CXXBaseObjectRegion *
  887. MemRegionManager::getCXXBaseObjectRegion(const CXXRecordDecl *RD,
  888. const SubRegion *Super,
  889. bool IsVirtual) {
  890. if (isa<TypedValueRegion>(Super)) {
  891. assert(isValidBaseClass(RD, cast<TypedValueRegion>(Super), IsVirtual));
  892. (void)&isValidBaseClass;
  893. if (IsVirtual) {
  894. // Virtual base regions should not be layered, since the layout rules
  895. // are different.
  896. while (const auto *Base = dyn_cast<CXXBaseObjectRegion>(Super))
  897. Super = cast<SubRegion>(Base->getSuperRegion());
  898. assert(Super && !isa<MemSpaceRegion>(Super));
  899. }
  900. }
  901. return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
  902. }
  903. const CXXDerivedObjectRegion *
  904. MemRegionManager::getCXXDerivedObjectRegion(const CXXRecordDecl *RD,
  905. const SubRegion *Super) {
  906. return getSubRegion<CXXDerivedObjectRegion>(RD, Super);
  907. }
  908. const CXXThisRegion*
  909. MemRegionManager::getCXXThisRegion(QualType thisPointerTy,
  910. const LocationContext *LC) {
  911. const auto *PT = thisPointerTy->getAs<PointerType>();
  912. assert(PT);
  913. // Inside the body of the operator() of a lambda a this expr might refer to an
  914. // object in one of the parent location contexts.
  915. const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl());
  916. // FIXME: when operator() of lambda is analyzed as a top level function and
  917. // 'this' refers to a this to the enclosing scope, there is no right region to
  918. // return.
  919. while (!LC->inTopFrame() && (!D || D->isStatic() ||
  920. PT != D->getThisType()->getAs<PointerType>())) {
  921. LC = LC->getParent();
  922. D = dyn_cast<CXXMethodDecl>(LC->getDecl());
  923. }
  924. const StackFrameContext *STC = LC->getStackFrame();
  925. assert(STC);
  926. return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
  927. }
  928. const AllocaRegion*
  929. MemRegionManager::getAllocaRegion(const Expr *E, unsigned cnt,
  930. const LocationContext *LC) {
  931. const StackFrameContext *STC = LC->getStackFrame();
  932. assert(STC);
  933. return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
  934. }
  935. const MemSpaceRegion *MemRegion::getMemorySpace() const {
  936. const MemRegion *R = this;
  937. const auto *SR = dyn_cast<SubRegion>(this);
  938. while (SR) {
  939. R = SR->getSuperRegion();
  940. SR = dyn_cast<SubRegion>(R);
  941. }
  942. return dyn_cast<MemSpaceRegion>(R);
  943. }
  944. bool MemRegion::hasStackStorage() const {
  945. return isa<StackSpaceRegion>(getMemorySpace());
  946. }
  947. bool MemRegion::hasStackNonParametersStorage() const {
  948. return isa<StackLocalsSpaceRegion>(getMemorySpace());
  949. }
  950. bool MemRegion::hasStackParametersStorage() const {
  951. return isa<StackArgumentsSpaceRegion>(getMemorySpace());
  952. }
  953. bool MemRegion::hasGlobalsOrParametersStorage() const {
  954. const MemSpaceRegion *MS = getMemorySpace();
  955. return isa<StackArgumentsSpaceRegion>(MS) ||
  956. isa<GlobalsSpaceRegion>(MS);
  957. }
  958. // getBaseRegion strips away all elements and fields, and get the base region
  959. // of them.
  960. const MemRegion *MemRegion::getBaseRegion() const {
  961. const MemRegion *R = this;
  962. while (true) {
  963. switch (R->getKind()) {
  964. case MemRegion::ElementRegionKind:
  965. case MemRegion::FieldRegionKind:
  966. case MemRegion::ObjCIvarRegionKind:
  967. case MemRegion::CXXBaseObjectRegionKind:
  968. case MemRegion::CXXDerivedObjectRegionKind:
  969. R = cast<SubRegion>(R)->getSuperRegion();
  970. continue;
  971. default:
  972. break;
  973. }
  974. break;
  975. }
  976. return R;
  977. }
  978. // getgetMostDerivedObjectRegion gets the region of the root class of a C++
  979. // class hierarchy.
  980. const MemRegion *MemRegion::getMostDerivedObjectRegion() const {
  981. const MemRegion *R = this;
  982. while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
  983. R = BR->getSuperRegion();
  984. return R;
  985. }
  986. bool MemRegion::isSubRegionOf(const MemRegion *) const {
  987. return false;
  988. }
  989. //===----------------------------------------------------------------------===//
  990. // View handling.
  991. //===----------------------------------------------------------------------===//
  992. const MemRegion *MemRegion::StripCasts(bool StripBaseAndDerivedCasts) const {
  993. const MemRegion *R = this;
  994. while (true) {
  995. switch (R->getKind()) {
  996. case ElementRegionKind: {
  997. const auto *ER = cast<ElementRegion>(R);
  998. if (!ER->getIndex().isZeroConstant())
  999. return R;
  1000. R = ER->getSuperRegion();
  1001. break;
  1002. }
  1003. case CXXBaseObjectRegionKind:
  1004. case CXXDerivedObjectRegionKind:
  1005. if (!StripBaseAndDerivedCasts)
  1006. return R;
  1007. R = cast<TypedValueRegion>(R)->getSuperRegion();
  1008. break;
  1009. default:
  1010. return R;
  1011. }
  1012. }
  1013. }
  1014. const SymbolicRegion *MemRegion::getSymbolicBase() const {
  1015. const auto *SubR = dyn_cast<SubRegion>(this);
  1016. while (SubR) {
  1017. if (const auto *SymR = dyn_cast<SymbolicRegion>(SubR))
  1018. return SymR;
  1019. SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
  1020. }
  1021. return nullptr;
  1022. }
  1023. RegionRawOffset ElementRegion::getAsArrayOffset() const {
  1024. int64_t offset = 0;
  1025. const ElementRegion *ER = this;
  1026. const MemRegion *superR = nullptr;
  1027. ASTContext &C = getContext();
  1028. // FIXME: Handle multi-dimensional arrays.
  1029. while (ER) {
  1030. superR = ER->getSuperRegion();
  1031. // FIXME: generalize to symbolic offsets.
  1032. SVal index = ER->getIndex();
  1033. if (auto CI = index.getAs<nonloc::ConcreteInt>()) {
  1034. // Update the offset.
  1035. int64_t i = CI->getValue().getSExtValue();
  1036. if (i != 0) {
  1037. QualType elemType = ER->getElementType();
  1038. // If we are pointing to an incomplete type, go no further.
  1039. if (elemType->isIncompleteType()) {
  1040. superR = ER;
  1041. break;
  1042. }
  1043. int64_t size = C.getTypeSizeInChars(elemType).getQuantity();
  1044. if (auto NewOffset = llvm::checkedMulAdd(i, size, offset)) {
  1045. offset = *NewOffset;
  1046. } else {
  1047. LLVM_DEBUG(llvm::dbgs() << "MemRegion::getAsArrayOffset: "
  1048. << "offset overflowing, returning unknown\n");
  1049. return nullptr;
  1050. }
  1051. }
  1052. // Go to the next ElementRegion (if any).
  1053. ER = dyn_cast<ElementRegion>(superR);
  1054. continue;
  1055. }
  1056. return nullptr;
  1057. }
  1058. assert(superR && "super region cannot be NULL");
  1059. return RegionRawOffset(superR, CharUnits::fromQuantity(offset));
  1060. }
  1061. /// Returns true if \p Base is an immediate base class of \p Child
  1062. static bool isImmediateBase(const CXXRecordDecl *Child,
  1063. const CXXRecordDecl *Base) {
  1064. assert(Child && "Child must not be null");
  1065. // Note that we do NOT canonicalize the base class here, because
  1066. // ASTRecordLayout doesn't either. If that leads us down the wrong path,
  1067. // so be it; at least we won't crash.
  1068. for (const auto &I : Child->bases()) {
  1069. if (I.getType()->getAsCXXRecordDecl() == Base)
  1070. return true;
  1071. }
  1072. return false;
  1073. }
  1074. static RegionOffset calculateOffset(const MemRegion *R) {
  1075. const MemRegion *SymbolicOffsetBase = nullptr;
  1076. int64_t Offset = 0;
  1077. while (true) {
  1078. switch (R->getKind()) {
  1079. case MemRegion::CodeSpaceRegionKind:
  1080. case MemRegion::StackLocalsSpaceRegionKind:
  1081. case MemRegion::StackArgumentsSpaceRegionKind:
  1082. case MemRegion::HeapSpaceRegionKind:
  1083. case MemRegion::UnknownSpaceRegionKind:
  1084. case MemRegion::StaticGlobalSpaceRegionKind:
  1085. case MemRegion::GlobalInternalSpaceRegionKind:
  1086. case MemRegion::GlobalSystemSpaceRegionKind:
  1087. case MemRegion::GlobalImmutableSpaceRegionKind:
  1088. // Stores can bind directly to a region space to set a default value.
  1089. assert(Offset == 0 && !SymbolicOffsetBase);
  1090. goto Finish;
  1091. case MemRegion::FunctionCodeRegionKind:
  1092. case MemRegion::BlockCodeRegionKind:
  1093. case MemRegion::BlockDataRegionKind:
  1094. // These will never have bindings, but may end up having values requested
  1095. // if the user does some strange casting.
  1096. if (Offset != 0)
  1097. SymbolicOffsetBase = R;
  1098. goto Finish;
  1099. case MemRegion::SymbolicRegionKind:
  1100. case MemRegion::AllocaRegionKind:
  1101. case MemRegion::CompoundLiteralRegionKind:
  1102. case MemRegion::CXXThisRegionKind:
  1103. case MemRegion::StringRegionKind:
  1104. case MemRegion::ObjCStringRegionKind:
  1105. case MemRegion::VarRegionKind:
  1106. case MemRegion::CXXTempObjectRegionKind:
  1107. // Usual base regions.
  1108. goto Finish;
  1109. case MemRegion::ObjCIvarRegionKind:
  1110. // This is a little strange, but it's a compromise between
  1111. // ObjCIvarRegions having unknown compile-time offsets (when using the
  1112. // non-fragile runtime) and yet still being distinct, non-overlapping
  1113. // regions. Thus we treat them as "like" base regions for the purposes
  1114. // of computing offsets.
  1115. goto Finish;
  1116. case MemRegion::CXXBaseObjectRegionKind: {
  1117. const auto *BOR = cast<CXXBaseObjectRegion>(R);
  1118. R = BOR->getSuperRegion();
  1119. QualType Ty;
  1120. bool RootIsSymbolic = false;
  1121. if (const auto *TVR = dyn_cast<TypedValueRegion>(R)) {
  1122. Ty = TVR->getDesugaredValueType(R->getContext());
  1123. } else if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
  1124. // If our base region is symbolic, we don't know what type it really is.
  1125. // Pretend the type of the symbol is the true dynamic type.
  1126. // (This will at least be self-consistent for the life of the symbol.)
  1127. Ty = SR->getSymbol()->getType()->getPointeeType();
  1128. RootIsSymbolic = true;
  1129. }
  1130. const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
  1131. if (!Child) {
  1132. // We cannot compute the offset of the base class.
  1133. SymbolicOffsetBase = R;
  1134. } else {
  1135. if (RootIsSymbolic) {
  1136. // Base layers on symbolic regions may not be type-correct.
  1137. // Double-check the inheritance here, and revert to a symbolic offset
  1138. // if it's invalid (e.g. due to a reinterpret_cast).
  1139. if (BOR->isVirtual()) {
  1140. if (!Child->isVirtuallyDerivedFrom(BOR->getDecl()))
  1141. SymbolicOffsetBase = R;
  1142. } else {
  1143. if (!isImmediateBase(Child, BOR->getDecl()))
  1144. SymbolicOffsetBase = R;
  1145. }
  1146. }
  1147. }
  1148. // Don't bother calculating precise offsets if we already have a
  1149. // symbolic offset somewhere in the chain.
  1150. if (SymbolicOffsetBase)
  1151. continue;
  1152. CharUnits BaseOffset;
  1153. const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(Child);
  1154. if (BOR->isVirtual())
  1155. BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
  1156. else
  1157. BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
  1158. // The base offset is in chars, not in bits.
  1159. Offset += BaseOffset.getQuantity() * R->getContext().getCharWidth();
  1160. break;
  1161. }
  1162. case MemRegion::CXXDerivedObjectRegionKind: {
  1163. // TODO: Store the base type in the CXXDerivedObjectRegion and use it.
  1164. goto Finish;
  1165. }
  1166. case MemRegion::ElementRegionKind: {
  1167. const auto *ER = cast<ElementRegion>(R);
  1168. R = ER->getSuperRegion();
  1169. QualType EleTy = ER->getValueType();
  1170. if (EleTy->isIncompleteType()) {
  1171. // We cannot compute the offset of the base class.
  1172. SymbolicOffsetBase = R;
  1173. continue;
  1174. }
  1175. SVal Index = ER->getIndex();
  1176. if (Optional<nonloc::ConcreteInt> CI =
  1177. Index.getAs<nonloc::ConcreteInt>()) {
  1178. // Don't bother calculating precise offsets if we already have a
  1179. // symbolic offset somewhere in the chain.
  1180. if (SymbolicOffsetBase)
  1181. continue;
  1182. int64_t i = CI->getValue().getSExtValue();
  1183. // This type size is in bits.
  1184. Offset += i * R->getContext().getTypeSize(EleTy);
  1185. } else {
  1186. // We cannot compute offset for non-concrete index.
  1187. SymbolicOffsetBase = R;
  1188. }
  1189. break;
  1190. }
  1191. case MemRegion::FieldRegionKind: {
  1192. const auto *FR = cast<FieldRegion>(R);
  1193. R = FR->getSuperRegion();
  1194. assert(R);
  1195. const RecordDecl *RD = FR->getDecl()->getParent();
  1196. if (RD->isUnion() || !RD->isCompleteDefinition()) {
  1197. // We cannot compute offset for incomplete type.
  1198. // For unions, we could treat everything as offset 0, but we'd rather
  1199. // treat each field as a symbolic offset so they aren't stored on top
  1200. // of each other, since we depend on things in typed regions actually
  1201. // matching their types.
  1202. SymbolicOffsetBase = R;
  1203. }
  1204. // Don't bother calculating precise offsets if we already have a
  1205. // symbolic offset somewhere in the chain.
  1206. if (SymbolicOffsetBase)
  1207. continue;
  1208. // Get the field number.
  1209. unsigned idx = 0;
  1210. for (RecordDecl::field_iterator FI = RD->field_begin(),
  1211. FE = RD->field_end(); FI != FE; ++FI, ++idx) {
  1212. if (FR->getDecl() == *FI)
  1213. break;
  1214. }
  1215. const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(RD);
  1216. // This is offset in bits.
  1217. Offset += Layout.getFieldOffset(idx);
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. Finish:
  1223. if (SymbolicOffsetBase)
  1224. return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
  1225. return RegionOffset(R, Offset);
  1226. }
  1227. RegionOffset MemRegion::getAsOffset() const {
  1228. if (!cachedOffset)
  1229. cachedOffset = calculateOffset(this);
  1230. return *cachedOffset;
  1231. }
  1232. //===----------------------------------------------------------------------===//
  1233. // BlockDataRegion
  1234. //===----------------------------------------------------------------------===//
  1235. std::pair<const VarRegion *, const VarRegion *>
  1236. BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
  1237. MemRegionManager &MemMgr = *getMemRegionManager();
  1238. const VarRegion *VR = nullptr;
  1239. const VarRegion *OriginalVR = nullptr;
  1240. if (!VD->hasAttr<BlocksAttr>() && VD->hasLocalStorage()) {
  1241. VR = MemMgr.getVarRegion(VD, this);
  1242. OriginalVR = MemMgr.getVarRegion(VD, LC);
  1243. }
  1244. else {
  1245. if (LC) {
  1246. VR = MemMgr.getVarRegion(VD, LC);
  1247. OriginalVR = VR;
  1248. }
  1249. else {
  1250. VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
  1251. OriginalVR = MemMgr.getVarRegion(VD, LC);
  1252. }
  1253. }
  1254. return std::make_pair(VR, OriginalVR);
  1255. }
  1256. void BlockDataRegion::LazyInitializeReferencedVars() {
  1257. if (ReferencedVars)
  1258. return;
  1259. AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
  1260. const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl());
  1261. auto NumBlockVars =
  1262. std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end());
  1263. if (NumBlockVars == 0) {
  1264. ReferencedVars = (void*) 0x1;
  1265. return;
  1266. }
  1267. MemRegionManager &MemMgr = *getMemRegionManager();
  1268. llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
  1269. BumpVectorContext BC(A);
  1270. using VarVec = BumpVector<const MemRegion *>;
  1271. auto *BV = A.Allocate<VarVec>();
  1272. new (BV) VarVec(BC, NumBlockVars);
  1273. auto *BVOriginal = A.Allocate<VarVec>();
  1274. new (BVOriginal) VarVec(BC, NumBlockVars);
  1275. for (const auto *VD : ReferencedBlockVars) {
  1276. const VarRegion *VR = nullptr;
  1277. const VarRegion *OriginalVR = nullptr;
  1278. std::tie(VR, OriginalVR) = getCaptureRegions(VD);
  1279. assert(VR);
  1280. assert(OriginalVR);
  1281. BV->push_back(VR, BC);
  1282. BVOriginal->push_back(OriginalVR, BC);
  1283. }
  1284. ReferencedVars = BV;
  1285. OriginalVars = BVOriginal;
  1286. }
  1287. BlockDataRegion::referenced_vars_iterator
  1288. BlockDataRegion::referenced_vars_begin() const {
  1289. const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
  1290. auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
  1291. if (Vec == (void*) 0x1)
  1292. return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
  1293. auto *VecOriginal =
  1294. static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
  1295. return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
  1296. VecOriginal->begin());
  1297. }
  1298. BlockDataRegion::referenced_vars_iterator
  1299. BlockDataRegion::referenced_vars_end() const {
  1300. const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
  1301. auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
  1302. if (Vec == (void*) 0x1)
  1303. return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
  1304. auto *VecOriginal =
  1305. static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
  1306. return BlockDataRegion::referenced_vars_iterator(Vec->end(),
  1307. VecOriginal->end());
  1308. }
  1309. const VarRegion *BlockDataRegion::getOriginalRegion(const VarRegion *R) const {
  1310. for (referenced_vars_iterator I = referenced_vars_begin(),
  1311. E = referenced_vars_end();
  1312. I != E; ++I) {
  1313. if (I.getCapturedRegion() == R)
  1314. return I.getOriginalRegion();
  1315. }
  1316. return nullptr;
  1317. }
  1318. //===----------------------------------------------------------------------===//
  1319. // RegionAndSymbolInvalidationTraits
  1320. //===----------------------------------------------------------------------===//
  1321. void RegionAndSymbolInvalidationTraits::setTrait(SymbolRef Sym,
  1322. InvalidationKinds IK) {
  1323. SymTraitsMap[Sym] |= IK;
  1324. }
  1325. void RegionAndSymbolInvalidationTraits::setTrait(const MemRegion *MR,
  1326. InvalidationKinds IK) {
  1327. assert(MR);
  1328. if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
  1329. setTrait(SR->getSymbol(), IK);
  1330. else
  1331. MRTraitsMap[MR] |= IK;
  1332. }
  1333. bool RegionAndSymbolInvalidationTraits::hasTrait(SymbolRef Sym,
  1334. InvalidationKinds IK) const {
  1335. const_symbol_iterator I = SymTraitsMap.find(Sym);
  1336. if (I != SymTraitsMap.end())
  1337. return I->second & IK;
  1338. return false;
  1339. }
  1340. bool RegionAndSymbolInvalidationTraits::hasTrait(const MemRegion *MR,
  1341. InvalidationKinds IK) const {
  1342. if (!MR)
  1343. return false;
  1344. if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
  1345. return hasTrait(SR->getSymbol(), IK);
  1346. const_region_iterator I = MRTraitsMap.find(MR);
  1347. if (I != MRTraitsMap.end())
  1348. return I->second & IK;
  1349. return false;
  1350. }