BasicValueFactory.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //===- BasicValueFactory.cpp - Basic values for Path Sens analysis --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines BasicValueFactory, a class that manages the lifetime
  10. // of APSInt objects and symbolic constraints used by ExprEngine
  11. // and related classes.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
  19. #include "llvm/ADT/APSInt.h"
  20. #include "llvm/ADT/FoldingSet.h"
  21. #include "llvm/ADT/ImmutableList.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include <cassert>
  24. #include <cstdint>
  25. #include <utility>
  26. using namespace clang;
  27. using namespace ento;
  28. void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
  29. llvm::ImmutableList<SVal> L) {
  30. T.Profile(ID);
  31. ID.AddPointer(L.getInternalPointer());
  32. }
  33. void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
  34. const StoreRef &store,
  35. const TypedValueRegion *region) {
  36. ID.AddPointer(store.getStore());
  37. ID.AddPointer(region);
  38. }
  39. void PointerToMemberData::Profile(
  40. llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
  41. llvm::ImmutableList<const CXXBaseSpecifier *> L) {
  42. ID.AddPointer(D);
  43. ID.AddPointer(L.getInternalPointer());
  44. }
  45. using SValData = std::pair<SVal, uintptr_t>;
  46. using SValPair = std::pair<SVal, SVal>;
  47. namespace llvm {
  48. template<> struct FoldingSetTrait<SValData> {
  49. static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
  50. X.first.Profile(ID);
  51. ID.AddPointer( (void*) X.second);
  52. }
  53. };
  54. template<> struct FoldingSetTrait<SValPair> {
  55. static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
  56. X.first.Profile(ID);
  57. X.second.Profile(ID);
  58. }
  59. };
  60. } // namespace llvm
  61. using PersistentSValsTy =
  62. llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData>>;
  63. using PersistentSValPairsTy =
  64. llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair>>;
  65. BasicValueFactory::~BasicValueFactory() {
  66. // Note that the dstor for the contents of APSIntSet will never be called,
  67. // so we iterate over the set and invoke the dstor for each APSInt. This
  68. // frees an aux. memory allocated to represent very large constants.
  69. for (const auto &I : APSIntSet)
  70. I.getValue().~APSInt();
  71. delete (PersistentSValsTy*) PersistentSVals;
  72. delete (PersistentSValPairsTy*) PersistentSValPairs;
  73. }
  74. const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
  75. llvm::FoldingSetNodeID ID;
  76. void *InsertPos;
  77. using FoldNodeTy = llvm::FoldingSetNodeWrapper<llvm::APSInt>;
  78. X.Profile(ID);
  79. FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
  80. if (!P) {
  81. P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
  82. new (P) FoldNodeTy(X);
  83. APSIntSet.InsertNode(P, InsertPos);
  84. }
  85. return *P;
  86. }
  87. const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
  88. bool isUnsigned) {
  89. llvm::APSInt V(X, isUnsigned);
  90. return getValue(V);
  91. }
  92. const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
  93. bool isUnsigned) {
  94. llvm::APSInt V(BitWidth, isUnsigned);
  95. V = X;
  96. return getValue(V);
  97. }
  98. const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
  99. return getValue(getAPSIntType(T).getValue(X));
  100. }
  101. const CompoundValData*
  102. BasicValueFactory::getCompoundValData(QualType T,
  103. llvm::ImmutableList<SVal> Vals) {
  104. llvm::FoldingSetNodeID ID;
  105. CompoundValData::Profile(ID, T, Vals);
  106. void *InsertPos;
  107. CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
  108. if (!D) {
  109. D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
  110. new (D) CompoundValData(T, Vals);
  111. CompoundValDataSet.InsertNode(D, InsertPos);
  112. }
  113. return D;
  114. }
  115. const LazyCompoundValData*
  116. BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
  117. const TypedValueRegion *region) {
  118. llvm::FoldingSetNodeID ID;
  119. LazyCompoundValData::Profile(ID, store, region);
  120. void *InsertPos;
  121. LazyCompoundValData *D =
  122. LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
  123. if (!D) {
  124. D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
  125. new (D) LazyCompoundValData(store, region);
  126. LazyCompoundValDataSet.InsertNode(D, InsertPos);
  127. }
  128. return D;
  129. }
  130. const PointerToMemberData *BasicValueFactory::getPointerToMemberData(
  131. const DeclaratorDecl *DD, llvm::ImmutableList<const CXXBaseSpecifier *> L) {
  132. llvm::FoldingSetNodeID ID;
  133. PointerToMemberData::Profile(ID, DD, L);
  134. void *InsertPos;
  135. PointerToMemberData *D =
  136. PointerToMemberDataSet.FindNodeOrInsertPos(ID, InsertPos);
  137. if (!D) {
  138. D = (PointerToMemberData*) BPAlloc.Allocate<PointerToMemberData>();
  139. new (D) PointerToMemberData(DD, L);
  140. PointerToMemberDataSet.InsertNode(D, InsertPos);
  141. }
  142. return D;
  143. }
  144. const PointerToMemberData *BasicValueFactory::accumCXXBase(
  145. llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
  146. const nonloc::PointerToMember &PTM) {
  147. nonloc::PointerToMember::PTMDataType PTMDT = PTM.getPTMData();
  148. const DeclaratorDecl *DD = nullptr;
  149. llvm::ImmutableList<const CXXBaseSpecifier *> PathList;
  150. if (PTMDT.isNull() || PTMDT.is<const DeclaratorDecl *>()) {
  151. if (PTMDT.is<const DeclaratorDecl *>())
  152. DD = PTMDT.get<const DeclaratorDecl *>();
  153. PathList = CXXBaseListFactory.getEmptyList();
  154. } else { // const PointerToMemberData *
  155. const PointerToMemberData *PTMD =
  156. PTMDT.get<const PointerToMemberData *>();
  157. DD = PTMD->getDeclaratorDecl();
  158. PathList = PTMD->getCXXBaseList();
  159. }
  160. for (const auto &I : llvm::reverse(PathRange))
  161. PathList = prependCXXBase(I, PathList);
  162. return getPointerToMemberData(DD, PathList);
  163. }
  164. const llvm::APSInt*
  165. BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
  166. const llvm::APSInt& V1, const llvm::APSInt& V2) {
  167. switch (Op) {
  168. default:
  169. llvm_unreachable("Invalid Opcode.");
  170. case BO_Mul:
  171. return &getValue( V1 * V2 );
  172. case BO_Div:
  173. if (V2 == 0) // Avoid division by zero
  174. return nullptr;
  175. return &getValue( V1 / V2 );
  176. case BO_Rem:
  177. if (V2 == 0) // Avoid division by zero
  178. return nullptr;
  179. return &getValue( V1 % V2 );
  180. case BO_Add:
  181. return &getValue( V1 + V2 );
  182. case BO_Sub:
  183. return &getValue( V1 - V2 );
  184. case BO_Shl: {
  185. // FIXME: This logic should probably go higher up, where we can
  186. // test these conditions symbolically.
  187. if (V2.isSigned() && V2.isNegative())
  188. return nullptr;
  189. uint64_t Amt = V2.getZExtValue();
  190. if (Amt >= V1.getBitWidth())
  191. return nullptr;
  192. if (!Ctx.getLangOpts().CPlusPlus2a) {
  193. if (V1.isSigned() && V1.isNegative())
  194. return nullptr;
  195. if (V1.isSigned() && Amt > V1.countLeadingZeros())
  196. return nullptr;
  197. }
  198. return &getValue( V1.operator<<( (unsigned) Amt ));
  199. }
  200. case BO_Shr: {
  201. // FIXME: This logic should probably go higher up, where we can
  202. // test these conditions symbolically.
  203. if (V2.isSigned() && V2.isNegative())
  204. return nullptr;
  205. uint64_t Amt = V2.getZExtValue();
  206. if (Amt >= V1.getBitWidth())
  207. return nullptr;
  208. return &getValue( V1.operator>>( (unsigned) Amt ));
  209. }
  210. case BO_LT:
  211. return &getTruthValue( V1 < V2 );
  212. case BO_GT:
  213. return &getTruthValue( V1 > V2 );
  214. case BO_LE:
  215. return &getTruthValue( V1 <= V2 );
  216. case BO_GE:
  217. return &getTruthValue( V1 >= V2 );
  218. case BO_EQ:
  219. return &getTruthValue( V1 == V2 );
  220. case BO_NE:
  221. return &getTruthValue( V1 != V2 );
  222. // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
  223. case BO_And:
  224. return &getValue( V1 & V2 );
  225. case BO_Or:
  226. return &getValue( V1 | V2 );
  227. case BO_Xor:
  228. return &getValue( V1 ^ V2 );
  229. }
  230. }
  231. const std::pair<SVal, uintptr_t>&
  232. BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
  233. // Lazily create the folding set.
  234. if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
  235. llvm::FoldingSetNodeID ID;
  236. void *InsertPos;
  237. V.Profile(ID);
  238. ID.AddPointer((void*) Data);
  239. PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
  240. using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValData>;
  241. FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
  242. if (!P) {
  243. P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
  244. new (P) FoldNodeTy(std::make_pair(V, Data));
  245. Map.InsertNode(P, InsertPos);
  246. }
  247. return P->getValue();
  248. }
  249. const std::pair<SVal, SVal>&
  250. BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
  251. // Lazily create the folding set.
  252. if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
  253. llvm::FoldingSetNodeID ID;
  254. void *InsertPos;
  255. V1.Profile(ID);
  256. V2.Profile(ID);
  257. PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
  258. using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValPair>;
  259. FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
  260. if (!P) {
  261. P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
  262. new (P) FoldNodeTy(std::make_pair(V1, V2));
  263. Map.InsertNode(P, InsertPos);
  264. }
  265. return P->getValue();
  266. }
  267. const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
  268. return &getPersistentSValWithData(X, 0).first;
  269. }