2009-11-24 16:24:26 +08:00
|
|
|
//=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-23 02:53:44 +08:00
|
|
|
// This defines UndefResultChecker, a builtin check in ExprEngine that
|
2009-11-24 16:24:26 +08:00
|
|
|
// performs checks for undefined results of non-assignment binary operators.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:27:22 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:22 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-11-24 16:24:26 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-24 16:24:26 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UndefResultChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::PostStmt<BinaryOperator> > {
|
2009-11-24 16:24:26 +08:00
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
|
|
|
|
2009-11-24 16:24:26 +08:00
|
|
|
public:
|
2011-02-28 09:27:22 +08:00
|
|
|
void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
|
2009-11-24 16:24:26 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-02-28 09:27:22 +08:00
|
|
|
void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
|
|
|
|
CheckerContext &C) const {
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef state = C.getState();
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx = C.getLocationContext();
|
|
|
|
if (state->getSVal(B, LCtx).isUndef()) {
|
2013-06-19 07:16:15 +08:00
|
|
|
|
|
|
|
// Do not report assignments of uninitialized values inside swap functions.
|
|
|
|
// This should allow to swap partially uninitialized structs
|
|
|
|
// (radar://14129997)
|
|
|
|
if (const FunctionDecl *EnclosingFunctionDecl =
|
|
|
|
dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
|
|
|
|
if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
|
|
|
|
return;
|
|
|
|
|
2009-11-24 16:24:26 +08:00
|
|
|
// Generate an error node.
|
2010-12-21 05:19:09 +08:00
|
|
|
ExplodedNode *N = C.generateSink();
|
2009-11-24 16:24:26 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(
|
|
|
|
new BuiltinBug(this, "Result of operation is garbage or undefined"));
|
2009-11-24 16:24:26 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> sbuf;
|
2009-11-24 16:24:26 +08:00
|
|
|
llvm::raw_svector_ostream OS(sbuf);
|
2014-05-27 10:45:47 +08:00
|
|
|
const Expr *Ex = nullptr;
|
2009-11-24 16:24:26 +08:00
|
|
|
bool isLeft = true;
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
|
2009-11-24 16:24:26 +08:00
|
|
|
Ex = B->getLHS()->IgnoreParenCasts();
|
|
|
|
isLeft = true;
|
|
|
|
}
|
2012-01-07 06:09:28 +08:00
|
|
|
else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
|
2009-11-24 16:24:26 +08:00
|
|
|
Ex = B->getRHS()->IgnoreParenCasts();
|
|
|
|
isLeft = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ex) {
|
|
|
|
OS << "The " << (isLeft ? "left" : "right")
|
|
|
|
<< " operand of '"
|
|
|
|
<< BinaryOperator::getOpcodeStr(B->getOpcode())
|
|
|
|
<< "' is a garbage value";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Neither operand was undefined, but the result is undefined.
|
|
|
|
OS << "The result of the '"
|
|
|
|
<< BinaryOperator::getOpcodeStr(B->getOpcode())
|
|
|
|
<< "' expression is undefined";
|
|
|
|
}
|
2015-06-23 21:15:32 +08:00
|
|
|
auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
|
2009-11-29 14:37:44 +08:00
|
|
|
if (Ex) {
|
|
|
|
report->addRange(Ex->getSourceRange());
|
2012-08-28 08:50:51 +08:00
|
|
|
bugreporter::trackNullOrUndefValue(N, Ex, *report);
|
2009-11-29 14:37:44 +08:00
|
|
|
}
|
2009-11-24 16:24:26 +08:00
|
|
|
else
|
2012-08-28 08:50:51 +08:00
|
|
|
bugreporter::trackNullOrUndefValue(N, B, *report);
|
2012-06-06 14:25:37 +08:00
|
|
|
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(report));
|
2009-11-24 16:24:26 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-28 09:27:22 +08:00
|
|
|
|
|
|
|
void ento::registerUndefResultChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefResultChecker>();
|
|
|
|
}
|