clang 20.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 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>(V)) {
47 if (ExplodedNode *N = C.generateErrorNode()) {
48 auto report = std::make_unique<PathSensitiveBugReport>(
49 BT_undef, BT_undef.getDescription(), N);
51 C.emitReport(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(notNullState, nullState) = state->assume(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(nullState)) {
68 auto report = std::make_unique<PathSensitiveBugReport>(
69 BT_null, BT_null.getDescription(), N);
71
72 C.emitReport(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(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}
#define V(N, I)
Definition: ASTContext.h:3341
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:476
Represents Objective-C's @synchronized statement.
Definition: StmtObjC.h:303
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.
The JSON file list parser is used to communicate input to InstallAPI.