93 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This defines ObjCAtSyncChecker, a builtin check that checks for null pointers10// 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 ObjCAtSyncChecker27 : 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 namespace37 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);50 bugreporter::trackExpressionValue(N, Ex, *report);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 since66 // 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);70 bugreporter::trackExpressionValue(N, Ex, *report);71 72 C.emitReport(std::move(report));73 return;74 }75 }76 // Don't add a transition for 'nullState'. If the value is77 // under-constrained to be null or non-null, assume it is non-null78 // 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}93