2010-07-07 07:11:01 +08:00
|
|
|
//= CStringChecker.h - Checks calls to C string functions ----------*- C++ -*-//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines CStringChecker, which is an assortment of checks on calls
|
|
|
|
// to functions in <string.h>.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-16 05:25:03 +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-24 09:05:30 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
|
2010-07-07 07:11:01 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class CStringChecker : public Checker< eval::Call,
|
2011-02-24 09:05:30 +08:00
|
|
|
check::PreStmt<DeclStmt>,
|
|
|
|
check::LiveSymbols,
|
|
|
|
check::DeadSymbols,
|
|
|
|
check::RegionChanges
|
|
|
|
> {
|
|
|
|
mutable llvm::OwningPtr<BugType> BT_Null, BT_Bounds, BT_BoundsWrite,
|
2011-06-15 13:52:56 +08:00
|
|
|
BT_Overlap, BT_NotCString,
|
|
|
|
BT_AdditionOverflow;
|
2010-07-07 07:11:01 +08:00
|
|
|
public:
|
|
|
|
static void *getTag() { static int tag; return &tag; }
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
bool evalCall(const CallExpr *CE, CheckerContext &C) const;
|
|
|
|
void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
|
|
|
|
void checkLiveSymbols(const GRState *state, SymbolReaper &SR) const;
|
|
|
|
void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
|
|
|
|
bool wantsRegionChangeUpdate(const GRState *state) const;
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
const GRState *checkRegionChanges(const GRState *state,
|
2011-05-03 03:42:42 +08:00
|
|
|
const StoreManager::InvalidatedSymbols *,
|
2011-02-24 09:05:30 +08:00
|
|
|
const MemRegion * const *Begin,
|
|
|
|
const MemRegion * const *End) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
typedef void (CStringChecker::*FnCheck)(CheckerContext &,
|
|
|
|
const CallExpr *) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-01 05:36:53 +08:00
|
|
|
void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-01 05:36:53 +08:00
|
|
|
void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
|
|
|
|
const GRState *state,
|
2010-07-09 07:57:29 +08:00
|
|
|
const Expr *Size, const Expr *Source, const Expr *Dest,
|
2011-04-01 05:36:53 +08:00
|
|
|
bool Restricted = false,
|
|
|
|
bool IsMempcpy = false) const;
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
|
2011-02-22 12:55:05 +08:00
|
|
|
void evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool IsStrnlen = false) const;
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
|
2011-02-22 12:58:34 +08:00
|
|
|
void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool returnEnd,
|
2011-04-09 23:12:58 +08:00
|
|
|
bool isBounded, bool isAppending) const;
|
|
|
|
|
|
|
|
void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
|
|
|
|
void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-26 06:21:00 +08:00
|
|
|
void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-28 23:09:11 +08:00
|
|
|
void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-05-03 03:05:49 +08:00
|
|
|
void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
|
2011-04-26 06:21:00 +08:00
|
|
|
void evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
|
2011-04-28 23:09:11 +08:00
|
|
|
bool isBounded = false, bool ignoreCase = false) const;
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Utility methods
|
2010-07-09 07:57:29 +08:00
|
|
|
std::pair<const GRState*, const GRState*>
|
2011-02-24 09:05:30 +08:00
|
|
|
static assumeZero(CheckerContext &C,
|
|
|
|
const GRState *state, SVal V, QualType Ty);
|
|
|
|
|
|
|
|
static const GRState *setCStringLength(const GRState *state,
|
|
|
|
const MemRegion *MR, SVal strLength);
|
|
|
|
static SVal getCStringLengthForRegion(CheckerContext &C,
|
|
|
|
const GRState *&state,
|
2011-06-15 13:52:56 +08:00
|
|
|
const Expr *Ex, const MemRegion *MR,
|
|
|
|
bool hypothetical);
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal getCStringLength(CheckerContext &C, const GRState *&state,
|
2011-06-15 13:52:56 +08:00
|
|
|
const Expr *Ex, SVal Buf,
|
|
|
|
bool hypothetical = false) const;
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
const StringLiteral *getCStringLiteral(CheckerContext &C,
|
|
|
|
const GRState *&state,
|
|
|
|
const Expr *expr,
|
|
|
|
SVal val) const;
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
static const GRState *InvalidateBuffer(CheckerContext &C,
|
|
|
|
const GRState *state,
|
|
|
|
const Expr *Ex, SVal V);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
static bool SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
|
|
|
|
const MemRegion *MR);
|
2010-07-27 09:37:31 +08:00
|
|
|
|
|
|
|
// Re-usable checks
|
2010-12-02 15:49:45 +08:00
|
|
|
const GRState *checkNonNull(CheckerContext &C, const GRState *state,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Expr *S, SVal l) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
const GRState *CheckLocation(CheckerContext &C, const GRState *state,
|
2010-08-16 15:51:42 +08:00
|
|
|
const Expr *S, SVal l,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool IsDestination = false) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
const GRState *CheckBufferAccess(CheckerContext &C, const GRState *state,
|
|
|
|
const Expr *Size,
|
|
|
|
const Expr *FirstBuf,
|
2010-08-16 15:51:42 +08:00
|
|
|
const Expr *SecondBuf = NULL,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool FirstIsDestination = false) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
const GRState *CheckOverlap(CheckerContext &C, const GRState *state,
|
2010-07-09 07:57:29 +08:00
|
|
|
const Expr *Size, const Expr *First,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Expr *Second) const;
|
2010-12-02 15:49:45 +08:00
|
|
|
void emitOverlapBug(CheckerContext &C, const GRState *state,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Stmt *First, const Stmt *Second) const;
|
2011-06-15 13:52:56 +08:00
|
|
|
const GRState *checkAdditionOverflow(CheckerContext &C, const GRState *state,
|
|
|
|
NonLoc left, NonLoc right) const;
|
2010-07-07 07:11:01 +08:00
|
|
|
};
|
2010-08-15 05:02:52 +08:00
|
|
|
|
|
|
|
class CStringLength {
|
|
|
|
public:
|
|
|
|
typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
|
|
|
|
};
|
2010-07-07 07:11:01 +08:00
|
|
|
} //end anonymous namespace
|
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
namespace clang {
|
2010-12-23 15:20:52 +08:00
|
|
|
namespace ento {
|
2010-08-15 05:02:52 +08:00
|
|
|
template <>
|
|
|
|
struct GRStateTrait<CStringLength>
|
|
|
|
: public GRStatePartialTrait<CStringLength::EntryMap> {
|
|
|
|
static void *GDMIndex() { return CStringChecker::getTag(); }
|
|
|
|
};
|
|
|
|
}
|
2010-12-23 02:53:20 +08:00
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Individual checks and utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
std::pair<const GRState*, const GRState*>
|
2010-12-02 06:16:56 +08:00
|
|
|
CStringChecker::assumeZero(CheckerContext &C, const GRState *state, SVal V,
|
2010-07-09 07:57:29 +08:00
|
|
|
QualType Ty) {
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
|
|
|
|
if (!val)
|
2010-07-09 07:57:29 +08:00
|
|
|
return std::pair<const GRState*, const GRState *>(state, state);
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
|
|
|
|
return state->assume(svalBuilder.evalEQ(state, *val, zero));
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
const GRState *CStringChecker::checkNonNull(CheckerContext &C,
|
2010-07-09 07:57:29 +08:00
|
|
|
const GRState *state,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Expr *S, SVal l) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
const GRState *stateNull, *stateNonNull;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
|
2010-07-07 15:48:06 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
if (stateNull && !stateNonNull) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(stateNull);
|
2010-07-07 15:48:06 +08:00
|
|
|
if (!N)
|
|
|
|
return NULL;
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
if (!BT_Null)
|
2011-02-24 09:05:30 +08:00
|
|
|
BT_Null.reset(new BuiltinBug("API",
|
|
|
|
"Null pointer argument in call to byte string function"));
|
2010-07-07 15:48:06 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
2011-02-24 09:05:30 +08:00
|
|
|
BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get());
|
2010-07-07 15:48:06 +08:00
|
|
|
EnhancedBugReport *report = new EnhancedBugReport(*BT,
|
|
|
|
BT->getDescription(), N);
|
|
|
|
|
|
|
|
report->addRange(S->getSourceRange());
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, S);
|
|
|
|
C.EmitReport(report);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From here on, assume that the value is non-null.
|
2010-07-09 07:57:29 +08:00
|
|
|
assert(stateNonNull);
|
|
|
|
return stateNonNull;
|
2010-07-07 15:48:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
|
|
|
|
const GRState *CStringChecker::CheckLocation(CheckerContext &C,
|
|
|
|
const GRState *state,
|
2010-08-16 15:51:42 +08:00
|
|
|
const Expr *S, SVal l,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool IsDestination) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Check for out of bound array element access.
|
|
|
|
const MemRegion *R = l.getAsRegion();
|
|
|
|
if (!R)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
const ElementRegion *ER = dyn_cast<ElementRegion>(R);
|
|
|
|
if (!ER)
|
|
|
|
return state;
|
|
|
|
|
2010-08-11 14:10:55 +08:00
|
|
|
assert(ER->getValueType() == C.getASTContext().CharTy &&
|
2010-07-07 07:11:01 +08:00
|
|
|
"CheckLocation should only be called with char* ElementRegions");
|
|
|
|
|
|
|
|
// Get the size of the array.
|
2010-12-02 15:49:45 +08:00
|
|
|
const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2011-06-16 13:51:02 +08:00
|
|
|
SVal Extent =
|
|
|
|
svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
|
2010-07-07 07:11:01 +08:00
|
|
|
DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
|
|
|
|
|
|
|
|
// Get the index of the accessed element.
|
2010-09-09 18:51:37 +08:00
|
|
|
DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
const GRState *StInBound = state->assumeInBound(Idx, Size, true);
|
|
|
|
const GRState *StOutBound = state->assumeInBound(Idx, Size, false);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (StOutBound && !StInBound) {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(StOutBound);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!N)
|
|
|
|
return NULL;
|
|
|
|
|
2010-08-16 15:51:42 +08:00
|
|
|
BuiltinBug *BT;
|
|
|
|
if (IsDestination) {
|
|
|
|
if (!BT_BoundsWrite) {
|
2011-02-24 09:05:30 +08:00
|
|
|
BT_BoundsWrite.reset(new BuiltinBug("Out-of-bound array access",
|
|
|
|
"Byte string function overflows destination buffer"));
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
2011-02-24 09:05:30 +08:00
|
|
|
BT = static_cast<BuiltinBug*>(BT_BoundsWrite.get());
|
2010-08-16 15:51:42 +08:00
|
|
|
} else {
|
|
|
|
if (!BT_Bounds) {
|
2011-02-24 09:05:30 +08:00
|
|
|
BT_Bounds.reset(new BuiltinBug("Out-of-bound array access",
|
|
|
|
"Byte string function accesses out-of-bound array element"));
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
2011-02-24 09:05:30 +08:00
|
|
|
BT = static_cast<BuiltinBug*>(BT_Bounds.get());
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// FIXME: It would be nice to eventually make this diagnostic more clear,
|
|
|
|
// e.g., by referencing the original declaration or by saying *why* this
|
|
|
|
// reference is outside the range.
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
RangedBugReport *report = new RangedBugReport(*BT, BT->getDescription(), N);
|
|
|
|
|
|
|
|
report->addRange(S->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Array bound check succeeded. From this point forward the array bound
|
|
|
|
// should always succeed.
|
|
|
|
return StInBound;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GRState *CStringChecker::CheckBufferAccess(CheckerContext &C,
|
|
|
|
const GRState *state,
|
|
|
|
const Expr *Size,
|
|
|
|
const Expr *FirstBuf,
|
2010-08-16 15:51:42 +08:00
|
|
|
const Expr *SecondBuf,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool FirstIsDestination) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2011-06-16 13:51:02 +08:00
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
QualType sizeTy = Size->getType();
|
2010-07-07 07:11:01 +08:00
|
|
|
QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
|
|
|
|
|
2010-07-07 15:48:06 +08:00
|
|
|
// Check that the first buffer is non-null.
|
|
|
|
SVal BufVal = state->getSVal(FirstBuf);
|
2010-12-02 15:49:45 +08:00
|
|
|
state = checkNonNull(C, state, FirstBuf, BufVal);
|
2010-07-07 15:48:06 +08:00
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// Get the access length and make sure it is known.
|
|
|
|
SVal LengthVal = state->getSVal(Size);
|
|
|
|
NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
|
|
|
|
if (!Length)
|
|
|
|
return state;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
// Compute the offset of the last element to be accessed: size-1.
|
2010-12-02 15:49:45 +08:00
|
|
|
NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
|
|
|
|
NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
|
|
|
|
*Length, One, sizeTy));
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-04-15 13:22:18 +08:00
|
|
|
// Check that the first buffer is sufficiently long.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
|
2010-08-06 07:11:30 +08:00
|
|
|
if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
|
|
|
|
LastOffset, PtrTy);
|
2010-08-16 15:51:42 +08:00
|
|
|
state = CheckLocation(C, state, FirstBuf, BufEnd, FirstIsDestination);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2010-08-06 07:11:30 +08:00
|
|
|
// If the buffer isn't large enough, abort.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// If there's a second buffer, check it as well.
|
|
|
|
if (SecondBuf) {
|
|
|
|
BufVal = state->getSVal(SecondBuf);
|
2010-12-02 15:49:45 +08:00
|
|
|
state = checkNonNull(C, state, SecondBuf, BufVal);
|
2010-07-07 15:48:06 +08:00
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
|
2010-08-06 07:11:30 +08:00
|
|
|
if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
|
|
|
|
LastOffset, PtrTy);
|
2010-08-06 07:11:30 +08:00
|
|
|
state = CheckLocation(C, state, SecondBuf, BufEnd);
|
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Large enough or not, return this state!
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GRState *CStringChecker::CheckOverlap(CheckerContext &C,
|
|
|
|
const GRState *state,
|
2010-07-09 07:57:29 +08:00
|
|
|
const Expr *Size,
|
2010-07-07 07:11:01 +08:00
|
|
|
const Expr *First,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Expr *Second) const {
|
2010-07-07 07:11:01 +08:00
|
|
|
// Do a simple check for overlap: if the two arguments are from the same
|
|
|
|
// buffer, see if the end of the first is greater than the start of the second
|
|
|
|
// or vice versa.
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
2010-07-07 07:11:01 +08:00
|
|
|
const GRState *stateTrue, *stateFalse;
|
|
|
|
|
|
|
|
// Get the buffer values and make sure they're known locations.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal firstVal = state->getSVal(First);
|
|
|
|
SVal secondVal = state->getSVal(Second);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
Loc *firstLoc = dyn_cast<Loc>(&firstVal);
|
|
|
|
if (!firstLoc)
|
2010-07-07 07:11:01 +08:00
|
|
|
return state;
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
Loc *secondLoc = dyn_cast<Loc>(&secondVal);
|
|
|
|
if (!secondLoc)
|
2010-07-07 07:11:01 +08:00
|
|
|
return state;
|
|
|
|
|
|
|
|
// Are the two values the same?
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
llvm::tie(stateTrue, stateFalse) =
|
|
|
|
state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
if (stateTrue && !stateFalse) {
|
|
|
|
// If the values are known to be equal, that's automatically an overlap.
|
2010-12-02 15:49:45 +08:00
|
|
|
emitOverlapBug(C, stateTrue, First, Second);
|
2010-07-07 07:11:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
// assume the two expressions are not equal.
|
2010-07-07 07:11:01 +08:00
|
|
|
assert(stateFalse);
|
|
|
|
state = stateFalse;
|
|
|
|
|
|
|
|
// Which value comes first?
|
2011-06-16 13:51:02 +08:00
|
|
|
QualType cmpTy = svalBuilder.getComparisonType();
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
|
|
|
|
*firstLoc, *secondLoc, cmpTy);
|
|
|
|
DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
|
|
|
|
if (!reverseTest)
|
2010-07-07 07:11:01 +08:00
|
|
|
return state;
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (stateTrue) {
|
|
|
|
if (stateFalse) {
|
|
|
|
// If we don't know which one comes first, we can't perform this test.
|
|
|
|
return state;
|
|
|
|
} else {
|
2010-12-02 15:49:45 +08:00
|
|
|
// Switch the values so that firstVal is before secondVal.
|
|
|
|
Loc *tmpLoc = firstLoc;
|
|
|
|
firstLoc = secondLoc;
|
|
|
|
secondLoc = tmpLoc;
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// Switch the Exprs as well, so that they still correspond.
|
|
|
|
const Expr *tmpExpr = First;
|
|
|
|
First = Second;
|
|
|
|
Second = tmpExpr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the length, and make sure it too is known.
|
|
|
|
SVal LengthVal = state->getSVal(Size);
|
|
|
|
NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
|
|
|
|
if (!Length)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// Convert the first buffer's start address to char*.
|
|
|
|
// Bail out if the cast fails.
|
|
|
|
QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
|
2011-06-16 13:51:02 +08:00
|
|
|
SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy,
|
|
|
|
First->getType());
|
2010-07-07 07:11:01 +08:00
|
|
|
Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
|
|
|
|
if (!FirstStartLoc)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// Compute the end of the first buffer. Bail out if THAT fails.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
|
2010-07-07 07:11:01 +08:00
|
|
|
*FirstStartLoc, *Length, CharPtrTy);
|
|
|
|
Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
|
|
|
|
if (!FirstEndLoc)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// Is the end of the first buffer past the start of the second buffer?
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
|
|
|
|
*FirstEndLoc, *secondLoc, cmpTy);
|
2010-07-07 07:11:01 +08:00
|
|
|
DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
|
|
|
|
if (!OverlapTest)
|
|
|
|
return state;
|
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
if (stateTrue && !stateFalse) {
|
|
|
|
// Overlap!
|
2010-12-02 15:49:45 +08:00
|
|
|
emitOverlapBug(C, stateTrue, First, Second);
|
2010-07-07 07:11:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-02 06:16:56 +08:00
|
|
|
// assume the two expressions don't overlap.
|
2010-07-07 07:11:01 +08:00
|
|
|
assert(stateFalse);
|
|
|
|
return stateFalse;
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
void CStringChecker::emitOverlapBug(CheckerContext &C, const GRState *state,
|
2011-02-24 09:05:30 +08:00
|
|
|
const Stmt *First, const Stmt *Second) const {
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink(state);
|
2010-07-07 07:11:01 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT_Overlap)
|
2011-02-24 09:05:30 +08:00
|
|
|
BT_Overlap.reset(new BugType("Unix API", "Improper arguments"));
|
2010-07-07 07:11:01 +08:00
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
RangedBugReport *report =
|
|
|
|
new RangedBugReport(*BT_Overlap,
|
|
|
|
"Arguments must not be overlapping buffers", N);
|
|
|
|
report->addRange(First->getSourceRange());
|
|
|
|
report->addRange(Second->getSourceRange());
|
|
|
|
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
const GRState *CStringChecker::checkAdditionOverflow(CheckerContext &C,
|
|
|
|
const GRState *state,
|
|
|
|
NonLoc left,
|
|
|
|
NonLoc right) const {
|
|
|
|
// If a previous check has failed, propagate the failure.
|
|
|
|
if (!state)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
|
|
|
|
|
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
|
|
|
const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
|
|
|
|
NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
|
|
|
|
|
|
|
|
SVal maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
|
|
|
|
sizeTy);
|
|
|
|
|
|
|
|
if (maxMinusRight.isUnknownOrUndef()) {
|
|
|
|
// Try switching the operands. (The order of these two assignments is
|
|
|
|
// important!)
|
|
|
|
maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
|
|
|
|
sizeTy);
|
|
|
|
left = right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NonLoc *maxMinusRightNL = dyn_cast<NonLoc>(&maxMinusRight)) {
|
|
|
|
QualType cmpTy = svalBuilder.getConditionType();
|
|
|
|
// If left > max - right, we have an overflow.
|
|
|
|
SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
|
|
|
|
*maxMinusRightNL, cmpTy);
|
|
|
|
|
|
|
|
const GRState *stateOverflow, *stateOkay;
|
|
|
|
llvm::tie(stateOverflow, stateOkay) =
|
|
|
|
state->assume(cast<DefinedOrUnknownSVal>(willOverflow));
|
|
|
|
|
|
|
|
if (stateOverflow && !stateOkay) {
|
|
|
|
// We have an overflow. Emit a bug report.
|
|
|
|
ExplodedNode *N = C.generateSink(stateOverflow);
|
|
|
|
if (!N)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!BT_AdditionOverflow)
|
|
|
|
BT_AdditionOverflow.reset(new BuiltinBug("API",
|
|
|
|
"Sum of expressions causes overflow"));
|
|
|
|
|
|
|
|
llvm::SmallString<120> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
// This isn't a great error message, but this should never occur in real
|
|
|
|
// code anyway -- you'd have to create a buffer longer than a size_t can
|
|
|
|
// represent, which is sort of a contradiction.
|
|
|
|
os << "This expression will create a string whose length is too big to "
|
|
|
|
<< "be represented as a size_t";
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
BugReport *report = new BugReport(*BT_AdditionOverflow, os.str(), N);
|
|
|
|
C.EmitReport(report);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From now on, assume an overflow didn't occur.
|
|
|
|
assert(stateOkay);
|
|
|
|
state = stateOkay;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
const GRState *CStringChecker::setCStringLength(const GRState *state,
|
2010-08-16 15:51:42 +08:00
|
|
|
const MemRegion *MR,
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal strLength) {
|
|
|
|
assert(!strLength.isUndef() && "Attempt to set an undefined string length");
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
MR = MR->StripCasts();
|
|
|
|
|
|
|
|
switch (MR->getKind()) {
|
|
|
|
case MemRegion::StringRegionKind:
|
|
|
|
// FIXME: This can happen if we strcpy() into a string region. This is
|
|
|
|
// undefined [C99 6.4.5p6], but we should still warn about it.
|
|
|
|
return state;
|
|
|
|
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
|
|
|
case MemRegion::VarRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-06-15 13:14:03 +08:00
|
|
|
// These are the types we can currently track string lengths for.
|
|
|
|
break;
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
// FIXME: Handle element regions by upper-bounding the parent region's
|
|
|
|
// string length.
|
|
|
|
return state;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Other regions (mostly non-data) can't have a reliable C string length.
|
|
|
|
// For now, just ignore the change.
|
|
|
|
// FIXME: These are rare but not impossible. We should output some kind of
|
|
|
|
// warning for things like strcpy((char[]){'a', 0}, "b");
|
|
|
|
return state;
|
|
|
|
}
|
2011-06-15 13:14:03 +08:00
|
|
|
|
|
|
|
if (strLength.isUnknown())
|
|
|
|
return state->remove<CStringLength>(MR);
|
|
|
|
|
|
|
|
return state->set<CStringLength>(MR, strLength);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
|
2010-08-15 05:02:52 +08:00
|
|
|
const GRState *&state,
|
|
|
|
const Expr *Ex,
|
2011-06-15 13:52:56 +08:00
|
|
|
const MemRegion *MR,
|
|
|
|
bool hypothetical) {
|
|
|
|
if (!hypothetical) {
|
|
|
|
// If there's a recorded length, go ahead and return it.
|
|
|
|
const SVal *Recorded = state->get<CStringLength>(MR);
|
|
|
|
if (Recorded)
|
|
|
|
return *Recorded;
|
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
|
|
|
|
// Otherwise, get a new symbol and update the state.
|
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
2011-02-24 09:05:30 +08:00
|
|
|
SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
|
|
|
|
MR, Ex, sizeTy, Count);
|
2011-06-15 13:52:56 +08:00
|
|
|
|
|
|
|
if (!hypothetical)
|
|
|
|
state = state->set<CStringLength>(MR, strLength);
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
return strLength;
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal CStringChecker::getCStringLength(CheckerContext &C, const GRState *&state,
|
2011-06-15 13:52:56 +08:00
|
|
|
const Expr *Ex, SVal Buf,
|
|
|
|
bool hypothetical) const {
|
2010-07-27 09:37:31 +08:00
|
|
|
const MemRegion *MR = Buf.getAsRegion();
|
|
|
|
if (!MR) {
|
|
|
|
// If we can't get a region, see if it's something we /know/ isn't a
|
|
|
|
// C string. In the context of locations, the only time we can issue such
|
|
|
|
// a warning is for labels.
|
|
|
|
if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateNode(state)) {
|
2010-07-27 09:37:31 +08:00
|
|
|
if (!BT_NotCString)
|
2011-02-24 09:05:30 +08:00
|
|
|
BT_NotCString.reset(new BuiltinBug("API",
|
|
|
|
"Argument is not a null-terminated string."));
|
2010-07-27 09:37:31 +08:00
|
|
|
|
|
|
|
llvm::SmallString<120> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
os << "Argument to byte string function is the address of the label '"
|
2011-02-17 13:38:27 +08:00
|
|
|
<< Label->getLabel()->getName()
|
2010-07-27 09:37:31 +08:00
|
|
|
<< "', which is not a null-terminated string";
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
|
|
|
|
os.str(), N);
|
|
|
|
|
|
|
|
report->addRange(Ex->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
|
|
|
|
return UndefinedVal();
|
|
|
|
}
|
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
// If it's not a region and not a label, give up.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
// If we have a region, strip casts from it and see if we can figure out
|
|
|
|
// its length. For anything we can't figure out, just return UnknownVal.
|
|
|
|
MR = MR->StripCasts();
|
|
|
|
|
|
|
|
switch (MR->getKind()) {
|
|
|
|
case MemRegion::StringRegionKind: {
|
|
|
|
// Modifying the contents of string regions is undefined [C99 6.4.5p6],
|
|
|
|
// so we can assume that the byte length is the correct C string length.
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
|
|
|
const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
|
|
|
|
return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
case MemRegion::SymbolicRegionKind:
|
|
|
|
case MemRegion::AllocaRegionKind:
|
|
|
|
case MemRegion::VarRegionKind:
|
|
|
|
case MemRegion::FieldRegionKind:
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2011-06-15 13:52:56 +08:00
|
|
|
return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
|
2010-08-15 05:02:52 +08:00
|
|
|
case MemRegion::CompoundLiteralRegionKind:
|
|
|
|
// FIXME: Can we track this? Is it necessary?
|
|
|
|
return UnknownVal();
|
|
|
|
case MemRegion::ElementRegionKind:
|
|
|
|
// FIXME: How can we handle this? It's not good enough to subtract the
|
|
|
|
// offset from the base string length; consider "123\x00567" and &a[5].
|
|
|
|
return UnknownVal();
|
|
|
|
default:
|
|
|
|
// Other regions (mostly non-data) can't have a reliable C string length.
|
|
|
|
// In this case, an error is emitted and UndefinedVal is returned.
|
|
|
|
// The caller should always be prepared to handle this case.
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateNode(state)) {
|
2010-08-15 05:02:52 +08:00
|
|
|
if (!BT_NotCString)
|
2011-02-24 09:05:30 +08:00
|
|
|
BT_NotCString.reset(new BuiltinBug("API",
|
|
|
|
"Argument is not a null-terminated string."));
|
2010-08-15 05:02:52 +08:00
|
|
|
|
|
|
|
llvm::SmallString<120> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
|
|
|
os << "Argument to byte string function is ";
|
|
|
|
|
|
|
|
if (SummarizeRegion(os, C.getASTContext(), MR))
|
|
|
|
os << ", which is not a null-terminated string";
|
|
|
|
else
|
|
|
|
os << "not a null-terminated string";
|
|
|
|
|
|
|
|
// Generate a report for this bug.
|
|
|
|
EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
|
|
|
|
os.str(), N);
|
|
|
|
|
|
|
|
report->addRange(Ex->getSourceRange());
|
|
|
|
C.EmitReport(report);
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
|
|
|
|
2010-08-15 05:02:52 +08:00
|
|
|
return UndefinedVal();
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
|
|
|
|
const GRState *&state, const Expr *expr, SVal val) const {
|
|
|
|
|
|
|
|
// Get the memory region pointed to by the val.
|
|
|
|
const MemRegion *bufRegion = val.getAsRegion();
|
|
|
|
if (!bufRegion)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Strip casts off the memory region.
|
|
|
|
bufRegion = bufRegion->StripCasts();
|
|
|
|
|
|
|
|
// Cast the memory region to a string region.
|
|
|
|
const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
|
|
|
|
if (!strRegion)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Return the actual string in the string region.
|
|
|
|
return strRegion->getStringLiteral();
|
|
|
|
}
|
|
|
|
|
2010-08-16 15:51:42 +08:00
|
|
|
const GRState *CStringChecker::InvalidateBuffer(CheckerContext &C,
|
|
|
|
const GRState *state,
|
|
|
|
const Expr *E, SVal V) {
|
|
|
|
Loc *L = dyn_cast<Loc>(&V);
|
|
|
|
if (!L)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
// FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
|
|
|
|
// some assumptions about the value that CFRefCount can't. Even so, it should
|
|
|
|
// probably be refactored.
|
|
|
|
if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
|
|
|
|
const MemRegion *R = MR->getRegion()->StripCasts();
|
|
|
|
|
|
|
|
// Are we dealing with an ElementRegion? If so, we should be invalidating
|
|
|
|
// the super-region.
|
|
|
|
if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
|
|
|
|
R = ER->getSuperRegion();
|
|
|
|
// FIXME: What about layers of ElementRegions?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate this region.
|
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
2011-02-12 03:48:15 +08:00
|
|
|
return state->invalidateRegion(R, E, Count, NULL);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a non-region value by chance, just remove the binding.
|
|
|
|
// FIXME: is this necessary or correct? This handles the non-Region
|
|
|
|
// cases. Is it ever valid to store to these?
|
|
|
|
return state->unbindLoc(*L);
|
|
|
|
}
|
|
|
|
|
2010-07-27 09:37:31 +08:00
|
|
|
bool CStringChecker::SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
|
|
|
|
const MemRegion *MR) {
|
|
|
|
const TypedRegion *TR = dyn_cast<TypedRegion>(MR);
|
|
|
|
if (!TR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (TR->getKind()) {
|
|
|
|
case MemRegion::FunctionTextRegionKind: {
|
|
|
|
const FunctionDecl *FD = cast<FunctionTextRegion>(TR)->getDecl();
|
|
|
|
if (FD)
|
|
|
|
os << "the address of the function '" << FD << "'";
|
|
|
|
else
|
|
|
|
os << "the address of a function";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case MemRegion::BlockTextRegionKind:
|
|
|
|
os << "block text";
|
|
|
|
return true;
|
|
|
|
case MemRegion::BlockDataRegionKind:
|
|
|
|
os << "a block";
|
|
|
|
return true;
|
|
|
|
case MemRegion::CXXThisRegionKind:
|
2010-11-26 16:52:48 +08:00
|
|
|
case MemRegion::CXXTempObjectRegionKind:
|
|
|
|
os << "a C++ temp object of type " << TR->getValueType().getAsString();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
|
|
|
case MemRegion::VarRegionKind:
|
2010-08-11 14:10:55 +08:00
|
|
|
os << "a variable of type" << TR->getValueType().getAsString();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
|
|
|
case MemRegion::FieldRegionKind:
|
2010-08-11 14:10:55 +08:00
|
|
|
os << "a field of type " << TR->getValueType().getAsString();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
|
|
|
case MemRegion::ObjCIvarRegionKind:
|
2010-08-11 14:10:55 +08:00
|
|
|
os << "an instance variable of type " << TR->getValueType().getAsString();
|
2010-07-27 09:37:31 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-12-02 05:57:22 +08:00
|
|
|
// evaluation of individual function calls.
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-04-01 05:36:53 +08:00
|
|
|
void CStringChecker::evalCopyCommon(CheckerContext &C,
|
|
|
|
const CallExpr *CE,
|
|
|
|
const GRState *state,
|
2010-07-09 07:57:29 +08:00
|
|
|
const Expr *Size, const Expr *Dest,
|
2011-04-01 05:36:53 +08:00
|
|
|
const Expr *Source, bool Restricted,
|
|
|
|
bool IsMempcpy) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// See if the size argument is zero.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal sizeVal = state->getSVal(Size);
|
|
|
|
QualType sizeTy = Size->getType();
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
const GRState *stateZeroSize, *stateNonZeroSize;
|
2011-06-16 13:51:02 +08:00
|
|
|
llvm::tie(stateZeroSize, stateNonZeroSize) =
|
|
|
|
assumeZero(C, state, sizeVal, sizeTy);
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2011-04-01 05:36:53 +08:00
|
|
|
// Get the value of the Dest.
|
|
|
|
SVal destVal = state->getSVal(Dest);
|
|
|
|
|
|
|
|
// If the size is zero, there won't be any actual memory access, so
|
|
|
|
// just bind the return value to the destination buffer and return.
|
|
|
|
if (stateZeroSize) {
|
2011-06-04 08:04:22 +08:00
|
|
|
stateZeroSize = stateZeroSize->BindExpr(CE, destVal);
|
2010-12-02 15:49:45 +08:00
|
|
|
C.addTransition(stateZeroSize);
|
2011-04-01 05:36:53 +08:00
|
|
|
}
|
2010-07-09 07:57:29 +08:00
|
|
|
|
|
|
|
// If the size can be nonzero, we have to check the other arguments.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (stateNonZeroSize) {
|
2011-06-04 08:04:22 +08:00
|
|
|
state = stateNonZeroSize;
|
2011-04-01 05:36:53 +08:00
|
|
|
|
|
|
|
// Ensure the destination is not null. If it is NULL there will be a
|
|
|
|
// NULL pointer dereference.
|
|
|
|
state = checkNonNull(C, state, Dest, destVal);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the value of the Src.
|
|
|
|
SVal srcVal = state->getSVal(Source);
|
|
|
|
|
|
|
|
// Ensure the source is not null. If it is NULL there will be a
|
|
|
|
// NULL pointer dereference.
|
|
|
|
state = checkNonNull(C, state, Source, srcVal);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
2011-06-04 09:50:25 +08:00
|
|
|
// Ensure the accesses are valid and that the buffers do not overlap.
|
2010-08-16 15:51:42 +08:00
|
|
|
state = CheckBufferAccess(C, state, Size, Dest, Source,
|
|
|
|
/* FirstIsDst = */ true);
|
2010-07-09 07:57:29 +08:00
|
|
|
if (Restricted)
|
|
|
|
state = CheckOverlap(C, state, Size, Dest, Source);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-06-04 09:50:25 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
2011-06-04 09:47:27 +08:00
|
|
|
|
2011-06-04 09:50:25 +08:00
|
|
|
// If this is mempcpy, get the byte after the last byte copied and
|
|
|
|
// bind the expr.
|
|
|
|
if (IsMempcpy) {
|
|
|
|
loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal);
|
|
|
|
assert(destRegVal && "Destination should be a known MemRegionVal here");
|
|
|
|
|
|
|
|
// Get the length to copy.
|
|
|
|
NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&sizeVal);
|
|
|
|
|
|
|
|
if (lenValNonLoc) {
|
|
|
|
// Get the byte after the last byte copied.
|
|
|
|
SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add,
|
|
|
|
*destRegVal,
|
|
|
|
*lenValNonLoc,
|
|
|
|
Dest->getType());
|
|
|
|
|
|
|
|
// The byte after the last byte copied is the return value.
|
|
|
|
state = state->BindExpr(CE, lastElement);
|
2011-06-04 09:47:27 +08:00
|
|
|
} else {
|
2011-06-04 09:50:25 +08:00
|
|
|
// If we don't know how much we copied, we can at least
|
|
|
|
// conjure a return value for later.
|
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
|
|
|
SVal result =
|
|
|
|
C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
|
|
|
|
state = state->BindExpr(CE, result);
|
2011-04-01 05:36:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-04 09:50:25 +08:00
|
|
|
} else {
|
|
|
|
// All other copies return the destination buffer.
|
|
|
|
// (Well, bcopy() has a void return type, but this won't hurt.)
|
|
|
|
state = state->BindExpr(CE, destVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
2011-06-04 09:50:25 +08:00
|
|
|
|
|
|
|
// Invalidate the destination.
|
|
|
|
// FIXME: Even if we can't perfectly model the copy, we should see if we
|
|
|
|
// can use LazyCompoundVals to copy the source values into the destination.
|
|
|
|
// This would probably remove any existing bindings past the end of the
|
|
|
|
// copied region, but that's still an improvement over blank invalidation.
|
|
|
|
state = InvalidateBuffer(C, state, Dest, state->getSVal(Dest));
|
|
|
|
C.addTransition(state);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// void *memcpy(void *restrict dst, const void *restrict src, size_t n);
|
|
|
|
// The return value is the address of the destination buffer.
|
|
|
|
const Expr *Dest = CE->getArg(0);
|
2010-07-07 07:11:01 +08:00
|
|
|
const GRState *state = C.getState();
|
2011-06-04 09:47:27 +08:00
|
|
|
|
2011-04-01 05:36:53 +08:00
|
|
|
evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
// void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
|
|
|
|
// The return value is a pointer to the byte following the last written byte.
|
|
|
|
const Expr *Dest = CE->getArg(0);
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
|
|
|
|
evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// void *memmove(void *dst, const void *src, size_t n);
|
2010-07-07 07:11:01 +08:00
|
|
|
// The return value is the address of the destination buffer.
|
2010-07-09 07:57:29 +08:00
|
|
|
const Expr *Dest = CE->getArg(0);
|
|
|
|
const GRState *state = C.getState();
|
2011-06-04 09:47:27 +08:00
|
|
|
|
2011-04-01 05:36:53 +08:00
|
|
|
evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
|
2010-07-07 07:11:01 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-09 07:57:29 +08:00
|
|
|
// void bcopy(const void *src, void *dst, size_t n);
|
2011-04-01 05:36:53 +08:00
|
|
|
evalCopyCommon(C, CE, C.getState(),
|
|
|
|
CE->getArg(2), CE->getArg(1), CE->getArg(0));
|
2010-07-09 07:57:29 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
|
2010-07-07 16:15:01 +08:00
|
|
|
// int memcmp(const void *s1, const void *s2, size_t n);
|
|
|
|
const Expr *Left = CE->getArg(0);
|
|
|
|
const Expr *Right = CE->getArg(1);
|
|
|
|
const Expr *Size = CE->getArg(2);
|
|
|
|
|
|
|
|
const GRState *state = C.getState();
|
2010-12-02 15:49:45 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// See if the size argument is zero.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal sizeVal = state->getSVal(Size);
|
|
|
|
QualType sizeTy = Size->getType();
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
const GRState *stateZeroSize, *stateNonZeroSize;
|
|
|
|
llvm::tie(stateZeroSize, stateNonZeroSize) =
|
|
|
|
assumeZero(C, state, sizeVal, sizeTy);
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the size can be zero, the result will be 0 in that case, and we don't
|
|
|
|
// have to check either of the buffers.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (stateZeroSize) {
|
|
|
|
state = stateZeroSize;
|
|
|
|
state = state->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
|
2010-07-09 07:57:29 +08:00
|
|
|
C.addTransition(state);
|
2010-07-07 16:15:01 +08:00
|
|
|
}
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the size can be nonzero, we have to check the other arguments.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (stateNonZeroSize) {
|
|
|
|
state = stateNonZeroSize;
|
2010-07-09 07:57:29 +08:00
|
|
|
// If we know the two buffers are the same, we know the result is 0.
|
|
|
|
// First, get the two buffers' addresses. Another checker will have already
|
|
|
|
// made sure they're not undefined.
|
|
|
|
DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(state->getSVal(Left));
|
|
|
|
DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(state->getSVal(Right));
|
|
|
|
|
|
|
|
// See if they are the same.
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
|
2010-07-09 07:57:29 +08:00
|
|
|
const GRState *StSameBuf, *StNotSameBuf;
|
2010-12-02 06:16:56 +08:00
|
|
|
llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
|
2010-07-09 07:57:29 +08:00
|
|
|
|
2011-06-16 13:51:02 +08:00
|
|
|
// If the two arguments might be the same buffer, we know the result is 0,
|
2010-07-09 07:57:29 +08:00
|
|
|
// and we only need to check one size.
|
|
|
|
if (StSameBuf) {
|
|
|
|
state = StSameBuf;
|
|
|
|
state = CheckBufferAccess(C, state, Size, Left);
|
|
|
|
if (state) {
|
2010-12-02 15:49:45 +08:00
|
|
|
state = StSameBuf->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
|
2010-07-09 07:57:29 +08:00
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
}
|
2010-07-07 16:15:01 +08:00
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the two arguments might be different buffers, we have to check the
|
|
|
|
// size of both of them.
|
|
|
|
if (StNotSameBuf) {
|
|
|
|
state = StNotSameBuf;
|
|
|
|
state = CheckBufferAccess(C, state, Size, Left, Right);
|
|
|
|
if (state) {
|
|
|
|
// The return value is the comparison result, which we don't know.
|
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
|
2010-07-09 07:57:29 +08:00
|
|
|
state = state->BindExpr(CE, CmpV);
|
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-07 16:15:01 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalstrLength(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2010-07-27 09:37:31 +08:00
|
|
|
// size_t strlen(const char *s);
|
2011-02-22 12:55:05 +08:00
|
|
|
evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalstrnLength(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
2011-02-22 12:55:05 +08:00
|
|
|
// size_t strnlen(const char *s, size_t maxlen);
|
|
|
|
evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
|
2011-02-24 09:05:30 +08:00
|
|
|
bool IsStrnlen) const {
|
2010-07-27 09:37:31 +08:00
|
|
|
const GRState *state = C.getState();
|
2011-06-14 09:15:31 +08:00
|
|
|
|
|
|
|
if (IsStrnlen) {
|
|
|
|
const Expr *maxlenExpr = CE->getArg(1);
|
|
|
|
SVal maxlenVal = state->getSVal(maxlenExpr);
|
|
|
|
|
|
|
|
const GRState *stateZeroSize, *stateNonZeroSize;
|
|
|
|
llvm::tie(stateZeroSize, stateNonZeroSize) =
|
|
|
|
assumeZero(C, state, maxlenVal, maxlenExpr->getType());
|
|
|
|
|
|
|
|
// If the size can be zero, the result will be 0 in that case, and we don't
|
|
|
|
// have to check the string itself.
|
|
|
|
if (stateZeroSize) {
|
|
|
|
SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
|
|
|
|
stateZeroSize = stateZeroSize->BindExpr(CE, zero);
|
|
|
|
C.addTransition(stateZeroSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the size is GUARANTEED to be zero, we're done!
|
|
|
|
if (!stateNonZeroSize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise, record the assumption that the size is nonzero.
|
|
|
|
state = stateNonZeroSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the string argument is non-null.
|
2010-07-27 09:37:31 +08:00
|
|
|
const Expr *Arg = CE->getArg(0);
|
|
|
|
SVal ArgVal = state->getSVal(Arg);
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
state = checkNonNull(C, state, Arg, ArgVal);
|
2010-07-27 09:37:31 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
SVal strLength = getCStringLength(C, state, Arg, ArgVal);
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
// If the argument isn't a valid C string, there's no valid state to
|
|
|
|
// transition to.
|
|
|
|
if (strLength.isUndef())
|
|
|
|
return;
|
2011-02-22 12:55:05 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
DefinedOrUnknownSVal result = UnknownVal();
|
2011-06-14 09:15:31 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
// If the check is for strnlen() then bind the return value to no more than
|
|
|
|
// the maxlen value.
|
|
|
|
if (IsStrnlen) {
|
2011-06-16 13:51:02 +08:00
|
|
|
QualType cmpTy = C.getSValBuilder().getComparisonType();
|
2011-06-14 09:15:31 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
// It's a little unfortunate to be getting this again,
|
|
|
|
// but it's not that expensive...
|
|
|
|
const Expr *maxlenExpr = CE->getArg(1);
|
|
|
|
SVal maxlenVal = state->getSVal(maxlenExpr);
|
|
|
|
|
|
|
|
NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
|
|
|
|
NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
|
|
|
|
|
|
|
|
if (strLengthNL && maxlenValNL) {
|
|
|
|
const GRState *stateStringTooLong, *stateStringNotTooLong;
|
|
|
|
|
|
|
|
// Check if the strLength is greater than the maxlen.
|
|
|
|
llvm::tie(stateStringTooLong, stateStringNotTooLong) =
|
|
|
|
state->assume(cast<DefinedOrUnknownSVal>
|
|
|
|
(C.getSValBuilder().evalBinOpNN(state, BO_GT,
|
|
|
|
*strLengthNL,
|
|
|
|
*maxlenValNL,
|
|
|
|
cmpTy)));
|
|
|
|
|
|
|
|
if (stateStringTooLong && !stateStringNotTooLong) {
|
|
|
|
// If the string is longer than maxlen, return maxlen.
|
|
|
|
result = *maxlenValNL;
|
|
|
|
} else if (stateStringNotTooLong && !stateStringTooLong) {
|
|
|
|
// If the string is shorter than maxlen, return its length.
|
|
|
|
result = *strLengthNL;
|
2011-06-14 09:15:31 +08:00
|
|
|
}
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-06-14 09:26:48 +08:00
|
|
|
if (result.isUnknown()) {
|
|
|
|
// If we don't have enough information for a comparison, there's
|
|
|
|
// no guarantee the full string length will actually be returned.
|
|
|
|
// All we know is the return value is the min of the string length
|
|
|
|
// and the limit. This is better than nothing.
|
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
|
|
|
result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
|
|
|
|
NonLoc *resultNL = cast<NonLoc>(&result);
|
|
|
|
|
|
|
|
if (strLengthNL) {
|
|
|
|
state = state->assume(cast<DefinedOrUnknownSVal>
|
|
|
|
(C.getSValBuilder().evalBinOpNN(state, BO_LE,
|
|
|
|
*resultNL,
|
|
|
|
*strLengthNL,
|
|
|
|
cmpTy)), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxlenValNL) {
|
|
|
|
state = state->assume(cast<DefinedOrUnknownSVal>
|
|
|
|
(C.getSValBuilder().evalBinOpNN(state, BO_LE,
|
|
|
|
*resultNL,
|
|
|
|
*maxlenValNL,
|
|
|
|
cmpTy)), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// This is a plain strlen(), not strnlen().
|
|
|
|
result = cast<DefinedOrUnknownSVal>(strLength);
|
|
|
|
|
|
|
|
// If we don't know the length of the string, conjure a return
|
|
|
|
// value, so it can be used in constraints, at least.
|
|
|
|
if (result.isUnknown()) {
|
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
|
|
|
result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
|
|
|
|
}
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
2011-06-14 09:26:48 +08:00
|
|
|
|
|
|
|
// Bind the return value.
|
|
|
|
assert(!result.isUnknown() && "Should have conjured a value by now");
|
|
|
|
state = state->BindExpr(CE, result);
|
|
|
|
C.addTransition(state);
|
2010-07-27 09:37:31 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
|
2010-08-16 15:51:42 +08:00
|
|
|
// char *strcpy(char *restrict dst, const char *restrict src);
|
2011-04-09 23:12:58 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
|
|
|
/* returnEnd = */ false,
|
|
|
|
/* isBounded = */ false,
|
|
|
|
/* isAppending = */ false);
|
2011-02-22 12:58:34 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
|
2011-06-04 08:05:23 +08:00
|
|
|
// char *strncpy(char *restrict dst, const char *restrict src, size_t n);
|
2011-04-09 23:12:58 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
|
|
|
/* returnEnd = */ false,
|
|
|
|
/* isBounded = */ true,
|
|
|
|
/* isAppending = */ false);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
|
2010-08-16 15:51:42 +08:00
|
|
|
// char *stpcpy(char *restrict dst, const char *restrict src);
|
2011-04-09 23:12:58 +08:00
|
|
|
evalStrcpyCommon(C, CE,
|
|
|
|
/* returnEnd = */ true,
|
|
|
|
/* isBounded = */ false,
|
|
|
|
/* isAppending = */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
//char *strcat(char *restrict s1, const char *restrict s2);
|
|
|
|
evalStrcpyCommon(C, CE,
|
|
|
|
/* returnEnd = */ false,
|
|
|
|
/* isBounded = */ false,
|
|
|
|
/* isAppending = */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
//char *strncat(char *restrict s1, const char *restrict s2, size_t n);
|
|
|
|
evalStrcpyCommon(C, CE,
|
|
|
|
/* returnEnd = */ false,
|
|
|
|
/* isBounded = */ true,
|
|
|
|
/* isAppending = */ true);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
|
2011-04-09 23:12:58 +08:00
|
|
|
bool returnEnd, bool isBounded,
|
|
|
|
bool isAppending) const {
|
2010-08-16 15:51:42 +08:00
|
|
|
const GRState *state = C.getState();
|
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
// Check that the destination is non-null.
|
2010-08-16 15:51:42 +08:00
|
|
|
const Expr *Dst = CE->getArg(0);
|
|
|
|
SVal DstVal = state->getSVal(Dst);
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
state = checkNonNull(C, state, Dst, DstVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check that the source is non-null.
|
2010-12-02 15:49:45 +08:00
|
|
|
const Expr *srcExpr = CE->getArg(1);
|
|
|
|
SVal srcVal = state->getSVal(srcExpr);
|
|
|
|
state = checkNonNull(C, state, srcExpr, srcVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string length of the source.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
// If the source isn't a valid C string, give up.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (strLength.isUndef())
|
2010-08-16 15:51:42 +08:00
|
|
|
return;
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType cmpTy = svalBuilder.getConditionType();
|
|
|
|
|
|
|
|
SVal amountCopied = UnknownVal();
|
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
// If the function is strncpy, strncat, etc... it is bounded.
|
|
|
|
if (isBounded) {
|
|
|
|
// Get the max number of characters to copy.
|
2011-02-22 12:58:34 +08:00
|
|
|
const Expr *lenExpr = CE->getArg(2);
|
|
|
|
SVal lenVal = state->getSVal(lenExpr);
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// Protect against misdeclared strncpy().
|
|
|
|
lenVal = svalBuilder.evalCast(lenVal,
|
|
|
|
svalBuilder.getContext().getSizeType(),
|
|
|
|
lenExpr->getType());
|
2011-04-29 02:59:43 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
|
2011-02-22 12:58:34 +08:00
|
|
|
NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal);
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If we know both values, we might be able to figure out how much
|
|
|
|
// we're copying.
|
|
|
|
if (strLengthNL && lenValNL) {
|
|
|
|
const GRState *stateSourceTooLong, *stateSourceNotTooLong;
|
|
|
|
|
|
|
|
// Check if the max number to copy is less than the length of the src.
|
|
|
|
llvm::tie(stateSourceTooLong, stateSourceNotTooLong) =
|
|
|
|
state->assume(cast<DefinedOrUnknownSVal>
|
|
|
|
(svalBuilder.evalBinOpNN(state, BO_GT, *strLengthNL,
|
|
|
|
*lenValNL, cmpTy)));
|
|
|
|
|
|
|
|
if (stateSourceTooLong && !stateSourceNotTooLong) {
|
|
|
|
// Max number to copy is less than the length of the src, so the actual
|
|
|
|
// strLength copied is the max number arg.
|
|
|
|
state = stateSourceTooLong;
|
|
|
|
amountCopied = lenVal;
|
|
|
|
|
|
|
|
} else if (!stateSourceTooLong && stateSourceNotTooLong) {
|
|
|
|
// The source buffer entirely fits in the bound.
|
|
|
|
state = stateSourceNotTooLong;
|
|
|
|
amountCopied = strLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we couldn't pin down the copy length, at least bound it.
|
|
|
|
if (amountCopied.isUnknown()) {
|
|
|
|
// Try to get a "hypothetical" string length symbol, which we can later
|
|
|
|
// set as a real value if that turns out to be the case.
|
|
|
|
amountCopied = getCStringLength(C, state, lenExpr, srcVal, true);
|
|
|
|
assert(!amountCopied.isUndef());
|
|
|
|
|
|
|
|
if (NonLoc *amountCopiedNL = dyn_cast<NonLoc>(&amountCopied)) {
|
|
|
|
if (lenValNL) {
|
|
|
|
// amountCopied <= lenVal
|
|
|
|
SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE,
|
|
|
|
*amountCopiedNL,
|
|
|
|
*lenValNL,
|
|
|
|
cmpTy);
|
|
|
|
state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanBound),
|
|
|
|
true);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strLengthNL) {
|
|
|
|
// amountCopied <= strlen(source)
|
|
|
|
SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE,
|
|
|
|
*amountCopiedNL,
|
|
|
|
*strLengthNL,
|
|
|
|
cmpTy);
|
|
|
|
state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanSrc),
|
|
|
|
true);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// The function isn't bounded. The amount copied should match the length
|
|
|
|
// of the source buffer.
|
|
|
|
amountCopied = strLength;
|
2011-02-22 12:58:34 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
assert(state);
|
|
|
|
|
|
|
|
// This represents the number of characters copied into the destination
|
|
|
|
// buffer. (It may not actually be the strlen if the destination buffer
|
|
|
|
// is not terminated.)
|
|
|
|
SVal finalStrLength = UnknownVal();
|
|
|
|
|
2011-04-09 23:12:58 +08:00
|
|
|
// If this is an appending function (strcat, strncat...) then set the
|
|
|
|
// string length to strlen(src) + strlen(dst) since the buffer will
|
|
|
|
// ultimately contain both.
|
|
|
|
if (isAppending) {
|
2011-06-16 13:51:02 +08:00
|
|
|
// Get the string length of the destination. If the destination is memory
|
|
|
|
// that can't have a string length, we shouldn't be copying into it anyway.
|
2011-04-09 23:12:58 +08:00
|
|
|
SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
|
|
|
|
if (dstStrLength.isUndef())
|
|
|
|
return;
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
QualType sizeTy = svalBuilder.getContext().getSizeType();
|
|
|
|
|
|
|
|
NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&amountCopied);
|
2011-04-09 23:12:58 +08:00
|
|
|
NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength);
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If we know both string lengths, we might know the final string length.
|
|
|
|
if (srcStrLengthNL && dstStrLengthNL) {
|
|
|
|
// Make sure the two lengths together don't overflow a size_t.
|
|
|
|
state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL,
|
|
|
|
*dstStrLengthNL, sizeTy);
|
|
|
|
}
|
2011-04-09 23:12:58 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// If we couldn't get a single value for the final string length,
|
|
|
|
// we can at least bound it by the individual lengths.
|
|
|
|
if (finalStrLength.isUnknown()) {
|
|
|
|
// Try to get a "hypothetical" string length symbol, which we can later
|
|
|
|
// set as a real value if that turns out to be the case.
|
|
|
|
finalStrLength = getCStringLength(C, state, CE, DstVal, true);
|
|
|
|
assert(!finalStrLength.isUndef());
|
|
|
|
|
|
|
|
if (NonLoc *finalStrLengthNL = dyn_cast<NonLoc>(&finalStrLength)) {
|
|
|
|
if (srcStrLengthNL) {
|
|
|
|
// finalStrLength >= srcStrLength
|
|
|
|
SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE,
|
|
|
|
*finalStrLengthNL,
|
|
|
|
*srcStrLengthNL,
|
|
|
|
cmpTy);
|
|
|
|
state = state->assume(cast<DefinedOrUnknownSVal>(sourceInResult),
|
|
|
|
true);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dstStrLengthNL) {
|
|
|
|
// finalStrLength >= dstStrLength
|
|
|
|
SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
|
|
|
|
*finalStrLengthNL,
|
|
|
|
*dstStrLengthNL,
|
|
|
|
cmpTy);
|
|
|
|
state = state->assume(cast<DefinedOrUnknownSVal>(destInResult),
|
|
|
|
true);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-09 23:12:58 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
|
|
|
|
// the final string length will match the input string length.
|
|
|
|
finalStrLength = amountCopied;
|
2011-04-09 23:12:58 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
// The final result of the function will either be a pointer past the last
|
|
|
|
// copied element, or a pointer to the start of the destination buffer.
|
2010-12-02 15:49:45 +08:00
|
|
|
SVal Result = (returnEnd ? UnknownVal() : DstVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
assert(state);
|
|
|
|
|
2010-08-16 15:51:42 +08:00
|
|
|
// If the destination is a MemRegion, try to check for a buffer overflow and
|
|
|
|
// record the new string length.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
|
2011-06-15 13:52:56 +08:00
|
|
|
// If the final length is known, we can check for an overflow.
|
|
|
|
if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&finalStrLength)) {
|
|
|
|
SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
|
|
|
|
*knownStrLength,
|
|
|
|
Dst->getType());
|
2010-08-16 15:51:42 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
state = CheckLocation(C, state, Dst, lastElement, /* IsDst = */ true);
|
2010-08-16 15:51:42 +08:00
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If this is a stpcpy-style copy, the last element is the return value.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (returnEnd)
|
|
|
|
Result = lastElement;
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate the destination. This must happen before we set the C string
|
|
|
|
// length because invalidation will clear the length.
|
|
|
|
// FIXME: Even if we can't perfectly model the copy, we should see if we
|
|
|
|
// can use LazyCompoundVals to copy the source values into the destination.
|
|
|
|
// This would probably remove any existing bindings past the end of the
|
|
|
|
// string, but that's still an improvement over blank invalidation.
|
2010-12-02 15:49:45 +08:00
|
|
|
state = InvalidateBuffer(C, state, Dst, *dstRegVal);
|
2010-08-16 15:51:42 +08:00
|
|
|
|
|
|
|
// Set the C string length of the destination.
|
2011-06-15 13:52:56 +08:00
|
|
|
state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:52:56 +08:00
|
|
|
assert(state);
|
|
|
|
|
2010-08-16 15:51:42 +08:00
|
|
|
// If this is a stpcpy-style copy, but we were unable to check for a buffer
|
|
|
|
// overflow, we still need a result. Conjure a return value.
|
2010-12-02 15:49:45 +08:00
|
|
|
if (returnEnd && Result.isUnknown()) {
|
2010-08-16 15:51:42 +08:00
|
|
|
unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
|
2011-06-15 13:52:56 +08:00
|
|
|
Result = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
|
2010-08-16 15:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the return value.
|
|
|
|
state = state->BindExpr(CE, Result);
|
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
|
2011-04-13 01:08:43 +08:00
|
|
|
void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
//int strcmp(const char *restrict s1, const char *restrict s2);
|
2011-04-28 23:09:11 +08:00
|
|
|
evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false);
|
2011-04-26 06:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
|
|
|
|
//int strncmp(const char *restrict s1, const char *restrict s2, size_t n);
|
2011-04-28 23:09:11 +08:00
|
|
|
evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStringChecker::evalStrcasecmp(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
|
|
|
//int strcasecmp(const char *restrict s1, const char *restrict s2);
|
|
|
|
evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true);
|
2011-04-26 06:21:00 +08:00
|
|
|
}
|
2011-04-13 01:08:43 +08:00
|
|
|
|
2011-05-03 03:05:49 +08:00
|
|
|
void CStringChecker::evalStrncasecmp(CheckerContext &C,
|
|
|
|
const CallExpr *CE) const {
|
|
|
|
//int strncasecmp(const char *restrict s1, const char *restrict s2, size_t n);
|
|
|
|
evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true);
|
|
|
|
}
|
|
|
|
|
2011-04-26 06:21:00 +08:00
|
|
|
void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
|
2011-04-28 23:09:11 +08:00
|
|
|
bool isBounded, bool ignoreCase) const {
|
2011-04-13 01:08:43 +08:00
|
|
|
const GRState *state = C.getState();
|
|
|
|
|
|
|
|
// Check that the first string is non-null
|
|
|
|
const Expr *s1 = CE->getArg(0);
|
|
|
|
SVal s1Val = state->getSVal(s1);
|
|
|
|
state = checkNonNull(C, state, s1, s1Val);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check that the second string is non-null.
|
|
|
|
const Expr *s2 = CE->getArg(1);
|
|
|
|
SVal s2Val = state->getSVal(s2);
|
|
|
|
state = checkNonNull(C, state, s2, s2Val);
|
|
|
|
if (!state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string length of the first string or give up.
|
|
|
|
SVal s1Length = getCStringLength(C, state, s1, s1Val);
|
|
|
|
if (s1Length.isUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string length of the second string or give up.
|
|
|
|
SVal s2Length = getCStringLength(C, state, s2, s2Val);
|
|
|
|
if (s2Length.isUndef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the string literal of the first string.
|
|
|
|
const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val);
|
|
|
|
if (!s1StrLiteral)
|
|
|
|
return;
|
|
|
|
llvm::StringRef s1StrRef = s1StrLiteral->getString();
|
|
|
|
|
|
|
|
// Get the string literal of the second string.
|
|
|
|
const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val);
|
|
|
|
if (!s2StrLiteral)
|
|
|
|
return;
|
|
|
|
llvm::StringRef s2StrRef = s2StrLiteral->getString();
|
|
|
|
|
2011-04-26 06:21:00 +08:00
|
|
|
int result;
|
|
|
|
if (isBounded) {
|
|
|
|
// Get the max number of characters to compare.
|
|
|
|
const Expr *lenExpr = CE->getArg(2);
|
|
|
|
SVal lenVal = state->getSVal(lenExpr);
|
|
|
|
|
|
|
|
// Dynamically cast the length to a ConcreteInt. If it is not a ConcreteInt
|
|
|
|
// then give up, otherwise get the value and use it as the bounds.
|
|
|
|
nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&lenVal);
|
|
|
|
if (!CI)
|
|
|
|
return;
|
|
|
|
llvm::APSInt lenInt(CI->getValue());
|
|
|
|
|
2011-05-03 03:05:49 +08:00
|
|
|
// Create substrings of each to compare the prefix.
|
|
|
|
s1StrRef = s1StrRef.substr(0, (size_t)lenInt.getLimitedValue());
|
|
|
|
s2StrRef = s2StrRef.substr(0, (size_t)lenInt.getLimitedValue());
|
|
|
|
}
|
2011-04-28 23:09:11 +08:00
|
|
|
|
2011-05-03 03:05:49 +08:00
|
|
|
if (ignoreCase) {
|
|
|
|
// Compare string 1 to string 2 the same way strcasecmp() does.
|
|
|
|
result = s1StrRef.compare_lower(s2StrRef);
|
2011-04-26 06:21:00 +08:00
|
|
|
} else {
|
|
|
|
// Compare string 1 to string 2 the same way strcmp() does.
|
2011-05-03 03:05:49 +08:00
|
|
|
result = s1StrRef.compare(s2StrRef);
|
2011-04-26 06:21:00 +08:00
|
|
|
}
|
2011-04-13 01:08:43 +08:00
|
|
|
|
|
|
|
// Build the SVal of the comparison to bind the return value.
|
|
|
|
SValBuilder &svalBuilder = C.getSValBuilder();
|
|
|
|
QualType intTy = svalBuilder.getContext().IntTy;
|
|
|
|
SVal resultVal = svalBuilder.makeIntVal(result, intTy);
|
|
|
|
|
|
|
|
// Bind the return value of the expression.
|
|
|
|
// Set the return value.
|
|
|
|
state = state->BindExpr(CE, resultVal);
|
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-08-15 05:02:52 +08:00
|
|
|
// The driver method, and other Checker callbacks.
|
2010-07-09 07:57:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-07-07 07:11:01 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
|
2010-07-07 07:11:01 +08:00
|
|
|
// Get the callee. All the functions we care about are C functions
|
|
|
|
// with simple identifiers.
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
const Expr *Callee = CE->getCallee();
|
|
|
|
const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
|
|
|
|
|
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the name of the callee. If it's a builtin, strip off the prefix.
|
2010-11-02 07:16:05 +08:00
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II) // if no identifier, not a simple C function
|
|
|
|
return false;
|
|
|
|
llvm::StringRef Name = II->getName();
|
2010-07-07 07:11:01 +08:00
|
|
|
if (Name.startswith("__builtin_"))
|
|
|
|
Name = Name.substr(10);
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
|
|
|
|
.Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
|
2011-06-04 07:42:56 +08:00
|
|
|
.Cases("mempcpy", "__mempcpy_chk", &CStringChecker::evalMempcpy)
|
2010-12-02 05:57:22 +08:00
|
|
|
.Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
|
|
|
|
.Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
|
|
|
|
.Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
|
2011-05-04 00:34:26 +08:00
|
|
|
//.Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy)
|
2010-12-02 05:57:22 +08:00
|
|
|
.Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
|
2011-04-09 23:12:58 +08:00
|
|
|
.Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat)
|
|
|
|
.Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat)
|
2010-12-02 15:49:45 +08:00
|
|
|
.Case("strlen", &CStringChecker::evalstrLength)
|
2011-02-22 12:55:05 +08:00
|
|
|
.Case("strnlen", &CStringChecker::evalstrnLength)
|
2011-04-13 01:08:43 +08:00
|
|
|
.Case("strcmp", &CStringChecker::evalStrcmp)
|
2011-04-26 06:21:00 +08:00
|
|
|
.Case("strncmp", &CStringChecker::evalStrncmp)
|
2011-04-28 23:09:11 +08:00
|
|
|
.Case("strcasecmp", &CStringChecker::evalStrcasecmp)
|
2011-05-03 03:05:49 +08:00
|
|
|
.Case("strncasecmp", &CStringChecker::evalStrncasecmp)
|
2010-12-02 05:57:22 +08:00
|
|
|
.Case("bcopy", &CStringChecker::evalBcopy)
|
2010-07-07 07:11:01 +08:00
|
|
|
.Default(NULL);
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// If the callee isn't a string function, let another checker handle it.
|
2010-12-02 05:57:22 +08:00
|
|
|
if (!evalFunction)
|
2010-07-07 07:11:01 +08:00
|
|
|
return false;
|
|
|
|
|
2010-07-09 07:57:29 +08:00
|
|
|
// Check and evaluate the call.
|
2010-12-02 05:57:22 +08:00
|
|
|
(this->*evalFunction)(C, CE);
|
2010-07-07 07:11:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
// Record string length for char a[] = "abc";
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
|
|
|
|
for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const VarDecl *D = dyn_cast<VarDecl>(*I);
|
|
|
|
if (!D)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// FIXME: Handle array fields of structs.
|
|
|
|
if (!D->getType()->isArrayType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Expr *Init = D->getInit();
|
|
|
|
if (!Init)
|
|
|
|
continue;
|
|
|
|
if (!isa<StringLiteral>(Init))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Loc VarLoc = state->getLValue(D, C.getPredecessor()->getLocationContext());
|
|
|
|
const MemRegion *MR = VarLoc.getAsRegion();
|
|
|
|
if (!MR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SVal StrVal = state->getSVal(Init);
|
|
|
|
assert(StrVal.isValid() && "Initializer string is unknown or undefined");
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal strLength
|
|
|
|
= cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
|
2010-08-15 05:02:52 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
state = state->set<CStringLength>(MR, strLength);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
C.addTransition(state);
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
bool CStringChecker::wantsRegionChangeUpdate(const GRState *state) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
CStringLength::EntryMap Entries = state->get<CStringLength>();
|
|
|
|
return !Entries.isEmpty();
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
const GRState *
|
|
|
|
CStringChecker::checkRegionChanges(const GRState *state,
|
2011-05-03 03:42:42 +08:00
|
|
|
const StoreManager::InvalidatedSymbols *,
|
2011-02-24 09:05:30 +08:00
|
|
|
const MemRegion * const *Begin,
|
|
|
|
const MemRegion * const *End) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
CStringLength::EntryMap Entries = state->get<CStringLength>();
|
|
|
|
if (Entries.isEmpty())
|
|
|
|
return state;
|
|
|
|
|
|
|
|
llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
|
|
|
|
llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
|
|
|
|
|
|
|
|
// First build sets for the changed regions and their super-regions.
|
|
|
|
for ( ; Begin != End; ++Begin) {
|
|
|
|
const MemRegion *MR = *Begin;
|
|
|
|
Invalidated.insert(MR);
|
|
|
|
|
|
|
|
SuperRegions.insert(MR);
|
|
|
|
while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
|
|
|
|
MR = SR->getSuperRegion();
|
|
|
|
SuperRegions.insert(MR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
|
|
|
|
|
|
|
|
// Then loop over the entries in the current state.
|
|
|
|
for (CStringLength::EntryMap::iterator I = Entries.begin(),
|
|
|
|
E = Entries.end(); I != E; ++I) {
|
|
|
|
const MemRegion *MR = I.getKey();
|
|
|
|
|
|
|
|
// Is this entry for a super-region of a changed region?
|
|
|
|
if (SuperRegions.count(MR)) {
|
2010-11-24 08:54:37 +08:00
|
|
|
Entries = F.remove(Entries, MR);
|
2010-08-15 05:02:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this entry for a sub-region of a changed region?
|
|
|
|
const MemRegion *Super = MR;
|
|
|
|
while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
|
|
|
|
Super = SR->getSuperRegion();
|
|
|
|
if (Invalidated.count(Super)) {
|
2010-11-24 08:54:37 +08:00
|
|
|
Entries = F.remove(Entries, MR);
|
2010-08-15 05:02:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state->set<CStringLength>(Entries);
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::checkLiveSymbols(const GRState *state,
|
|
|
|
SymbolReaper &SR) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
// Mark all symbols in our string length map as valid.
|
|
|
|
CStringLength::EntryMap Entries = state->get<CStringLength>();
|
|
|
|
|
|
|
|
for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SVal Len = I.getData();
|
2011-06-15 13:52:56 +08:00
|
|
|
|
|
|
|
for (SVal::symbol_iterator si = Len.symbol_begin(), se = Len.symbol_end();
|
|
|
|
si != se; ++si)
|
|
|
|
SR.markInUse(*si);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 09:05:30 +08:00
|
|
|
void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
|
|
|
|
CheckerContext &C) const {
|
2010-08-15 05:02:52 +08:00
|
|
|
if (!SR.hasDeadSymbols())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
CStringLength::EntryMap Entries = state->get<CStringLength>();
|
|
|
|
if (Entries.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
|
|
|
|
for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SVal Len = I.getData();
|
|
|
|
if (SymbolRef Sym = Len.getAsSymbol()) {
|
|
|
|
if (SR.isDead(Sym))
|
2010-11-24 08:54:37 +08:00
|
|
|
Entries = F.remove(Entries, I.getKey());
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state = state->set<CStringLength>(Entries);
|
2010-12-21 05:19:09 +08:00
|
|
|
C.generateNode(state);
|
2010-08-15 05:02:52 +08:00
|
|
|
}
|
2011-02-24 09:05:30 +08:00
|
|
|
|
|
|
|
void ento::registerCStringChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<CStringChecker>();
|
|
|
|
}
|