ObjCAtSyncChecker.cpp revision 00bd44d5677783527d7517c1ffe45e4d75a0f56f
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#include "llvm/ADT/STLExtras.h"
24
25using namespace clang;
26using namespace ento;
27
28namespace {
29class ObjCAtSyncChecker
30    : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
31  mutable llvm::OwningPtr<BuiltinBug> BT_null;
32  mutable llvm::OwningPtr<BuiltinBug> BT_undef;
33
34public:
35  void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
36};
37} // end anonymous namespace
38
39void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
40                                     CheckerContext &C) const {
41
42  const Expr *Ex = S->getSynchExpr();
43  ProgramStateRef state = C.getState();
44  SVal V = state->getSVal(Ex, C.getLocationContext());
45
46  // Uninitialized value used for the mutex?
47  if (isa<UndefinedVal>(V)) {
48    if (ExplodedNode *N = C.generateSink()) {
49      if (!BT_undef)
50        BT_undef.reset(new BuiltinBug("Uninitialized value used as mutex "
51                                  "for @synchronized"));
52      BugReport *report =
53        new BugReport(*BT_undef, BT_undef->getDescription(), N);
54      report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
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
79        C.EmitReport(report);
80        return;
81      }
82    }
83    // Don't add a transition for 'nullState'.  If the value is
84    // under-constrained to be null or non-null, assume it is non-null
85    // afterwards.
86  }
87
88  if (notNullState)
89    C.addTransition(notNullState);
90}
91
92void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
93  if (mgr.getLangOptions().ObjC2)
94    mgr.registerChecker<ObjCAtSyncChecker>();
95}
96