blob: de5e6dca5e8ec2ade8713e52bfeb493d10287a98 [file] [log] [blame]
Zhongxing Xu9b9d7312009-11-03 07:35:33 +00001//===--- AttrNonNullChecker.h - Undefined arguments checker ----*- C++ -*--===//
2//
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//
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000010// This defines AttrNonNullChecker, a builtin check in ExprEngine that
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000011// performs checks for arguments declared to have nonnull attribute.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000015#include "ClangSACheckers.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rose4f7df9b2012-07-26 21:39:41 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000022
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000025
Ted Kremenek43253152009-11-11 05:50:44 +000026namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000027class AttrNonNullChecker
Jordan Rose682b3162012-07-02 19:28:21 +000028 : public Checker< check::PreCall > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000029 mutable OwningPtr<BugType> BT;
Ted Kremenek43253152009-11-11 05:50:44 +000030public:
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000031
Jordan Rose682b3162012-07-02 19:28:21 +000032 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Ted Kremenek43253152009-11-11 05:50:44 +000033};
34} // end anonymous namespace
35
Jordan Rose682b3162012-07-02 19:28:21 +000036void AttrNonNullChecker::checkPreCall(const CallEvent &Call,
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000037 CheckerContext &C) const {
Jordan Rose682b3162012-07-02 19:28:21 +000038 const Decl *FD = Call.getDecl();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000039 if (!FD)
40 return;
41
Jordan Rose682b3162012-07-02 19:28:21 +000042 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000043 if (!Att)
44 return;
45
Jordan Rose682b3162012-07-02 19:28:21 +000046 ProgramStateRef state = C.getState();
47
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000048 // Iterate through the arguments of CE and check them for null.
Jordan Rose682b3162012-07-02 19:28:21 +000049 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx) {
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000050 if (!Att->isNonNull(idx))
51 continue;
52
Jordan Rose682b3162012-07-02 19:28:21 +000053 SVal V = Call.getArgSVal(idx);
David Blaikie05785d12013-02-20 22:23:23 +000054 Optional<DefinedSVal> DV = V.getAs<DefinedSVal>();
Jordy Rose3d858882010-06-21 20:08:28 +000055
56 // If the value is unknown or undefined, we can't perform this check.
57 if (!DV)
58 continue;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000059
David Blaikie2fdacbc2013-02-20 05:52:05 +000060 if (!DV->getAs<Loc>()) {
Ted Kremenekdcf85a82010-11-09 02:11:43 +000061 // If the argument is a union type, we want to handle a potential
Jordan Rose682b3162012-07-02 19:28:21 +000062 // transparent_union GCC extension.
63 const Expr *ArgE = Call.getArgExpr(idx);
64 if (!ArgE)
65 continue;
66
67 QualType T = ArgE->getType();
Ted Kremenekdcf85a82010-11-09 02:11:43 +000068 const RecordType *UT = T->getAsUnionType();
69 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
70 continue;
Jordan Rose682b3162012-07-02 19:28:21 +000071
David Blaikie05785d12013-02-20 22:23:23 +000072 if (Optional<nonloc::CompoundVal> CSV =
David Blaikie2fdacbc2013-02-20 05:52:05 +000073 DV->getAs<nonloc::CompoundVal>()) {
Ted Kremenekdcf85a82010-11-09 02:11:43 +000074 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
75 assert(CSV_I != CSV->end());
76 V = *CSV_I;
David Blaikie2fdacbc2013-02-20 05:52:05 +000077 DV = V.getAs<DefinedSVal>();
Ted Kremenekdcf85a82010-11-09 02:11:43 +000078 assert(++CSV_I == CSV->end());
79 if (!DV)
80 continue;
Jordan Rose682b3162012-07-02 19:28:21 +000081 } else {
Ted Kremenekdcf85a82010-11-09 02:11:43 +000082 // FIXME: Handle LazyCompoundVals?
83 continue;
84 }
85 }
86
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000087 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek49b1e382012-01-26 21:29:00 +000088 ProgramStateRef stateNotNull, stateNull;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000089 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000090
91 if (stateNull && !stateNotNull) {
92 // Generate an error node. Check for a null node in case
93 // we cache out.
Ted Kremenek750b7ac2010-12-20 21:19:09 +000094 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000095
96 // Lazily allocate the BugType object if it hasn't already been
97 // created. Ownership is transferred to the BugReporter object once
98 // the BugReport is passed to 'EmitWarning'.
99 if (!BT)
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000100 BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
101 "API"));
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000102
Anna Zaks3a6bdf82011-08-17 23:00:25 +0000103 BugReport *R =
Anna Zaksf86615c2011-08-19 22:33:38 +0000104 new BugReport(*BT, "Null pointer passed as an argument to a "
105 "'nonnull' parameter", errorNode);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000106
107 // Highlight the range of the argument that was null.
Jordan Rose682b3162012-07-02 19:28:21 +0000108 R->addRange(Call.getArgSourceRange(idx));
109 if (const Expr *ArgE = Call.getArgExpr(idx))
Jordan Rosea0f7d352012-08-28 00:50:51 +0000110 bugreporter::trackNullOrUndefValue(errorNode, ArgE, *R);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000111 // Emit the bug report.
Jordan Rosee10d5a72012-11-02 01:53:40 +0000112 C.emitReport(R);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000113 }
114
115 // Always return. Either we cached out or we just emitted an error.
116 return;
117 }
118
119 // If a pointer value passed the check we should assume that it is
120 // indeed not null from this point forward.
121 assert(stateNotNull);
122 state = stateNotNull;
123 }
124
125 // If we reach here all of the arguments passed the nonnull check.
126 // If 'state' has been updated generated a new node.
Anna Zaksda4c8d62011-10-26 21:06:34 +0000127 C.addTransition(state);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000128}
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000129
130void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
131 mgr.registerChecker<AttrNonNullChecker>();
132}