blob: 293114ff2f484e3f54eb9f5682d35fc2717cdfb1 [file] [log] [blame]
Anna Zaks80412c42013-03-09 03:23:14 +00001//===--- NonNullParamChecker.cpp - Undefined arguments checker -*- C++ -*--===//
Zhongxing Xu94943b62009-11-03 07:35:33 +00002//
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//
Anna Zaks80412c42013-03-09 03:23:14 +000010// This defines NonNullParamChecker, which checks for arguments expected not to
11// be null due to:
12// - the corresponding parameters being declared to have nonnull attribute
13// - the corresponding parameters being references; since the call would form
14// a reference to a null pointer
Zhongxing Xu94943b62009-11-03 07:35:33 +000015//
16//===----------------------------------------------------------------------===//
17
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000018#include "ClangSACheckers.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000019#include "clang/AST/Attr.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000021#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000022#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosef540c542012-07-26 21:39:41 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000025
26using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Zhongxing Xu94943b62009-11-03 07:35:33 +000028
Ted Kremenekf493f492009-11-11 05:50:44 +000029namespace {
Anna Zaks80412c42013-03-09 03:23:14 +000030class NonNullParamChecker
Jordan Rosefe6a0112012-07-02 19:28:21 +000031 : public Checker< check::PreCall > {
Stephen Hines651f13c2014-04-23 16:59:28 -070032 mutable std::unique_ptr<BugType> BTAttrNonNull;
33 mutable std::unique_ptr<BugType> BTNullRefArg;
34
Ted Kremenekf493f492009-11-11 05:50:44 +000035public:
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000036
Jordan Rosefe6a0112012-07-02 19:28:21 +000037 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Anna Zaks018e9aa2013-03-07 03:02:36 +000038
39 BugReport *genReportNullAttrNonNull(const ExplodedNode *ErrorN,
40 const Expr *ArgE) const;
41 BugReport *genReportReferenceToNullPointer(const ExplodedNode *ErrorN,
42 const Expr *ArgE) const;
Ted Kremenekf493f492009-11-11 05:50:44 +000043};
44} // end anonymous namespace
45
Anna Zaks80412c42013-03-09 03:23:14 +000046void NonNullParamChecker::checkPreCall(const CallEvent &Call,
Stephen Hines651f13c2014-04-23 16:59:28 -070047 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +000048 const Decl *FD = Call.getDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000049 if (!FD)
50 return;
51
Jordan Rosefe6a0112012-07-02 19:28:21 +000052 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu94943b62009-11-03 07:35:33 +000053
Jordan Rosefe6a0112012-07-02 19:28:21 +000054 ProgramStateRef state = C.getState();
55
Anna Zaks018e9aa2013-03-07 03:02:36 +000056 CallEvent::param_type_iterator TyI = Call.param_type_begin(),
57 TyE = Call.param_type_end();
58
59 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx){
60
61 // Check if the parameter is a reference. We want to report when reference
62 // to a null pointer is passed as a paramter.
63 bool haveRefTypeParam = false;
64 if (TyI != TyE) {
65 haveRefTypeParam = (*TyI)->isReferenceType();
66 TyI++;
67 }
68
69 bool haveAttrNonNull = Att && Att->isNonNull(idx);
Stephen Hines651f13c2014-04-23 16:59:28 -070070 if (!haveAttrNonNull) {
71 // Check if the parameter is also marked 'nonnull'.
72 ArrayRef<ParmVarDecl*> parms = Call.parameters();
73 if (idx < parms.size())
74 haveAttrNonNull = parms[idx]->hasAttr<NonNullAttr>();
75 }
Anna Zaks018e9aa2013-03-07 03:02:36 +000076
77 if (!haveRefTypeParam && !haveAttrNonNull)
Zhongxing Xu94943b62009-11-03 07:35:33 +000078 continue;
79
Anna Zaks018e9aa2013-03-07 03:02:36 +000080 // If the value is unknown or undefined, we can't perform this check.
81 const Expr *ArgE = Call.getArgExpr(idx);
Jordan Rosefe6a0112012-07-02 19:28:21 +000082 SVal V = Call.getArgSVal(idx);
David Blaikiedc84cd52013-02-20 22:23:23 +000083 Optional<DefinedSVal> DV = V.getAs<DefinedSVal>();
Jordy Rose9a126852010-06-21 20:08:28 +000084 if (!DV)
85 continue;
Zhongxing Xu94943b62009-11-03 07:35:33 +000086
Anna Zaks018e9aa2013-03-07 03:02:36 +000087 // Process the case when the argument is not a location.
88 assert(!haveRefTypeParam || DV->getAs<Loc>());
89
90 if (haveAttrNonNull && !DV->getAs<Loc>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000091 // If the argument is a union type, we want to handle a potential
Jordan Rosefe6a0112012-07-02 19:28:21 +000092 // transparent_union GCC extension.
Jordan Rosefe6a0112012-07-02 19:28:21 +000093 if (!ArgE)
94 continue;
95
96 QualType T = ArgE->getType();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000097 const RecordType *UT = T->getAsUnionType();
98 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
99 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +0000100
David Blaikiedc84cd52013-02-20 22:23:23 +0000101 if (Optional<nonloc::CompoundVal> CSV =
David Blaikie5251abe2013-02-20 05:52:05 +0000102 DV->getAs<nonloc::CompoundVal>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +0000103 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
104 assert(CSV_I != CSV->end());
105 V = *CSV_I;
David Blaikie5251abe2013-02-20 05:52:05 +0000106 DV = V.getAs<DefinedSVal>();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +0000107 assert(++CSV_I == CSV->end());
Stephen Hines651f13c2014-04-23 16:59:28 -0700108 // FIXME: Handle (some_union){ some_other_union_val }, which turns into
109 // a LazyCompoundVal inside a CompoundVal.
110 if (!V.getAs<Loc>())
Anna Zaks42773d62013-03-06 20:26:02 +0000111 continue;
112 // Retrieve the corresponding expression.
113 if (const CompoundLiteralExpr *CE = dyn_cast<CompoundLiteralExpr>(ArgE))
114 if (const InitListExpr *IE =
115 dyn_cast<InitListExpr>(CE->getInitializer()))
116 ArgE = dyn_cast<Expr>(*(IE->begin()));
117
Jordan Rosefe6a0112012-07-02 19:28:21 +0000118 } else {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +0000119 // FIXME: Handle LazyCompoundVals?
120 continue;
121 }
122 }
123
Zhongxing Xu94943b62009-11-03 07:35:33 +0000124 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000125 ProgramStateRef stateNotNull, stateNull;
Stephen Hines651f13c2014-04-23 16:59:28 -0700126 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000127
128 if (stateNull && !stateNotNull) {
129 // Generate an error node. Check for a null node in case
130 // we cache out.
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000131 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-11-03 07:35:33 +0000132
Anna Zaks018e9aa2013-03-07 03:02:36 +0000133 BugReport *R = 0;
134 if (haveAttrNonNull)
135 R = genReportNullAttrNonNull(errorNode, ArgE);
136 else if (haveRefTypeParam)
137 R = genReportReferenceToNullPointer(errorNode, ArgE);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000138
139 // Highlight the range of the argument that was null.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000140 R->addRange(Call.getArgSourceRange(idx));
Anna Zaks018e9aa2013-03-07 03:02:36 +0000141
Zhongxing Xu94943b62009-11-03 07:35:33 +0000142 // Emit the bug report.
Jordan Rose785950e2012-11-02 01:53:40 +0000143 C.emitReport(R);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000144 }
145
146 // Always return. Either we cached out or we just emitted an error.
147 return;
148 }
149
150 // If a pointer value passed the check we should assume that it is
151 // indeed not null from this point forward.
152 assert(stateNotNull);
153 state = stateNotNull;
154 }
155
156 // If we reach here all of the arguments passed the nonnull check.
157 // If 'state' has been updated generated a new node.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000158 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000159}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000160
Anna Zaks80412c42013-03-09 03:23:14 +0000161BugReport *NonNullParamChecker::genReportNullAttrNonNull(
Anna Zaks018e9aa2013-03-07 03:02:36 +0000162 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
163 // Lazily allocate the BugType object if it hasn't already been
164 // created. Ownership is transferred to the BugReporter object once
165 // the BugReport is passed to 'EmitWarning'.
166 if (!BTAttrNonNull)
167 BTAttrNonNull.reset(new BugType(
Stephen Hines651f13c2014-04-23 16:59:28 -0700168 this, "Argument with 'nonnull' attribute passed null", "API"));
Anna Zaks018e9aa2013-03-07 03:02:36 +0000169
170 BugReport *R = new BugReport(*BTAttrNonNull,
171 "Null pointer passed as an argument to a 'nonnull' parameter",
172 ErrorNode);
173 if (ArgE)
174 bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R);
175
176 return R;
177}
178
Anna Zaks80412c42013-03-09 03:23:14 +0000179BugReport *NonNullParamChecker::genReportReferenceToNullPointer(
Anna Zaks018e9aa2013-03-07 03:02:36 +0000180 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
181 if (!BTNullRefArg)
Stephen Hines651f13c2014-04-23 16:59:28 -0700182 BTNullRefArg.reset(new BuiltinBug(this, "Dereference of null pointer"));
Anna Zaks018e9aa2013-03-07 03:02:36 +0000183
184 BugReport *R = new BugReport(*BTNullRefArg,
185 "Forming reference to null pointer",
186 ErrorNode);
187 if (ArgE) {
188 const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE);
189 if (ArgEDeref == 0)
190 ArgEDeref = ArgE;
191 bugreporter::trackNullOrUndefValue(ErrorNode,
192 ArgEDeref,
193 *R);
194 }
195 return R;
196
197}
198
Anna Zaks80412c42013-03-09 03:23:14 +0000199void ento::registerNonNullParamChecker(CheckerManager &mgr) {
200 mgr.registerChecker<NonNullParamChecker>();
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000201}