blob: 777e9ea219b3be3dac071829975fb7a41d7fef9c [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"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenekd99bd552010-12-23 19:38:26 +000021#include "clang/StaticAnalyzer/Checkers/DereferenceChecker.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000023
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000026
27namespace {
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000028class ObjCAtSyncChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000029 : public Checker< check::PreStmt<ObjCAtSynchronizedStmt> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000030 mutable OwningPtr<BuiltinBug> BT_null;
31 mutable OwningPtr<BuiltinBug> BT_undef;
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000032
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000033public:
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000034 void checkPreStmt(const ObjCAtSynchronizedStmt *S, CheckerContext &C) const;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000035};
36} // end anonymous namespace
37
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000038void ObjCAtSyncChecker::checkPreStmt(const ObjCAtSynchronizedStmt *S,
39 CheckerContext &C) const {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000040
41 const Expr *Ex = S->getSynchExpr();
Ted Kremenek49b1e382012-01-26 21:29:00 +000042 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000043 SVal V = state->getSVal(Ex, C.getLocationContext());
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000044
45 // Uninitialized value used for the mutex?
46 if (isa<UndefinedVal>(V)) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +000047 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000048 if (!BT_undef)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000049 BT_undef.reset(new BuiltinBug("Uninitialized value used as mutex "
50 "for @synchronized"));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000051 BugReport *report =
52 new BugReport(*BT_undef, BT_undef->getDescription(), N);
Ted Kremenek1e809b42012-03-09 01:13:14 +000053 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex,
54 report));
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000055 C.EmitReport(report);
56 }
57 return;
58 }
59
Ted Kremenekc07d8352010-10-25 20:20:56 +000060 if (V.isUnknown())
61 return;
62
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000063 // Check for null mutexes.
Ted Kremenek49b1e382012-01-26 21:29:00 +000064 ProgramStateRef notNullState, nullState;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000065 llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000066
67 if (nullState) {
68 if (!notNullState) {
69 // Generate an error node. This isn't a sink since
70 // a null mutex just means no synchronization occurs.
Anna Zaksda4c8d62011-10-26 21:06:34 +000071 if (ExplodedNode *N = C.addTransition(nullState)) {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000072 if (!BT_null)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000073 BT_null.reset(new BuiltinBug("Nil value used as mutex for @synchronized() "
74 "(no synchronization will occur)"));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000075 BugReport *report =
76 new BugReport(*BT_null, BT_null->getDescription(), N);
Ted Kremenek1e809b42012-03-09 01:13:14 +000077 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex,
78 report));
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000079
80 C.EmitReport(report);
Ted Kremenekc5644e12010-10-21 15:38:55 +000081 return;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000082 }
83 }
Ted Kremenekc5644e12010-10-21 15:38:55 +000084 // Don't add a transition for 'nullState'. If the value is
85 // under-constrained to be null or non-null, assume it is non-null
86 // afterwards.
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000087 }
88
89 if (notNullState)
Anna Zaksda4c8d62011-10-26 21:06:34 +000090 C.addTransition(notNullState);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000091}
Ted Kremenekc5644e12010-10-21 15:38:55 +000092
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000093void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000094 if (mgr.getLangOpts().ObjC2)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000095 mgr.registerChecker<ObjCAtSyncChecker>();
96}