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