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