1 //== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
11 // used as mutexes for @synchronized.
13 //===----------------------------------------------------------------------===//
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"
24 using namespace clang;
28 class ObjCAtSyncChecker
29 : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
30 mutable OwningPtr<BuiltinBug> BT_null;
31 mutable OwningPtr<BuiltinBug> BT_undef;
34 void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
36 } // end anonymous namespace
38 void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
39 CheckerContext &C) const {
41 const Expr *Ex = S->getSynchExpr();
42 ProgramStateRef state = C.getState();
43 SVal V = state->getSVal(Ex, C.getLocationContext());
45 // Uninitialized value used for the mutex?
46 if (isa<UndefinedVal>(V)) {
47 if (ExplodedNode *N = C.generateSink()) {
49 BT_undef.reset(new BuiltinBug("Uninitialized value used as mutex "
50 "for @synchronized"));
52 new BugReport(*BT_undef, BT_undef->getDescription(), N);
53 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex,
63 // Check for null mutexes.
64 ProgramStateRef notNullState, nullState;
65 llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
69 // Generate an error node. This isn't a sink since
70 // a null mutex just means no synchronization occurs.
71 if (ExplodedNode *N = C.addTransition(nullState)) {
73 BT_null.reset(new BuiltinBug("Nil value used as mutex for @synchronized() "
74 "(no synchronization will occur)"));
76 new BugReport(*BT_null, BT_null->getDescription(), N);
77 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex,
84 // Don't add a transition for 'nullState'. If the value is
85 // under-constrained to be null or non-null, assume it is non-null
90 C.addTransition(notNullState);
93 void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
94 if (mgr.getLangOpts().ObjC2)
95 mgr.registerChecker<ObjCAtSyncChecker>();