2010-12-02 05:28:31 +08:00
|
|
|
// SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*-
|
2009-07-22 05:03:30 +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 SValBuilder, the base class for all (complete) SValBuilder
|
2009-07-22 05:03:30 +08:00
|
|
|
// implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2012-06-23 07:55:50 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
2009-07-22 05:03:30 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-07-22 05:03:30 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic SVal creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void SValBuilder::anchor() { }
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
|
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeNull();
|
|
|
|
|
2013-04-09 10:30:33 +08:00
|
|
|
if (type->isIntegralOrEnumerationType())
|
2011-03-01 08:45:32 +08:00
|
|
|
return makeIntVal(0, type);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
// FIXME: Handle floats.
|
|
|
|
// FIXME: Handle structs.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
const llvm::APSInt& rhs, QualType type) {
|
2010-12-02 15:49:45 +08:00
|
|
|
// The Environment ensures we always get a persistent APSInt in
|
|
|
|
// BasicValueFactory, so we don't need to get the APSInt from
|
|
|
|
// BasicValueFactory again.
|
2011-11-18 07:07:28 +08:00
|
|
|
assert(lhs);
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(!Loc::isLocType(type));
|
2011-12-06 02:58:30 +08:00
|
|
|
return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-12-11 07:36:51 +08:00
|
|
|
NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs,
|
|
|
|
BinaryOperator::Opcode op, const SymExpr *rhs,
|
|
|
|
QualType type) {
|
|
|
|
assert(rhs);
|
|
|
|
assert(!Loc::isLocType(type));
|
|
|
|
return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
const SymExpr *rhs, QualType type) {
|
2011-11-18 07:07:28 +08:00
|
|
|
assert(lhs && rhs);
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(!Loc::isLocType(type));
|
2011-12-06 02:58:30 +08:00
|
|
|
return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *operand,
|
|
|
|
QualType fromTy, QualType toTy) {
|
|
|
|
assert(operand);
|
|
|
|
assert(!Loc::isLocType(toTy));
|
|
|
|
return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
|
|
|
|
}
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SValBuilder::convertToArrayIndex(SVal val) {
|
|
|
|
if (val.isUnknownOrUndef())
|
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
// Common case: we have an appropriately sized integer.
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const llvm::APSInt& I = CI->getValue();
|
|
|
|
if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
|
2011-03-01 08:45:32 +08:00
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalCastFromNonLoc(val.castAs<NonLoc>(), ArrayIndexTy);
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
|
|
|
|
return makeTruthVal(boolean->getValue());
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal
|
2011-08-13 04:02:48 +08:00
|
|
|
SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = region->getValueType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getRegionValueSymbol(region);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:38:07 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
|
|
|
|
const Expr *Ex,
|
2012-08-22 14:26:06 +08:00
|
|
|
const LocationContext *LCtx,
|
2013-04-16 06:38:07 +08:00
|
|
|
unsigned Count) {
|
|
|
|
QualType T = Ex->getType();
|
|
|
|
|
|
|
|
// Compute the type of the result. If the expression is not an R-value, the
|
|
|
|
// result should be a location.
|
|
|
|
QualType ExType = Ex->getType();
|
|
|
|
if (Ex->isGLValue())
|
|
|
|
T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
|
|
|
|
|
|
|
|
return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-08-22 14:26:06 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
|
|
|
|
const Expr *expr,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
QualType type,
|
|
|
|
unsigned count) {
|
2011-03-01 08:45:32 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2012-08-22 14:26:06 +08:00
|
|
|
SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2012-03-10 09:34:17 +08:00
|
|
|
|
2012-08-22 14:26:06 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
QualType type,
|
|
|
|
unsigned visitCount) {
|
2012-03-10 09:34:17 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(type))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2012-08-22 14:26:06 +08:00
|
|
|
SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
|
2012-03-10 09:34:17 +08:00
|
|
|
|
|
|
|
if (Loc::isLocType(type))
|
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:32 +08:00
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
unsigned VisitCount) {
|
|
|
|
QualType T = E->getType();
|
|
|
|
assert(Loc::isLocType(T));
|
|
|
|
assert(SymbolManager::canSymbolicate(T));
|
|
|
|
|
2012-08-22 14:26:06 +08:00
|
|
|
SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount);
|
2012-06-07 11:57:32 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
|
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
|
|
|
|
const MemRegion *region,
|
|
|
|
const Expr *expr, QualType type,
|
|
|
|
unsigned count) {
|
|
|
|
assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym =
|
|
|
|
SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
|
2011-08-13 04:02:48 +08:00
|
|
|
const TypedValueRegion *region) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = region->getValueType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
|
2011-03-01 08:45:32 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getFunctionTextRegion(func));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
|
|
|
|
CanQualType locTy,
|
2013-11-20 08:11:42 +08:00
|
|
|
const LocationContext *locContext,
|
|
|
|
unsigned blockCount) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const BlockTextRegion *BC =
|
2011-10-24 09:32:45 +08:00
|
|
|
MemMgr.getBlockTextRegion(block, locTy, locContext->getAnalysisDeclContext());
|
2013-11-20 08:11:42 +08:00
|
|
|
const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext,
|
|
|
|
blockCount);
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(BD);
|
|
|
|
}
|
|
|
|
|
2012-06-23 07:55:50 +08:00
|
|
|
/// Return a memory region for the 'this' object reference.
|
|
|
|
loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
|
|
|
|
const StackFrameContext *SFC) {
|
|
|
|
return loc::MemRegionVal(getRegionManager().
|
|
|
|
getCXXThisRegion(D->getThisType(getContext()), SFC));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a memory region for the 'this' object reference.
|
|
|
|
loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D,
|
|
|
|
const StackFrameContext *SFC) {
|
|
|
|
const Type *T = D->getTypeForDecl();
|
|
|
|
QualType PT = getContext().getPointerType(QualType(T, 0));
|
|
|
|
return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC));
|
|
|
|
}
|
|
|
|
|
[analyzer] Consolidate constant evaluation logic in SValBuilder.
Previously, this was scattered across Environment (literal expressions),
ExprEngine (default arguments), and RegionStore (global constants). The
former special-cased several kinds of simple constant expressions, while
the latter two deferred to the AST's constant evaluator.
Now, these are all unified as SValBuilder::getConstantVal(). To keep
Environment fast, the special cases for simple constant expressions have
been left in, but the main benefits are that (a) unusual constants like
ObjCStringLiterals now work as default arguments and global constant
initializers, and (b) we're not duplicating code between ExprEngine and
RegionStore.
This actually caught a bug in our test suite, which is awesome: we stop
tracking allocated memory if it's passed as an argument along with some
kind of callback, but not if the callback is 0. We were testing this in
a case where the callback parameter had a default value, but that value
was 0. After this change, the analyzer now (correctly) flags that as a
leak!
<rdar://problem/13773117>
llvm-svn: 180894
2013-05-02 07:10:44 +08:00
|
|
|
Optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
|
|
|
|
E = E->IgnoreParens();
|
|
|
|
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
// Handle expressions that we treat differently from the AST's constant
|
|
|
|
// evaluator.
|
|
|
|
case Stmt::AddrLabelExprClass:
|
|
|
|
return makeLoc(cast<AddrLabelExpr>(E));
|
|
|
|
|
|
|
|
case Stmt::CXXScalarValueInitExprClass:
|
|
|
|
case Stmt::ImplicitValueInitExprClass:
|
|
|
|
return makeZeroVal(E->getType());
|
|
|
|
|
|
|
|
case Stmt::ObjCStringLiteralClass: {
|
|
|
|
const ObjCStringLiteral *SL = cast<ObjCStringLiteral>(E);
|
|
|
|
return makeLoc(getRegionManager().getObjCStringRegion(SL));
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::StringLiteralClass: {
|
|
|
|
const StringLiteral *SL = cast<StringLiteral>(E);
|
|
|
|
return makeLoc(getRegionManager().getStringRegion(SL));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path some expressions to avoid the overhead of going through the AST's
|
|
|
|
// constant evaluator
|
|
|
|
case Stmt::CharacterLiteralClass: {
|
|
|
|
const CharacterLiteral *C = cast<CharacterLiteral>(E);
|
|
|
|
return makeIntVal(C->getValue(), C->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::CXXBoolLiteralExprClass:
|
|
|
|
return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
|
|
|
|
|
|
|
|
case Stmt::IntegerLiteralClass:
|
|
|
|
return makeIntVal(cast<IntegerLiteral>(E));
|
|
|
|
|
|
|
|
case Stmt::ObjCBoolLiteralExprClass:
|
|
|
|
return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
|
|
|
|
|
|
|
|
case Stmt::CXXNullPtrLiteralExprClass:
|
|
|
|
return makeNull();
|
|
|
|
|
2013-07-18 01:16:38 +08:00
|
|
|
case Stmt::ImplicitCastExprClass: {
|
|
|
|
const CastExpr *CE = cast<CastExpr>(E);
|
|
|
|
if (CE->getCastKind() == CK_ArrayToPointerDecay) {
|
|
|
|
Optional<SVal> ArrayVal = getConstantVal(CE->getSubExpr());
|
|
|
|
if (!ArrayVal)
|
|
|
|
return None;
|
|
|
|
return evalCast(*ArrayVal, CE->getType(), CE->getSubExpr()->getType());
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
}
|
|
|
|
|
[analyzer] Consolidate constant evaluation logic in SValBuilder.
Previously, this was scattered across Environment (literal expressions),
ExprEngine (default arguments), and RegionStore (global constants). The
former special-cased several kinds of simple constant expressions, while
the latter two deferred to the AST's constant evaluator.
Now, these are all unified as SValBuilder::getConstantVal(). To keep
Environment fast, the special cases for simple constant expressions have
been left in, but the main benefits are that (a) unusual constants like
ObjCStringLiterals now work as default arguments and global constant
initializers, and (b) we're not duplicating code between ExprEngine and
RegionStore.
This actually caught a bug in our test suite, which is awesome: we stop
tracking allocated memory if it's passed as an argument along with some
kind of callback, but not if the callback is 0. We were testing this in
a case where the callback parameter had a default value, but that value
was 0. After this change, the analyzer now (correctly) flags that as a
leak!
<rdar://problem/13773117>
llvm-svn: 180894
2013-05-02 07:10:44 +08:00
|
|
|
// If we don't have a special case, fall back to the AST's constant evaluator.
|
|
|
|
default: {
|
2013-05-03 03:51:20 +08:00
|
|
|
// Don't try to come up with a value for materialized temporaries.
|
|
|
|
if (E->isGLValue())
|
|
|
|
return None;
|
|
|
|
|
[analyzer] Consolidate constant evaluation logic in SValBuilder.
Previously, this was scattered across Environment (literal expressions),
ExprEngine (default arguments), and RegionStore (global constants). The
former special-cased several kinds of simple constant expressions, while
the latter two deferred to the AST's constant evaluator.
Now, these are all unified as SValBuilder::getConstantVal(). To keep
Environment fast, the special cases for simple constant expressions have
been left in, but the main benefits are that (a) unusual constants like
ObjCStringLiterals now work as default arguments and global constant
initializers, and (b) we're not duplicating code between ExprEngine and
RegionStore.
This actually caught a bug in our test suite, which is awesome: we stop
tracking allocated memory if it's passed as an argument along with some
kind of callback, but not if the callback is 0. We were testing this in
a case where the callback parameter had a default value, but that value
was 0. After this change, the analyzer now (correctly) flags that as a
leak!
<rdar://problem/13773117>
llvm-svn: 180894
2013-05-02 07:10:44 +08:00
|
|
|
ASTContext &Ctx = getContext();
|
|
|
|
llvm::APSInt Result;
|
|
|
|
if (E->EvaluateAsInt(Result, Ctx))
|
|
|
|
return makeIntVal(Result);
|
|
|
|
|
2013-05-03 03:51:20 +08:00
|
|
|
if (Loc::isLocType(E->getType()))
|
|
|
|
if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
|
|
|
|
return makeNull();
|
[analyzer] Consolidate constant evaluation logic in SValBuilder.
Previously, this was scattered across Environment (literal expressions),
ExprEngine (default arguments), and RegionStore (global constants). The
former special-cased several kinds of simple constant expressions, while
the latter two deferred to the AST's constant evaluator.
Now, these are all unified as SValBuilder::getConstantVal(). To keep
Environment fast, the special cases for simple constant expressions have
been left in, but the main benefits are that (a) unusual constants like
ObjCStringLiterals now work as default arguments and global constant
initializers, and (b) we're not duplicating code between ExprEngine and
RegionStore.
This actually caught a bug in our test suite, which is awesome: we stop
tracking allocated memory if it's passed as an argument along with some
kind of callback, but not if the callback is 0. We were testing this in
a case where the callback parameter had a default value, but that value
was 0. After this change, the analyzer now (correctly) flags that as a
leak!
<rdar://problem/13773117>
llvm-svn: 180894
2013-05-02 07:10:44 +08:00
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2012-05-02 05:10:26 +08:00
|
|
|
SVal SValBuilder::makeSymExprValNN(ProgramStateRef State,
|
2012-05-02 08:05:23 +08:00
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
NonLoc LHS, NonLoc RHS,
|
|
|
|
QualType ResultTy) {
|
2012-05-03 10:13:46 +08:00
|
|
|
if (!State->isTainted(RHS) && !State->isTainted(LHS))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2012-05-02 08:05:23 +08:00
|
|
|
const SymExpr *symLHS = LHS.getAsSymExpr();
|
|
|
|
const SymExpr *symRHS = RHS.getAsSymExpr();
|
2012-05-03 10:13:50 +08:00
|
|
|
// TODO: When the Max Complexity is reached, we should conjure a symbol
|
|
|
|
// instead of generating an Unknown value and propagate the taint info to it.
|
|
|
|
const unsigned MaxComp = 10000; // 100000 28X
|
2012-05-02 05:10:26 +08:00
|
|
|
|
2012-05-03 10:13:50 +08:00
|
|
|
if (symLHS && symRHS &&
|
|
|
|
(symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
|
2012-05-02 08:05:23 +08:00
|
|
|
return makeNonLoc(symLHS, Op, symRHS, ResultTy);
|
|
|
|
|
2012-05-03 10:13:50 +08:00
|
|
|
if (symLHS && symLHS->computeComplexity() < MaxComp)
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>())
|
2012-05-02 08:05:23 +08:00
|
|
|
return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
|
2012-05-02 05:10:26 +08:00
|
|
|
|
2012-05-03 10:13:50 +08:00
|
|
|
if (symRHS && symRHS->computeComplexity() < MaxComp)
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>())
|
2012-05-02 08:05:23 +08:00
|
|
|
return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
|
|
|
|
|
|
|
|
return UnknownVal();
|
2011-11-18 07:07:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal lhs, SVal rhs, QualType type) {
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (lhs.isUndef() || rhs.isUndef())
|
2009-08-26 02:44:25 +08:00
|
|
|
return UndefinedVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (lhs.isUnknown() || rhs.isUnknown())
|
2009-08-26 02:44:25 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<Loc> LV = lhs.getAs<Loc>()) {
|
|
|
|
if (Optional<Loc> RV = rhs.getAs<Loc>())
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalBinOpLL(state, op, *LV, *RV, type);
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<Loc> RV = rhs.getAs<Loc>()) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// Support pointer arithmetic where the addend is on the left
|
|
|
|
// and the pointer on the right.
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(op == BO_Add);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
// Commute the operands.
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
|
|
|
|
type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal lhs,
|
|
|
|
DefinedOrUnknownSVal rhs) {
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalBinOp(state, BO_EQ, lhs, rhs, Context.IntTy)
|
|
|
|
.castAs<DefinedOrUnknownSVal>();
|
2009-09-12 06:07:28 +08:00
|
|
|
}
|
|
|
|
|
2012-01-13 08:56:55 +08:00
|
|
|
/// Recursively check if the pointer types are equal modulo const, volatile,
|
2013-02-06 03:52:28 +08:00
|
|
|
/// and restrict qualifiers. Also, assume that all types are similar to 'void'.
|
|
|
|
/// Assumes the input types are canonical.
|
|
|
|
static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
|
|
|
|
QualType FromTy) {
|
|
|
|
while (Context.UnwrapSimilarPointerTypes(ToTy, FromTy)) {
|
2012-01-13 08:56:55 +08:00
|
|
|
Qualifiers Quals1, Quals2;
|
2013-02-06 03:52:28 +08:00
|
|
|
ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
|
|
|
|
FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
|
2012-01-13 08:56:55 +08:00
|
|
|
|
|
|
|
// Make sure that non cvr-qualifiers the other qualifiers (e.g., address
|
|
|
|
// spaces) are identical.
|
|
|
|
Quals1.removeCVRQualifiers();
|
|
|
|
Quals2.removeCVRQualifiers();
|
|
|
|
if (Quals1 != Quals2)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-06 03:52:28 +08:00
|
|
|
// If we are casting to void, the 'From' value can be used to represent the
|
|
|
|
// 'To' value.
|
|
|
|
if (ToTy->isVoidType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (ToTy != FromTy)
|
2012-01-13 08:56:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-26 15:15:40 +08:00
|
|
|
// FIXME: should rewrite according to the cast kind.
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
|
2012-01-13 08:56:55 +08:00
|
|
|
castTy = Context.getCanonicalType(castTy);
|
|
|
|
originalTy = Context.getCanonicalType(originalTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
if (val.isUnknownOrUndef() || castTy == originalTy)
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-05-02 02:19:59 +08:00
|
|
|
if (castTy->isBooleanType()) {
|
|
|
|
if (val.isUnknownOrUndef())
|
|
|
|
return val;
|
|
|
|
if (val.isConstant())
|
|
|
|
return makeTruthVal(!val.isZeroConstant(), castTy);
|
2013-09-19 02:58:58 +08:00
|
|
|
if (!Loc::isLocType(originalTy) &&
|
|
|
|
!originalTy->isIntegralOrEnumerationType() &&
|
|
|
|
!originalTy->isMemberPointerType())
|
|
|
|
return UnknownVal();
|
[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 (SymbolRef Sym = val.getAsSymbol(true)) {
|
2013-05-02 02:19:59 +08:00
|
|
|
BasicValueFactory &BVF = getBasicValueFactory();
|
|
|
|
// FIXME: If we had a state here, we could see if the symbol is known to
|
|
|
|
// be zero, but we don't.
|
|
|
|
return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), 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
|
|
|
// Loc values are not always true, they could be weakly linked functions.
|
|
|
|
if (Optional<Loc> L = val.getAs<Loc>())
|
|
|
|
return evalCastFromLoc(*L, castTy);
|
2013-05-02 02:19:59 +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
|
|
|
Loc L = val.castAs<nonloc::LocAsInteger>().getLoc();
|
|
|
|
return evalCastFromLoc(L, castTy);
|
2013-05-02 02:19:59 +08:00
|
|
|
}
|
|
|
|
|
2013-02-06 03:52:28 +08:00
|
|
|
// For const casts, casts to void, just propagate the value.
|
2010-01-05 17:27:03 +08:00
|
|
|
if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
|
2013-02-06 03:52:28 +08:00
|
|
|
if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy),
|
|
|
|
Context.getPointerType(originalTy)))
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2010-02-03 05:11:40 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from pointers to integers.
|
2013-04-09 10:30:33 +08:00
|
|
|
if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy))
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalCastFromLoc(val.castAs<Loc>(), castTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from integers to pointers.
|
2013-04-09 10:30:33 +08:00
|
|
|
if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) {
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) {
|
2009-12-23 10:52:14 +08:00
|
|
|
if (const MemRegion *R = LV->getLoc().getAsRegion()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
StoreManager &storeMgr = StateMgr.getStoreManager();
|
2011-02-12 03:48:15 +08:00
|
|
|
R = storeMgr.castRegion(R, castTy);
|
2010-02-04 12:56:43 +08:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-12-23 10:52:14 +08:00
|
|
|
}
|
2010-02-04 12:56:43 +08:00
|
|
|
return LV->getLoc();
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2011-12-07 07:12:27 +08:00
|
|
|
return dispatchCast(val, castTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Just pass through function and block pointers.
|
|
|
|
if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
|
2011-02-17 05:13:32 +08:00
|
|
|
assert(Loc::isLocType(castTy));
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from array type to another type.
|
2013-05-29 07:24:01 +08:00
|
|
|
if (const ArrayType *arrayT =
|
|
|
|
dyn_cast<ArrayType>(originalTy.getCanonicalType())) {
|
2009-07-22 05:03:30 +08:00
|
|
|
// We will always decay to a pointer.
|
2013-05-29 07:24:01 +08:00
|
|
|
QualType elemTy = arrayT->getElementType();
|
|
|
|
val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Are we casting from an array to a pointer? If so just pass on
|
|
|
|
// the decayed value.
|
2012-05-25 01:31:57 +08:00
|
|
|
if (castTy->isPointerType() || castTy->isReferenceType())
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Are we casting from an array to an integer? If so, cast the decayed
|
|
|
|
// pointer value to an integer.
|
2013-04-09 10:30:33 +08:00
|
|
|
assert(castTy->isIntegralOrEnumerationType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// FIXME: Keep these here for now in case we decide soon that we
|
|
|
|
// need the original decayed type.
|
|
|
|
// QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
|
|
|
|
// QualType pointerTy = C.getPointerType(elemTy);
|
2013-02-20 13:52:05 +08:00
|
|
|
return evalCastFromLoc(val.castAs<Loc>(), castTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from a region to a specific type.
|
|
|
|
if (const MemRegion *R = val.getAsRegion()) {
|
2012-05-02 05:58:29 +08:00
|
|
|
// Handle other casts of locations to integers.
|
2013-04-09 10:30:33 +08:00
|
|
|
if (castTy->isIntegralOrEnumerationType())
|
2012-05-02 05:58:29 +08:00
|
|
|
return evalCastFromLoc(loc::MemRegionVal(R), castTy);
|
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// FIXME: We should handle the case where we strip off view layers to get
|
|
|
|
// to a desugared type.
|
2011-02-17 05:13:32 +08:00
|
|
|
if (!Loc::isLocType(castTy)) {
|
2010-11-16 04:09:42 +08:00
|
|
|
// FIXME: There can be gross cases where one casts the result of a function
|
|
|
|
// (that returns a pointer) to some other value that happens to fit
|
|
|
|
// within that pointer value. We currently have no good way to
|
|
|
|
// model such operations. When this happens, the underlying operation
|
|
|
|
// is that the caller is reasoning about bits. Conceptually we are
|
|
|
|
// layering a "view" of a location on top of those bits. Perhaps
|
|
|
|
// we need to be more lazy about mutual possible views, even on an
|
|
|
|
// SVal? This may be necessary for bit-level reasoning as well.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// We get a symbolic function pointer for a dereference of a function
|
|
|
|
// pointer, but it is of function type. Example:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// struct FPRec {
|
2009-09-09 23:08:12 +08:00
|
|
|
// void (*my_func)(int * x);
|
2009-07-22 05:03:30 +08:00
|
|
|
// };
|
|
|
|
//
|
|
|
|
// int bar(int x);
|
|
|
|
//
|
|
|
|
// int f1_a(struct FPRec* foo) {
|
|
|
|
// int x;
|
|
|
|
// (*foo->my_func)(&x);
|
|
|
|
// return bar(x)+1; // no-warning
|
|
|
|
// }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
|
2011-02-19 16:03:18 +08:00
|
|
|
originalTy->isBlockPointerType() || castTy->isReferenceType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
StoreManager &storeMgr = StateMgr.getStoreManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
// Delegate to store manager to get the result of casting a region to a
|
|
|
|
// different type. If the MemRegion* returned is NULL, this expression
|
2010-12-02 05:57:22 +08:00
|
|
|
// Evaluates to UnknownVal.
|
2011-02-12 03:48:15 +08:00
|
|
|
R = storeMgr.castRegion(R, castTy);
|
2010-02-04 12:56:43 +08:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
return dispatchCast(val, castTy);
|
2009-09-12 06:07:28 +08:00
|
|
|
}
|