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