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