xref: /minix3/external/bsd/llvm/dist/clang/lib/StaticAnalyzer/Checkers/ObjCAtSyncChecker.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
11f4a2713aSLionel Sambuc // used as mutexes for @synchronized.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
16f4a2713aSLionel Sambuc #include "clang/AST/StmtObjC.h"
17f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
19f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc using namespace clang;
24f4a2713aSLionel Sambuc using namespace ento;
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc namespace {
27f4a2713aSLionel Sambuc class ObjCAtSyncChecker
28f4a2713aSLionel Sambuc     : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
29*0a6a1f1dSLionel Sambuc   mutable std::unique_ptr<BuiltinBug> BT_null;
30*0a6a1f1dSLionel Sambuc   mutable std::unique_ptr<BuiltinBug> BT_undef;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc public:
33f4a2713aSLionel Sambuc   void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
34f4a2713aSLionel Sambuc };
35f4a2713aSLionel Sambuc } // end anonymous namespace
36f4a2713aSLionel Sambuc 
checkPreStmt(const ObjCAtSynchronizedStmt * S,CheckerContext & C) const37f4a2713aSLionel Sambuc void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
38f4a2713aSLionel Sambuc                                      CheckerContext &C) const {
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc   const Expr *Ex = S->getSynchExpr();
41f4a2713aSLionel Sambuc   ProgramStateRef state = C.getState();
42f4a2713aSLionel Sambuc   SVal V = state->getSVal(Ex, C.getLocationContext());
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   // Uninitialized value used for the mutex?
45f4a2713aSLionel Sambuc   if (V.getAs<UndefinedVal>()) {
46f4a2713aSLionel Sambuc     if (ExplodedNode *N = C.generateSink()) {
47f4a2713aSLionel Sambuc       if (!BT_undef)
48*0a6a1f1dSLionel Sambuc         BT_undef.reset(new BuiltinBug(this, "Uninitialized value used as mutex "
49f4a2713aSLionel Sambuc                                             "for @synchronized"));
50f4a2713aSLionel Sambuc       BugReport *report =
51f4a2713aSLionel Sambuc         new BugReport(*BT_undef, BT_undef->getDescription(), N);
52f4a2713aSLionel Sambuc       bugreporter::trackNullOrUndefValue(N, Ex, *report);
53f4a2713aSLionel Sambuc       C.emitReport(report);
54f4a2713aSLionel Sambuc     }
55f4a2713aSLionel Sambuc     return;
56f4a2713aSLionel Sambuc   }
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc   if (V.isUnknown())
59f4a2713aSLionel Sambuc     return;
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc   // Check for null mutexes.
62f4a2713aSLionel Sambuc   ProgramStateRef notNullState, nullState;
63*0a6a1f1dSLionel Sambuc   std::tie(notNullState, nullState) = state->assume(V.castAs<DefinedSVal>());
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc   if (nullState) {
66f4a2713aSLionel Sambuc     if (!notNullState) {
67f4a2713aSLionel Sambuc       // Generate an error node.  This isn't a sink since
68f4a2713aSLionel Sambuc       // a null mutex just means no synchronization occurs.
69f4a2713aSLionel Sambuc       if (ExplodedNode *N = C.addTransition(nullState)) {
70f4a2713aSLionel Sambuc         if (!BT_null)
71*0a6a1f1dSLionel Sambuc           BT_null.reset(new BuiltinBug(
72*0a6a1f1dSLionel Sambuc               this, "Nil value used as mutex for @synchronized() "
73f4a2713aSLionel Sambuc                     "(no synchronization will occur)"));
74f4a2713aSLionel Sambuc         BugReport *report =
75f4a2713aSLionel Sambuc           new BugReport(*BT_null, BT_null->getDescription(), N);
76f4a2713aSLionel Sambuc         bugreporter::trackNullOrUndefValue(N, Ex, *report);
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc         C.emitReport(report);
79f4a2713aSLionel Sambuc         return;
80f4a2713aSLionel Sambuc       }
81f4a2713aSLionel Sambuc     }
82f4a2713aSLionel Sambuc     // Don't add a transition for 'nullState'.  If the value is
83f4a2713aSLionel Sambuc     // under-constrained to be null or non-null, assume it is non-null
84f4a2713aSLionel Sambuc     // afterwards.
85f4a2713aSLionel Sambuc   }
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc   if (notNullState)
88f4a2713aSLionel Sambuc     C.addTransition(notNullState);
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc 
registerObjCAtSyncChecker(CheckerManager & mgr)91f4a2713aSLionel Sambuc void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
92f4a2713aSLionel Sambuc   if (mgr.getLangOpts().ObjC2)
93f4a2713aSLionel Sambuc     mgr.registerChecker<ObjCAtSyncChecker>();
94f4a2713aSLionel Sambuc }
95