12754fe60SDimitry Andric //== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
22754fe60SDimitry Andric //
32754fe60SDimitry Andric // The LLVM Compiler Infrastructure
42754fe60SDimitry Andric //
52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source
62754fe60SDimitry Andric // License. See LICENSE.TXT for details.
72754fe60SDimitry Andric //
82754fe60SDimitry Andric //===----------------------------------------------------------------------===//
92754fe60SDimitry Andric //
102754fe60SDimitry Andric // This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
112754fe60SDimitry Andric // used as mutexes for @synchronized.
122754fe60SDimitry Andric //
132754fe60SDimitry Andric //===----------------------------------------------------------------------===//
142754fe60SDimitry Andric
15*b5893f02SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16dff0c46cSDimitry Andric #include "clang/AST/StmtObjC.h"
17139f7f9bSDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
183b0f4066SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
192754fe60SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20dd6029ffSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
212754fe60SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
222754fe60SDimitry Andric
232754fe60SDimitry Andric using namespace clang;
242754fe60SDimitry Andric using namespace ento;
252754fe60SDimitry Andric
262754fe60SDimitry Andric namespace {
27dd6029ffSDimitry Andric class ObjCAtSyncChecker
283b0f4066SDimitry Andric : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
2959d1ed5bSDimitry Andric mutable std::unique_ptr<BuiltinBug> BT_null;
3059d1ed5bSDimitry Andric mutable std::unique_ptr<BuiltinBug> BT_undef;
31dd6029ffSDimitry Andric
322754fe60SDimitry Andric public:
33dd6029ffSDimitry Andric void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
342754fe60SDimitry Andric };
352754fe60SDimitry Andric } // end anonymous namespace
362754fe60SDimitry Andric
checkPreStmt(const ObjCAtSynchronizedStmt * S,CheckerContext & C) const37dd6029ffSDimitry Andric void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
38dd6029ffSDimitry Andric CheckerContext &C) const {
392754fe60SDimitry Andric
402754fe60SDimitry Andric const Expr *Ex = S->getSynchExpr();
41dff0c46cSDimitry Andric ProgramStateRef state = C.getState();
424ba319b5SDimitry Andric SVal V = C.getSVal(Ex);
432754fe60SDimitry Andric
442754fe60SDimitry Andric // Uninitialized value used for the mutex?
45139f7f9bSDimitry Andric if (V.getAs<UndefinedVal>()) {
460623d748SDimitry Andric if (ExplodedNode *N = C.generateErrorNode()) {
472754fe60SDimitry Andric if (!BT_undef)
4859d1ed5bSDimitry Andric BT_undef.reset(new BuiltinBug(this, "Uninitialized value used as mutex "
49dd6029ffSDimitry Andric "for @synchronized"));
503dac3a9bSDimitry Andric auto report =
513dac3a9bSDimitry Andric llvm::make_unique<BugReport>(*BT_undef, BT_undef->getDescription(), N);
52*b5893f02SDimitry Andric bugreporter::trackExpressionValue(N, Ex, *report);
533dac3a9bSDimitry Andric C.emitReport(std::move(report));
542754fe60SDimitry Andric }
552754fe60SDimitry Andric return;
562754fe60SDimitry Andric }
572754fe60SDimitry Andric
582754fe60SDimitry Andric if (V.isUnknown())
592754fe60SDimitry Andric return;
602754fe60SDimitry Andric
612754fe60SDimitry Andric // Check for null mutexes.
62dff0c46cSDimitry Andric ProgramStateRef notNullState, nullState;
6359d1ed5bSDimitry Andric std::tie(notNullState, nullState) = state->assume(V.castAs<DefinedSVal>());
642754fe60SDimitry Andric
652754fe60SDimitry Andric if (nullState) {
662754fe60SDimitry Andric if (!notNullState) {
672754fe60SDimitry Andric // Generate an error node. This isn't a sink since
682754fe60SDimitry Andric // a null mutex just means no synchronization occurs.
690623d748SDimitry Andric if (ExplodedNode *N = C.generateNonFatalErrorNode(nullState)) {
702754fe60SDimitry Andric if (!BT_null)
7159d1ed5bSDimitry Andric BT_null.reset(new BuiltinBug(
7259d1ed5bSDimitry Andric this, "Nil value used as mutex for @synchronized() "
73dd6029ffSDimitry Andric "(no synchronization will occur)"));
743dac3a9bSDimitry Andric auto report =
753dac3a9bSDimitry Andric llvm::make_unique<BugReport>(*BT_null, BT_null->getDescription(), N);
76*b5893f02SDimitry Andric bugreporter::trackExpressionValue(N, Ex, *report);
772754fe60SDimitry Andric
783dac3a9bSDimitry Andric C.emitReport(std::move(report));
792754fe60SDimitry Andric return;
802754fe60SDimitry Andric }
812754fe60SDimitry Andric }
822754fe60SDimitry Andric // Don't add a transition for 'nullState'. If the value is
832754fe60SDimitry Andric // under-constrained to be null or non-null, assume it is non-null
842754fe60SDimitry Andric // afterwards.
852754fe60SDimitry Andric }
862754fe60SDimitry Andric
872754fe60SDimitry Andric if (notNullState)
882754fe60SDimitry Andric C.addTransition(notNullState);
892754fe60SDimitry Andric }
902754fe60SDimitry Andric
registerObjCAtSyncChecker(CheckerManager & mgr)91dd6029ffSDimitry Andric void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
92*b5893f02SDimitry Andric if (mgr.getLangOpts().ObjC)
93dd6029ffSDimitry Andric mgr.registerChecker<ObjCAtSyncChecker>();
94dd6029ffSDimitry Andric }
95