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