blob: 6185a8ee8c48a40e9fb2aabc16fc4d0ee917c74e [file] [log] [blame]
Zhongxing Xu94943b62009-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 Kyrtzidisd2592a32010-12-22 18:53:44 +000010// This defines AttrNonNullChecker, a builtin check in ExprEngine that
Zhongxing Xu94943b62009-11-03 07:35:33 +000011// performs checks for arguments declared to have nonnull attribute.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000015#include "ClangSACheckers.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosef540c542012-07-26 21:39:41 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000022
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xu94943b62009-11-03 07:35:33 +000025
Ted Kremenekf493f492009-11-11 05:50:44 +000026namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000027class AttrNonNullChecker
Jordan Rosefe6a0112012-07-02 19:28:21 +000028 : public Checker< check::PreCall > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000029 mutable OwningPtr<BugType> BT;
Ted Kremenekf493f492009-11-11 05:50:44 +000030public:
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000031
Jordan Rosefe6a0112012-07-02 19:28:21 +000032 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Ted Kremenekf493f492009-11-11 05:50:44 +000033};
34} // end anonymous namespace
35
Jordan Rosefe6a0112012-07-02 19:28:21 +000036void AttrNonNullChecker::checkPreCall(const CallEvent &Call,
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000037 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +000038 const Decl *FD = Call.getDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000039 if (!FD)
40 return;
41
Jordan Rosefe6a0112012-07-02 19:28:21 +000042 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu94943b62009-11-03 07:35:33 +000043 if (!Att)
44 return;
45
Jordan Rosefe6a0112012-07-02 19:28:21 +000046 ProgramStateRef state = C.getState();
47
Zhongxing Xu94943b62009-11-03 07:35:33 +000048 // Iterate through the arguments of CE and check them for null.
Jordan Rosefe6a0112012-07-02 19:28:21 +000049 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx) {
Zhongxing Xu94943b62009-11-03 07:35:33 +000050 if (!Att->isNonNull(idx))
51 continue;
52
Jordan Rosefe6a0112012-07-02 19:28:21 +000053 SVal V = Call.getArgSVal(idx);
David Blaikie5251abe2013-02-20 05:52:05 +000054 llvm::Optional<DefinedSVal> DV = V.getAs<DefinedSVal>();
Jordy Rose9a126852010-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 Xu94943b62009-11-03 07:35:33 +000059
David Blaikie5251abe2013-02-20 05:52:05 +000060 if (!DV->getAs<Loc>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000061 // If the argument is a union type, we want to handle a potential
Jordan Rosefe6a0112012-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 Kremenekbb0ba0b2010-11-09 02:11:43 +000068 const RecordType *UT = T->getAsUnionType();
69 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
70 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +000071
David Blaikie5251abe2013-02-20 05:52:05 +000072 if (llvm::Optional<nonloc::CompoundVal> CSV =
73 DV->getAs<nonloc::CompoundVal>()) {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000074 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
75 assert(CSV_I != CSV->end());
76 V = *CSV_I;
David Blaikie5251abe2013-02-20 05:52:05 +000077 DV = V.getAs<DefinedSVal>();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000078 assert(++CSV_I == CSV->end());
79 if (!DV)
80 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +000081 } else {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000082 // FIXME: Handle LazyCompoundVals?
83 continue;
84 }
85 }
86
Zhongxing Xu94943b62009-11-03 07:35:33 +000087 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +000088 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +000089 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-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 Kremenekd048c6e2010-12-20 21:19:09 +000094 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-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 Kyrtzidisbd900762011-02-28 01:28:01 +0000100 BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
101 "API"));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000102
Anna Zakse172e8b2011-08-17 23:00:25 +0000103 BugReport *R =
Anna Zaks50bbc162011-08-19 22:33:38 +0000104 new BugReport(*BT, "Null pointer passed as an argument to a "
105 "'nonnull' parameter", errorNode);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000106
107 // Highlight the range of the argument that was null.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000108 R->addRange(Call.getArgSourceRange(idx));
109 if (const Expr *ArgE = Call.getArgExpr(idx))
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000110 bugreporter::trackNullOrUndefValue(errorNode, ArgE, *R);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000111 // Emit the bug report.
Jordan Rose785950e2012-11-02 01:53:40 +0000112 C.emitReport(R);
Zhongxing Xu94943b62009-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 Zaks0bd6b112011-10-26 21:06:34 +0000127 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000128}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000129
130void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
131 mgr.registerChecker<AttrNonNullChecker>();
132}