1f4a2713aSLionel Sambuc //= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- C++ -*-//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This defines UnixAPIChecker, which is an assortment of checks on calls
11f4a2713aSLionel Sambuc // to various, widely used UNIX/Posix functions.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
17f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
19f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/Optional.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
26f4a2713aSLionel Sambuc #include <fcntl.h>
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc using namespace clang;
29f4a2713aSLionel Sambuc using namespace ento;
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc namespace {
32f4a2713aSLionel Sambuc class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > {
33*0a6a1f1dSLionel Sambuc mutable std::unique_ptr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero;
34f4a2713aSLionel Sambuc mutable Optional<uint64_t> Val_O_CREAT;
35f4a2713aSLionel Sambuc
36f4a2713aSLionel Sambuc public:
37f4a2713aSLionel Sambuc void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
38f4a2713aSLionel Sambuc
39f4a2713aSLionel Sambuc void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
40f4a2713aSLionel Sambuc void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
41f4a2713aSLionel Sambuc void CheckCallocZero(CheckerContext &C, const CallExpr *CE) const;
42f4a2713aSLionel Sambuc void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
43f4a2713aSLionel Sambuc void CheckReallocZero(CheckerContext &C, const CallExpr *CE) const;
44f4a2713aSLionel Sambuc void CheckReallocfZero(CheckerContext &C, const CallExpr *CE) const;
45f4a2713aSLionel Sambuc void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const;
46f4a2713aSLionel Sambuc void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const;
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
49f4a2713aSLionel Sambuc const CallExpr *) const;
50f4a2713aSLionel Sambuc private:
51f4a2713aSLionel Sambuc bool ReportZeroByteAllocation(CheckerContext &C,
52f4a2713aSLionel Sambuc ProgramStateRef falseState,
53f4a2713aSLionel Sambuc const Expr *arg,
54f4a2713aSLionel Sambuc const char *fn_name) const;
55f4a2713aSLionel Sambuc void BasicAllocationCheck(CheckerContext &C,
56f4a2713aSLionel Sambuc const CallExpr *CE,
57f4a2713aSLionel Sambuc const unsigned numArgs,
58f4a2713aSLionel Sambuc const unsigned sizeArg,
59f4a2713aSLionel Sambuc const char *fn) const;
LazyInitialize(std::unique_ptr<BugType> & BT,const char * name) const60*0a6a1f1dSLionel Sambuc void LazyInitialize(std::unique_ptr<BugType> &BT, const char *name) const {
61f4a2713aSLionel Sambuc if (BT)
62f4a2713aSLionel Sambuc return;
63*0a6a1f1dSLionel Sambuc BT.reset(new BugType(this, name, categories::UnixAPI));
64f4a2713aSLionel Sambuc }
65*0a6a1f1dSLionel Sambuc void ReportOpenBug(CheckerContext &C,
66*0a6a1f1dSLionel Sambuc ProgramStateRef State,
67*0a6a1f1dSLionel Sambuc const char *Msg,
68*0a6a1f1dSLionel Sambuc SourceRange SR) const;
69*0a6a1f1dSLionel Sambuc };
70*0a6a1f1dSLionel Sambuc } //end anonymous namespace
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
73f4a2713aSLionel Sambuc // "open" (man 2 open)
74f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
75f4a2713aSLionel Sambuc
ReportOpenBug(CheckerContext & C,ProgramStateRef State,const char * Msg,SourceRange SR) const76*0a6a1f1dSLionel Sambuc void UnixAPIChecker::ReportOpenBug(CheckerContext &C,
77*0a6a1f1dSLionel Sambuc ProgramStateRef State,
78*0a6a1f1dSLionel Sambuc const char *Msg,
79*0a6a1f1dSLionel Sambuc SourceRange SR) const {
80*0a6a1f1dSLionel Sambuc ExplodedNode *N = C.generateSink(State);
81*0a6a1f1dSLionel Sambuc if (!N)
82*0a6a1f1dSLionel Sambuc return;
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc LazyInitialize(BT_open, "Improper use of 'open'");
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc BugReport *Report = new BugReport(*BT_open, Msg, N);
87*0a6a1f1dSLionel Sambuc Report->addRange(SR);
88*0a6a1f1dSLionel Sambuc C.emitReport(Report);
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
CheckOpen(CheckerContext & C,const CallExpr * CE) const91f4a2713aSLionel Sambuc void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
92*0a6a1f1dSLionel Sambuc ProgramStateRef state = C.getState();
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc if (CE->getNumArgs() < 2) {
95*0a6a1f1dSLionel Sambuc // The frontend should issue a warning for this case, so this is a sanity
96*0a6a1f1dSLionel Sambuc // check.
97*0a6a1f1dSLionel Sambuc return;
98*0a6a1f1dSLionel Sambuc } else if (CE->getNumArgs() == 3) {
99*0a6a1f1dSLionel Sambuc const Expr *Arg = CE->getArg(2);
100*0a6a1f1dSLionel Sambuc QualType QT = Arg->getType();
101*0a6a1f1dSLionel Sambuc if (!QT->isIntegerType()) {
102*0a6a1f1dSLionel Sambuc ReportOpenBug(C, state,
103*0a6a1f1dSLionel Sambuc "Third argument to 'open' is not an integer",
104*0a6a1f1dSLionel Sambuc Arg->getSourceRange());
105*0a6a1f1dSLionel Sambuc return;
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc } else if (CE->getNumArgs() > 3) {
108*0a6a1f1dSLionel Sambuc ReportOpenBug(C, state,
109*0a6a1f1dSLionel Sambuc "Call to 'open' with more than three arguments",
110*0a6a1f1dSLionel Sambuc CE->getArg(3)->getSourceRange());
111*0a6a1f1dSLionel Sambuc return;
112*0a6a1f1dSLionel Sambuc }
113*0a6a1f1dSLionel Sambuc
114f4a2713aSLionel Sambuc // The definition of O_CREAT is platform specific. We need a better way
115f4a2713aSLionel Sambuc // of querying this information from the checking environment.
116f4a2713aSLionel Sambuc if (!Val_O_CREAT.hasValue()) {
117f4a2713aSLionel Sambuc if (C.getASTContext().getTargetInfo().getTriple().getVendor()
118f4a2713aSLionel Sambuc == llvm::Triple::Apple)
119f4a2713aSLionel Sambuc Val_O_CREAT = 0x0200;
120f4a2713aSLionel Sambuc else {
121f4a2713aSLionel Sambuc // FIXME: We need a more general way of getting the O_CREAT value.
122f4a2713aSLionel Sambuc // We could possibly grovel through the preprocessor state, but
123f4a2713aSLionel Sambuc // that would require passing the Preprocessor object to the ExprEngine.
124*0a6a1f1dSLionel Sambuc // See also: MallocChecker.cpp / M_ZERO.
125f4a2713aSLionel Sambuc return;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
129f4a2713aSLionel Sambuc // Now check if oflags has O_CREAT set.
130f4a2713aSLionel Sambuc const Expr *oflagsEx = CE->getArg(1);
131f4a2713aSLionel Sambuc const SVal V = state->getSVal(oflagsEx, C.getLocationContext());
132f4a2713aSLionel Sambuc if (!V.getAs<NonLoc>()) {
133f4a2713aSLionel Sambuc // The case where 'V' can be a location can only be due to a bad header,
134f4a2713aSLionel Sambuc // so in this case bail out.
135f4a2713aSLionel Sambuc return;
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc NonLoc oflags = V.castAs<NonLoc>();
138f4a2713aSLionel Sambuc NonLoc ocreateFlag = C.getSValBuilder()
139f4a2713aSLionel Sambuc .makeIntVal(Val_O_CREAT.getValue(), oflagsEx->getType()).castAs<NonLoc>();
140f4a2713aSLionel Sambuc SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And,
141f4a2713aSLionel Sambuc oflags, ocreateFlag,
142f4a2713aSLionel Sambuc oflagsEx->getType());
143f4a2713aSLionel Sambuc if (maskedFlagsUC.isUnknownOrUndef())
144f4a2713aSLionel Sambuc return;
145f4a2713aSLionel Sambuc DefinedSVal maskedFlags = maskedFlagsUC.castAs<DefinedSVal>();
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc // Check if maskedFlags is non-zero.
148f4a2713aSLionel Sambuc ProgramStateRef trueState, falseState;
149*0a6a1f1dSLionel Sambuc std::tie(trueState, falseState) = state->assume(maskedFlags);
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc // Only emit an error if the value of 'maskedFlags' is properly
152f4a2713aSLionel Sambuc // constrained;
153f4a2713aSLionel Sambuc if (!(trueState && !falseState))
154f4a2713aSLionel Sambuc return;
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc if (CE->getNumArgs() < 3) {
157*0a6a1f1dSLionel Sambuc ReportOpenBug(C, trueState,
158f4a2713aSLionel Sambuc "Call to 'open' requires a third argument when "
159*0a6a1f1dSLionel Sambuc "the 'O_CREAT' flag is set",
160*0a6a1f1dSLionel Sambuc oflagsEx->getSourceRange());
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc }
163f4a2713aSLionel Sambuc
164f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
165f4a2713aSLionel Sambuc // pthread_once
166f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
167f4a2713aSLionel Sambuc
CheckPthreadOnce(CheckerContext & C,const CallExpr * CE) const168f4a2713aSLionel Sambuc void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C,
169f4a2713aSLionel Sambuc const CallExpr *CE) const {
170f4a2713aSLionel Sambuc
171f4a2713aSLionel Sambuc // This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
172f4a2713aSLionel Sambuc // They can possibly be refactored.
173f4a2713aSLionel Sambuc
174f4a2713aSLionel Sambuc if (CE->getNumArgs() < 1)
175f4a2713aSLionel Sambuc return;
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc // Check if the first argument is stack allocated. If so, issue a warning
178f4a2713aSLionel Sambuc // because that's likely to be bad news.
179f4a2713aSLionel Sambuc ProgramStateRef state = C.getState();
180f4a2713aSLionel Sambuc const MemRegion *R =
181f4a2713aSLionel Sambuc state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
182f4a2713aSLionel Sambuc if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
183f4a2713aSLionel Sambuc return;
184f4a2713aSLionel Sambuc
185f4a2713aSLionel Sambuc ExplodedNode *N = C.generateSink(state);
186f4a2713aSLionel Sambuc if (!N)
187f4a2713aSLionel Sambuc return;
188f4a2713aSLionel Sambuc
189f4a2713aSLionel Sambuc SmallString<256> S;
190f4a2713aSLionel Sambuc llvm::raw_svector_ostream os(S);
191f4a2713aSLionel Sambuc os << "Call to 'pthread_once' uses";
192f4a2713aSLionel Sambuc if (const VarRegion *VR = dyn_cast<VarRegion>(R))
193f4a2713aSLionel Sambuc os << " the local variable '" << VR->getDecl()->getName() << '\'';
194f4a2713aSLionel Sambuc else
195f4a2713aSLionel Sambuc os << " stack allocated memory";
196f4a2713aSLionel Sambuc os << " for the \"control\" value. Using such transient memory for "
197f4a2713aSLionel Sambuc "the control value is potentially dangerous.";
198f4a2713aSLionel Sambuc if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
199f4a2713aSLionel Sambuc os << " Perhaps you intended to declare the variable as 'static'?";
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'");
202f4a2713aSLionel Sambuc
203f4a2713aSLionel Sambuc BugReport *report = new BugReport(*BT_pthreadOnce, os.str(), N);
204f4a2713aSLionel Sambuc report->addRange(CE->getArg(0)->getSourceRange());
205f4a2713aSLionel Sambuc C.emitReport(report);
206f4a2713aSLionel Sambuc }
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
209f4a2713aSLionel Sambuc // "calloc", "malloc", "realloc", "reallocf", "alloca" and "valloc"
210f4a2713aSLionel Sambuc // with allocation size 0
211f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
212f4a2713aSLionel Sambuc // FIXME: Eventually these should be rolled into the MallocChecker, but right now
213f4a2713aSLionel Sambuc // they're more basic and valuable for widespread use.
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc // Returns true if we try to do a zero byte allocation, false otherwise.
216f4a2713aSLionel Sambuc // Fills in trueState and falseState.
IsZeroByteAllocation(ProgramStateRef state,const SVal argVal,ProgramStateRef * trueState,ProgramStateRef * falseState)217f4a2713aSLionel Sambuc static bool IsZeroByteAllocation(ProgramStateRef state,
218f4a2713aSLionel Sambuc const SVal argVal,
219f4a2713aSLionel Sambuc ProgramStateRef *trueState,
220f4a2713aSLionel Sambuc ProgramStateRef *falseState) {
221*0a6a1f1dSLionel Sambuc std::tie(*trueState, *falseState) =
222f4a2713aSLionel Sambuc state->assume(argVal.castAs<DefinedSVal>());
223f4a2713aSLionel Sambuc
224f4a2713aSLionel Sambuc return (*falseState && !*trueState);
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc // Generates an error report, indicating that the function whose name is given
228f4a2713aSLionel Sambuc // will perform a zero byte allocation.
229*0a6a1f1dSLionel Sambuc // Returns false if an error occurred, true otherwise.
ReportZeroByteAllocation(CheckerContext & C,ProgramStateRef falseState,const Expr * arg,const char * fn_name) const230f4a2713aSLionel Sambuc bool UnixAPIChecker::ReportZeroByteAllocation(CheckerContext &C,
231f4a2713aSLionel Sambuc ProgramStateRef falseState,
232f4a2713aSLionel Sambuc const Expr *arg,
233f4a2713aSLionel Sambuc const char *fn_name) const {
234f4a2713aSLionel Sambuc ExplodedNode *N = C.generateSink(falseState);
235f4a2713aSLionel Sambuc if (!N)
236f4a2713aSLionel Sambuc return false;
237f4a2713aSLionel Sambuc
238f4a2713aSLionel Sambuc LazyInitialize(BT_mallocZero,
239f4a2713aSLionel Sambuc "Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)");
240f4a2713aSLionel Sambuc
241f4a2713aSLionel Sambuc SmallString<256> S;
242f4a2713aSLionel Sambuc llvm::raw_svector_ostream os(S);
243f4a2713aSLionel Sambuc os << "Call to '" << fn_name << "' has an allocation size of 0 bytes";
244f4a2713aSLionel Sambuc BugReport *report = new BugReport(*BT_mallocZero, os.str(), N);
245f4a2713aSLionel Sambuc
246f4a2713aSLionel Sambuc report->addRange(arg->getSourceRange());
247f4a2713aSLionel Sambuc bugreporter::trackNullOrUndefValue(N, arg, *report);
248f4a2713aSLionel Sambuc C.emitReport(report);
249f4a2713aSLionel Sambuc
250f4a2713aSLionel Sambuc return true;
251f4a2713aSLionel Sambuc }
252f4a2713aSLionel Sambuc
253f4a2713aSLionel Sambuc // Does a basic check for 0-sized allocations suitable for most of the below
254f4a2713aSLionel Sambuc // functions (modulo "calloc")
BasicAllocationCheck(CheckerContext & C,const CallExpr * CE,const unsigned numArgs,const unsigned sizeArg,const char * fn) const255f4a2713aSLionel Sambuc void UnixAPIChecker::BasicAllocationCheck(CheckerContext &C,
256f4a2713aSLionel Sambuc const CallExpr *CE,
257f4a2713aSLionel Sambuc const unsigned numArgs,
258f4a2713aSLionel Sambuc const unsigned sizeArg,
259f4a2713aSLionel Sambuc const char *fn) const {
260f4a2713aSLionel Sambuc // Sanity check for the correct number of arguments
261f4a2713aSLionel Sambuc if (CE->getNumArgs() != numArgs)
262f4a2713aSLionel Sambuc return;
263f4a2713aSLionel Sambuc
264f4a2713aSLionel Sambuc // Check if the allocation size is 0.
265f4a2713aSLionel Sambuc ProgramStateRef state = C.getState();
266*0a6a1f1dSLionel Sambuc ProgramStateRef trueState = nullptr, falseState = nullptr;
267f4a2713aSLionel Sambuc const Expr *arg = CE->getArg(sizeArg);
268f4a2713aSLionel Sambuc SVal argVal = state->getSVal(arg, C.getLocationContext());
269f4a2713aSLionel Sambuc
270f4a2713aSLionel Sambuc if (argVal.isUnknownOrUndef())
271f4a2713aSLionel Sambuc return;
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc // Is the value perfectly constrained to zero?
274f4a2713aSLionel Sambuc if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
275f4a2713aSLionel Sambuc (void) ReportZeroByteAllocation(C, falseState, arg, fn);
276f4a2713aSLionel Sambuc return;
277f4a2713aSLionel Sambuc }
278f4a2713aSLionel Sambuc // Assume the value is non-zero going forward.
279f4a2713aSLionel Sambuc assert(trueState);
280f4a2713aSLionel Sambuc if (trueState != state)
281f4a2713aSLionel Sambuc C.addTransition(trueState);
282f4a2713aSLionel Sambuc }
283f4a2713aSLionel Sambuc
CheckCallocZero(CheckerContext & C,const CallExpr * CE) const284f4a2713aSLionel Sambuc void UnixAPIChecker::CheckCallocZero(CheckerContext &C,
285f4a2713aSLionel Sambuc const CallExpr *CE) const {
286f4a2713aSLionel Sambuc unsigned int nArgs = CE->getNumArgs();
287f4a2713aSLionel Sambuc if (nArgs != 2)
288f4a2713aSLionel Sambuc return;
289f4a2713aSLionel Sambuc
290f4a2713aSLionel Sambuc ProgramStateRef state = C.getState();
291*0a6a1f1dSLionel Sambuc ProgramStateRef trueState = nullptr, falseState = nullptr;
292f4a2713aSLionel Sambuc
293f4a2713aSLionel Sambuc unsigned int i;
294f4a2713aSLionel Sambuc for (i = 0; i < nArgs; i++) {
295f4a2713aSLionel Sambuc const Expr *arg = CE->getArg(i);
296f4a2713aSLionel Sambuc SVal argVal = state->getSVal(arg, C.getLocationContext());
297f4a2713aSLionel Sambuc if (argVal.isUnknownOrUndef()) {
298f4a2713aSLionel Sambuc if (i == 0)
299f4a2713aSLionel Sambuc continue;
300f4a2713aSLionel Sambuc else
301f4a2713aSLionel Sambuc return;
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc
304f4a2713aSLionel Sambuc if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
305f4a2713aSLionel Sambuc if (ReportZeroByteAllocation(C, falseState, arg, "calloc"))
306f4a2713aSLionel Sambuc return;
307f4a2713aSLionel Sambuc else if (i == 0)
308f4a2713aSLionel Sambuc continue;
309f4a2713aSLionel Sambuc else
310f4a2713aSLionel Sambuc return;
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc }
313f4a2713aSLionel Sambuc
314f4a2713aSLionel Sambuc // Assume the value is non-zero going forward.
315f4a2713aSLionel Sambuc assert(trueState);
316f4a2713aSLionel Sambuc if (trueState != state)
317f4a2713aSLionel Sambuc C.addTransition(trueState);
318f4a2713aSLionel Sambuc }
319f4a2713aSLionel Sambuc
CheckMallocZero(CheckerContext & C,const CallExpr * CE) const320f4a2713aSLionel Sambuc void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
321f4a2713aSLionel Sambuc const CallExpr *CE) const {
322f4a2713aSLionel Sambuc BasicAllocationCheck(C, CE, 1, 0, "malloc");
323f4a2713aSLionel Sambuc }
324f4a2713aSLionel Sambuc
CheckReallocZero(CheckerContext & C,const CallExpr * CE) const325f4a2713aSLionel Sambuc void UnixAPIChecker::CheckReallocZero(CheckerContext &C,
326f4a2713aSLionel Sambuc const CallExpr *CE) const {
327f4a2713aSLionel Sambuc BasicAllocationCheck(C, CE, 2, 1, "realloc");
328f4a2713aSLionel Sambuc }
329f4a2713aSLionel Sambuc
CheckReallocfZero(CheckerContext & C,const CallExpr * CE) const330f4a2713aSLionel Sambuc void UnixAPIChecker::CheckReallocfZero(CheckerContext &C,
331f4a2713aSLionel Sambuc const CallExpr *CE) const {
332f4a2713aSLionel Sambuc BasicAllocationCheck(C, CE, 2, 1, "reallocf");
333f4a2713aSLionel Sambuc }
334f4a2713aSLionel Sambuc
CheckAllocaZero(CheckerContext & C,const CallExpr * CE) const335f4a2713aSLionel Sambuc void UnixAPIChecker::CheckAllocaZero(CheckerContext &C,
336f4a2713aSLionel Sambuc const CallExpr *CE) const {
337f4a2713aSLionel Sambuc BasicAllocationCheck(C, CE, 1, 0, "alloca");
338f4a2713aSLionel Sambuc }
339f4a2713aSLionel Sambuc
CheckVallocZero(CheckerContext & C,const CallExpr * CE) const340f4a2713aSLionel Sambuc void UnixAPIChecker::CheckVallocZero(CheckerContext &C,
341f4a2713aSLionel Sambuc const CallExpr *CE) const {
342f4a2713aSLionel Sambuc BasicAllocationCheck(C, CE, 1, 0, "valloc");
343f4a2713aSLionel Sambuc }
344f4a2713aSLionel Sambuc
345f4a2713aSLionel Sambuc
346f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
347f4a2713aSLionel Sambuc // Central dispatch function.
348f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
349f4a2713aSLionel Sambuc
checkPreStmt(const CallExpr * CE,CheckerContext & C) const350f4a2713aSLionel Sambuc void UnixAPIChecker::checkPreStmt(const CallExpr *CE,
351f4a2713aSLionel Sambuc CheckerContext &C) const {
352f4a2713aSLionel Sambuc const FunctionDecl *FD = C.getCalleeDecl(CE);
353f4a2713aSLionel Sambuc if (!FD || FD->getKind() != Decl::Function)
354f4a2713aSLionel Sambuc return;
355f4a2713aSLionel Sambuc
356f4a2713aSLionel Sambuc StringRef FName = C.getCalleeName(FD);
357f4a2713aSLionel Sambuc if (FName.empty())
358f4a2713aSLionel Sambuc return;
359f4a2713aSLionel Sambuc
360f4a2713aSLionel Sambuc SubChecker SC =
361f4a2713aSLionel Sambuc llvm::StringSwitch<SubChecker>(FName)
362f4a2713aSLionel Sambuc .Case("open", &UnixAPIChecker::CheckOpen)
363f4a2713aSLionel Sambuc .Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
364f4a2713aSLionel Sambuc .Case("calloc", &UnixAPIChecker::CheckCallocZero)
365f4a2713aSLionel Sambuc .Case("malloc", &UnixAPIChecker::CheckMallocZero)
366f4a2713aSLionel Sambuc .Case("realloc", &UnixAPIChecker::CheckReallocZero)
367f4a2713aSLionel Sambuc .Case("reallocf", &UnixAPIChecker::CheckReallocfZero)
368f4a2713aSLionel Sambuc .Cases("alloca", "__builtin_alloca", &UnixAPIChecker::CheckAllocaZero)
369f4a2713aSLionel Sambuc .Case("valloc", &UnixAPIChecker::CheckVallocZero)
370*0a6a1f1dSLionel Sambuc .Default(nullptr);
371f4a2713aSLionel Sambuc
372f4a2713aSLionel Sambuc if (SC)
373f4a2713aSLionel Sambuc (this->*SC)(C, CE);
374f4a2713aSLionel Sambuc }
375f4a2713aSLionel Sambuc
376f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
377f4a2713aSLionel Sambuc // Registration.
378f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
379f4a2713aSLionel Sambuc
registerUnixAPIChecker(CheckerManager & mgr)380f4a2713aSLionel Sambuc void ento::registerUnixAPIChecker(CheckerManager &mgr) {
381f4a2713aSLionel Sambuc mgr.registerChecker<UnixAPIChecker>();
382f4a2713aSLionel Sambuc }
383