1//== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
10// used as mutexes for @synchronized.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15#include "clang/AST/StmtObjC.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.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/PathSensitive/ExprEngine.h"
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26class ObjCAtSyncChecker
27 : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
28 const BugType BT_null{this, "Nil value used as mutex for @synchronized() "
29 "(no synchronization will occur)"};
30 const BugType BT_undef{this, "Uninitialized value used as mutex "
31 "for @synchronized"};
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 = C.getSVal(Ex);
44
45 // Uninitialized value used for the mutex?
46 if (isa<UndefinedVal>(Val: V)) {
47 if (ExplodedNode *N = C.generateErrorNode()) {
48 auto report = std::make_unique<PathSensitiveBugReport>(
49 args: BT_undef, args: BT_undef.getDescription(), args&: N);
50 bugreporter::trackExpressionValue(N, E: Ex, R&: *report);
51 C.emitReport(R: std::move(report));
52 }
53 return;
54 }
55
56 if (V.isUnknown())
57 return;
58
59 // Check for null mutexes.
60 ProgramStateRef notNullState, nullState;
61 std::tie(args&: notNullState, args&: nullState) = state->assume(Cond: V.castAs<DefinedSVal>());
62
63 if (nullState) {
64 if (!notNullState) {
65 // Generate an error node. This isn't a sink since
66 // a null mutex just means no synchronization occurs.
67 if (ExplodedNode *N = C.generateNonFatalErrorNode(State: nullState)) {
68 auto report = std::make_unique<PathSensitiveBugReport>(
69 args: BT_null, args: BT_null.getDescription(), args&: N);
70 bugreporter::trackExpressionValue(N, E: Ex, R&: *report);
71
72 C.emitReport(R: std::move(report));
73 return;
74 }
75 }
76 // Don't add a transition for 'nullState'. If the value is
77 // under-constrained to be null or non-null, assume it is non-null
78 // afterwards.
79 }
80
81 if (notNullState)
82 C.addTransition(State: notNullState);
83}
84
85void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
86 mgr.registerChecker<ObjCAtSyncChecker>();
87}
88
89bool ento::shouldRegisterObjCAtSyncChecker(const CheckerManager &mgr) {
90 const LangOptions &LO = mgr.getLangOpts();
91 return LO.ObjC;
92}
93

source code of clang/lib/StaticAnalyzer/Checkers/ObjCAtSyncChecker.cpp