blob: 3af793c23f1c724ff726c603036112e11705bb0b [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);
Jordy Rose9a126852010-06-21 20:08:28 +000054 DefinedSVal *DV = dyn_cast<DefinedSVal>(&V);
55
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
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000060 if (!isa<Loc>(*DV)) {
61 // 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
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000072 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;
Jordan Rosefe6a0112012-07-02 19:28:21 +000080 } else {
Ted Kremenekbb0ba0b2010-11-09 02:11:43 +000081 // FIXME: Handle LazyCompoundVals?
82 continue;
83 }
84 }
85
Zhongxing Xu94943b62009-11-03 07:35:33 +000086 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek8bef8232012-01-26 21:29:00 +000087 ProgramStateRef stateNotNull, stateNull;
Ted Kremenek28f47b92010-12-01 22:16:56 +000088 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu94943b62009-11-03 07:35:33 +000089
90 if (stateNull && !stateNotNull) {
91 // Generate an error node. Check for a null node in case
92 // we cache out.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000093 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu94943b62009-11-03 07:35:33 +000094
95 // Lazily allocate the BugType object if it hasn't already been
96 // created. Ownership is transferred to the BugReporter object once
97 // the BugReport is passed to 'EmitWarning'.
98 if (!BT)
Argyrios Kyrtzidisbd900762011-02-28 01:28:01 +000099 BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
100 "API"));
Zhongxing Xu94943b62009-11-03 07:35:33 +0000101
Anna Zakse172e8b2011-08-17 23:00:25 +0000102 BugReport *R =
Anna Zaks50bbc162011-08-19 22:33:38 +0000103 new BugReport(*BT, "Null pointer passed as an argument to a "
104 "'nonnull' parameter", errorNode);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000105
106 // Highlight the range of the argument that was null.
Jordan Rosefe6a0112012-07-02 19:28:21 +0000107 R->addRange(Call.getArgSourceRange(idx));
108 if (const Expr *ArgE = Call.getArgExpr(idx))
Jordan Rosea1f81bb2012-08-28 00:50:51 +0000109 bugreporter::trackNullOrUndefValue(errorNode, ArgE, *R);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000110 // Emit the bug report.
Jordan Rose785950e2012-11-02 01:53:40 +0000111 C.emitReport(R);
Zhongxing Xu94943b62009-11-03 07:35:33 +0000112 }
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}