blob: 27faf18d4c26be77ec227f39a68a252a937c33c6 [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"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosefe6a0112012-07-02 19:28:21 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/Calls.h"
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu94943b62009-11-03 07:35:33 +000024
Ted Kremenekf493f492009-11-11 05:50:44 +000025namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000026class AttrNonNullChecker
Jordan Rosefe6a0112012-07-02 19:28:21 +000027 : public Checker< check::PreCall > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000028 mutable OwningPtr<BugType> BT;
Ted Kremenekf493f492009-11-11 05:50:44 +000029public:
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000030
Jordan Rosefe6a0112012-07-02 19:28:21 +000031 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Ted Kremenekf493f492009-11-11 05:50:44 +000032};
33} // end anonymous namespace
34
Jordan Rosefe6a0112012-07-02 19:28:21 +000035void AttrNonNullChecker::checkPreCall(const CallEvent &Call,
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000036 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +000037 const Decl *FD = Call.getDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000038 if (!FD)
39 return;
40
Jordan Rosefe6a0112012-07-02 19:28:21 +000041 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu94943b62009-11-03 07:35:33 +000042 if (!Att)
43 return;
44
Jordan Rosefe6a0112012-07-02 19:28:21 +000045 ProgramStateRef state = C.getState();
46
Zhongxing Xu94943b62009-11-03 07:35:33 +000047 // Iterate through the arguments of CE and check them for null.
Jordan Rosefe6a0112012-07-02 19:28:21 +000048 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx) {
Zhongxing Xu94943b62009-11-03 07:35:33 +000049 if (!Att->isNonNull(idx))
50 continue;
51
Jordan Rosefe6a0112012-07-02 19:28:21 +000052 SVal V = Call.getArgSVal(idx);
Jordy Rose9a126852010-06-21 20:08:28 +000053 DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
54
55 // If the value is unknown or undefined, we can't perform this check.
56 if (!DV)
57 continue;
Zhongxing Xu94943b62009-11-03 07:35:33 +000058
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000059 if (!isa<Loc>(*DV)) {
60 // If the argument is a union type, we want to handle a potential
Jordan Rosefe6a0112012-07-02 19:28:21 +000061 // transparent_union GCC extension.
62 const Expr *ArgE = Call.getArgExpr(idx);
63 if (!ArgE)
64 continue;
65
66 QualType T = ArgE->getType();
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000067 const RecordType *UT = T->getAsUnionType();
68 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
69 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +000070
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000071 if (nonloc::CompoundVal *CSV = dyn_cast<nonloc::CompoundVal>(DV)) {
72 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
73 assert(CSV_I != CSV->end());
74 V = *CSV_I;
75 DV = dyn_cast<DefinedSVal>(&V);
76 assert(++CSV_I == CSV->end());
77 if (!DV)
78 continue;
Jordan Rosefe6a0112012-07-02 19:28:21 +000079 } else {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000080 // FIXME: Handle LazyCompoundVals?
81 continue;
82 }
83 }
84
Zhongxing Xu94943b62009-11-03 07:35:33 +000085 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +000086 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +000087 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-11-03 07:35:33 +000088
89 if (stateNull && !stateNotNull) {
90 // Generate an error node. Check for a null node in case
91 // we cache out.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000092 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-11-03 07:35:33 +000093
94 // Lazily allocate the BugType object if it hasn't already been
95 // created. Ownership is transferred to the BugReporter object once
96 // the BugReport is passed to 'EmitWarning'.
97 if (!BT)
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000098 BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
99 "API"));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000100
Anna Zakse172e8b2011-08-17 23:00:25 +0000101 BugReport *R =
Anna Zaks50bbc162011-08-19 22:33:38 +0000102 new BugReport(*BT, "Null pointer passed as an argument to a "
103 "'nonnull' parameter", errorNode);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000104
105 // Highlight the range of the argument that was null.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000106 R->addRange(Call.getArgSourceRange(idx));
107 if (const Expr *ArgE = Call.getArgExpr(idx))
108 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(errorNode,
109 ArgE, R));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000110 // Emit the bug report.
111 C.EmitReport(R);
112 }
113
114 // Always return. Either we cached out or we just emitted an error.
115 return;
116 }
117
118 // If a pointer value passed the check we should assume that it is
119 // indeed not null from this point forward.
120 assert(stateNotNull);
121 state = stateNotNull;
122 }
123
124 // If we reach here all of the arguments passed the nonnull check.
125 // If 'state' has been updated generated a new node.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000126 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000127}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000128
129void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
130 mgr.registerChecker<AttrNonNullChecker>();
131}