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