2010-02-25 08:20:35 +08:00
|
|
|
//= UnixAPIChecker.h - Checks preconditions for various Unix APIs --*- C++ -*-//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines UnixAPIChecker, which is an assortment of checks on calls
|
|
|
|
// to various, widely used UNIX/Posix functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-15 15:42:33 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2011-02-23 09:05:36 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2010-04-10 04:26:58 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2012-02-04 20:31:12 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-02-25 08:20:35 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-02-25 08:20:35 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-02-25 08:20:35 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > {
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT_open, BT_pthreadOnce, BT_mallocZero;
|
2011-02-23 09:05:36 +08:00
|
|
|
mutable Optional<uint64_t> Val_O_CREAT;
|
2010-04-09 06:15:34 +08:00
|
|
|
|
2010-02-25 08:20:35 +08:00
|
|
|
public:
|
2011-02-23 09:05:36 +08:00
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
2011-07-15 14:28:59 +08:00
|
|
|
|
|
|
|
void CheckOpen(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void CheckPthreadOnce(CheckerContext &C, const CallExpr *CE) const;
|
2012-01-04 07:43:13 +08:00
|
|
|
void CheckCallocZero(CheckerContext &C, const CallExpr *CE) const;
|
2011-07-15 14:28:59 +08:00
|
|
|
void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
|
2012-01-04 07:43:13 +08:00
|
|
|
void CheckReallocZero(CheckerContext &C, const CallExpr *CE) const;
|
2012-10-30 09:37:16 +08:00
|
|
|
void CheckReallocfZero(CheckerContext &C, const CallExpr *CE) const;
|
2012-01-11 16:13:21 +08:00
|
|
|
void CheckAllocaZero(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void CheckVallocZero(CheckerContext &C, const CallExpr *CE) const;
|
2011-07-15 14:28:59 +08:00
|
|
|
|
|
|
|
typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
|
|
|
|
const CallExpr *) const;
|
2012-01-04 07:43:13 +08:00
|
|
|
private:
|
|
|
|
bool ReportZeroByteAllocation(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef falseState,
|
2012-01-04 07:43:13 +08:00
|
|
|
const Expr *arg,
|
|
|
|
const char *fn_name) const;
|
2012-01-11 16:13:21 +08:00
|
|
|
void BasicAllocationCheck(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
|
|
|
const unsigned numArgs,
|
|
|
|
const unsigned sizeArg,
|
|
|
|
const char *fn) const;
|
2014-03-08 04:03:18 +08:00
|
|
|
void LazyInitialize(std::unique_ptr<BugType> &BT, const char *name) const {
|
2014-02-12 05:49:21 +08:00
|
|
|
if (BT)
|
|
|
|
return;
|
|
|
|
BT.reset(new BugType(this, name, categories::UnixAPI));
|
|
|
|
}
|
2014-08-21 00:58:03 +08:00
|
|
|
void ReportOpenBug(CheckerContext &C,
|
|
|
|
ProgramStateRef State,
|
|
|
|
const char *Msg,
|
|
|
|
SourceRange SR) const;
|
2010-02-25 08:20:35 +08:00
|
|
|
};
|
|
|
|
} //end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "open" (man 2 open)
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-21 00:58:03 +08:00
|
|
|
void UnixAPIChecker::ReportOpenBug(CheckerContext &C,
|
|
|
|
ProgramStateRef State,
|
|
|
|
const char *Msg,
|
|
|
|
SourceRange SR) const {
|
|
|
|
ExplodedNode *N = C.generateSink(State);
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LazyInitialize(BT_open, "Improper use of 'open'");
|
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
auto Report = llvm::make_unique<BugReport>(*BT_open, Msg, N);
|
2014-08-21 00:58:03 +08:00
|
|
|
Report->addRange(SR);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(Report));
|
2014-08-21 00:58:03 +08:00
|
|
|
}
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
|
2014-08-21 00:58:03 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
|
|
|
|
|
|
|
if (CE->getNumArgs() < 2) {
|
|
|
|
// The frontend should issue a warning for this case, so this is a sanity
|
|
|
|
// check.
|
|
|
|
return;
|
2014-08-21 00:58:09 +08:00
|
|
|
} else if (CE->getNumArgs() == 3) {
|
|
|
|
const Expr *Arg = CE->getArg(2);
|
|
|
|
QualType QT = Arg->getType();
|
|
|
|
if (!QT->isIntegerType()) {
|
|
|
|
ReportOpenBug(C, state,
|
|
|
|
"Third argument to 'open' is not an integer",
|
|
|
|
Arg->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
2014-08-21 00:58:03 +08:00
|
|
|
} else if (CE->getNumArgs() > 3) {
|
|
|
|
ReportOpenBug(C, state,
|
|
|
|
"Call to 'open' with more than three arguments",
|
|
|
|
CE->getArg(3)->getSourceRange());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-09 06:15:34 +08:00
|
|
|
// The definition of O_CREAT is platform specific. We need a better way
|
|
|
|
// of querying this information from the checking environment.
|
2011-07-15 14:28:59 +08:00
|
|
|
if (!Val_O_CREAT.hasValue()) {
|
2011-09-02 08:18:52 +08:00
|
|
|
if (C.getASTContext().getTargetInfo().getTriple().getVendor()
|
|
|
|
== llvm::Triple::Apple)
|
2011-07-15 14:28:59 +08:00
|
|
|
Val_O_CREAT = 0x0200;
|
2010-04-09 06:15:34 +08:00
|
|
|
else {
|
|
|
|
// FIXME: We need a more general way of getting the O_CREAT value.
|
|
|
|
// We could possibly grovel through the preprocessor state, but
|
2010-12-23 02:53:44 +08:00
|
|
|
// that would require passing the Preprocessor object to the ExprEngine.
|
2014-03-27 01:05:46 +08:00
|
|
|
// See also: MallocChecker.cpp / M_ZERO.
|
2010-04-09 06:15:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:20:35 +08:00
|
|
|
// Now check if oflags has O_CREAT set.
|
|
|
|
const Expr *oflagsEx = CE->getArg(1);
|
2012-01-07 06:09:28 +08:00
|
|
|
const SVal V = state->getSVal(oflagsEx, C.getLocationContext());
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!V.getAs<NonLoc>()) {
|
2010-02-25 08:20:35 +08:00
|
|
|
// The case where 'V' can be a location can only be due to a bad header,
|
|
|
|
// so in this case bail out.
|
|
|
|
return;
|
|
|
|
}
|
2013-02-20 13:52:05 +08:00
|
|
|
NonLoc oflags = V.castAs<NonLoc>();
|
|
|
|
NonLoc ocreateFlag = C.getSValBuilder()
|
|
|
|
.makeIntVal(Val_O_CREAT.getValue(), oflagsEx->getType()).castAs<NonLoc>();
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal maskedFlagsUC = C.getSValBuilder().evalBinOpNN(state, BO_And,
|
2010-12-02 05:28:31 +08:00
|
|
|
oflags, ocreateFlag,
|
|
|
|
oflagsEx->getType());
|
2010-02-25 08:20:35 +08:00
|
|
|
if (maskedFlagsUC.isUnknownOrUndef())
|
|
|
|
return;
|
2013-02-20 13:52:05 +08:00
|
|
|
DefinedSVal maskedFlags = maskedFlagsUC.castAs<DefinedSVal>();
|
2010-02-25 08:20:35 +08:00
|
|
|
|
|
|
|
// Check if maskedFlags is non-zero.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef trueState, falseState;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(trueState, falseState) = state->assume(maskedFlags);
|
2010-02-25 08:20:35 +08:00
|
|
|
|
|
|
|
// Only emit an error if the value of 'maskedFlags' is properly
|
|
|
|
// constrained;
|
|
|
|
if (!(trueState && !falseState))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CE->getNumArgs() < 3) {
|
2014-08-21 00:58:03 +08:00
|
|
|
ReportOpenBug(C, trueState,
|
|
|
|
"Call to 'open' requires a third argument when "
|
|
|
|
"the 'O_CREAT' flag is set",
|
|
|
|
oflagsEx->getSourceRange());
|
2010-02-25 08:20:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-09 03:53:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// pthread_once
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
void UnixAPIChecker::CheckPthreadOnce(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2010-04-09 03:53:31 +08:00
|
|
|
|
|
|
|
// This is similar to 'CheckDispatchOnce' in the MacOSXAPIChecker.
|
|
|
|
// They can possibly be refactored.
|
|
|
|
|
|
|
|
if (CE->getNumArgs() < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check if the first argument is stack allocated. If so, issue a warning
|
|
|
|
// because that's likely to be bad news.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const MemRegion *R =
|
|
|
|
state->getSVal(CE->getArg(0), C.getLocationContext()).getAsRegion();
|
2010-04-09 03:53:31 +08:00
|
|
|
if (!R || !isa<StackSpaceRegion>(R->getMemorySpace()))
|
|
|
|
return;
|
|
|
|
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(state);
|
2010-04-09 03:53:31 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> S;
|
2010-04-09 03:53:31 +08:00
|
|
|
llvm::raw_svector_ostream os(S);
|
|
|
|
os << "Call to 'pthread_once' uses";
|
|
|
|
if (const VarRegion *VR = dyn_cast<VarRegion>(R))
|
|
|
|
os << " the local variable '" << VR->getDecl()->getName() << '\'';
|
|
|
|
else
|
|
|
|
os << " stack allocated memory";
|
|
|
|
os << " for the \"control\" value. Using such transient memory for "
|
|
|
|
"the control value is potentially dangerous.";
|
|
|
|
if (isa<VarRegion>(R) && isa<StackLocalsSpaceRegion>(R->getMemorySpace()))
|
|
|
|
os << " Perhaps you intended to declare the variable as 'static'?";
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
LazyInitialize(BT_pthreadOnce, "Improper use of 'pthread_once'");
|
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
auto report = llvm::make_unique<BugReport>(*BT_pthreadOnce, os.str(), N);
|
2010-04-09 03:53:31 +08:00
|
|
|
report->addRange(CE->getArg(0)->getSourceRange());
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(report));
|
2010-04-09 03:53:31 +08:00
|
|
|
}
|
|
|
|
|
2010-11-17 02:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2012-10-30 09:37:16 +08:00
|
|
|
// "calloc", "malloc", "realloc", "reallocf", "alloca" and "valloc"
|
|
|
|
// with allocation size 0
|
2010-11-17 02:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2012-01-11 16:13:21 +08:00
|
|
|
// FIXME: Eventually these should be rolled into the MallocChecker, but right now
|
|
|
|
// they're more basic and valuable for widespread use.
|
2010-11-17 02:47:04 +08:00
|
|
|
|
2012-01-04 07:43:13 +08:00
|
|
|
// Returns true if we try to do a zero byte allocation, false otherwise.
|
|
|
|
// Fills in trueState and falseState.
|
2012-01-27 05:29:00 +08:00
|
|
|
static bool IsZeroByteAllocation(ProgramStateRef state,
|
2012-01-04 07:43:13 +08:00
|
|
|
const SVal argVal,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef *trueState,
|
|
|
|
ProgramStateRef *falseState) {
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(*trueState, *falseState) =
|
2013-02-20 13:52:05 +08:00
|
|
|
state->assume(argVal.castAs<DefinedSVal>());
|
2012-01-11 16:13:21 +08:00
|
|
|
|
2012-01-04 07:43:13 +08:00
|
|
|
return (*falseState && !*trueState);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates an error report, indicating that the function whose name is given
|
|
|
|
// will perform a zero byte allocation.
|
2013-12-06 00:25:25 +08:00
|
|
|
// Returns false if an error occurred, true otherwise.
|
2012-01-04 07:43:13 +08:00
|
|
|
bool UnixAPIChecker::ReportZeroByteAllocation(CheckerContext &C,
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef falseState,
|
2012-01-04 07:43:13 +08:00
|
|
|
const Expr *arg,
|
|
|
|
const char *fn_name) const {
|
|
|
|
ExplodedNode *N = C.generateSink(falseState);
|
|
|
|
if (!N)
|
|
|
|
return false;
|
|
|
|
|
2012-01-11 16:13:21 +08:00
|
|
|
LazyInitialize(BT_mallocZero,
|
2014-02-12 05:49:21 +08:00
|
|
|
"Undefined allocation of 0 bytes (CERT MEM04-C; CWE-131)");
|
2012-01-04 07:43:13 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> S;
|
2012-01-04 07:43:13 +08:00
|
|
|
llvm::raw_svector_ostream os(S);
|
|
|
|
os << "Call to '" << fn_name << "' has an allocation size of 0 bytes";
|
2015-06-23 21:15:32 +08:00
|
|
|
auto report = llvm::make_unique<BugReport>(*BT_mallocZero, os.str(), N);
|
2012-01-04 07:43:13 +08:00
|
|
|
|
|
|
|
report->addRange(arg->getSourceRange());
|
2012-08-28 08:50:51 +08:00
|
|
|
bugreporter::trackNullOrUndefValue(N, arg, *report);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(report));
|
2012-01-04 07:43:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-11 16:13:21 +08:00
|
|
|
// Does a basic check for 0-sized allocations suitable for most of the below
|
|
|
|
// functions (modulo "calloc")
|
|
|
|
void UnixAPIChecker::BasicAllocationCheck(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
|
|
|
const unsigned numArgs,
|
|
|
|
const unsigned sizeArg,
|
|
|
|
const char *fn) const {
|
|
|
|
// Sanity check for the correct number of arguments
|
|
|
|
if (CE->getNumArgs() != numArgs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check if the allocation size is 0.
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2014-05-27 10:45:47 +08:00
|
|
|
ProgramStateRef trueState = nullptr, falseState = nullptr;
|
2012-01-11 16:13:21 +08:00
|
|
|
const Expr *arg = CE->getArg(sizeArg);
|
|
|
|
SVal argVal = state->getSVal(arg, C.getLocationContext());
|
|
|
|
|
|
|
|
if (argVal.isUnknownOrUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Is the value perfectly constrained to zero?
|
|
|
|
if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
|
|
|
|
(void) ReportZeroByteAllocation(C, falseState, arg, fn);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-23 16:59:39 +08:00
|
|
|
// Assume the value is non-zero going forward.
|
2012-01-11 16:13:21 +08:00
|
|
|
assert(trueState);
|
|
|
|
if (trueState != state)
|
|
|
|
C.addTransition(trueState);
|
|
|
|
}
|
|
|
|
|
2012-01-04 07:43:13 +08:00
|
|
|
void UnixAPIChecker::CheckCallocZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
|
|
|
unsigned int nArgs = CE->getNumArgs();
|
|
|
|
if (nArgs != 2)
|
|
|
|
return;
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2014-05-27 10:45:47 +08:00
|
|
|
ProgramStateRef trueState = nullptr, falseState = nullptr;
|
2012-01-04 07:43:13 +08:00
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < nArgs; i++) {
|
|
|
|
const Expr *arg = CE->getArg(i);
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal argVal = state->getSVal(arg, C.getLocationContext());
|
2012-01-04 07:43:13 +08:00
|
|
|
if (argVal.isUnknownOrUndef()) {
|
|
|
|
if (i == 0)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
|
|
|
|
if (ReportZeroByteAllocation(C, falseState, arg, "calloc"))
|
|
|
|
return;
|
|
|
|
else if (i == 0)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 16:59:39 +08:00
|
|
|
// Assume the value is non-zero going forward.
|
2012-01-04 07:43:13 +08:00
|
|
|
assert(trueState);
|
|
|
|
if (trueState != state)
|
|
|
|
C.addTransition(trueState);
|
|
|
|
}
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2012-01-11 16:13:21 +08:00
|
|
|
BasicAllocationCheck(C, CE, 1, 0, "malloc");
|
2012-01-04 07:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnixAPIChecker::CheckReallocZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2012-01-11 16:13:21 +08:00
|
|
|
BasicAllocationCheck(C, CE, 2, 1, "realloc");
|
|
|
|
}
|
2012-01-04 07:43:13 +08:00
|
|
|
|
2012-10-30 09:37:16 +08:00
|
|
|
void UnixAPIChecker::CheckReallocfZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
|
|
|
BasicAllocationCheck(C, CE, 2, 1, "reallocf");
|
|
|
|
}
|
|
|
|
|
2012-01-11 16:13:21 +08:00
|
|
|
void UnixAPIChecker::CheckAllocaZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
|
|
|
BasicAllocationCheck(C, CE, 1, 0, "alloca");
|
|
|
|
}
|
2012-01-04 07:43:13 +08:00
|
|
|
|
2012-01-11 16:13:21 +08:00
|
|
|
void UnixAPIChecker::CheckVallocZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
|
|
|
BasicAllocationCheck(C, CE, 1, 0, "valloc");
|
|
|
|
}
|
2012-01-04 07:43:13 +08:00
|
|
|
|
|
|
|
|
2010-02-25 08:20:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Central dispatch function.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-11 16:13:21 +08:00
|
|
|
void UnixAPIChecker::checkPreStmt(const CallExpr *CE,
|
|
|
|
CheckerContext &C) const {
|
2012-07-11 07:13:01 +08:00
|
|
|
const FunctionDecl *FD = C.getCalleeDecl(CE);
|
|
|
|
if (!FD || FD->getKind() != Decl::Function)
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef FName = C.getCalleeName(FD);
|
2011-12-01 13:57:37 +08:00
|
|
|
if (FName.empty())
|
2010-02-25 08:20:35 +08:00
|
|
|
return;
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
SubChecker SC =
|
2011-12-01 13:57:37 +08:00
|
|
|
llvm::StringSwitch<SubChecker>(FName)
|
2011-07-15 14:28:59 +08:00
|
|
|
.Case("open", &UnixAPIChecker::CheckOpen)
|
|
|
|
.Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
|
2012-01-04 07:43:13 +08:00
|
|
|
.Case("calloc", &UnixAPIChecker::CheckCallocZero)
|
2011-07-15 14:28:59 +08:00
|
|
|
.Case("malloc", &UnixAPIChecker::CheckMallocZero)
|
2012-01-04 07:43:13 +08:00
|
|
|
.Case("realloc", &UnixAPIChecker::CheckReallocZero)
|
2012-10-30 09:37:16 +08:00
|
|
|
.Case("reallocf", &UnixAPIChecker::CheckReallocfZero)
|
2012-01-11 16:13:21 +08:00
|
|
|
.Cases("alloca", "__builtin_alloca", &UnixAPIChecker::CheckAllocaZero)
|
|
|
|
.Case("valloc", &UnixAPIChecker::CheckVallocZero)
|
2014-05-27 10:45:47 +08:00
|
|
|
.Default(nullptr);
|
2011-07-15 14:28:59 +08:00
|
|
|
|
|
|
|
if (SC)
|
|
|
|
(this->*SC)(C, CE);
|
2010-02-25 08:20:35 +08:00
|
|
|
}
|
2011-02-23 09:05:36 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Registration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ento::registerUnixAPIChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UnixAPIChecker>();
|
|
|
|
}
|