blob: caf5b29cc334ab0da3f643cbcfdafb40ed790a00 [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 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> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000029 mutable OwningPtr<BuiltinBug> BT_null;
30 mutable OwningPtr<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?
45 if (isa<UndefinedVal>(V)) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +000046 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000047 if (!BT_undef)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000048 BT_undef.reset(new BuiltinBug("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 Rosecfb4eb22012-08-03 23:09:01 +000052 bugreporter::addTrackNullOrUndefValueVisitor(N, Ex, report);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000053 C.EmitReport(report);
54 }
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;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000063 llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
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)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000071 BT_null.reset(new BuiltinBug("Nil value used as mutex for @synchronized() "
72 "(no synchronization will occur)"));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000073 BugReport *report =
74 new BugReport(*BT_null, BT_null->getDescription(), N);
Jordan Rosecfb4eb22012-08-03 23:09:01 +000075 bugreporter::addTrackNullOrUndefValueVisitor(N, Ex, report);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000076
77 C.EmitReport(report);
Ted Kremenekc5644e12010-10-21 15:38:55 +000078 return;
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000079 }
80 }
Ted Kremenekc5644e12010-10-21 15:38:55 +000081 // Don't add a transition for 'nullState'. If the value is
82 // under-constrained to be null or non-null, assume it is non-null
83 // afterwards.
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000084 }
85
86 if (notNullState)
Anna Zaksda4c8d62011-10-26 21:06:34 +000087 C.addTransition(notNullState);
Ted Kremenek6c0cc5e2010-09-10 03:45:29 +000088}
Ted Kremenekc5644e12010-10-21 15:38:55 +000089
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000090void ento::registerObjCAtSyncChecker(CheckerManager &mgr) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000091 if (mgr.getLangOpts().ObjC2)
Argyrios Kyrtzidisaad83722011-02-23 07:19:18 +000092 mgr.registerChecker<ObjCAtSyncChecker>();
93}