RegionStore.cpp 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689
  1. //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
  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 a basic region store model. In this model, we do have field
  10. // sensitivity. But we assume nothing about the heap shape. So recursive data
  11. // structures are largely ignored. Basically we do 1-limiting analysis.
  12. // Parameter pointers are assumed with no aliasing. Pointee objects of
  13. // parameters are created lazily.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "clang/AST/Attr.h"
  17. #include "clang/AST/CharUnits.h"
  18. #include "clang/ASTMatchers/ASTMatchFinder.h"
  19. #include "clang/Analysis/Analyses/LiveVariables.h"
  20. #include "clang/Analysis/AnalysisDeclContext.h"
  21. #include "clang/Basic/JsonSupport.h"
  22. #include "clang/Basic/TargetInfo.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  24. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  25. #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
  26. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  27. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  28. #include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
  29. #include "llvm/ADT/ImmutableMap.h"
  30. #include "llvm/ADT/Optional.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <utility>
  33. using namespace clang;
  34. using namespace ento;
  35. //===----------------------------------------------------------------------===//
  36. // Representation of binding keys.
  37. //===----------------------------------------------------------------------===//
  38. namespace {
  39. class BindingKey {
  40. public:
  41. enum Kind { Default = 0x0, Direct = 0x1 };
  42. private:
  43. enum { Symbolic = 0x2 };
  44. llvm::PointerIntPair<const MemRegion *, 2> P;
  45. uint64_t Data;
  46. /// Create a key for a binding to region \p r, which has a symbolic offset
  47. /// from region \p Base.
  48. explicit BindingKey(const SubRegion *r, const SubRegion *Base, Kind k)
  49. : P(r, k | Symbolic), Data(reinterpret_cast<uintptr_t>(Base)) {
  50. assert(r && Base && "Must have known regions.");
  51. assert(getConcreteOffsetRegion() == Base && "Failed to store base region");
  52. }
  53. /// Create a key for a binding at \p offset from base region \p r.
  54. explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
  55. : P(r, k), Data(offset) {
  56. assert(r && "Must have known regions.");
  57. assert(getOffset() == offset && "Failed to store offset");
  58. assert((r == r->getBaseRegion() || isa<ObjCIvarRegion>(r) ||
  59. isa <CXXDerivedObjectRegion>(r)) &&
  60. "Not a base");
  61. }
  62. public:
  63. bool isDirect() const { return P.getInt() & Direct; }
  64. bool hasSymbolicOffset() const { return P.getInt() & Symbolic; }
  65. const MemRegion *getRegion() const { return P.getPointer(); }
  66. uint64_t getOffset() const {
  67. assert(!hasSymbolicOffset());
  68. return Data;
  69. }
  70. const SubRegion *getConcreteOffsetRegion() const {
  71. assert(hasSymbolicOffset());
  72. return reinterpret_cast<const SubRegion *>(static_cast<uintptr_t>(Data));
  73. }
  74. const MemRegion *getBaseRegion() const {
  75. if (hasSymbolicOffset())
  76. return getConcreteOffsetRegion()->getBaseRegion();
  77. return getRegion()->getBaseRegion();
  78. }
  79. void Profile(llvm::FoldingSetNodeID& ID) const {
  80. ID.AddPointer(P.getOpaqueValue());
  81. ID.AddInteger(Data);
  82. }
  83. static BindingKey Make(const MemRegion *R, Kind k);
  84. bool operator<(const BindingKey &X) const {
  85. if (P.getOpaqueValue() < X.P.getOpaqueValue())
  86. return true;
  87. if (P.getOpaqueValue() > X.P.getOpaqueValue())
  88. return false;
  89. return Data < X.Data;
  90. }
  91. bool operator==(const BindingKey &X) const {
  92. return P.getOpaqueValue() == X.P.getOpaqueValue() &&
  93. Data == X.Data;
  94. }
  95. LLVM_DUMP_METHOD void dump() const;
  96. };
  97. } // end anonymous namespace
  98. BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
  99. const RegionOffset &RO = R->getAsOffset();
  100. if (RO.hasSymbolicOffset())
  101. return BindingKey(cast<SubRegion>(R), cast<SubRegion>(RO.getRegion()), k);
  102. return BindingKey(RO.getRegion(), RO.getOffset(), k);
  103. }
  104. namespace llvm {
  105. static inline raw_ostream &operator<<(raw_ostream &Out, BindingKey K) {
  106. Out << "\"kind\": \"" << (K.isDirect() ? "Direct" : "Default")
  107. << "\", \"offset\": ";
  108. if (!K.hasSymbolicOffset())
  109. Out << K.getOffset();
  110. else
  111. Out << "null";
  112. return Out;
  113. }
  114. } // namespace llvm
  115. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  116. void BindingKey::dump() const { llvm::errs() << *this; }
  117. #endif
  118. //===----------------------------------------------------------------------===//
  119. // Actual Store type.
  120. //===----------------------------------------------------------------------===//
  121. typedef llvm::ImmutableMap<BindingKey, SVal> ClusterBindings;
  122. typedef llvm::ImmutableMapRef<BindingKey, SVal> ClusterBindingsRef;
  123. typedef std::pair<BindingKey, SVal> BindingPair;
  124. typedef llvm::ImmutableMap<const MemRegion *, ClusterBindings>
  125. RegionBindings;
  126. namespace {
  127. class RegionBindingsRef : public llvm::ImmutableMapRef<const MemRegion *,
  128. ClusterBindings> {
  129. ClusterBindings::Factory *CBFactory;
  130. // This flag indicates whether the current bindings are within the analysis
  131. // that has started from main(). It affects how we perform loads from
  132. // global variables that have initializers: if we have observed the
  133. // program execution from the start and we know that these variables
  134. // have not been overwritten yet, we can be sure that their initializers
  135. // are still relevant. This flag never gets changed when the bindings are
  136. // updated, so it could potentially be moved into RegionStoreManager
  137. // (as if it's the same bindings but a different loading procedure)
  138. // however that would have made the manager needlessly stateful.
  139. bool IsMainAnalysis;
  140. public:
  141. typedef llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>
  142. ParentTy;
  143. RegionBindingsRef(ClusterBindings::Factory &CBFactory,
  144. const RegionBindings::TreeTy *T,
  145. RegionBindings::TreeTy::Factory *F,
  146. bool IsMainAnalysis)
  147. : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(T, F),
  148. CBFactory(&CBFactory), IsMainAnalysis(IsMainAnalysis) {}
  149. RegionBindingsRef(const ParentTy &P,
  150. ClusterBindings::Factory &CBFactory,
  151. bool IsMainAnalysis)
  152. : llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>(P),
  153. CBFactory(&CBFactory), IsMainAnalysis(IsMainAnalysis) {}
  154. RegionBindingsRef add(key_type_ref K, data_type_ref D) const {
  155. return RegionBindingsRef(static_cast<const ParentTy *>(this)->add(K, D),
  156. *CBFactory, IsMainAnalysis);
  157. }
  158. RegionBindingsRef remove(key_type_ref K) const {
  159. return RegionBindingsRef(static_cast<const ParentTy *>(this)->remove(K),
  160. *CBFactory, IsMainAnalysis);
  161. }
  162. RegionBindingsRef addBinding(BindingKey K, SVal V) const;
  163. RegionBindingsRef addBinding(const MemRegion *R,
  164. BindingKey::Kind k, SVal V) const;
  165. const SVal *lookup(BindingKey K) const;
  166. const SVal *lookup(const MemRegion *R, BindingKey::Kind k) const;
  167. using llvm::ImmutableMapRef<const MemRegion *, ClusterBindings>::lookup;
  168. RegionBindingsRef removeBinding(BindingKey K);
  169. RegionBindingsRef removeBinding(const MemRegion *R,
  170. BindingKey::Kind k);
  171. RegionBindingsRef removeBinding(const MemRegion *R) {
  172. return removeBinding(R, BindingKey::Direct).
  173. removeBinding(R, BindingKey::Default);
  174. }
  175. Optional<SVal> getDirectBinding(const MemRegion *R) const;
  176. /// getDefaultBinding - Returns an SVal* representing an optional default
  177. /// binding associated with a region and its subregions.
  178. Optional<SVal> getDefaultBinding(const MemRegion *R) const;
  179. /// Return the internal tree as a Store.
  180. Store asStore() const {
  181. llvm::PointerIntPair<Store, 1, bool> Ptr = {
  182. asImmutableMap().getRootWithoutRetain(), IsMainAnalysis};
  183. return reinterpret_cast<Store>(Ptr.getOpaqueValue());
  184. }
  185. bool isMainAnalysis() const {
  186. return IsMainAnalysis;
  187. }
  188. void printJson(raw_ostream &Out, const char *NL = "\n",
  189. unsigned int Space = 0, bool IsDot = false) const {
  190. for (iterator I = begin(); I != end(); ++I) {
  191. // TODO: We might need a .printJson for I.getKey() as well.
  192. Indent(Out, Space, IsDot)
  193. << "{ \"cluster\": \"" << I.getKey() << "\", \"pointer\": \""
  194. << (const void *)I.getKey() << "\", \"items\": [" << NL;
  195. ++Space;
  196. const ClusterBindings &CB = I.getData();
  197. for (ClusterBindings::iterator CI = CB.begin(); CI != CB.end(); ++CI) {
  198. Indent(Out, Space, IsDot) << "{ " << CI.getKey() << ", \"value\": ";
  199. CI.getData().printJson(Out, /*AddQuotes=*/true);
  200. Out << " }";
  201. if (std::next(CI) != CB.end())
  202. Out << ',';
  203. Out << NL;
  204. }
  205. --Space;
  206. Indent(Out, Space, IsDot) << "]}";
  207. if (std::next(I) != end())
  208. Out << ',';
  209. Out << NL;
  210. }
  211. }
  212. LLVM_DUMP_METHOD void dump() const { printJson(llvm::errs()); }
  213. };
  214. } // end anonymous namespace
  215. typedef const RegionBindingsRef& RegionBindingsConstRef;
  216. Optional<SVal> RegionBindingsRef::getDirectBinding(const MemRegion *R) const {
  217. return Optional<SVal>::create(lookup(R, BindingKey::Direct));
  218. }
  219. Optional<SVal> RegionBindingsRef::getDefaultBinding(const MemRegion *R) const {
  220. return Optional<SVal>::create(lookup(R, BindingKey::Default));
  221. }
  222. RegionBindingsRef RegionBindingsRef::addBinding(BindingKey K, SVal V) const {
  223. const MemRegion *Base = K.getBaseRegion();
  224. const ClusterBindings *ExistingCluster = lookup(Base);
  225. ClusterBindings Cluster =
  226. (ExistingCluster ? *ExistingCluster : CBFactory->getEmptyMap());
  227. ClusterBindings NewCluster = CBFactory->add(Cluster, K, V);
  228. return add(Base, NewCluster);
  229. }
  230. RegionBindingsRef RegionBindingsRef::addBinding(const MemRegion *R,
  231. BindingKey::Kind k,
  232. SVal V) const {
  233. return addBinding(BindingKey::Make(R, k), V);
  234. }
  235. const SVal *RegionBindingsRef::lookup(BindingKey K) const {
  236. const ClusterBindings *Cluster = lookup(K.getBaseRegion());
  237. if (!Cluster)
  238. return nullptr;
  239. return Cluster->lookup(K);
  240. }
  241. const SVal *RegionBindingsRef::lookup(const MemRegion *R,
  242. BindingKey::Kind k) const {
  243. return lookup(BindingKey::Make(R, k));
  244. }
  245. RegionBindingsRef RegionBindingsRef::removeBinding(BindingKey K) {
  246. const MemRegion *Base = K.getBaseRegion();
  247. const ClusterBindings *Cluster = lookup(Base);
  248. if (!Cluster)
  249. return *this;
  250. ClusterBindings NewCluster = CBFactory->remove(*Cluster, K);
  251. if (NewCluster.isEmpty())
  252. return remove(Base);
  253. return add(Base, NewCluster);
  254. }
  255. RegionBindingsRef RegionBindingsRef::removeBinding(const MemRegion *R,
  256. BindingKey::Kind k){
  257. return removeBinding(BindingKey::Make(R, k));
  258. }
  259. //===----------------------------------------------------------------------===//
  260. // Fine-grained control of RegionStoreManager.
  261. //===----------------------------------------------------------------------===//
  262. namespace {
  263. struct minimal_features_tag {};
  264. struct maximal_features_tag {};
  265. class RegionStoreFeatures {
  266. bool SupportsFields;
  267. public:
  268. RegionStoreFeatures(minimal_features_tag) :
  269. SupportsFields(false) {}
  270. RegionStoreFeatures(maximal_features_tag) :
  271. SupportsFields(true) {}
  272. void enableFields(bool t) { SupportsFields = t; }
  273. bool supportsFields() const { return SupportsFields; }
  274. };
  275. }
  276. //===----------------------------------------------------------------------===//
  277. // Main RegionStore logic.
  278. //===----------------------------------------------------------------------===//
  279. namespace {
  280. class InvalidateRegionsWorker;
  281. class RegionStoreManager : public StoreManager {
  282. public:
  283. const RegionStoreFeatures Features;
  284. RegionBindings::Factory RBFactory;
  285. mutable ClusterBindings::Factory CBFactory;
  286. typedef std::vector<SVal> SValListTy;
  287. private:
  288. typedef llvm::DenseMap<const LazyCompoundValData *,
  289. SValListTy> LazyBindingsMapTy;
  290. LazyBindingsMapTy LazyBindingsMap;
  291. /// The largest number of fields a struct can have and still be
  292. /// considered "small".
  293. ///
  294. /// This is currently used to decide whether or not it is worth "forcing" a
  295. /// LazyCompoundVal on bind.
  296. ///
  297. /// This is controlled by 'region-store-small-struct-limit' option.
  298. /// To disable all small-struct-dependent behavior, set the option to "0".
  299. unsigned SmallStructLimit;
  300. /// A helper used to populate the work list with the given set of
  301. /// regions.
  302. void populateWorkList(InvalidateRegionsWorker &W,
  303. ArrayRef<SVal> Values,
  304. InvalidatedRegions *TopLevelRegions);
  305. public:
  306. RegionStoreManager(ProgramStateManager& mgr, const RegionStoreFeatures &f)
  307. : StoreManager(mgr), Features(f),
  308. RBFactory(mgr.getAllocator()), CBFactory(mgr.getAllocator()),
  309. SmallStructLimit(0) {
  310. SubEngine &Eng = StateMgr.getOwningEngine();
  311. AnalyzerOptions &Options = Eng.getAnalysisManager().options;
  312. SmallStructLimit = Options.RegionStoreSmallStructLimit;
  313. }
  314. /// setImplicitDefaultValue - Set the default binding for the provided
  315. /// MemRegion to the value implicitly defined for compound literals when
  316. /// the value is not specified.
  317. RegionBindingsRef setImplicitDefaultValue(RegionBindingsConstRef B,
  318. const MemRegion *R, QualType T);
  319. /// ArrayToPointer - Emulates the "decay" of an array to a pointer
  320. /// type. 'Array' represents the lvalue of the array being decayed
  321. /// to a pointer, and the returned SVal represents the decayed
  322. /// version of that lvalue (i.e., a pointer to the first element of
  323. /// the array). This is called by ExprEngine when evaluating
  324. /// casts from arrays to pointers.
  325. SVal ArrayToPointer(Loc Array, QualType ElementTy) override;
  326. /// Creates the Store that correctly represents memory contents before
  327. /// the beginning of the analysis of the given top-level stack frame.
  328. StoreRef getInitialStore(const LocationContext *InitLoc) override {
  329. bool IsMainAnalysis = false;
  330. if (const auto *FD = dyn_cast<FunctionDecl>(InitLoc->getDecl()))
  331. IsMainAnalysis = FD->isMain() && !Ctx.getLangOpts().CPlusPlus;
  332. return StoreRef(RegionBindingsRef(
  333. RegionBindingsRef::ParentTy(RBFactory.getEmptyMap(), RBFactory),
  334. CBFactory, IsMainAnalysis).asStore(), *this);
  335. }
  336. //===-------------------------------------------------------------------===//
  337. // Binding values to regions.
  338. //===-------------------------------------------------------------------===//
  339. RegionBindingsRef invalidateGlobalRegion(MemRegion::Kind K,
  340. const Expr *Ex,
  341. unsigned Count,
  342. const LocationContext *LCtx,
  343. RegionBindingsRef B,
  344. InvalidatedRegions *Invalidated);
  345. StoreRef invalidateRegions(Store store,
  346. ArrayRef<SVal> Values,
  347. const Expr *E, unsigned Count,
  348. const LocationContext *LCtx,
  349. const CallEvent *Call,
  350. InvalidatedSymbols &IS,
  351. RegionAndSymbolInvalidationTraits &ITraits,
  352. InvalidatedRegions *Invalidated,
  353. InvalidatedRegions *InvalidatedTopLevel) override;
  354. bool scanReachableSymbols(Store S, const MemRegion *R,
  355. ScanReachableSymbols &Callbacks) override;
  356. RegionBindingsRef removeSubRegionBindings(RegionBindingsConstRef B,
  357. const SubRegion *R);
  358. public: // Part of public interface to class.
  359. StoreRef Bind(Store store, Loc LV, SVal V) override {
  360. return StoreRef(bind(getRegionBindings(store), LV, V).asStore(), *this);
  361. }
  362. RegionBindingsRef bind(RegionBindingsConstRef B, Loc LV, SVal V);
  363. // BindDefaultInitial is only used to initialize a region with
  364. // a default value.
  365. StoreRef BindDefaultInitial(Store store, const MemRegion *R,
  366. SVal V) override {
  367. RegionBindingsRef B = getRegionBindings(store);
  368. // Use other APIs when you have to wipe the region that was initialized
  369. // earlier.
  370. assert(!(B.getDefaultBinding(R) || B.getDirectBinding(R)) &&
  371. "Double initialization!");
  372. B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V);
  373. return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this);
  374. }
  375. // BindDefaultZero is used for zeroing constructors that may accidentally
  376. // overwrite existing bindings.
  377. StoreRef BindDefaultZero(Store store, const MemRegion *R) override {
  378. // FIXME: The offsets of empty bases can be tricky because of
  379. // of the so called "empty base class optimization".
  380. // If a base class has been optimized out
  381. // we should not try to create a binding, otherwise we should.
  382. // Unfortunately, at the moment ASTRecordLayout doesn't expose
  383. // the actual sizes of the empty bases
  384. // and trying to infer them from offsets/alignments
  385. // seems to be error-prone and non-trivial because of the trailing padding.
  386. // As a temporary mitigation we don't create bindings for empty bases.
  387. if (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
  388. if (BR->getDecl()->isEmpty())
  389. return StoreRef(store, *this);
  390. RegionBindingsRef B = getRegionBindings(store);
  391. SVal V = svalBuilder.makeZeroVal(Ctx.CharTy);
  392. B = removeSubRegionBindings(B, cast<SubRegion>(R));
  393. B = B.addBinding(BindingKey::Make(R, BindingKey::Default), V);
  394. return StoreRef(B.asImmutableMap().getRootWithoutRetain(), *this);
  395. }
  396. /// Attempt to extract the fields of \p LCV and bind them to the struct region
  397. /// \p R.
  398. ///
  399. /// This path is used when it seems advantageous to "force" loading the values
  400. /// within a LazyCompoundVal to bind memberwise to the struct region, rather
  401. /// than using a Default binding at the base of the entire region. This is a
  402. /// heuristic attempting to avoid building long chains of LazyCompoundVals.
  403. ///
  404. /// \returns The updated store bindings, or \c None if binding non-lazily
  405. /// would be too expensive.
  406. Optional<RegionBindingsRef> tryBindSmallStruct(RegionBindingsConstRef B,
  407. const TypedValueRegion *R,
  408. const RecordDecl *RD,
  409. nonloc::LazyCompoundVal LCV);
  410. /// BindStruct - Bind a compound value to a structure.
  411. RegionBindingsRef bindStruct(RegionBindingsConstRef B,
  412. const TypedValueRegion* R, SVal V);
  413. /// BindVector - Bind a compound value to a vector.
  414. RegionBindingsRef bindVector(RegionBindingsConstRef B,
  415. const TypedValueRegion* R, SVal V);
  416. RegionBindingsRef bindArray(RegionBindingsConstRef B,
  417. const TypedValueRegion* R,
  418. SVal V);
  419. /// Clears out all bindings in the given region and assigns a new value
  420. /// as a Default binding.
  421. RegionBindingsRef bindAggregate(RegionBindingsConstRef B,
  422. const TypedRegion *R,
  423. SVal DefaultVal);
  424. /// Create a new store with the specified binding removed.
  425. /// \param ST the original store, that is the basis for the new store.
  426. /// \param L the location whose binding should be removed.
  427. StoreRef killBinding(Store ST, Loc L) override;
  428. void incrementReferenceCount(Store store) override {
  429. getRegionBindings(store).manualRetain();
  430. }
  431. /// If the StoreManager supports it, decrement the reference count of
  432. /// the specified Store object. If the reference count hits 0, the memory
  433. /// associated with the object is recycled.
  434. void decrementReferenceCount(Store store) override {
  435. getRegionBindings(store).manualRelease();
  436. }
  437. bool includedInBindings(Store store, const MemRegion *region) const override;
  438. /// Return the value bound to specified location in a given state.
  439. ///
  440. /// The high level logic for this method is this:
  441. /// getBinding (L)
  442. /// if L has binding
  443. /// return L's binding
  444. /// else if L is in killset
  445. /// return unknown
  446. /// else
  447. /// if L is on stack or heap
  448. /// return undefined
  449. /// else
  450. /// return symbolic
  451. SVal getBinding(Store S, Loc L, QualType T) override {
  452. return getBinding(getRegionBindings(S), L, T);
  453. }
  454. Optional<SVal> getDefaultBinding(Store S, const MemRegion *R) override {
  455. RegionBindingsRef B = getRegionBindings(S);
  456. // Default bindings are always applied over a base region so look up the
  457. // base region's default binding, otherwise the lookup will fail when R
  458. // is at an offset from R->getBaseRegion().
  459. return B.getDefaultBinding(R->getBaseRegion());
  460. }
  461. SVal getBinding(RegionBindingsConstRef B, Loc L, QualType T = QualType());
  462. SVal getBindingForElement(RegionBindingsConstRef B, const ElementRegion *R);
  463. SVal getBindingForField(RegionBindingsConstRef B, const FieldRegion *R);
  464. SVal getBindingForObjCIvar(RegionBindingsConstRef B, const ObjCIvarRegion *R);
  465. SVal getBindingForVar(RegionBindingsConstRef B, const VarRegion *R);
  466. SVal getBindingForLazySymbol(const TypedValueRegion *R);
  467. SVal getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
  468. const TypedValueRegion *R,
  469. QualType Ty);
  470. SVal getLazyBinding(const SubRegion *LazyBindingRegion,
  471. RegionBindingsRef LazyBinding);
  472. /// Get bindings for the values in a struct and return a CompoundVal, used
  473. /// when doing struct copy:
  474. /// struct s x, y;
  475. /// x = y;
  476. /// y's value is retrieved by this method.
  477. SVal getBindingForStruct(RegionBindingsConstRef B, const TypedValueRegion *R);
  478. SVal getBindingForArray(RegionBindingsConstRef B, const TypedValueRegion *R);
  479. NonLoc createLazyBinding(RegionBindingsConstRef B, const TypedValueRegion *R);
  480. /// Used to lazily generate derived symbols for bindings that are defined
  481. /// implicitly by default bindings in a super region.
  482. ///
  483. /// Note that callers may need to specially handle LazyCompoundVals, which
  484. /// are returned as is in case the caller needs to treat them differently.
  485. Optional<SVal> getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
  486. const MemRegion *superR,
  487. const TypedValueRegion *R,
  488. QualType Ty);
  489. /// Get the state and region whose binding this region \p R corresponds to.
  490. ///
  491. /// If there is no lazy binding for \p R, the returned value will have a null
  492. /// \c second. Note that a null pointer can represents a valid Store.
  493. std::pair<Store, const SubRegion *>
  494. findLazyBinding(RegionBindingsConstRef B, const SubRegion *R,
  495. const SubRegion *originalRegion);
  496. /// Returns the cached set of interesting SVals contained within a lazy
  497. /// binding.
  498. ///
  499. /// The precise value of "interesting" is determined for the purposes of
  500. /// RegionStore's internal analysis. It must always contain all regions and
  501. /// symbols, but may omit constants and other kinds of SVal.
  502. const SValListTy &getInterestingValues(nonloc::LazyCompoundVal LCV);
  503. //===------------------------------------------------------------------===//
  504. // State pruning.
  505. //===------------------------------------------------------------------===//
  506. /// removeDeadBindings - Scans the RegionStore of 'state' for dead values.
  507. /// It returns a new Store with these values removed.
  508. StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
  509. SymbolReaper& SymReaper) override;
  510. //===------------------------------------------------------------------===//
  511. // Region "extents".
  512. //===------------------------------------------------------------------===//
  513. // FIXME: This method will soon be eliminated; see the note in Store.h.
  514. DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state,
  515. const MemRegion* R,
  516. QualType EleTy) override;
  517. //===------------------------------------------------------------------===//
  518. // Utility methods.
  519. //===------------------------------------------------------------------===//
  520. RegionBindingsRef getRegionBindings(Store store) const {
  521. llvm::PointerIntPair<Store, 1, bool> Ptr;
  522. Ptr.setFromOpaqueValue(const_cast<void *>(store));
  523. return RegionBindingsRef(
  524. CBFactory,
  525. static_cast<const RegionBindings::TreeTy *>(Ptr.getPointer()),
  526. RBFactory.getTreeFactory(),
  527. Ptr.getInt());
  528. }
  529. void printJson(raw_ostream &Out, Store S, const char *NL = "\n",
  530. unsigned int Space = 0, bool IsDot = false) const override;
  531. void iterBindings(Store store, BindingsHandler& f) override {
  532. RegionBindingsRef B = getRegionBindings(store);
  533. for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
  534. const ClusterBindings &Cluster = I.getData();
  535. for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
  536. CI != CE; ++CI) {
  537. const BindingKey &K = CI.getKey();
  538. if (!K.isDirect())
  539. continue;
  540. if (const SubRegion *R = dyn_cast<SubRegion>(K.getRegion())) {
  541. // FIXME: Possibly incorporate the offset?
  542. if (!f.HandleBinding(*this, store, R, CI.getData()))
  543. return;
  544. }
  545. }
  546. }
  547. }
  548. };
  549. } // end anonymous namespace
  550. //===----------------------------------------------------------------------===//
  551. // RegionStore creation.
  552. //===----------------------------------------------------------------------===//
  553. std::unique_ptr<StoreManager>
  554. ento::CreateRegionStoreManager(ProgramStateManager &StMgr) {
  555. RegionStoreFeatures F = maximal_features_tag();
  556. return std::make_unique<RegionStoreManager>(StMgr, F);
  557. }
  558. std::unique_ptr<StoreManager>
  559. ento::CreateFieldsOnlyRegionStoreManager(ProgramStateManager &StMgr) {
  560. RegionStoreFeatures F = minimal_features_tag();
  561. F.enableFields(true);
  562. return std::make_unique<RegionStoreManager>(StMgr, F);
  563. }
  564. //===----------------------------------------------------------------------===//
  565. // Region Cluster analysis.
  566. //===----------------------------------------------------------------------===//
  567. namespace {
  568. /// Used to determine which global regions are automatically included in the
  569. /// initial worklist of a ClusterAnalysis.
  570. enum GlobalsFilterKind {
  571. /// Don't include any global regions.
  572. GFK_None,
  573. /// Only include system globals.
  574. GFK_SystemOnly,
  575. /// Include all global regions.
  576. GFK_All
  577. };
  578. template <typename DERIVED>
  579. class ClusterAnalysis {
  580. protected:
  581. typedef llvm::DenseMap<const MemRegion *, const ClusterBindings *> ClusterMap;
  582. typedef const MemRegion * WorkListElement;
  583. typedef SmallVector<WorkListElement, 10> WorkList;
  584. llvm::SmallPtrSet<const ClusterBindings *, 16> Visited;
  585. WorkList WL;
  586. RegionStoreManager &RM;
  587. ASTContext &Ctx;
  588. SValBuilder &svalBuilder;
  589. RegionBindingsRef B;
  590. protected:
  591. const ClusterBindings *getCluster(const MemRegion *R) {
  592. return B.lookup(R);
  593. }
  594. /// Returns true if all clusters in the given memspace should be initially
  595. /// included in the cluster analysis. Subclasses may provide their
  596. /// own implementation.
  597. bool includeEntireMemorySpace(const MemRegion *Base) {
  598. return false;
  599. }
  600. public:
  601. ClusterAnalysis(RegionStoreManager &rm, ProgramStateManager &StateMgr,
  602. RegionBindingsRef b)
  603. : RM(rm), Ctx(StateMgr.getContext()),
  604. svalBuilder(StateMgr.getSValBuilder()), B(std::move(b)) {}
  605. RegionBindingsRef getRegionBindings() const { return B; }
  606. bool isVisited(const MemRegion *R) {
  607. return Visited.count(getCluster(R));
  608. }
  609. void GenerateClusters() {
  610. // Scan the entire set of bindings and record the region clusters.
  611. for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end();
  612. RI != RE; ++RI){
  613. const MemRegion *Base = RI.getKey();
  614. const ClusterBindings &Cluster = RI.getData();
  615. assert(!Cluster.isEmpty() && "Empty clusters should be removed");
  616. static_cast<DERIVED*>(this)->VisitAddedToCluster(Base, Cluster);
  617. // If the base's memspace should be entirely invalidated, add the cluster
  618. // to the workspace up front.
  619. if (static_cast<DERIVED*>(this)->includeEntireMemorySpace(Base))
  620. AddToWorkList(WorkListElement(Base), &Cluster);
  621. }
  622. }
  623. bool AddToWorkList(WorkListElement E, const ClusterBindings *C) {
  624. if (C && !Visited.insert(C).second)
  625. return false;
  626. WL.push_back(E);
  627. return true;
  628. }
  629. bool AddToWorkList(const MemRegion *R) {
  630. return static_cast<DERIVED*>(this)->AddToWorkList(R);
  631. }
  632. void RunWorkList() {
  633. while (!WL.empty()) {
  634. WorkListElement E = WL.pop_back_val();
  635. const MemRegion *BaseR = E;
  636. static_cast<DERIVED*>(this)->VisitCluster(BaseR, getCluster(BaseR));
  637. }
  638. }
  639. void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C) {}
  640. void VisitCluster(const MemRegion *baseR, const ClusterBindings *C) {}
  641. void VisitCluster(const MemRegion *BaseR, const ClusterBindings *C,
  642. bool Flag) {
  643. static_cast<DERIVED*>(this)->VisitCluster(BaseR, C);
  644. }
  645. };
  646. }
  647. //===----------------------------------------------------------------------===//
  648. // Binding invalidation.
  649. //===----------------------------------------------------------------------===//
  650. bool RegionStoreManager::scanReachableSymbols(Store S, const MemRegion *R,
  651. ScanReachableSymbols &Callbacks) {
  652. assert(R == R->getBaseRegion() && "Should only be called for base regions");
  653. RegionBindingsRef B = getRegionBindings(S);
  654. const ClusterBindings *Cluster = B.lookup(R);
  655. if (!Cluster)
  656. return true;
  657. for (ClusterBindings::iterator RI = Cluster->begin(), RE = Cluster->end();
  658. RI != RE; ++RI) {
  659. if (!Callbacks.scan(RI.getData()))
  660. return false;
  661. }
  662. return true;
  663. }
  664. static inline bool isUnionField(const FieldRegion *FR) {
  665. return FR->getDecl()->getParent()->isUnion();
  666. }
  667. typedef SmallVector<const FieldDecl *, 8> FieldVector;
  668. static void getSymbolicOffsetFields(BindingKey K, FieldVector &Fields) {
  669. assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
  670. const MemRegion *Base = K.getConcreteOffsetRegion();
  671. const MemRegion *R = K.getRegion();
  672. while (R != Base) {
  673. if (const FieldRegion *FR = dyn_cast<FieldRegion>(R))
  674. if (!isUnionField(FR))
  675. Fields.push_back(FR->getDecl());
  676. R = cast<SubRegion>(R)->getSuperRegion();
  677. }
  678. }
  679. static bool isCompatibleWithFields(BindingKey K, const FieldVector &Fields) {
  680. assert(K.hasSymbolicOffset() && "Not implemented for concrete offset keys");
  681. if (Fields.empty())
  682. return true;
  683. FieldVector FieldsInBindingKey;
  684. getSymbolicOffsetFields(K, FieldsInBindingKey);
  685. ptrdiff_t Delta = FieldsInBindingKey.size() - Fields.size();
  686. if (Delta >= 0)
  687. return std::equal(FieldsInBindingKey.begin() + Delta,
  688. FieldsInBindingKey.end(),
  689. Fields.begin());
  690. else
  691. return std::equal(FieldsInBindingKey.begin(), FieldsInBindingKey.end(),
  692. Fields.begin() - Delta);
  693. }
  694. /// Collects all bindings in \p Cluster that may refer to bindings within
  695. /// \p Top.
  696. ///
  697. /// Each binding is a pair whose \c first is the key (a BindingKey) and whose
  698. /// \c second is the value (an SVal).
  699. ///
  700. /// The \p IncludeAllDefaultBindings parameter specifies whether to include
  701. /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is
  702. /// an aggregate within a larger aggregate with a default binding.
  703. static void
  704. collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
  705. SValBuilder &SVB, const ClusterBindings &Cluster,
  706. const SubRegion *Top, BindingKey TopKey,
  707. bool IncludeAllDefaultBindings) {
  708. FieldVector FieldsInSymbolicSubregions;
  709. if (TopKey.hasSymbolicOffset()) {
  710. getSymbolicOffsetFields(TopKey, FieldsInSymbolicSubregions);
  711. Top = TopKey.getConcreteOffsetRegion();
  712. TopKey = BindingKey::Make(Top, BindingKey::Default);
  713. }
  714. // Find the length (in bits) of the region being invalidated.
  715. uint64_t Length = UINT64_MAX;
  716. SVal Extent = Top->getExtent(SVB);
  717. if (Optional<nonloc::ConcreteInt> ExtentCI =
  718. Extent.getAs<nonloc::ConcreteInt>()) {
  719. const llvm::APSInt &ExtentInt = ExtentCI->getValue();
  720. assert(ExtentInt.isNonNegative() || ExtentInt.isUnsigned());
  721. // Extents are in bytes but region offsets are in bits. Be careful!
  722. Length = ExtentInt.getLimitedValue() * SVB.getContext().getCharWidth();
  723. } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(Top)) {
  724. if (FR->getDecl()->isBitField())
  725. Length = FR->getDecl()->getBitWidthValue(SVB.getContext());
  726. }
  727. for (ClusterBindings::iterator I = Cluster.begin(), E = Cluster.end();
  728. I != E; ++I) {
  729. BindingKey NextKey = I.getKey();
  730. if (NextKey.getRegion() == TopKey.getRegion()) {
  731. // FIXME: This doesn't catch the case where we're really invalidating a
  732. // region with a symbolic offset. Example:
  733. // R: points[i].y
  734. // Next: points[0].x
  735. if (NextKey.getOffset() > TopKey.getOffset() &&
  736. NextKey.getOffset() - TopKey.getOffset() < Length) {
  737. // Case 1: The next binding is inside the region we're invalidating.
  738. // Include it.
  739. Bindings.push_back(*I);
  740. } else if (NextKey.getOffset() == TopKey.getOffset()) {
  741. // Case 2: The next binding is at the same offset as the region we're
  742. // invalidating. In this case, we need to leave default bindings alone,
  743. // since they may be providing a default value for a regions beyond what
  744. // we're invalidating.
  745. // FIXME: This is probably incorrect; consider invalidating an outer
  746. // struct whose first field is bound to a LazyCompoundVal.
  747. if (IncludeAllDefaultBindings || NextKey.isDirect())
  748. Bindings.push_back(*I);
  749. }
  750. } else if (NextKey.hasSymbolicOffset()) {
  751. const MemRegion *Base = NextKey.getConcreteOffsetRegion();
  752. if (Top->isSubRegionOf(Base) && Top != Base) {
  753. // Case 3: The next key is symbolic and we just changed something within
  754. // its concrete region. We don't know if the binding is still valid, so
  755. // we'll be conservative and include it.
  756. if (IncludeAllDefaultBindings || NextKey.isDirect())
  757. if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
  758. Bindings.push_back(*I);
  759. } else if (const SubRegion *BaseSR = dyn_cast<SubRegion>(Base)) {
  760. // Case 4: The next key is symbolic, but we changed a known
  761. // super-region. In this case the binding is certainly included.
  762. if (BaseSR->isSubRegionOf(Top))
  763. if (isCompatibleWithFields(NextKey, FieldsInSymbolicSubregions))
  764. Bindings.push_back(*I);
  765. }
  766. }
  767. }
  768. }
  769. static void
  770. collectSubRegionBindings(SmallVectorImpl<BindingPair> &Bindings,
  771. SValBuilder &SVB, const ClusterBindings &Cluster,
  772. const SubRegion *Top, bool IncludeAllDefaultBindings) {
  773. collectSubRegionBindings(Bindings, SVB, Cluster, Top,
  774. BindingKey::Make(Top, BindingKey::Default),
  775. IncludeAllDefaultBindings);
  776. }
  777. RegionBindingsRef
  778. RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B,
  779. const SubRegion *Top) {
  780. BindingKey TopKey = BindingKey::Make(Top, BindingKey::Default);
  781. const MemRegion *ClusterHead = TopKey.getBaseRegion();
  782. if (Top == ClusterHead) {
  783. // We can remove an entire cluster's bindings all in one go.
  784. return B.remove(Top);
  785. }
  786. const ClusterBindings *Cluster = B.lookup(ClusterHead);
  787. if (!Cluster) {
  788. // If we're invalidating a region with a symbolic offset, we need to make
  789. // sure we don't treat the base region as uninitialized anymore.
  790. if (TopKey.hasSymbolicOffset()) {
  791. const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
  792. return B.addBinding(Concrete, BindingKey::Default, UnknownVal());
  793. }
  794. return B;
  795. }
  796. SmallVector<BindingPair, 32> Bindings;
  797. collectSubRegionBindings(Bindings, svalBuilder, *Cluster, Top, TopKey,
  798. /*IncludeAllDefaultBindings=*/false);
  799. ClusterBindingsRef Result(*Cluster, CBFactory);
  800. for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
  801. E = Bindings.end();
  802. I != E; ++I)
  803. Result = Result.remove(I->first);
  804. // If we're invalidating a region with a symbolic offset, we need to make sure
  805. // we don't treat the base region as uninitialized anymore.
  806. // FIXME: This isn't very precise; see the example in
  807. // collectSubRegionBindings.
  808. if (TopKey.hasSymbolicOffset()) {
  809. const SubRegion *Concrete = TopKey.getConcreteOffsetRegion();
  810. Result = Result.add(BindingKey::Make(Concrete, BindingKey::Default),
  811. UnknownVal());
  812. }
  813. if (Result.isEmpty())
  814. return B.remove(ClusterHead);
  815. return B.add(ClusterHead, Result.asImmutableMap());
  816. }
  817. namespace {
  818. class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
  819. {
  820. const Expr *Ex;
  821. unsigned Count;
  822. const LocationContext *LCtx;
  823. InvalidatedSymbols &IS;
  824. RegionAndSymbolInvalidationTraits &ITraits;
  825. StoreManager::InvalidatedRegions *Regions;
  826. GlobalsFilterKind GlobalsFilter;
  827. public:
  828. InvalidateRegionsWorker(RegionStoreManager &rm,
  829. ProgramStateManager &stateMgr,
  830. RegionBindingsRef b,
  831. const Expr *ex, unsigned count,
  832. const LocationContext *lctx,
  833. InvalidatedSymbols &is,
  834. RegionAndSymbolInvalidationTraits &ITraitsIn,
  835. StoreManager::InvalidatedRegions *r,
  836. GlobalsFilterKind GFK)
  837. : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
  838. Ex(ex), Count(count), LCtx(lctx), IS(is), ITraits(ITraitsIn), Regions(r),
  839. GlobalsFilter(GFK) {}
  840. void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
  841. void VisitBinding(SVal V);
  842. using ClusterAnalysis::AddToWorkList;
  843. bool AddToWorkList(const MemRegion *R);
  844. /// Returns true if all clusters in the memory space for \p Base should be
  845. /// be invalidated.
  846. bool includeEntireMemorySpace(const MemRegion *Base);
  847. /// Returns true if the memory space of the given region is one of the global
  848. /// regions specially included at the start of invalidation.
  849. bool isInitiallyIncludedGlobalRegion(const MemRegion *R);
  850. };
  851. }
  852. bool InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
  853. bool doNotInvalidateSuperRegion = ITraits.hasTrait(
  854. R, RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
  855. const MemRegion *BaseR = doNotInvalidateSuperRegion ? R : R->getBaseRegion();
  856. return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR));
  857. }
  858. void InvalidateRegionsWorker::VisitBinding(SVal V) {
  859. // A symbol? Mark it touched by the invalidation.
  860. if (SymbolRef Sym = V.getAsSymbol())
  861. IS.insert(Sym);
  862. if (const MemRegion *R = V.getAsRegion()) {
  863. AddToWorkList(R);
  864. return;
  865. }
  866. // Is it a LazyCompoundVal? All references get invalidated as well.
  867. if (Optional<nonloc::LazyCompoundVal> LCS =
  868. V.getAs<nonloc::LazyCompoundVal>()) {
  869. const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
  870. for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
  871. E = Vals.end();
  872. I != E; ++I)
  873. VisitBinding(*I);
  874. return;
  875. }
  876. }
  877. void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
  878. const ClusterBindings *C) {
  879. bool PreserveRegionsContents =
  880. ITraits.hasTrait(baseR,
  881. RegionAndSymbolInvalidationTraits::TK_PreserveContents);
  882. if (C) {
  883. for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I)
  884. VisitBinding(I.getData());
  885. // Invalidate regions contents.
  886. if (!PreserveRegionsContents)
  887. B = B.remove(baseR);
  888. }
  889. if (const auto *TO = dyn_cast<TypedValueRegion>(baseR)) {
  890. if (const auto *RD = TO->getValueType()->getAsCXXRecordDecl()) {
  891. // Lambdas can affect all static local variables without explicitly
  892. // capturing those.
  893. // We invalidate all static locals referenced inside the lambda body.
  894. if (RD->isLambda() && RD->getLambdaCallOperator()->getBody()) {
  895. using namespace ast_matchers;
  896. const char *DeclBind = "DeclBind";
  897. StatementMatcher RefToStatic = stmt(hasDescendant(declRefExpr(
  898. to(varDecl(hasStaticStorageDuration()).bind(DeclBind)))));
  899. auto Matches =
  900. match(RefToStatic, *RD->getLambdaCallOperator()->getBody(),
  901. RD->getASTContext());
  902. for (BoundNodes &Match : Matches) {
  903. auto *VD = Match.getNodeAs<VarDecl>(DeclBind);
  904. const VarRegion *ToInvalidate =
  905. RM.getRegionManager().getVarRegion(VD, LCtx);
  906. AddToWorkList(ToInvalidate);
  907. }
  908. }
  909. }
  910. }
  911. // BlockDataRegion? If so, invalidate captured variables that are passed
  912. // by reference.
  913. if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
  914. for (BlockDataRegion::referenced_vars_iterator
  915. BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
  916. BI != BE; ++BI) {
  917. const VarRegion *VR = BI.getCapturedRegion();
  918. const VarDecl *VD = VR->getDecl();
  919. if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) {
  920. AddToWorkList(VR);
  921. }
  922. else if (Loc::isLocType(VR->getValueType())) {
  923. // Map the current bindings to a Store to retrieve the value
  924. // of the binding. If that binding itself is a region, we should
  925. // invalidate that region. This is because a block may capture
  926. // a pointer value, but the thing pointed by that pointer may
  927. // get invalidated.
  928. SVal V = RM.getBinding(B, loc::MemRegionVal(VR));
  929. if (Optional<Loc> L = V.getAs<Loc>()) {
  930. if (const MemRegion *LR = L->getAsRegion())
  931. AddToWorkList(LR);
  932. }
  933. }
  934. }
  935. return;
  936. }
  937. // Symbolic region?
  938. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
  939. IS.insert(SR->getSymbol());
  940. // Nothing else should be done in the case when we preserve regions context.
  941. if (PreserveRegionsContents)
  942. return;
  943. // Otherwise, we have a normal data region. Record that we touched the region.
  944. if (Regions)
  945. Regions->push_back(baseR);
  946. if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
  947. // Invalidate the region by setting its default value to
  948. // conjured symbol. The type of the symbol is irrelevant.
  949. DefinedOrUnknownSVal V =
  950. svalBuilder.conjureSymbolVal(baseR, Ex, LCtx, Ctx.IntTy, Count);
  951. B = B.addBinding(baseR, BindingKey::Default, V);
  952. return;
  953. }
  954. if (!baseR->isBoundable())
  955. return;
  956. const TypedValueRegion *TR = cast<TypedValueRegion>(baseR);
  957. QualType T = TR->getValueType();
  958. if (isInitiallyIncludedGlobalRegion(baseR)) {
  959. // If the region is a global and we are invalidating all globals,
  960. // erasing the entry is good enough. This causes all globals to be lazily
  961. // symbolicated from the same base symbol.
  962. return;
  963. }
  964. if (T->isRecordType()) {
  965. // Invalidate the region by setting its default value to
  966. // conjured symbol. The type of the symbol is irrelevant.
  967. DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
  968. Ctx.IntTy, Count);
  969. B = B.addBinding(baseR, BindingKey::Default, V);
  970. return;
  971. }
  972. if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
  973. bool doNotInvalidateSuperRegion = ITraits.hasTrait(
  974. baseR,
  975. RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
  976. if (doNotInvalidateSuperRegion) {
  977. // We are not doing blank invalidation of the whole array region so we
  978. // have to manually invalidate each elements.
  979. Optional<uint64_t> NumElements;
  980. // Compute lower and upper offsets for region within array.
  981. if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
  982. NumElements = CAT->getSize().getZExtValue();
  983. if (!NumElements) // We are not dealing with a constant size array
  984. goto conjure_default;
  985. QualType ElementTy = AT->getElementType();
  986. uint64_t ElemSize = Ctx.getTypeSize(ElementTy);
  987. const RegionOffset &RO = baseR->getAsOffset();
  988. const MemRegion *SuperR = baseR->getBaseRegion();
  989. if (RO.hasSymbolicOffset()) {
  990. // If base region has a symbolic offset,
  991. // we revert to invalidating the super region.
  992. if (SuperR)
  993. AddToWorkList(SuperR);
  994. goto conjure_default;
  995. }
  996. uint64_t LowerOffset = RO.getOffset();
  997. uint64_t UpperOffset = LowerOffset + *NumElements * ElemSize;
  998. bool UpperOverflow = UpperOffset < LowerOffset;
  999. // Invalidate regions which are within array boundaries,
  1000. // or have a symbolic offset.
  1001. if (!SuperR)
  1002. goto conjure_default;
  1003. const ClusterBindings *C = B.lookup(SuperR);
  1004. if (!C)
  1005. goto conjure_default;
  1006. for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E;
  1007. ++I) {
  1008. const BindingKey &BK = I.getKey();
  1009. Optional<uint64_t> ROffset =
  1010. BK.hasSymbolicOffset() ? Optional<uint64_t>() : BK.getOffset();
  1011. // Check offset is not symbolic and within array's boundaries.
  1012. // Handles arrays of 0 elements and of 0-sized elements as well.
  1013. if (!ROffset ||
  1014. ((*ROffset >= LowerOffset && *ROffset < UpperOffset) ||
  1015. (UpperOverflow &&
  1016. (*ROffset >= LowerOffset || *ROffset < UpperOffset)) ||
  1017. (LowerOffset == UpperOffset && *ROffset == LowerOffset))) {
  1018. B = B.removeBinding(I.getKey());
  1019. // Bound symbolic regions need to be invalidated for dead symbol
  1020. // detection.
  1021. SVal V = I.getData();
  1022. const MemRegion *R = V.getAsRegion();
  1023. if (R && isa<SymbolicRegion>(R))
  1024. VisitBinding(V);
  1025. }
  1026. }
  1027. }
  1028. conjure_default:
  1029. // Set the default value of the array to conjured symbol.
  1030. DefinedOrUnknownSVal V =
  1031. svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
  1032. AT->getElementType(), Count);
  1033. B = B.addBinding(baseR, BindingKey::Default, V);
  1034. return;
  1035. }
  1036. DefinedOrUnknownSVal V = svalBuilder.conjureSymbolVal(baseR, Ex, LCtx,
  1037. T,Count);
  1038. assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
  1039. B = B.addBinding(baseR, BindingKey::Direct, V);
  1040. }
  1041. bool InvalidateRegionsWorker::isInitiallyIncludedGlobalRegion(
  1042. const MemRegion *R) {
  1043. switch (GlobalsFilter) {
  1044. case GFK_None:
  1045. return false;
  1046. case GFK_SystemOnly:
  1047. return isa<GlobalSystemSpaceRegion>(R->getMemorySpace());
  1048. case GFK_All:
  1049. return isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace());
  1050. }
  1051. llvm_unreachable("unknown globals filter");
  1052. }
  1053. bool InvalidateRegionsWorker::includeEntireMemorySpace(const MemRegion *Base) {
  1054. if (isInitiallyIncludedGlobalRegion(Base))
  1055. return true;
  1056. const MemSpaceRegion *MemSpace = Base->getMemorySpace();
  1057. return ITraits.hasTrait(MemSpace,
  1058. RegionAndSymbolInvalidationTraits::TK_EntireMemSpace);
  1059. }
  1060. RegionBindingsRef
  1061. RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K,
  1062. const Expr *Ex,
  1063. unsigned Count,
  1064. const LocationContext *LCtx,
  1065. RegionBindingsRef B,
  1066. InvalidatedRegions *Invalidated) {
  1067. // Bind the globals memory space to a new symbol that we will use to derive
  1068. // the bindings for all globals.
  1069. const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion(K);
  1070. SVal V = svalBuilder.conjureSymbolVal(/* symbolTag = */ (const void*) GS, Ex, LCtx,
  1071. /* type does not matter */ Ctx.IntTy,
  1072. Count);
  1073. B = B.removeBinding(GS)
  1074. .addBinding(BindingKey::Make(GS, BindingKey::Default), V);
  1075. // Even if there are no bindings in the global scope, we still need to
  1076. // record that we touched it.
  1077. if (Invalidated)
  1078. Invalidated->push_back(GS);
  1079. return B;
  1080. }
  1081. void RegionStoreManager::populateWorkList(InvalidateRegionsWorker &W,
  1082. ArrayRef<SVal> Values,
  1083. InvalidatedRegions *TopLevelRegions) {
  1084. for (ArrayRef<SVal>::iterator I = Values.begin(),
  1085. E = Values.end(); I != E; ++I) {
  1086. SVal V = *I;
  1087. if (Optional<nonloc::LazyCompoundVal> LCS =
  1088. V.getAs<nonloc::LazyCompoundVal>()) {
  1089. const SValListTy &Vals = getInterestingValues(*LCS);
  1090. for (SValListTy::const_iterator I = Vals.begin(),
  1091. E = Vals.end(); I != E; ++I) {
  1092. // Note: the last argument is false here because these are
  1093. // non-top-level regions.
  1094. if (const MemRegion *R = (*I).getAsRegion())
  1095. W.AddToWorkList(R);
  1096. }
  1097. continue;
  1098. }
  1099. if (const MemRegion *R = V.getAsRegion()) {
  1100. if (TopLevelRegions)
  1101. TopLevelRegions->push_back(R);
  1102. W.AddToWorkList(R);
  1103. continue;
  1104. }
  1105. }
  1106. }
  1107. StoreRef
  1108. RegionStoreManager::invalidateRegions(Store store,
  1109. ArrayRef<SVal> Values,
  1110. const Expr *Ex, unsigned Count,
  1111. const LocationContext *LCtx,
  1112. const CallEvent *Call,
  1113. InvalidatedSymbols &IS,
  1114. RegionAndSymbolInvalidationTraits &ITraits,
  1115. InvalidatedRegions *TopLevelRegions,
  1116. InvalidatedRegions *Invalidated) {
  1117. GlobalsFilterKind GlobalsFilter;
  1118. if (Call) {
  1119. if (Call->isInSystemHeader())
  1120. GlobalsFilter = GFK_SystemOnly;
  1121. else
  1122. GlobalsFilter = GFK_All;
  1123. } else {
  1124. GlobalsFilter = GFK_None;
  1125. }
  1126. RegionBindingsRef B = getRegionBindings(store);
  1127. InvalidateRegionsWorker W(*this, StateMgr, B, Ex, Count, LCtx, IS, ITraits,
  1128. Invalidated, GlobalsFilter);
  1129. // Scan the bindings and generate the clusters.
  1130. W.GenerateClusters();
  1131. // Add the regions to the worklist.
  1132. populateWorkList(W, Values, TopLevelRegions);
  1133. W.RunWorkList();
  1134. // Return the new bindings.
  1135. B = W.getRegionBindings();
  1136. // For calls, determine which global regions should be invalidated and
  1137. // invalidate them. (Note that function-static and immutable globals are never
  1138. // invalidated by this.)
  1139. // TODO: This could possibly be more precise with modules.
  1140. switch (GlobalsFilter) {
  1141. case GFK_All:
  1142. B = invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind,
  1143. Ex, Count, LCtx, B, Invalidated);
  1144. LLVM_FALLTHROUGH;
  1145. case GFK_SystemOnly:
  1146. B = invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind,
  1147. Ex, Count, LCtx, B, Invalidated);
  1148. LLVM_FALLTHROUGH;
  1149. case GFK_None:
  1150. break;
  1151. }
  1152. return StoreRef(B.asStore(), *this);
  1153. }
  1154. //===----------------------------------------------------------------------===//
  1155. // Extents for regions.
  1156. //===----------------------------------------------------------------------===//
  1157. DefinedOrUnknownSVal
  1158. RegionStoreManager::getSizeInElements(ProgramStateRef state,
  1159. const MemRegion *R,
  1160. QualType EleTy) {
  1161. SVal Size = cast<SubRegion>(R)->getExtent(svalBuilder);
  1162. const llvm::APSInt *SizeInt = svalBuilder.getKnownValue(state, Size);
  1163. if (!SizeInt)
  1164. return UnknownVal();
  1165. CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
  1166. if (Ctx.getAsVariableArrayType(EleTy)) {
  1167. // FIXME: We need to track extra state to properly record the size
  1168. // of VLAs. Returning UnknownVal here, however, is a stop-gap so that
  1169. // we don't have a divide-by-zero below.
  1170. return UnknownVal();
  1171. }
  1172. CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
  1173. // If a variable is reinterpreted as a type that doesn't fit into a larger
  1174. // type evenly, round it down.
  1175. // This is a signed value, since it's used in arithmetic with signed indices.
  1176. return svalBuilder.makeIntVal(RegionSize / EleSize,
  1177. svalBuilder.getArrayIndexType());
  1178. }
  1179. //===----------------------------------------------------------------------===//
  1180. // Location and region casting.
  1181. //===----------------------------------------------------------------------===//
  1182. /// ArrayToPointer - Emulates the "decay" of an array to a pointer
  1183. /// type. 'Array' represents the lvalue of the array being decayed
  1184. /// to a pointer, and the returned SVal represents the decayed
  1185. /// version of that lvalue (i.e., a pointer to the first element of
  1186. /// the array). This is called by ExprEngine when evaluating casts
  1187. /// from arrays to pointers.
  1188. SVal RegionStoreManager::ArrayToPointer(Loc Array, QualType T) {
  1189. if (Array.getAs<loc::ConcreteInt>())
  1190. return Array;
  1191. if (!Array.getAs<loc::MemRegionVal>())
  1192. return UnknownVal();
  1193. const SubRegion *R =
  1194. cast<SubRegion>(Array.castAs<loc::MemRegionVal>().getRegion());
  1195. NonLoc ZeroIdx = svalBuilder.makeZeroArrayIndex();
  1196. return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, R, Ctx));
  1197. }
  1198. //===----------------------------------------------------------------------===//
  1199. // Loading values from regions.
  1200. //===----------------------------------------------------------------------===//
  1201. SVal RegionStoreManager::getBinding(RegionBindingsConstRef B, Loc L, QualType T) {
  1202. assert(!L.getAs<UnknownVal>() && "location unknown");
  1203. assert(!L.getAs<UndefinedVal>() && "location undefined");
  1204. // For access to concrete addresses, return UnknownVal. Checks
  1205. // for null dereferences (and similar errors) are done by checkers, not
  1206. // the Store.
  1207. // FIXME: We can consider lazily symbolicating such memory, but we really
  1208. // should defer this when we can reason easily about symbolicating arrays
  1209. // of bytes.
  1210. if (L.getAs<loc::ConcreteInt>()) {
  1211. return UnknownVal();
  1212. }
  1213. if (!L.getAs<loc::MemRegionVal>()) {
  1214. return UnknownVal();
  1215. }
  1216. const MemRegion *MR = L.castAs<loc::MemRegionVal>().getRegion();
  1217. if (isa<BlockDataRegion>(MR)) {
  1218. return UnknownVal();
  1219. }
  1220. if (!isa<TypedValueRegion>(MR)) {
  1221. if (T.isNull()) {
  1222. if (const TypedRegion *TR = dyn_cast<TypedRegion>(MR))
  1223. T = TR->getLocationType()->getPointeeType();
  1224. else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
  1225. T = SR->getSymbol()->getType()->getPointeeType();
  1226. }
  1227. assert(!T.isNull() && "Unable to auto-detect binding type!");
  1228. assert(!T->isVoidType() && "Attempting to dereference a void pointer!");
  1229. MR = GetElementZeroRegion(cast<SubRegion>(MR), T);
  1230. } else {
  1231. T = cast<TypedValueRegion>(MR)->getValueType();
  1232. }
  1233. // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
  1234. // instead of 'Loc', and have the other Loc cases handled at a higher level.
  1235. const TypedValueRegion *R = cast<TypedValueRegion>(MR);
  1236. QualType RTy = R->getValueType();
  1237. // FIXME: we do not yet model the parts of a complex type, so treat the
  1238. // whole thing as "unknown".
  1239. if (RTy->isAnyComplexType())
  1240. return UnknownVal();
  1241. // FIXME: We should eventually handle funny addressing. e.g.:
  1242. //
  1243. // int x = ...;
  1244. // int *p = &x;
  1245. // char *q = (char*) p;
  1246. // char c = *q; // returns the first byte of 'x'.
  1247. //
  1248. // Such funny addressing will occur due to layering of regions.
  1249. if (RTy->isStructureOrClassType())
  1250. return getBindingForStruct(B, R);
  1251. // FIXME: Handle unions.
  1252. if (RTy->isUnionType())
  1253. return createLazyBinding(B, R);
  1254. if (RTy->isArrayType()) {
  1255. if (RTy->isConstantArrayType())
  1256. return getBindingForArray(B, R);
  1257. else
  1258. return UnknownVal();
  1259. }
  1260. // FIXME: handle Vector types.
  1261. if (RTy->isVectorType())
  1262. return UnknownVal();
  1263. if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
  1264. return CastRetrievedVal(getBindingForField(B, FR), FR, T);
  1265. if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
  1266. // FIXME: Here we actually perform an implicit conversion from the loaded
  1267. // value to the element type. Eventually we want to compose these values
  1268. // more intelligently. For example, an 'element' can encompass multiple
  1269. // bound regions (e.g., several bound bytes), or could be a subset of
  1270. // a larger value.
  1271. return CastRetrievedVal(getBindingForElement(B, ER), ER, T);
  1272. }
  1273. if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
  1274. // FIXME: Here we actually perform an implicit conversion from the loaded
  1275. // value to the ivar type. What we should model is stores to ivars
  1276. // that blow past the extent of the ivar. If the address of the ivar is
  1277. // reinterpretted, it is possible we stored a different value that could
  1278. // fit within the ivar. Either we need to cast these when storing them
  1279. // or reinterpret them lazily (as we do here).
  1280. return CastRetrievedVal(getBindingForObjCIvar(B, IVR), IVR, T);
  1281. }
  1282. if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
  1283. // FIXME: Here we actually perform an implicit conversion from the loaded
  1284. // value to the variable type. What we should model is stores to variables
  1285. // that blow past the extent of the variable. If the address of the
  1286. // variable is reinterpretted, it is possible we stored a different value
  1287. // that could fit within the variable. Either we need to cast these when
  1288. // storing them or reinterpret them lazily (as we do here).
  1289. return CastRetrievedVal(getBindingForVar(B, VR), VR, T);
  1290. }
  1291. const SVal *V = B.lookup(R, BindingKey::Direct);
  1292. // Check if the region has a binding.
  1293. if (V)
  1294. return *V;
  1295. // The location does not have a bound value. This means that it has
  1296. // the value it had upon its creation and/or entry to the analyzed
  1297. // function/method. These are either symbolic values or 'undefined'.
  1298. if (R->hasStackNonParametersStorage()) {
  1299. // All stack variables are considered to have undefined values
  1300. // upon creation. All heap allocated blocks are considered to
  1301. // have undefined values as well unless they are explicitly bound
  1302. // to specific values.
  1303. return UndefinedVal();
  1304. }
  1305. // All other values are symbolic.
  1306. return svalBuilder.getRegionValueSymbolVal(R);
  1307. }
  1308. static QualType getUnderlyingType(const SubRegion *R) {
  1309. QualType RegionTy;
  1310. if (const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(R))
  1311. RegionTy = TVR->getValueType();
  1312. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
  1313. RegionTy = SR->getSymbol()->getType();
  1314. return RegionTy;
  1315. }
  1316. /// Checks to see if store \p B has a lazy binding for region \p R.
  1317. ///
  1318. /// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected
  1319. /// if there are additional bindings within \p R.
  1320. ///
  1321. /// Note that unlike RegionStoreManager::findLazyBinding, this will not search
  1322. /// for lazy bindings for super-regions of \p R.
  1323. static Optional<nonloc::LazyCompoundVal>
  1324. getExistingLazyBinding(SValBuilder &SVB, RegionBindingsConstRef B,
  1325. const SubRegion *R, bool AllowSubregionBindings) {
  1326. Optional<SVal> V = B.getDefaultBinding(R);
  1327. if (!V)
  1328. return None;
  1329. Optional<nonloc::LazyCompoundVal> LCV = V->getAs<nonloc::LazyCompoundVal>();
  1330. if (!LCV)
  1331. return None;
  1332. // If the LCV is for a subregion, the types might not match, and we shouldn't
  1333. // reuse the binding.
  1334. QualType RegionTy = getUnderlyingType(R);
  1335. if (!RegionTy.isNull() &&
  1336. !RegionTy->isVoidPointerType()) {
  1337. QualType SourceRegionTy = LCV->getRegion()->getValueType();
  1338. if (!SVB.getContext().hasSameUnqualifiedType(RegionTy, SourceRegionTy))
  1339. return None;
  1340. }
  1341. if (!AllowSubregionBindings) {
  1342. // If there are any other bindings within this region, we shouldn't reuse
  1343. // the top-level binding.
  1344. SmallVector<BindingPair, 16> Bindings;
  1345. collectSubRegionBindings(Bindings, SVB, *B.lookup(R->getBaseRegion()), R,
  1346. /*IncludeAllDefaultBindings=*/true);
  1347. if (Bindings.size() > 1)
  1348. return None;
  1349. }
  1350. return *LCV;
  1351. }
  1352. std::pair<Store, const SubRegion *>
  1353. RegionStoreManager::findLazyBinding(RegionBindingsConstRef B,
  1354. const SubRegion *R,
  1355. const SubRegion *originalRegion) {
  1356. if (originalRegion != R) {
  1357. if (Optional<nonloc::LazyCompoundVal> V =
  1358. getExistingLazyBinding(svalBuilder, B, R, true))
  1359. return std::make_pair(V->getStore(), V->getRegion());
  1360. }
  1361. typedef std::pair<Store, const SubRegion *> StoreRegionPair;
  1362. StoreRegionPair Result = StoreRegionPair();
  1363. if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
  1364. Result = findLazyBinding(B, cast<SubRegion>(ER->getSuperRegion()),
  1365. originalRegion);
  1366. if (Result.second)
  1367. Result.second = MRMgr.getElementRegionWithSuper(ER, Result.second);
  1368. } else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
  1369. Result = findLazyBinding(B, cast<SubRegion>(FR->getSuperRegion()),
  1370. originalRegion);
  1371. if (Result.second)
  1372. Result.second = MRMgr.getFieldRegionWithSuper(FR, Result.second);
  1373. } else if (const CXXBaseObjectRegion *BaseReg =
  1374. dyn_cast<CXXBaseObjectRegion>(R)) {
  1375. // C++ base object region is another kind of region that we should blast
  1376. // through to look for lazy compound value. It is like a field region.
  1377. Result = findLazyBinding(B, cast<SubRegion>(BaseReg->getSuperRegion()),
  1378. originalRegion);
  1379. if (Result.second)
  1380. Result.second = MRMgr.getCXXBaseObjectRegionWithSuper(BaseReg,
  1381. Result.second);
  1382. }
  1383. return Result;
  1384. }
  1385. SVal RegionStoreManager::getBindingForElement(RegionBindingsConstRef B,
  1386. const ElementRegion* R) {
  1387. // We do not currently model bindings of the CompoundLiteralregion.
  1388. if (isa<CompoundLiteralRegion>(R->getBaseRegion()))
  1389. return UnknownVal();
  1390. // Check if the region has a binding.
  1391. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1392. return *V;
  1393. const MemRegion* superR = R->getSuperRegion();
  1394. // Check if the region is an element region of a string literal.
  1395. if (const StringRegion *StrR = dyn_cast<StringRegion>(superR)) {
  1396. // FIXME: Handle loads from strings where the literal is treated as
  1397. // an integer, e.g., *((unsigned int*)"hello")
  1398. QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
  1399. if (!Ctx.hasSameUnqualifiedType(T, R->getElementType()))
  1400. return UnknownVal();
  1401. const StringLiteral *Str = StrR->getStringLiteral();
  1402. SVal Idx = R->getIndex();
  1403. if (Optional<nonloc::ConcreteInt> CI = Idx.getAs<nonloc::ConcreteInt>()) {
  1404. int64_t i = CI->getValue().getSExtValue();
  1405. // Abort on string underrun. This can be possible by arbitrary
  1406. // clients of getBindingForElement().
  1407. if (i < 0)
  1408. return UndefinedVal();
  1409. int64_t length = Str->getLength();
  1410. // Technically, only i == length is guaranteed to be null.
  1411. // However, such overflows should be caught before reaching this point;
  1412. // the only time such an access would be made is if a string literal was
  1413. // used to initialize a larger array.
  1414. char c = (i >= length) ? '\0' : Str->getCodeUnit(i);
  1415. return svalBuilder.makeIntVal(c, T);
  1416. }
  1417. } else if (const VarRegion *VR = dyn_cast<VarRegion>(superR)) {
  1418. // Check if the containing array has an initialized value that we can trust.
  1419. // We can trust a const value or a value of a global initializer in main().
  1420. const VarDecl *VD = VR->getDecl();
  1421. if (VD->getType().isConstQualified() ||
  1422. R->getElementType().isConstQualified() ||
  1423. (B.isMainAnalysis() && VD->hasGlobalStorage())) {
  1424. if (const Expr *Init = VD->getAnyInitializer()) {
  1425. if (const auto *InitList = dyn_cast<InitListExpr>(Init)) {
  1426. // The array index has to be known.
  1427. if (auto CI = R->getIndex().getAs<nonloc::ConcreteInt>()) {
  1428. int64_t i = CI->getValue().getSExtValue();
  1429. // If it is known that the index is out of bounds, we can return
  1430. // an undefined value.
  1431. if (i < 0)
  1432. return UndefinedVal();
  1433. if (auto CAT = Ctx.getAsConstantArrayType(VD->getType()))
  1434. if (CAT->getSize().sle(i))
  1435. return UndefinedVal();
  1436. // If there is a list, but no init, it must be zero.
  1437. if (i >= InitList->getNumInits())
  1438. return svalBuilder.makeZeroVal(R->getElementType());
  1439. if (const Expr *ElemInit = InitList->getInit(i))
  1440. if (Optional<SVal> V = svalBuilder.getConstantVal(ElemInit))
  1441. return *V;
  1442. }
  1443. }
  1444. }
  1445. }
  1446. }
  1447. // Check for loads from a code text region. For such loads, just give up.
  1448. if (isa<CodeTextRegion>(superR))
  1449. return UnknownVal();
  1450. // Handle the case where we are indexing into a larger scalar object.
  1451. // For example, this handles:
  1452. // int x = ...
  1453. // char *y = &x;
  1454. // return *y;
  1455. // FIXME: This is a hack, and doesn't do anything really intelligent yet.
  1456. const RegionRawOffset &O = R->getAsArrayOffset();
  1457. // If we cannot reason about the offset, return an unknown value.
  1458. if (!O.getRegion())
  1459. return UnknownVal();
  1460. if (const TypedValueRegion *baseR =
  1461. dyn_cast_or_null<TypedValueRegion>(O.getRegion())) {
  1462. QualType baseT = baseR->getValueType();
  1463. if (baseT->isScalarType()) {
  1464. QualType elemT = R->getElementType();
  1465. if (elemT->isScalarType()) {
  1466. if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
  1467. if (const Optional<SVal> &V = B.getDirectBinding(superR)) {
  1468. if (SymbolRef parentSym = V->getAsSymbol())
  1469. return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
  1470. if (V->isUnknownOrUndef())
  1471. return *V;
  1472. // Other cases: give up. We are indexing into a larger object
  1473. // that has some value, but we don't know how to handle that yet.
  1474. return UnknownVal();
  1475. }
  1476. }
  1477. }
  1478. }
  1479. }
  1480. return getBindingForFieldOrElementCommon(B, R, R->getElementType());
  1481. }
  1482. SVal RegionStoreManager::getBindingForField(RegionBindingsConstRef B,
  1483. const FieldRegion* R) {
  1484. // Check if the region has a binding.
  1485. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1486. return *V;
  1487. // Is the field declared constant and has an in-class initializer?
  1488. const FieldDecl *FD = R->getDecl();
  1489. QualType Ty = FD->getType();
  1490. if (Ty.isConstQualified())
  1491. if (const Expr *Init = FD->getInClassInitializer())
  1492. if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
  1493. return *V;
  1494. // If the containing record was initialized, try to get its constant value.
  1495. const MemRegion* superR = R->getSuperRegion();
  1496. if (const auto *VR = dyn_cast<VarRegion>(superR)) {
  1497. const VarDecl *VD = VR->getDecl();
  1498. QualType RecordVarTy = VD->getType();
  1499. unsigned Index = FD->getFieldIndex();
  1500. // Either the record variable or the field has an initializer that we can
  1501. // trust. We trust initializers of constants and, additionally, respect
  1502. // initializers of globals when analyzing main().
  1503. if (RecordVarTy.isConstQualified() || Ty.isConstQualified() ||
  1504. (B.isMainAnalysis() && VD->hasGlobalStorage()))
  1505. if (const Expr *Init = VD->getAnyInitializer())
  1506. if (const auto *InitList = dyn_cast<InitListExpr>(Init)) {
  1507. if (Index < InitList->getNumInits()) {
  1508. if (const Expr *FieldInit = InitList->getInit(Index))
  1509. if (Optional<SVal> V = svalBuilder.getConstantVal(FieldInit))
  1510. return *V;
  1511. } else {
  1512. return svalBuilder.makeZeroVal(Ty);
  1513. }
  1514. }
  1515. }
  1516. return getBindingForFieldOrElementCommon(B, R, Ty);
  1517. }
  1518. Optional<SVal>
  1519. RegionStoreManager::getBindingForDerivedDefaultValue(RegionBindingsConstRef B,
  1520. const MemRegion *superR,
  1521. const TypedValueRegion *R,
  1522. QualType Ty) {
  1523. if (const Optional<SVal> &D = B.getDefaultBinding(superR)) {
  1524. const SVal &val = D.getValue();
  1525. if (SymbolRef parentSym = val.getAsSymbol())
  1526. return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
  1527. if (val.isZeroConstant())
  1528. return svalBuilder.makeZeroVal(Ty);
  1529. if (val.isUnknownOrUndef())
  1530. return val;
  1531. // Lazy bindings are usually handled through getExistingLazyBinding().
  1532. // We should unify these two code paths at some point.
  1533. if (val.getAs<nonloc::LazyCompoundVal>() ||
  1534. val.getAs<nonloc::CompoundVal>())
  1535. return val;
  1536. llvm_unreachable("Unknown default value");
  1537. }
  1538. return None;
  1539. }
  1540. SVal RegionStoreManager::getLazyBinding(const SubRegion *LazyBindingRegion,
  1541. RegionBindingsRef LazyBinding) {
  1542. SVal Result;
  1543. if (const ElementRegion *ER = dyn_cast<ElementRegion>(LazyBindingRegion))
  1544. Result = getBindingForElement(LazyBinding, ER);
  1545. else
  1546. Result = getBindingForField(LazyBinding,
  1547. cast<FieldRegion>(LazyBindingRegion));
  1548. // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
  1549. // default value for /part/ of an aggregate from a default value for the
  1550. // /entire/ aggregate. The most common case of this is when struct Outer
  1551. // has as its first member a struct Inner, which is copied in from a stack
  1552. // variable. In this case, even if the Outer's default value is symbolic, 0,
  1553. // or unknown, it gets overridden by the Inner's default value of undefined.
  1554. //
  1555. // This is a general problem -- if the Inner is zero-initialized, the Outer
  1556. // will now look zero-initialized. The proper way to solve this is with a
  1557. // new version of RegionStore that tracks the extent of a binding as well
  1558. // as the offset.
  1559. //
  1560. // This hack only takes care of the undefined case because that can very
  1561. // quickly result in a warning.
  1562. if (Result.isUndef())
  1563. Result = UnknownVal();
  1564. return Result;
  1565. }
  1566. SVal
  1567. RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B,
  1568. const TypedValueRegion *R,
  1569. QualType Ty) {
  1570. // At this point we have already checked in either getBindingForElement or
  1571. // getBindingForField if 'R' has a direct binding.
  1572. // Lazy binding?
  1573. Store lazyBindingStore = nullptr;
  1574. const SubRegion *lazyBindingRegion = nullptr;
  1575. std::tie(lazyBindingStore, lazyBindingRegion) = findLazyBinding(B, R, R);
  1576. if (lazyBindingRegion)
  1577. return getLazyBinding(lazyBindingRegion,
  1578. getRegionBindings(lazyBindingStore));
  1579. // Record whether or not we see a symbolic index. That can completely
  1580. // be out of scope of our lookup.
  1581. bool hasSymbolicIndex = false;
  1582. // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
  1583. // default value for /part/ of an aggregate from a default value for the
  1584. // /entire/ aggregate. The most common case of this is when struct Outer
  1585. // has as its first member a struct Inner, which is copied in from a stack
  1586. // variable. In this case, even if the Outer's default value is symbolic, 0,
  1587. // or unknown, it gets overridden by the Inner's default value of undefined.
  1588. //
  1589. // This is a general problem -- if the Inner is zero-initialized, the Outer
  1590. // will now look zero-initialized. The proper way to solve this is with a
  1591. // new version of RegionStore that tracks the extent of a binding as well
  1592. // as the offset.
  1593. //
  1594. // This hack only takes care of the undefined case because that can very
  1595. // quickly result in a warning.
  1596. bool hasPartialLazyBinding = false;
  1597. const SubRegion *SR = R;
  1598. while (SR) {
  1599. const MemRegion *Base = SR->getSuperRegion();
  1600. if (Optional<SVal> D = getBindingForDerivedDefaultValue(B, Base, R, Ty)) {
  1601. if (D->getAs<nonloc::LazyCompoundVal>()) {
  1602. hasPartialLazyBinding = true;
  1603. break;
  1604. }
  1605. return *D;
  1606. }
  1607. if (const ElementRegion *ER = dyn_cast<ElementRegion>(Base)) {
  1608. NonLoc index = ER->getIndex();
  1609. if (!index.isConstant())
  1610. hasSymbolicIndex = true;
  1611. }
  1612. // If our super region is a field or element itself, walk up the region
  1613. // hierarchy to see if there is a default value installed in an ancestor.
  1614. SR = dyn_cast<SubRegion>(Base);
  1615. }
  1616. if (R->hasStackNonParametersStorage()) {
  1617. if (isa<ElementRegion>(R)) {
  1618. // Currently we don't reason specially about Clang-style vectors. Check
  1619. // if superR is a vector and if so return Unknown.
  1620. if (const TypedValueRegion *typedSuperR =
  1621. dyn_cast<TypedValueRegion>(R->getSuperRegion())) {
  1622. if (typedSuperR->getValueType()->isVectorType())
  1623. return UnknownVal();
  1624. }
  1625. }
  1626. // FIXME: We also need to take ElementRegions with symbolic indexes into
  1627. // account. This case handles both directly accessing an ElementRegion
  1628. // with a symbolic offset, but also fields within an element with
  1629. // a symbolic offset.
  1630. if (hasSymbolicIndex)
  1631. return UnknownVal();
  1632. if (!hasPartialLazyBinding)
  1633. return UndefinedVal();
  1634. }
  1635. // All other values are symbolic.
  1636. return svalBuilder.getRegionValueSymbolVal(R);
  1637. }
  1638. SVal RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B,
  1639. const ObjCIvarRegion* R) {
  1640. // Check if the region has a binding.
  1641. if (const Optional<SVal> &V = B.getDirectBinding(R))
  1642. return *V;
  1643. const MemRegion *superR = R->getSuperRegion();
  1644. // Check if the super region has a default binding.
  1645. if (const Optional<SVal> &V = B.getDefaultBinding(superR)) {
  1646. if (SymbolRef parentSym = V->getAsSymbol())
  1647. return svalBuilder.getDerivedRegionValueSymbolVal(parentSym, R);
  1648. // Other cases: give up.
  1649. return UnknownVal();
  1650. }
  1651. return getBindingForLazySymbol(R);
  1652. }
  1653. SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B,
  1654. const VarRegion *R) {
  1655. // Check if the region has a binding.
  1656. if (Optional<SVal> V = B.getDirectBinding(R))
  1657. return *V;
  1658. if (Optional<SVal> V = B.getDefaultBinding(R))
  1659. return *V;
  1660. // Lazily derive a value for the VarRegion.
  1661. const VarDecl *VD = R->getDecl();
  1662. const MemSpaceRegion *MS = R->getMemorySpace();
  1663. // Arguments are always symbolic.
  1664. if (isa<StackArgumentsSpaceRegion>(MS))
  1665. return svalBuilder.getRegionValueSymbolVal(R);
  1666. // Is 'VD' declared constant? If so, retrieve the constant value.
  1667. if (VD->getType().isConstQualified()) {
  1668. if (const Expr *Init = VD->getAnyInitializer()) {
  1669. if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
  1670. return *V;
  1671. // If the variable is const qualified and has an initializer but
  1672. // we couldn't evaluate initializer to a value, treat the value as
  1673. // unknown.
  1674. return UnknownVal();
  1675. }
  1676. }
  1677. // This must come after the check for constants because closure-captured
  1678. // constant variables may appear in UnknownSpaceRegion.
  1679. if (isa<UnknownSpaceRegion>(MS))
  1680. return svalBuilder.getRegionValueSymbolVal(R);
  1681. if (isa<GlobalsSpaceRegion>(MS)) {
  1682. QualType T = VD->getType();
  1683. // If we're in main(), then global initializers have not become stale yet.
  1684. if (B.isMainAnalysis())
  1685. if (const Expr *Init = VD->getAnyInitializer())
  1686. if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
  1687. return *V;
  1688. // Function-scoped static variables are default-initialized to 0; if they
  1689. // have an initializer, it would have been processed by now.
  1690. // FIXME: This is only true when we're starting analysis from main().
  1691. // We're losing a lot of coverage here.
  1692. if (isa<StaticGlobalSpaceRegion>(MS))
  1693. return svalBuilder.makeZeroVal(T);
  1694. if (Optional<SVal> V = getBindingForDerivedDefaultValue(B, MS, R, T)) {
  1695. assert(!V->getAs<nonloc::LazyCompoundVal>());
  1696. return V.getValue();
  1697. }
  1698. return svalBuilder.getRegionValueSymbolVal(R);
  1699. }
  1700. return UndefinedVal();
  1701. }
  1702. SVal RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion *R) {
  1703. // All other values are symbolic.
  1704. return svalBuilder.getRegionValueSymbolVal(R);
  1705. }
  1706. const RegionStoreManager::SValListTy &
  1707. RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV) {
  1708. // First, check the cache.
  1709. LazyBindingsMapTy::iterator I = LazyBindingsMap.find(LCV.getCVData());
  1710. if (I != LazyBindingsMap.end())
  1711. return I->second;
  1712. // If we don't have a list of values cached, start constructing it.
  1713. SValListTy List;
  1714. const SubRegion *LazyR = LCV.getRegion();
  1715. RegionBindingsRef B = getRegionBindings(LCV.getStore());
  1716. // If this region had /no/ bindings at the time, there are no interesting
  1717. // values to return.
  1718. const ClusterBindings *Cluster = B.lookup(LazyR->getBaseRegion());
  1719. if (!Cluster)
  1720. return (LazyBindingsMap[LCV.getCVData()] = std::move(List));
  1721. SmallVector<BindingPair, 32> Bindings;
  1722. collectSubRegionBindings(Bindings, svalBuilder, *Cluster, LazyR,
  1723. /*IncludeAllDefaultBindings=*/true);
  1724. for (SmallVectorImpl<BindingPair>::const_iterator I = Bindings.begin(),
  1725. E = Bindings.end();
  1726. I != E; ++I) {
  1727. SVal V = I->second;
  1728. if (V.isUnknownOrUndef() || V.isConstant())
  1729. continue;
  1730. if (Optional<nonloc::LazyCompoundVal> InnerLCV =
  1731. V.getAs<nonloc::LazyCompoundVal>()) {
  1732. const SValListTy &InnerList = getInterestingValues(*InnerLCV);
  1733. List.insert(List.end(), InnerList.begin(), InnerList.end());
  1734. continue;
  1735. }
  1736. List.push_back(V);
  1737. }
  1738. return (LazyBindingsMap[LCV.getCVData()] = std::move(List));
  1739. }
  1740. NonLoc RegionStoreManager::createLazyBinding(RegionBindingsConstRef B,
  1741. const TypedValueRegion *R) {
  1742. if (Optional<nonloc::LazyCompoundVal> V =
  1743. getExistingLazyBinding(svalBuilder, B, R, false))
  1744. return *V;
  1745. return svalBuilder.makeLazyCompoundVal(StoreRef(B.asStore(), *this), R);
  1746. }
  1747. static bool isRecordEmpty(const RecordDecl *RD) {
  1748. if (!RD->field_empty())
  1749. return false;
  1750. if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD))
  1751. return CRD->getNumBases() == 0;
  1752. return true;
  1753. }
  1754. SVal RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B,
  1755. const TypedValueRegion *R) {
  1756. const RecordDecl *RD = R->getValueType()->castAs<RecordType>()->getDecl();
  1757. if (!RD->getDefinition() || isRecordEmpty(RD))
  1758. return UnknownVal();
  1759. return createLazyBinding(B, R);
  1760. }
  1761. SVal RegionStoreManager::getBindingForArray(RegionBindingsConstRef B,
  1762. const TypedValueRegion *R) {
  1763. assert(Ctx.getAsConstantArrayType(R->getValueType()) &&
  1764. "Only constant array types can have compound bindings.");
  1765. return createLazyBinding(B, R);
  1766. }
  1767. bool RegionStoreManager::includedInBindings(Store store,
  1768. const MemRegion *region) const {
  1769. RegionBindingsRef B = getRegionBindings(store);
  1770. region = region->getBaseRegion();
  1771. // Quick path: if the base is the head of a cluster, the region is live.
  1772. if (B.lookup(region))
  1773. return true;
  1774. // Slow path: if the region is the VALUE of any binding, it is live.
  1775. for (RegionBindingsRef::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
  1776. const ClusterBindings &Cluster = RI.getData();
  1777. for (ClusterBindings::iterator CI = Cluster.begin(), CE = Cluster.end();
  1778. CI != CE; ++CI) {
  1779. const SVal &D = CI.getData();
  1780. if (const MemRegion *R = D.getAsRegion())
  1781. if (R->getBaseRegion() == region)
  1782. return true;
  1783. }
  1784. }
  1785. return false;
  1786. }
  1787. //===----------------------------------------------------------------------===//
  1788. // Binding values to regions.
  1789. //===----------------------------------------------------------------------===//
  1790. StoreRef RegionStoreManager::killBinding(Store ST, Loc L) {
  1791. if (Optional<loc::MemRegionVal> LV = L.getAs<loc::MemRegionVal>())
  1792. if (const MemRegion* R = LV->getRegion())
  1793. return StoreRef(getRegionBindings(ST).removeBinding(R)
  1794. .asImmutableMap()
  1795. .getRootWithoutRetain(),
  1796. *this);
  1797. return StoreRef(ST, *this);
  1798. }
  1799. RegionBindingsRef
  1800. RegionStoreManager::bind(RegionBindingsConstRef B, Loc L, SVal V) {
  1801. if (L.getAs<loc::ConcreteInt>())
  1802. return B;
  1803. // If we get here, the location should be a region.
  1804. const MemRegion *R = L.castAs<loc::MemRegionVal>().getRegion();
  1805. // Check if the region is a struct region.
  1806. if (const TypedValueRegion* TR = dyn_cast<TypedValueRegion>(R)) {
  1807. QualType Ty = TR->getValueType();
  1808. if (Ty->isArrayType())
  1809. return bindArray(B, TR, V);
  1810. if (Ty->isStructureOrClassType())
  1811. return bindStruct(B, TR, V);
  1812. if (Ty->isVectorType())
  1813. return bindVector(B, TR, V);
  1814. if (Ty->isUnionType())
  1815. return bindAggregate(B, TR, V);
  1816. }
  1817. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
  1818. // Binding directly to a symbolic region should be treated as binding
  1819. // to element 0.
  1820. QualType T = SR->getSymbol()->getType();
  1821. if (T->isAnyPointerType() || T->isReferenceType())
  1822. T = T->getPointeeType();
  1823. R = GetElementZeroRegion(SR, T);
  1824. }
  1825. assert((!isa<CXXThisRegion>(R) || !B.lookup(R)) &&
  1826. "'this' pointer is not an l-value and is not assignable");
  1827. // Clear out bindings that may overlap with this binding.
  1828. RegionBindingsRef NewB = removeSubRegionBindings(B, cast<SubRegion>(R));
  1829. return NewB.addBinding(BindingKey::Make(R, BindingKey::Direct), V);
  1830. }
  1831. RegionBindingsRef
  1832. RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B,
  1833. const MemRegion *R,
  1834. QualType T) {
  1835. SVal V;
  1836. if (Loc::isLocType(T))
  1837. V = svalBuilder.makeNull();
  1838. else if (T->isIntegralOrEnumerationType())
  1839. V = svalBuilder.makeZeroVal(T);
  1840. else if (T->isStructureOrClassType() || T->isArrayType()) {
  1841. // Set the default value to a zero constant when it is a structure
  1842. // or array. The type doesn't really matter.
  1843. V = svalBuilder.makeZeroVal(Ctx.IntTy);
  1844. }
  1845. else {
  1846. // We can't represent values of this type, but we still need to set a value
  1847. // to record that the region has been initialized.
  1848. // If this assertion ever fires, a new case should be added above -- we
  1849. // should know how to default-initialize any value we can symbolicate.
  1850. assert(!SymbolManager::canSymbolicate(T) && "This type is representable");
  1851. V = UnknownVal();
  1852. }
  1853. return B.addBinding(R, BindingKey::Default, V);
  1854. }
  1855. RegionBindingsRef
  1856. RegionStoreManager::bindArray(RegionBindingsConstRef B,
  1857. const TypedValueRegion* R,
  1858. SVal Init) {
  1859. const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
  1860. QualType ElementTy = AT->getElementType();
  1861. Optional<uint64_t> Size;
  1862. if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
  1863. Size = CAT->getSize().getZExtValue();
  1864. // Check if the init expr is a literal. If so, bind the rvalue instead.
  1865. // FIXME: It's not responsibility of the Store to transform this lvalue
  1866. // to rvalue. ExprEngine or maybe even CFG should do this before binding.
  1867. if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
  1868. SVal V = getBinding(B.asStore(), *MRV, R->getValueType());
  1869. return bindAggregate(B, R, V);
  1870. }
  1871. // Handle lazy compound values.
  1872. if (Init.getAs<nonloc::LazyCompoundVal>())
  1873. return bindAggregate(B, R, Init);
  1874. if (Init.isUnknown())
  1875. return bindAggregate(B, R, UnknownVal());
  1876. // Remaining case: explicit compound values.
  1877. const nonloc::CompoundVal& CV = Init.castAs<nonloc::CompoundVal>();
  1878. nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
  1879. uint64_t i = 0;
  1880. RegionBindingsRef NewB(B);
  1881. for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
  1882. // The init list might be shorter than the array length.
  1883. if (VI == VE)
  1884. break;
  1885. const NonLoc &Idx = svalBuilder.makeArrayIndex(i);
  1886. const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
  1887. if (ElementTy->isStructureOrClassType())
  1888. NewB = bindStruct(NewB, ER, *VI);
  1889. else if (ElementTy->isArrayType())
  1890. NewB = bindArray(NewB, ER, *VI);
  1891. else
  1892. NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
  1893. }
  1894. // If the init list is shorter than the array length (or the array has
  1895. // variable length), set the array default value. Values that are already set
  1896. // are not overwritten.
  1897. if (!Size.hasValue() || i < Size.getValue())
  1898. NewB = setImplicitDefaultValue(NewB, R, ElementTy);
  1899. return NewB;
  1900. }
  1901. RegionBindingsRef RegionStoreManager::bindVector(RegionBindingsConstRef B,
  1902. const TypedValueRegion* R,
  1903. SVal V) {
  1904. QualType T = R->getValueType();
  1905. const VectorType *VT = T->castAs<VectorType>(); // Use castAs for typedefs.
  1906. // Handle lazy compound values and symbolic values.
  1907. if (V.getAs<nonloc::LazyCompoundVal>() || V.getAs<nonloc::SymbolVal>())
  1908. return bindAggregate(B, R, V);
  1909. // We may get non-CompoundVal accidentally due to imprecise cast logic or
  1910. // that we are binding symbolic struct value. Kill the field values, and if
  1911. // the value is symbolic go and bind it as a "default" binding.
  1912. if (!V.getAs<nonloc::CompoundVal>()) {
  1913. return bindAggregate(B, R, UnknownVal());
  1914. }
  1915. QualType ElemType = VT->getElementType();
  1916. nonloc::CompoundVal CV = V.castAs<nonloc::CompoundVal>();
  1917. nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
  1918. unsigned index = 0, numElements = VT->getNumElements();
  1919. RegionBindingsRef NewB(B);
  1920. for ( ; index != numElements ; ++index) {
  1921. if (VI == VE)
  1922. break;
  1923. NonLoc Idx = svalBuilder.makeArrayIndex(index);
  1924. const ElementRegion *ER = MRMgr.getElementRegion(ElemType, Idx, R, Ctx);
  1925. if (ElemType->isArrayType())
  1926. NewB = bindArray(NewB, ER, *VI);
  1927. else if (ElemType->isStructureOrClassType())
  1928. NewB = bindStruct(NewB, ER, *VI);
  1929. else
  1930. NewB = bind(NewB, loc::MemRegionVal(ER), *VI);
  1931. }
  1932. return NewB;
  1933. }
  1934. Optional<RegionBindingsRef>
  1935. RegionStoreManager::tryBindSmallStruct(RegionBindingsConstRef B,
  1936. const TypedValueRegion *R,
  1937. const RecordDecl *RD,
  1938. nonloc::LazyCompoundVal LCV) {
  1939. FieldVector Fields;
  1940. if (const CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(RD))
  1941. if (Class->getNumBases() != 0 || Class->getNumVBases() != 0)
  1942. return None;
  1943. for (const auto *FD : RD->fields()) {
  1944. if (FD->isUnnamedBitfield())
  1945. continue;
  1946. // If there are too many fields, or if any of the fields are aggregates,
  1947. // just use the LCV as a default binding.
  1948. if (Fields.size() == SmallStructLimit)
  1949. return None;
  1950. QualType Ty = FD->getType();
  1951. if (!(Ty->isScalarType() || Ty->isReferenceType()))
  1952. return None;
  1953. Fields.push_back(FD);
  1954. }
  1955. RegionBindingsRef NewB = B;
  1956. for (FieldVector::iterator I = Fields.begin(), E = Fields.end(); I != E; ++I){
  1957. const FieldRegion *SourceFR = MRMgr.getFieldRegion(*I, LCV.getRegion());
  1958. SVal V = getBindingForField(getRegionBindings(LCV.getStore()), SourceFR);
  1959. const FieldRegion *DestFR = MRMgr.getFieldRegion(*I, R);
  1960. NewB = bind(NewB, loc::MemRegionVal(DestFR), V);
  1961. }
  1962. return NewB;
  1963. }
  1964. RegionBindingsRef RegionStoreManager::bindStruct(RegionBindingsConstRef B,
  1965. const TypedValueRegion* R,
  1966. SVal V) {
  1967. if (!Features.supportsFields())
  1968. return B;
  1969. QualType T = R->getValueType();
  1970. assert(T->isStructureOrClassType());
  1971. const RecordType* RT = T->castAs<RecordType>();
  1972. const RecordDecl *RD = RT->getDecl();
  1973. if (!RD->isCompleteDefinition())
  1974. return B;
  1975. // Handle lazy compound values and symbolic values.
  1976. if (Optional<nonloc::LazyCompoundVal> LCV =
  1977. V.getAs<nonloc::LazyCompoundVal>()) {
  1978. if (Optional<RegionBindingsRef> NewB = tryBindSmallStruct(B, R, RD, *LCV))
  1979. return *NewB;
  1980. return bindAggregate(B, R, V);
  1981. }
  1982. if (V.getAs<nonloc::SymbolVal>())
  1983. return bindAggregate(B, R, V);
  1984. // We may get non-CompoundVal accidentally due to imprecise cast logic or
  1985. // that we are binding symbolic struct value. Kill the field values, and if
  1986. // the value is symbolic go and bind it as a "default" binding.
  1987. if (V.isUnknown() || !V.getAs<nonloc::CompoundVal>())
  1988. return bindAggregate(B, R, UnknownVal());
  1989. // The raw CompoundVal is essentially a symbolic InitListExpr: an (immutable)
  1990. // list of other values. It appears pretty much only when there's an actual
  1991. // initializer list expression in the program, and the analyzer tries to
  1992. // unwrap it as soon as possible.
  1993. // This code is where such unwrap happens: when the compound value is put into
  1994. // the object that it was supposed to initialize (it's an *initializer* list,
  1995. // after all), instead of binding the whole value to the whole object, we bind
  1996. // sub-values to sub-objects. Sub-values may themselves be compound values,
  1997. // and in this case the procedure becomes recursive.
  1998. // FIXME: The annoying part about compound values is that they don't carry
  1999. // any sort of information about which value corresponds to which sub-object.
  2000. // It's simply a list of values in the middle of nowhere; we expect to match
  2001. // them to sub-objects, essentially, "by index": first value binds to
  2002. // the first field, second value binds to the second field, etc.
  2003. // It would have been much safer to organize non-lazy compound values as
  2004. // a mapping from fields/bases to values.
  2005. const nonloc::CompoundVal& CV = V.castAs<nonloc::CompoundVal>();
  2006. nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
  2007. RegionBindingsRef NewB(B);
  2008. // In C++17 aggregates may have base classes, handle those as well.
  2009. // They appear before fields in the initializer list / compound value.
  2010. if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
  2011. // If the object was constructed with a constructor, its value is a
  2012. // LazyCompoundVal. If it's a raw CompoundVal, it means that we're
  2013. // performing aggregate initialization. The only exception from this
  2014. // rule is sending an Objective-C++ message that returns a C++ object
  2015. // to a nil receiver; in this case the semantics is to return a
  2016. // zero-initialized object even if it's a C++ object that doesn't have
  2017. // this sort of constructor; the CompoundVal is empty in this case.
  2018. assert((CRD->isAggregate() || (Ctx.getLangOpts().ObjC && VI == VE)) &&
  2019. "Non-aggregates are constructed with a constructor!");
  2020. for (const auto &B : CRD->bases()) {
  2021. // (Multiple inheritance is fine though.)
  2022. assert(!B.isVirtual() && "Aggregates cannot have virtual base classes!");
  2023. if (VI == VE)
  2024. break;
  2025. QualType BTy = B.getType();
  2026. assert(BTy->isStructureOrClassType() && "Base classes must be classes!");
  2027. const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl();
  2028. assert(BRD && "Base classes must be C++ classes!");
  2029. const CXXBaseObjectRegion *BR =
  2030. MRMgr.getCXXBaseObjectRegion(BRD, R, /*IsVirtual=*/false);
  2031. NewB = bindStruct(NewB, BR, *VI);
  2032. ++VI;
  2033. }
  2034. }
  2035. RecordDecl::field_iterator FI, FE;
  2036. for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI) {
  2037. if (VI == VE)
  2038. break;
  2039. // Skip any unnamed bitfields to stay in sync with the initializers.
  2040. if (FI->isUnnamedBitfield())
  2041. continue;
  2042. QualType FTy = FI->getType();
  2043. const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
  2044. if (FTy->isArrayType())
  2045. NewB = bindArray(NewB, FR, *VI);
  2046. else if (FTy->isStructureOrClassType())
  2047. NewB = bindStruct(NewB, FR, *VI);
  2048. else
  2049. NewB = bind(NewB, loc::MemRegionVal(FR), *VI);
  2050. ++VI;
  2051. }
  2052. // There may be fewer values in the initialize list than the fields of struct.
  2053. if (FI != FE) {
  2054. NewB = NewB.addBinding(R, BindingKey::Default,
  2055. svalBuilder.makeIntVal(0, false));
  2056. }
  2057. return NewB;
  2058. }
  2059. RegionBindingsRef
  2060. RegionStoreManager::bindAggregate(RegionBindingsConstRef B,
  2061. const TypedRegion *R,
  2062. SVal Val) {
  2063. // Remove the old bindings, using 'R' as the root of all regions
  2064. // we will invalidate. Then add the new binding.
  2065. return removeSubRegionBindings(B, R).addBinding(R, BindingKey::Default, Val);
  2066. }
  2067. //===----------------------------------------------------------------------===//
  2068. // State pruning.
  2069. //===----------------------------------------------------------------------===//
  2070. namespace {
  2071. class RemoveDeadBindingsWorker
  2072. : public ClusterAnalysis<RemoveDeadBindingsWorker> {
  2073. SmallVector<const SymbolicRegion *, 12> Postponed;
  2074. SymbolReaper &SymReaper;
  2075. const StackFrameContext *CurrentLCtx;
  2076. public:
  2077. RemoveDeadBindingsWorker(RegionStoreManager &rm,
  2078. ProgramStateManager &stateMgr,
  2079. RegionBindingsRef b, SymbolReaper &symReaper,
  2080. const StackFrameContext *LCtx)
  2081. : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
  2082. SymReaper(symReaper), CurrentLCtx(LCtx) {}
  2083. // Called by ClusterAnalysis.
  2084. void VisitAddedToCluster(const MemRegion *baseR, const ClusterBindings &C);
  2085. void VisitCluster(const MemRegion *baseR, const ClusterBindings *C);
  2086. using ClusterAnalysis<RemoveDeadBindingsWorker>::VisitCluster;
  2087. using ClusterAnalysis::AddToWorkList;
  2088. bool AddToWorkList(const MemRegion *R);
  2089. bool UpdatePostponed();
  2090. void VisitBinding(SVal V);
  2091. };
  2092. }
  2093. bool RemoveDeadBindingsWorker::AddToWorkList(const MemRegion *R) {
  2094. const MemRegion *BaseR = R->getBaseRegion();
  2095. return AddToWorkList(WorkListElement(BaseR), getCluster(BaseR));
  2096. }
  2097. void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
  2098. const ClusterBindings &C) {
  2099. if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
  2100. if (SymReaper.isLive(VR))
  2101. AddToWorkList(baseR, &C);
  2102. return;
  2103. }
  2104. if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
  2105. if (SymReaper.isLive(SR->getSymbol()))
  2106. AddToWorkList(SR, &C);
  2107. else
  2108. Postponed.push_back(SR);
  2109. return;
  2110. }
  2111. if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
  2112. AddToWorkList(baseR, &C);
  2113. return;
  2114. }
  2115. // CXXThisRegion in the current or parent location context is live.
  2116. if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
  2117. const auto *StackReg =
  2118. cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
  2119. const StackFrameContext *RegCtx = StackReg->getStackFrame();
  2120. if (CurrentLCtx &&
  2121. (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx)))
  2122. AddToWorkList(TR, &C);
  2123. }
  2124. }
  2125. void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
  2126. const ClusterBindings *C) {
  2127. if (!C)
  2128. return;
  2129. // Mark the symbol for any SymbolicRegion with live bindings as live itself.
  2130. // This means we should continue to track that symbol.
  2131. if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(baseR))
  2132. SymReaper.markLive(SymR->getSymbol());
  2133. for (ClusterBindings::iterator I = C->begin(), E = C->end(); I != E; ++I) {
  2134. // Element index of a binding key is live.
  2135. SymReaper.markElementIndicesLive(I.getKey().getRegion());
  2136. VisitBinding(I.getData());
  2137. }
  2138. }
  2139. void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
  2140. // Is it a LazyCompoundVal? All referenced regions are live as well.
  2141. if (Optional<nonloc::LazyCompoundVal> LCS =
  2142. V.getAs<nonloc::LazyCompoundVal>()) {
  2143. const RegionStoreManager::SValListTy &Vals = RM.getInterestingValues(*LCS);
  2144. for (RegionStoreManager::SValListTy::const_iterator I = Vals.begin(),
  2145. E = Vals.end();
  2146. I != E; ++I)
  2147. VisitBinding(*I);
  2148. return;
  2149. }
  2150. // If V is a region, then add it to the worklist.
  2151. if (const MemRegion *R = V.getAsRegion()) {
  2152. AddToWorkList(R);
  2153. SymReaper.markLive(R);
  2154. // All regions captured by a block are also live.
  2155. if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
  2156. BlockDataRegion::referenced_vars_iterator I = BR->referenced_vars_begin(),
  2157. E = BR->referenced_vars_end();
  2158. for ( ; I != E; ++I)
  2159. AddToWorkList(I.getCapturedRegion());
  2160. }
  2161. }
  2162. // Update the set of live symbols.
  2163. for (auto SI = V.symbol_begin(), SE = V.symbol_end(); SI!=SE; ++SI)
  2164. SymReaper.markLive(*SI);
  2165. }
  2166. bool RemoveDeadBindingsWorker::UpdatePostponed() {
  2167. // See if any postponed SymbolicRegions are actually live now, after
  2168. // having done a scan.
  2169. bool Changed = false;
  2170. for (auto I = Postponed.begin(), E = Postponed.end(); I != E; ++I) {
  2171. if (const SymbolicRegion *SR = *I) {
  2172. if (SymReaper.isLive(SR->getSymbol())) {
  2173. Changed |= AddToWorkList(SR);
  2174. *I = nullptr;
  2175. }
  2176. }
  2177. }
  2178. return Changed;
  2179. }
  2180. StoreRef RegionStoreManager::removeDeadBindings(Store store,
  2181. const StackFrameContext *LCtx,
  2182. SymbolReaper& SymReaper) {
  2183. RegionBindingsRef B = getRegionBindings(store);
  2184. RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
  2185. W.GenerateClusters();
  2186. // Enqueue the region roots onto the worklist.
  2187. for (SymbolReaper::region_iterator I = SymReaper.region_begin(),
  2188. E = SymReaper.region_end(); I != E; ++I) {
  2189. W.AddToWorkList(*I);
  2190. }
  2191. do W.RunWorkList(); while (W.UpdatePostponed());
  2192. // We have now scanned the store, marking reachable regions and symbols
  2193. // as live. We now remove all the regions that are dead from the store
  2194. // as well as update DSymbols with the set symbols that are now dead.
  2195. for (RegionBindingsRef::iterator I = B.begin(), E = B.end(); I != E; ++I) {
  2196. const MemRegion *Base = I.getKey();
  2197. // If the cluster has been visited, we know the region has been marked.
  2198. // Otherwise, remove the dead entry.
  2199. if (!W.isVisited(Base))
  2200. B = B.remove(Base);
  2201. }
  2202. return StoreRef(B.asStore(), *this);
  2203. }
  2204. //===----------------------------------------------------------------------===//
  2205. // Utility methods.
  2206. //===----------------------------------------------------------------------===//
  2207. void RegionStoreManager::printJson(raw_ostream &Out, Store S, const char *NL,
  2208. unsigned int Space, bool IsDot) const {
  2209. RegionBindingsRef Bindings = getRegionBindings(S);
  2210. Indent(Out, Space, IsDot) << "\"store\": ";
  2211. if (Bindings.isEmpty()) {
  2212. Out << "null," << NL;
  2213. return;
  2214. }
  2215. Out << "{ \"pointer\": \"" << Bindings.asStore() << "\", \"items\": [" << NL;
  2216. Bindings.printJson(Out, NL, Space + 1, IsDot);
  2217. Indent(Out, Space, IsDot) << "]}," << NL;
  2218. }