ObjCAtSyncChecker.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  20. #include "clang/StaticAnalyzer/Checkers/DereferenceChecker.h"
  21. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. using namespace clang;
  24. using namespace ento;
  25. namespace {
  26. class ObjCAtSyncChecker
  27. : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
  28. mutable llvm::OwningPtr<BuiltinBug> BT_null;
  29. mutable llvm::OwningPtr<BuiltinBug> BT_undef;
  30. public:
  31. void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
  32. };
  33. } // end anonymous namespace
  34. void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
  35. CheckerContext &C) const {
  36. const Expr *Ex = S->getSynchExpr();
  37. ProgramStateRef state = C.getState();
  38. SVal V = state->getSVal(Ex, C.getLocationContext());
  39. // Uninitialized value used for the mutex?
  40. if (isa<UndefinedVal>(V)) {
  41. if (ExplodedNode *N = C.generateSink()) {
  42. if (!BT_undef)
  43. BT_undef.reset(new BuiltinBug("Uninitialized value used as mutex "
  44. "for @synchronized"));
  45. BugReport *report =
  46. new BugReport(*BT_undef, BT_undef->getDescription(), N);
  47. report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
  48. C.EmitReport(report);
  49. }
  50. return;
  51. }
  52. if (V.isUnknown())
  53. return;
  54. // Check for null mutexes.
  55. ProgramStateRef notNullState, nullState;
  56. llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
  57. if (nullState) {
  58. if (!notNullState) {
  59. // Generate an error node. This isn't a sink since
  60. // a null mutex just means no synchronization occurs.
  61. if (ExplodedNode *N = C.addTransition(nullState)) {
  62. if (!BT_null)
  63. BT_null.reset(new BuiltinBug("Nil value used as mutex for @synchronized() "
  64. "(no synchronization will occur)"));
  65. BugReport *report =
  66. new BugReport(*BT_null, BT_null->getDescription(), N);
  67. report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
  68. C.EmitReport(report);
  69. return;
  70. }
  71. }
  72. // Don't add a transition for 'nullState'. If the value is
  73. // under-constrained to be null or non-null, assume it is non-null
  74. // afterwards.
  75. }
  76. if (notNullState)
  77. C.addTransition(notNullState);
  78. }
  79. void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
  80. if (mgr.getLangOptions().ObjC2)
  81. mgr.registerChecker<ObjCAtSyncChecker>();
  82. }