UninitializedObject.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //===----- UninitializedObject.h ---------------------------------*- 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 helper classes for UninitializedObjectChecker and
  10. // documentation about the logic of it.
  11. //
  12. // The checker reports uninitialized fields in objects created after a
  13. // constructor call.
  14. //
  15. // This checker has several options:
  16. // - "Pedantic" (boolean). If its not set or is set to false, the checker
  17. // won't emit warnings for objects that don't have at least one initialized
  18. // field. This may be set with
  19. //
  20. // `-analyzer-config optin.cplusplus.UninitializedObject:Pedantic=true`.
  21. //
  22. // - "NotesAsWarnings" (boolean). If set to true, the checker will emit a
  23. // warning for each uninitialized field, as opposed to emitting one warning
  24. // per constructor call, and listing the uninitialized fields that belongs
  25. // to it in notes. Defaults to false.
  26. //
  27. // `-analyzer-config \
  28. // optin.cplusplus.UninitializedObject:NotesAsWarnings=true`.
  29. //
  30. // - "CheckPointeeInitialization" (boolean). If set to false, the checker will
  31. // not analyze the pointee of pointer/reference fields, and will only check
  32. // whether the object itself is initialized. Defaults to false.
  33. //
  34. // `-analyzer-config \
  35. // optin.cplusplus.UninitializedObject:CheckPointeeInitialization=true`.
  36. //
  37. // TODO: With some clever heuristics, some pointers should be dereferenced
  38. // by default. For example, if the pointee is constructed within the
  39. // constructor call, it's reasonable to say that no external object
  40. // references it, and we wouldn't generate multiple report on the same
  41. // pointee.
  42. //
  43. // - "IgnoreRecordsWithField" (string). If supplied, the checker will not
  44. // analyze structures that have a field with a name or type name that
  45. // matches the given pattern. Defaults to "".
  46. //
  47. // `-analyzer-config \
  48. // optin.cplusplus.UninitializedObject:IgnoreRecordsWithField="[Tt]ag|[Kk]ind"`.
  49. //
  50. // - "IgnoreGuardedFields" (boolean). If set to true, the checker will analyze
  51. // _syntactically_ whether the found uninitialized object is used without a
  52. // preceding assert call. Defaults to false.
  53. //
  54. // `-analyzer-config \
  55. // optin.cplusplus.UninitializedObject:IgnoreGuardedFields=true`.
  56. //
  57. // Most of the following methods as well as the checker itself is defined in
  58. // UninitializedObjectChecker.cpp.
  59. //
  60. // Some methods are implemented in UninitializedPointee.cpp, to reduce the
  61. // complexity of the main checker file.
  62. //
  63. //===----------------------------------------------------------------------===//
  64. #ifndef LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
  65. #define LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H
  66. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  67. namespace clang {
  68. namespace ento {
  69. struct UninitObjCheckerOptions {
  70. bool IsPedantic = false;
  71. bool ShouldConvertNotesToWarnings = false;
  72. bool CheckPointeeInitialization = false;
  73. std::string IgnoredRecordsWithFieldPattern;
  74. bool IgnoreGuardedFields = false;
  75. };
  76. /// A lightweight polymorphic wrapper around FieldRegion *. We'll use this
  77. /// interface to store addinitional information about fields. As described
  78. /// later, a list of these objects (i.e. "fieldchain") will be constructed and
  79. /// used for printing note messages should an uninitialized value be found.
  80. class FieldNode {
  81. protected:
  82. const FieldRegion *FR;
  83. /// FieldNodes are never meant to be created on the heap, see
  84. /// FindUninitializedFields::addFieldToUninits().
  85. /* non-virtual */ ~FieldNode() = default;
  86. public:
  87. FieldNode(const FieldRegion *FR) : FR(FR) {}
  88. // We'll delete all of these special member functions to force the users of
  89. // this interface to only store references to FieldNode objects in containers.
  90. FieldNode() = delete;
  91. FieldNode(const FieldNode &) = delete;
  92. FieldNode(FieldNode &&) = delete;
  93. FieldNode &operator=(const FieldNode &) = delete;
  94. FieldNode &operator=(const FieldNode &&) = delete;
  95. void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddPointer(this); }
  96. /// Helper method for uniqueing.
  97. bool isSameRegion(const FieldRegion *OtherFR) const {
  98. // Special FieldNode descendants may wrap nullpointers (for example if they
  99. // describe a special relationship between two elements of the fieldchain)
  100. // -- we wouldn't like to unique these objects.
  101. if (FR == nullptr)
  102. return false;
  103. return FR == OtherFR;
  104. }
  105. const FieldRegion *getRegion() const { return FR; }
  106. const FieldDecl *getDecl() const {
  107. assert(FR);
  108. return FR->getDecl();
  109. }
  110. // When a fieldchain is printed, it will have the following format (without
  111. // newline, indices are in order of insertion, from 1 to n):
  112. //
  113. // <note_message_n>'<prefix_n><prefix_n-1>...<prefix_1>
  114. // this-><node_1><separator_1><node_2><separator_2>...<node_n>'
  115. /// If this is the last element of the fieldchain, this method will print the
  116. /// note message associated with it.
  117. /// The note message should state something like "uninitialized field" or
  118. /// "uninitialized pointee" etc.
  119. virtual void printNoteMsg(llvm::raw_ostream &Out) const = 0;
  120. /// Print any prefixes before the fieldchain. Could contain casts, etc.
  121. virtual void printPrefix(llvm::raw_ostream &Out) const = 0;
  122. /// Print the node. Should contain the name of the field stored in FR.
  123. virtual void printNode(llvm::raw_ostream &Out) const = 0;
  124. /// Print the separator. For example, fields may be separated with '.' or
  125. /// "->".
  126. virtual void printSeparator(llvm::raw_ostream &Out) const = 0;
  127. virtual bool isBase() const { return false; }
  128. };
  129. /// Returns with Field's name. This is a helper function to get the correct name
  130. /// even if Field is a captured lambda variable.
  131. std::string getVariableName(const FieldDecl *Field);
  132. /// Represents a field chain. A field chain is a list of fields where the first
  133. /// element of the chain is the object under checking (not stored), and every
  134. /// other element is a field, and the element that precedes it is the object
  135. /// that contains it.
  136. ///
  137. /// Note that this class is immutable (essentially a wrapper around an
  138. /// ImmutableList), new FieldChainInfo objects may be created by member
  139. /// functions such as add() and replaceHead().
  140. class FieldChainInfo {
  141. public:
  142. using FieldChain = llvm::ImmutableList<const FieldNode &>;
  143. private:
  144. FieldChain::Factory &ChainFactory;
  145. FieldChain Chain;
  146. FieldChainInfo(FieldChain::Factory &F, FieldChain NewChain)
  147. : FieldChainInfo(F) {
  148. Chain = NewChain;
  149. }
  150. public:
  151. FieldChainInfo() = delete;
  152. FieldChainInfo(FieldChain::Factory &F) : ChainFactory(F) {}
  153. FieldChainInfo(const FieldChainInfo &Other) = default;
  154. /// Constructs a new FieldChainInfo object with \p FN appended.
  155. template <class FieldNodeT> FieldChainInfo add(const FieldNodeT &FN);
  156. /// Constructs a new FieldChainInfo object with \p FN as the new head of the
  157. /// list.
  158. template <class FieldNodeT> FieldChainInfo replaceHead(const FieldNodeT &FN);
  159. bool contains(const FieldRegion *FR) const;
  160. bool isEmpty() const { return Chain.isEmpty(); }
  161. const FieldNode &getHead() const { return Chain.getHead(); }
  162. const FieldRegion *getUninitRegion() const { return getHead().getRegion(); }
  163. void printNoteMsg(llvm::raw_ostream &Out) const;
  164. };
  165. using UninitFieldMap = std::map<const FieldRegion *, llvm::SmallString<50>>;
  166. /// Searches for and stores uninitialized fields in a non-union object.
  167. class FindUninitializedFields {
  168. ProgramStateRef State;
  169. const TypedValueRegion *const ObjectR;
  170. const UninitObjCheckerOptions Opts;
  171. bool IsAnyFieldInitialized = false;
  172. FieldChainInfo::FieldChain::Factory ChainFactory;
  173. /// A map for assigning uninitialized regions to note messages. For example,
  174. ///
  175. /// struct A {
  176. /// int x;
  177. /// };
  178. ///
  179. /// A a;
  180. ///
  181. /// After analyzing `a`, the map will contain a pair for `a.x`'s region and
  182. /// the note message "uninitialized field 'this->x'.
  183. UninitFieldMap UninitFields;
  184. public:
  185. /// Constructs the FindUninitializedField object, searches for and stores
  186. /// uninitialized fields in R.
  187. FindUninitializedFields(ProgramStateRef State,
  188. const TypedValueRegion *const R,
  189. const UninitObjCheckerOptions &Opts);
  190. /// Returns with the modified state and a map of (uninitialized region,
  191. /// note message) pairs.
  192. std::pair<ProgramStateRef, const UninitFieldMap &> getResults() {
  193. return {State, UninitFields};
  194. }
  195. /// Returns whether the analyzed region contains at least one initialized
  196. /// field. Note that this includes subfields as well, not just direct ones,
  197. /// and will return false if an uninitialized pointee is found with
  198. /// CheckPointeeInitialization enabled.
  199. bool isAnyFieldInitialized() { return IsAnyFieldInitialized; }
  200. private:
  201. // For the purposes of this checker, we'll regard the analyzed region as a
  202. // directed tree, where
  203. // * the root is the object under checking
  204. // * every node is an object that is
  205. // - a union
  206. // - a non-union record
  207. // - dereferenceable (see isDereferencableType())
  208. // - an array
  209. // - of a primitive type (see isPrimitiveType())
  210. // * the parent of each node is the object that contains it
  211. // * every leaf is an array, a primitive object, a nullptr or an undefined
  212. // pointer.
  213. //
  214. // Example:
  215. //
  216. // struct A {
  217. // struct B {
  218. // int x, y = 0;
  219. // };
  220. // B b;
  221. // int *iptr = new int;
  222. // B* bptr;
  223. //
  224. // A() {}
  225. // };
  226. //
  227. // The directed tree:
  228. //
  229. // ->x
  230. // /
  231. // ->b--->y
  232. // /
  233. // A-->iptr->(int value)
  234. // \
  235. // ->bptr
  236. //
  237. // From this we'll construct a vector of fieldchains, where each fieldchain
  238. // represents an uninitialized field. An uninitialized field may be a
  239. // primitive object, a pointer, a pointee or a union without a single
  240. // initialized field.
  241. // In the above example, for the default constructor call we'll end up with
  242. // these fieldchains:
  243. //
  244. // this->b.x
  245. // this->iptr (pointee uninit)
  246. // this->bptr (pointer uninit)
  247. //
  248. // We'll traverse each node of the above graph with the appropriate one of
  249. // these methods:
  250. /// Checks the region of a union object, and returns true if no field is
  251. /// initialized within the region.
  252. bool isUnionUninit(const TypedValueRegion *R);
  253. /// Checks a region of a non-union object, and returns true if an
  254. /// uninitialized field is found within the region.
  255. bool isNonUnionUninit(const TypedValueRegion *R, FieldChainInfo LocalChain);
  256. /// Checks a region of a pointer or reference object, and returns true if the
  257. /// ptr/ref object itself or any field within the pointee's region is
  258. /// uninitialized.
  259. bool isDereferencableUninit(const FieldRegion *FR, FieldChainInfo LocalChain);
  260. /// Returns true if the value of a primitive object is uninitialized.
  261. bool isPrimitiveUninit(const SVal &V);
  262. // Note that we don't have a method for arrays -- the elements of an array are
  263. // often left uninitialized intentionally even when it is of a C++ record
  264. // type, so we'll assume that an array is always initialized.
  265. // TODO: Add a support for nonloc::LocAsInteger.
  266. /// Processes LocalChain and attempts to insert it into UninitFields. Returns
  267. /// true on success. Also adds the head of the list and \p PointeeR (if
  268. /// supplied) to the GDM as already analyzed objects.
  269. ///
  270. /// Since this class analyzes regions with recursion, we'll only store
  271. /// references to temporary FieldNode objects created on the stack. This means
  272. /// that after analyzing a leaf of the directed tree described above, the
  273. /// elements LocalChain references will be destructed, so we can't store it
  274. /// directly.
  275. bool addFieldToUninits(FieldChainInfo LocalChain,
  276. const MemRegion *PointeeR = nullptr);
  277. };
  278. /// Returns true if T is a primitive type. An object of a primitive type only
  279. /// needs to be analyzed as much as checking whether their value is undefined.
  280. inline bool isPrimitiveType(const QualType &T) {
  281. return T->isBuiltinType() || T->isEnumeralType() ||
  282. T->isFunctionType() || T->isAtomicType() ||
  283. T->isVectorType() || T->isScalarType();
  284. }
  285. inline bool isDereferencableType(const QualType &T) {
  286. return T->isAnyPointerType() || T->isReferenceType();
  287. }
  288. // Template method definitions.
  289. template <class FieldNodeT>
  290. inline FieldChainInfo FieldChainInfo::add(const FieldNodeT &FN) {
  291. assert(!contains(FN.getRegion()) &&
  292. "Can't add a field that is already a part of the "
  293. "fieldchain! Is this a cyclic reference?");
  294. FieldChainInfo NewChain = *this;
  295. NewChain.Chain = ChainFactory.add(FN, Chain);
  296. return NewChain;
  297. }
  298. template <class FieldNodeT>
  299. inline FieldChainInfo FieldChainInfo::replaceHead(const FieldNodeT &FN) {
  300. FieldChainInfo NewChain(ChainFactory, Chain.getTail());
  301. return NewChain.add(FN);
  302. }
  303. } // end of namespace ento
  304. } // end of namespace clang
  305. #endif // LLVM_CLANG_STATICANALYZER_UNINITIALIZEDOBJECT_H