blob: d1593419e8415bfe167437e601dcfe8e09c4243d [file] [log] [blame]
Zhongxing Xu94943b62009-11-03 07:35:33 +00001//===--- AttrNonNullChecker.h - Undefined arguments checker ----*- 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//
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000010// This defines AttrNonNullChecker, a builtin check in ExprEngine that
Zhongxing Xu94943b62009-11-03 07:35:33 +000011// performs checks for arguments declared to have nonnull attribute.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000015#include "ClangSACheckers.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosef540c542012-07-26 21:39:41 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000022
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xu94943b62009-11-03 07:35:33 +000025
Ted Kremenekf493f492009-11-11 05:50:44 +000026namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000027class AttrNonNullChecker
Jordan Rosefe6a0112012-07-02 19:28:21 +000028 : public Checker< check::PreCall > {
Anna Zaks018e9aa2013-03-07 03:02:36 +000029 mutable OwningPtr<BugType> BTAttrNonNull;
30 mutable OwningPtr<BugType> BTNullRefArg;
Ted Kremenekf493f492009-11-11 05:50:44 +000031public:
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000032
Jordan Rosefe6a0112012-07-02 19:28:21 +000033 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Anna Zaks018e9aa2013-03-07 03:02:36 +000034
35 BugReport *genReportNullAttrNonNull(const ExplodedNode *ErrorN,
36 const Expr *ArgE) const;
37 BugReport *genReportReferenceToNullPointer(const ExplodedNode *ErrorN,
38 const Expr *ArgE) const;
Ted Kremenekf493f492009-11-11 05:50:44 +000039};
40} // end anonymous namespace
41
Jordan Rosefe6a0112012-07-02 19:28:21 +000042void AttrNonNullChecker::checkPreCall(const CallEvent &Call,
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000043 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +000044 const Decl *FD = Call.getDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000045 if (!FD)
46 return;
47
Jordan Rosefe6a0112012-07-02 19:28:21 +000048 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu94943b62009-11-03 07:35:33 +000049
Jordan Rosefe6a0112012-07-02 19:28:21 +000050 ProgramStateRef state = C.getState();
51
Anna Zaks018e9aa2013-03-07 03:02:36 +000052 CallEvent::param_type_iterator TyI = Call.param_type_begin(),
53 TyE = Call.param_type_end();
54
55 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx){
56
57 // Check if the parameter is a reference. We want to report when reference
58 // to a null pointer is passed as a paramter.
59 bool haveRefTypeParam = false;
60 if (TyI != TyE) {
61 haveRefTypeParam = (*TyI)->isReferenceType();
62 TyI++;
63 }
64
65 bool haveAttrNonNull = Att && Att->isNonNull(idx);
66
67 if (!haveRefTypeParam && !haveAttrNonNull)
Zhongxing Xu94943b62009-11-03 07:35:33 +000068 continue;
69
Anna Zaks018e9aa2013-03-07 03:02:36 +000070 // If the value is unknown or undefined, we can't perform this check.
71 const Expr *ArgE = Call.getArgExpr(idx);
Jordan Rosefe6a0112012-07-02 19:28:21 +000072 SVal V = Call.getArgSVal(idx);
David Blaikiedc84cd52013-02-20 22:23:23 +000073 Optional<DefinedSVal> DV = V.getAs<DefinedSVal>();
Jordy Rose9a126852010-06-21 20:08:28 +000074 if (!DV)
75 continue;
Zhongxing Xu94943b62009-11-03 07:35:33 +000076
Anna Zaks018e9aa2013-03-07 03:02:36 +000077 // Process the case when the argument is not a location.
78 assert(!haveRefTypeParam || DV->getAs<Loc>());
79
80 if (haveAttrNonNull && !DV->getAs<Loc>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000081 // If the argument is a union type, we want to handle a potential
Jordan Rosefe6a0112012-07-02 19:28:21 +000082 // transparent_union GCC extension.
Jordan Rosefe6a0112012-07-02 19:28:21 +000083 if (!ArgE)
84 continue;
85
86 QualType T = ArgE->getType();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000087 const RecordType *UT = T->getAsUnionType();
88 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
89 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +000090
David Blaikiedc84cd52013-02-20 22:23:23 +000091 if (Optional<nonloc::CompoundVal> CSV =
David Blaikie5251abe2013-02-20 05:52:05 +000092 DV->getAs<nonloc::CompoundVal>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000093 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
94 assert(CSV_I != CSV->end());
95 V = *CSV_I;
David Blaikie5251abe2013-02-20 05:52:05 +000096 DV = V.getAs<DefinedSVal>();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000097 assert(++CSV_I == CSV->end());
98 if (!DV)
Anna Zaks42773d62013-03-06 20:26:02 +000099 continue;
100 // Retrieve the corresponding expression.
101 if (const CompoundLiteralExpr *CE = dyn_cast<CompoundLiteralExpr>(ArgE))
102 if (const InitListExpr *IE =
103 dyn_cast<InitListExpr>(CE->getInitializer()))
104 ArgE = dyn_cast<Expr>(*(IE->begin()));
105
Jordan Rosefe6a0112012-07-02 19:28:21 +0000106 } else {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +0000107 // FIXME: Handle LazyCompoundVals?
108 continue;
109 }
110 }
111
Zhongxing Xu94943b62009-11-03 07:35:33 +0000112 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000113 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000114 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000115
116 if (stateNull && !stateNotNull) {
117 // Generate an error node. Check for a null node in case
118 // we cache out.
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000119 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-11-03 07:35:33 +0000120
Anna Zaks018e9aa2013-03-07 03:02:36 +0000121 BugReport *R = 0;
122 if (haveAttrNonNull)
123 R = genReportNullAttrNonNull(errorNode, ArgE);
124 else if (haveRefTypeParam)
125 R = genReportReferenceToNullPointer(errorNode, ArgE);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000126
127 // Highlight the range of the argument that was null.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000128 R->addRange(Call.getArgSourceRange(idx));
Anna Zaks018e9aa2013-03-07 03:02:36 +0000129
Zhongxing Xu94943b62009-11-03 07:35:33 +0000130 // Emit the bug report.
Jordan Rose785950e2012-11-02 01:53:40 +0000131 C.emitReport(R);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000132 }
133
134 // Always return. Either we cached out or we just emitted an error.
135 return;
136 }
137
138 // If a pointer value passed the check we should assume that it is
139 // indeed not null from this point forward.
140 assert(stateNotNull);
141 state = stateNotNull;
142 }
143
144 // If we reach here all of the arguments passed the nonnull check.
145 // If 'state' has been updated generated a new node.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000146 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000147}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000148
Anna Zaks018e9aa2013-03-07 03:02:36 +0000149BugReport *AttrNonNullChecker::genReportNullAttrNonNull(
150 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
151 // Lazily allocate the BugType object if it hasn't already been
152 // created. Ownership is transferred to the BugReporter object once
153 // the BugReport is passed to 'EmitWarning'.
154 if (!BTAttrNonNull)
155 BTAttrNonNull.reset(new BugType(
156 "Argument with 'nonnull' attribute passed null",
157 "API"));
158
159 BugReport *R = new BugReport(*BTAttrNonNull,
160 "Null pointer passed as an argument to a 'nonnull' parameter",
161 ErrorNode);
162 if (ArgE)
163 bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R);
164
165 return R;
166}
167
168BugReport *AttrNonNullChecker::genReportReferenceToNullPointer(
169 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
170 if (!BTNullRefArg)
171 BTNullRefArg.reset(new BuiltinBug("Dereference of null pointer"));
172
173 BugReport *R = new BugReport(*BTNullRefArg,
174 "Forming reference to null pointer",
175 ErrorNode);
176 if (ArgE) {
177 const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE);
178 if (ArgEDeref == 0)
179 ArgEDeref = ArgE;
180 bugreporter::trackNullOrUndefValue(ErrorNode,
181 ArgEDeref,
182 *R);
183 }
184 return R;
185
186}
187
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000188void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
189 mgr.registerChecker<AttrNonNullChecker>();
190}