RegionStore.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. //== RegionStore.cpp - Field-sensitive store model --------------*- 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 a basic region store model. In this model, we do have field
  11. // sensitivity. But we assume nothing about the heap shape. So recursive data
  12. // structures are largely ignored. Basically we do 1-limiting analysis.
  13. // Parameter pointers are assumed with no aliasing. Pointee objects of
  14. // parameters are created lazily.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "clang/AST/Attr.h"
  18. #include "clang/AST/CharUnits.h"
  19. #include "clang/Analysis/Analyses/LiveVariables.h"
  20. #include "clang/Analysis/AnalysisContext.h"
  21. #include "clang/Basic/TargetInfo.h"
  22. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
  24. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  25. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  26. #include "llvm/ADT/ImmutableList.h"
  27. #include "llvm/ADT/ImmutableMap.h"
  28. #include "llvm/ADT/Optional.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. using namespace clang;
  31. using namespace ento;
  32. //===----------------------------------------------------------------------===//
  33. // Representation of binding keys.
  34. //===----------------------------------------------------------------------===//
  35. namespace {
  36. class BindingKey {
  37. public:
  38. enum Kind { Default = 0x0, Direct = 0x1 };
  39. private:
  40. enum { Symbolic = 0x2 };
  41. llvm::PointerIntPair<const MemRegion *, 2> P;
  42. uint64_t Data;
  43. /// Create a key for a binding to region \p r, which has a symbolic offset
  44. /// from region \p Base.
  45. explicit BindingKey(const SubRegion *r, const SubRegion *Base, Kind k)
  46. : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) {
  47. assert(r && Base && "Must have known regions.");
  48. assert(getConcreteOffsetRegion() == Base && "Failed to store base region");
  49. }
  50. /// Create a key for a binding at \p offset from base region \p r.
  51. explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
  52. : P(r, k), Data(offset) {
  53. assert(r && "Must have known regions.");
  54. assert(getOffset() == offset && "Failed to store offset");
  55. assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r)) && "Not a base");
  56. }
  57. public:
  58. bool isDirect() const { return P.getInt() & Direct; }
  59. bool hasSymbolicOffset() const { return P.getInt() & Symbolic; }
  60. const MemRegion *getRegion() const { return P.getPointer(); }
  61. uint64_t getOffset() const {
  62. assert(!hasSymbolicOffset());
  63. return Data;
  64. }
  65. const SubRegion *getConcreteOffsetRegion() const {
  66. assert(hasSymbolicOffset());
  67. return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data));
  68. }
  69. const MemRegion *getBaseRegion() const {
  70. if (hasSymbolicOffset())
  71. return getConcreteOffsetRegion()->getBaseRegion();
  72. return getRegion()->getBaseRegion();
  73. }
  74. void Profile(llvm::FoldingSetNodeID& ID) const {
  75. ID.AddPointer(P.getOpaqueValue());
  76. ID.AddInteger(Data);
  77. }
  78. static BindingKey Make(const MemRegion *R, Kind k);
  79. bool operator<(const BindingKey &X) const {
  80. if (P.getOpaqueValue() < X.P.getOpaqueValue())
  81. return true;
  82. if (P.getOpaqueValue() > X.P.getOpaqueValue())
  83. return false;
  84. return Data < X.Data;
  85. }
  86. bool operator==(const BindingKey &X) const {
  87. return P.getOpaqueValue() == X.P.getOpaqueValue() &&
  88. Data == X.Data;
  89. }
  90. LLVM_ATTRIBUTE_USED void dump() const;
  91. };
  92. } // end anonymous namespace
  93. BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
  94. const RegionOffset &RO = R->getAsOffset();
  95. if (RO.hasSymbolicOffset())
  96. return BindingKey(cast<SubRegion>(R), cast<SubRegion>(RO.getRegion()), k);
  97. return BindingKey(RO.getRegion(), RO.getOffset(), k);
  98. }
  99. namespace llvm {
  100. static inline
  101. raw_ostream &operator<<(raw_ostream &os, BindingKey K) {
  102. os << '(' << K.getRegion();
  103. if (!K.hasSymbolicOffset())
  104. os << ',' << K.getOffset();
  105. os << ',' << (K.isDirect() ? "direct" : "default")
  106. << ')';
  107. return os;
  108. }
  109. } // end llvm namespace
  110. void BindingKey::dump() const {
  111. llvm::errs() << *this;
  112. }
  113. //===----------------------------------------------------------------------===//
  114. // Actual Store type.
  115. //===----------------------------------------------------------------------===//
  116. typedef llvm::ImmutableMap<BindingKey, SVal> ClusterBindings;
  117. typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef;
  118. typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings>
  119. RegionBindings;
  120. namespace {
  121. class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
  122. ClusterBindings> {
  123. ClusterBindings::Factory &CBFactory;
  124. public:
  125. typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>
  126. ParentTy;
  127. RegionBindingsRef(ClusterBindings::Factory &CBFactory,
  128. const RegionBindings::TreeTy *T,
  129. RegionBindings::TreeTy::Factory *F)
  130. : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F),
  131. CBFactory(CBFactory) {}
  132. RegionBindingsRef(const ParentTy &P, ClusterBindings::Factory &CBFactory)
  133. : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P),
  134. CBFactory(CBFactory) {}
  135. RegionBindingsRef add(key_type_ref K, data_type_ref D) const {
  136. return RegionBindingsRef(static_cast<const ParentTy*>(this)->add(K, D),
  137. CBFactory);
  138. }
  139. RegionBindingsRef remove(key_type_ref K) const {
  140. return RegionBindingsRef(static_cast<const ParentTy*>(this)->remove(K),
  141. CBFactory);
  142. }
  143. RegionBindingsRef addBinding(BindingKey K, SVal V) const;
  144. RegionBindingsRef addBinding(const MemRegion *R,
  145. BindingKey::Kind k, SVal V) const;
  146. RegionBindingsRef &operator=(const RegionBindingsRef &X) {
  147. *static_cast<ParentTy*>(this) = X;
  148. return *this;
  149. }
  150. const SVal *lookup(BindingKey K) const;
  151. const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const;
  152. const ClusterBindings *lookup(const MemRegion *R) const {
  153. return static_cast<const ParentTy*>(this)->lookup(R);
  154. }
  155. RegionBindingsRef removeBinding(BindingKey K);
  156. RegionBindingsRef removeBinding(const MemRegion *R,
  157. BindingKey::Kind k);
  158. RegionBindingsRef removeBinding(const MemRegion *R) {
  159. return removeBinding(R, BindingKey::Direct).
  160. removeBinding(R, BindingKey::Default);
  161. }
  162. Optional<SVal> getDirectBinding(const MemRegion *R) const;
  163. /// getDefaultBinding - Returns an SVal* representing an optional default
  164. /// binding associated with a region and its subregions.
  165. Optional<SVal> getDefaultBinding(const MemRegion *R) const;
  166. /// Return the internal tree as a Store.
  167. Store asStore() const {
  168. return asImmutableMap().getRootWithoutRetain();
  169. }
  170. void dump(raw_ostream &OS, const char *nl) const {
  171. for (iterator I = begin(), E = end(); I != E; ++I) {
  172. const ClusterBindings &Cluster = I.getData();
  173. for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
  174. CI != CE; ++CI) {
  175. OS << ' ' << CI.getKey() << " : " << CI.getData() << nl;
  176. }
  177. OS << nl;
  178. }
  179. }
  180. LLVM_ATTRIBUTE_USED void dump() const {
  181. dump(llvm::errs(), "\n");
  182. }
  183. };
  184. } // end anonymous namespace
  185. typedef const RegionBindingsRef& RegionBindingsConstRef;
  186. Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const {
  187. if (const SVal *V = lookup(R, BindingKey::Direct))
  188. return *V;
  189. return Optional<SVal>();
  190. }
  191. Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const {
  192. if (R->isBoundable())
  193. if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R))
  194. if (TR->getValueType()->isUnionType())
  195. return UnknownVal();
  196. if (const SVal *V = lookup(R, BindingKey::Default))
  197. return *V;
  198. return Optional<SVal>();
  199. }
  200. RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const {
  201. const MemRegion *Base = K.getBaseRegion();
  202. const ClusterBindings *ExistingCluster = lookup(Base);
  203. ClusterBindings Cluster = (ExistingCluster ? *ExistingCluster
  204. : CBFactory.getEmptyMap());
  205. ClusterBindings NewCluster = CBFactory.add(Cluster, K, V);
  206. return add(Base, NewCluster);
  207. }
  208. RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R,
  209. BindingKey::Kind k,
  210. SVal V) const {
  211. return addBinding(BindingKey::Make(R, k), V);
  212. }
  213. const SVal *RegionBindingsRef::lookup(BindingKey K) const {
  214. const ClusterBindings *Cluster = lookup(K.getBaseRegion());
  215. if (!Cluster)
  216. return 0;
  217. return Cluster->lookup(K);
  218. }
  219. const SVal *RegionBindingsRef::lookup(const MemRegion *R,
  220. BindingKey::Kind k) const {
  221. return lookup(BindingKey::Make(R, k));
  222. }
  223. RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) {
  224. const MemRegion *Base = K.getBaseRegion();
  225. const ClusterBindings *Cluster = lookup(Base);
  226. if (!Cluster)
  227. return *this;
  228. ClusterBindings NewCluster = CBFactory.remove(*Cluster, K);
  229. if (NewCluster.isEmpty())
  230. return remove(Base);
  231. return add(Base, NewCluster);
  232. }
  233. RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R,
  234. BindingKey::Kind k){
  235. return removeBinding(BindingKey::Make(R, k));
  236. }
  237. //===----------------------------------------------------------------------===//
  238. // Fine-grained control of RegionStoreManager.
  239. //===----------------------------------------------------------------------===//
  240. namespace {
  241. struct minimal_features_tag {};
  242. struct maximal_features_tag {};
  243. class RegionStoreFeatures {
  244. bool SupportsFields;
  245. public:
  246. RegionStoreFeatures(minimal_features_tag) :
  247. SupportsFields(false) {}
  248. RegionStoreFeatures(maximal_features_tag) :
  249. SupportsFields(true) {}
  250. void enableFields(bool t) { SupportsFields = t; }
  251. bool supportsFields() const { return SupportsFields; }
  252. };
  253. }
  254. //===----------------------------------------------------------------------===//
  255. // Main RegionStore logic.
  256. //===----------------------------------------------------------------------===//
  257. namespace {
  258. class RegionStoreManager : public StoreManager {
  259. public:
  260. const RegionStoreFeatures Features;
  261. RegionBindings::Factory RBFactory;
  262. mutable ClusterBindings::Factory CBFactory;
  263. typedef std::vector<SVal> SValListTy;
  264. private:
  265. typedef llvm::DenseMap<const LazyCompoundValData *,
  266. SValListTy> LazyBindingsMapTy;
  267. LazyBindingsMapTy LazyBindingsMap;
  268. public:
  269. RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f)
  270. : StoreManager(mgr), Features(f),
  271. RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()) {}
  272. /// setImplicitDefaultValue - Set the default binding for the provided
  273. /// MemRegion to the value implicitly defined for compound literals when
  274. /// the value is not specified.
  275. RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B,
  276. const MemRegion *R, QualType T);
  277. /// ArrayToPointer - Emulates the "decay" of an array to a pointer
  278. /// type. 'Array' represents the lvalue of the array being decayed
  279. /// to a pointer, and the returned SVal represents the decayed
  280. /// version of that lvalue (i.e., a pointer to the first element of
  281. /// the array). This is called by ExprEngine when evaluating
  282. /// casts from arrays to pointers.
  283. SVal ArrayToPointer(Loc Array);
  284. StoreRef getInitialStore(const LocationContext *InitLoc) {
  285. return StoreRef(RBFactory.getEmptyMap().getRootWithoutRetain(), *this);
  286. }
  287. //===-------------------------------------------------------------------===//
  288. // Binding values to regions.
  289. //===-------------------------------------------------------------------===//
  290. RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K,
  291. const Expr *Ex,
  292. unsigned Count,
  293. const LocationContext *LCtx,
  294. RegionBindingsRef B,
  295. InvalidatedRegions *Invalidated);
  296. StoreRef invalidateRegions(Store store, ArrayRef<const MemRegion *> Regions,
  297. const Expr *E, unsigned Count,
  298. const LocationContext *LCtx,
  299. InvalidatedSymbols &IS,
  300. const CallEvent *Call,
  301. InvalidatedRegions *Invalidated);
  302. bool scanReachableSymbols(Store S, const MemRegion *R,
  303. ScanReachableSymbols &Callbacks);
  304. RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B,
  305. const SubRegion *R);
  306. public: // Part of public interface to class.
  307. virtual StoreRef Bind(Store store, Loc LV, SVal V) {
  308. return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this);
  309. }
  310. RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V);
  311. // BindDefault is only used to initialize a region with a default value.
  312. StoreRef BindDefault(Store store, const MemRegion *R, SVal V) {
  313. RegionBindingsRef B = getRegionBindings(store);
  314. assert(!B.lookup(R, BindingKey::Default));
  315. assert(!B.lookup(R, BindingKey::Direct));
  316. return StoreRef(B.addBinding(R, BindingKey::Default, V)
  317. .asImmutableMap()
  318. .getRootWithoutRetain(), *this);
  319. }
  320. /// \brief Create a new store that binds a value to a compound literal.
  321. ///
  322. /// \param ST The original store whose bindings are the basis for the new
  323. /// store.
  324. ///
  325. /// \param CL The compound literal to bind (the binding key).
  326. ///
  327. /// \param LC The LocationContext for the binding.
  328. ///
  329. /// \param V The value to bind to the compound literal.
  330. StoreRef bindCompoundLiteral(Store ST,
  331. const CompoundLiteralExpr *CL,
  332. const LocationContext *LC, SVal V);
  333. /// BindStruct - Bind a compound value to a structure.
  334. RegionBindingsRef bindStruct(RegionBindingsConstRef B,
  335. const TypedValueRegion* R, SVal V);
  336. /// BindVector - Bind a compound value to a vector.
  337. RegionBindingsRef bindVector(RegionBindingsConstRef B,
  338. const TypedValueRegion* R, SVal V);
  339. RegionBindingsRef bindArray(RegionBindingsConstRef B,
  340. const TypedValueRegion* R,
  341. SVal V);
  342. /// Clears out all bindings in the given region and assigns a new value
  343. /// as a Default binding.
  344. RegionBindingsRef bindAggregate(RegionBindingsConstRef B,
  345. const TypedRegion *R,
  346. SVal DefaultVal);
  347. /// \brief Create a new store with the specified binding removed.
  348. /// \param ST the original store, that is the basis for the new store.
  349. /// \param L the location whose binding should be removed.
  350. virtual StoreRef killBinding(Store ST, Loc L);
  351. void incrementReferenceCount(Store store) {
  352. getRegionBindings(store).manualRetain();
  353. }
  354. /// If the StoreManager supports it, decrement the reference count of
  355. /// the specified Store object. If the reference count hits 0, the memory
  356. /// associated with the object is recycled.
  357. void decrementReferenceCount(Store store) {
  358. getRegionBindings(store).manualRelease();
  359. }
  360. bool includedInBindings(Store store, const MemRegion *region) const;
  361. /// \brief Return the value bound to specified location in a given state.
  362. ///
  363. /// The high level logic for this method is this:
  364. /// getBinding (L)
  365. /// if L has binding
  366. /// return L's binding
  367. /// else if L is in killset
  368. /// return unknown
  369. /// else
  370. /// if L is on stack or heap
  371. /// return undefined
  372. /// else
  373. /// return symbolic
  374. virtual SVal getBinding(Store S, Loc L, QualType T) {
  375. return getBinding(getRegionBindings(S), L, T);
  376. }
  377. SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType());
  378. SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R);
  379. SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R);
  380. SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R);
  381. SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R);
  382. SVal getBindingForLazySymbol(const TypedValueRegion *R);
  383. SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
  384. const TypedValueRegion *R,
  385. QualType Ty,
  386. const MemRegion *superR);
  387. SVal getLazyBinding(const MemRegion *LazyBindingRegion,
  388. RegionBindingsRef LazyBinding);
  389. /// Get bindings for the values in a struct and return a CompoundVal, used
  390. /// when doing struct copy:
  391. /// struct s x, y;
  392. /// x = y;
  393. /// y's value is retrieved by this method.
  394. SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R);
  395. SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R);
  396. NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R);
  397. /// Used to lazily generate derived symbols for bindings that are defined
  398. /// implicitly by default bindings in a super region.
  399. Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
  400. const MemRegion *superR,
  401. const TypedValueRegion *R,
  402. QualType Ty);
  403. /// Get the state and region whose binding this region \p R corresponds to.
  404. ///
  405. /// If there is no lazy binding for \p R, the returned value will have a null
  406. /// \c second. Note that a null pointer can represents a valid Store.
  407. std::pair<Store, const MemRegion *>
  408. getLazyBinding(RegionBindingsConstRef B, const MemRegion *R,
  409. const MemRegion *originalRegion);
  410. /// Returns the cached set of interesting SVals contained within a lazy
  411. /// binding.
  412. ///
  413. /// The precise value of "interesting" is determined for the purposes of
  414. /// RegionStore's internal analysis. It must always contain all regions and
  415. /// symbols, but may omit constants and other kinds of SVal.
  416. const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV);
  417. //===------------------------------------------------------------------===//
  418. // State pruning.
  419. //===------------------------------------------------------------------===//
  420. /// removeDeadBindings - Scans the RegionStore of 'state' for dead values.
  421. /// It returns a new Store with these values removed.
  422. StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
  423. SymbolReaper& SymReaper);
  424. //===------------------------------------------------------------------===//
  425. // Region "extents".
  426. //===------------------------------------------------------------------===//
  427. // FIXME: This method will soon be eliminated; see the note in Store.h.
  428. DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state,
  429. const MemRegion* R, QualType EleTy);
  430. //===------------------------------------------------------------------===//
  431. // Utility methods.
  432. //===------------------------------------------------------------------===//
  433. RegionBindingsRef getRegionBindings(Store store) const {
  434. return RegionBindingsRef(CBFactory,
  435. static_cast<const RegionBindings::TreeTy*>(store),
  436. RBFactory.getTreeFactory());
  437. }
  438. void print(Store store, raw_ostream &Out, const char* nl,
  439. const char *sep);
  440. void iterBindings(Store store, BindingsHandler& f) {
  441. RegionBindingsRef B = getRegionBindings(store);
  442. for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
  443. const ClusterBindings &Cluster = I.getData();
  444. for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
  445. CI != CE; ++CI) {
  446. const BindingKey &K = CI.getKey();
  447. if (!K.isDirect())
  448. continue;
  449. if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) {
  450. // FIXME: Possibly incorporate the offset?
  451. if (!f.HandleBinding(*this, store, R, CI.getData()))
  452. return;
  453. }
  454. }
  455. }
  456. }
  457. };
  458. } // end anonymous namespace
  459. //===----------------------------------------------------------------------===//
  460. // RegionStore creation.
  461. //===----------------------------------------------------------------------===//
  462. StoreManager *ento::CreateRegionStoreManager(ProgramStateManager& StMgr) {
  463. RegionStoreFeatures F = maximal_features_tag();
  464. return new RegionStoreManager(StMgr, F);
  465. }
  466. StoreManager *
  467. ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) {
  468. RegionStoreFeatures F = minimal_features_tag();
  469. F.enableFields(true);
  470. return new RegionStoreManager(StMgr, F);
  471. }
  472. //===----------------------------------------------------------------------===//
  473. // Region Cluster analysis.
  474. //===----------------------------------------------------------------------===//
  475. namespace {
  476. template <typename DERIVED>
  477. class ClusterAnalysis {
  478. protected:
  479. typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap;
  480. typedef SmallVector<const MemRegion *, 10> WorkList;
  481. llvm::SmallPtrSet<const ClusterBindings *, 16> Visited;
  482. WorkList WL;
  483. RegionStoreManager &RM;
  484. ASTContext &Ctx;
  485. SValBuilder &svalBuilder;
  486. RegionBindingsRef B;
  487. const bool includeGlobals;
  488. const ClusterBindings *getCluster(const MemRegion *R) {
  489. return B.lookup(R);
  490. }
  491. public:
  492. ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr,
  493. RegionBindingsRef b, const bool includeGlobals)
  494. : RM(rm), Ctx(StateMgr.getContext()),
  495. svalBuilder(StateMgr.getSValBuilder()),
  496. B(b), includeGlobals(includeGlobals) {}
  497. RegionBindingsRef getRegionBindings() const { return B; }
  498. bool isVisited(const MemRegion *R) {
  499. return Visited.count(getCluster(R));
  500. }
  501. void GenerateClusters() {
  502. // Scan the entire set of bindings and record the region clusters.
  503. for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end();
  504. RI != RE; ++RI){
  505. const MemRegion *Base = RI.getKey();
  506. const ClusterBindings &Cluster = RI.getData();
  507. assert(!Cluster.isEmpty() && "Empty clusters should be removed");
  508. static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster);
  509. if (includeGlobals)
  510. if (isa<NonStaticGlobalSpaceRegion>(Base->getMemorySpace()))
  511. AddToWorkList(Base, &Cluster);
  512. }
  513. }
  514. bool AddToWorkList(const MemRegion *R, const ClusterBindings *C) {
  515. if (C && !Visited.insert(C))
  516. return false;
  517. WL.push_back(R);
  518. return true;
  519. }
  520. bool AddToWorkList(const MemRegion *R) {
  521. const MemRegion *baseR = R->getBaseRegion();
  522. return AddToWorkList(baseR, getCluster(baseR));
  523. }
  524. void RunWorkList() {
  525. while (!WL.empty()) {
  526. const MemRegion *baseR = WL.pop_back_val();
  527. // First visit the cluster.
  528. if (const ClusterBindings *Cluster = getCluster(baseR))
  529. static_cast<DERIVED*>(this)->VisitCluster(baseR, *Cluster);
  530. // Next, visit the base region.
  531. static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
  532. }
  533. }
  534. public:
  535. void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {}
  536. void VisitCluster(const MemRegion *baseR, const ClusterBindings &C) {}
  537. void VisitBaseRegion(const MemRegion *baseR) {}
  538. };
  539. }
  540. //===----------------------------------------------------------------------===//
  541. // Binding invalidation.
  542. //===----------------------------------------------------------------------===//
  543. bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R,
  544. ScanReachableSymbols &Callbacks) {
  545. assert(R == R->getBaseRegion() && "Should only be called for base regions");
  546. RegionBindingsRef B = getRegionBindings(S);
  547. const ClusterBindings *Cluster = B.lookup(R);
  548. if (!Cluster)
  549. return true;
  550. for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end();
  551. RI != RE; ++RI) {
  552. if (!Callbacks.scan(RI.getData()))
  553. return false;
  554. }
  555. return true;
  556. }
  557. static inline bool isUnionField(const FieldRegion *FR) {
  558. return FR->getDecl()->getParent()->isUnion();
  559. }
  560. typedef SmallVector<const FieldDecl *, 8> FieldVector;
  561. void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) {
  562. assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
  563. const MemRegion *Base = K.getConcreteOffsetRegion();
  564. const MemRegion *R = K.getRegion();
  565. while (R != Base) {
  566. if (const FieldRegion *FR = dyn_cast<FieldRegion>(R))
  567. if (!isUnionField(FR))
  568. Fields.push_back(FR->getDecl());
  569. R = cast<SubRegion>(R)->getSuperRegion();
  570. }
  571. }
  572. static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) {
  573. assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
  574. if (Fields.empty())
  575. return true;
  576. FieldVector FieldsInBindingKey;
  577. getSymbolicOffsetFields(K, FieldsInBindingKey);
  578. ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size();
  579. if (Delta >= 0)
  580. return std::equal(FieldsInBindingKey.begin() + Delta,
  581. FieldsInBindingKey.end(),
  582. Fields.begin());
  583. else
  584. return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(),
  585. Fields.begin() - Delta);
  586. }
  587. /// Collects all keys in \p Cluster that may refer to bindings within \p Top.
  588. ///
  589. /// The \p IncludeAllDefaultBindings parameter specifies whether to include
  590. /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is
  591. /// an aggregate within a larger aggregate with a default binding.
  592. static void collectSubRegionKeys(SmallVectorImpl<BindingKey> &Keys,
  593. SValBuilder &SVB,
  594. const ClusterBindings &Cluster,
  595. const SubRegion *Top, BindingKey TopKey,
  596. bool IncludeAllDefaultBindings) {
  597. FieldVector FieldsInSymbolicSubregions;
  598. if (TopKey.hasSymbolicOffset()) {
  599. getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions);
  600. Top = cast<SubRegion>(TopKey.getConcreteOffsetRegion());
  601. TopKey = BindingKey::Make(Top, BindingKey::Default);
  602. }
  603. // This assumes the region being invalidated is char-aligned. This isn't
  604. // true for bitfields, but since bitfields have no subregions they shouldn't
  605. // be using this function anyway.
  606. uint64_t Length = UINT64_MAX;
  607. SVal Extent = Top->getExtent(SVB);
  608. if (Optional<nonloc::ConcreteInt> ExtentCI =
  609. Extent.getAs<nonloc::ConcreteInt>()) {
  610. const llvm::APSInt &ExtentInt = ExtentCI->getValue();
  611. assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned());
  612. // Extents are in bytes but region offsets are in bits. Be careful!
  613. Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth();
  614. }
  615. for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end();
  616. I != E; ++I) {
  617. BindingKey NextKey = I.getKey();
  618. if (NextKey.getRegion() == TopKey.getRegion()) {
  619. // FIXME: This doesn't catch the case where we're really invalidating a
  620. // region with a symbolic offset. Example:
  621. // R: points[i].y
  622. // Next: points[0].x
  623. if (NextKey.getOffset() > TopKey.getOffset() &&
  624. NextKey.getOffset() - TopKey.getOffset() < Length) {
  625. // Case 1: The next binding is inside the region we're invalidating.
  626. // Remove it.
  627. Keys.push_back(NextKey);
  628. } else if (NextKey.getOffset() == TopKey.getOffset()) {
  629. // Case 2: The next binding is at the same offset as the region we're
  630. // invalidating. In this case, we need to leave default bindings alone,
  631. // since they may be providing a default value for a regions beyond what
  632. // we're invalidating.
  633. // FIXME: This is probably incorrect; consider invalidating an outer
  634. // struct whose first field is bound to a LazyCompoundVal.
  635. if (IncludeAllDefaultBindings || NextKey.isDirect())
  636. Keys.push_back(NextKey);
  637. }
  638. } else if (NextKey.hasSymbolicOffset()) {
  639. const MemRegion *Base = NextKey.getConcreteOffsetRegion();
  640. if (Top->isSubRegionOf(Base)) {
  641. // Case 3: The next key is symbolic and we just changed something within
  642. // its concrete region. We don't know if the binding is still valid, so
  643. // we'll be conservative and remove it.
  644. if (IncludeAllDefaultBindings || NextKey.isDirect())
  645. if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
  646. Keys.push_back(NextKey);
  647. } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) {
  648. // Case 4: The next key is symbolic, but we changed a known
  649. // super-region. In this case the binding is certainly no longer valid.
  650. if (Top == Base || BaseSR->isSubRegionOf(Top))
  651. if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
  652. Keys.push_back(NextKey);
  653. }
  654. }
  655. }
  656. }
  657. static void collectSubRegionKeys(SmallVectorImpl<BindingKey> &Keys,
  658. SValBuilder &SVB,
  659. const ClusterBindings &Cluster,
  660. const SubRegion *Top,
  661. bool IncludeAllDefaultBindings) {
  662. collectSubRegionKeys(Keys, SVB, Cluster, Top,
  663. BindingKey::Make(Top, BindingKey::Default),
  664. IncludeAllDefaultBindings);
  665. }
  666. RegionBindingsRef
  667. RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B,
  668. const SubRegion *Top) {
  669. BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default);
  670. const MemRegion *ClusterHead = TopKey.getBaseRegion();
  671. if (Top == ClusterHead) {
  672. // We can remove an entire cluster's bindings all in one go.
  673. return B.remove(Top);
  674. }
  675. const ClusterBindings *Cluster = B.lookup(ClusterHead);
  676. if (!Cluster)
  677. return B;
  678. SmallVector<BindingKey, 32> Keys;
  679. collectSubRegionKeys(Keys, svalBuilder, *Cluster, Top, TopKey,
  680. /*IncludeAllDefaultBindings=*/false);
  681. ClusterBindingsRef Result(*Cluster, CBFactory);
  682. for (SmallVectorImpl<BindingKey>::const_iterator I = Keys.begin(),
  683. E = Keys.end();
  684. I != E; ++I)
  685. Result = Result.remove(*I);
  686. // If we're invalidating a region with a symbolic offset, we need to make sure
  687. // we don't treat the base region as uninitialized anymore.
  688. // FIXME: This isn't very precise; see the example in the loop.
  689. if (TopKey.hasSymbolicOffset()) {
  690. const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
  691. Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default),
  692. UnknownVal());
  693. }
  694. if (Result.isEmpty())
  695. return B.remove(ClusterHead);
  696. return B.add(ClusterHead, Result.asImmutableMap());
  697. }
  698. namespace {
  699. class invalidateRegionsWorker : public ClusterAnalysis<invalidateRegionsWorker>
  700. {
  701. const Expr *Ex;
  702. unsigned Count;
  703. const LocationContext *LCtx;
  704. InvalidatedSymbols &IS;
  705. StoreManager::InvalidatedRegions *Regions;
  706. public:
  707. invalidateRegionsWorker(RegionStoreManager &rm,
  708. ProgramStateManager &stateMgr,
  709. RegionBindingsRef b,
  710. const Expr *ex, unsigned count,
  711. const LocationContext *lctx,
  712. InvalidatedSymbols &is,
  713. StoreManager::InvalidatedRegions *r,
  714. bool includeGlobals)
  715. : ClusterAnalysis<invalidateRegionsWorker>(rm, stateMgr, b, includeGlobals),
  716. Ex(ex), Count(count), LCtx(lctx), IS(is), Regions(r) {}
  717. void VisitCluster(const MemRegion *baseR, const ClusterBindings &C);
  718. void VisitBaseRegion(const MemRegion *baseR);
  719. private:
  720. void VisitBinding(SVal V);
  721. };
  722. }
  723. void invalidateRegionsWorker::VisitBinding(SVal V) {
  724. // A symbol? Mark it touched by the invalidation.
  725. if (SymbolRef Sym = V.getAsSymbol())
  726. IS.insert(Sym);
  727. if (const MemRegion *R = V.getAsRegion()) {
  728. AddToWorkList(R);
  729. return;
  730. }
  731. // Is it a LazyCompoundVal? All references get invalidated as well.
  732. if (Optional<nonloc::LazyCompoundVal> LCS =
  733. V.getAs<nonloc::LazyCompoundVal>()) {
  734. const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
  735. for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
  736. E = Vals.end();
  737. I != E; ++I)
  738. VisitBinding(*I);
  739. return;
  740. }
  741. }
  742. void invalidateRegionsWorker::VisitCluster(const MemRegion *BaseR,
  743. const ClusterBindings &C) {
  744. for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I)
  745. VisitBinding(I.getData());
  746. B = B.remove(BaseR);
  747. }
  748. void invalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
  749. // Symbolic region? Mark that symbol touched by the invalidation.
  750. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
  751. IS.insert(SR->getSymbol());
  752. // BlockDataRegion? If so, invalidate captured variables that are passed
  753. // by reference.
  754. if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
  755. for (BlockDataRegion::referenced_vars_iterator
  756. BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
  757. BI != BE; ++BI) {
  758. const VarRegion *VR = BI.getCapturedRegion();
  759. const VarDecl *VD = VR->getDecl();
  760. if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) {
  761. AddToWorkList(VR);
  762. }
  763. else if (Loc::isLocType(VR->getValueType())) {
  764. // Map the current bindings to a Store to retrieve the value
  765. // of the binding. If that binding itself is a region, we should
  766. // invalidate that region. This is because a block may capture
  767. // a pointer value, but the thing pointed by that pointer may
  768. // get invalidated.
  769. SVal V = RM.getBinding(B, loc::MemRegionVal(VR));
  770. if (Optional<Loc> L = V.getAs<Loc>()) {
  771. if (const MemRegion *LR = L->getAsRegion())
  772. AddToWorkList(LR);
  773. }
  774. }
  775. }
  776. return;
  777. }
  778. // Otherwise, we have a normal data region. Record that we touched the region.
  779. if (Regions)
  780. Regions->push_back(baseR);
  781. if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
  782. // Invalidate the region by setting its default value to
  783. // conjured symbol. The type of the symbol is irrelavant.
  784. DefinedOrUnknownSVal V =
  785. svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count);
  786. B = B.addBinding(baseR, BindingKey::Default, V);
  787. return;
  788. }
  789. if (!baseR->isBoundable())
  790. return;
  791. const TypedValueRegion *TR = cast<TypedValueRegion>(baseR);
  792. QualType T = TR->getValueType();
  793. // Invalidate the binding.
  794. if (T->isStructureOrClassType()) {
  795. // Invalidate the region by setting its default value to
  796. // conjured symbol. The type of the symbol is irrelavant.
  797. DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
  798. Ctx.IntTy, Count);
  799. B = B.addBinding(baseR, BindingKey::Default, V);
  800. return;
  801. }
  802. if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
  803. // Set the default value of the array to conjured symbol.
  804. DefinedOrUnknownSVal V =
  805. svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
  806. AT->getElementType(), Count);
  807. B = B.addBinding(baseR, BindingKey::Default, V);
  808. return;
  809. }
  810. if (includeGlobals &&
  811. isa<NonStaticGlobalSpaceRegion>(baseR->getMemorySpace())) {
  812. // If the region is a global and we are invalidating all globals,
  813. // just erase the entry. This causes all globals to be lazily
  814. // symbolicated from the same base symbol.
  815. B = B.removeBinding(baseR);
  816. return;
  817. }
  818. DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
  819. T,Count);
  820. assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
  821. B = B.addBinding(baseR, BindingKey::Direct, V);
  822. }
  823. RegionBindingsRef
  824. RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K,
  825. const Expr *Ex,
  826. unsigned Count,
  827. const LocationContext *LCtx,
  828. RegionBindingsRef B,
  829. InvalidatedRegions *Invalidated) {
  830. // Bind the globals memory space to a new symbol that we will use to derive
  831. // the bindings for all globals.
  832. const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K);
  833. SVal V = svalBuilder.conjureSymbolVal(/* SymbolTag = */ (const void*) GS, Ex, LCtx,
  834. /* type does not matter */ Ctx.IntTy,
  835. Count);
  836. B = B.removeBinding(GS)
  837. .addBinding(BindingKey::Make(GS, BindingKey::Default), V);
  838. // Even if there are no bindings in the global scope, we still need to
  839. // record that we touched it.
  840. if (Invalidated)
  841. Invalidated->push_back(GS);
  842. return B;
  843. }
  844. StoreRef
  845. RegionStoreManager::invalidateRegions(Store store,
  846. ArrayRef<const MemRegion *> Regions,
  847. const Expr *Ex, unsigned Count,
  848. const LocationContext *LCtx,
  849. InvalidatedSymbols &IS,
  850. const CallEvent *Call,
  851. InvalidatedRegions *Invalidated) {
  852. invalidateRegionsWorker W(*this, StateMgr,
  853. RegionStoreManager::getRegionBindings(store),
  854. Ex, Count, LCtx, IS, Invalidated, false);
  855. // Scan the bindings and generate the clusters.
  856. W.GenerateClusters();
  857. // Add the regions to the worklist.
  858. for (ArrayRef<const MemRegion *>::iterator
  859. I = Regions.begin(), E = Regions.end(); I != E; ++I)
  860. W.AddToWorkList(*I);
  861. W.RunWorkList();
  862. // Return the new bindings.
  863. RegionBindingsRef B = W.getRegionBindings();
  864. // For all globals which are not static nor immutable: determine which global
  865. // regions should be invalidated and invalidate them.
  866. // TODO: This could possibly be more precise with modules.
  867. //
  868. // System calls invalidate only system globals.
  869. if (Call && Call->isInSystemHeader()) {
  870. B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
  871. Ex, Count, LCtx, B, Invalidated);
  872. // Internal calls might invalidate both system and internal globals.
  873. } else {
  874. B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
  875. Ex, Count, LCtx, B, Invalidated);
  876. B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind,
  877. Ex, Count, LCtx, B, Invalidated);
  878. }
  879. return StoreRef(B.asStore(), *this);
  880. }
  881. //===----------------------------------------------------------------------===//
  882. // Extents for regions.
  883. //===----------------------------------------------------------------------===//
  884. DefinedOrUnknownSVal
  885. RegionStoreManager::getSizeInElements(ProgramStateRef state,
  886. const MemRegion *R,
  887. QualType EleTy) {
  888. SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder);
  889. const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size);
  890. if (!SizeInt)
  891. return UnknownVal();
  892. CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
  893. if (Ctx.getAsVariableArrayType(EleTy)) {
  894. // FIXME: We need to track extra state to properly record the size
  895. // of VLAs. Returning UnknownVal here, however, is a stop-gap so that
  896. // we don't have a divide-by-zero below.
  897. return UnknownVal();
  898. }
  899. CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
  900. // If a variable is reinterpreted as a type that doesn't fit into a larger
  901. // type evenly, round it down.
  902. // This is a signed value, since it's used in arithmetic with signed indices.
  903. return svalBuilder.makeIntVal(RegionSize / EleSize, false);
  904. }
  905. //===----------------------------------------------------------------------===//
  906. // Location and region casting.
  907. //===----------------------------------------------------------------------===//
  908. /// ArrayToPointer - Emulates the "decay" of an array to a pointer
  909. /// type. 'Array' represents the lvalue of the array being decayed
  910. /// to a pointer, and the returned SVal represents the decayed
  911. /// version of that lvalue (i.e., a pointer to the first element of
  912. /// the array). This is called by ExprEngine when evaluating casts
  913. /// from arrays to pointers.
  914. SVal RegionStoreManager::ArrayToPointer(Loc Array) {
  915. if (!Array.getAs<loc::MemRegionVal>())
  916. return UnknownVal();
  917. const MemRegion* R = Array.castAs<loc::MemRegionVal>().getRegion();
  918. const TypedValueRegion* ArrayR = dyn_cast<TypedValueRegion>(R);
  919. if (!ArrayR)
  920. return UnknownVal();
  921. // Strip off typedefs from the ArrayRegion's ValueType.
  922. QualType T = ArrayR->getValueType().getDesugaredType(Ctx);
  923. const ArrayType *AT = cast<ArrayType>(T);
  924. T = AT->getElementType();
  925. NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex();
  926. return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
  927. }
  928. //===----------------------------------------------------------------------===//
  929. // Loading values from regions.
  930. //===----------------------------------------------------------------------===//
  931. SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) {
  932. assert(!L.getAs<UnknownVal>() && "location unknown");
  933. assert(!L.getAs<UndefinedVal>() && "location undefined");
  934. // For access to concrete addresses, return UnknownVal. Checks
  935. // for null dereferences (and similar errors) are done by checkers, not
  936. // the Store.
  937. // FIXME: We can consider lazily symbolicating such memory, but we really
  938. // should defer this when we can reason easily about symbolicating arrays
  939. // of bytes.
  940. if (L.getAs<loc::ConcreteInt>()) {
  941. return UnknownVal();
  942. }
  943. if (!L.getAs<loc::MemRegionVal>()) {
  944. return UnknownVal();
  945. }
  946. const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion();
  947. if (isa<AllocaRegion>(MR) ||
  948. isa<SymbolicRegion>(MR) ||
  949. isa<CodeTextRegion>(MR)) {
  950. if (T.isNull()) {
  951. if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
  952. T = TR->getLocationType();
  953. else {
  954. const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
  955. T = SR->getSymbol()->getType();
  956. }
  957. }
  958. MR = GetElementZeroRegion(MR, T);
  959. }
  960. // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
  961. // instead of 'Loc', and have the other Loc cases handled at a higher level.
  962. const TypedValueRegion *R = cast<TypedValueRegion>(MR);
  963. QualType RTy = R->getValueType();
  964. // FIXME: we do not yet model the parts of a complex type, so treat the
  965. // whole thing as "unknown".
  966. if (RTy->isAnyComplexType())
  967. return UnknownVal();
  968. // FIXME: We should eventually handle funny addressing. e.g.:
  969. //
  970. // int x = ...;
  971. // int *p = &x;
  972. // char *q = (char*) p;
  973. // char c = *q; // returns the first byte of 'x'.
  974. //
  975. // Such funny addressing will occur due to layering of regions.
  976. if (RTy->isStructureOrClassType())
  977. return getBindingForStruct(B, R);
  978. // FIXME: Handle unions.
  979. if (RTy->isUnionType())
  980. return UnknownVal();
  981. if (RTy->isArrayType()) {
  982. if (RTy->isConstantArrayType())
  983. return getBindingForArray(B, R);
  984. else
  985. return UnknownVal();
  986. }
  987. // FIXME: handle Vector types.
  988. if (RTy->isVectorType())
  989. return UnknownVal();
  990. if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
  991. return CastRetrievedVal(getBindingForField(B, FR), FR, T, false);
  992. if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
  993. // FIXME: Here we actually perform an implicit conversion from the loaded
  994. // value to the element type. Eventually we want to compose these values
  995. // more intelligently. For example, an 'element' can encompass multiple
  996. // bound regions (e.g., several bound bytes), or could be a subset of
  997. // a larger value.
  998. return CastRetrievedVal(getBindingForElement(B, ER), ER, T, false);
  999. }
  1000. if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
  1001. // FIXME: Here we actually perform an implicit conversion from the loaded
  1002. // value to the ivar type. What we should model is stores to ivars
  1003. // that blow past the extent of the ivar. If the address of the ivar is
  1004. // reinterpretted, it is possible we stored a different value that could
  1005. // fit within the ivar. Either we need to cast these when storing them
  1006. // or reinterpret them lazily (as we do here).
  1007. return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T, false);
  1008. }
  1009. if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
  1010. // FIXME: Here we actually perform an implicit conversion from the loaded
  1011. // value to the variable type. What we should model is stores to variables
  1012. // that blow past the extent of the variable. If the address of the
  1013. // variable is reinterpretted, it is possible we stored a different value
  1014. // that could fit within the variable. Either we need to cast these when
  1015. // storing them or reinterpret them lazily (as we do here).
  1016. return CastRetrievedVal(getBindingForVar(B, VR), VR, T, false);
  1017. }
  1018. const SVal *V = B.lookup(R, BindingKey::Direct);
  1019. // Check if the region has a binding.
  1020. if (V)
  1021. return *V;
  1022. // The location does not have a bound value. This means that it has
  1023. // the value it had upon its creation and/or entry to the analyzed
  1024. // function/method. These are either symbolic values or 'undefined'.
  1025. if (R->hasStackNonParametersStorage()) {
  1026. // All stack variables are considered to have undefined values
  1027. // upon creation. All heap allocated blocks are considered to
  1028. // have undefined values as well unless they are explicitly bound
  1029. // to specific values.
  1030. return UndefinedVal();
  1031. }
  1032. // All other values are symbolic.
  1033. return svalBuilder.getRegionValueSymbolVal(R);
  1034. }
  1035. std::pair<Store, const MemRegion *>
  1036. RegionStoreManager::getLazyBinding(RegionBindingsConstRef B,
  1037. const MemRegion *R,
  1038. const MemRegion *originalRegion) {
  1039. typedef std::pair<Store, const MemRegion *> StoreRegionPair;
  1040. if (originalRegion != R) {
  1041. if (Optional<SVal> OV = B.getDefaultBinding(R)) {
  1042. if (Optional<nonloc::LazyCompoundVal> V =
  1043. OV->getAs<nonloc::LazyCompoundVal>())
  1044. return std::make_pair(V->getStore(), V->getRegion());
  1045. }
  1046. }
  1047. if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
  1048. StoreRegionPair X = getLazyBinding(B, ER->getSuperRegion(), originalRegion);
  1049. if (X.second)
  1050. return std::make_pair(X.first,
  1051. MRMgr.getElementRegionWithSuper(ER, X.second));
  1052. }
  1053. else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
  1054. StoreRegionPair X = getLazyBinding(B, FR->getSuperRegion(), originalRegion);
  1055. if (X.second) {
  1056. return std::make_pair(X.first,
  1057. MRMgr.getFieldRegionWithSuper(FR, X.second));
  1058. }
  1059. } else if (const CXXBaseObjectRegion *BaseReg =
  1060. dyn_cast<CXXBaseObjectRegion>(R)) {
  1061. // C++ base object region is another kind of region that we should blast
  1062. // through to look for lazy compound value. It is like a field region.
  1063. StoreRegionPair Result = getLazyBinding(B, BaseReg->getSuperRegion(),
  1064. originalRegion);
  1065. if (Result.second) {
  1066. // Make sure the types match up.
  1067. const CXXRecordDecl *LazyD = 0;
  1068. if (const TypedValueRegion *LazyR =
  1069. dyn_cast<TypedValueRegion>(Result.second)) {
  1070. LazyD = LazyR->getValueType()->getAsCXXRecordDecl();
  1071. if (LazyD)
  1072. LazyD = LazyD->getCanonicalDecl();
  1073. }
  1074. typedef SmallVector<const CXXBaseObjectRegion *, 4> BaseListTy;
  1075. BaseListTy BaseRegions;
  1076. do {
  1077. BaseRegions.push_back(BaseReg);
  1078. BaseReg = dyn_cast<CXXBaseObjectRegion>(BaseReg->getSuperRegion());
  1079. } while (BaseReg && LazyD != BaseReg->getDecl()->getCanonicalDecl());
  1080. // Layer the base regions on the result in least-to-most derived order.
  1081. for (BaseListTy::const_reverse_iterator I = BaseRegions.rbegin(),
  1082. E = BaseRegions.rend();
  1083. I != E; ++I) {
  1084. Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(*I,
  1085. Result.second);
  1086. }
  1087. }
  1088. return Result;
  1089. }
  1090. return StoreRegionPair();
  1091. }
  1092. SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
  1093. const ElementRegion* R) {
  1094. // We do not currently model bindings of the CompoundLiteralregion.
  1095. if (isa<CompoundLiteralRegion>(R->getBaseRegion()))
  1096. return UnknownVal();
  1097. // Check if the region has a binding.
  1098. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1099. return *V;
  1100. const MemRegion* superR = R->getSuperRegion();
  1101. // Check if the region is an element region of a string literal.
  1102. if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
  1103. // FIXME: Handle loads from strings where the literal is treated as
  1104. // an integer, e.g., *((unsigned int*)"hello")
  1105. QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
  1106. if (T != Ctx.getCanonicalType(R->getElementType()))
  1107. return UnknownVal();
  1108. const StringLiteral *Str = StrR->getStringLiteral();
  1109. SVal Idx = R->getIndex();
  1110. if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) {
  1111. int64_t i = CI->getValue().getSExtValue();
  1112. // Abort on string underrun. This can be possible by arbitrary
  1113. // clients of getBindingForElement().
  1114. if (i < 0)
  1115. return UndefinedVal();
  1116. int64_t length = Str->getLength();
  1117. // Technically, only i == length is guaranteed to be null.
  1118. // However, such overflows should be caught before reaching this point;
  1119. // the only time such an access would be made is if a string literal was
  1120. // used to initialize a larger array.
  1121. char c = (i >= length) ? '\0' : Str->getCodeUnit(i);
  1122. return svalBuilder.makeIntVal(c, T);
  1123. }
  1124. }
  1125. // Check for loads from a code text region. For such loads, just give up.
  1126. if (isa<CodeTextRegion>(superR))
  1127. return UnknownVal();
  1128. // Handle the case where we are indexing into a larger scalar object.
  1129. // For example, this handles:
  1130. // int x = ...
  1131. // char *y = &x;
  1132. // return *y;
  1133. // FIXME: This is a hack, and doesn't do anything really intelligent yet.
  1134. const RegionRawOffset &O = R->getAsArrayOffset();
  1135. // If we cannot reason about the offset, return an unknown value.
  1136. if (!O.getRegion())
  1137. return UnknownVal();
  1138. if (const TypedValueRegion *baseR =
  1139. dyn_cast_or_null<TypedValueRegion>(O.getRegion())) {
  1140. QualType baseT = baseR->getValueType();
  1141. if (baseT->isScalarType()) {
  1142. QualType elemT = R->getElementType();
  1143. if (elemT->isScalarType()) {
  1144. if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
  1145. if (const Optional<SVal> &V = B.getDirectBinding(superR)) {
  1146. if (SymbolRef parentSym = V->getAsSymbol())
  1147. return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
  1148. if (V->isUnknownOrUndef())
  1149. return *V;
  1150. // Other cases: give up. We are indexing into a larger object
  1151. // that has some value, but we don't know how to handle that yet.
  1152. return UnknownVal();
  1153. }
  1154. }
  1155. }
  1156. }
  1157. }
  1158. return getBindingForFieldOrElementCommon(B, R, R->getElementType(),
  1159. superR);
  1160. }
  1161. SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B,
  1162. const FieldRegion* R) {
  1163. // Check if the region has a binding.
  1164. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1165. return *V;
  1166. QualType Ty = R->getValueType();
  1167. return getBindingForFieldOrElementCommon(B, R, Ty, R->getSuperRegion());
  1168. }
  1169. Optional<SVal>
  1170. RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
  1171. const MemRegion *superR,
  1172. const TypedValueRegion *R,
  1173. QualType Ty) {
  1174. if (const Optional<SVal> &D = B.getDefaultBinding(superR)) {
  1175. const SVal &val = D.getValue();
  1176. if (SymbolRef parentSym = val.getAsSymbol())
  1177. return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
  1178. if (val.isZeroConstant())
  1179. return svalBuilder.makeZeroVal(Ty);
  1180. if (val.isUnknownOrUndef())
  1181. return val;
  1182. // Lazy bindings are handled later.
  1183. if (val.getAs<nonloc::LazyCompoundVal>())
  1184. return Optional<SVal>();
  1185. llvm_unreachable("Unknown default value");
  1186. }
  1187. return Optional<SVal>();
  1188. }
  1189. SVal RegionStoreManager::getLazyBinding(const MemRegion *LazyBindingRegion,
  1190. RegionBindingsRef LazyBinding) {
  1191. SVal Result;
  1192. if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion))
  1193. Result = getBindingForElement(LazyBinding, ER);
  1194. else
  1195. Result = getBindingForField(LazyBinding,
  1196. cast<FieldRegion>(LazyBindingRegion));
  1197. // This is a hack to deal with RegionStore's inability to distinguish a
  1198. // default value for /part/ of an aggregate from a default value for the
  1199. // /entire/ aggregate. The most common case of this is when struct Outer
  1200. // has as its first member a struct Inner, which is copied in from a stack
  1201. // variable. In this case, even if the Outer's default value is symbolic, 0,
  1202. // or unknown, it gets overridden by the Inner's default value of undefined.
  1203. //
  1204. // This is a general problem -- if the Inner is zero-initialized, the Outer
  1205. // will now look zero-initialized. The proper way to solve this is with a
  1206. // new version of RegionStore that tracks the extent of a binding as well
  1207. // as the offset.
  1208. //
  1209. // This hack only takes care of the undefined case because that can very
  1210. // quickly result in a warning.
  1211. if (Result.isUndef())
  1212. Result = UnknownVal();
  1213. return Result;
  1214. }
  1215. SVal
  1216. RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
  1217. const TypedValueRegion *R,
  1218. QualType Ty,
  1219. const MemRegion *superR) {
  1220. // At this point we have already checked in either getBindingForElement or
  1221. // getBindingForField if 'R' has a direct binding.
  1222. // Lazy binding?
  1223. Store lazyBindingStore = NULL;
  1224. const MemRegion *lazyBindingRegion = NULL;
  1225. llvm::tie(lazyBindingStore, lazyBindingRegion) = getLazyBinding(B, R, R);
  1226. if (lazyBindingRegion)
  1227. return getLazyBinding(lazyBindingRegion,
  1228. getRegionBindings(lazyBindingStore));
  1229. // Record whether or not we see a symbolic index. That can completely
  1230. // be out of scope of our lookup.
  1231. bool hasSymbolicIndex = false;
  1232. while (superR) {
  1233. if (const Optional<SVal> &D =
  1234. getBindingForDerivedDefaultValue(B, superR, R, Ty))
  1235. return *D;
  1236. if (const ElementRegion *ER = dyn_cast<ElementRegion>(superR)) {
  1237. NonLoc index = ER->getIndex();
  1238. if (!index.isConstant())
  1239. hasSymbolicIndex = true;
  1240. }
  1241. // If our super region is a field or element itself, walk up the region
  1242. // hierarchy to see if there is a default value installed in an ancestor.
  1243. if (const SubRegion *SR = dyn_cast<SubRegion>(superR)) {
  1244. superR = SR->getSuperRegion();
  1245. continue;
  1246. }
  1247. break;
  1248. }
  1249. if (R->hasStackNonParametersStorage()) {
  1250. if (isa<ElementRegion>(R)) {
  1251. // Currently we don't reason specially about Clang-style vectors. Check
  1252. // if superR is a vector and if so return Unknown.
  1253. if (const TypedValueRegion *typedSuperR =
  1254. dyn_cast<TypedValueRegion>(superR)) {
  1255. if (typedSuperR->getValueType()->isVectorType())
  1256. return UnknownVal();
  1257. }
  1258. }
  1259. // FIXME: We also need to take ElementRegions with symbolic indexes into
  1260. // account. This case handles both directly accessing an ElementRegion
  1261. // with a symbolic offset, but also fields within an element with
  1262. // a symbolic offset.
  1263. if (hasSymbolicIndex)
  1264. return UnknownVal();
  1265. return UndefinedVal();
  1266. }
  1267. // All other values are symbolic.
  1268. return svalBuilder.getRegionValueSymbolVal(R);
  1269. }
  1270. SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B,
  1271. const ObjCIvarRegion* R) {
  1272. // Check if the region has a binding.
  1273. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1274. return *V;
  1275. const MemRegion *superR = R->getSuperRegion();
  1276. // Check if the super region has a default binding.
  1277. if (const Optional<SVal> &V = B.getDefaultBinding(superR)) {
  1278. if (SymbolRef parentSym = V->getAsSymbol())
  1279. return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
  1280. // Other cases: give up.
  1281. return UnknownVal();
  1282. }
  1283. return getBindingForLazySymbol(R);
  1284. }
  1285. static Optional<SVal> getConstValue(SValBuilder &SVB, const VarDecl *VD) {
  1286. ASTContext &Ctx = SVB.getContext();
  1287. if (!VD->getType().isConstQualified())
  1288. return Optional<SVal>();
  1289. const Expr *Init = VD->getInit();
  1290. if (!Init)
  1291. return Optional<SVal>();
  1292. llvm::APSInt Result;
  1293. if (Init->EvaluateAsInt(Result, Ctx))
  1294. return SVB.makeIntVal(Result);
  1295. if (Init->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
  1296. return SVB.makeNull();
  1297. // FIXME: Handle other possible constant expressions.
  1298. return Optional<SVal>();
  1299. }
  1300. SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B,
  1301. const VarRegion *R) {
  1302. // Check if the region has a binding.
  1303. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1304. return *V;
  1305. // Lazily derive a value for the VarRegion.
  1306. const VarDecl *VD = R->getDecl();
  1307. const MemSpaceRegion *MS = R->getMemorySpace();
  1308. // Arguments are always symbolic.
  1309. if (isa<StackArgumentsSpaceRegion>(MS))
  1310. return svalBuilder.getRegionValueSymbolVal(R);
  1311. // Is 'VD' declared constant? If so, retrieve the constant value.
  1312. if (Optional<SVal> V = getConstValue(svalBuilder, VD))
  1313. return *V;
  1314. // This must come after the check for constants because closure-captured
  1315. // constant variables may appear in UnknownSpaceRegion.
  1316. if (isa<UnknownSpaceRegion>(MS))
  1317. return svalBuilder.getRegionValueSymbolVal(R);
  1318. if (isa<GlobalsSpaceRegion>(MS)) {
  1319. QualType T = VD->getType();
  1320. // Function-scoped static variables are default-initialized to 0; if they
  1321. // have an initializer, it would have been processed by now.
  1322. if (isa<StaticGlobalSpaceRegion>(MS))
  1323. return svalBuilder.makeZeroVal(T);
  1324. if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T))
  1325. return V.getValue();
  1326. return svalBuilder.getRegionValueSymbolVal(R);
  1327. }
  1328. return UndefinedVal();
  1329. }
  1330. SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) {
  1331. // All other values are symbolic.
  1332. return svalBuilder.getRegionValueSymbolVal(R);
  1333. }
  1334. const RegionStoreManager::SValListTy &
  1335. RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) {
  1336. // First, check the cache.
  1337. LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData());
  1338. if (I != LazyBindingsMap.end())
  1339. return I->second;
  1340. // If we don't have a list of values cached, start constructing it.
  1341. SValListTy List;
  1342. const SubRegion *LazyR = LCV.getRegion();
  1343. RegionBindingsRef B = getRegionBindings(LCV.getStore());
  1344. // If this region had /no/ bindings at the time, there are no interesting
  1345. // values to return.
  1346. const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion());
  1347. if (!Cluster)
  1348. return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List));
  1349. SmallVector<BindingKey, 32> Keys;
  1350. collectSubRegionKeys(Keys, svalBuilder, *Cluster, LazyR,
  1351. /*IncludeAllDefaultBindings=*/true);
  1352. for (SmallVectorImpl<BindingKey>::const_iterator I = Keys.begin(),
  1353. E = Keys.end();
  1354. I != E; ++I) {
  1355. SVal V = *Cluster->lookup(*I);
  1356. if (V.isUnknownOrUndef() || V.isConstant())
  1357. continue;
  1358. if (Optional<nonloc::LazyCompoundVal> InnerLCV =
  1359. V.getAs<nonloc::LazyCompoundVal>()) {
  1360. const SValListTy &InnerList = getInterestingValues(*InnerLCV);
  1361. List.insert(List.end(), InnerList.begin(), InnerList.end());
  1362. continue;
  1363. }
  1364. List.push_back(V);
  1365. }
  1366. return (LazyBindingsMap[LCV.getCVData()] = llvm_move(List));
  1367. }
  1368. NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B,
  1369. const TypedValueRegion *R) {
  1370. // If we already have a lazy binding, and it's for the whole structure,
  1371. // don't create a new lazy binding.
  1372. if (Optional<SVal> V = B.getDefaultBinding(R)) {
  1373. if (Optional<nonloc::LazyCompoundVal> LCV =
  1374. V->getAs<nonloc::LazyCompoundVal>()) {
  1375. QualType RegionTy = R->getValueType();
  1376. QualType SourceRegionTy = LCV->getRegion()->getValueType();
  1377. if (Ctx.hasSameUnqualifiedType(RegionTy, SourceRegionTy))
  1378. return *LCV;
  1379. }
  1380. }
  1381. return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R);
  1382. }
  1383. SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B,
  1384. const TypedValueRegion *R) {
  1385. const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl();
  1386. if (RD->field_empty())
  1387. return UnknownVal();
  1388. return createLazyBinding(B, R);
  1389. }
  1390. SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B,
  1391. const TypedValueRegion *R) {
  1392. assert(Ctx.getAsConstantArrayType(R->getValueType()) &&
  1393. "Only constant array types can have compound bindings.");
  1394. return createLazyBinding(B, R);
  1395. }
  1396. bool RegionStoreManager::includedInBindings(Store store,
  1397. const MemRegion *region) const {
  1398. RegionBindingsRef B = getRegionBindings(store);
  1399. region = region->getBaseRegion();
  1400. // Quick path: if the base is the head of a cluster, the region is live.
  1401. if (B.lookup(region))
  1402. return true;
  1403. // Slow path: if the region is the VALUE of any binding, it is live.
  1404. for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
  1405. const ClusterBindings &Cluster = RI.getData();
  1406. for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
  1407. CI != CE; ++CI) {
  1408. const SVal &D = CI.getData();
  1409. if (const MemRegion *R = D.getAsRegion())
  1410. if (R->getBaseRegion() == region)
  1411. return true;
  1412. }
  1413. }
  1414. return false;
  1415. }
  1416. //===----------------------------------------------------------------------===//
  1417. // Binding values to regions.
  1418. //===----------------------------------------------------------------------===//
  1419. StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
  1420. if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>())
  1421. if (const MemRegion* R = LV->getRegion())
  1422. return StoreRef(getRegionBindings(ST).removeBinding(R)
  1423. .asImmutableMap()
  1424. .getRootWithoutRetain(),
  1425. *this);
  1426. return StoreRef(ST, *this);
  1427. }
  1428. RegionBindingsRef
  1429. RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
  1430. if (L.getAs<loc::ConcreteInt>())
  1431. return B;
  1432. // If we get here, the location should be a region.
  1433. const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
  1434. // Check if the region is a struct region.
  1435. if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {
  1436. QualType Ty = TR->getValueType();
  1437. if (Ty->isArrayType())
  1438. return bindArray(B, TR, V);
  1439. if (Ty->isStructureOrClassType())
  1440. return bindStruct(B, TR, V);
  1441. if (Ty->isVectorType())
  1442. return bindVector(B, TR, V);
  1443. }
  1444. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
  1445. // Binding directly to a symbolic region should be treated as binding
  1446. // to element 0.
  1447. QualType T = SR->getSymbol()->getType();
  1448. if (T->isAnyPointerType() || T->isReferenceType())
  1449. T = T->getPointeeType();
  1450. R = GetElementZeroRegion(SR, T);
  1451. }
  1452. // Clear out bindings that may overlap with this binding.
  1453. RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R));
  1454. return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V);
  1455. }
  1456. // FIXME: this method should be merged into Bind().
  1457. StoreRef RegionStoreManager::bindCompoundLiteral(Store ST,
  1458. const CompoundLiteralExpr *CL,
  1459. const LocationContext *LC,
  1460. SVal V) {
  1461. return Bind(ST, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)), V);
  1462. }
  1463. RegionBindingsRef
  1464. RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B,
  1465. const MemRegion *R,
  1466. QualType T) {
  1467. SVal V;
  1468. if (Loc::isLocType(T))
  1469. V = svalBuilder.makeNull();
  1470. else if (T->isIntegerType())
  1471. V = svalBuilder.makeZeroVal(T);
  1472. else if (T->isStructureOrClassType() || T->isArrayType()) {
  1473. // Set the default value to a zero constant when it is a structure
  1474. // or array. The type doesn't really matter.
  1475. V = svalBuilder.makeZeroVal(Ctx.IntTy);
  1476. }
  1477. else {
  1478. // We can't represent values of this type, but we still need to set a value
  1479. // to record that the region has been initialized.
  1480. // If this assertion ever fires, a new case should be added above -- we
  1481. // should know how to default-initialize any value we can symbolicate.
  1482. assert(!SymbolManager::canSymbolicate(T) && "This type is representable");
  1483. V = UnknownVal();
  1484. }
  1485. return B.addBinding(R, BindingKey::Default, V);
  1486. }
  1487. RegionBindingsRef
  1488. RegionStoreManager::bindArray(RegionBindingsConstRef B,
  1489. const TypedValueRegion* R,
  1490. SVal Init) {
  1491. const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
  1492. QualType ElementTy = AT->getElementType();
  1493. Optional<uint64_t> Size;
  1494. if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
  1495. Size = CAT->getSize().getZExtValue();
  1496. // Check if the init expr is a string literal.
  1497. if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
  1498. const StringRegion *S = cast<StringRegion>(MRV->getRegion());
  1499. // Treat the string as a lazy compound value.
  1500. StoreRef store(B.asStore(), *this);
  1501. nonloc::LazyCompoundVal LCV = svalBuilder.makeLazyCompoundVal(store, S)
  1502. .castAs<nonloc::LazyCompoundVal>();
  1503. return bindAggregate(B, R, LCV);
  1504. }
  1505. // Handle lazy compound values.
  1506. if (Init.getAs<nonloc::LazyCompoundVal>())
  1507. return bindAggregate(B, R, Init);
  1508. // Remaining case: explicit compound values.
  1509. if (Init.isUnknown())
  1510. return setImplicitDefaultValue(B, R, ElementTy);
  1511. const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>();
  1512. nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
  1513. uint64_t i = 0;
  1514. RegionBindingsRef NewB(B);
  1515. for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
  1516. // The init list might be shorter than the array length.
  1517. if (VI == VE)
  1518. break;
  1519. const NonLoc &Idx = svalBuilder.makeArrayIndex(i);
  1520. const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
  1521. if (ElementTy->isStructureOrClassType())
  1522. NewB = bindStruct(NewB, ER, *VI);
  1523. else if (ElementTy->isArrayType())
  1524. NewB = bindArray(NewB, ER, *VI);
  1525. else
  1526. NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI);
  1527. }
  1528. // If the init list is shorter than the array length, set the
  1529. // array default value.
  1530. if (Size.hasValue() && i < Size.getValue())
  1531. NewB = setImplicitDefaultValue(NewB, R, ElementTy);
  1532. return NewB;
  1533. }
  1534. RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
  1535. const TypedValueRegion* R,
  1536. SVal V) {
  1537. QualType T = R->getValueType();
  1538. assert(T->isVectorType());
  1539. const VectorType *VT = T->getAs<VectorType>(); // Use getAs for typedefs.
  1540. // Handle lazy compound values and symbolic values.
  1541. if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
  1542. return bindAggregate(B, R, V);
  1543. // We may get non-CompoundVal accidentally due to imprecise cast logic or
  1544. // that we are binding symbolic struct value. Kill the field values, and if
  1545. // the value is symbolic go and bind it as a "default" binding.
  1546. if (!V.getAs<nonloc::CompoundVal>()) {
  1547. return bindAggregate(B, R, UnknownVal());
  1548. }
  1549. QualType ElemType = VT->getElementType();
  1550. nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>();
  1551. nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
  1552. unsigned index = 0, numElements = VT->getNumElements();
  1553. RegionBindingsRef NewB(B);
  1554. for ( ; index != numElements ; ++index) {
  1555. if (VI == VE)
  1556. break;
  1557. NonLoc Idx = svalBuilder.makeArrayIndex(index);
  1558. const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx);
  1559. if (ElemType->isArrayType())
  1560. NewB = bindArray(NewB, ER, *VI);
  1561. else if (ElemType->isStructureOrClassType())
  1562. NewB = bindStruct(NewB, ER, *VI);
  1563. else
  1564. NewB = bind(NewB, svalBuilder.makeLoc(ER), *VI);
  1565. }
  1566. return NewB;
  1567. }
  1568. RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
  1569. const TypedValueRegion* R,
  1570. SVal V) {
  1571. if (!Features.supportsFields())
  1572. return B;
  1573. QualType T = R->getValueType();
  1574. assert(T->isStructureOrClassType());
  1575. const RecordType* RT = T->getAs<RecordType>();
  1576. RecordDecl *RD = RT->getDecl();
  1577. if (!RD->isCompleteDefinition())
  1578. return B;
  1579. // Handle lazy compound values and symbolic values.
  1580. if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
  1581. return bindAggregate(B, R, V);
  1582. // We may get non-CompoundVal accidentally due to imprecise cast logic or
  1583. // that we are binding symbolic struct value. Kill the field values, and if
  1584. // the value is symbolic go and bind it as a "default" binding.
  1585. if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>())
  1586. return bindAggregate(B, R, UnknownVal());
  1587. const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>();
  1588. nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
  1589. RecordDecl::field_iterator FI, FE;
  1590. RegionBindingsRef NewB(B);
  1591. for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) {
  1592. if (VI == VE)
  1593. break;
  1594. // Skip any unnamed bitfields to stay in sync with the initializers.
  1595. if (FI->isUnnamedBitfield())
  1596. continue;
  1597. QualType FTy = FI->getType();
  1598. const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
  1599. if (FTy->isArrayType())
  1600. NewB = bindArray(NewB, FR, *VI);
  1601. else if (FTy->isStructureOrClassType())
  1602. NewB = bindStruct(NewB, FR, *VI);
  1603. else
  1604. NewB = bind(NewB, svalBuilder.makeLoc(FR), *VI);
  1605. ++VI;
  1606. }
  1607. // There may be fewer values in the initialize list than the fields of struct.
  1608. if (FI != FE) {
  1609. NewB = NewB.addBinding(R, BindingKey::Default,
  1610. svalBuilder.makeIntVal(0, false));
  1611. }
  1612. return NewB;
  1613. }
  1614. RegionBindingsRef
  1615. RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
  1616. const TypedRegion *R,
  1617. SVal Val) {
  1618. // Remove the old bindings, using 'R' as the root of all regions
  1619. // we will invalidate. Then add the new binding.
  1620. return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
  1621. }
  1622. //===----------------------------------------------------------------------===//
  1623. // State pruning.
  1624. //===----------------------------------------------------------------------===//
  1625. namespace {
  1626. class removeDeadBindingsWorker :
  1627. public ClusterAnalysis<removeDeadBindingsWorker> {
  1628. SmallVector<const SymbolicRegion*, 12> Postponed;
  1629. SymbolReaper &SymReaper;
  1630. const StackFrameContext *CurrentLCtx;
  1631. public:
  1632. removeDeadBindingsWorker(RegionStoreManager &rm,
  1633. ProgramStateManager &stateMgr,
  1634. RegionBindingsRef b, SymbolReaper &symReaper,
  1635. const StackFrameContext *LCtx)
  1636. : ClusterAnalysis<removeDeadBindingsWorker>(rm, stateMgr, b,
  1637. /* includeGlobals = */ false),
  1638. SymReaper(symReaper), CurrentLCtx(LCtx) {}
  1639. // Called by ClusterAnalysis.
  1640. void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C);
  1641. void VisitCluster(const MemRegion *baseR, const ClusterBindings &C);
  1642. bool UpdatePostponed();
  1643. void VisitBinding(SVal V);
  1644. };
  1645. }
  1646. void removeDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
  1647. const ClusterBindings &C) {
  1648. if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
  1649. if (SymReaper.isLive(VR))
  1650. AddToWorkList(baseR, &C);
  1651. return;
  1652. }
  1653. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
  1654. if (SymReaper.isLive(SR->getSymbol()))
  1655. AddToWorkList(SR, &C);
  1656. else
  1657. Postponed.push_back(SR);
  1658. return;
  1659. }
  1660. if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
  1661. AddToWorkList(baseR, &C);
  1662. return;
  1663. }
  1664. // CXXThisRegion in the current or parent location context is live.
  1665. if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
  1666. const StackArgumentsSpaceRegion *StackReg =
  1667. cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
  1668. const StackFrameContext *RegCtx = StackReg->getStackFrame();
  1669. if (CurrentLCtx &&
  1670. (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)))
  1671. AddToWorkList(TR, &C);
  1672. }
  1673. }
  1674. void removeDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
  1675. const ClusterBindings &C) {
  1676. // Mark the symbol for any SymbolicRegion with live bindings as live itself.
  1677. // This means we should continue to track that symbol.
  1678. if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR))
  1679. SymReaper.markLive(SymR->getSymbol());
  1680. for (ClusterBindings::iterator I = C.begin(), E = C.end(); I != E; ++I)
  1681. VisitBinding(I.getData());
  1682. }
  1683. void removeDeadBindingsWorker::VisitBinding(SVal V) {
  1684. // Is it a LazyCompoundVal? All referenced regions are live as well.
  1685. if (Optional<nonloc::LazyCompoundVal> LCS =
  1686. V.getAs<nonloc::LazyCompoundVal>()) {
  1687. const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
  1688. for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
  1689. E = Vals.end();
  1690. I != E; ++I)
  1691. VisitBinding(*I);
  1692. return;
  1693. }
  1694. // If V is a region, then add it to the worklist.
  1695. if (const MemRegion *R = V.getAsRegion()) {
  1696. AddToWorkList(R);
  1697. // All regions captured by a block are also live.
  1698. if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
  1699. BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(),
  1700. E = BR->referenced_vars_end();
  1701. for ( ; I != E; ++I)
  1702. AddToWorkList(I.getCapturedRegion());
  1703. }
  1704. }
  1705. // Update the set of live symbols.
  1706. for (SymExpr::symbol_iterator SI = V.symbol_begin(), SE = V.symbol_end();
  1707. SI!=SE; ++SI)
  1708. SymReaper.markLive(*SI);
  1709. }
  1710. bool removeDeadBindingsWorker::UpdatePostponed() {
  1711. // See if any postponed SymbolicRegions are actually live now, after
  1712. // having done a scan.
  1713. bool changed = false;
  1714. for (SmallVectorImpl<const SymbolicRegion*>::iterator
  1715. I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
  1716. if (const SymbolicRegion *SR = *I) {
  1717. if (SymReaper.isLive(SR->getSymbol())) {
  1718. changed |= AddToWorkList(SR);
  1719. *I = NULL;
  1720. }
  1721. }
  1722. }
  1723. return changed;
  1724. }
  1725. StoreRef RegionStoreManager::removeDeadBindings(Store store,
  1726. const StackFrameContext *LCtx,
  1727. SymbolReaper& SymReaper) {
  1728. RegionBindingsRef B = getRegionBindings(store);
  1729. removeDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
  1730. W.GenerateClusters();
  1731. // Enqueue the region roots onto the worklist.
  1732. for (SymbolReaper::region_iterator I = SymReaper.region_begin(),
  1733. E = SymReaper.region_end(); I != E; ++I) {
  1734. W.AddToWorkList(*I);
  1735. }
  1736. do W.RunWorkList(); while (W.UpdatePostponed());
  1737. // We have now scanned the store, marking reachable regions and symbols
  1738. // as live. We now remove all the regions that are dead from the store
  1739. // as well as update DSymbols with the set symbols that are now dead.
  1740. for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
  1741. const MemRegion *Base = I.getKey();
  1742. // If the cluster has been visited, we know the region has been marked.
  1743. if (W.isVisited(Base))
  1744. continue;
  1745. // Remove the dead entry.
  1746. B = B.remove(Base);
  1747. if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(Base))
  1748. SymReaper.maybeDead(SymR->getSymbol());
  1749. // Mark all non-live symbols that this binding references as dead.
  1750. const ClusterBindings &Cluster = I.getData();
  1751. for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
  1752. CI != CE; ++CI) {
  1753. SVal X = CI.getData();
  1754. SymExpr::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
  1755. for (; SI != SE; ++SI)
  1756. SymReaper.maybeDead(*SI);
  1757. }
  1758. }
  1759. return StoreRef(B.asStore(), *this);
  1760. }
  1761. //===----------------------------------------------------------------------===//
  1762. // Utility methods.
  1763. //===----------------------------------------------------------------------===//
  1764. void RegionStoreManager::print(Store store, raw_ostream &OS,
  1765. const char* nl, const char *sep) {
  1766. RegionBindingsRef B = getRegionBindings(store);
  1767. OS << "Store (direct and default bindings), "
  1768. << B.asStore()
  1769. << " :" << nl;
  1770. B.dump(OS, nl);
  1771. }