2010-12-02 05:28:31 +08:00
|
|
|
// SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*-
|
2009-06-26 08:25:05 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-02 05:28:31 +08:00
|
|
|
// This file defines SimpleSValBuilder, a basic implementation of SValBuilder.
|
2009-06-26 08:25:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2018-04-11 14:21:12 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2018-04-11 14:21:12 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
|
2017-04-13 15:20:04 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValVisitor.h"
|
2009-06-26 08:25:05 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-06-26 08:25:05 +08:00
|
|
|
|
|
|
|
namespace {
|
2010-12-02 05:28:31 +08:00
|
|
|
class SimpleSValBuilder : public SValBuilder {
|
2009-07-22 05:03:30 +08:00
|
|
|
protected:
|
2014-03-15 12:29:04 +08:00
|
|
|
SVal dispatchCast(SVal val, QualType castTy) override;
|
|
|
|
SVal evalCastFromNonLoc(NonLoc val, QualType castTy) override;
|
|
|
|
SVal evalCastFromLoc(Loc val, QualType castTy) override;
|
2009-07-22 05:03:30 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
public:
|
2010-12-02 15:49:45 +08:00
|
|
|
SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
|
2011-08-16 06:09:50 +08:00
|
|
|
ProgramStateManager &stateMgr)
|
2010-12-02 15:49:45 +08:00
|
|
|
: SValBuilder(alloc, context, stateMgr) {}
|
2015-10-20 21:23:58 +08:00
|
|
|
~SimpleSValBuilder() override {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
SVal evalMinus(NonLoc val) override;
|
|
|
|
SVal evalComplement(NonLoc val) override;
|
|
|
|
SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op,
|
|
|
|
NonLoc lhs, NonLoc rhs, QualType resultTy) override;
|
|
|
|
SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op,
|
|
|
|
Loc lhs, Loc rhs, QualType resultTy) override;
|
|
|
|
SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op,
|
|
|
|
Loc lhs, NonLoc rhs, QualType resultTy) override;
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
/// getKnownValue - evaluates a given SVal. If the SVal has only one possible
|
2010-07-04 08:00:41 +08:00
|
|
|
/// (integer) value, that value is returned. Otherwise, returns NULL.
|
2014-03-15 12:29:04 +08:00
|
|
|
const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal V) override;
|
|
|
|
|
2017-04-13 15:20:04 +08:00
|
|
|
/// Recursively descends into symbolic expressions and replaces symbols
|
|
|
|
/// with their known values (in the sense of the getKnownValue() method).
|
|
|
|
SVal simplifySVal(ProgramStateRef State, SVal V) override;
|
|
|
|
|
2010-06-20 12:56:29 +08:00
|
|
|
SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op,
|
|
|
|
const llvm::APSInt &RHS, QualType resultTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2009-06-26 08:25:05 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2010-12-23 15:20:52 +08:00
|
|
|
SValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
|
|
|
|
ASTContext &context,
|
2011-08-16 06:09:50 +08:00
|
|
|
ProgramStateManager &stateMgr) {
|
2010-12-02 15:49:45 +08:00
|
|
|
return new SimpleSValBuilder(alloc, context, stateMgr);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Casts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-16 05:33:26 +08:00
|
|
|
SVal SimpleSValBuilder::dispatchCast(SVal Val, QualType CastTy) {
|
2013-02-20 13:52:05 +08:00
|
|
|
assert(Val.getAs<Loc>() || Val.getAs<NonLoc>());
|
|
|
|
return Val.getAs<Loc>() ? evalCastFromLoc(Val.castAs<Loc>(), CastTy)
|
|
|
|
: evalCastFromNonLoc(Val.castAs<NonLoc>(), CastTy);
|
2011-12-07 07:12:27 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) {
|
2011-02-17 05:13:32 +08:00
|
|
|
bool isLocType = Loc::isLocType(castTy);
|
2016-12-16 05:27:06 +08:00
|
|
|
if (val.getAs<nonloc::PointerToMember>())
|
|
|
|
return val;
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::LocAsInteger> LI = val.getAs<nonloc::LocAsInteger>()) {
|
2009-07-21 08:12:07 +08:00
|
|
|
if (isLocType)
|
2009-07-21 05:39:27 +08:00
|
|
|
return LI->getLoc();
|
2010-01-11 10:33:26 +08:00
|
|
|
// FIXME: Correctly support promotions/truncations.
|
2017-07-11 08:30:14 +08:00
|
|
|
unsigned castSize = Context.getIntWidth(castTy);
|
2010-01-11 10:33:26 +08:00
|
|
|
if (castSize == LI->getNumBits())
|
2009-07-21 08:12:07 +08:00
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeLocAsInteger(LI->getLoc(), castSize);
|
2009-07-21 08:12:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (const SymExpr *se = val.getAsSymbolicExpression()) {
|
2012-09-26 14:00:14 +08:00
|
|
|
QualType T = Context.getCanonicalType(se->getType());
|
2011-12-09 11:34:02 +08:00
|
|
|
// If types are the same or both are integers, ignore the cast.
|
2009-09-25 08:18:15 +08:00
|
|
|
// FIXME: Remove this hack when we support symbolic truncation/extension.
|
|
|
|
// HACK: If both castTy and T are integers, ignore the cast. This is
|
|
|
|
// not a permanent solution. Eventually we want to precisely handle
|
|
|
|
// extension/truncation of symbolic integers. This prevents us from losing
|
|
|
|
// precision when we assign 'x = y' and 'y' is symbolic and x and y are
|
|
|
|
// different integer types.
|
2011-12-09 11:34:02 +08:00
|
|
|
if (haveSameType(T, castTy))
|
2009-09-25 08:18:15 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
if (!isLocType)
|
|
|
|
return makeNonLoc(se, T, castTy);
|
2009-07-21 08:12:07 +08:00
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-12-05 12:47:09 +08:00
|
|
|
// If value is a non-integer constant, produce unknown.
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!val.getAs<nonloc::ConcreteInt>())
|
2009-06-26 08:25:05 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-11-29 08:50:20 +08:00
|
|
|
// Handle casts to a boolean type.
|
|
|
|
if (castTy->isBooleanType()) {
|
2013-02-20 13:52:05 +08:00
|
|
|
bool b = val.castAs<nonloc::ConcreteInt>().getValue().getBoolValue();
|
2012-11-29 08:50:20 +08:00
|
|
|
return makeTruthVal(b, castTy);
|
|
|
|
}
|
|
|
|
|
2011-12-09 06:38:43 +08:00
|
|
|
// Only handle casts from integers to integers - if val is an integer constant
|
2013-12-05 12:47:09 +08:00
|
|
|
// being cast to a non-integer type, produce unknown.
|
2013-04-09 10:30:33 +08:00
|
|
|
if (!isLocType && !castTy->isIntegralOrEnumerationType())
|
2009-06-26 08:25:05 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
llvm::APSInt i = val.castAs<nonloc::ConcreteInt>().getValue();
|
2012-05-08 11:26:58 +08:00
|
|
|
BasicVals.getAPSIntType(castTy).apply(i);
|
2009-06-26 08:25:05 +08:00
|
|
|
|
|
|
|
if (isLocType)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntLocVal(i);
|
2009-06-26 08:25:05 +08:00
|
|
|
else
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(i);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SimpleSValBuilder::evalCastFromLoc(Loc val, QualType castTy) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
// Casts from pointers -> pointers, just return the lval.
|
|
|
|
//
|
|
|
|
// Casts from pointers -> references, just return the lval. These
|
|
|
|
// can be introduced by the frontend for corner cases, e.g
|
|
|
|
// casting from va_list* to __builtin_va_list&.
|
|
|
|
//
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(castTy) || castTy->isReferenceType())
|
2009-06-26 08:25:05 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
// FIXME: Handle transparent unions where a value can be "transparently"
|
|
|
|
// lifted into a union type.
|
|
|
|
if (castTy->isUnionType())
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
// Casting a Loc to a bool will almost always be true,
|
|
|
|
// unless this is a weak function or a symbolic region.
|
|
|
|
if (castTy->isBooleanType()) {
|
|
|
|
switch (val.getSubKind()) {
|
2016-01-13 21:49:29 +08:00
|
|
|
case loc::MemRegionValKind: {
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
const MemRegion *R = val.castAs<loc::MemRegionVal>().getRegion();
|
2016-01-13 21:49:29 +08:00
|
|
|
if (const FunctionCodeRegion *FTR = dyn_cast<FunctionCodeRegion>(R))
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FTR->getDecl()))
|
|
|
|
if (FD->isWeak())
|
|
|
|
// FIXME: Currently we are using an extent symbol here,
|
|
|
|
// because there are no generic region address metadata
|
|
|
|
// symbols to use, only content metadata.
|
|
|
|
return nonloc::SymbolVal(SymMgr.getExtentSymbol(FTR));
|
|
|
|
|
|
|
|
if (const SymbolicRegion *SymR = R->getSymbolicBase())
|
2018-07-17 08:42:35 +08:00
|
|
|
return makeNonLoc(SymR->getSymbol(), BO_NE,
|
|
|
|
BasicVals.getZeroWithPtrWidth(), castTy);
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
|
|
|
|
// FALL-THROUGH
|
2017-06-03 14:26:27 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
2013-12-05 12:47:09 +08:00
|
|
|
// Labels and non-symbolic memory regions are always true.
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
return makeTruthVal(true, castTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-09 10:30:33 +08:00
|
|
|
if (castTy->isIntegralOrEnumerationType()) {
|
2017-07-11 08:30:14 +08:00
|
|
|
unsigned BitWidth = Context.getIntWidth(castTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
if (!val.getAs<loc::ConcreteInt>())
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeLocAsInteger(val, BitWidth);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
llvm::APSInt i = val.castAs<loc::ConcreteInt>().getValue();
|
2012-05-08 11:26:58 +08:00
|
|
|
BasicVals.getAPSIntType(castTy).apply(i);
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(i);
|
2010-04-17 01:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// All other cases: return 'UnknownVal'. This includes casting pointers
|
|
|
|
// to floats, which is probably badness it itself, but this is a good
|
|
|
|
// intermediate solution until we do something better.
|
|
|
|
return UnknownVal();
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for unary operators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SimpleSValBuilder::evalMinus(NonLoc val) {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (val.getSubKind()) {
|
2009-06-26 08:25:05 +08:00
|
|
|
case nonloc::ConcreteIntKind:
|
2013-02-20 13:52:05 +08:00
|
|
|
return val.castAs<nonloc::ConcreteInt>().evalMinus(*this);
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SimpleSValBuilder::evalComplement(NonLoc X) {
|
2009-06-26 08:25:05 +08:00
|
|
|
switch (X.getSubKind()) {
|
|
|
|
case nonloc::ConcreteIntKind:
|
2013-02-20 13:52:05 +08:00
|
|
|
return X.castAs<nonloc::ConcreteInt>().evalComplement(*this);
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for binary operators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-02 05:28:31 +08:00
|
|
|
SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS,
|
2010-06-20 12:56:29 +08:00
|
|
|
BinaryOperator::Opcode op,
|
|
|
|
const llvm::APSInt &RHS,
|
|
|
|
QualType resultTy) {
|
|
|
|
bool isIdempotent = false;
|
|
|
|
|
|
|
|
// Check for a few special cases with known reductions first.
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
// We can't reduce this case; just treat it normally.
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a*0 and a*1
|
|
|
|
if (RHS == 0)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(0, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
else if (RHS == 1)
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Div:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a/0 and a/1
|
|
|
|
if (RHS == 0)
|
|
|
|
// This is also handled elsewhere.
|
|
|
|
return UndefinedVal();
|
|
|
|
else if (RHS == 1)
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Rem:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a%0 and a%1
|
|
|
|
if (RHS == 0)
|
|
|
|
// This is also handled elsewhere.
|
|
|
|
return UndefinedVal();
|
|
|
|
else if (RHS == 1)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(0, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Add:
|
|
|
|
case BO_Sub:
|
|
|
|
case BO_Shl:
|
|
|
|
case BO_Shr:
|
|
|
|
case BO_Xor:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a+0, a-0, a<<0, a>>0, a^0
|
|
|
|
if (RHS == 0)
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_And:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a&0 and a&(~0)
|
|
|
|
if (RHS == 0)
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeIntVal(0, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
else if (RHS.isAllOnesValue())
|
|
|
|
isIdempotent = true;
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Or:
|
2010-06-20 12:56:29 +08:00
|
|
|
// a|0 and a|(~0)
|
|
|
|
if (RHS == 0)
|
|
|
|
isIdempotent = true;
|
|
|
|
else if (RHS.isAllOnesValue()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS);
|
2010-06-20 12:56:29 +08:00
|
|
|
return nonloc::ConcreteInt(Result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Idempotent ops (like a*1) can still change the type of an expression.
|
2011-03-01 08:45:32 +08:00
|
|
|
// Wrap the LHS up in a NonLoc again and let evalCastFromNonLoc do the
|
|
|
|
// dirty work.
|
2011-12-06 02:58:30 +08:00
|
|
|
if (isIdempotent)
|
2011-12-06 02:58:25 +08:00
|
|
|
return evalCastFromNonLoc(nonloc::SymbolVal(LHS), resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
|
|
|
|
// If we reach this point, the expression cannot be simplified.
|
2012-05-07 07:40:02 +08:00
|
|
|
// Make a SymbolVal for the entire expression, after converting the RHS.
|
|
|
|
const llvm::APSInt *ConvertedRHS = &RHS;
|
|
|
|
if (BinaryOperator::isComparisonOp(op)) {
|
|
|
|
// We're looking for a type big enough to compare the symbolic value
|
|
|
|
// with the given constant.
|
|
|
|
// FIXME: This is an approximation of Sema::UsualArithmeticConversions.
|
|
|
|
ASTContext &Ctx = getContext();
|
2012-09-26 14:00:14 +08:00
|
|
|
QualType SymbolType = LHS->getType();
|
2012-05-07 07:40:02 +08:00
|
|
|
uint64_t ValWidth = RHS.getBitWidth();
|
|
|
|
uint64_t TypeWidth = Ctx.getTypeSize(SymbolType);
|
|
|
|
|
|
|
|
if (ValWidth < TypeWidth) {
|
|
|
|
// If the value is too small, extend it.
|
|
|
|
ConvertedRHS = &BasicVals.Convert(SymbolType, RHS);
|
|
|
|
} else if (ValWidth == TypeWidth) {
|
|
|
|
// If the value is signed but the symbol is unsigned, do the comparison
|
|
|
|
// in unsigned space. [C99 6.3.1.8]
|
|
|
|
// (For the opposite case, the value is already unsigned.)
|
|
|
|
if (RHS.isSigned() && !SymbolType->isSignedIntegerOrEnumerationType())
|
|
|
|
ConvertedRHS = &BasicVals.Convert(SymbolType, RHS);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ConvertedRHS = &BasicVals.Convert(resultTy, RHS);
|
|
|
|
|
|
|
|
return makeNonLoc(LHS, op, *ConvertedRHS, resultTy);
|
2010-06-20 12:56:29 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 14:21:12 +08:00
|
|
|
// See if Sym is known to be a relation Rel with Bound.
|
|
|
|
static bool isInRelation(BinaryOperator::Opcode Rel, SymbolRef Sym,
|
|
|
|
llvm::APSInt Bound, ProgramStateRef State) {
|
|
|
|
SValBuilder &SVB = State->getStateManager().getSValBuilder();
|
|
|
|
SVal Result =
|
|
|
|
SVB.evalBinOpNN(State, Rel, nonloc::SymbolVal(Sym),
|
|
|
|
nonloc::ConcreteInt(Bound), SVB.getConditionType());
|
|
|
|
if (auto DV = Result.getAs<DefinedSVal>()) {
|
|
|
|
return !State->assume(*DV, false);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if Sym is known to be within [min/4, max/4], where min and max
|
|
|
|
// are the bounds of the symbol's integral type. With such symbols,
|
|
|
|
// some manipulations can be performed without the risk of overflow.
|
|
|
|
// assume() doesn't cause infinite recursion because we should be dealing
|
|
|
|
// with simpler symbols on every recursive call.
|
|
|
|
static bool isWithinConstantOverflowBounds(SymbolRef Sym,
|
|
|
|
ProgramStateRef State) {
|
|
|
|
SValBuilder &SVB = State->getStateManager().getSValBuilder();
|
|
|
|
BasicValueFactory &BV = SVB.getBasicValueFactory();
|
|
|
|
|
|
|
|
QualType T = Sym->getType();
|
|
|
|
assert(T->isSignedIntegerOrEnumerationType() &&
|
|
|
|
"This only works with signed integers!");
|
|
|
|
APSIntType AT = BV.getAPSIntType(T);
|
|
|
|
|
|
|
|
llvm::APSInt Max = AT.getMaxValue() / AT.getValue(4), Min = -Max;
|
|
|
|
return isInRelation(BO_LE, Sym, Max, State) &&
|
|
|
|
isInRelation(BO_GE, Sym, Min, State);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for the concrete integers: see if I is within [min/4, max/4].
|
|
|
|
static bool isWithinConstantOverflowBounds(llvm::APSInt I) {
|
|
|
|
APSIntType AT(I);
|
|
|
|
assert(!AT.isUnsigned() &&
|
|
|
|
"This only works with signed integers!");
|
|
|
|
|
|
|
|
llvm::APSInt Max = AT.getMaxValue() / AT.getValue(4), Min = -Max;
|
|
|
|
return (I <= Max) && (I >= -Max);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<SymbolRef, llvm::APSInt>
|
|
|
|
decomposeSymbol(SymbolRef Sym, BasicValueFactory &BV) {
|
|
|
|
if (const auto *SymInt = dyn_cast<SymIntExpr>(Sym))
|
|
|
|
if (BinaryOperator::isAdditiveOp(SymInt->getOpcode()))
|
|
|
|
return std::make_pair(SymInt->getLHS(),
|
|
|
|
(SymInt->getOpcode() == BO_Add) ?
|
|
|
|
(SymInt->getRHS()) :
|
|
|
|
(-SymInt->getRHS()));
|
|
|
|
|
|
|
|
// Fail to decompose: "reduce" the problem to the "$x + 0" case.
|
|
|
|
return std::make_pair(Sym, BV.getValue(0, Sym->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simplify "(LSym + LInt) Op (RSym + RInt)" assuming all values are of the
|
|
|
|
// same signed integral type and no overflows occur (which should be checked
|
|
|
|
// by the caller).
|
|
|
|
static NonLoc doRearrangeUnchecked(ProgramStateRef State,
|
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
SymbolRef LSym, llvm::APSInt LInt,
|
|
|
|
SymbolRef RSym, llvm::APSInt RInt) {
|
|
|
|
SValBuilder &SVB = State->getStateManager().getSValBuilder();
|
|
|
|
BasicValueFactory &BV = SVB.getBasicValueFactory();
|
|
|
|
SymbolManager &SymMgr = SVB.getSymbolManager();
|
|
|
|
|
|
|
|
QualType SymTy = LSym->getType();
|
|
|
|
assert(SymTy == RSym->getType() &&
|
|
|
|
"Symbols are not of the same type!");
|
|
|
|
assert(APSIntType(LInt) == BV.getAPSIntType(SymTy) &&
|
|
|
|
"Integers are not of the same type as symbols!");
|
|
|
|
assert(APSIntType(RInt) == BV.getAPSIntType(SymTy) &&
|
|
|
|
"Integers are not of the same type as symbols!");
|
|
|
|
|
|
|
|
QualType ResultTy;
|
|
|
|
if (BinaryOperator::isComparisonOp(Op))
|
|
|
|
ResultTy = SVB.getConditionType();
|
|
|
|
else if (BinaryOperator::isAdditiveOp(Op))
|
|
|
|
ResultTy = SymTy;
|
|
|
|
else
|
|
|
|
llvm_unreachable("Operation not suitable for unchecked rearrangement!");
|
|
|
|
|
|
|
|
// FIXME: Can we use assume() without getting into an infinite recursion?
|
|
|
|
if (LSym == RSym)
|
|
|
|
return SVB.evalBinOpNN(State, Op, nonloc::ConcreteInt(LInt),
|
|
|
|
nonloc::ConcreteInt(RInt), ResultTy)
|
|
|
|
.castAs<NonLoc>();
|
|
|
|
|
|
|
|
SymbolRef ResultSym = nullptr;
|
|
|
|
BinaryOperator::Opcode ResultOp;
|
|
|
|
llvm::APSInt ResultInt;
|
|
|
|
if (BinaryOperator::isComparisonOp(Op)) {
|
|
|
|
// Prefer comparing to a non-negative number.
|
|
|
|
// FIXME: Maybe it'd be better to have consistency in
|
|
|
|
// "$x - $y" vs. "$y - $x" because those are solver's keys.
|
|
|
|
if (LInt > RInt) {
|
|
|
|
ResultSym = SymMgr.getSymSymExpr(RSym, BO_Sub, LSym, SymTy);
|
|
|
|
ResultOp = BinaryOperator::reverseComparisonOp(Op);
|
|
|
|
ResultInt = LInt - RInt; // Opposite order!
|
|
|
|
} else {
|
|
|
|
ResultSym = SymMgr.getSymSymExpr(LSym, BO_Sub, RSym, SymTy);
|
|
|
|
ResultOp = Op;
|
|
|
|
ResultInt = RInt - LInt; // Opposite order!
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ResultSym = SymMgr.getSymSymExpr(LSym, Op, RSym, SymTy);
|
|
|
|
ResultInt = (Op == BO_Add) ? (LInt + RInt) : (LInt - RInt);
|
|
|
|
ResultOp = BO_Add;
|
|
|
|
// Bring back the cosmetic difference.
|
|
|
|
if (ResultInt < 0) {
|
|
|
|
ResultInt = -ResultInt;
|
|
|
|
ResultOp = BO_Sub;
|
|
|
|
} else if (ResultInt == 0) {
|
|
|
|
// Shortcut: Simplify "$x + 0" to "$x".
|
|
|
|
return nonloc::SymbolVal(ResultSym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const llvm::APSInt &PersistentResultInt = BV.getValue(ResultInt);
|
|
|
|
return nonloc::SymbolVal(
|
|
|
|
SymMgr.getSymIntExpr(ResultSym, ResultOp, PersistentResultInt, ResultTy));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rearrange if symbol type matches the result type and if the operator is a
|
|
|
|
// comparison operator, both symbol and constant must be within constant
|
|
|
|
// overflow bounds.
|
|
|
|
static bool shouldRearrange(ProgramStateRef State, BinaryOperator::Opcode Op,
|
|
|
|
SymbolRef Sym, llvm::APSInt Int, QualType Ty) {
|
|
|
|
return Sym->getType() == Ty &&
|
|
|
|
(!BinaryOperator::isComparisonOp(Op) ||
|
2018-07-31 03:24:48 +08:00
|
|
|
(isWithinConstantOverflowBounds(Sym, State) &&
|
2018-04-11 14:21:12 +08:00
|
|
|
isWithinConstantOverflowBounds(Int)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<NonLoc> tryRearrange(ProgramStateRef State,
|
|
|
|
BinaryOperator::Opcode Op, NonLoc Lhs,
|
|
|
|
NonLoc Rhs, QualType ResultTy) {
|
|
|
|
ProgramStateManager &StateMgr = State->getStateManager();
|
|
|
|
SValBuilder &SVB = StateMgr.getSValBuilder();
|
|
|
|
|
|
|
|
// We expect everything to be of the same type - this type.
|
|
|
|
QualType SingleTy;
|
|
|
|
|
|
|
|
auto &Opts =
|
|
|
|
StateMgr.getOwningEngine()->getAnalysisManager().getAnalyzerOptions();
|
|
|
|
|
2018-07-23 18:50:20 +08:00
|
|
|
// FIXME: After putting complexity threshold to the symbols we can always
|
|
|
|
// rearrange additive operations but rearrange comparisons only if
|
|
|
|
// option is set.
|
[analyzer] Evaluate all non-checker config options before analysis
In earlier patches regarding AnalyzerOptions, a lot of effort went into
gathering all config options, and changing the interface so that potential
misuse can be eliminited.
Up until this point, AnalyzerOptions only evaluated an option when it was
querried. For example, if we had a "-no-false-positives" flag, AnalyzerOptions
would store an Optional field for it that would be None up until somewhere in
the code until the flag's getter function is called.
However, now that we're confident that we've gathered all configs, we can
evaluate off of them before analysis, so we can emit a error on invalid input
even if that prticular flag will not matter in that particular run of the
analyzer. Another very big benefit of this is that debug.ConfigDumper will now
show the value of all configs every single time.
Also, almost all options related class have a similar interface, so uniformity
is also a benefit.
The implementation for errors on invalid input will be commited shorty.
Differential Revision: https://reviews.llvm.org/D53692
llvm-svn: 348031
2018-12-01 04:44:00 +08:00
|
|
|
if(!Opts.ShouldAggressivelySimplifyBinaryOperation)
|
2018-07-23 18:50:20 +08:00
|
|
|
return None;
|
|
|
|
|
2018-04-11 14:21:12 +08:00
|
|
|
SymbolRef LSym = Lhs.getAsSymbol();
|
|
|
|
if (!LSym)
|
|
|
|
return None;
|
|
|
|
|
2018-07-23 18:50:20 +08:00
|
|
|
if (BinaryOperator::isComparisonOp(Op)) {
|
2018-04-11 14:21:12 +08:00
|
|
|
SingleTy = LSym->getType();
|
|
|
|
if (ResultTy != SVB.getConditionType())
|
|
|
|
return None;
|
|
|
|
// Initialize SingleTy later with a symbol's type.
|
|
|
|
} else if (BinaryOperator::isAdditiveOp(Op)) {
|
|
|
|
SingleTy = ResultTy;
|
2018-04-14 04:23:02 +08:00
|
|
|
if (LSym->getType() != SingleTy)
|
|
|
|
return None;
|
2018-04-11 14:21:12 +08:00
|
|
|
} else {
|
|
|
|
// Don't rearrange other operations.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!SingleTy.isNull() && "We should have figured out the type by now!");
|
|
|
|
|
2018-11-30 18:37:44 +08:00
|
|
|
// Rearrange signed symbolic expressions only
|
|
|
|
if (!SingleTy->isSignedIntegerOrEnumerationType())
|
|
|
|
return None;
|
|
|
|
|
2018-04-11 14:21:12 +08:00
|
|
|
SymbolRef RSym = Rhs.getAsSymbol();
|
|
|
|
if (!RSym || RSym->getType() != SingleTy)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
BasicValueFactory &BV = State->getBasicVals();
|
|
|
|
llvm::APSInt LInt, RInt;
|
|
|
|
std::tie(LSym, LInt) = decomposeSymbol(LSym, BV);
|
|
|
|
std::tie(RSym, RInt) = decomposeSymbol(RSym, BV);
|
|
|
|
if (!shouldRearrange(State, Op, LSym, LInt, SingleTy) ||
|
|
|
|
!shouldRearrange(State, Op, RSym, RInt, SingleTy))
|
|
|
|
return None;
|
|
|
|
|
|
|
|
// We know that no overflows can occur anymore.
|
|
|
|
return doRearrangeUnchecked(State, Op, LSym, LInt, RSym, RInt);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SimpleSValBuilder::evalBinOpNN(ProgramStateRef state,
|
2009-10-06 09:39:48 +08:00
|
|
|
BinaryOperator::Opcode op,
|
2009-06-26 08:25:05 +08:00
|
|
|
NonLoc lhs, NonLoc rhs,
|
2009-07-14 05:55:12 +08:00
|
|
|
QualType resultTy) {
|
2012-05-03 10:13:53 +08:00
|
|
|
NonLoc InputLHS = lhs;
|
|
|
|
NonLoc InputRHS = rhs;
|
|
|
|
|
2009-07-14 05:55:12 +08:00
|
|
|
// Handle trivial case where left-side and right-side are the same.
|
|
|
|
if (lhs == rhs)
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Xor:
|
|
|
|
case BO_Sub:
|
2012-09-05 03:34:58 +08:00
|
|
|
if (resultTy->isIntegralOrEnumerationType())
|
|
|
|
return makeIntVal(0, resultTy);
|
|
|
|
return evalCastFromNonLoc(makeIntVal(0, /*Unsigned=*/false), resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Or:
|
|
|
|
case BO_And:
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromNonLoc(lhs, resultTy);
|
2009-07-14 05:55:12 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-26 08:25:05 +08:00
|
|
|
while (1) {
|
|
|
|
switch (lhs.getSubKind()) {
|
|
|
|
default:
|
2018-08-30 04:28:54 +08:00
|
|
|
return makeSymExprValNN(op, lhs, rhs, resultTy);
|
2016-12-16 05:27:06 +08:00
|
|
|
case nonloc::PointerToMemberKind: {
|
|
|
|
assert(rhs.getSubKind() == nonloc::PointerToMemberKind &&
|
|
|
|
"Both SVals should have pointer-to-member-type");
|
|
|
|
auto LPTM = lhs.castAs<nonloc::PointerToMember>(),
|
|
|
|
RPTM = rhs.castAs<nonloc::PointerToMember>();
|
|
|
|
auto LPTMD = LPTM.getPTMData(), RPTMD = RPTM.getPTMData();
|
|
|
|
switch (op) {
|
|
|
|
case BO_EQ:
|
|
|
|
return makeTruthVal(LPTMD == RPTMD, resultTy);
|
|
|
|
case BO_NE:
|
|
|
|
return makeTruthVal(LPTMD != RPTMD, resultTy);
|
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
}
|
2009-06-26 08:25:05 +08:00
|
|
|
case nonloc::LocAsIntegerKind: {
|
2013-02-20 13:52:05 +08:00
|
|
|
Loc lhsL = lhs.castAs<nonloc::LocAsInteger>().getLoc();
|
2009-06-26 08:25:05 +08:00
|
|
|
switch (rhs.getSubKind()) {
|
|
|
|
case nonloc::LocAsIntegerKind:
|
2017-08-29 05:38:14 +08:00
|
|
|
// FIXME: at the moment the implementation
|
2017-08-29 05:15:21 +08:00
|
|
|
// of modeling "pointers as integers" is not complete.
|
|
|
|
if (!BinaryOperator::isComparisonOp(op))
|
2017-08-29 05:38:14 +08:00
|
|
|
return UnknownVal();
|
2010-12-02 05:57:22 +08:00
|
|
|
return evalBinOpLL(state, op, lhsL,
|
2013-02-20 13:52:05 +08:00
|
|
|
rhs.castAs<nonloc::LocAsInteger>().getLoc(),
|
2009-09-09 23:08:12 +08:00
|
|
|
resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
case nonloc::ConcreteIntKind: {
|
2017-08-29 05:38:14 +08:00
|
|
|
// FIXME: at the moment the implementation
|
2017-08-29 05:15:21 +08:00
|
|
|
// of modeling "pointers as integers" is not complete.
|
|
|
|
if (!BinaryOperator::isComparisonOp(op))
|
|
|
|
return UnknownVal();
|
2009-06-26 08:25:05 +08:00
|
|
|
// Transform the integer into a location and compare.
|
2017-03-28 23:57:12 +08:00
|
|
|
// FIXME: This only makes sense for comparisons. If we want to, say,
|
|
|
|
// add 1 to a LocAsInteger, we'd better unpack the Loc and add to it,
|
|
|
|
// then pack it back into a LocAsInteger.
|
2013-02-20 13:52:05 +08:00
|
|
|
llvm::APSInt i = rhs.castAs<nonloc::ConcreteInt>().getValue();
|
2012-05-08 11:26:58 +08:00
|
|
|
BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i);
|
2010-12-02 15:49:45 +08:00
|
|
|
return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
default:
|
2009-06-26 08:25:05 +08:00
|
|
|
switch (op) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
|
|
|
// This case also handles pointer arithmetic.
|
2018-08-30 04:28:54 +08:00
|
|
|
return makeSymExprValNN(op, InputLHS, InputRHS, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
case nonloc::ConcreteIntKind: {
|
2013-02-20 13:52:05 +08:00
|
|
|
llvm::APSInt LHSValue = lhs.castAs<nonloc::ConcreteInt>().getValue();
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
// If we're dealing with two known constants, just perform the operation.
|
|
|
|
if (const llvm::APSInt *KnownRHSValue = getKnownValue(state, rhs)) {
|
|
|
|
llvm::APSInt RHSValue = *KnownRHSValue;
|
|
|
|
if (BinaryOperator::isComparisonOp(op)) {
|
|
|
|
// We're looking for a type big enough to compare the two values.
|
2012-05-08 11:26:58 +08:00
|
|
|
// FIXME: This is not correct. char + short will result in a promotion
|
|
|
|
// to int. Unfortunately we have lost types by this point.
|
|
|
|
APSIntType CompareType = std::max(APSIntType(LHSValue),
|
|
|
|
APSIntType(RHSValue));
|
|
|
|
CompareType.apply(LHSValue);
|
|
|
|
CompareType.apply(RHSValue);
|
2012-05-07 07:40:02 +08:00
|
|
|
} else if (!BinaryOperator::isShiftOp(op)) {
|
2012-05-08 11:26:58 +08:00
|
|
|
APSIntType IntType = BasicVals.getAPSIntType(resultTy);
|
|
|
|
IntType.apply(LHSValue);
|
|
|
|
IntType.apply(RHSValue);
|
2011-06-15 12:55:49 +08:00
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
const llvm::APSInt *Result =
|
|
|
|
BasicVals.evalAPSInt(op, LHSValue, RHSValue);
|
|
|
|
if (!Result)
|
|
|
|
return UndefinedVal();
|
|
|
|
|
|
|
|
return nonloc::ConcreteInt(*Result);
|
2011-06-15 12:55:49 +08:00
|
|
|
}
|
|
|
|
|
2012-05-07 07:40:02 +08:00
|
|
|
// Swap the left and right sides and flip the operator if doing so
|
|
|
|
// allows us to better reason about the expression (this is a form
|
|
|
|
// of expression canonicalization).
|
|
|
|
// While we're at it, catch some special cases for non-commutative ops.
|
|
|
|
switch (op) {
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
2013-03-23 09:21:20 +08:00
|
|
|
op = BinaryOperator::reverseComparisonOp(op);
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-05-07 07:40:02 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
|
|
|
case BO_Add:
|
|
|
|
case BO_Mul:
|
|
|
|
case BO_And:
|
|
|
|
case BO_Xor:
|
|
|
|
case BO_Or:
|
|
|
|
std::swap(lhs, rhs);
|
|
|
|
continue;
|
|
|
|
case BO_Shr:
|
|
|
|
// (~0)>>a
|
|
|
|
if (LHSValue.isAllOnesValue() && LHSValue.isSigned())
|
|
|
|
return evalCastFromNonLoc(lhs, resultTy);
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2012-05-07 07:40:02 +08:00
|
|
|
case BO_Shl:
|
|
|
|
// 0<<a and 0>>a
|
|
|
|
if (LHSValue == 0)
|
|
|
|
return evalCastFromNonLoc(lhs, resultTy);
|
2018-08-30 04:28:54 +08:00
|
|
|
return makeSymExprValNN(op, InputLHS, InputRHS, resultTy);
|
2012-05-07 07:40:02 +08:00
|
|
|
default:
|
2018-08-30 04:28:54 +08:00
|
|
|
return makeSymExprValNN(op, InputLHS, InputRHS, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case nonloc::SymbolValKind: {
|
2012-05-07 07:40:02 +08:00
|
|
|
// We only handle LHS as simple symbols or SymIntExprs.
|
2013-02-20 13:52:05 +08:00
|
|
|
SymbolRef Sym = lhs.castAs<nonloc::SymbolVal>().getSymbol();
|
2011-12-06 02:58:30 +08:00
|
|
|
|
|
|
|
// LHS is a symbolic expression.
|
2012-05-07 07:40:02 +08:00
|
|
|
if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(Sym)) {
|
2011-12-06 02:58:30 +08:00
|
|
|
|
|
|
|
// Is this a logical not? (!x is represented as x == 0.)
|
|
|
|
if (op == BO_EQ && rhs.isZeroConstant()) {
|
|
|
|
// We know how to negate certain expressions. Simplify them here.
|
|
|
|
|
|
|
|
BinaryOperator::Opcode opc = symIntExpr->getOpcode();
|
|
|
|
switch (opc) {
|
|
|
|
default:
|
|
|
|
// We don't know how to negate this operation.
|
|
|
|
// Just handle it as if it were a normal comparison to 0.
|
|
|
|
break;
|
|
|
|
case BO_LAnd:
|
|
|
|
case BO_LOr:
|
|
|
|
llvm_unreachable("Logical operators handled by branching logic.");
|
|
|
|
case BO_Assign:
|
|
|
|
case BO_MulAssign:
|
|
|
|
case BO_DivAssign:
|
|
|
|
case BO_RemAssign:
|
|
|
|
case BO_AddAssign:
|
|
|
|
case BO_SubAssign:
|
|
|
|
case BO_ShlAssign:
|
|
|
|
case BO_ShrAssign:
|
|
|
|
case BO_AndAssign:
|
|
|
|
case BO_XorAssign:
|
|
|
|
case BO_OrAssign:
|
|
|
|
case BO_Comma:
|
|
|
|
llvm_unreachable("'=' and ',' operators handled by ExprEngine.");
|
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
llvm_unreachable("Pointer arithmetic not handled here.");
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
2013-05-02 02:19:59 +08:00
|
|
|
assert(resultTy->isBooleanType() ||
|
|
|
|
resultTy == getConditionType());
|
|
|
|
assert(symIntExpr->getType()->isBooleanType() ||
|
|
|
|
getContext().hasSameUnqualifiedType(symIntExpr->getType(),
|
|
|
|
getConditionType()));
|
2011-12-06 02:58:30 +08:00
|
|
|
// Negate the comparison and make a value.
|
2013-03-23 09:21:20 +08:00
|
|
|
opc = BinaryOperator::negateComparisonOp(opc);
|
2011-12-06 02:58:30 +08:00
|
|
|
return makeNonLoc(symIntExpr->getLHS(), opc,
|
|
|
|
symIntExpr->getRHS(), resultTy);
|
2009-10-06 11:44:49 +08:00
|
|
|
}
|
2009-10-06 09:39:48 +08:00
|
|
|
}
|
2010-08-10 04:31:57 +08:00
|
|
|
|
2011-12-06 02:58:30 +08:00
|
|
|
// For now, only handle expressions whose RHS is a constant.
|
2012-05-07 07:40:02 +08:00
|
|
|
if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs)) {
|
|
|
|
// If both the LHS and the current expression are additive,
|
|
|
|
// fold their constants and try again.
|
|
|
|
if (BinaryOperator::isAdditiveOp(op)) {
|
|
|
|
BinaryOperator::Opcode lop = symIntExpr->getOpcode();
|
|
|
|
if (BinaryOperator::isAdditiveOp(lop)) {
|
|
|
|
// Convert the two constants to a common type, then combine them.
|
|
|
|
|
|
|
|
// resultTy may not be the best type to convert to, but it's
|
|
|
|
// probably the best choice in expressions with mixed type
|
|
|
|
// (such as x+1U+2LL). The rules for implicit conversions should
|
|
|
|
// choose a reasonable type to preserve the expression, and will
|
|
|
|
// at least match how the value is going to be used.
|
2012-05-08 11:26:58 +08:00
|
|
|
APSIntType IntType = BasicVals.getAPSIntType(resultTy);
|
|
|
|
const llvm::APSInt &first = IntType.convert(symIntExpr->getRHS());
|
|
|
|
const llvm::APSInt &second = IntType.convert(*RHSValue);
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
const llvm::APSInt *newRHS;
|
|
|
|
if (lop == op)
|
|
|
|
newRHS = BasicVals.evalAPSInt(BO_Add, first, second);
|
|
|
|
else
|
|
|
|
newRHS = BasicVals.evalAPSInt(BO_Sub, first, second);
|
|
|
|
|
|
|
|
assert(newRHS && "Invalid operation despite common type!");
|
|
|
|
rhs = nonloc::ConcreteInt(*newRHS);
|
|
|
|
lhs = nonloc::SymbolVal(symIntExpr->getLHS());
|
|
|
|
op = lop;
|
|
|
|
continue;
|
|
|
|
}
|
2010-08-10 04:31:57 +08:00
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
|
|
|
// Otherwise, make a SymIntExpr out of the expression.
|
|
|
|
return MakeSymIntVal(symIntExpr, op, *RHSValue, resultTy);
|
2010-08-10 04:31:57 +08:00
|
|
|
}
|
2013-03-23 09:21:05 +08:00
|
|
|
}
|
2010-08-10 04:31:57 +08:00
|
|
|
|
2013-03-23 09:21:05 +08:00
|
|
|
// Does the symbolic expression simplify to a constant?
|
|
|
|
// If so, "fold" the constant by setting 'lhs' to a ConcreteInt
|
|
|
|
// and try again.
|
2017-04-13 15:20:04 +08:00
|
|
|
SVal simplifiedLhs = simplifySVal(state, lhs);
|
|
|
|
if (simplifiedLhs != lhs)
|
|
|
|
if (auto simplifiedLhsAsNonLoc = simplifiedLhs.getAs<NonLoc>()) {
|
|
|
|
lhs = *simplifiedLhsAsNonLoc;
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-07 07:40:02 +08:00
|
|
|
|
2013-03-23 09:21:05 +08:00
|
|
|
// Is the RHS a constant?
|
|
|
|
if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs))
|
|
|
|
return MakeSymIntVal(Sym, op, *RHSValue, resultTy);
|
|
|
|
|
2018-04-11 14:21:12 +08:00
|
|
|
if (Optional<NonLoc> V = tryRearrange(state, op, lhs, rhs, resultTy))
|
|
|
|
return *V;
|
|
|
|
|
2012-05-07 07:40:02 +08:00
|
|
|
// Give up -- this is not a symbolic expression we can handle.
|
2018-08-30 04:28:54 +08:00
|
|
|
return makeSymExprValNN(op, InputLHS, InputRHS, resultTy);
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:31:43 +08:00
|
|
|
static SVal evalBinOpFieldRegionFieldRegion(const FieldRegion *LeftFR,
|
|
|
|
const FieldRegion *RightFR,
|
|
|
|
BinaryOperator::Opcode op,
|
|
|
|
QualType resultTy,
|
|
|
|
SimpleSValBuilder &SVB) {
|
|
|
|
// Only comparisons are meaningful here!
|
|
|
|
if (!BinaryOperator::isComparisonOp(op))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Next, see if the two FRs have the same super-region.
|
|
|
|
// FIXME: This doesn't handle casts yet, and simply stripping the casts
|
|
|
|
// doesn't help.
|
|
|
|
if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const FieldDecl *LeftFD = LeftFR->getDecl();
|
|
|
|
const FieldDecl *RightFD = RightFR->getDecl();
|
|
|
|
const RecordDecl *RD = LeftFD->getParent();
|
|
|
|
|
|
|
|
// Make sure the two FRs are from the same kind of record. Just in case!
|
|
|
|
// FIXME: This is probably where inheritance would be a problem.
|
|
|
|
if (RD != RightFD->getParent())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// We know for sure that the two fields are not the same, since that
|
|
|
|
// would have given us the same SVal.
|
|
|
|
if (op == BO_EQ)
|
|
|
|
return SVB.makeTruthVal(false, resultTy);
|
|
|
|
if (op == BO_NE)
|
|
|
|
return SVB.makeTruthVal(true, resultTy);
|
|
|
|
|
|
|
|
// Iterate through the fields and see which one comes first.
|
|
|
|
// [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
|
|
|
|
// members and the units in which bit-fields reside have addresses that
|
|
|
|
// increase in the order in which they are declared."
|
|
|
|
bool leftFirst = (op == BO_LT || op == BO_LE);
|
2014-03-09 04:12:42 +08:00
|
|
|
for (const auto *I : RD->fields()) {
|
|
|
|
if (I == LeftFD)
|
2013-05-29 01:31:43 +08:00
|
|
|
return SVB.makeTruthVal(leftFirst, resultTy);
|
2014-03-09 04:12:42 +08:00
|
|
|
if (I == RightFD)
|
2013-05-29 01:31:43 +08:00
|
|
|
return SVB.makeTruthVal(!leftFirst, resultTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Fields not found in parent record's definition");
|
|
|
|
}
|
|
|
|
|
2010-06-28 16:26:15 +08:00
|
|
|
// FIXME: all this logic will change if/when we have MemRegion::getLocation().
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SimpleSValBuilder::evalBinOpLL(ProgramStateRef state,
|
2010-06-28 16:26:15 +08:00
|
|
|
BinaryOperator::Opcode op,
|
|
|
|
Loc lhs, Loc rhs,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType resultTy) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// Only comparisons and subtractions are valid operations on two pointers.
|
|
|
|
// See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
|
2010-12-02 05:57:22 +08:00
|
|
|
// However, if a pointer is casted to an integer, evalBinOpNN may end up
|
2010-06-30 09:35:20 +08:00
|
|
|
// calling this function with another operation (PR7527). We don't attempt to
|
|
|
|
// model this for now, but it could be useful, particularly when the
|
|
|
|
// "location" is actually an integer value that's been passed through a void*.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub))
|
2010-06-30 09:35:20 +08:00
|
|
|
return UnknownVal();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
// Special cases for when both sides are identical.
|
|
|
|
if (lhs == rhs) {
|
|
|
|
switch (op) {
|
2009-06-26 08:25:05 +08:00
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unimplemented operation for two identical values");
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeZeroVal(resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lhs.getSubKind()) {
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Ordering not implemented for this Loc.");
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
// The only thing we know about labels is that they're non-null.
|
|
|
|
if (rhs.isZeroConstant()) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(lhs, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_LT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// There may be two labels for the same location, and a function region may
|
|
|
|
// have the same address as a label at the start of the function (depending
|
|
|
|
// on the ABI).
|
|
|
|
// FIXME: we can probably do a comparison against other MemRegions, though.
|
|
|
|
// FIXME: is there a way to tell if two labels refer to the same location?
|
2015-09-08 11:50:52 +08:00
|
|
|
return UnknownVal();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
case loc::ConcreteIntKind: {
|
|
|
|
// If one of the operands is a symbol and the other is a constant,
|
|
|
|
// build an expression for use by the constraint manager.
|
|
|
|
if (SymbolRef rSym = rhs.getAsLocSymbol()) {
|
2017-07-13 05:43:42 +08:00
|
|
|
// We can only build expressions with symbols on the left,
|
|
|
|
// so we need a reversible operator.
|
2017-12-14 23:16:18 +08:00
|
|
|
if (!BinaryOperator::isComparisonOp(op) || op == BO_Cmp)
|
2017-07-13 05:43:42 +08:00
|
|
|
return UnknownVal();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
2017-07-13 05:43:42 +08:00
|
|
|
const llvm::APSInt &lVal = lhs.castAs<loc::ConcreteInt>().getValue();
|
2013-03-23 09:21:20 +08:00
|
|
|
op = BinaryOperator::reverseComparisonOp(op);
|
|
|
|
return makeNonLoc(rSym, op, lVal, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If both operands are constants, just perform the operation.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) {
|
2013-02-20 13:52:05 +08:00
|
|
|
SVal ResultVal =
|
|
|
|
lhs.castAs<loc::ConcreteInt>().evalBinOp(BasicVals, op, *rInt);
|
2013-03-23 09:21:29 +08:00
|
|
|
if (Optional<NonLoc> Result = ResultVal.getAs<NonLoc>())
|
|
|
|
return evalCastFromNonLoc(*Result, resultTy);
|
|
|
|
|
|
|
|
assert(!ResultVal.getAs<Loc>() && "Loc-Loc ops should not produce Locs");
|
|
|
|
return UnknownVal();
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case comparisons against NULL.
|
|
|
|
// This must come after the test if the RHS is a symbol, which is used to
|
|
|
|
// build constraints. The address of any non-symbolic region is guaranteed
|
|
|
|
// to be non-NULL, as is any label.
|
2013-02-20 13:52:05 +08:00
|
|
|
assert(rhs.getAs<loc::MemRegionVal>() || rhs.getAs<loc::GotoLabel>());
|
2010-06-28 16:26:15 +08:00
|
|
|
if (lhs.isZeroConstant()) {
|
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_LE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparing an arbitrary integer to a region or label address is
|
|
|
|
// completely unknowable.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2016-01-13 21:49:29 +08:00
|
|
|
case loc::MemRegionValKind: {
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// If one of the operands is a symbol and the other is a constant,
|
|
|
|
// build an expression for use by the constraint manager.
|
2017-09-26 03:32:33 +08:00
|
|
|
if (SymbolRef lSym = lhs.getAsLocSymbol(true)) {
|
|
|
|
if (BinaryOperator::isComparisonOp(op))
|
|
|
|
return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
// Special case comparisons to NULL.
|
|
|
|
// This must come after the test if the LHS is a symbol, which is used to
|
|
|
|
// build constraints. The address of any non-symbolic region is guaranteed
|
|
|
|
// to be non-NULL.
|
|
|
|
if (rInt->isZeroConstant()) {
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
if (op == BO_Sub)
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(lhs, resultTy);
|
[analyzer] Add support for testing the presence of weak functions.
When casting the address of a FunctionTextRegion to bool, or when adding
constraints to such an address, use a stand-in symbol to represent the
presence or absence of the function if the function is weakly linked.
This is groundwork for possible simple availability testing checks, and
can already catch mistakes involving inverted null checks for
weakly-linked functions.
Currently, the implementation reuses the "extent" symbols, originally created
for tracking the size of a malloc region. Since FunctionTextRegions cannot
be dereferenced, the extent symbol will never be used for anything else.
Still, this probably deserves a refactoring in the future.
This patch does not attempt to support testing the presence of weak
/variables/ (global variables), which would likely require much more of
a change and a generalization of "region structure metadata", like the
current "extents", vs. "region contents metadata", like CStringChecker's
"string length".
Patch by Richard <tarka.t.otter@googlemail.com>!
llvm-svn: 189492
2013-08-29 01:07:04 +08:00
|
|
|
|
|
|
|
if (BinaryOperator::isComparisonOp(op)) {
|
|
|
|
QualType boolType = getContext().BoolTy;
|
|
|
|
NonLoc l = evalCastFromLoc(lhs, boolType).castAs<NonLoc>();
|
|
|
|
NonLoc r = makeTruthVal(false, boolType).castAs<NonLoc>();
|
|
|
|
return evalBinOpNN(state, op, l, r, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparing a region to an arbitrary integer is completely unknowable.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get both values as regions, if possible.
|
|
|
|
const MemRegion *LeftMR = lhs.getAsRegion();
|
2016-01-13 21:49:29 +08:00
|
|
|
assert(LeftMR && "MemRegionValKind SVal doesn't have a region!");
|
2010-06-28 16:26:15 +08:00
|
|
|
|
|
|
|
const MemRegion *RightMR = rhs.getAsRegion();
|
|
|
|
if (!RightMR)
|
|
|
|
// The RHS is probably a label, which in theory could address a region.
|
|
|
|
// FIXME: we can probably make a more useful statement about non-code
|
|
|
|
// regions, though.
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
const MemRegion *LeftBase = LeftMR->getBaseRegion();
|
|
|
|
const MemRegion *RightBase = RightMR->getBaseRegion();
|
2013-03-23 09:21:05 +08:00
|
|
|
const MemSpaceRegion *LeftMS = LeftBase->getMemorySpace();
|
|
|
|
const MemSpaceRegion *RightMS = RightBase->getMemorySpace();
|
|
|
|
const MemSpaceRegion *UnknownMS = MemMgr.getUnknownRegion();
|
2012-06-07 11:57:32 +08:00
|
|
|
|
|
|
|
// If the two regions are from different known memory spaces they cannot be
|
|
|
|
// equal. Also, assume that no symbolic region (whose memory space is
|
|
|
|
// unknown) is on the stack.
|
|
|
|
if (LeftMS != RightMS &&
|
|
|
|
((LeftMS != UnknownMS && RightMS != UnknownMS) ||
|
|
|
|
(isa<StackSpaceRegion>(LeftMS) || isa<StackSpaceRegion>(RightMS)))) {
|
2010-06-28 16:26:15 +08:00
|
|
|
switch (op) {
|
|
|
|
default:
|
|
|
|
return UnknownVal();
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(false, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(true, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
// If both values wrap regions, see if they're from different base regions.
|
|
|
|
// Note, heap base symbolic regions are assumed to not alias with
|
|
|
|
// each other; for example, we assume that malloc returns different address
|
|
|
|
// on each invocation.
|
2016-11-01 01:27:26 +08:00
|
|
|
// FIXME: ObjC object pointers always reside on the heap, but currently
|
|
|
|
// we treat their memory space as unknown, because symbolic pointers
|
|
|
|
// to ObjC objects may alias. There should be a way to construct
|
|
|
|
// possibly-aliasing heap-based regions. For instance, MacOSXApiChecker
|
|
|
|
// guesses memory space for ObjC object pointers manually instead of
|
|
|
|
// relying on us.
|
2012-06-07 11:57:32 +08:00
|
|
|
if (LeftBase != RightBase &&
|
|
|
|
((!isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) ||
|
2012-06-08 04:18:08 +08:00
|
|
|
(isa<HeapSpaceRegion>(LeftMS) || isa<HeapSpaceRegion>(RightMS))) ){
|
2012-03-06 07:06:19 +08:00
|
|
|
switch (op) {
|
2012-06-07 11:57:32 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
case BO_EQ:
|
|
|
|
return makeTruthVal(false, resultTy);
|
|
|
|
case BO_NE:
|
|
|
|
return makeTruthVal(true, resultTy);
|
2012-03-06 07:06:19 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
|
2013-05-29 01:31:43 +08:00
|
|
|
// Handle special cases for when both regions are element regions.
|
|
|
|
const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
|
|
|
|
const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR);
|
|
|
|
if (RightER && LeftER) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// Next, see if the two ERs have the same super-region and matching types.
|
|
|
|
// FIXME: This should do something useful even if the types don't match,
|
|
|
|
// though if both indexes are constant the RegionRawOffset path will
|
|
|
|
// give the correct answer.
|
|
|
|
if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
|
|
|
|
LeftER->getElementType() == RightER->getElementType()) {
|
|
|
|
// Get the left index and cast it to the correct type.
|
|
|
|
// If the index is unknown or undefined, bail out here.
|
|
|
|
SVal LeftIndexVal = LeftER->getIndex();
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> LeftIndex = LeftIndexVal.getAs<NonLoc>();
|
2010-06-28 16:26:15 +08:00
|
|
|
if (!LeftIndex)
|
|
|
|
return UnknownVal();
|
2012-11-29 08:50:20 +08:00
|
|
|
LeftIndexVal = evalCastFromNonLoc(*LeftIndex, ArrayIndexTy);
|
2013-02-20 13:52:05 +08:00
|
|
|
LeftIndex = LeftIndexVal.getAs<NonLoc>();
|
2010-06-28 16:26:15 +08:00
|
|
|
if (!LeftIndex)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Do the same for the right index.
|
|
|
|
SVal RightIndexVal = RightER->getIndex();
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<NonLoc> RightIndex = RightIndexVal.getAs<NonLoc>();
|
2010-06-28 16:26:15 +08:00
|
|
|
if (!RightIndex)
|
|
|
|
return UnknownVal();
|
2012-11-29 08:50:20 +08:00
|
|
|
RightIndexVal = evalCastFromNonLoc(*RightIndex, ArrayIndexTy);
|
2013-02-20 13:52:05 +08:00
|
|
|
RightIndex = RightIndexVal.getAs<NonLoc>();
|
2010-06-28 16:26:15 +08:00
|
|
|
if (!RightIndex)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// Actually perform the operation.
|
2010-12-02 05:57:22 +08:00
|
|
|
// evalBinOpNN expects the two indexes to already be the right type.
|
|
|
|
return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
2013-05-29 01:31:43 +08:00
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
|
2013-05-29 01:31:43 +08:00
|
|
|
// Special handling of the FieldRegions, even with symbolic offsets.
|
|
|
|
const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
|
|
|
|
const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR);
|
|
|
|
if (RightFR && LeftFR) {
|
|
|
|
SVal R = evalBinOpFieldRegionFieldRegion(LeftFR, RightFR, op, resultTy,
|
|
|
|
*this);
|
|
|
|
if (!R.isUnknown())
|
|
|
|
return R;
|
|
|
|
}
|
2010-06-28 16:26:15 +08:00
|
|
|
|
2013-05-29 01:31:43 +08:00
|
|
|
// Compare the regions using the raw offsets.
|
|
|
|
RegionOffset LeftOffset = LeftMR->getAsOffset();
|
|
|
|
RegionOffset RightOffset = RightMR->getAsOffset();
|
2010-06-28 16:26:15 +08:00
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
if (LeftOffset.getRegion() != nullptr &&
|
2013-05-29 01:31:43 +08:00
|
|
|
LeftOffset.getRegion() == RightOffset.getRegion() &&
|
|
|
|
!LeftOffset.hasSymbolicOffset() && !RightOffset.hasSymbolicOffset()) {
|
|
|
|
int64_t left = LeftOffset.getOffset();
|
|
|
|
int64_t right = RightOffset.getOffset();
|
|
|
|
|
|
|
|
switch (op) {
|
2010-06-28 16:26:15 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left < right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GT:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left > right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left <= right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left >= right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left == right, resultTy);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeTruthVal(left != right, resultTy);
|
2010-06-28 16:26:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-23 09:21:05 +08:00
|
|
|
// At this point we're not going to get a good answer, but we can try
|
|
|
|
// conjuring an expression instead.
|
|
|
|
SymbolRef LHSSym = lhs.getAsLocSymbol();
|
|
|
|
SymbolRef RHSSym = rhs.getAsLocSymbol();
|
|
|
|
if (LHSSym && RHSSym)
|
|
|
|
return makeNonLoc(LHSSym, op, RHSSym, resultTy);
|
|
|
|
|
2010-06-28 16:26:15 +08:00
|
|
|
// If we get here, we have no way of comparing the regions.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state,
|
2009-06-26 08:25:05 +08:00
|
|
|
BinaryOperator::Opcode op,
|
2009-09-09 23:08:12 +08:00
|
|
|
Loc lhs, NonLoc rhs, QualType resultTy) {
|
2016-12-16 05:27:06 +08:00
|
|
|
if (op >= BO_PtrMemD && op <= BO_PtrMemI) {
|
|
|
|
if (auto PTMSV = rhs.getAs<nonloc::PointerToMember>()) {
|
|
|
|
if (PTMSV->isNullMemberPointer())
|
|
|
|
return UndefinedVal();
|
|
|
|
if (const FieldDecl *FD = PTMSV->getDeclAs<FieldDecl>()) {
|
|
|
|
SVal Result = lhs;
|
|
|
|
|
|
|
|
for (const auto &I : *PTMSV)
|
|
|
|
Result = StateMgr.getStoreManager().evalDerivedToBase(
|
|
|
|
Result, I->getType(),I->isVirtual());
|
|
|
|
return state->getLValue(FD, Result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rhs;
|
|
|
|
}
|
|
|
|
|
2013-07-02 09:37:40 +08:00
|
|
|
assert(!BinaryOperator::isComparisonOp(op) &&
|
|
|
|
"arguments to comparison ops must be of the same type");
|
|
|
|
|
2010-12-24 16:39:33 +08:00
|
|
|
// Special case: rhs is a zero constant.
|
|
|
|
if (rhs.isZeroConstant())
|
|
|
|
return lhs;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-10-10 19:01:49 +08:00
|
|
|
// Perserve the null pointer so that it can be found by the DerefChecker.
|
|
|
|
if (lhs.isZeroConstant())
|
|
|
|
return lhs;
|
|
|
|
|
2010-09-03 09:07:06 +08:00
|
|
|
// We are dealing with pointer arithmetic.
|
|
|
|
|
|
|
|
// Handle pointer arithmetic on constant values.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> rhsInt = rhs.getAs<nonloc::ConcreteInt>()) {
|
|
|
|
if (Optional<loc::ConcreteInt> lhsInt = lhs.getAs<loc::ConcreteInt>()) {
|
2010-09-03 09:07:06 +08:00
|
|
|
const llvm::APSInt &leftI = lhsInt->getValue();
|
|
|
|
assert(leftI.isUnsigned());
|
|
|
|
llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true);
|
|
|
|
|
|
|
|
// Convert the bitwidth of rightI. This should deal with overflow
|
|
|
|
// since we are dealing with concrete values.
|
2010-12-07 16:25:34 +08:00
|
|
|
rightI = rightI.extOrTrunc(leftI.getBitWidth());
|
2010-09-03 09:07:06 +08:00
|
|
|
|
|
|
|
// Offset the increment by the pointer size.
|
|
|
|
llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true);
|
2017-10-10 19:01:49 +08:00
|
|
|
QualType pointeeType = resultTy->getPointeeType();
|
|
|
|
Multiplicand = getContext().getTypeSizeInChars(pointeeType).getQuantity();
|
2010-09-03 09:07:06 +08:00
|
|
|
rightI *= Multiplicand;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-09-03 09:07:06 +08:00
|
|
|
// Compute the adjusted pointer.
|
|
|
|
switch (op) {
|
|
|
|
case BO_Add:
|
|
|
|
rightI = leftI + rightI;
|
|
|
|
break;
|
|
|
|
case BO_Sub:
|
|
|
|
rightI = leftI - rightI;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid pointer arithmetic operation");
|
|
|
|
}
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::ConcreteInt(getBasicValueFactory().getValue(rightI));
|
2010-09-03 09:07:06 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-24 16:39:33 +08:00
|
|
|
// Handle cases where 'lhs' is a region.
|
|
|
|
if (const MemRegion *region = lhs.getAsRegion()) {
|
2013-02-20 13:52:05 +08:00
|
|
|
rhs = convertToArrayIndex(rhs).castAs<NonLoc>();
|
2010-12-24 16:39:33 +08:00
|
|
|
SVal index = UnknownVal();
|
2017-04-13 17:56:07 +08:00
|
|
|
const SubRegion *superR = nullptr;
|
2017-03-28 23:57:12 +08:00
|
|
|
// We need to know the type of the pointer in order to add an integer to it.
|
|
|
|
// Depending on the type, different amount of bytes is added.
|
2010-12-24 16:39:33 +08:00
|
|
|
QualType elementType;
|
|
|
|
|
|
|
|
if (const ElementRegion *elemReg = dyn_cast<ElementRegion>(region)) {
|
2011-04-12 11:49:37 +08:00
|
|
|
assert(op == BO_Add || op == BO_Sub);
|
|
|
|
index = evalBinOpNN(state, op, elemReg->getIndex(), rhs,
|
2010-12-24 16:39:33 +08:00
|
|
|
getArrayIndexType());
|
2017-04-13 17:56:07 +08:00
|
|
|
superR = cast<SubRegion>(elemReg->getSuperRegion());
|
2010-12-24 16:39:33 +08:00
|
|
|
elementType = elemReg->getElementType();
|
|
|
|
}
|
|
|
|
else if (isa<SubRegion>(region)) {
|
2015-09-19 03:13:22 +08:00
|
|
|
assert(op == BO_Add || op == BO_Sub);
|
|
|
|
index = (op == BO_Add) ? rhs : evalMinus(rhs);
|
2017-04-13 17:56:07 +08:00
|
|
|
superR = cast<SubRegion>(region);
|
2017-03-28 23:57:12 +08:00
|
|
|
// TODO: Is this actually reliable? Maybe improving our MemRegion
|
|
|
|
// hierarchy to provide typed regions for all non-void pointers would be
|
|
|
|
// better. For instance, we cannot extend this towards LocAsInteger
|
|
|
|
// operations, where result type of the expression is integer.
|
2012-09-06 01:11:22 +08:00
|
|
|
if (resultTy->isAnyPointerType())
|
|
|
|
elementType = resultTy->getPointeeType();
|
2010-12-24 16:39:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-18 06:40:36 +08:00
|
|
|
// Represent arithmetic on void pointers as arithmetic on char pointers.
|
|
|
|
// It is fine when a TypedValueRegion of char value type represents
|
|
|
|
// a void pointer. Note that arithmetic on void pointers is a GCC extension.
|
|
|
|
if (elementType->isVoidType())
|
|
|
|
elementType = getContext().CharTy;
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<NonLoc> indexV = index.getAs<NonLoc>()) {
|
2010-12-24 16:39:33 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV,
|
|
|
|
superR, getContext()));
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
return UnknownVal();
|
2009-06-26 08:25:05 +08:00
|
|
|
}
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
const llvm::APSInt *SimpleSValBuilder::getKnownValue(ProgramStateRef state,
|
2010-07-04 08:00:41 +08:00
|
|
|
SVal V) {
|
2018-08-30 04:29:59 +08:00
|
|
|
V = simplifySVal(state, V);
|
2010-07-04 08:00:41 +08:00
|
|
|
if (V.isUnknownOrUndef())
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::ConcreteInt> X = V.getAs<loc::ConcreteInt>())
|
2010-07-04 08:00:41 +08:00
|
|
|
return &X->getValue();
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> X = V.getAs<nonloc::ConcreteInt>())
|
2010-07-04 08:00:41 +08:00
|
|
|
return &X->getValue();
|
|
|
|
|
|
|
|
if (SymbolRef Sym = V.getAsSymbol())
|
2012-09-08 06:31:01 +08:00
|
|
|
return state->getConstraintManager().getSymVal(state, Sym);
|
2010-07-04 08:00:41 +08:00
|
|
|
|
2017-07-13 05:43:42 +08:00
|
|
|
// FIXME: Add support for SymExprs.
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2010-07-04 08:00:41 +08:00
|
|
|
}
|
2017-04-13 15:20:04 +08:00
|
|
|
|
|
|
|
SVal SimpleSValBuilder::simplifySVal(ProgramStateRef State, SVal V) {
|
|
|
|
// For now, this function tries to constant-fold symbols inside a
|
|
|
|
// nonloc::SymbolVal, and does nothing else. More simplifications should
|
|
|
|
// be possible, such as constant-folding an index in an ElementRegion.
|
|
|
|
|
|
|
|
class Simplifier : public FullSValVisitor<Simplifier, SVal> {
|
|
|
|
ProgramStateRef State;
|
|
|
|
SValBuilder &SVB;
|
|
|
|
|
2018-06-01 01:27:28 +08:00
|
|
|
// Cache results for the lifetime of the Simplifier. Results change every
|
|
|
|
// time new constraints are added to the program state, which is the whole
|
|
|
|
// point of simplifying, and for that very reason it's pointless to maintain
|
|
|
|
// the same cache for the duration of the whole analysis.
|
|
|
|
llvm::DenseMap<SymbolRef, SVal> Cached;
|
|
|
|
|
2018-06-01 01:22:38 +08:00
|
|
|
static bool isUnchanged(SymbolRef Sym, SVal Val) {
|
|
|
|
return Sym == Val.getAsSymbol();
|
|
|
|
}
|
|
|
|
|
2018-08-01 03:29:25 +08:00
|
|
|
SVal cache(SymbolRef Sym, SVal V) {
|
|
|
|
Cached[Sym] = V;
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
SVal skip(SymbolRef Sym) {
|
|
|
|
return cache(Sym, SVB.makeSymbolVal(Sym));
|
|
|
|
}
|
|
|
|
|
2017-04-13 15:20:04 +08:00
|
|
|
public:
|
|
|
|
Simplifier(ProgramStateRef State)
|
|
|
|
: State(State), SVB(State->getStateManager().getSValBuilder()) {}
|
|
|
|
|
|
|
|
SVal VisitSymbolData(const SymbolData *S) {
|
2018-08-01 03:29:25 +08:00
|
|
|
// No cache here.
|
2017-04-13 15:20:04 +08:00
|
|
|
if (const llvm::APSInt *I =
|
2018-07-17 08:22:27 +08:00
|
|
|
SVB.getKnownValue(State, SVB.makeSymbolVal(S)))
|
2017-04-13 15:20:04 +08:00
|
|
|
return Loc::isLocType(S->getType()) ? (SVal)SVB.makeIntLocVal(*I)
|
|
|
|
: (SVal)SVB.makeIntVal(*I);
|
2018-06-01 01:22:38 +08:00
|
|
|
return SVB.makeSymbolVal(S);
|
2017-04-13 15:20:04 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 05:43:42 +08:00
|
|
|
// TODO: Support SymbolCast. Support IntSymExpr when/if we actually
|
|
|
|
// start producing them.
|
2017-04-13 15:20:04 +08:00
|
|
|
|
|
|
|
SVal VisitSymIntExpr(const SymIntExpr *S) {
|
2018-06-01 01:27:28 +08:00
|
|
|
auto I = Cached.find(S);
|
|
|
|
if (I != Cached.end())
|
|
|
|
return I->second;
|
|
|
|
|
2017-04-13 15:20:04 +08:00
|
|
|
SVal LHS = Visit(S->getLHS());
|
2018-08-01 03:29:25 +08:00
|
|
|
if (isUnchanged(S->getLHS(), LHS))
|
|
|
|
return skip(S);
|
|
|
|
|
2017-04-13 15:20:04 +08:00
|
|
|
SVal RHS;
|
|
|
|
// By looking at the APSInt in the right-hand side of S, we cannot
|
|
|
|
// figure out if it should be treated as a Loc or as a NonLoc.
|
|
|
|
// So make our guess by recalling that we cannot multiply pointers
|
|
|
|
// or compare a pointer to an integer.
|
|
|
|
if (Loc::isLocType(S->getLHS()->getType()) &&
|
|
|
|
BinaryOperator::isComparisonOp(S->getOpcode())) {
|
|
|
|
// The usual conversion of $sym to &SymRegion{$sym}, as they have
|
|
|
|
// the same meaning for Loc-type symbols, but the latter form
|
|
|
|
// is preferred in SVal computations for being Loc itself.
|
|
|
|
if (SymbolRef Sym = LHS.getAsSymbol()) {
|
|
|
|
assert(Loc::isLocType(Sym->getType()));
|
|
|
|
LHS = SVB.makeLoc(Sym);
|
|
|
|
}
|
|
|
|
RHS = SVB.makeIntLocVal(S->getRHS());
|
|
|
|
} else {
|
|
|
|
RHS = SVB.makeIntVal(S->getRHS());
|
|
|
|
}
|
2018-06-01 01:27:28 +08:00
|
|
|
|
2018-08-01 03:29:25 +08:00
|
|
|
return cache(
|
|
|
|
S, SVB.evalBinOp(State, S->getOpcode(), LHS, RHS, S->getType()));
|
2017-04-13 15:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal VisitSymSymExpr(const SymSymExpr *S) {
|
2018-06-01 01:27:28 +08:00
|
|
|
auto I = Cached.find(S);
|
|
|
|
if (I != Cached.end())
|
|
|
|
return I->second;
|
|
|
|
|
2018-08-01 03:26:34 +08:00
|
|
|
// For now don't try to simplify mixed Loc/NonLoc expressions
|
|
|
|
// because they often appear from LocAsInteger operations
|
|
|
|
// and we don't know how to combine a LocAsInteger
|
|
|
|
// with a concrete value.
|
|
|
|
if (Loc::isLocType(S->getLHS()->getType()) !=
|
2018-08-01 03:29:25 +08:00
|
|
|
Loc::isLocType(S->getRHS()->getType()))
|
|
|
|
return skip(S);
|
2018-08-01 03:26:34 +08:00
|
|
|
|
2017-04-13 15:20:04 +08:00
|
|
|
SVal LHS = Visit(S->getLHS());
|
|
|
|
SVal RHS = Visit(S->getRHS());
|
2018-08-01 03:29:25 +08:00
|
|
|
if (isUnchanged(S->getLHS(), LHS) && isUnchanged(S->getRHS(), RHS))
|
|
|
|
return skip(S);
|
|
|
|
|
|
|
|
return cache(
|
|
|
|
S, SVB.evalBinOp(State, S->getOpcode(), LHS, RHS, S->getType()));
|
2017-04-13 15:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal VisitSymExpr(SymbolRef S) { return nonloc::SymbolVal(S); }
|
|
|
|
|
|
|
|
SVal VisitMemRegion(const MemRegion *R) { return loc::MemRegionVal(R); }
|
|
|
|
|
|
|
|
SVal VisitNonLocSymbolVal(nonloc::SymbolVal V) {
|
|
|
|
// Simplification is much more costly than computing complexity.
|
|
|
|
// For high complexity, it may be not worth it.
|
|
|
|
return Visit(V.getSymbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
SVal VisitSVal(SVal V) { return V; }
|
|
|
|
};
|
|
|
|
|
2018-06-01 01:22:38 +08:00
|
|
|
// A crude way of preventing this function from calling itself from evalBinOp.
|
|
|
|
static bool isReentering = false;
|
|
|
|
if (isReentering)
|
|
|
|
return V;
|
|
|
|
|
|
|
|
isReentering = true;
|
|
|
|
SVal SimplifiedV = Simplifier(State).Visit(V);
|
|
|
|
isReentering = false;
|
|
|
|
|
|
|
|
return SimplifiedV;
|
2017-04-13 15:20:04 +08:00
|
|
|
}
|