blob: 8296eb93c5aed18837389eff3475c8e4b1c0e447 [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"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xu94943b62009-11-03 07:35:33 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xu94943b62009-11-03 07:35:33 +000023
Ted Kremenekf493f492009-11-11 05:50:44 +000024namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025class AttrNonNullChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000026 : public Checker< check::PreStmt<CallExpr> > {
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000027 mutable llvm::OwningPtr<BugType> BT;
Ted Kremenekf493f492009-11-11 05:50:44 +000028public:
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000029
30 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Ted Kremenekf493f492009-11-11 05:50:44 +000031};
32} // end anonymous namespace
33
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000034void AttrNonNullChecker::checkPreStmt(const CallExpr *CE,
35 CheckerContext &C) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000036 const ProgramState *state = C.getState();
Zhongxing Xu94943b62009-11-03 07:35:33 +000037
38 // Check if the callee has a 'nonnull' attribute.
Ted Kremenek13976632010-02-08 16:18:51 +000039 SVal X = state->getSVal(CE->getCallee());
Zhongxing Xu94943b62009-11-03 07:35:33 +000040
Ted Kremenek9c378f72011-08-12 23:37:29 +000041 const FunctionDecl *FD = X.getAsFunctionDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000042 if (!FD)
43 return;
44
45 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
46 if (!Att)
47 return;
48
49 // Iterate through the arguments of CE and check them for null.
50 unsigned idx = 0;
51
52 for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
53 ++I, ++idx) {
54
55 if (!Att->isNonNull(idx))
56 continue;
57
Jordy Rose9a126852010-06-21 20:08:28 +000058 SVal V = state->getSVal(*I);
59 DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
60
61 // If the value is unknown or undefined, we can't perform this check.
62 if (!DV)
63 continue;
Zhongxing Xu94943b62009-11-03 07:35:33 +000064
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000065 if (!isa<Loc>(*DV)) {
66 // If the argument is a union type, we want to handle a potential
67 // transparent_unoin GCC extension.
68 QualType T = (*I)->getType();
69 const RecordType *UT = T->getAsUnionType();
70 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
71 continue;
72 if (nonloc::CompoundVal *CSV = dyn_cast<nonloc::CompoundVal>(DV)) {
73 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
74 assert(CSV_I != CSV->end());
75 V = *CSV_I;
76 DV = dyn_cast<DefinedSVal>(&V);
77 assert(++CSV_I == CSV->end());
78 if (!DV)
79 continue;
80 }
81 else {
82 // FIXME: Handle LazyCompoundVals?
83 continue;
84 }
85 }
86
Zhongxing Xu94943b62009-11-03 07:35:33 +000087 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek18c66fd2011-08-15 22:09:50 +000088 const ProgramState *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.
108 const Expr *arg = *I;
109 R->addRange(arg->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +0000110 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(errorNode,
111 arg));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000112 // Emit the bug report.
113 C.EmitReport(R);
114 }
115
116 // Always return. Either we cached out or we just emitted an error.
117 return;
118 }
119
120 // If a pointer value passed the check we should assume that it is
121 // indeed not null from this point forward.
122 assert(stateNotNull);
123 state = stateNotNull;
124 }
125
126 // If we reach here all of the arguments passed the nonnull check.
127 // If 'state' has been updated generated a new node.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000128 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000129}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000130
131void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
132 mgr.registerChecker<AttrNonNullChecker>();
133}