2011-08-16 07:05:22 +08:00
|
|
|
//= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- C++ -*--=
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements ProgramState and ProgramStateManager.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2011-08-16 07:05:22 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2012-07-31 04:21:55 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-08-16 07:05:22 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SubEngine.h"
|
2011-11-17 03:58:10 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/TaintManager.h"
|
2011-08-16 07:05:22 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2012-01-31 10:23:28 +08:00
|
|
|
namespace clang { namespace ento {
|
|
|
|
/// Increments the number of times this state is referenced.
|
|
|
|
|
|
|
|
void ProgramStateRetain(const ProgramState *state) {
|
|
|
|
++const_cast<ProgramState*>(state)->refCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decrement the number of times this state is referenced.
|
|
|
|
void ProgramStateRelease(const ProgramState *state) {
|
|
|
|
assert(state->refCount > 0);
|
|
|
|
ProgramState *s = const_cast<ProgramState*>(state);
|
|
|
|
if (--s->refCount == 0) {
|
|
|
|
ProgramStateManager &Mgr = s->getStateManager();
|
|
|
|
Mgr.StateSet.RemoveNode(s);
|
2015-09-08 11:50:52 +08:00
|
|
|
s->~ProgramState();
|
2012-01-31 10:23:28 +08:00
|
|
|
Mgr.freeStates.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 07:07:51 +08:00
|
|
|
}}
|
2012-01-31 10:23:28 +08:00
|
|
|
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
|
|
|
|
StoreRef st, GenericDataMap gdm)
|
|
|
|
: stateMgr(mgr),
|
|
|
|
Env(env),
|
|
|
|
store(st.getStore()),
|
|
|
|
GDM(gdm),
|
|
|
|
refCount(0) {
|
|
|
|
stateMgr->getStoreManager().incrementReferenceCount(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramState::ProgramState(const ProgramState &RHS)
|
|
|
|
: llvm::FoldingSetNode(),
|
|
|
|
stateMgr(RHS.stateMgr),
|
|
|
|
Env(RHS.Env),
|
|
|
|
store(RHS.store),
|
|
|
|
GDM(RHS.GDM),
|
|
|
|
refCount(0) {
|
|
|
|
stateMgr->getStoreManager().incrementReferenceCount(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramState::~ProgramState() {
|
|
|
|
if (store)
|
|
|
|
stateMgr->getStoreManager().decrementReferenceCount(store);
|
|
|
|
}
|
|
|
|
|
2012-07-31 04:21:55 +08:00
|
|
|
ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
|
|
|
|
StoreManagerCreator CreateSMgr,
|
|
|
|
ConstraintManagerCreator CreateCMgr,
|
|
|
|
llvm::BumpPtrAllocator &alloc,
|
2012-10-02 00:53:40 +08:00
|
|
|
SubEngine *SubEng)
|
|
|
|
: Eng(SubEng), EnvMgr(alloc), GDMFactory(alloc),
|
2012-07-31 04:21:55 +08:00
|
|
|
svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
|
|
|
|
CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
|
2014-09-05 07:54:37 +08:00
|
|
|
StoreMgr = (*CreateSMgr)(*this);
|
|
|
|
ConstraintMgr = (*CreateCMgr)(*this, SubEng);
|
2012-07-31 04:21:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramStateManager::~ProgramStateManager() {
|
|
|
|
for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
|
|
|
|
I!=E; ++I)
|
|
|
|
I->second.second(I->second.first);
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateManager::removeDeadBindings(ProgramStateRef state,
|
2011-08-16 07:05:22 +08:00
|
|
|
const StackFrameContext *LCtx,
|
|
|
|
SymbolReaper& SymReaper) {
|
|
|
|
|
|
|
|
// This code essentially performs a "mark-and-sweep" of the VariableBindings.
|
|
|
|
// The roots are any Block-level exprs and Decls that our liveness algorithm
|
|
|
|
// tells us are live. We then see what Decls they may reference, and keep
|
|
|
|
// those around. This code more than likely can be made faster, and the
|
|
|
|
// frequency of which this method is called should be experimented with
|
|
|
|
// for optimum performance.
|
|
|
|
ProgramState NewState = *state;
|
|
|
|
|
|
|
|
NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
|
|
|
|
|
|
|
|
// Clean up the store.
|
|
|
|
StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
|
|
|
|
SymReaper);
|
|
|
|
NewState.setStore(newStore);
|
|
|
|
SymReaper.setReapedStore(newStore);
|
2012-09-26 03:03:06 +08:00
|
|
|
|
|
|
|
ProgramStateRef Result = getPersistentState(NewState);
|
|
|
|
return ConstraintMgr->removeDeadBindings(Result, SymReaper);
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:57 +08:00
|
|
|
ProgramStateRef ProgramState::bindLoc(Loc LV,
|
|
|
|
SVal V,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
bool notifyChanges) const {
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramStateManager &Mgr = getStateManager();
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
|
2011-08-16 07:05:22 +08:00
|
|
|
LV, V));
|
|
|
|
const MemRegion *MR = LV.getAsRegion();
|
2012-08-22 14:00:18 +08:00
|
|
|
if (MR && Mgr.getOwningEngine() && notifyChanges)
|
2017-01-13 08:50:57 +08:00
|
|
|
return Mgr.getOwningEngine()->processRegionChange(newState, MR, LCtx);
|
2011-08-16 07:05:22 +08:00
|
|
|
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:57 +08:00
|
|
|
ProgramStateRef ProgramState::bindDefault(SVal loc,
|
|
|
|
SVal V,
|
|
|
|
const LocationContext *LCtx) const {
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramStateManager &Mgr = getStateManager();
|
2013-02-20 13:52:05 +08:00
|
|
|
const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
|
2011-08-16 07:05:22 +08:00
|
|
|
const StoreRef &newStore = Mgr.StoreMgr->BindDefault(getStore(), R, V);
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef new_state = makeWithStore(newStore);
|
2015-09-08 11:50:52 +08:00
|
|
|
return Mgr.getOwningEngine() ?
|
2017-01-13 08:50:57 +08:00
|
|
|
Mgr.getOwningEngine()->processRegionChange(new_state, R, LCtx) :
|
2011-08-16 07:05:22 +08:00
|
|
|
new_state;
|
|
|
|
}
|
|
|
|
|
2013-03-21 04:35:53 +08:00
|
|
|
typedef ArrayRef<const MemRegion *> RegionList;
|
2013-04-02 09:28:24 +08:00
|
|
|
typedef ArrayRef<SVal> ValueList;
|
2013-03-21 04:35:53 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef
|
2013-03-21 04:35:53 +08:00
|
|
|
ProgramState::invalidateRegions(RegionList Regions,
|
2013-09-25 07:47:29 +08:00
|
|
|
const Expr *E, unsigned Count,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
bool CausedByPointerEscape,
|
|
|
|
InvalidatedSymbols *IS,
|
|
|
|
const CallEvent *Call,
|
|
|
|
RegionAndSymbolInvalidationTraits *ITraits) const {
|
2013-04-02 09:28:24 +08:00
|
|
|
SmallVector<SVal, 8> Values;
|
|
|
|
for (RegionList::const_iterator I = Regions.begin(),
|
2013-04-03 07:47:53 +08:00
|
|
|
End = Regions.end(); I != End; ++I)
|
2013-04-02 09:28:24 +08:00
|
|
|
Values.push_back(loc::MemRegionVal(*I));
|
|
|
|
|
|
|
|
return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
|
2013-09-25 07:47:29 +08:00
|
|
|
IS, ITraits, Call);
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
2013-04-02 09:28:24 +08:00
|
|
|
ProgramStateRef
|
|
|
|
ProgramState::invalidateRegions(ValueList Values,
|
2013-09-25 07:47:29 +08:00
|
|
|
const Expr *E, unsigned Count,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
bool CausedByPointerEscape,
|
|
|
|
InvalidatedSymbols *IS,
|
|
|
|
const CallEvent *Call,
|
|
|
|
RegionAndSymbolInvalidationTraits *ITraits) const {
|
|
|
|
|
2013-04-02 09:28:24 +08:00
|
|
|
return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
|
2013-09-25 07:47:29 +08:00
|
|
|
IS, ITraits, Call);
|
2013-04-02 09:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgramStateRef
|
|
|
|
ProgramState::invalidateRegionsImpl(ValueList Values,
|
2011-08-28 06:51:26 +08:00
|
|
|
const Expr *E, unsigned Count,
|
2012-02-18 07:13:45 +08:00
|
|
|
const LocationContext *LCtx,
|
2012-12-21 09:50:14 +08:00
|
|
|
bool CausedByPointerEscape,
|
2013-09-25 07:47:29 +08:00
|
|
|
InvalidatedSymbols *IS,
|
|
|
|
RegionAndSymbolInvalidationTraits *ITraits,
|
|
|
|
const CallEvent *Call) const {
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramStateManager &Mgr = getStateManager();
|
|
|
|
SubEngine* Eng = Mgr.getOwningEngine();
|
2013-03-29 07:15:29 +08:00
|
|
|
|
2013-09-25 07:47:29 +08:00
|
|
|
InvalidatedSymbols Invalidated;
|
|
|
|
if (!IS)
|
|
|
|
IS = &Invalidated;
|
|
|
|
|
|
|
|
RegionAndSymbolInvalidationTraits ITraitsLocal;
|
|
|
|
if (!ITraits)
|
|
|
|
ITraits = &ITraitsLocal;
|
|
|
|
|
2012-12-20 08:38:25 +08:00
|
|
|
if (Eng) {
|
2013-04-02 09:28:24 +08:00
|
|
|
StoreManager::InvalidatedRegions TopLevelInvalidated;
|
2011-08-28 06:51:26 +08:00
|
|
|
StoreManager::InvalidatedRegions Invalidated;
|
2011-08-16 07:05:22 +08:00
|
|
|
const StoreRef &newStore
|
2013-09-25 07:47:29 +08:00
|
|
|
= Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
|
|
|
|
*IS, *ITraits, &TopLevelInvalidated,
|
2013-04-02 09:28:24 +08:00
|
|
|
&Invalidated);
|
2012-12-20 08:38:25 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef newState = makeWithStore(newStore);
|
2012-12-20 08:38:25 +08:00
|
|
|
|
2013-03-29 07:15:29 +08:00
|
|
|
if (CausedByPointerEscape) {
|
2013-09-25 07:47:29 +08:00
|
|
|
newState = Eng->notifyCheckersOfPointerEscape(newState, IS,
|
2013-04-02 09:28:24 +08:00
|
|
|
TopLevelInvalidated,
|
2015-09-08 11:50:52 +08:00
|
|
|
Invalidated, Call,
|
2013-09-25 07:47:29 +08:00
|
|
|
*ITraits);
|
2013-03-29 07:15:29 +08:00
|
|
|
}
|
2012-12-20 08:38:25 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
return Eng->processRegionChanges(newState, IS, TopLevelInvalidated,
|
2017-01-13 08:50:57 +08:00
|
|
|
Invalidated, LCtx, Call);
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const StoreRef &newStore =
|
2013-09-25 07:47:29 +08:00
|
|
|
Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
|
2014-05-27 10:45:47 +08:00
|
|
|
*IS, *ITraits, nullptr, nullptr);
|
2011-08-16 07:05:22 +08:00
|
|
|
return makeWithStore(newStore);
|
|
|
|
}
|
|
|
|
|
2012-08-22 14:37:46 +08:00
|
|
|
ProgramStateRef ProgramState::killBinding(Loc LV) const {
|
2013-02-20 13:52:05 +08:00
|
|
|
assert(!LV.getAs<loc::MemRegionVal>() && "Use invalidateRegion instead.");
|
2011-08-16 07:05:22 +08:00
|
|
|
|
|
|
|
Store OldStore = getStore();
|
2012-08-22 14:37:46 +08:00
|
|
|
const StoreRef &newStore =
|
|
|
|
getStateManager().StoreMgr->killBinding(OldStore, LV);
|
2011-08-16 07:05:22 +08:00
|
|
|
|
|
|
|
if (newStore.getStore() == OldStore)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
return makeWithStore(newStore);
|
|
|
|
}
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
ProgramStateRef
|
2012-07-11 06:07:57 +08:00
|
|
|
ProgramState::enterStackFrame(const CallEvent &Call,
|
|
|
|
const StackFrameContext *CalleeCtx) const {
|
|
|
|
const StoreRef &NewStore =
|
|
|
|
getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
|
|
|
|
return makeWithStore(NewStore);
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
|
|
|
|
// We only want to do fetches from regions that we can actually bind
|
|
|
|
// values. For example, SymbolicRegions of type 'id<...>' cannot
|
|
|
|
// have direct bindings (but their can be bindings on their subregions).
|
|
|
|
if (!R->isBoundable())
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
|
|
|
|
QualType T = TR->getValueType();
|
2013-04-09 10:30:33 +08:00
|
|
|
if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
|
2011-08-16 07:05:22 +08:00
|
|
|
return getSVal(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
SVal ProgramState::getSVal(Loc location, QualType T) const {
|
|
|
|
SVal V = getRawSVal(cast<Loc>(location), T);
|
|
|
|
|
|
|
|
// If 'V' is a symbolic value that is *perfectly* constrained to
|
|
|
|
// be a constant value, use that value instead to lessen the burden
|
|
|
|
// on later analysis stages (so we have less symbolic values to reason
|
|
|
|
// about).
|
|
|
|
if (!T.isNull()) {
|
|
|
|
if (SymbolRef sym = V.getAsSymbol()) {
|
2012-09-08 06:31:01 +08:00
|
|
|
if (const llvm::APSInt *Int = getStateManager()
|
|
|
|
.getConstraintManager()
|
|
|
|
.getSymVal(this, sym)) {
|
2011-08-16 07:05:22 +08:00
|
|
|
// FIXME: Because we don't correctly model (yet) sign-extension
|
|
|
|
// and truncation of symbolic values, we need to convert
|
|
|
|
// the integer value to the correct signedness and bitwidth.
|
|
|
|
//
|
|
|
|
// This shows up in the following:
|
|
|
|
//
|
|
|
|
// char foo();
|
|
|
|
// unsigned x = foo();
|
|
|
|
// if (x == 54)
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// The symbolic value stored to 'x' is actually the conjured
|
|
|
|
// symbol for the call to foo(); the type of that symbol is 'char',
|
|
|
|
// not unsigned.
|
|
|
|
const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-02-20 13:52:05 +08:00
|
|
|
if (V.getAs<Loc>())
|
2011-08-16 07:05:22 +08:00
|
|
|
return loc::ConcreteInt(NewV);
|
|
|
|
else
|
|
|
|
return nonloc::ConcreteInt(NewV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-08-16 07:05:22 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramState::BindExpr(const Stmt *S,
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx,
|
|
|
|
SVal V, bool Invalidate) const{
|
|
|
|
Environment NewEnv =
|
|
|
|
getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
|
|
|
|
Invalidate);
|
2011-08-16 07:05:22 +08:00
|
|
|
if (NewEnv == Env)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
ProgramState NewSt = *this;
|
|
|
|
NewSt.Env = NewEnv;
|
|
|
|
return getStateManager().getPersistentState(NewSt);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
|
2011-08-16 07:05:22 +08:00
|
|
|
DefinedOrUnknownSVal UpperBound,
|
2012-01-30 14:42:48 +08:00
|
|
|
bool Assumption,
|
|
|
|
QualType indexTy) const {
|
2011-08-16 07:05:22 +08:00
|
|
|
if (Idx.isUnknown() || UpperBound.isUnknown())
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// Build an expression for 0 <= Idx < UpperBound.
|
|
|
|
// This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
|
|
|
|
// FIXME: This should probably be part of SValBuilder.
|
|
|
|
ProgramStateManager &SM = getStateManager();
|
|
|
|
SValBuilder &svalBuilder = SM.getSValBuilder();
|
|
|
|
ASTContext &Ctx = svalBuilder.getContext();
|
|
|
|
|
|
|
|
// Get the offset: the minimum value of the array index type.
|
|
|
|
BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
|
|
|
|
// FIXME: This should be using ValueManager::ArrayindexTy...somehow.
|
2012-01-30 14:42:48 +08:00
|
|
|
if (indexTy.isNull())
|
|
|
|
indexTy = Ctx.IntTy;
|
2011-08-16 07:05:22 +08:00
|
|
|
nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
|
|
|
|
|
|
|
|
// Adjust the index.
|
|
|
|
SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
|
2013-02-20 13:52:05 +08:00
|
|
|
Idx.castAs<NonLoc>(), Min, indexTy);
|
2011-08-16 07:05:22 +08:00
|
|
|
if (newIdx.isUnknownOrUndef())
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// Adjust the upper bound.
|
|
|
|
SVal newBound =
|
2013-02-20 13:52:05 +08:00
|
|
|
svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
|
2011-08-16 07:05:22 +08:00
|
|
|
Min, indexTy);
|
|
|
|
|
|
|
|
if (newBound.isUnknownOrUndef())
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// Build the actual comparison.
|
2013-02-20 13:52:05 +08:00
|
|
|
SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
|
|
|
|
newBound.castAs<NonLoc>(), Ctx.IntTy);
|
2011-08-16 07:05:22 +08:00
|
|
|
if (inBound.isUnknownOrUndef())
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// Finally, let the constraint manager take care of it.
|
|
|
|
ConstraintManager &CM = SM.getConstraintManager();
|
2013-02-20 13:52:05 +08:00
|
|
|
return CM.assume(this, inBound.castAs<DefinedSVal>(), Assumption);
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
2013-03-02 11:20:52 +08:00
|
|
|
ConditionTruthVal ProgramState::isNull(SVal V) const {
|
|
|
|
if (V.isZeroConstant())
|
|
|
|
return true;
|
|
|
|
|
2013-04-04 03:28:12 +08:00
|
|
|
if (V.isConstant())
|
|
|
|
return false;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-04-20 09:15:42 +08:00
|
|
|
SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
|
2013-03-02 11:20:52 +08:00
|
|
|
if (!Sym)
|
2013-04-04 03:28:12 +08:00
|
|
|
return ConditionTruthVal();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-03-02 11:20:52 +08:00
|
|
|
return getStateManager().ConstraintMgr->isNull(this, Sym);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramState State(this,
|
|
|
|
EnvMgr.getInitialEnvironment(),
|
|
|
|
StoreMgr->getInitialStore(InitLoc),
|
|
|
|
GDMFactory.getEmptyMap());
|
|
|
|
|
|
|
|
return getPersistentState(State);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
|
|
|
|
ProgramStateRef FromState,
|
|
|
|
ProgramStateRef GDMState) {
|
2012-01-31 10:23:28 +08:00
|
|
|
ProgramState NewState(*FromState);
|
2011-08-16 07:05:22 +08:00
|
|
|
NewState.GDM = GDMState->GDM;
|
|
|
|
return getPersistentState(NewState);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
|
2011-08-16 07:05:22 +08:00
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
State.Profile(ID);
|
|
|
|
void *InsertPos;
|
|
|
|
|
|
|
|
if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return I;
|
|
|
|
|
2014-05-27 10:45:47 +08:00
|
|
|
ProgramState *newState = nullptr;
|
2011-08-16 07:05:22 +08:00
|
|
|
if (!freeStates.empty()) {
|
|
|
|
newState = freeStates.back();
|
2015-09-08 11:50:52 +08:00
|
|
|
freeStates.pop_back();
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
newState = (ProgramState*) Alloc.Allocate<ProgramState>();
|
|
|
|
}
|
|
|
|
new (newState) ProgramState(State);
|
|
|
|
StateSet.InsertNode(newState, InsertPos);
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
|
2012-01-31 10:23:28 +08:00
|
|
|
ProgramState NewSt(*this);
|
2011-08-16 07:05:22 +08:00
|
|
|
NewSt.setStore(store);
|
|
|
|
return getStateManager().getPersistentState(NewSt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::setStore(const StoreRef &newStore) {
|
|
|
|
Store newStoreStore = newStore.getStore();
|
|
|
|
if (newStoreStore)
|
|
|
|
stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
|
|
|
|
if (store)
|
|
|
|
stateMgr->getStoreManager().decrementReferenceCount(store);
|
|
|
|
store = newStoreStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// State pretty-printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
void ProgramState::print(raw_ostream &Out,
|
2011-08-29 03:11:56 +08:00
|
|
|
const char *NL, const char *Sep) const {
|
2011-08-16 07:05:22 +08:00
|
|
|
// Print the store.
|
|
|
|
ProgramStateManager &Mgr = getStateManager();
|
2011-08-29 03:11:56 +08:00
|
|
|
Mgr.getStoreManager().print(getStore(), Out, NL, Sep);
|
2011-08-16 07:05:22 +08:00
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
// Print out the environment.
|
|
|
|
Env.print(Out, NL, Sep);
|
2011-08-16 07:05:22 +08:00
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
// Print out the constraints.
|
2011-08-29 03:11:56 +08:00
|
|
|
Mgr.getConstraintManager().print(this, Out, NL, Sep);
|
2011-08-16 07:05:22 +08:00
|
|
|
|
|
|
|
// Print checker-specific data.
|
2011-08-29 03:11:56 +08:00
|
|
|
Mgr.getOwningEngine()->printState(Out, this, NL, Sep);
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
void ProgramState::printDOT(raw_ostream &Out) const {
|
|
|
|
print(Out, "\\l", "\\|");
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void ProgramState::dump() const {
|
2012-01-07 06:09:28 +08:00
|
|
|
print(llvm::errs());
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
2012-01-20 08:11:16 +08:00
|
|
|
void ProgramState::printTaint(raw_ostream &Out,
|
|
|
|
const char *NL, const char *Sep) const {
|
|
|
|
TaintMapImpl TM = get<TaintMap>();
|
|
|
|
|
|
|
|
if (!TM.isEmpty())
|
|
|
|
Out <<"Tainted Symbols:" << NL;
|
|
|
|
|
|
|
|
for (TaintMapImpl::iterator I = TM.begin(), E = TM.end(); I != E; ++I) {
|
|
|
|
Out << I->first << " : " << I->second << NL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramState::dumpTaint() const {
|
|
|
|
printTaint(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2011-08-16 07:05:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generic Data Map.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void *const* ProgramState::FindGDM(void *K) const {
|
|
|
|
return GDM.lookup(K);
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
ProgramStateManager::FindGDMContext(void *K,
|
|
|
|
void *(*CreateContext)(llvm::BumpPtrAllocator&),
|
|
|
|
void (*DeleteContext)(void*)) {
|
|
|
|
|
|
|
|
std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
|
|
|
|
if (!p.first) {
|
|
|
|
p.first = CreateContext(Alloc);
|
|
|
|
p.second = DeleteContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.first;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramState::GenericDataMap M1 = St->getGDM();
|
|
|
|
ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
|
|
|
|
|
|
|
|
if (M1 == M2)
|
|
|
|
return St;
|
|
|
|
|
|
|
|
ProgramState NewSt = *St;
|
|
|
|
NewSt.GDM = M2;
|
|
|
|
return getPersistentState(NewSt);
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
|
2011-08-16 07:05:22 +08:00
|
|
|
ProgramState::GenericDataMap OldM = state->getGDM();
|
|
|
|
ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
|
|
|
|
|
|
|
|
if (NewM == OldM)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
ProgramState NewState = *state;
|
|
|
|
NewState.GDM = NewM;
|
|
|
|
return getPersistentState(NewState);
|
|
|
|
}
|
|
|
|
|
2013-08-19 23:23:34 +08:00
|
|
|
bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
|
|
|
|
bool wasVisited = !visited.insert(val.getCVData()).second;
|
|
|
|
if (wasVisited)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
StoreManager &StoreMgr = state->getStateManager().getStoreManager();
|
|
|
|
// FIXME: We don't really want to use getBaseRegion() here because pointer
|
|
|
|
// arithmetic doesn't apply, but scanReachableSymbols only accepts base
|
|
|
|
// regions right now.
|
|
|
|
const MemRegion *R = val.getRegion()->getBaseRegion();
|
|
|
|
return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
|
|
|
|
}
|
|
|
|
|
2011-08-16 07:05:22 +08:00
|
|
|
bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
|
|
|
|
for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I)
|
|
|
|
if (!scan(*I))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScanReachableSymbols::scan(const SymExpr *sym) {
|
2016-11-19 05:07:03 +08:00
|
|
|
for (SymExpr::symbol_iterator SI = sym->symbol_begin(),
|
|
|
|
SE = sym->symbol_end();
|
|
|
|
SI != SE; ++SI) {
|
|
|
|
bool wasVisited = !visited.insert(*SI).second;
|
|
|
|
if (wasVisited)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!visitor.VisitSymbol(*SI))
|
|
|
|
return false;
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
2016-11-19 05:07:03 +08:00
|
|
|
|
2011-08-16 07:05:22 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScanReachableSymbols::scan(SVal val) {
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
|
2011-08-16 07:05:22 +08:00
|
|
|
return scan(X->getRegion());
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::LazyCompoundVal> X =
|
2013-08-19 23:23:34 +08:00
|
|
|
val.getAs<nonloc::LazyCompoundVal>())
|
|
|
|
return scan(*X);
|
2012-08-09 02:23:31 +08:00
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
|
2011-08-16 07:05:22 +08:00
|
|
|
return scan(X->getLoc());
|
|
|
|
|
|
|
|
if (SymbolRef Sym = val.getAsSymbol())
|
|
|
|
return scan(Sym);
|
|
|
|
|
|
|
|
if (const SymExpr *Sym = val.getAsSymbolicExpression())
|
|
|
|
return scan(Sym);
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
if (Optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
|
2011-08-16 07:05:22 +08:00
|
|
|
return scan(*X);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScanReachableSymbols::scan(const MemRegion *R) {
|
|
|
|
if (isa<MemSpaceRegion>(R))
|
|
|
|
return true;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-08-19 23:23:34 +08:00
|
|
|
bool wasVisited = !visited.insert(R).second;
|
|
|
|
if (wasVisited)
|
2011-08-16 07:05:22 +08:00
|
|
|
return true;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-02-21 08:46:29 +08:00
|
|
|
if (!visitor.VisitMemRegion(R))
|
|
|
|
return false;
|
2011-08-16 07:05:22 +08:00
|
|
|
|
|
|
|
// If this is a symbolic region, visit the symbol for the region.
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
|
|
|
|
if (!visitor.VisitSymbol(SR->getSymbol()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If this is a subregion, also visit the parent regions.
|
2012-08-09 02:23:27 +08:00
|
|
|
if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
|
|
|
|
const MemRegion *Super = SR->getSuperRegion();
|
|
|
|
if (!scan(Super))
|
2011-08-16 07:05:22 +08:00
|
|
|
return false;
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
// When we reach the topmost region, scan all symbols in it.
|
|
|
|
if (isa<MemSpaceRegion>(Super)) {
|
|
|
|
StoreManager &StoreMgr = state->getStateManager().getStoreManager();
|
|
|
|
if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
// Regions captured by a block are also implicitly reachable.
|
|
|
|
if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
|
|
|
|
BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
|
|
|
|
E = BDR->referenced_vars_end();
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
if (!scan(I.getCapturedRegion()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 02:23:27 +08:00
|
|
|
return true;
|
2011-08-16 07:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
|
|
|
|
ScanReachableSymbols S(this, visitor);
|
|
|
|
return S.scan(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramState::scanReachableSymbols(const SVal *I, const SVal *E,
|
|
|
|
SymbolVisitor &visitor) const {
|
|
|
|
ScanReachableSymbols S(this, visitor);
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
if (!S.scan(*I))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramState::scanReachableSymbols(const MemRegion * const *I,
|
|
|
|
const MemRegion * const *E,
|
|
|
|
SymbolVisitor &visitor) const {
|
|
|
|
ScanReachableSymbols S(this, visitor);
|
|
|
|
for ( ; I != E; ++I) {
|
|
|
|
if (!S.scan(*I))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-17 03:58:10 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramState::addTaint(const Stmt *S,
|
2012-01-07 06:09:28 +08:00
|
|
|
const LocationContext *LCtx,
|
2011-11-17 03:58:10 +08:00
|
|
|
TaintTagType Kind) const {
|
2011-12-17 02:28:50 +08:00
|
|
|
if (const Expr *E = dyn_cast_or_null<Expr>(S))
|
|
|
|
S = E->IgnoreParens();
|
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
return addTaint(getSVal(S, LCtx), Kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramStateRef ProgramState::addTaint(SVal V,
|
|
|
|
TaintTagType Kind) const {
|
|
|
|
SymbolRef Sym = V.getAsSymbol();
|
2011-12-14 08:55:58 +08:00
|
|
|
if (Sym)
|
|
|
|
return addTaint(Sym, Kind);
|
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
// If the SVal represents a structure, try to mass-taint all values within the
|
|
|
|
// structure. For now it only works efficiently on lazy compound values that
|
|
|
|
// were conjured during a conservative evaluation of a function - either as
|
|
|
|
// return values of functions that return structures or arrays by value, or as
|
|
|
|
// values of structures or arrays passed into the function by reference,
|
|
|
|
// directly or through pointer aliasing. Such lazy compound values are
|
|
|
|
// characterized by having exactly one binding in their captured store within
|
|
|
|
// their parent region, which is a conjured symbol default-bound to the base
|
|
|
|
// region of the parent region.
|
|
|
|
if (auto LCV = V.getAs<nonloc::LazyCompoundVal>()) {
|
|
|
|
if (Optional<SVal> binding = getStateManager().StoreMgr->getDefaultBinding(*LCV)) {
|
|
|
|
if (SymbolRef Sym = binding->getAsSymbol())
|
|
|
|
return addPartialTaint(Sym, LCV->getRegion(), Kind);
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 08:55:58 +08:00
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
const MemRegion *R = V.getAsRegion();
|
|
|
|
return addTaint(R, Kind);
|
2011-11-17 03:58:10 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramState::addTaint(const MemRegion *R,
|
2011-12-14 08:56:15 +08:00
|
|
|
TaintTagType Kind) const {
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast_or_null<SymbolicRegion>(R))
|
|
|
|
return addTaint(SR->getSymbol(), Kind);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef ProgramState::addTaint(SymbolRef Sym,
|
2011-11-17 03:58:10 +08:00
|
|
|
TaintTagType Kind) const {
|
2012-01-20 08:11:16 +08:00
|
|
|
// If this is a symbol cast, remove the cast before adding the taint. Taint
|
|
|
|
// is cast agnostic.
|
|
|
|
while (const SymbolCast *SC = dyn_cast<SymbolCast>(Sym))
|
|
|
|
Sym = SC->getOperand();
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
ProgramStateRef NewState = set<TaintMap>(Sym, Kind);
|
2011-11-17 03:58:10 +08:00
|
|
|
assert(NewState);
|
|
|
|
return NewState;
|
|
|
|
}
|
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
ProgramStateRef ProgramState::addPartialTaint(SymbolRef ParentSym,
|
|
|
|
const SubRegion *SubRegion,
|
|
|
|
TaintTagType Kind) const {
|
|
|
|
// Ignore partial taint if the entire parent symbol is already tainted.
|
|
|
|
if (contains<TaintMap>(ParentSym) && *get<TaintMap>(ParentSym) == Kind)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// Partial taint applies if only a portion of the symbol is tainted.
|
|
|
|
if (SubRegion == SubRegion->getBaseRegion())
|
|
|
|
return addTaint(ParentSym, Kind);
|
|
|
|
|
2017-05-30 02:54:02 +08:00
|
|
|
const TaintedSubRegions *SavedRegs = get<DerivedSymTaint>(ParentSym);
|
|
|
|
TaintedSubRegions Regs =
|
|
|
|
SavedRegs ? *SavedRegs : stateMgr->TSRFactory.getEmptyMap();
|
2017-05-29 23:42:56 +08:00
|
|
|
|
2017-05-30 02:54:02 +08:00
|
|
|
Regs = stateMgr->TSRFactory.add(Regs, SubRegion, Kind);
|
|
|
|
ProgramStateRef NewState = set<DerivedSymTaint>(ParentSym, Regs);
|
2017-05-29 23:42:56 +08:00
|
|
|
assert(NewState);
|
|
|
|
return NewState;
|
|
|
|
}
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
bool ProgramState::isTainted(const Stmt *S, const LocationContext *LCtx,
|
|
|
|
TaintTagType Kind) const {
|
2011-12-17 02:28:50 +08:00
|
|
|
if (const Expr *E = dyn_cast_or_null<Expr>(S))
|
|
|
|
S = E->IgnoreParens();
|
|
|
|
|
2012-01-07 06:09:28 +08:00
|
|
|
SVal val = getSVal(S, LCtx);
|
2011-12-08 03:56:13 +08:00
|
|
|
return isTainted(val, Kind);
|
2011-11-17 03:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramState::isTainted(SVal V, TaintTagType Kind) const {
|
2011-12-07 09:09:52 +08:00
|
|
|
if (const SymExpr *Sym = V.getAsSymExpr())
|
|
|
|
return isTainted(Sym, Kind);
|
2011-12-17 08:26:29 +08:00
|
|
|
if (const MemRegion *Reg = V.getAsRegion())
|
|
|
|
return isTainted(Reg, Kind);
|
2011-12-07 09:09:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProgramState::isTainted(const MemRegion *Reg, TaintTagType K) const {
|
|
|
|
if (!Reg)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Element region (array element) is tainted if either the base or the offset
|
|
|
|
// are tainted.
|
|
|
|
if (const ElementRegion *ER = dyn_cast<ElementRegion>(Reg))
|
|
|
|
return isTainted(ER->getSuperRegion(), K) || isTainted(ER->getIndex(), K);
|
|
|
|
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Reg))
|
|
|
|
return isTainted(SR->getSymbol(), K);
|
|
|
|
|
|
|
|
if (const SubRegion *ER = dyn_cast<SubRegion>(Reg))
|
|
|
|
return isTainted(ER->getSuperRegion(), K);
|
|
|
|
|
|
|
|
return false;
|
2011-11-17 03:58:10 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 10:33:10 +08:00
|
|
|
bool ProgramState::isTainted(SymbolRef Sym, TaintTagType Kind) const {
|
2011-11-18 06:59:00 +08:00
|
|
|
if (!Sym)
|
|
|
|
return false;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2011-12-07 09:09:52 +08:00
|
|
|
// Traverse all the symbols this symbol depends on to see if any are tainted.
|
2011-12-07 07:12:33 +08:00
|
|
|
for (SymExpr::symbol_iterator SI = Sym->symbol_begin(), SE =Sym->symbol_end();
|
|
|
|
SI != SE; ++SI) {
|
2012-09-26 03:03:06 +08:00
|
|
|
if (!isa<SymbolData>(*SI))
|
|
|
|
continue;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
if (const TaintTagType *Tag = get<TaintMap>(*SI)) {
|
|
|
|
if (*Tag == Kind)
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-07 07:12:33 +08:00
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
if (const SymbolDerived *SD = dyn_cast<SymbolDerived>(*SI)) {
|
|
|
|
// If this is a SymbolDerived with a tainted parent, it's also tainted.
|
|
|
|
if (isTainted(SD->getParentSymbol(), Kind))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If this is a SymbolDerived with the same parent symbol as another
|
|
|
|
// tainted SymbolDerived and a region that's a sub-region of that tainted
|
|
|
|
// symbol, it's also tainted.
|
2017-05-30 02:54:02 +08:00
|
|
|
if (const TaintedSubRegions *Regs =
|
|
|
|
get<DerivedSymTaint>(SD->getParentSymbol())) {
|
2017-05-29 23:42:56 +08:00
|
|
|
const TypedValueRegion *R = SD->getRegion();
|
2017-05-30 02:54:02 +08:00
|
|
|
for (auto I : *Regs) {
|
2017-05-29 23:42:56 +08:00
|
|
|
// FIXME: The logic to identify tainted regions could be more
|
|
|
|
// complete. For example, this would not currently identify
|
|
|
|
// overlapping fields in a union as tainted. To identify this we can
|
|
|
|
// check for overlapping/nested byte offsets.
|
2017-05-30 02:54:02 +08:00
|
|
|
if (Kind == I.second &&
|
|
|
|
(R == I.first || R->isSubRegionOf(I.first)))
|
2017-05-29 23:42:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 06:38:43 +08:00
|
|
|
|
|
|
|
// If memory region is tainted, data is also tainted.
|
2017-05-29 23:42:56 +08:00
|
|
|
if (const SymbolRegionValue *SRV = dyn_cast<SymbolRegionValue>(*SI)) {
|
|
|
|
if (isTainted(SRV->getRegion(), Kind))
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-20 08:11:16 +08:00
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
// If this is a SymbolCast from a tainted value, it's also tainted.
|
|
|
|
if (const SymbolCast *SC = dyn_cast<SymbolCast>(*SI)) {
|
|
|
|
if (isTainted(SC->getOperand(), Kind))
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-17 03:58:10 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-05-29 23:42:56 +08:00
|
|
|
return false;
|
2011-11-17 03:58:10 +08:00
|
|
|
}
|
2012-07-31 04:31:29 +08:00
|
|
|
|