blob: fb7ae2c76f24dc4053b250ed00f10ccfd6eb93c8 [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> > {
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000030 mutable llvm::OwningPtr<BuiltinBug> BT_null;
31 mutable llvm::OwningPtr<BuiltinBug> BT_undef;
32
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);
Anna Zaksf86615c2011-08-19 22:33:38 +000053 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000054 C.EmitReport(report);
55 }
56 return;
57 }
58
Ted Kremenekc07d8352010-10-25 20:20:56 +000059 if (V.isUnknown())
60 return;
61
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000062 // Check for null mutexes.
Ted Kremenek49b1e382012-01-26 21:29:00 +000063 ProgramStateRef notNullState, nullState;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000064 llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000065
66 if (nullState) {
67 if (!notNullState) {
68 // Generate an error node. This isn't a sink since
69 // a null mutex just means no synchronization occurs.
Anna Zaksda4c8d62011-10-26 21:06:34 +000070 if (ExplodedNode *N = C.addTransition(nullState)) {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000071 if (!BT_null)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000072 BT_null.reset(new BuiltinBug("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);
Anna Zaksf86615c2011-08-19 22:33:38 +000076 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000077
78 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) {
92 if (mgr.getLangOptions().ObjC2)
93 mgr.registerChecker<ObjCAtSyncChecker>();
94}