DereferenceChecker.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //===-- DereferenceChecker.cpp - Null dereference checker -----------------===//
  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 defines NullDerefChecker, a builtin check in ExprEngine that performs
  10. // checks for null pointers at loads and stores.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ExprObjC.h"
  14. #include "clang/AST/ExprOpenMP.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  17. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  18. #include "clang/StaticAnalyzer/Core/Checker.h"
  19. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  20. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  21. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
  22. #include "llvm/ADT/SmallString.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace clang;
  25. using namespace ento;
  26. namespace {
  27. class DereferenceChecker
  28. : public Checker< check::Location,
  29. check::Bind,
  30. EventDispatcher<ImplicitNullDerefEvent> > {
  31. enum DerefKind { NullPointer, UndefinedPointerValue };
  32. BugType BT_Null{this, "Dereference of null pointer", categories::LogicError};
  33. BugType BT_Undef{this, "Dereference of undefined pointer value",
  34. categories::LogicError};
  35. void reportBug(DerefKind K, ProgramStateRef State, const Stmt *S,
  36. CheckerContext &C) const;
  37. bool suppressReport(CheckerContext &C, const Expr *E) const;
  38. public:
  39. void checkLocation(SVal location, bool isLoad, const Stmt* S,
  40. CheckerContext &C) const;
  41. void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
  42. static void AddDerefSource(raw_ostream &os,
  43. SmallVectorImpl<SourceRange> &Ranges,
  44. const Expr *Ex, const ProgramState *state,
  45. const LocationContext *LCtx,
  46. bool loadedFrom = false);
  47. bool SuppressAddressSpaces = false;
  48. };
  49. } // end anonymous namespace
  50. void
  51. DereferenceChecker::AddDerefSource(raw_ostream &os,
  52. SmallVectorImpl<SourceRange> &Ranges,
  53. const Expr *Ex,
  54. const ProgramState *state,
  55. const LocationContext *LCtx,
  56. bool loadedFrom) {
  57. Ex = Ex->IgnoreParenLValueCasts();
  58. switch (Ex->getStmtClass()) {
  59. default:
  60. break;
  61. case Stmt::DeclRefExprClass: {
  62. const DeclRefExpr *DR = cast<DeclRefExpr>(Ex);
  63. if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
  64. os << " (" << (loadedFrom ? "loaded from" : "from")
  65. << " variable '" << VD->getName() << "')";
  66. Ranges.push_back(DR->getSourceRange());
  67. }
  68. break;
  69. }
  70. case Stmt::MemberExprClass: {
  71. const MemberExpr *ME = cast<MemberExpr>(Ex);
  72. os << " (" << (loadedFrom ? "loaded from" : "via")
  73. << " field '" << ME->getMemberNameInfo() << "')";
  74. SourceLocation L = ME->getMemberLoc();
  75. Ranges.push_back(SourceRange(L, L));
  76. break;
  77. }
  78. case Stmt::ObjCIvarRefExprClass: {
  79. const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(Ex);
  80. os << " (" << (loadedFrom ? "loaded from" : "via")
  81. << " ivar '" << IV->getDecl()->getName() << "')";
  82. SourceLocation L = IV->getLocation();
  83. Ranges.push_back(SourceRange(L, L));
  84. break;
  85. }
  86. }
  87. }
  88. static const Expr *getDereferenceExpr(const Stmt *S, bool IsBind=false){
  89. const Expr *E = nullptr;
  90. // Walk through lvalue casts to get the original expression
  91. // that syntactically caused the load.
  92. if (const Expr *expr = dyn_cast<Expr>(S))
  93. E = expr->IgnoreParenLValueCasts();
  94. if (IsBind) {
  95. const VarDecl *VD;
  96. const Expr *Init;
  97. std::tie(VD, Init) = parseAssignment(S);
  98. if (VD && Init)
  99. E = Init;
  100. }
  101. return E;
  102. }
  103. bool DereferenceChecker::suppressReport(CheckerContext &C,
  104. const Expr *E) const {
  105. // Do not report dereferences on memory that use address space #256, #257,
  106. // and #258. Those address spaces are used when dereferencing address spaces
  107. // relative to the GS, FS, and SS segments on x86/x86-64 targets.
  108. // Dereferencing a null pointer in these address spaces is not defined
  109. // as an error. All other null dereferences in other address spaces
  110. // are defined as an error unless explicitly defined.
  111. // See https://clang.llvm.org/docs/LanguageExtensions.html, the section
  112. // "X86/X86-64 Language Extensions"
  113. QualType Ty = E->getType();
  114. if (!Ty.hasAddressSpace())
  115. return false;
  116. if (SuppressAddressSpaces)
  117. return true;
  118. const llvm::Triple::ArchType Arch =
  119. C.getASTContext().getTargetInfo().getTriple().getArch();
  120. if ((Arch == llvm::Triple::x86) || (Arch == llvm::Triple::x86_64)) {
  121. switch (toTargetAddressSpace(E->getType().getAddressSpace())) {
  122. case 256:
  123. case 257:
  124. case 258:
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. static bool isDeclRefExprToReference(const Expr *E) {
  131. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  132. return DRE->getDecl()->getType()->isReferenceType();
  133. return false;
  134. }
  135. void DereferenceChecker::reportBug(DerefKind K, ProgramStateRef State,
  136. const Stmt *S, CheckerContext &C) const {
  137. const BugType *BT = nullptr;
  138. llvm::StringRef DerefStr1;
  139. llvm::StringRef DerefStr2;
  140. switch (K) {
  141. case DerefKind::NullPointer:
  142. BT = &BT_Null;
  143. DerefStr1 = " results in a null pointer dereference";
  144. DerefStr2 = " results in a dereference of a null pointer";
  145. break;
  146. case DerefKind::UndefinedPointerValue:
  147. BT = &BT_Undef;
  148. DerefStr1 = " results in an undefined pointer dereference";
  149. DerefStr2 = " results in a dereference of an undefined pointer value";
  150. break;
  151. };
  152. // Generate an error node.
  153. ExplodedNode *N = C.generateErrorNode(State);
  154. if (!N)
  155. return;
  156. SmallString<100> buf;
  157. llvm::raw_svector_ostream os(buf);
  158. SmallVector<SourceRange, 2> Ranges;
  159. switch (S->getStmtClass()) {
  160. case Stmt::ArraySubscriptExprClass: {
  161. os << "Array access";
  162. const ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(S);
  163. AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(),
  164. State.get(), N->getLocationContext());
  165. os << DerefStr1;
  166. break;
  167. }
  168. case Stmt::OMPArraySectionExprClass: {
  169. os << "Array access";
  170. const OMPArraySectionExpr *AE = cast<OMPArraySectionExpr>(S);
  171. AddDerefSource(os, Ranges, AE->getBase()->IgnoreParenCasts(),
  172. State.get(), N->getLocationContext());
  173. os << DerefStr1;
  174. break;
  175. }
  176. case Stmt::UnaryOperatorClass: {
  177. os << BT->getDescription();
  178. const UnaryOperator *U = cast<UnaryOperator>(S);
  179. AddDerefSource(os, Ranges, U->getSubExpr()->IgnoreParens(),
  180. State.get(), N->getLocationContext(), true);
  181. break;
  182. }
  183. case Stmt::MemberExprClass: {
  184. const MemberExpr *M = cast<MemberExpr>(S);
  185. if (M->isArrow() || isDeclRefExprToReference(M->getBase())) {
  186. os << "Access to field '" << M->getMemberNameInfo() << "'" << DerefStr2;
  187. AddDerefSource(os, Ranges, M->getBase()->IgnoreParenCasts(),
  188. State.get(), N->getLocationContext(), true);
  189. }
  190. break;
  191. }
  192. case Stmt::ObjCIvarRefExprClass: {
  193. const ObjCIvarRefExpr *IV = cast<ObjCIvarRefExpr>(S);
  194. os << "Access to instance variable '" << *IV->getDecl() << "'" << DerefStr2;
  195. AddDerefSource(os, Ranges, IV->getBase()->IgnoreParenCasts(),
  196. State.get(), N->getLocationContext(), true);
  197. break;
  198. }
  199. default:
  200. break;
  201. }
  202. auto report = std::make_unique<PathSensitiveBugReport>(
  203. *BT, buf.empty() ? BT->getDescription() : buf.str(), N);
  204. bugreporter::trackExpressionValue(N, bugreporter::getDerefExpr(S), *report);
  205. for (SmallVectorImpl<SourceRange>::iterator
  206. I = Ranges.begin(), E = Ranges.end(); I!=E; ++I)
  207. report->addRange(*I);
  208. C.emitReport(std::move(report));
  209. }
  210. void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S,
  211. CheckerContext &C) const {
  212. // Check for dereference of an undefined value.
  213. if (l.isUndef()) {
  214. const Expr *DerefExpr = getDereferenceExpr(S);
  215. if (!suppressReport(C, DerefExpr))
  216. reportBug(DerefKind::UndefinedPointerValue, C.getState(), DerefExpr, C);
  217. return;
  218. }
  219. DefinedOrUnknownSVal location = l.castAs<DefinedOrUnknownSVal>();
  220. // Check for null dereferences.
  221. if (!isa<Loc>(location))
  222. return;
  223. ProgramStateRef state = C.getState();
  224. ProgramStateRef notNullState, nullState;
  225. std::tie(notNullState, nullState) = state->assume(location);
  226. if (nullState) {
  227. if (!notNullState) {
  228. // We know that 'location' can only be null. This is what
  229. // we call an "explicit" null dereference.
  230. const Expr *expr = getDereferenceExpr(S);
  231. if (!suppressReport(C, expr)) {
  232. reportBug(DerefKind::NullPointer, nullState, expr, C);
  233. return;
  234. }
  235. }
  236. // Otherwise, we have the case where the location could either be
  237. // null or not-null. Record the error node as an "implicit" null
  238. // dereference.
  239. if (ExplodedNode *N = C.generateSink(nullState, C.getPredecessor())) {
  240. ImplicitNullDerefEvent event = {l, isLoad, N, &C.getBugReporter(),
  241. /*IsDirectDereference=*/true};
  242. dispatchEvent(event);
  243. }
  244. }
  245. // From this point forward, we know that the location is not null.
  246. C.addTransition(notNullState);
  247. }
  248. void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S,
  249. CheckerContext &C) const {
  250. // If we're binding to a reference, check if the value is known to be null.
  251. if (V.isUndef())
  252. return;
  253. const MemRegion *MR = L.getAsRegion();
  254. const TypedValueRegion *TVR = dyn_cast_or_null<TypedValueRegion>(MR);
  255. if (!TVR)
  256. return;
  257. if (!TVR->getValueType()->isReferenceType())
  258. return;
  259. ProgramStateRef State = C.getState();
  260. ProgramStateRef StNonNull, StNull;
  261. std::tie(StNonNull, StNull) = State->assume(V.castAs<DefinedOrUnknownSVal>());
  262. if (StNull) {
  263. if (!StNonNull) {
  264. const Expr *expr = getDereferenceExpr(S, /*IsBind=*/true);
  265. if (!suppressReport(C, expr)) {
  266. reportBug(DerefKind::NullPointer, StNull, expr, C);
  267. return;
  268. }
  269. }
  270. // At this point the value could be either null or non-null.
  271. // Record this as an "implicit" null dereference.
  272. if (ExplodedNode *N = C.generateSink(StNull, C.getPredecessor())) {
  273. ImplicitNullDerefEvent event = {V, /*isLoad=*/true, N,
  274. &C.getBugReporter(),
  275. /*IsDirectDereference=*/true};
  276. dispatchEvent(event);
  277. }
  278. }
  279. // Unlike a regular null dereference, initializing a reference with a
  280. // dereferenced null pointer does not actually cause a runtime exception in
  281. // Clang's implementation of references.
  282. //
  283. // int &r = *p; // safe??
  284. // if (p != NULL) return; // uh-oh
  285. // r = 5; // trap here
  286. //
  287. // The standard says this is invalid as soon as we try to create a "null
  288. // reference" (there is no such thing), but turning this into an assumption
  289. // that 'p' is never null will not match our actual runtime behavior.
  290. // So we do not record this assumption, allowing us to warn on the last line
  291. // of this example.
  292. //
  293. // We do need to add a transition because we may have generated a sink for
  294. // the "implicit" null dereference.
  295. C.addTransition(State, this);
  296. }
  297. void ento::registerDereferenceChecker(CheckerManager &mgr) {
  298. auto *Chk = mgr.registerChecker<DereferenceChecker>();
  299. Chk->SuppressAddressSpaces = mgr.getAnalyzerOptions().getCheckerBooleanOption(
  300. mgr.getCurrentCheckerName(), "SuppressAddressSpaces");
  301. }
  302. bool ento::shouldRegisterDereferenceChecker(const CheckerManager &mgr) {
  303. return true;
  304. }