blob: a3dc9648f4de1bdacaf7607852d31bc21c827020 [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();
Ted Kremenek5eca4822012-01-06 22:09:28 +000037 const LocationContext *LCtx = C.getLocationContext();
Zhongxing Xu94943b62009-11-03 07:35:33 +000038
39 // Check if the callee has a 'nonnull' attribute.
Ted Kremenek5eca4822012-01-06 22:09:28 +000040 SVal X = state->getSVal(CE->getCallee(), LCtx);
Zhongxing Xu94943b62009-11-03 07:35:33 +000041
Ted Kremenek9c378f72011-08-12 23:37:29 +000042 const FunctionDecl *FD = X.getAsFunctionDecl();
Zhongxing Xu94943b62009-11-03 07:35:33 +000043 if (!FD)
44 return;
45
46 const NonNullAttr* Att = FD->getAttr<NonNullAttr>();
47 if (!Att)
48 return;
49
50 // Iterate through the arguments of CE and check them for null.
51 unsigned idx = 0;
52
53 for (CallExpr::const_arg_iterator I=CE->arg_begin(), E=CE->arg_end(); I!=E;
54 ++I, ++idx) {
55
56 if (!Att->isNonNull(idx))
57 continue;
58
Ted Kremenek5eca4822012-01-06 22:09:28 +000059 SVal V = state->getSVal(*I, LCtx);
Jordy Rose9a126852010-06-21 20:08:28 +000060 DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
61
62 // If the value is unknown or undefined, we can't perform this check.
63 if (!DV)
64 continue;
Zhongxing Xu94943b62009-11-03 07:35:33 +000065
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000066 if (!isa<Loc>(*DV)) {
67 // If the argument is a union type, we want to handle a potential
68 // transparent_unoin GCC extension.
69 QualType T = (*I)->getType();
70 const RecordType *UT = T->getAsUnionType();
71 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
72 continue;
73 if (nonloc::CompoundVal *CSV = dyn_cast<nonloc::CompoundVal>(DV)) {
74 nonloc::CompoundVal::iterator CSV_I = CSV->begin();
75 assert(CSV_I != CSV->end());
76 V = *CSV_I;
77 DV = dyn_cast<DefinedSVal>(&V);
78 assert(++CSV_I == CSV->end());
79 if (!DV)
80 continue;
81 }
82 else {
83 // FIXME: Handle LazyCompoundVals?
84 continue;
85 }
86 }
87
Zhongxing Xu94943b62009-11-03 07:35:33 +000088 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek18c66fd2011-08-15 22:09:50 +000089 const ProgramState *stateNotNull, *stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +000090 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-11-03 07:35:33 +000091
92 if (stateNull && !stateNotNull) {
93 // Generate an error node. Check for a null node in case
94 // we cache out.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000095 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-11-03 07:35:33 +000096
97 // Lazily allocate the BugType object if it hasn't already been
98 // created. Ownership is transferred to the BugReporter object once
99 // the BugReport is passed to 'EmitWarning'.
100 if (!BT)
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000101 BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
102 "API"));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000103
Anna Zakse172e8b2011-08-17 23:00:25 +0000104 BugReport *R =
Anna Zaks50bbc162011-08-19 22:33:38 +0000105 new BugReport(*BT, "Null pointer passed as an argument to a "
106 "'nonnull' parameter", errorNode);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000107
108 // Highlight the range of the argument that was null.
109 const Expr *arg = *I;
110 R->addRange(arg->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +0000111 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(errorNode,
112 arg));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000113 // Emit the bug report.
114 C.EmitReport(R);
115 }
116
117 // Always return. Either we cached out or we just emitted an error.
118 return;
119 }
120
121 // If a pointer value passed the check we should assume that it is
122 // indeed not null from this point forward.
123 assert(stateNotNull);
124 state = stateNotNull;
125 }
126
127 // If we reach here all of the arguments passed the nonnull check.
128 // If 'state' has been updated generated a new node.
Anna Zaks0bd6b112011-10-26 21:06:34 +0000129 C.addTransition(state);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000130}
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +0000131
132void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
133 mgr.registerChecker<AttrNonNullChecker>();
134}