ObjCAtSyncChecker.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- 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 defines ObjCAtSyncChecker, a builtin check that checks for null pointers
  10. // used as mutexes for @synchronized.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/AST/StmtObjC.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  20. using namespace clang;
  21. using namespace ento;
  22. namespace {
  23. class ObjCAtSyncChecker
  24. : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
  25. mutable std::unique_ptr<BuiltinBug> BT_null;
  26. mutable std::unique_ptr<BuiltinBug> BT_undef;
  27. public:
  28. void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
  29. };
  30. } // end anonymous namespace
  31. void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
  32. CheckerContext &C) const {
  33. const Expr *Ex = S->getSynchExpr();
  34. ProgramStateRef state = C.getState();
  35. SVal V = C.getSVal(Ex);
  36. // Uninitialized value used for the mutex?
  37. if (V.getAs<UndefinedVal>()) {
  38. if (ExplodedNode *N = C.generateErrorNode()) {
  39. if (!BT_undef)
  40. BT_undef.reset(new BuiltinBug(this, "Uninitialized value used as mutex "
  41. "for @synchronized"));
  42. auto report = std::make_unique<PathSensitiveBugReport>(
  43. *BT_undef, BT_undef->getDescription(), N);
  44. bugreporter::trackExpressionValue(N, Ex, *report);
  45. C.emitReport(std::move(report));
  46. }
  47. return;
  48. }
  49. if (V.isUnknown())
  50. return;
  51. // Check for null mutexes.
  52. ProgramStateRef notNullState, nullState;
  53. std::tie(notNullState, nullState) = state->assume(V.castAs<DefinedSVal>());
  54. if (nullState) {
  55. if (!notNullState) {
  56. // Generate an error node. This isn't a sink since
  57. // a null mutex just means no synchronization occurs.
  58. if (ExplodedNode *N = C.generateNonFatalErrorNode(nullState)) {
  59. if (!BT_null)
  60. BT_null.reset(new BuiltinBug(
  61. this, "Nil value used as mutex for @synchronized() "
  62. "(no synchronization will occur)"));
  63. auto report = std::make_unique<PathSensitiveBugReport>(
  64. *BT_null, BT_null->getDescription(), N);
  65. bugreporter::trackExpressionValue(N, Ex, *report);
  66. C.emitReport(std::move(report));
  67. return;
  68. }
  69. }
  70. // Don't add a transition for 'nullState'. If the value is
  71. // under-constrained to be null or non-null, assume it is non-null
  72. // afterwards.
  73. }
  74. if (notNullState)
  75. C.addTransition(notNullState);
  76. }
  77. void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
  78. mgr.registerChecker<ObjCAtSyncChecker>();
  79. }
  80. bool ento::shouldRegisterObjCAtSyncChecker(const CheckerManager &mgr) {
  81. const LangOptions &LO = mgr.getLangOpts();
  82. return LO.ObjC;
  83. }