InnerPointerChecker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //=== InnerPointerChecker.cpp -------------------------------------*- 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 check that marks a raw pointer to a C++ container's
  10. // inner buffer released when the object is destroyed. This information can
  11. // be used by MallocChecker to detect use-after-free problems.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "AllocationState.h"
  15. #include "InterCheckerAPI.h"
  16. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  17. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  18. #include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h"
  19. #include "clang/StaticAnalyzer/Core/Checker.h"
  20. #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
  21. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  22. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  23. using namespace clang;
  24. using namespace ento;
  25. // Associate container objects with a set of raw pointer symbols.
  26. REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(PtrSet, SymbolRef)
  27. REGISTER_MAP_WITH_PROGRAMSTATE(RawPtrMap, const MemRegion *, PtrSet)
  28. namespace {
  29. class InnerPointerChecker
  30. : public Checker<check::DeadSymbols, check::PostCall> {
  31. CallDescription AppendFn, AssignFn, AddressofFn, ClearFn, CStrFn, DataFn,
  32. DataMemberFn, EraseFn, InsertFn, PopBackFn, PushBackFn, ReplaceFn,
  33. ReserveFn, ResizeFn, ShrinkToFitFn, SwapFn;
  34. public:
  35. class InnerPointerBRVisitor : public BugReporterVisitor {
  36. SymbolRef PtrToBuf;
  37. public:
  38. InnerPointerBRVisitor(SymbolRef Sym) : PtrToBuf(Sym) {}
  39. static void *getTag() {
  40. static int Tag = 0;
  41. return &Tag;
  42. }
  43. void Profile(llvm::FoldingSetNodeID &ID) const override {
  44. ID.AddPointer(getTag());
  45. }
  46. virtual PathDiagnosticPieceRef
  47. VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
  48. PathSensitiveBugReport &BR) override;
  49. // FIXME: Scan the map once in the visitor's constructor and do a direct
  50. // lookup by region.
  51. bool isSymbolTracked(ProgramStateRef State, SymbolRef Sym) {
  52. RawPtrMapTy Map = State->get<RawPtrMap>();
  53. for (const auto &Entry : Map) {
  54. if (Entry.second.contains(Sym))
  55. return true;
  56. }
  57. return false;
  58. }
  59. };
  60. InnerPointerChecker()
  61. : AppendFn({"std", "basic_string", "append"}),
  62. AssignFn({"std", "basic_string", "assign"}),
  63. AddressofFn({"std", "addressof"}),
  64. ClearFn({"std", "basic_string", "clear"}),
  65. CStrFn({"std", "basic_string", "c_str"}), DataFn({"std", "data"}, 1),
  66. DataMemberFn({"std", "basic_string", "data"}),
  67. EraseFn({"std", "basic_string", "erase"}),
  68. InsertFn({"std", "basic_string", "insert"}),
  69. PopBackFn({"std", "basic_string", "pop_back"}),
  70. PushBackFn({"std", "basic_string", "push_back"}),
  71. ReplaceFn({"std", "basic_string", "replace"}),
  72. ReserveFn({"std", "basic_string", "reserve"}),
  73. ResizeFn({"std", "basic_string", "resize"}),
  74. ShrinkToFitFn({"std", "basic_string", "shrink_to_fit"}),
  75. SwapFn({"std", "basic_string", "swap"}) {}
  76. /// Check whether the called member function potentially invalidates
  77. /// pointers referring to the container object's inner buffer.
  78. bool isInvalidatingMemberFunction(const CallEvent &Call) const;
  79. /// Check whether the called function returns a raw inner pointer.
  80. bool isInnerPointerAccessFunction(const CallEvent &Call) const;
  81. /// Mark pointer symbols associated with the given memory region released
  82. /// in the program state.
  83. void markPtrSymbolsReleased(const CallEvent &Call, ProgramStateRef State,
  84. const MemRegion *ObjRegion,
  85. CheckerContext &C) const;
  86. /// Standard library functions that take a non-const `basic_string` argument by
  87. /// reference may invalidate its inner pointers. Check for these cases and
  88. /// mark the pointers released.
  89. void checkFunctionArguments(const CallEvent &Call, ProgramStateRef State,
  90. CheckerContext &C) const;
  91. /// Record the connection between raw pointers referring to a container
  92. /// object's inner buffer and the object's memory region in the program state.
  93. /// Mark potentially invalidated pointers released.
  94. void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
  95. /// Clean up the program state map.
  96. void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
  97. };
  98. } // end anonymous namespace
  99. bool InnerPointerChecker::isInvalidatingMemberFunction(
  100. const CallEvent &Call) const {
  101. if (const auto *MemOpCall = dyn_cast<CXXMemberOperatorCall>(&Call)) {
  102. OverloadedOperatorKind Opc = MemOpCall->getOriginExpr()->getOperator();
  103. if (Opc == OO_Equal || Opc == OO_PlusEqual)
  104. return true;
  105. return false;
  106. }
  107. return isa<CXXDestructorCall>(Call) ||
  108. matchesAny(Call, AppendFn, AssignFn, ClearFn, EraseFn, InsertFn,
  109. PopBackFn, PushBackFn, ReplaceFn, ReserveFn, ResizeFn,
  110. ShrinkToFitFn, SwapFn);
  111. }
  112. bool InnerPointerChecker::isInnerPointerAccessFunction(
  113. const CallEvent &Call) const {
  114. return matchesAny(Call, CStrFn, DataFn, DataMemberFn);
  115. }
  116. void InnerPointerChecker::markPtrSymbolsReleased(const CallEvent &Call,
  117. ProgramStateRef State,
  118. const MemRegion *MR,
  119. CheckerContext &C) const {
  120. if (const PtrSet *PS = State->get<RawPtrMap>(MR)) {
  121. const Expr *Origin = Call.getOriginExpr();
  122. for (const auto Symbol : *PS) {
  123. // NOTE: `Origin` may be null, and will be stored so in the symbol's
  124. // `RefState` in MallocChecker's `RegionState` program state map.
  125. State = allocation_state::markReleased(State, Symbol, Origin);
  126. }
  127. State = State->remove<RawPtrMap>(MR);
  128. C.addTransition(State);
  129. return;
  130. }
  131. }
  132. void InnerPointerChecker::checkFunctionArguments(const CallEvent &Call,
  133. ProgramStateRef State,
  134. CheckerContext &C) const {
  135. if (const auto *FC = dyn_cast<AnyFunctionCall>(&Call)) {
  136. const FunctionDecl *FD = FC->getDecl();
  137. if (!FD || !FD->isInStdNamespace())
  138. return;
  139. for (unsigned I = 0, E = FD->getNumParams(); I != E; ++I) {
  140. QualType ParamTy = FD->getParamDecl(I)->getType();
  141. if (!ParamTy->isReferenceType() ||
  142. ParamTy->getPointeeType().isConstQualified())
  143. continue;
  144. // In case of member operator calls, `this` is counted as an
  145. // argument but not as a parameter.
  146. bool isaMemberOpCall = isa<CXXMemberOperatorCall>(FC);
  147. unsigned ArgI = isaMemberOpCall ? I+1 : I;
  148. SVal Arg = FC->getArgSVal(ArgI);
  149. const auto *ArgRegion =
  150. dyn_cast_or_null<TypedValueRegion>(Arg.getAsRegion());
  151. if (!ArgRegion)
  152. continue;
  153. // std::addressof function accepts a non-const reference as an argument,
  154. // but doesn't modify it.
  155. if (AddressofFn.matches(Call))
  156. continue;
  157. markPtrSymbolsReleased(Call, State, ArgRegion, C);
  158. }
  159. }
  160. }
  161. // [string.require]
  162. //
  163. // "References, pointers, and iterators referring to the elements of a
  164. // basic_string sequence may be invalidated by the following uses of that
  165. // basic_string object:
  166. //
  167. // -- As an argument to any standard library function taking a reference
  168. // to non-const basic_string as an argument. For example, as an argument to
  169. // non-member functions swap(), operator>>(), and getline(), or as an argument
  170. // to basic_string::swap().
  171. //
  172. // -- Calling non-const member functions, except operator[], at, front, back,
  173. // begin, rbegin, end, and rend."
  174. void InnerPointerChecker::checkPostCall(const CallEvent &Call,
  175. CheckerContext &C) const {
  176. ProgramStateRef State = C.getState();
  177. // TODO: Do we need these to be typed?
  178. const TypedValueRegion *ObjRegion = nullptr;
  179. if (const auto *ICall = dyn_cast<CXXInstanceCall>(&Call)) {
  180. ObjRegion = dyn_cast_or_null<TypedValueRegion>(
  181. ICall->getCXXThisVal().getAsRegion());
  182. // Check [string.require] / second point.
  183. if (isInvalidatingMemberFunction(Call)) {
  184. markPtrSymbolsReleased(Call, State, ObjRegion, C);
  185. return;
  186. }
  187. }
  188. if (isInnerPointerAccessFunction(Call)) {
  189. if (isa<SimpleFunctionCall>(Call)) {
  190. // NOTE: As of now, we only have one free access function: std::data.
  191. // If we add more functions like this in the list, hardcoded
  192. // argument index should be changed.
  193. ObjRegion =
  194. dyn_cast_or_null<TypedValueRegion>(Call.getArgSVal(0).getAsRegion());
  195. }
  196. if (!ObjRegion)
  197. return;
  198. SVal RawPtr = Call.getReturnValue();
  199. if (SymbolRef Sym = RawPtr.getAsSymbol(/*IncludeBaseRegions=*/true)) {
  200. // Start tracking this raw pointer by adding it to the set of symbols
  201. // associated with this container object in the program state map.
  202. PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
  203. const PtrSet *SetPtr = State->get<RawPtrMap>(ObjRegion);
  204. PtrSet Set = SetPtr ? *SetPtr : F.getEmptySet();
  205. assert(C.wasInlined || !Set.contains(Sym));
  206. Set = F.add(Set, Sym);
  207. State = State->set<RawPtrMap>(ObjRegion, Set);
  208. C.addTransition(State);
  209. }
  210. return;
  211. }
  212. // Check [string.require] / first point.
  213. checkFunctionArguments(Call, State, C);
  214. }
  215. void InnerPointerChecker::checkDeadSymbols(SymbolReaper &SymReaper,
  216. CheckerContext &C) const {
  217. ProgramStateRef State = C.getState();
  218. PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
  219. RawPtrMapTy RPM = State->get<RawPtrMap>();
  220. for (const auto &Entry : RPM) {
  221. if (!SymReaper.isLiveRegion(Entry.first)) {
  222. // Due to incomplete destructor support, some dead regions might
  223. // remain in the program state map. Clean them up.
  224. State = State->remove<RawPtrMap>(Entry.first);
  225. }
  226. if (const PtrSet *OldSet = State->get<RawPtrMap>(Entry.first)) {
  227. PtrSet CleanedUpSet = *OldSet;
  228. for (const auto Symbol : Entry.second) {
  229. if (!SymReaper.isLive(Symbol))
  230. CleanedUpSet = F.remove(CleanedUpSet, Symbol);
  231. }
  232. State = CleanedUpSet.isEmpty()
  233. ? State->remove<RawPtrMap>(Entry.first)
  234. : State->set<RawPtrMap>(Entry.first, CleanedUpSet);
  235. }
  236. }
  237. C.addTransition(State);
  238. }
  239. namespace clang {
  240. namespace ento {
  241. namespace allocation_state {
  242. std::unique_ptr<BugReporterVisitor> getInnerPointerBRVisitor(SymbolRef Sym) {
  243. return std::make_unique<InnerPointerChecker::InnerPointerBRVisitor>(Sym);
  244. }
  245. const MemRegion *getContainerObjRegion(ProgramStateRef State, SymbolRef Sym) {
  246. RawPtrMapTy Map = State->get<RawPtrMap>();
  247. for (const auto &Entry : Map) {
  248. if (Entry.second.contains(Sym)) {
  249. return Entry.first;
  250. }
  251. }
  252. return nullptr;
  253. }
  254. } // end namespace allocation_state
  255. } // end namespace ento
  256. } // end namespace clang
  257. PathDiagnosticPieceRef InnerPointerChecker::InnerPointerBRVisitor::VisitNode(
  258. const ExplodedNode *N, BugReporterContext &BRC, PathSensitiveBugReport &) {
  259. if (!isSymbolTracked(N->getState(), PtrToBuf) ||
  260. isSymbolTracked(N->getFirstPred()->getState(), PtrToBuf))
  261. return nullptr;
  262. const Stmt *S = N->getStmtForDiagnostics();
  263. if (!S)
  264. return nullptr;
  265. const MemRegion *ObjRegion =
  266. allocation_state::getContainerObjRegion(N->getState(), PtrToBuf);
  267. const auto *TypedRegion = cast<TypedValueRegion>(ObjRegion);
  268. QualType ObjTy = TypedRegion->getValueType();
  269. SmallString<256> Buf;
  270. llvm::raw_svector_ostream OS(Buf);
  271. OS << "Pointer to inner buffer of '" << ObjTy.getAsString()
  272. << "' obtained here";
  273. PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
  274. N->getLocationContext());
  275. return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
  276. }
  277. void ento::registerInnerPointerChecker(CheckerManager &Mgr) {
  278. registerInnerPointerCheckerAux(Mgr);
  279. Mgr.registerChecker<InnerPointerChecker>();
  280. }
  281. bool ento::shouldRegisterInnerPointerChecker(const CheckerManager &mgr) {
  282. return true;
  283. }