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"
|
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"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-02-23 09:05:36 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-04-10 04:26:58 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2010-02-25 08:20:35 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-04-10 04:26:58 +08:00
|
|
|
using llvm::Optional;
|
2010-02-25 08:20:35 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class UnixAPIChecker : public Checker< check::PreStmt<CallExpr> > {
|
2011-07-15 14:28:59 +08:00
|
|
|
mutable llvm::OwningPtr<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;
|
|
|
|
void CheckMallocZero(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
|
|
|
|
typedef void (UnixAPIChecker::*SubChecker)(CheckerContext &,
|
|
|
|
const CallExpr *) const;
|
2010-02-25 08:20:35 +08:00
|
|
|
};
|
|
|
|
} //end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
static inline void LazyInitialize(llvm::OwningPtr<BugType> &BT,
|
|
|
|
const char *name) {
|
2010-02-25 08:20:35 +08:00
|
|
|
if (BT)
|
|
|
|
return;
|
2011-07-15 14:28:59 +08:00
|
|
|
BT.reset(new BugType(name, "Unix API"));
|
2010-02-25 08:20:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "open" (man 2 open)
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
|
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()) {
|
2010-04-09 06:15:34 +08:00
|
|
|
if (C.getASTContext().Target.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.
|
2010-04-09 06:15:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:20:35 +08:00
|
|
|
// Look at the 'oflags' argument for the O_CREAT flag.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-02-25 08:20:35 +08:00
|
|
|
|
|
|
|
if (CE->getNumArgs() < 2) {
|
|
|
|
// The frontend should issue a warning for this case, so this is a sanity
|
|
|
|
// check.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check if oflags has O_CREAT set.
|
|
|
|
const Expr *oflagsEx = CE->getArg(1);
|
|
|
|
const SVal V = state->getSVal(oflagsEx);
|
|
|
|
if (!isa<NonLoc>(V)) {
|
|
|
|
// The case where 'V' can be a location can only be due to a bad header,
|
|
|
|
// so in this case bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NonLoc oflags = cast<NonLoc>(V);
|
|
|
|
NonLoc ocreateFlag =
|
2011-07-15 14:28:59 +08:00
|
|
|
cast<NonLoc>(C.getSValBuilder().makeIntVal(Val_O_CREAT.getValue(),
|
2010-02-25 08:20:35 +08:00
|
|
|
oflagsEx->getType()));
|
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;
|
|
|
|
DefinedSVal maskedFlags = cast<DefinedSVal>(maskedFlagsUC);
|
|
|
|
|
|
|
|
// Check if maskedFlags is non-zero.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *trueState, *falseState;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::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) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(trueState);
|
2010-02-25 13:44:05 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
LazyInitialize(BT_open, "Improper use of 'open'");
|
|
|
|
|
|
|
|
RangedBugReport *report =
|
|
|
|
new RangedBugReport(*BT_open,
|
2010-02-25 08:20:35 +08:00
|
|
|
"Call to 'open' requires a third argument when "
|
|
|
|
"the 'O_CREAT' flag is set", N);
|
|
|
|
report->addRange(oflagsEx->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-04-09 03:53:31 +08:00
|
|
|
const MemRegion *R = state->getSVal(CE->getArg(0)).getAsRegion();
|
|
|
|
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;
|
|
|
|
|
|
|
|
llvm::SmallString<256> S;
|
|
|
|
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'");
|
|
|
|
|
|
|
|
RangedBugReport *report = new RangedBugReport(*BT_pthreadOnce, os.str(), N);
|
2010-04-09 03:53:31 +08:00
|
|
|
report->addRange(CE->getArg(0)->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
|
2010-11-17 02:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "malloc" with allocation size 0
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// FIXME: Eventually this should be rolled into the MallocChecker, but this
|
|
|
|
// check is more basic and is valuable for widespread use.
|
2011-07-15 14:28:59 +08:00
|
|
|
void UnixAPIChecker::CheckMallocZero(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2010-11-17 02:47:04 +08:00
|
|
|
|
|
|
|
// Sanity check that malloc takes one argument.
|
|
|
|
if (CE->getNumArgs() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check if the allocation size is 0.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-11-17 02:47:04 +08:00
|
|
|
SVal argVal = state->getSVal(CE->getArg(0));
|
|
|
|
|
|
|
|
if (argVal.isUnknownOrUndef())
|
|
|
|
return;
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *trueState, *falseState;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(trueState, falseState) = state->assume(cast<DefinedSVal>(argVal));
|
2010-11-17 02:47:04 +08:00
|
|
|
|
|
|
|
// Is the value perfectly constrained to zero?
|
|
|
|
if (falseState && !trueState) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(falseState);
|
2010-11-17 02:47:04 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2010-11-17 08:50:34 +08:00
|
|
|
// FIXME: Add reference to CERT advisory, and/or C99 standard in bug
|
|
|
|
// output.
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
LazyInitialize(BT_mallocZero, "Undefined allocation of 0 bytes");
|
2010-11-17 02:47:04 +08:00
|
|
|
|
|
|
|
EnhancedBugReport *report =
|
2011-07-15 14:28:59 +08:00
|
|
|
new EnhancedBugReport(*BT_mallocZero, "Call to 'malloc' has an allocation"
|
|
|
|
" size of 0 bytes", N);
|
2010-11-17 02:47:04 +08:00
|
|
|
report->addRange(CE->getArg(0)->getSourceRange());
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
|
|
|
|
CE->getArg(0));
|
|
|
|
C.EmitReport(report);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Assume the the value is non-zero going forward.
|
|
|
|
assert(trueState);
|
|
|
|
if (trueState != state) {
|
|
|
|
C.addTransition(trueState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 08:20:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Central dispatch function.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-23 09:05:36 +08:00
|
|
|
void UnixAPIChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
|
2010-02-25 08:20:35 +08:00
|
|
|
// Get the callee. All the functions we care about are C functions
|
|
|
|
// with simple identifiers.
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-02-25 08:20:35 +08:00
|
|
|
const Expr *Callee = CE->getCallee();
|
2011-07-15 14:28:59 +08:00
|
|
|
const FunctionDecl *Fn = state->getSVal(Callee).getAsFunctionDecl();
|
2010-02-25 08:20:35 +08:00
|
|
|
|
|
|
|
if (!Fn)
|
|
|
|
return;
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
const IdentifierInfo *FI = Fn->getIdentifier();
|
2010-02-25 08:20:35 +08:00
|
|
|
if (!FI)
|
|
|
|
return;
|
|
|
|
|
2011-07-15 14:28:59 +08:00
|
|
|
SubChecker SC =
|
|
|
|
llvm::StringSwitch<SubChecker>(FI->getName())
|
|
|
|
.Case("open", &UnixAPIChecker::CheckOpen)
|
|
|
|
.Case("pthread_once", &UnixAPIChecker::CheckPthreadOnce)
|
|
|
|
.Case("malloc", &UnixAPIChecker::CheckMallocZero)
|
|
|
|
.Default(NULL);
|
|
|
|
|
|
|
|
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>();
|
|
|
|
}
|