blob: 40619a2a85326d36be74bc7252613ab322b4d57d [file] [log] [blame]
Ted Kremenek942e24d2010-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 Kyrtzidisd2592a32010-12-22 18:53:44 +000015#include "ExprEngineInternalChecks.h"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000016#include "clang/GR/BugReporter/BugType.h"
17#include "clang/GR/Checkers/DereferenceChecker.h"
18#include "clang/GR/PathSensitive/CheckerVisitor.h"
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000019#include "clang/GR/PathSensitive/ExprEngine.h"
Ted Kremenek942e24d2010-09-10 03:45:29 +000020
21using namespace clang;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000022using namespace GR;
Ted Kremenek942e24d2010-09-10 03:45:29 +000023
24namespace {
25class ObjCAtSyncChecker : public CheckerVisitor<ObjCAtSyncChecker> {
26 BuiltinBug *BT_null;
27 BuiltinBug *BT_undef;
28public:
29 ObjCAtSyncChecker() : BT_null(0), BT_undef(0) {}
30 static void *getTag() { static int tag = 0; return &tag; }
31 void PreVisitObjCAtSynchronizedStmt(CheckerContext &C,
32 const ObjCAtSynchronizedStmt *S);
33};
34} // end anonymous namespace
35
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000036void GR::RegisterObjCAtSyncChecker(ExprEngine &Eng) {
Ted Kremenek67e40d42010-11-08 16:52:54 +000037 // @synchronized is an Objective-C 2 feature.
38 if (Eng.getContext().getLangOptions().ObjC2)
39 Eng.registerCheck(new ObjCAtSyncChecker());
Ted Kremenek942e24d2010-09-10 03:45:29 +000040}
41
42void ObjCAtSyncChecker::PreVisitObjCAtSynchronizedStmt(CheckerContext &C,
43 const ObjCAtSynchronizedStmt *S) {
44
45 const Expr *Ex = S->getSynchExpr();
46 const GRState *state = C.getState();
47 SVal V = state->getSVal(Ex);
48
49 // Uninitialized value used for the mutex?
50 if (isa<UndefinedVal>(V)) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000051 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek942e24d2010-09-10 03:45:29 +000052 if (!BT_undef)
53 BT_undef = new BuiltinBug("Uninitialized value used as mutex "
54 "for @synchronized");
55 EnhancedBugReport *report =
56 new EnhancedBugReport(*BT_undef, BT_undef->getDescription(), N);
57 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
58 C.EmitReport(report);
59 }
60 return;
61 }
62
Ted Kremenek0d4f7672010-10-25 20:20:56 +000063 if (V.isUnknown())
64 return;
65
Ted Kremenek942e24d2010-09-10 03:45:29 +000066 // Check for null mutexes.
67 const GRState *notNullState, *nullState;
Ted Kremenek28f47b92010-12-01 22:16:56 +000068 llvm::tie(notNullState, nullState) = state->assume(cast<DefinedSVal>(V));
Ted Kremenek942e24d2010-09-10 03:45:29 +000069
70 if (nullState) {
71 if (!notNullState) {
72 // Generate an error node. This isn't a sink since
73 // a null mutex just means no synchronization occurs.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000074 if (ExplodedNode *N = C.generateNode(nullState)) {
Ted Kremenek942e24d2010-09-10 03:45:29 +000075 if (!BT_null)
76 BT_null = new BuiltinBug("Nil value used as mutex for @synchronized() "
77 "(no synchronization will occur)");
78 EnhancedBugReport *report =
79 new EnhancedBugReport(*BT_null, BT_null->getDescription(), N);
80 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
81 Ex);
82
83 C.EmitReport(report);
Ted Kremenek1adee4b2010-10-21 15:38:55 +000084 return;
Ted Kremenek942e24d2010-09-10 03:45:29 +000085 }
86 }
Ted Kremenek1adee4b2010-10-21 15:38:55 +000087 // Don't add a transition for 'nullState'. If the value is
88 // under-constrained to be null or non-null, assume it is non-null
89 // afterwards.
Ted Kremenek942e24d2010-09-10 03:45:29 +000090 }
91
92 if (notNullState)
93 C.addTransition(notNullState);
94}
Ted Kremenek1adee4b2010-10-21 15:38:55 +000095