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