blob: fbf2d73dd86c4d8e646d61f717586a60dc73dd09 [file] [log] [blame]
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +00001//== ObjCAtSyncChecker.cpp - nil mutex checker for @synchronized -*- C++ -*--=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines ObjCAtSyncChecker, a builtin check that checks for null pointers
11// used as mutexes for @synchronized.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +000015#include "ClangSACheckers.h"
Benjamin Kramer11764ab2012-01-28 12:06:22 +000016#include "clang/AST/StmtObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000022
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000025
26namespace {
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000027class ObjCAtSyncChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000028 : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000029 mutable std::unique_ptr<BuiltinBug> BT_null;
30 mutable std::unique_ptr<BuiltinBug> BT_undef;
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000031
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000032public:
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000033 void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000034};
35} // end anonymous namespace
36
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000037void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
38 CheckerContext &C) const {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000039
40 const Expr *Ex = S->getSynchExpr();
Ted Kremenek49b1e382012-01-26 21:29:00 +000041 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000042 SVal V = state->getSVal(Ex, C.getLocationContext());
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000043
44 // Uninitialized value used for the mutex?
David Blaikie2fdacbc2013-02-20 05:52:05 +000045 if (V.getAs<UndefinedVal>()) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +000046 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000047 if (!BT_undef)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000048 BT_undef.reset(new BuiltinBug(this, "Uninitialized value used as mutex "
49 "for @synchronized"));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000050 BugReport *report =
51 new BugReport(*BT_undef, BT_undef->getDescription(), N);
Jordan Rosea0f7d352012-08-28 00:50:51 +000052 bugreporter::trackNullOrUndefValue(N, Ex, *report);
Jordan Rosee10d5a72012-11-02 01:53:40 +000053 C.emitReport(report);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000054 }
55 return;
56 }
57
Ted Kremenekc07d8352010-10-25 20:20:56 +000058 if (V.isUnknown())
59 return;
60
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000061 // Check for null mutexes.
Ted Kremenek49b1e382012-01-26 21:29:00 +000062 ProgramStateRef notNullState, nullState;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +000063 std::tie(notNullState, nullState) = state->assume(V.castAs<DefinedSVal>());
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000064
65 if (nullState) {
66 if (!notNullState) {
67 // Generate an error node. This isn't a sink since
68 // a null mutex just means no synchronization occurs.
Anna Zaksda4c8d62011-10-26 21:06:34 +000069 if (ExplodedNode *N = C.addTransition(nullState)) {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000070 if (!BT_null)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000071 BT_null.reset(new BuiltinBug(
72 this, "Nil value used as mutex for @synchronized() "
73 "(no synchronization will occur)"));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000074 BugReport *report =
75 new BugReport(*BT_null, BT_null->getDescription(), N);
Jordan Rosea0f7d352012-08-28 00:50:51 +000076 bugreporter::trackNullOrUndefValue(N, Ex, *report);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000077
Jordan Rosee10d5a72012-11-02 01:53:40 +000078 C.emitReport(report);
Ted Kremenekc5644e12010-10-21 15:38:55 +000079 return;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000080 }
81 }
Ted Kremenekc5644e12010-10-21 15:38:55 +000082 // Don't add a transition for 'nullState'. If the value is
83 // under-constrained to be null or non-null, assume it is non-null
84 // afterwards.
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000085 }
86
87 if (notNullState)
Anna Zaksda4c8d62011-10-26 21:06:34 +000088 C.addTransition(notNullState);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000089}
Ted Kremenekc5644e12010-10-21 15:38:55 +000090
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000091void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000092 if (mgr.getLangOpts().ObjC2)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000093 mgr.registerChecker<ObjCAtSyncChecker>();
94}