blob: 0ecc391f3e8f0c8dc80b2d821751b742fa596259 [file] [log] [blame]
Ted Kremenek381d1bf2010-02-25 00:20:35 +00001//= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- 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//
10// This defines UnixAPIChecker, which is an assortment of checks on calls
11// to various, widely used UNIX/Posix functions.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000020#include "clang/Basic/TargetInfo.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000021#include "llvm/ADT/Optional.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000022#include "llvm/ADT/StringSwitch.h"
Ted Kremenek381d1bf2010-02-25 00:20:35 +000023#include <fcntl.h>
24
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenek66d51422010-04-09 20:26:58 +000027using llvm::Optional;
Ted Kremenek381d1bf2010-02-25 00:20:35 +000028
29namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000030class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > {
Jordy Roseaf5b0432011-07-15 06:28:59 +000031 mutable llvm::OwningPtr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero;
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000032 mutable Optional<uint64_t> Val_O_CREAT;
Ted Kremenekbace4ba2010-04-08 22:15:34 +000033
34public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000035 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Roseaf5b0432011-07-15 06:28:59 +000036
37 void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
38 void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
39 void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
40
41 typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
42 const CallExpr *) const;
Ted Kremenek381d1bf2010-02-25 00:20:35 +000043};
44} //end anonymous namespace
45
Ted Kremenek381d1bf2010-02-25 00:20:35 +000046//===----------------------------------------------------------------------===//
47// Utility functions.
48//===----------------------------------------------------------------------===//
49
Jordy Roseaf5b0432011-07-15 06:28:59 +000050static inline void LazyInitialize(llvm::OwningPtr<BugType> &BT,
51 const char *name) {
Ted Kremenek381d1bf2010-02-25 00:20:35 +000052 if (BT)
53 return;
Jordy Roseaf5b0432011-07-15 06:28:59 +000054 BT.reset(new BugType(name, "Unix API"));
Ted Kremenek381d1bf2010-02-25 00:20:35 +000055}
56
57//===----------------------------------------------------------------------===//
58// "open" (man 2 open)
59//===----------------------------------------------------------------------===//
60
Jordy Roseaf5b0432011-07-15 06:28:59 +000061void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenekbace4ba2010-04-08 22:15:34 +000062 // The definition of O_CREAT is platform specific. We need a better way
63 // of querying this information from the checking environment.
Jordy Roseaf5b0432011-07-15 06:28:59 +000064 if (!Val_O_CREAT.hasValue()) {
Ted Kremenekbace4ba2010-04-08 22:15:34 +000065 if (C.getASTContext().Target.getTriple().getVendor() == llvm::Triple::Apple)
Jordy Roseaf5b0432011-07-15 06:28:59 +000066 Val_O_CREAT = 0x0200;
Ted Kremenekbace4ba2010-04-08 22:15:34 +000067 else {
68 // FIXME: We need a more general way of getting the O_CREAT value.
69 // We could possibly grovel through the preprocessor state, but
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000070 // that would require passing the Preprocessor object to the ExprEngine.
Ted Kremenekbace4ba2010-04-08 22:15:34 +000071 return;
72 }
73 }
74
Ted Kremenek381d1bf2010-02-25 00:20:35 +000075 // Look at the 'oflags' argument for the O_CREAT flag.
76 const GRState *state = C.getState();
77
78 if (CE->getNumArgs() < 2) {
79 // The frontend should issue a warning for this case, so this is a sanity
80 // check.
81 return;
82 }
83
84 // Now check if oflags has O_CREAT set.
85 const Expr *oflagsEx = CE->getArg(1);
86 const SVal V = state->getSVal(oflagsEx);
87 if (!isa<NonLoc>(V)) {
88 // The case where 'V' can be a location can only be due to a bad header,
89 // so in this case bail out.
90 return;
91 }
92 NonLoc oflags = cast<NonLoc>(V);
93 NonLoc ocreateFlag =
Jordy Roseaf5b0432011-07-15 06:28:59 +000094 cast<NonLoc>(C.getSValBuilder().makeIntVal(Val_O_CREAT.getValue(),
Ted Kremenek381d1bf2010-02-25 00:20:35 +000095 oflagsEx->getType()));
Ted Kremenek9c149532010-12-01 21:57:22 +000096 SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And,
Ted Kremenek846eabd2010-12-01 21:28:31 +000097 oflags, ocreateFlag,
98 oflagsEx->getType());
Ted Kremenek381d1bf2010-02-25 00:20:35 +000099 if (maskedFlagsUC.isUnknownOrUndef())
100 return;
101 DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC);
102
103 // Check if maskedFlags is non-zero.
104 const GRState *trueState, *falseState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000105 llvm::tie(trueState, falseState) = state->assume(maskedFlags);
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000106
107 // Only emit an error if the value of 'maskedFlags' is properly
108 // constrained;
109 if (!(trueState && !falseState))
110 return;
111
112 if (CE->getNumArgs() < 3) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000113 ExplodedNode *N = C.generateSink(trueState);
Ted Kremenekc757d792010-02-25 05:44:05 +0000114 if (!N)
115 return;
116
Jordy Roseaf5b0432011-07-15 06:28:59 +0000117 LazyInitialize(BT_open, "Improper use of 'open'");
118
119 RangedBugReport *report =
120 new RangedBugReport(*BT_open,
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000121 "Call to 'open' requires a third argument when "
122 "the 'O_CREAT' flag is set", N);
123 report->addRange(oflagsEx->getSourceRange());
124 C.EmitReport(report);
125 }
126}
127
128//===----------------------------------------------------------------------===//
Ted Kremenek99d98382010-04-08 19:53:31 +0000129// pthread_once
130//===----------------------------------------------------------------------===//
131
Jordy Roseaf5b0432011-07-15 06:28:59 +0000132void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C,
133 const CallExpr *CE) const {
Ted Kremenek99d98382010-04-08 19:53:31 +0000134
135 // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
136 // They can possibly be refactored.
137
Ted Kremenek99d98382010-04-08 19:53:31 +0000138 if (CE->getNumArgs() < 1)
139 return;
140
141 // Check if the first argument is stack allocated. If so, issue a warning
142 // because that's likely to be bad news.
143 const GRState *state = C.getState();
144 const MemRegion *R = state->getSVal(CE->getArg(0)).getAsRegion();
145 if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
146 return;
147
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000148 ExplodedNode *N = C.generateSink(state);
Ted Kremenek99d98382010-04-08 19:53:31 +0000149 if (!N)
150 return;
151
152 llvm::SmallString<256> S;
153 llvm::raw_svector_ostream os(S);
154 os << "Call to 'pthread_once' uses";
155 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
156 os << " the local variable '" << VR->getDecl()->getName() << '\'';
157 else
158 os << " stack allocated memory";
159 os << " for the \"control\" value. Using such transient memory for "
160 "the control value is potentially dangerous.";
161 if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
162 os << " Perhaps you intended to declare the variable as 'static'?";
163
Jordy Roseaf5b0432011-07-15 06:28:59 +0000164 LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'");
165
166 RangedBugReport *report = new RangedBugReport(*BT_pthreadOnce, os.str(), N);
Ted Kremenek99d98382010-04-08 19:53:31 +0000167 report->addRange(CE->getArg(0)->getSourceRange());
168 C.EmitReport(report);
169}
170
171//===----------------------------------------------------------------------===//
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000172// "malloc" with allocation size 0
173//===----------------------------------------------------------------------===//
174
175// FIXME: Eventually this should be rolled into the MallocChecker, but this
176// check is more basic and is valuable for widespread use.
Jordy Roseaf5b0432011-07-15 06:28:59 +0000177void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
178 const CallExpr *CE) const {
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000179
180 // Sanity check that malloc takes one argument.
181 if (CE->getNumArgs() != 1)
182 return;
183
184 // Check if the allocation size is 0.
185 const GRState *state = C.getState();
186 SVal argVal = state->getSVal(CE->getArg(0));
187
188 if (argVal.isUnknownOrUndef())
189 return;
190
191 const GRState *trueState, *falseState;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000192 llvm::tie(trueState, falseState) = state->assume(cast<DefinedSVal>(argVal));
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000193
194 // Is the value perfectly constrained to zero?
195 if (falseState && !trueState) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +0000196 ExplodedNode *N = C.generateSink(falseState);
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000197 if (!N)
198 return;
199
Ted Kremenek8fec9722010-11-17 00:50:34 +0000200 // FIXME: Add reference to CERT advisory, and/or C99 standard in bug
201 // output.
202
Jordy Roseaf5b0432011-07-15 06:28:59 +0000203 LazyInitialize(BT_mallocZero, "Undefined allocation of 0 bytes");
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000204
205 EnhancedBugReport *report =
Jordy Roseaf5b0432011-07-15 06:28:59 +0000206 new EnhancedBugReport(*BT_mallocZero, "Call to 'malloc' has an allocation"
207 " size of 0 bytes", N);
Ted Kremenekb12fbc22010-11-16 18:47:04 +0000208 report->addRange(CE->getArg(0)->getSourceRange());
209 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
210 CE->getArg(0));
211 C.EmitReport(report);
212 return;
213 }
214 // Assume the the value is non-zero going forward.
215 assert(trueState);
216 if (trueState != state) {
217 C.addTransition(trueState);
218 }
219}
220
221//===----------------------------------------------------------------------===//
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000222// Central dispatch function.
223//===----------------------------------------------------------------------===//
224
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000225void UnixAPIChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000226 // Get the callee. All the functions we care about are C functions
227 // with simple identifiers.
228 const GRState *state = C.getState();
229 const Expr *Callee = CE->getCallee();
Jordy Roseaf5b0432011-07-15 06:28:59 +0000230 const FunctionDecl *Fn = state->getSVal(Callee).getAsFunctionDecl();
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000231
232 if (!Fn)
233 return;
234
Jordy Roseaf5b0432011-07-15 06:28:59 +0000235 const IdentifierInfo *FI = Fn->getIdentifier();
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000236 if (!FI)
237 return;
238
Jordy Roseaf5b0432011-07-15 06:28:59 +0000239 SubChecker SC =
240 llvm::StringSwitch<SubChecker>(FI->getName())
241 .Case("open", &UnixAPIChecker::CheckOpen)
242 .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
243 .Case("malloc", &UnixAPIChecker::CheckMallocZero)
244 .Default(NULL);
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000245
Jordy Roseaf5b0432011-07-15 06:28:59 +0000246 if (SC)
247 (this->*SC)(C, CE);
Ted Kremenek381d1bf2010-02-25 00:20:35 +0000248}
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +0000249
250//===----------------------------------------------------------------------===//
251// Registration.
252//===----------------------------------------------------------------------===//
253
254void ento::registerUnixAPIChecker(CheckerManager &mgr) {
255 mgr.registerChecker<UnixAPIChecker>();
256}