blob: 01d2c0491b85f0cb934dc115061b908078749924 [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
Gabor Horvath8d3ad6b2015-08-27 18:49:07 +000031 : public Checker< check::PreCall, EventDispatcher<ImplicitNullDerefEvent> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000032 mutable std::unique_ptr<BugType> BTAttrNonNull;
33 mutable std::unique_ptr<BugType> BTNullRefArg;
34
Ted Kremenek43253152009-11-11 05:50:44 +000035public:
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000036
Jordan Rose682b3162012-07-02 19:28:21 +000037 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Anna Zaks9e0da9e02013-03-07 03:02:36 +000038
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000039 std::unique_ptr<BugReport>
40 genReportNullAttrNonNull(const ExplodedNode *ErrorN, const Expr *ArgE) const;
41 std::unique_ptr<BugReport>
42 genReportReferenceToNullPointer(const ExplodedNode *ErrorN,
43 const Expr *ArgE) const;
Ted Kremenek43253152009-11-11 05:50:44 +000044};
45} // end anonymous namespace
46
George Karpenkov65e64912018-03-07 19:27:32 +000047/// \return Bitvector marking non-null attributes.
48static llvm::SmallBitVector getNonNullAttrs(const CallEvent &Call) {
Jordan Rose682b3162012-07-02 19:28:21 +000049 const Decl *FD = Call.getDecl();
Jordan Rose679659f2014-10-13 19:38:02 +000050 unsigned NumArgs = Call.getNumArgs();
51 llvm::SmallBitVector AttrNonNull(NumArgs);
52 for (const auto *NonNull : FD->specific_attrs<NonNullAttr>()) {
53 if (!NonNull->args_size()) {
54 AttrNonNull.set(0, NumArgs);
55 break;
56 }
Joel E. Denny81508102018-03-13 14:51:22 +000057 for (const ParamIdx &Idx : NonNull->args()) {
58 unsigned IdxAST = Idx.getASTIndex();
59 if (IdxAST >= NumArgs)
Jordan Rose679659f2014-10-13 19:38:02 +000060 continue;
Joel E. Denny81508102018-03-13 14:51:22 +000061 AttrNonNull.set(IdxAST);
Jordan Rose679659f2014-10-13 19:38:02 +000062 }
63 }
George Karpenkov65e64912018-03-07 19:27:32 +000064 return AttrNonNull;
65}
66
67void NonNullParamChecker::checkPreCall(const CallEvent &Call,
68 CheckerContext &C) const {
69 if (!Call.getDecl())
70 return;
71
72 llvm::SmallBitVector AttrNonNull = getNonNullAttrs(Call);
73 unsigned NumArgs = Call.getNumArgs();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000074
Jordan Rose682b3162012-07-02 19:28:21 +000075 ProgramStateRef state = C.getState();
George Karpenkov65e64912018-03-07 19:27:32 +000076 ArrayRef<ParmVarDecl*> parms = Call.parameters();
Anna Zaks9e0da9e02013-03-07 03:02:36 +000077
Jordan Rose679659f2014-10-13 19:38:02 +000078 for (unsigned idx = 0; idx < NumArgs; ++idx) {
George Karpenkov65e64912018-03-07 19:27:32 +000079 // For vararg functions, a corresponding parameter decl may not exist.
80 bool HasParam = idx < parms.size();
Anna Zaks9e0da9e02013-03-07 03:02:36 +000081
82 // Check if the parameter is a reference. We want to report when reference
Simon Pilgrim2c518802017-03-30 14:13:19 +000083 // to a null pointer is passed as a parameter.
George Karpenkov65e64912018-03-07 19:27:32 +000084 bool haveRefTypeParam =
85 HasParam ? parms[idx]->getType()->isReferenceType() : false;
Jordan Rose679659f2014-10-13 19:38:02 +000086 bool haveAttrNonNull = AttrNonNull[idx];
Anna Zaks9e0da9e02013-03-07 03:02:36 +000087
George Karpenkov65e64912018-03-07 19:27:32 +000088 // Check if the parameter is also marked 'nonnull'.
89 if (!haveAttrNonNull && HasParam)
90 haveAttrNonNull = parms[idx]->hasAttr<NonNullAttr>();
91
92 if (!haveAttrNonNull && !haveRefTypeParam)
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000093 continue;
94
Anna Zaks9e0da9e02013-03-07 03:02:36 +000095 // If the value is unknown or undefined, we can't perform this check.
96 const Expr *ArgE = Call.getArgExpr(idx);
Jordan Rose682b3162012-07-02 19:28:21 +000097 SVal V = Call.getArgSVal(idx);
George Karpenkov65e64912018-03-07 19:27:32 +000098 auto DV = V.getAs<DefinedSVal>();
Jordy Rose3d858882010-06-21 20:08:28 +000099 if (!DV)
100 continue;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000101
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000102 assert(!haveRefTypeParam || DV->getAs<Loc>());
103
George Karpenkov65e64912018-03-07 19:27:32 +0000104 // Process the case when the argument is not a location.
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000105 if (haveAttrNonNull && !DV->getAs<Loc>()) {
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000106 // If the argument is a union type, we want to handle a potential
Jordan Rose682b3162012-07-02 19:28:21 +0000107 // transparent_union GCC extension.
Jordan Rose682b3162012-07-02 19:28:21 +0000108 if (!ArgE)
109 continue;
110
111 QualType T = ArgE->getType();
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000112 const RecordType *UT = T->getAsUnionType();
113 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
114 continue;
Jordan Rose682b3162012-07-02 19:28:21 +0000115
George Karpenkov65e64912018-03-07 19:27:32 +0000116 auto CSV = DV->getAs<nonloc::CompoundVal>();
Anna Zaks23c85ed2013-03-06 20:26:02 +0000117
George Karpenkov65e64912018-03-07 19:27:32 +0000118 // FIXME: Handle LazyCompoundVals?
119 if (!CSV)
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000120 continue;
George Karpenkov65e64912018-03-07 19:27:32 +0000121
122 V = *(CSV->begin());
123 DV = V.getAs<DefinedSVal>();
124 assert(++CSV->begin() == CSV->end());
125 // FIXME: Handle (some_union){ some_other_union_val }, which turns into
126 // a LazyCompoundVal inside a CompoundVal.
127 if (!V.getAs<Loc>())
128 continue;
129
130 // Retrieve the corresponding expression.
131 if (const auto *CE = dyn_cast<CompoundLiteralExpr>(ArgE))
132 if (const auto *IE = dyn_cast<InitListExpr>(CE->getInitializer()))
133 ArgE = dyn_cast<Expr>(*(IE->begin()));
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000134 }
135
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000136 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek49b1e382012-01-26 21:29:00 +0000137 ProgramStateRef stateNotNull, stateNull;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000138 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000139
George Karpenkov65e64912018-03-07 19:27:32 +0000140 // Generate an error node. Check for a null node in case
141 // we cache out.
142 if (stateNull && !stateNotNull) {
143 if (ExplodedNode *errorNode = C.generateErrorNode(stateNull)) {
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000144
George Karpenkov65e64912018-03-07 19:27:32 +0000145 std::unique_ptr<BugReport> R;
146 if (haveAttrNonNull)
147 R = genReportNullAttrNonNull(errorNode, ArgE);
148 else if (haveRefTypeParam)
149 R = genReportReferenceToNullPointer(errorNode, ArgE);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000150
George Karpenkov65e64912018-03-07 19:27:32 +0000151 // Highlight the range of the argument that was null.
152 R->addRange(Call.getArgSourceRange(idx));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000153
George Karpenkov65e64912018-03-07 19:27:32 +0000154 // Emit the bug report.
155 C.emitReport(std::move(R));
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000156 }
George Karpenkov65e64912018-03-07 19:27:32 +0000157
158 // Always return. Either we cached out or we just emitted an error.
159 return;
160 }
161
162 if (stateNull) {
Devin Coughline39bd402015-09-16 22:03:05 +0000163 if (ExplodedNode *N = C.generateSink(stateNull, C.getPredecessor())) {
Gabor Horvath8d3ad6b2015-08-27 18:49:07 +0000164 ImplicitNullDerefEvent event = {
George Karpenkov65e64912018-03-07 19:27:32 +0000165 V, false, N, &C.getBugReporter(),
166 /*IsDirectDereference=*/haveRefTypeParam};
Gabor Horvath8d3ad6b2015-08-27 18:49:07 +0000167 dispatchEvent(event);
168 }
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000169 }
170
171 // If a pointer value passed the check we should assume that it is
172 // indeed not null from this point forward.
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000173 state = stateNotNull;
174 }
175
176 // If we reach here all of the arguments passed the nonnull check.
177 // If 'state' has been updated generated a new node.
Anna Zaksda4c8d62011-10-26 21:06:34 +0000178 C.addTransition(state);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000179}
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000180
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000181std::unique_ptr<BugReport>
182NonNullParamChecker::genReportNullAttrNonNull(const ExplodedNode *ErrorNode,
183 const Expr *ArgE) const {
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000184 // Lazily allocate the BugType object if it hasn't already been
185 // created. Ownership is transferred to the BugReporter object once
186 // the BugReport is passed to 'EmitWarning'.
187 if (!BTAttrNonNull)
188 BTAttrNonNull.reset(new BugType(
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000189 this, "Argument with 'nonnull' attribute passed null", "API"));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000190
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000191 auto R = llvm::make_unique<BugReport>(
192 *BTAttrNonNull,
193 "Null pointer passed as an argument to a 'nonnull' parameter", ErrorNode);
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000194 if (ArgE)
195 bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R);
196
197 return R;
198}
199
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000200std::unique_ptr<BugReport> NonNullParamChecker::genReportReferenceToNullPointer(
201 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000202 if (!BTNullRefArg)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000203 BTNullRefArg.reset(new BuiltinBug(this, "Dereference of null pointer"));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000204
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000205 auto R = llvm::make_unique<BugReport>(
206 *BTNullRefArg, "Forming reference to null pointer", ErrorNode);
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000207 if (ArgE) {
208 const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE);
Craig Topper0dbb7832014-05-27 02:45:47 +0000209 if (!ArgEDeref)
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000210 ArgEDeref = ArgE;
211 bugreporter::trackNullOrUndefValue(ErrorNode,
212 ArgEDeref,
213 *R);
214 }
215 return R;
216
217}
218
Anna Zaksef893392013-03-09 03:23:14 +0000219void ento::registerNonNullParamChecker(CheckerManager &mgr) {
220 mgr.registerChecker<NonNullParamChecker>();
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000221}