blob: 7f9a00ff876d9604677c386930f22706bb8ce4a6 [file] [log] [blame]
Ted Kremenekd55522f2010-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 Kyrtzidisa6d04d52011-02-15 07:42:33 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/TargetInfo.h"
17#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek26984fb2010-04-09 20:26:58 +000021#include "llvm/ADT/Optional.h"
Benjamin Kramer3307c5082012-02-04 12:31:12 +000022#include "llvm/ADT/STLExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/ADT/SmallString.h"
Devin Coughlin74810142016-12-16 23:31:56 +000024#include "llvm/ADT/StringExtras.h"
Benjamin Kramerc0483222010-03-27 21:19:47 +000025#include "llvm/ADT/StringSwitch.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenekd55522f2010-02-25 00:20:35 +000027#include <fcntl.h>
28
29using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000030using namespace ento;
Ted Kremenekd55522f2010-02-25 00:20:35 +000031
Devin Coughlin74810142016-12-16 23:31:56 +000032enum class OpenVariant {
33 /// The standard open() call:
34 /// int open(const char *path, int oflag, ...);
35 Open,
36
37 /// The variant taking a directory file descriptor and a relative path:
38 /// int openat(int fd, const char *path, int oflag, ...);
39 OpenAt
40};
41
Ted Kremenekd55522f2010-02-25 00:20:35 +000042namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000043class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000044 mutable std::unique_ptr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero;
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000045 mutable Optional<uint64_t> Val_O_CREAT;
Ted Kremenek212182d2010-04-08 22:15:34 +000046
47public:
Artem Dergachev7caff0e2017-06-27 11:14:39 +000048 DefaultBool CheckMisuse, CheckPortability;
49
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000050 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Rosef3dd00a2011-07-15 06:28:59 +000051
52 void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
Devin Coughlin74810142016-12-16 23:31:56 +000053 void CheckOpenAt(CheckerContext &C, const CallExpr *CE) const;
54
Jordy Rosef3dd00a2011-07-15 06:28:59 +000055 void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek134a83a2012-01-03 23:43:13 +000056 void CheckCallocZero(CheckerContext &C, const CallExpr *CE) const;
Jordy Rosef3dd00a2011-07-15 06:28:59 +000057 void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek134a83a2012-01-03 23:43:13 +000058 void CheckReallocZero(CheckerContext &C, const CallExpr *CE) const;
Jordan Rose9e068aa2012-10-30 01:37:16 +000059 void CheckReallocfZero(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek940e00f2012-01-11 08:13:21 +000060 void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const;
David Majnemer51169932016-10-31 05:37:48 +000061 void CheckAllocaWithAlignZero(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek940e00f2012-01-11 08:13:21 +000062 void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const;
Jordy Rosef3dd00a2011-07-15 06:28:59 +000063
64 typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
65 const CallExpr *) const;
Ted Kremenek134a83a2012-01-03 23:43:13 +000066private:
Devin Coughlin74810142016-12-16 23:31:56 +000067
68 void CheckOpenVariant(CheckerContext &C,
69 const CallExpr *CE, OpenVariant Variant) const;
70
Ted Kremenek134a83a2012-01-03 23:43:13 +000071 bool ReportZeroByteAllocation(CheckerContext &C,
Ted Kremenek49b1e382012-01-26 21:29:00 +000072 ProgramStateRef falseState,
Ted Kremenek134a83a2012-01-03 23:43:13 +000073 const Expr *arg,
74 const char *fn_name) const;
Ted Kremenek940e00f2012-01-11 08:13:21 +000075 void BasicAllocationCheck(CheckerContext &C,
76 const CallExpr *CE,
77 const unsigned numArgs,
78 const unsigned sizeArg,
79 const char *fn) const;
Ahmed Charlesb8984322014-03-07 20:03:18 +000080 void LazyInitialize(std::unique_ptr<BugType> &BT, const char *name) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000081 if (BT)
82 return;
83 BT.reset(new BugType(this, name, categories::UnixAPI));
84 }
Jordan Rosecd4db5c2014-08-20 16:58:03 +000085 void ReportOpenBug(CheckerContext &C,
86 ProgramStateRef State,
87 const char *Msg,
88 SourceRange SR) const;
Ted Kremenekd55522f2010-02-25 00:20:35 +000089};
90} //end anonymous namespace
91
Ted Kremenekd55522f2010-02-25 00:20:35 +000092//===----------------------------------------------------------------------===//
Ted Kremenekd55522f2010-02-25 00:20:35 +000093// "open" (man 2 open)
94//===----------------------------------------------------------------------===//
95
Jordan Rosecd4db5c2014-08-20 16:58:03 +000096void UnixAPIChecker::ReportOpenBug(CheckerContext &C,
97 ProgramStateRef State,
98 const char *Msg,
99 SourceRange SR) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000100 ExplodedNode *N = C.generateErrorNode(State);
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000101 if (!N)
102 return;
103
104 LazyInitialize(BT_open, "Improper use of 'open'");
105
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000106 auto Report = llvm::make_unique<BugReport>(*BT_open, Msg, N);
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000107 Report->addRange(SR);
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000108 C.emitReport(std::move(Report));
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000109}
110
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000111void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
Devin Coughlin74810142016-12-16 23:31:56 +0000112 CheckOpenVariant(C, CE, OpenVariant::Open);
113}
114
115void UnixAPIChecker::CheckOpenAt(CheckerContext &C, const CallExpr *CE) const {
116 CheckOpenVariant(C, CE, OpenVariant::OpenAt);
117}
118
119void UnixAPIChecker::CheckOpenVariant(CheckerContext &C,
120 const CallExpr *CE,
121 OpenVariant Variant) const {
122 // The index of the argument taking the flags open flags (O_RDONLY,
123 // O_WRONLY, O_CREAT, etc.),
124 unsigned int FlagsArgIndex;
125 const char *VariantName;
126 switch (Variant) {
127 case OpenVariant::Open:
128 FlagsArgIndex = 1;
129 VariantName = "open";
130 break;
131 case OpenVariant::OpenAt:
132 FlagsArgIndex = 2;
133 VariantName = "openat";
134 break;
135 };
136
137 // All calls should at least provide arguments up to the 'flags' parameter.
138 unsigned int MinArgCount = FlagsArgIndex + 1;
139
140 // If the flags has O_CREAT set then open/openat() require an additional
141 // argument specifying the file mode (permission bits) for the created file.
142 unsigned int CreateModeArgIndex = FlagsArgIndex + 1;
143
144 // The create mode argument should be the last argument.
145 unsigned int MaxArgCount = CreateModeArgIndex + 1;
146
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000147 ProgramStateRef state = C.getState();
148
Devin Coughlin74810142016-12-16 23:31:56 +0000149 if (CE->getNumArgs() < MinArgCount) {
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000150 // The frontend should issue a warning for this case, so this is a sanity
151 // check.
152 return;
Devin Coughlin74810142016-12-16 23:31:56 +0000153 } else if (CE->getNumArgs() == MaxArgCount) {
154 const Expr *Arg = CE->getArg(CreateModeArgIndex);
Jordan Roseba129af2014-08-20 16:58:09 +0000155 QualType QT = Arg->getType();
156 if (!QT->isIntegerType()) {
Devin Coughlin74810142016-12-16 23:31:56 +0000157 SmallString<256> SBuf;
158 llvm::raw_svector_ostream OS(SBuf);
159 OS << "The " << CreateModeArgIndex + 1
160 << llvm::getOrdinalSuffix(CreateModeArgIndex + 1)
161 << " argument to '" << VariantName << "' is not an integer";
162
Jordan Roseba129af2014-08-20 16:58:09 +0000163 ReportOpenBug(C, state,
Devin Coughlin74810142016-12-16 23:31:56 +0000164 SBuf.c_str(),
Jordan Roseba129af2014-08-20 16:58:09 +0000165 Arg->getSourceRange());
166 return;
167 }
Devin Coughlin74810142016-12-16 23:31:56 +0000168 } else if (CE->getNumArgs() > MaxArgCount) {
169 SmallString<256> SBuf;
170 llvm::raw_svector_ostream OS(SBuf);
171 OS << "Call to '" << VariantName << "' with more than " << MaxArgCount
172 << " arguments";
173
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000174 ReportOpenBug(C, state,
Devin Coughlin74810142016-12-16 23:31:56 +0000175 SBuf.c_str(),
176 CE->getArg(MaxArgCount)->getSourceRange());
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000177 return;
178 }
179
Ted Kremenek212182d2010-04-08 22:15:34 +0000180 // The definition of O_CREAT is platform specific. We need a better way
181 // of querying this information from the checking environment.
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000182 if (!Val_O_CREAT.hasValue()) {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000183 if (C.getASTContext().getTargetInfo().getTriple().getVendor()
Douglas Gregore8bbc122011-09-02 00:18:52 +0000184 == llvm::Triple::Apple)
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000185 Val_O_CREAT = 0x0200;
Ted Kremenek212182d2010-04-08 22:15:34 +0000186 else {
187 // FIXME: We need a more general way of getting the O_CREAT value.
188 // We could possibly grovel through the preprocessor state, but
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000189 // that would require passing the Preprocessor object to the ExprEngine.
Jordan Rose6b33c6f2014-03-26 17:05:46 +0000190 // See also: MallocChecker.cpp / M_ZERO.
Ted Kremenek212182d2010-04-08 22:15:34 +0000191 return;
192 }
193 }
194
Ted Kremenekd55522f2010-02-25 00:20:35 +0000195 // Now check if oflags has O_CREAT set.
Devin Coughlin74810142016-12-16 23:31:56 +0000196 const Expr *oflagsEx = CE->getArg(FlagsArgIndex);
Ted Kremenek632e3b72012-01-06 22:09:28 +0000197 const SVal V = state->getSVal(oflagsEx, C.getLocationContext());
David Blaikie2fdacbc2013-02-20 05:52:05 +0000198 if (!V.getAs<NonLoc>()) {
Ted Kremenekd55522f2010-02-25 00:20:35 +0000199 // The case where 'V' can be a location can only be due to a bad header,
200 // so in this case bail out.
201 return;
202 }
David Blaikie2fdacbc2013-02-20 05:52:05 +0000203 NonLoc oflags = V.castAs<NonLoc>();
204 NonLoc ocreateFlag = C.getSValBuilder()
205 .makeIntVal(Val_O_CREAT.getValue(), oflagsEx->getType()).castAs<NonLoc>();
Ted Kremenekdc891422010-12-01 21:57:22 +0000206 SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And,
Ted Kremenek9d0bb1e2010-12-01 21:28:31 +0000207 oflags, ocreateFlag,
208 oflagsEx->getType());
Ted Kremenekd55522f2010-02-25 00:20:35 +0000209 if (maskedFlagsUC.isUnknownOrUndef())
210 return;
David Blaikie2fdacbc2013-02-20 05:52:05 +0000211 DefinedSVal maskedFlags = maskedFlagsUC.castAs<DefinedSVal>();
Ted Kremenekd55522f2010-02-25 00:20:35 +0000212
213 // Check if maskedFlags is non-zero.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000214 ProgramStateRef trueState, falseState;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000215 std::tie(trueState, falseState) = state->assume(maskedFlags);
Ted Kremenekd55522f2010-02-25 00:20:35 +0000216
217 // Only emit an error if the value of 'maskedFlags' is properly
218 // constrained;
219 if (!(trueState && !falseState))
220 return;
221
Devin Coughlin74810142016-12-16 23:31:56 +0000222 if (CE->getNumArgs() < MaxArgCount) {
223 SmallString<256> SBuf;
224 llvm::raw_svector_ostream OS(SBuf);
225 OS << "Call to '" << VariantName << "' requires a "
226 << CreateModeArgIndex + 1
227 << llvm::getOrdinalSuffix(CreateModeArgIndex + 1)
228 << " argument when the 'O_CREAT' flag is set";
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000229 ReportOpenBug(C, trueState,
Devin Coughlin74810142016-12-16 23:31:56 +0000230 SBuf.c_str(),
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000231 oflagsEx->getSourceRange());
Ted Kremenekd55522f2010-02-25 00:20:35 +0000232 }
233}
234
235//===----------------------------------------------------------------------===//
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000236// pthread_once
237//===----------------------------------------------------------------------===//
238
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000239void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C,
240 const CallExpr *CE) const {
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000241
242 // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
243 // They can possibly be refactored.
244
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000245 if (CE->getNumArgs() < 1)
246 return;
247
248 // Check if the first argument is stack allocated. If so, issue a warning
249 // because that's likely to be bad news.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000250 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000251 const MemRegion *R =
252 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000253 if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
254 return;
255
Devin Coughline39bd402015-09-16 22:03:05 +0000256 ExplodedNode *N = C.generateErrorNode(state);
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000257 if (!N)
258 return;
259
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000260 SmallString<256> S;
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000261 llvm::raw_svector_ostream os(S);
262 os << "Call to 'pthread_once' uses";
263 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
264 os << " the local variable '" << VR->getDecl()->getName() << '\'';
265 else
266 os << " stack allocated memory";
267 os << " for the \"control\" value. Using such transient memory for "
268 "the control value is potentially dangerous.";
269 if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
270 os << " Perhaps you intended to declare the variable as 'static'?";
271
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000272 LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'");
273
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000274 auto report = llvm::make_unique<BugReport>(*BT_pthreadOnce, os.str(), N);
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000275 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000276 C.emitReport(std::move(report));
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000277}
278
279//===----------------------------------------------------------------------===//
Jordan Rose9e068aa2012-10-30 01:37:16 +0000280// "calloc", "malloc", "realloc", "reallocf", "alloca" and "valloc"
281// with allocation size 0
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000282//===----------------------------------------------------------------------===//
Ted Kremenek940e00f2012-01-11 08:13:21 +0000283// FIXME: Eventually these should be rolled into the MallocChecker, but right now
284// they're more basic and valuable for widespread use.
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000285
Ted Kremenek134a83a2012-01-03 23:43:13 +0000286// Returns true if we try to do a zero byte allocation, false otherwise.
287// Fills in trueState and falseState.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000288static bool IsZeroByteAllocation(ProgramStateRef state,
Ted Kremenek134a83a2012-01-03 23:43:13 +0000289 const SVal argVal,
Ted Kremenek49b1e382012-01-26 21:29:00 +0000290 ProgramStateRef *trueState,
291 ProgramStateRef *falseState) {
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000292 std::tie(*trueState, *falseState) =
David Blaikie2fdacbc2013-02-20 05:52:05 +0000293 state->assume(argVal.castAs<DefinedSVal>());
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000294
Ted Kremenek134a83a2012-01-03 23:43:13 +0000295 return (*falseState && !*trueState);
296}
297
298// Generates an error report, indicating that the function whose name is given
299// will perform a zero byte allocation.
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000300// Returns false if an error occurred, true otherwise.
Ted Kremenek134a83a2012-01-03 23:43:13 +0000301bool UnixAPIChecker::ReportZeroByteAllocation(CheckerContext &C,
Ted Kremenek49b1e382012-01-26 21:29:00 +0000302 ProgramStateRef falseState,
Ted Kremenek134a83a2012-01-03 23:43:13 +0000303 const Expr *arg,
304 const char *fn_name) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000305 ExplodedNode *N = C.generateErrorNode(falseState);
Ted Kremenek134a83a2012-01-03 23:43:13 +0000306 if (!N)
307 return false;
308
Ted Kremenek940e00f2012-01-11 08:13:21 +0000309 LazyInitialize(BT_mallocZero,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000310 "Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)");
Ted Kremenek134a83a2012-01-03 23:43:13 +0000311
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000312 SmallString<256> S;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000313 llvm::raw_svector_ostream os(S);
Ted Kremenek134a83a2012-01-03 23:43:13 +0000314 os << "Call to '" << fn_name << "' has an allocation size of 0 bytes";
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000315 auto report = llvm::make_unique<BugReport>(*BT_mallocZero, os.str(), N);
Ted Kremenek134a83a2012-01-03 23:43:13 +0000316
317 report->addRange(arg->getSourceRange());
Jordan Rosea0f7d352012-08-28 00:50:51 +0000318 bugreporter::trackNullOrUndefValue(N, arg, *report);
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000319 C.emitReport(std::move(report));
Ted Kremenek134a83a2012-01-03 23:43:13 +0000320
321 return true;
322}
323
Ted Kremenek940e00f2012-01-11 08:13:21 +0000324// Does a basic check for 0-sized allocations suitable for most of the below
325// functions (modulo "calloc")
326void UnixAPIChecker::BasicAllocationCheck(CheckerContext &C,
327 const CallExpr *CE,
328 const unsigned numArgs,
329 const unsigned sizeArg,
330 const char *fn) const {
331 // Sanity check for the correct number of arguments
332 if (CE->getNumArgs() != numArgs)
333 return;
334
335 // Check if the allocation size is 0.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000336 ProgramStateRef state = C.getState();
Craig Topper0dbb7832014-05-27 02:45:47 +0000337 ProgramStateRef trueState = nullptr, falseState = nullptr;
Ted Kremenek940e00f2012-01-11 08:13:21 +0000338 const Expr *arg = CE->getArg(sizeArg);
339 SVal argVal = state->getSVal(arg, C.getLocationContext());
340
341 if (argVal.isUnknownOrUndef())
342 return;
343
344 // Is the value perfectly constrained to zero?
345 if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000346 (void) ReportZeroByteAllocation(C, falseState, arg, fn);
Ted Kremenek940e00f2012-01-11 08:13:21 +0000347 return;
348 }
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000349 // Assume the value is non-zero going forward.
Ted Kremenek940e00f2012-01-11 08:13:21 +0000350 assert(trueState);
351 if (trueState != state)
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000352 C.addTransition(trueState);
Ted Kremenek940e00f2012-01-11 08:13:21 +0000353}
354
Ted Kremenek134a83a2012-01-03 23:43:13 +0000355void UnixAPIChecker::CheckCallocZero(CheckerContext &C,
356 const CallExpr *CE) const {
357 unsigned int nArgs = CE->getNumArgs();
358 if (nArgs != 2)
359 return;
360
Ted Kremenek49b1e382012-01-26 21:29:00 +0000361 ProgramStateRef state = C.getState();
Craig Topper0dbb7832014-05-27 02:45:47 +0000362 ProgramStateRef trueState = nullptr, falseState = nullptr;
Ted Kremenek134a83a2012-01-03 23:43:13 +0000363
364 unsigned int i;
365 for (i = 0; i < nArgs; i++) {
366 const Expr *arg = CE->getArg(i);
Ted Kremenek632e3b72012-01-06 22:09:28 +0000367 SVal argVal = state->getSVal(arg, C.getLocationContext());
Ted Kremenek134a83a2012-01-03 23:43:13 +0000368 if (argVal.isUnknownOrUndef()) {
369 if (i == 0)
370 continue;
371 else
372 return;
373 }
374
375 if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
376 if (ReportZeroByteAllocation(C, falseState, arg, "calloc"))
377 return;
378 else if (i == 0)
379 continue;
380 else
381 return;
382 }
383 }
384
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000385 // Assume the value is non-zero going forward.
Ted Kremenek134a83a2012-01-03 23:43:13 +0000386 assert(trueState);
387 if (trueState != state)
388 C.addTransition(trueState);
389}
390
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000391void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
392 const CallExpr *CE) const {
Ted Kremenek940e00f2012-01-11 08:13:21 +0000393 BasicAllocationCheck(C, CE, 1, 0, "malloc");
Ted Kremenek134a83a2012-01-03 23:43:13 +0000394}
395
396void UnixAPIChecker::CheckReallocZero(CheckerContext &C,
397 const CallExpr *CE) const {
Ted Kremenek940e00f2012-01-11 08:13:21 +0000398 BasicAllocationCheck(C, CE, 2, 1, "realloc");
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000399}
Ted Kremenek940e00f2012-01-11 08:13:21 +0000400
Jordan Rose9e068aa2012-10-30 01:37:16 +0000401void UnixAPIChecker::CheckReallocfZero(CheckerContext &C,
402 const CallExpr *CE) const {
403 BasicAllocationCheck(C, CE, 2, 1, "reallocf");
404}
405
Ted Kremenek940e00f2012-01-11 08:13:21 +0000406void UnixAPIChecker::CheckAllocaZero(CheckerContext &C,
407 const CallExpr *CE) const {
408 BasicAllocationCheck(C, CE, 1, 0, "alloca");
409}
410
David Majnemer51169932016-10-31 05:37:48 +0000411void UnixAPIChecker::CheckAllocaWithAlignZero(CheckerContext &C,
412 const CallExpr *CE) const {
413 BasicAllocationCheck(C, CE, 2, 0, "__builtin_alloca_with_align");
414}
415
Ted Kremenek940e00f2012-01-11 08:13:21 +0000416void UnixAPIChecker::CheckVallocZero(CheckerContext &C,
417 const CallExpr *CE) const {
418 BasicAllocationCheck(C, CE, 1, 0, "valloc");
419}
420
421
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000422//===----------------------------------------------------------------------===//
Ted Kremenekd55522f2010-02-25 00:20:35 +0000423// Central dispatch function.
424//===----------------------------------------------------------------------===//
425
Ted Kremenek940e00f2012-01-11 08:13:21 +0000426void UnixAPIChecker::checkPreStmt(const CallExpr *CE,
427 CheckerContext &C) const {
Jordan Rose6cd16c52012-07-10 23:13:01 +0000428 const FunctionDecl *FD = C.getCalleeDecl(CE);
429 if (!FD || FD->getKind() != Decl::Function)
430 return;
431
Devin Coughlinaa0fd762016-12-17 01:08:17 +0000432 // Don't treat functions in namespaces with the same name a Unix function
433 // as a call to the Unix function.
434 const DeclContext *NamespaceCtx = FD->getEnclosingNamespaceContext();
435 if (NamespaceCtx && isa<NamespaceDecl>(NamespaceCtx))
436 return;
437
Jordan Rose6cd16c52012-07-10 23:13:01 +0000438 StringRef FName = C.getCalleeName(FD);
Anna Zaksc6aa5312011-12-01 05:57:37 +0000439 if (FName.empty())
Ted Kremenekd55522f2010-02-25 00:20:35 +0000440 return;
441
Artem Dergachev7caff0e2017-06-27 11:14:39 +0000442 if (CheckMisuse) {
443 if (SubChecker SC =
444 llvm::StringSwitch<SubChecker>(FName)
445 .Case("open", &UnixAPIChecker::CheckOpen)
446 .Case("openat", &UnixAPIChecker::CheckOpenAt)
447 .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
448 .Default(nullptr)) {
449 (this->*SC)(C, CE);
450 }
451 }
452 if (CheckPortability) {
453 if (SubChecker SC =
454 llvm::StringSwitch<SubChecker>(FName)
455 .Case("calloc", &UnixAPIChecker::CheckCallocZero)
456 .Case("malloc", &UnixAPIChecker::CheckMallocZero)
457 .Case("realloc", &UnixAPIChecker::CheckReallocZero)
458 .Case("reallocf", &UnixAPIChecker::CheckReallocfZero)
459 .Cases("alloca", "__builtin_alloca",
460 &UnixAPIChecker::CheckAllocaZero)
461 .Case("__builtin_alloca_with_align",
462 &UnixAPIChecker::CheckAllocaWithAlignZero)
463 .Case("valloc", &UnixAPIChecker::CheckVallocZero)
464 .Default(nullptr)) {
465 (this->*SC)(C, CE);
466 }
467 }
Ted Kremenekd55522f2010-02-25 00:20:35 +0000468}
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000469
470//===----------------------------------------------------------------------===//
471// Registration.
472//===----------------------------------------------------------------------===//
473
Artem Dergachev7caff0e2017-06-27 11:14:39 +0000474#define REGISTER_CHECKER(Name) \
475 void ento::registerUnixAPI##Name##Checker(CheckerManager &mgr) { \
476 mgr.registerChecker<UnixAPIChecker>()->Check##Name = true; \
477 }
478
479REGISTER_CHECKER(Misuse)
480REGISTER_CHECKER(Portability)