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