ObjCAtSyncChecker.cpp revision 4e4d08403ca5cfd4d558fa2936215d3a4e5a528d
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
24using namespace clang;
25using namespace ento;
26
27namespace {
28class ObjCAtSyncChecker
29    : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
30  mutable OwningPtr<BuiltinBug> BT_null;
31  mutable OwningPtr<BuiltinBug> BT_undef;
32
33public:
34  void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
35};
36} // end anonymous namespace
37
38void 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                                                                      report));
55      C.EmitReport(report);
56    }
57    return;
58  }
59
60  if (V.isUnknown())
61    return;
62
63  // Check for null mutexes.
64  ProgramStateRef notNullState, nullState;
65  llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
66
67  if (nullState) {
68    if (!notNullState) {
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)) {
72        if (!BT_null)
73          BT_null.reset(new BuiltinBug("Nil value used as mutex for @synchronized() "
74                                   "(no synchronization will occur)"));
75        BugReport *report =
76          new BugReport(*BT_null, BT_null->getDescription(), N);
77        report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex,
78                                                                        report));
79
80        C.EmitReport(report);
81        return;
82      }
83    }
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
86    // afterwards.
87  }
88
89  if (notNullState)
90    C.addTransition(notNullState);
91}
92
93void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
94  if (mgr.getLangOpts().ObjC2)
95    mgr.registerChecker<ObjCAtSyncChecker>();
96}
97