blob: 3af793c23f1c724ff726c603036112e11705bb0b [file] [log] [blame]
Zhongxing Xu9b9d7312009-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 Kyrtzidis1696f502010-12-22 18:53:44 +000010// This defines AttrNonNullChecker, a builtin check in ExprEngine that
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000011// performs checks for arguments declared to have nonnull attribute.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000015#include "ClangSACheckers.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rose4f7df9b2012-07-26 21:39:41 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000022
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000025
Ted Kremenek43253152009-11-11 05:50:44 +000026namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000027class AttrNonNullChecker
Jordan Rose682b3162012-07-02 19:28:21 +000028 : public Checker< check::PreCall > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000029 mutable OwningPtr<BugType> BT;
Ted Kremenek43253152009-11-11 05:50:44 +000030public:
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000031
Jordan Rose682b3162012-07-02 19:28:21 +000032 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Ted Kremenek43253152009-11-11 05:50:44 +000033};
34} // end anonymous namespace
35
Jordan Rose682b3162012-07-02 19:28:21 +000036void AttrNonNullChecker::checkPreCall(const CallEvent &Call,
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +000037 CheckerContext &C) const {
Jordan Rose682b3162012-07-02 19:28:21 +000038 const Decl *FD = Call.getDecl();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000039 if (!FD)
40 return;
41
Jordan Rose682b3162012-07-02 19:28:21 +000042 const NonNullAttr *Att = FD->getAttr<NonNullAttr>();
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000043 if (!Att)
44 return;
45
Jordan Rose682b3162012-07-02 19:28:21 +000046 ProgramStateRef state = C.getState();
47
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000048 // Iterate through the arguments of CE and check them for null.
Jordan Rose682b3162012-07-02 19:28:21 +000049 for (unsigned idx = 0, count = Call.getNumArgs(); idx != count; ++idx) {
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000050 if (!Att->isNonNull(idx))
51 continue;
52
Jordan Rose682b3162012-07-02 19:28:21 +000053 SVal V = Call.getArgSVal(idx);
Jordy Rose3d858882010-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 Xu9b9d7312009-11-03 07:35:33 +000059
Ted Kremenekdcf85a82010-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 Rose682b3162012-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 Kremenekdcf85a82010-11-09 02:11:43 +000068 const RecordType *UT = T->getAsUnionType();
69 if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>())
70 continue;
Jordan Rose682b3162012-07-02 19:28:21 +000071
Ted Kremenekdcf85a82010-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 Rose682b3162012-07-02 19:28:21 +000080 } else {
Ted Kremenekdcf85a82010-11-09 02:11:43 +000081 // FIXME: Handle LazyCompoundVals?
82 continue;
83 }
84 }
85
Zhongxing Xu9b9d7312009-11-03 07:35:33 +000086 ConstraintManager &CM = C.getConstraintManager();
Ted Kremenek49b1e382012-01-26 21:29:00 +000087 ProgramStateRef stateNotNull, stateNull;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000088 llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
Zhongxing Xu9b9d7312009-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 Kremenek750b7ac2010-12-20 21:19:09 +000093 if (ExplodedNode *errorNode = C.generateSink(stateNull)) {
Zhongxing Xu9b9d7312009-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 Kyrtzidis6fff2e32011-02-28 01:28:01 +000099 BT.reset(new BugType("Argument with 'nonnull' attribute passed null",
100 "API"));
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000101
Anna Zaks3a6bdf82011-08-17 23:00:25 +0000102 BugReport *R =
Anna Zaksf86615c2011-08-19 22:33:38 +0000103 new BugReport(*BT, "Null pointer passed as an argument to a "
104 "'nonnull' parameter", errorNode);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000105
106 // Highlight the range of the argument that was null.
Jordan Rose682b3162012-07-02 19:28:21 +0000107 R->addRange(Call.getArgSourceRange(idx));
108 if (const Expr *ArgE = Call.getArgExpr(idx))
Jordan Rosea0f7d352012-08-28 00:50:51 +0000109 bugreporter::trackNullOrUndefValue(errorNode, ArgE, *R);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000110 // Emit the bug report.
Jordan Rosee10d5a72012-11-02 01:53:40 +0000111 C.emitReport(R);
Zhongxing Xu9b9d7312009-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 Zaksda4c8d62011-10-26 21:06:34 +0000126 C.addTransition(state);
Zhongxing Xu9b9d7312009-11-03 07:35:33 +0000127}
Argyrios Kyrtzidis6fff2e32011-02-28 01:28:01 +0000128
129void ento::registerAttrNonNullChecker(CheckerManager &mgr) {
130 mgr.registerChecker<AttrNonNullChecker>();
131}