blob: 273a7a38824ad8399317de55ce15eb15ca23edd1 [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 > {
Anna Zaks018e9aa2013-03-07 03:02:36 +000032 mutable OwningPtr<BugType> BTAttrNonNull;
33 mutable OwningPtr<BugType> BTNullRefArg;
Ted Kremenekf493f492009-11-11 05:50:44 +000034public:
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000035
Jordan Rosefe6a0112012-07-02 19:28:21 +000036 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Anna Zaks018e9aa2013-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 Kremenekf493f492009-11-11 05:50:44 +000042};
43} // end anonymous namespace
44
Anna Zaks80412c42013-03-09 03:23:14 +000045void NonNullParamChecker::checkPreCall(const CallEvent &Call,
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000046 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +000047 const Decl *FD = Call.getDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000048 if (!FD)
49 return;
50
Jordan Rosefe6a0112012-07-02 19:28:21 +000051 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu94943b62009-11-03 07:35:33 +000052
Jordan Rosefe6a0112012-07-02 19:28:21 +000053 ProgramStateRef state = C.getState();
54
Anna Zaks018e9aa2013-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);
69
70 if (!haveRefTypeParam && !haveAttrNonNull)
Zhongxing Xu94943b62009-11-03 07:35:33 +000071 continue;
72
Anna Zaks018e9aa2013-03-07 03:02:36 +000073 // If the value is unknown or undefined, we can't perform this check.
74 const Expr *ArgE = Call.getArgExpr(idx);
Jordan Rosefe6a0112012-07-02 19:28:21 +000075 SVal V = Call.getArgSVal(idx);
David Blaikiedc84cd52013-02-20 22:23:23 +000076 Optional<DefinedSVal> DV = V.getAs<DefinedSVal>();
Jordy Rose9a126852010-06-21 20:08:28 +000077 if (!DV)
78 continue;
Zhongxing Xu94943b62009-11-03 07:35:33 +000079
Anna Zaks018e9aa2013-03-07 03:02:36 +000080 // Process the case when the argument is not a location.
81 assert(!haveRefTypeParam || DV->getAs<Loc>());
82
83 if (haveAttrNonNull && !DV->getAs<Loc>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000084 // If the argument is a union type, we want to handle a potential
Jordan Rosefe6a0112012-07-02 19:28:21 +000085 // transparent_union GCC extension.
Jordan Rosefe6a0112012-07-02 19:28:21 +000086 if (!ArgE)
87 continue;
88
89 QualType T = ArgE->getType();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000090 const RecordType *UT = T->getAsUnionType();
91 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
92 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +000093
David Blaikiedc84cd52013-02-20 22:23:23 +000094 if (Optional<nonloc::CompoundVal> CSV =
David Blaikie5251abe2013-02-20 05:52:05 +000095 DV->getAs<nonloc::CompoundVal>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000096 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
97 assert(CSV_I != CSV->end());
98 V = *CSV_I;
David Blaikie5251abe2013-02-20 05:52:05 +000099 DV = V.getAs<DefinedSVal>();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +0000100 assert(++CSV_I == CSV->end());
101 if (!DV)
Anna Zaks42773d62013-03-06 20:26:02 +0000102 continue;
103 // Retrieve the corresponding expression.
104 if (const CompoundLiteralExpr *CE = dyn_cast<CompoundLiteralExpr>(ArgE))
105 if (const InitListExpr *IE =
106 dyn_cast<InitListExpr>(CE->getInitializer()))
107 ArgE = dyn_cast<Expr>(*(IE->begin()));
108
Jordan Rosefe6a0112012-07-02 19:28:21 +0000109 } else {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +0000110 // FIXME: Handle LazyCompoundVals?
111 continue;
112 }
113 }
114
Zhongxing Xu94943b62009-11-03 07:35:33 +0000115 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +0000116 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000117 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000118
119 if (stateNull && !stateNotNull) {
120 // Generate an error node. Check for a null node in case
121 // we cache out.
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000122 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-11-03 07:35:33 +0000123
Anna Zaks018e9aa2013-03-07 03:02:36 +0000124 BugReport *R = 0;
125 if (haveAttrNonNull)
126 R = genReportNullAttrNonNull(errorNode, ArgE);
127 else if (haveRefTypeParam)
128 R = genReportReferenceToNullPointer(errorNode, ArgE);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000129
130 // Highlight the range of the argument that was null.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000131 R->addRange(Call.getArgSourceRange(idx));
Anna Zaks018e9aa2013-03-07 03:02:36 +0000132
Zhongxing Xu94943b62009-11-03 07:35:33 +0000133 // Emit the bug report.
Jordan Rose785950e2012-11-02 01:53:40 +0000134 C.emitReport(R);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000135 }
136
137 // Always return. Either we cached out or we just emitted an error.
138 return;
139 }
140
141 // If a pointer value passed the check we should assume that it is
142 // indeed not null from this point forward.
143 assert(stateNotNull);
144 state = stateNotNull;
145 }
146
147 // If we reach here all of the arguments passed the nonnull check.
148 // If 'state' has been updated generated a new node.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000149 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000150}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000151
Anna Zaks80412c42013-03-09 03:23:14 +0000152BugReport *NonNullParamChecker::genReportNullAttrNonNull(
Anna Zaks018e9aa2013-03-07 03:02:36 +0000153 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
154 // Lazily allocate the BugType object if it hasn't already been
155 // created. Ownership is transferred to the BugReporter object once
156 // the BugReport is passed to 'EmitWarning'.
157 if (!BTAttrNonNull)
158 BTAttrNonNull.reset(new BugType(
159 "Argument with 'nonnull' attribute passed null",
160 "API"));
161
162 BugReport *R = new BugReport(*BTAttrNonNull,
163 "Null pointer passed as an argument to a 'nonnull' parameter",
164 ErrorNode);
165 if (ArgE)
166 bugreporter::trackNullOrUndefValue(ErrorNode, ArgE, *R);
167
168 return R;
169}
170
Anna Zaks80412c42013-03-09 03:23:14 +0000171BugReport *NonNullParamChecker::genReportReferenceToNullPointer(
Anna Zaks018e9aa2013-03-07 03:02:36 +0000172 const ExplodedNode *ErrorNode, const Expr *ArgE) const {
173 if (!BTNullRefArg)
174 BTNullRefArg.reset(new BuiltinBug("Dereference of null pointer"));
175
176 BugReport *R = new BugReport(*BTNullRefArg,
177 "Forming reference to null pointer",
178 ErrorNode);
179 if (ArgE) {
180 const Expr *ArgEDeref = bugreporter::getDerefExpr(ArgE);
181 if (ArgEDeref == 0)
182 ArgEDeref = ArgE;
183 bugreporter::trackNullOrUndefValue(ErrorNode,
184 ArgEDeref,
185 *R);
186 }
187 return R;
188
189}
190
Anna Zaks80412c42013-03-09 03:23:14 +0000191void ento::registerNonNullParamChecker(CheckerManager &mgr) {
192 mgr.registerChecker<NonNullParamChecker>();
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000193}