blob: 26bf597bd950c690c27df01490fcb81466fc7f07 [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:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000048 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
Jordy Rosef3dd00a2011-07-15 06:28:59 +000049
50 void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
Devin Coughlin74810142016-12-16 23:31:56 +000051 void CheckOpenAt(CheckerContext &C, const CallExpr *CE) const;
52
Jordy Rosef3dd00a2011-07-15 06:28:59 +000053 void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek134a83a2012-01-03 23:43:13 +000054 void CheckCallocZero(CheckerContext &C, const CallExpr *CE) const;
Jordy Rosef3dd00a2011-07-15 06:28:59 +000055 void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek134a83a2012-01-03 23:43:13 +000056 void CheckReallocZero(CheckerContext &C, const CallExpr *CE) const;
Jordan Rose9e068aa2012-10-30 01:37:16 +000057 void CheckReallocfZero(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek940e00f2012-01-11 08:13:21 +000058 void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const;
David Majnemer51169932016-10-31 05:37:48 +000059 void CheckAllocaWithAlignZero(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek940e00f2012-01-11 08:13:21 +000060 void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const;
Jordy Rosef3dd00a2011-07-15 06:28:59 +000061
62 typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
63 const CallExpr *) const;
Ted Kremenek134a83a2012-01-03 23:43:13 +000064private:
Devin Coughlin74810142016-12-16 23:31:56 +000065
66 void CheckOpenVariant(CheckerContext &C,
67 const CallExpr *CE, OpenVariant Variant) const;
68
Ted Kremenek134a83a2012-01-03 23:43:13 +000069 bool ReportZeroByteAllocation(CheckerContext &C,
Ted Kremenek49b1e382012-01-26 21:29:00 +000070 ProgramStateRef falseState,
Ted Kremenek134a83a2012-01-03 23:43:13 +000071 const Expr *arg,
72 const char *fn_name) const;
Ted Kremenek940e00f2012-01-11 08:13:21 +000073 void BasicAllocationCheck(CheckerContext &C,
74 const CallExpr *CE,
75 const unsigned numArgs,
76 const unsigned sizeArg,
77 const char *fn) const;
Ahmed Charlesb8984322014-03-07 20:03:18 +000078 void LazyInitialize(std::unique_ptr<BugType> &BT, const char *name) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000079 if (BT)
80 return;
81 BT.reset(new BugType(this, name, categories::UnixAPI));
82 }
Jordan Rosecd4db5c2014-08-20 16:58:03 +000083 void ReportOpenBug(CheckerContext &C,
84 ProgramStateRef State,
85 const char *Msg,
86 SourceRange SR) const;
Ted Kremenekd55522f2010-02-25 00:20:35 +000087};
88} //end anonymous namespace
89
Ted Kremenekd55522f2010-02-25 00:20:35 +000090//===----------------------------------------------------------------------===//
Ted Kremenekd55522f2010-02-25 00:20:35 +000091// "open" (man 2 open)
92//===----------------------------------------------------------------------===//
93
Jordan Rosecd4db5c2014-08-20 16:58:03 +000094void UnixAPIChecker::ReportOpenBug(CheckerContext &C,
95 ProgramStateRef State,
96 const char *Msg,
97 SourceRange SR) const {
Devin Coughline39bd402015-09-16 22:03:05 +000098 ExplodedNode *N = C.generateErrorNode(State);
Jordan Rosecd4db5c2014-08-20 16:58:03 +000099 if (!N)
100 return;
101
102 LazyInitialize(BT_open, "Improper use of 'open'");
103
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000104 auto Report = llvm::make_unique<BugReport>(*BT_open, Msg, N);
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000105 Report->addRange(SR);
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000106 C.emitReport(std::move(Report));
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000107}
108
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000109void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
Devin Coughlin74810142016-12-16 23:31:56 +0000110 CheckOpenVariant(C, CE, OpenVariant::Open);
111}
112
113void UnixAPIChecker::CheckOpenAt(CheckerContext &C, const CallExpr *CE) const {
114 CheckOpenVariant(C, CE, OpenVariant::OpenAt);
115}
116
117void UnixAPIChecker::CheckOpenVariant(CheckerContext &C,
118 const CallExpr *CE,
119 OpenVariant Variant) const {
120 // The index of the argument taking the flags open flags (O_RDONLY,
121 // O_WRONLY, O_CREAT, etc.),
122 unsigned int FlagsArgIndex;
123 const char *VariantName;
124 switch (Variant) {
125 case OpenVariant::Open:
126 FlagsArgIndex = 1;
127 VariantName = "open";
128 break;
129 case OpenVariant::OpenAt:
130 FlagsArgIndex = 2;
131 VariantName = "openat";
132 break;
133 };
134
135 // All calls should at least provide arguments up to the 'flags' parameter.
136 unsigned int MinArgCount = FlagsArgIndex + 1;
137
138 // If the flags has O_CREAT set then open/openat() require an additional
139 // argument specifying the file mode (permission bits) for the created file.
140 unsigned int CreateModeArgIndex = FlagsArgIndex + 1;
141
142 // The create mode argument should be the last argument.
143 unsigned int MaxArgCount = CreateModeArgIndex + 1;
144
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000145 ProgramStateRef state = C.getState();
146
Devin Coughlin74810142016-12-16 23:31:56 +0000147 if (CE->getNumArgs() < MinArgCount) {
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000148 // The frontend should issue a warning for this case, so this is a sanity
149 // check.
150 return;
Devin Coughlin74810142016-12-16 23:31:56 +0000151 } else if (CE->getNumArgs() == MaxArgCount) {
152 const Expr *Arg = CE->getArg(CreateModeArgIndex);
Jordan Roseba129af2014-08-20 16:58:09 +0000153 QualType QT = Arg->getType();
154 if (!QT->isIntegerType()) {
Devin Coughlin74810142016-12-16 23:31:56 +0000155 SmallString<256> SBuf;
156 llvm::raw_svector_ostream OS(SBuf);
157 OS << "The " << CreateModeArgIndex + 1
158 << llvm::getOrdinalSuffix(CreateModeArgIndex + 1)
159 << " argument to '" << VariantName << "' is not an integer";
160
Jordan Roseba129af2014-08-20 16:58:09 +0000161 ReportOpenBug(C, state,
Devin Coughlin74810142016-12-16 23:31:56 +0000162 SBuf.c_str(),
Jordan Roseba129af2014-08-20 16:58:09 +0000163 Arg->getSourceRange());
164 return;
165 }
Devin Coughlin74810142016-12-16 23:31:56 +0000166 } else if (CE->getNumArgs() > MaxArgCount) {
167 SmallString<256> SBuf;
168 llvm::raw_svector_ostream OS(SBuf);
169 OS << "Call to '" << VariantName << "' with more than " << MaxArgCount
170 << " arguments";
171
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000172 ReportOpenBug(C, state,
Devin Coughlin74810142016-12-16 23:31:56 +0000173 SBuf.c_str(),
174 CE->getArg(MaxArgCount)->getSourceRange());
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000175 return;
176 }
177
Ted Kremenek212182d2010-04-08 22:15:34 +0000178 // The definition of O_CREAT is platform specific. We need a better way
179 // of querying this information from the checking environment.
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000180 if (!Val_O_CREAT.hasValue()) {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000181 if (C.getASTContext().getTargetInfo().getTriple().getVendor()
Douglas Gregore8bbc122011-09-02 00:18:52 +0000182 == llvm::Triple::Apple)
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000183 Val_O_CREAT = 0x0200;
Ted Kremenek212182d2010-04-08 22:15:34 +0000184 else {
185 // FIXME: We need a more general way of getting the O_CREAT value.
186 // We could possibly grovel through the preprocessor state, but
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000187 // that would require passing the Preprocessor object to the ExprEngine.
Jordan Rose6b33c6f2014-03-26 17:05:46 +0000188 // See also: MallocChecker.cpp / M_ZERO.
Ted Kremenek212182d2010-04-08 22:15:34 +0000189 return;
190 }
191 }
192
Ted Kremenekd55522f2010-02-25 00:20:35 +0000193 // Now check if oflags has O_CREAT set.
Devin Coughlin74810142016-12-16 23:31:56 +0000194 const Expr *oflagsEx = CE->getArg(FlagsArgIndex);
Ted Kremenek632e3b72012-01-06 22:09:28 +0000195 const SVal V = state->getSVal(oflagsEx, C.getLocationContext());
David Blaikie2fdacbc2013-02-20 05:52:05 +0000196 if (!V.getAs<NonLoc>()) {
Ted Kremenekd55522f2010-02-25 00:20:35 +0000197 // The case where 'V' can be a location can only be due to a bad header,
198 // so in this case bail out.
199 return;
200 }
David Blaikie2fdacbc2013-02-20 05:52:05 +0000201 NonLoc oflags = V.castAs<NonLoc>();
202 NonLoc ocreateFlag = C.getSValBuilder()
203 .makeIntVal(Val_O_CREAT.getValue(), oflagsEx->getType()).castAs<NonLoc>();
Ted Kremenekdc891422010-12-01 21:57:22 +0000204 SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And,
Ted Kremenek9d0bb1e2010-12-01 21:28:31 +0000205 oflags, ocreateFlag,
206 oflagsEx->getType());
Ted Kremenekd55522f2010-02-25 00:20:35 +0000207 if (maskedFlagsUC.isUnknownOrUndef())
208 return;
David Blaikie2fdacbc2013-02-20 05:52:05 +0000209 DefinedSVal maskedFlags = maskedFlagsUC.castAs<DefinedSVal>();
Ted Kremenekd55522f2010-02-25 00:20:35 +0000210
211 // Check if maskedFlags is non-zero.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000212 ProgramStateRef trueState, falseState;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000213 std::tie(trueState, falseState) = state->assume(maskedFlags);
Ted Kremenekd55522f2010-02-25 00:20:35 +0000214
215 // Only emit an error if the value of 'maskedFlags' is properly
216 // constrained;
217 if (!(trueState && !falseState))
218 return;
219
Devin Coughlin74810142016-12-16 23:31:56 +0000220 if (CE->getNumArgs() < MaxArgCount) {
221 SmallString<256> SBuf;
222 llvm::raw_svector_ostream OS(SBuf);
223 OS << "Call to '" << VariantName << "' requires a "
224 << CreateModeArgIndex + 1
225 << llvm::getOrdinalSuffix(CreateModeArgIndex + 1)
226 << " argument when the 'O_CREAT' flag is set";
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000227 ReportOpenBug(C, trueState,
Devin Coughlin74810142016-12-16 23:31:56 +0000228 SBuf.c_str(),
Jordan Rosecd4db5c2014-08-20 16:58:03 +0000229 oflagsEx->getSourceRange());
Ted Kremenekd55522f2010-02-25 00:20:35 +0000230 }
231}
232
233//===----------------------------------------------------------------------===//
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000234// pthread_once
235//===----------------------------------------------------------------------===//
236
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000237void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C,
238 const CallExpr *CE) const {
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000239
240 // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
241 // They can possibly be refactored.
242
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000243 if (CE->getNumArgs() < 1)
244 return;
245
246 // Check if the first argument is stack allocated. If so, issue a warning
247 // because that's likely to be bad news.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000248 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +0000249 const MemRegion *R =
250 state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000251 if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
252 return;
253
Devin Coughline39bd402015-09-16 22:03:05 +0000254 ExplodedNode *N = C.generateErrorNode(state);
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000255 if (!N)
256 return;
257
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000258 SmallString<256> S;
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000259 llvm::raw_svector_ostream os(S);
260 os << "Call to 'pthread_once' uses";
261 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
262 os << " the local variable '" << VR->getDecl()->getName() << '\'';
263 else
264 os << " stack allocated memory";
265 os << " for the \"control\" value. Using such transient memory for "
266 "the control value is potentially dangerous.";
267 if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
268 os << " Perhaps you intended to declare the variable as 'static'?";
269
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000270 LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'");
271
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000272 auto report = llvm::make_unique<BugReport>(*BT_pthreadOnce, os.str(), N);
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000273 report->addRange(CE->getArg(0)->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000274 C.emitReport(std::move(report));
Ted Kremenekea4a5ab2010-04-08 19:53:31 +0000275}
276
277//===----------------------------------------------------------------------===//
Jordan Rose9e068aa2012-10-30 01:37:16 +0000278// "calloc", "malloc", "realloc", "reallocf", "alloca" and "valloc"
279// with allocation size 0
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000280//===----------------------------------------------------------------------===//
Ted Kremenek940e00f2012-01-11 08:13:21 +0000281// FIXME: Eventually these should be rolled into the MallocChecker, but right now
282// they're more basic and valuable for widespread use.
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000283
Ted Kremenek134a83a2012-01-03 23:43:13 +0000284// Returns true if we try to do a zero byte allocation, false otherwise.
285// Fills in trueState and falseState.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000286static bool IsZeroByteAllocation(ProgramStateRef state,
Ted Kremenek134a83a2012-01-03 23:43:13 +0000287 const SVal argVal,
Ted Kremenek49b1e382012-01-26 21:29:00 +0000288 ProgramStateRef *trueState,
289 ProgramStateRef *falseState) {
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000290 std::tie(*trueState, *falseState) =
David Blaikie2fdacbc2013-02-20 05:52:05 +0000291 state->assume(argVal.castAs<DefinedSVal>());
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000292
Ted Kremenek134a83a2012-01-03 23:43:13 +0000293 return (*falseState && !*trueState);
294}
295
296// Generates an error report, indicating that the function whose name is given
297// will perform a zero byte allocation.
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000298// Returns false if an error occurred, true otherwise.
Ted Kremenek134a83a2012-01-03 23:43:13 +0000299bool UnixAPIChecker::ReportZeroByteAllocation(CheckerContext &C,
Ted Kremenek49b1e382012-01-26 21:29:00 +0000300 ProgramStateRef falseState,
Ted Kremenek134a83a2012-01-03 23:43:13 +0000301 const Expr *arg,
302 const char *fn_name) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000303 ExplodedNode *N = C.generateErrorNode(falseState);
Ted Kremenek134a83a2012-01-03 23:43:13 +0000304 if (!N)
305 return false;
306
Ted Kremenek940e00f2012-01-11 08:13:21 +0000307 LazyInitialize(BT_mallocZero,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000308 "Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)");
Ted Kremenek134a83a2012-01-03 23:43:13 +0000309
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000310 SmallString<256> S;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000311 llvm::raw_svector_ostream os(S);
Ted Kremenek134a83a2012-01-03 23:43:13 +0000312 os << "Call to '" << fn_name << "' has an allocation size of 0 bytes";
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000313 auto report = llvm::make_unique<BugReport>(*BT_mallocZero, os.str(), N);
Ted Kremenek134a83a2012-01-03 23:43:13 +0000314
315 report->addRange(arg->getSourceRange());
Jordan Rosea0f7d352012-08-28 00:50:51 +0000316 bugreporter::trackNullOrUndefValue(N, arg, *report);
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000317 C.emitReport(std::move(report));
Ted Kremenek134a83a2012-01-03 23:43:13 +0000318
319 return true;
320}
321
Ted Kremenek940e00f2012-01-11 08:13:21 +0000322// Does a basic check for 0-sized allocations suitable for most of the below
323// functions (modulo "calloc")
324void UnixAPIChecker::BasicAllocationCheck(CheckerContext &C,
325 const CallExpr *CE,
326 const unsigned numArgs,
327 const unsigned sizeArg,
328 const char *fn) const {
329 // Sanity check for the correct number of arguments
330 if (CE->getNumArgs() != numArgs)
331 return;
332
333 // Check if the allocation size is 0.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000334 ProgramStateRef state = C.getState();
Craig Topper0dbb7832014-05-27 02:45:47 +0000335 ProgramStateRef trueState = nullptr, falseState = nullptr;
Ted Kremenek940e00f2012-01-11 08:13:21 +0000336 const Expr *arg = CE->getArg(sizeArg);
337 SVal argVal = state->getSVal(arg, C.getLocationContext());
338
339 if (argVal.isUnknownOrUndef())
340 return;
341
342 // Is the value perfectly constrained to zero?
343 if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000344 (void) ReportZeroByteAllocation(C, falseState, arg, fn);
Ted Kremenek940e00f2012-01-11 08:13:21 +0000345 return;
346 }
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000347 // Assume the value is non-zero going forward.
Ted Kremenek940e00f2012-01-11 08:13:21 +0000348 assert(trueState);
349 if (trueState != state)
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000350 C.addTransition(trueState);
Ted Kremenek940e00f2012-01-11 08:13:21 +0000351}
352
Ted Kremenek134a83a2012-01-03 23:43:13 +0000353void UnixAPIChecker::CheckCallocZero(CheckerContext &C,
354 const CallExpr *CE) const {
355 unsigned int nArgs = CE->getNumArgs();
356 if (nArgs != 2)
357 return;
358
Ted Kremenek49b1e382012-01-26 21:29:00 +0000359 ProgramStateRef state = C.getState();
Craig Topper0dbb7832014-05-27 02:45:47 +0000360 ProgramStateRef trueState = nullptr, falseState = nullptr;
Ted Kremenek134a83a2012-01-03 23:43:13 +0000361
362 unsigned int i;
363 for (i = 0; i < nArgs; i++) {
364 const Expr *arg = CE->getArg(i);
Ted Kremenek632e3b72012-01-06 22:09:28 +0000365 SVal argVal = state->getSVal(arg, C.getLocationContext());
Ted Kremenek134a83a2012-01-03 23:43:13 +0000366 if (argVal.isUnknownOrUndef()) {
367 if (i == 0)
368 continue;
369 else
370 return;
371 }
372
373 if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
374 if (ReportZeroByteAllocation(C, falseState, arg, "calloc"))
375 return;
376 else if (i == 0)
377 continue;
378 else
379 return;
380 }
381 }
382
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000383 // Assume the value is non-zero going forward.
Ted Kremenek134a83a2012-01-03 23:43:13 +0000384 assert(trueState);
385 if (trueState != state)
386 C.addTransition(trueState);
387}
388
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000389void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
390 const CallExpr *CE) const {
Ted Kremenek940e00f2012-01-11 08:13:21 +0000391 BasicAllocationCheck(C, CE, 1, 0, "malloc");
Ted Kremenek134a83a2012-01-03 23:43:13 +0000392}
393
394void UnixAPIChecker::CheckReallocZero(CheckerContext &C,
395 const CallExpr *CE) const {
Ted Kremenek940e00f2012-01-11 08:13:21 +0000396 BasicAllocationCheck(C, CE, 2, 1, "realloc");
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000397}
Ted Kremenek940e00f2012-01-11 08:13:21 +0000398
Jordan Rose9e068aa2012-10-30 01:37:16 +0000399void UnixAPIChecker::CheckReallocfZero(CheckerContext &C,
400 const CallExpr *CE) const {
401 BasicAllocationCheck(C, CE, 2, 1, "reallocf");
402}
403
Ted Kremenek940e00f2012-01-11 08:13:21 +0000404void UnixAPIChecker::CheckAllocaZero(CheckerContext &C,
405 const CallExpr *CE) const {
406 BasicAllocationCheck(C, CE, 1, 0, "alloca");
407}
408
David Majnemer51169932016-10-31 05:37:48 +0000409void UnixAPIChecker::CheckAllocaWithAlignZero(CheckerContext &C,
410 const CallExpr *CE) const {
411 BasicAllocationCheck(C, CE, 2, 0, "__builtin_alloca_with_align");
412}
413
Ted Kremenek940e00f2012-01-11 08:13:21 +0000414void UnixAPIChecker::CheckVallocZero(CheckerContext &C,
415 const CallExpr *CE) const {
416 BasicAllocationCheck(C, CE, 1, 0, "valloc");
417}
418
419
Ted Kremenek0c27bcf2010-11-16 18:47:04 +0000420//===----------------------------------------------------------------------===//
Ted Kremenekd55522f2010-02-25 00:20:35 +0000421// Central dispatch function.
422//===----------------------------------------------------------------------===//
423
Ted Kremenek940e00f2012-01-11 08:13:21 +0000424void UnixAPIChecker::checkPreStmt(const CallExpr *CE,
425 CheckerContext &C) const {
Jordan Rose6cd16c52012-07-10 23:13:01 +0000426 const FunctionDecl *FD = C.getCalleeDecl(CE);
427 if (!FD || FD->getKind() != Decl::Function)
428 return;
429
Devin Coughlinaa0fd762016-12-17 01:08:17 +0000430 // Don't treat functions in namespaces with the same name a Unix function
431 // as a call to the Unix function.
432 const DeclContext *NamespaceCtx = FD->getEnclosingNamespaceContext();
433 if (NamespaceCtx && isa<NamespaceDecl>(NamespaceCtx))
434 return;
435
Jordan Rose6cd16c52012-07-10 23:13:01 +0000436 StringRef FName = C.getCalleeName(FD);
Anna Zaksc6aa5312011-12-01 05:57:37 +0000437 if (FName.empty())
Ted Kremenekd55522f2010-02-25 00:20:35 +0000438 return;
439
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000440 SubChecker SC =
Anna Zaksc6aa5312011-12-01 05:57:37 +0000441 llvm::StringSwitch<SubChecker>(FName)
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000442 .Case("open", &UnixAPIChecker::CheckOpen)
Devin Coughlin74810142016-12-16 23:31:56 +0000443 .Case("openat", &UnixAPIChecker::CheckOpenAt)
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000444 .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
Ted Kremenek134a83a2012-01-03 23:43:13 +0000445 .Case("calloc", &UnixAPIChecker::CheckCallocZero)
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000446 .Case("malloc", &UnixAPIChecker::CheckMallocZero)
Ted Kremenek134a83a2012-01-03 23:43:13 +0000447 .Case("realloc", &UnixAPIChecker::CheckReallocZero)
Jordan Rose9e068aa2012-10-30 01:37:16 +0000448 .Case("reallocf", &UnixAPIChecker::CheckReallocfZero)
Ted Kremenek940e00f2012-01-11 08:13:21 +0000449 .Cases("alloca", "__builtin_alloca", &UnixAPIChecker::CheckAllocaZero)
David Majnemer51169932016-10-31 05:37:48 +0000450 .Case("__builtin_alloca_with_align",
451 &UnixAPIChecker::CheckAllocaWithAlignZero)
Ted Kremenek940e00f2012-01-11 08:13:21 +0000452 .Case("valloc", &UnixAPIChecker::CheckVallocZero)
Craig Topper0dbb7832014-05-27 02:45:47 +0000453 .Default(nullptr);
Ted Kremenekd55522f2010-02-25 00:20:35 +0000454
Jordy Rosef3dd00a2011-07-15 06:28:59 +0000455 if (SC)
456 (this->*SC)(C, CE);
Ted Kremenekd55522f2010-02-25 00:20:35 +0000457}
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000458
459//===----------------------------------------------------------------------===//
460// Registration.
461//===----------------------------------------------------------------------===//
462
463void ento::registerUnixAPIChecker(CheckerManager &mgr) {
464 mgr.registerChecker<UnixAPIChecker>();
465}