clang 18.0.0git
ObjCAtSyncChecker.cpp
Go to the documentation of this file.
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
15#include "clang/AST/StmtObjC.h"
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26class ObjCAtSyncChecker
27 : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
28 mutable std::unique_ptr<BugType> BT_null;
29 mutable std::unique_ptr<BugType> BT_undef;
30
31public:
32 void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
33};
34} // end anonymous namespace
35
36void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
37 CheckerContext &C) const {
38
39 const Expr *Ex = S->getSynchExpr();
40 ProgramStateRef state = C.getState();
41 SVal V = C.getSVal(Ex);
42
43 // Uninitialized value used for the mutex?
44 if (isa<UndefinedVal>(V)) {
45 if (ExplodedNode *N = C.generateErrorNode()) {
46 if (!BT_undef)
47 BT_undef.reset(new BugType(this, "Uninitialized value used as mutex "
48 "for @synchronized"));
49 auto report = std::make_unique<PathSensitiveBugReport>(
50 *BT_undef, BT_undef->getDescription(), N);
52 C.emitReport(std::move(report));
53 }
54 return;
55 }
56
57 if (V.isUnknown())
58 return;
59
60 // Check for null mutexes.
61 ProgramStateRef notNullState, nullState;
62 std::tie(notNullState, nullState) = state->assume(V.castAs<DefinedSVal>());
63
64 if (nullState) {
65 if (!notNullState) {
66 // Generate an error node. This isn't a sink since
67 // a null mutex just means no synchronization occurs.
68 if (ExplodedNode *N = C.generateNonFatalErrorNode(nullState)) {
69 if (!BT_null)
70 BT_null.reset(
71 new BugType(this, "Nil value used as mutex for @synchronized() "
72 "(no synchronization will occur)"));
73 auto report = std::make_unique<PathSensitiveBugReport>(
74 *BT_null, BT_null->getDescription(), N);
76
77 C.emitReport(std::move(report));
78 return;
79 }
80 }
81 // Don't add a transition for 'nullState'. If the value is
82 // under-constrained to be null or non-null, assume it is non-null
83 // afterwards.
84 }
85
86 if (notNullState)
87 C.addTransition(notNullState);
88}
89
90void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
91 mgr.registerChecker<ObjCAtSyncChecker>();
92}
93
94bool ento::shouldRegisterObjCAtSyncChecker(const CheckerManager &mgr) {
95 const LangOptions &LO = mgr.getLangOpts();
96 return LO.ObjC;
97}
#define V(N, I)
Definition: ASTContext.h:3241
Defines the Objective-C statement AST node classes.
This represents one expression.
Definition: Expr.h:110
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:83
Represents Objective-C's @synchronized statement.
Definition: StmtObjC.h:302
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const LangOptions & getLangOpts() const
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.