blob: 838be2829edcf51d4e2088ba2aa84f066df9f945 [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 }
Nico Weberbbf64822018-03-07 02:22:41 +000057 for (unsigned Val : NonNull->args()) {
58 if (Val >= NumArgs)
Jordan Rose679659f2014-10-13 19:38:02 +000059 continue;
Nico Weberbbf64822018-03-07 02:22:41 +000060 AttrNonNull.set(Val);
Jordan Rose679659f2014-10-13 19:38:02 +000061 }
62 }
George Karpenkov65e64912018-03-07 19:27:32 +000063 return AttrNonNull;
64}
65
66void NonNullParamChecker::checkPreCall(const CallEvent &Call,
67 CheckerContext &C) const {
68 if (!Call.getDecl())
69 return;
70
71 llvm::SmallBitVector AttrNonNull = getNonNullAttrs(Call);
72 unsigned NumArgs = Call.getNumArgs();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000073
Jordan Rose682b3162012-07-02 19:28:21 +000074 ProgramStateRef state = C.getState();
George Karpenkov65e64912018-03-07 19:27:32 +000075 ArrayRef<ParmVarDecl*> parms = Call.parameters();
Anna Zaks9e0da9e02013-03-07 03:02:36 +000076
Jordan Rose679659f2014-10-13 19:38:02 +000077 for (unsigned idx = 0; idx < NumArgs; ++idx) {
George Karpenkov65e64912018-03-07 19:27:32 +000078 // For vararg functions, a corresponding parameter decl may not exist.
79 bool HasParam = idx < parms.size();
Anna Zaks9e0da9e02013-03-07 03:02:36 +000080
81 // Check if the parameter is a reference. We want to report when reference
Simon Pilgrim2c518802017-03-30 14:13:19 +000082 // to a null pointer is passed as a parameter.
George Karpenkov65e64912018-03-07 19:27:32 +000083 bool haveRefTypeParam =
84 HasParam ? parms[idx]->getType()->isReferenceType() : false;
Jordan Rose679659f2014-10-13 19:38:02 +000085 bool haveAttrNonNull = AttrNonNull[idx];
Anna Zaks9e0da9e02013-03-07 03:02:36 +000086
George Karpenkov65e64912018-03-07 19:27:32 +000087 // Check if the parameter is also marked 'nonnull'.
88 if (!haveAttrNonNull && HasParam)
89 haveAttrNonNull = parms[idx]->hasAttr<NonNullAttr>();
90
91 if (!haveAttrNonNull && !haveRefTypeParam)
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000092 continue;
93
Anna Zaks9e0da9e02013-03-07 03:02:36 +000094 // If the value is unknown or undefined, we can't perform this check.
95 const Expr *ArgE = Call.getArgExpr(idx);
Jordan Rose682b3162012-07-02 19:28:21 +000096 SVal V = Call.getArgSVal(idx);
George Karpenkov65e64912018-03-07 19:27:32 +000097 auto DV = V.getAs<DefinedSVal>();
Jordy Rose3d858882010-06-21 20:08:28 +000098 if (!DV)
99 continue;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000100
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000101 assert(!haveRefTypeParam || DV->getAs<Loc>());
102
George Karpenkov65e64912018-03-07 19:27:32 +0000103 // Process the case when the argument is not a location.
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000104 if (haveAttrNonNull && !DV->getAs<Loc>()) {
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000105 // If the argument is a union type, we want to handle a potential
Jordan Rose682b3162012-07-02 19:28:21 +0000106 // transparent_union GCC extension.
Jordan Rose682b3162012-07-02 19:28:21 +0000107 if (!ArgE)
108 continue;
109
110 QualType T = ArgE->getType();
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000111 const RecordType *UT = T->getAsUnionType();
112 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
113 continue;
Jordan Rose682b3162012-07-02 19:28:21 +0000114
George Karpenkov65e64912018-03-07 19:27:32 +0000115 auto CSV = DV->getAs<nonloc::CompoundVal>();
Anna Zaks23c85ed2013-03-06 20:26:02 +0000116
George Karpenkov65e64912018-03-07 19:27:32 +0000117 // FIXME: Handle LazyCompoundVals?
118 if (!CSV)
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000119 continue;
George Karpenkov65e64912018-03-07 19:27:32 +0000120
121 V = *(CSV->begin());
122 DV = V.getAs<DefinedSVal>();
123 assert(++CSV->begin() == CSV->end());
124 // FIXME: Handle (some_union){ some_other_union_val }, which turns into
125 // a LazyCompoundVal inside a CompoundVal.
126 if (!V.getAs<Loc>())
127 continue;
128
129 // Retrieve the corresponding expression.
130 if (const auto *CE = dyn_cast<CompoundLiteralExpr>(ArgE))
131 if (const auto *IE = dyn_cast<InitListExpr>(CE->getInitializer()))
132 ArgE = dyn_cast<Expr>(*(IE->begin()));
Ted Kremenekdcf85a82010-11-09 02:11:43 +0000133 }
134
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000135 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek49b1e382012-01-26 21:29:00 +0000136 ProgramStateRef stateNotNull, stateNull;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000137 std::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000138
George Karpenkov65e64912018-03-07 19:27:32 +0000139 // Generate an error node. Check for a null node in case
140 // we cache out.
141 if (stateNull && !stateNotNull) {
142 if (ExplodedNode *errorNode = C.generateErrorNode(stateNull)) {
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000143
George Karpenkov65e64912018-03-07 19:27:32 +0000144 std::unique_ptr<BugReport> R;
145 if (haveAttrNonNull)
146 R = genReportNullAttrNonNull(errorNode, ArgE);
147 else if (haveRefTypeParam)
148 R = genReportReferenceToNullPointer(errorNode, ArgE);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000149
George Karpenkov65e64912018-03-07 19:27:32 +0000150 // Highlight the range of the argument that was null.
151 R->addRange(Call.getArgSourceRange(idx));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000152
George Karpenkov65e64912018-03-07 19:27:32 +0000153 // Emit the bug report.
154 C.emitReport(std::move(R));
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000155 }
George Karpenkov65e64912018-03-07 19:27:32 +0000156
157 // Always return. Either we cached out or we just emitted an error.
158 return;
159 }
160
161 if (stateNull) {
Devin Coughline39bd402015-09-16 22:03:05 +0000162 if (ExplodedNode *N = C.generateSink(stateNull, C.getPredecessor())) {
Gabor Horvath8d3ad6b2015-08-27 18:49:07 +0000163 ImplicitNullDerefEvent event = {
George Karpenkov65e64912018-03-07 19:27:32 +0000164 V, false, N, &C.getBugReporter(),
165 /*IsDirectDereference=*/haveRefTypeParam};
Gabor Horvath8d3ad6b2015-08-27 18:49:07 +0000166 dispatchEvent(event);
167 }
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000168 }
169
170 // If a pointer value passed the check we should assume that it is
171 // indeed not null from this point forward.
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000172 state = stateNotNull;
173 }
174
175 // If we reach here all of the arguments passed the nonnull check.
176 // If 'state' has been updated generated a new node.
Anna Zaksda4c8d62011-10-26 21:06:34 +0000177 C.addTransition(state);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000178}
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000179
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000180std::unique_ptr<BugReport>
181NonNullParamChecker::genReportNullAttrNonNull(const ExplodedNode *ErrorNode,
182 const Expr *ArgE) const {
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000183 // Lazily allocate the BugType object if it hasn't already been
184 // created. Ownership is transferred to the BugReporter object once
185 // the BugReport is passed to 'EmitWarning'.
186 if (!BTAttrNonNull)
187 BTAttrNonNull.reset(new BugType(
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000188 this, "Argument with 'nonnull' attribute passed null", "API"));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000189
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000190 auto R = llvm::make_unique<BugReport>(
191 *BTAttrNonNull,
192 "Null pointer passed as an argument to a 'nonnull' parameter", ErrorNode);
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000193 if (ArgE)
194 bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R);
195
196 return R;
197}
198
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000199std::unique_ptr<BugReport> NonNullParamChecker::genReportReferenceToNullPointer(
200 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000201 if (!BTNullRefArg)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000202 BTNullRefArg.reset(new BuiltinBug(this, "Dereference of null pointer"));
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000203
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000204 auto R = llvm::make_unique<BugReport>(
205 *BTNullRefArg, "Forming reference to null pointer", ErrorNode);
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000206 if (ArgE) {
207 const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE);
Craig Topper0dbb7832014-05-27 02:45:47 +0000208 if (!ArgEDeref)
Anna Zaks9e0da9e02013-03-07 03:02:36 +0000209 ArgEDeref = ArgE;
210 bugreporter::trackNullOrUndefValue(ErrorNode,
211 ArgEDeref,
212 *R);
213 }
214 return R;
215
216}
217
Anna Zaksef893392013-03-09 03:23:14 +0000218void ento::registerNonNullParamChecker(CheckerManager &mgr) {
219 mgr.registerChecker<NonNullParamChecker>();
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000220}