ObjCAtSyncChecker.cpp 3.1 KB

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