blob: bad27899835955cec427dbc343f68bca67e5b650 [file] [log] [blame]
Anna Zaksef893392013-03-09 03:23:14 +00001//===--- NonNullParamChecker.cpp - Undefined arguments checker -*- C++ -*--===//
Zhongxing Xu9b9d7312009-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 Zaksef893392013-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 Xu9b9d7312009-11-03 07:35:33 +000015//
16//===----------------------------------------------------------------------===//
17
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000018#include "ClangSACheckers.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000021#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000022#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rose4f7df9b2012-07-26 21:39:41 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000024#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000025
26using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000027using namespace ento;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000028
Ted Kremenek43253152009-11-11 05:50:44 +000029namespace {
Anna Zaksef893392013-03-09 03:23:14 +000030class NonNullParamChecker
Jordan Rose682b3162012-07-02 19:28:21 +000031 : public Checker< check::PreCall > {
Anna Zaks9e0da9e02013-03-07 03:02:36 +000032 mutable OwningPtr<BugType> BTAttrNonNull;
33 mutable OwningPtr<BugType> BTNullRefArg;
Ted Kremenek43253152009-11-11 05:50:44 +000034public:
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000035
Jordan Rose682b3162012-07-02 19:28:21 +000036 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Anna Zaks9e0da9e02013-03-07 03:02:36 +000037
38 BugReport *genReportNullAttrNonNull(const ExplodedNode *ErrorN,
39 const Expr *ArgE) const;
40 BugReport *genReportReferenceToNullPointer(const ExplodedNode *ErrorN,
41 const Expr *ArgE) const;
Ted Kremenek43253152009-11-11 05:50:44 +000042};
43} // end anonymous namespace
44
Anna Zaksef893392013-03-09 03:23:14 +000045void NonNullParamChecker::checkPreCall(const CallEvent &Call,
Ted Kremenekf0ae7d02014-01-17 07:15:35 +000046 CheckerContext &C) const {
Jordan Rose682b3162012-07-02 19:28:21 +000047 const Decl *FD = Call.getDecl();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000048 if (!FD)
49 return;
50
Jordan Rose682b3162012-07-02 19:28:21 +000051 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000052
Jordan Rose682b3162012-07-02 19:28:21 +000053 ProgramStateRef state = C.getState();
54
Anna Zaks9e0da9e02013-03-07 03:02:36 +000055 CallEvent::param_type_iterator TyI = Call.param_type_begin(),
56 TyE = Call.param_type_end();
57
58 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx){
59
60 // Check if the parameter is a reference. We want to report when reference
61 // to a null pointer is passed as a paramter.
62 bool haveRefTypeParam = false;
63 if (TyI != TyE) {
64 haveRefTypeParam = (*TyI)->isReferenceType();
65 TyI++;
66 }
67
68 bool haveAttrNonNull = Att && Att->isNonNull(idx);
Ted Kremenekf0ae7d02014-01-17 07:15:35 +000069 if (!haveAttrNonNull) {
70 // Check if the parameter is also marked 'nonnull'.
71 ArrayRef<ParmVarDecl*> parms = Call.parameters();
72 if (idx < parms.size())
73 haveAttrNonNull = parms[idx]->hasAttr<NonNullAttr>();
74 }
Anna Zaks9e0da9e02013-03-07 03:02:36 +000075
76 if (!haveRefTypeParam && !haveAttrNonNull)
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000077 continue;
78
Anna Zaks9e0da9e02013-03-07 03:02:36 +000079 // If the value is unknown or undefined, we can't perform this check.
80 const Expr *ArgE = Call.getArgExpr(idx);
Jordan Rose682b3162012-07-02 19:28:21 +000081 SVal V = Call.getArgSVal(idx);
David Blaikie05785d12013-02-20 22:23:23 +000082 Optional<DefinedSVal> DV = V.getAs<DefinedSVal>();
Jordy Rose3d858882010-06-21 20:08:28 +000083 if (!DV)
84 continue;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000085
Anna Zaks9e0da9e02013-03-07 03:02:36 +000086 // Process the case when the argument is not a location.
87 assert(!haveRefTypeParam || DV->getAs<Loc>());
88
89 if (haveAttrNonNull && !DV->getAs<Loc>()) {
Ted Kremenekdcf85a82010-11-09 02:11:43 +000090 // If the argument is a union type, we want to handle a potential
Jordan Rose682b3162012-07-02 19:28:21 +000091 // transparent_union GCC extension.
Jordan Rose682b3162012-07-02 19:28:21 +000092 if (!ArgE)
93 continue;
94
95 QualType T = ArgE->getType();
Ted Kremenekdcf85a82010-11-09 02:11:43 +000096 const RecordType *UT = T->getAsUnionType();
97 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
98 continue;
Jordan Rose682b3162012-07-02 19:28:21 +000099
David Blaikie05785d12013-02-20 22:23:23 +0000100 if (Optional<nonloc::CompoundVal> CSV =
David Blaikie2fdacbc2013-02-20 05:52:05 +0000101 DV->getAs<nonloc::CompoundVal>()) {
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000102 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
103 assert(CSV_I != CSV->end());
104 V = *CSV_I;
David Blaikie2fdacbc2013-02-20 05:52:05 +0000105 DV = V.getAs<DefinedSVal>();
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000106 assert(++CSV_I == CSV->end());
Jordan Rosee359d012014-02-26 01:20:19 +0000107 // FIXME: Handle (some_union){ some_other_union_val }, which turns into
108 // a LazyCompoundVal inside a CompoundVal.
109 if (!V.getAs<Loc>())
Anna Zaks23c85ed2013-03-06 20:26:02 +0000110 continue;
111 // Retrieve the corresponding expression.
112 if (const CompoundLiteralExpr *CE = dyn_cast<CompoundLiteralExpr>(ArgE))
113 if (const InitListExpr *IE =
114 dyn_cast<InitListExpr>(CE->getInitializer()))
115 ArgE = dyn_cast<Expr>(*(IE->begin()));
116
Jordan Rose682b3162012-07-02 19:28:21 +0000117 } else {
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000118 // FIXME: Handle LazyCompoundVals?
119 continue;
120 }
121 }
122
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000123 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek49b1e382012-01-26 21:29:00 +0000124 ProgramStateRef stateNotNull, stateNull;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000125 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000126
127 if (stateNull && !stateNotNull) {
128 // Generate an error node. Check for a null node in case
129 // we cache out.
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000130 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000131
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000132 BugReport *R = 0;
133 if (haveAttrNonNull)
134 R = genReportNullAttrNonNull(errorNode, ArgE);
135 else if (haveRefTypeParam)
136 R = genReportReferenceToNullPointer(errorNode, ArgE);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000137
138 // Highlight the range of the argument that was null.
Jordan Rose682b3162012-07-02 19:28:21 +0000139 R->addRange(Call.getArgSourceRange(idx));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000140
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000141 // Emit the bug report.
Jordan Rosee10d5a72012-11-02 01:53:40 +0000142 C.emitReport(R);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000143 }
144
145 // Always return. Either we cached out or we just emitted an error.
146 return;
147 }
148
149 // If a pointer value passed the check we should assume that it is
150 // indeed not null from this point forward.
151 assert(stateNotNull);
152 state = stateNotNull;
153 }
154
155 // If we reach here all of the arguments passed the nonnull check.
156 // If 'state' has been updated generated a new node.
Anna Zaksda4c8d62011-10-26 21:06:34 +0000157 C.addTransition(state);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000158}
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000159
Anna Zaksef893392013-03-09 03:23:14 +0000160BugReport *NonNullParamChecker::genReportNullAttrNonNull(
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000161 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
162 // Lazily allocate the BugType object if it hasn't already been
163 // created. Ownership is transferred to the BugReporter object once
164 // the BugReport is passed to 'EmitWarning'.
165 if (!BTAttrNonNull)
166 BTAttrNonNull.reset(new BugType(
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000167 this, "Argument with 'nonnull' attribute passed null", "API"));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000168
169 BugReport *R = new BugReport(*BTAttrNonNull,
170 "Null pointer passed as an argument to a 'nonnull' parameter",
171 ErrorNode);
172 if (ArgE)
173 bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R);
174
175 return R;
176}
177
Anna Zaksef893392013-03-09 03:23:14 +0000178BugReport *NonNullParamChecker::genReportReferenceToNullPointer(
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000179 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
180 if (!BTNullRefArg)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000181 BTNullRefArg.reset(new BuiltinBug(this, "Dereference of null pointer"));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000182
183 BugReport *R = new BugReport(*BTNullRefArg,
184 "Forming reference to null pointer",
185 ErrorNode);
186 if (ArgE) {
187 const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE);
188 if (ArgEDeref == 0)
189 ArgEDeref = ArgE;
190 bugreporter::trackNullOrUndefValue(ErrorNode,
191 ArgEDeref,
192 *R);
193 }
194 return R;
195
196}
197
Anna Zaksef893392013-03-09 03:23:14 +0000198void ento::registerNonNullParamChecker(CheckerManager &mgr) {
199 mgr.registerChecker<NonNullParamChecker>();
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000200}