opnsense-src/lib/Checker/Environment.cpp

165 lines
5.1 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04:00
//== Environment.cpp - Map from Stmt* to Locations/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 defined the Environment and EnvironmentManager classes.
//
//===----------------------------------------------------------------------===//
2010-02-16 04:31:36 -05:00
#include "clang/Checker/PathSensitive/GRState.h"
2009-06-02 13:58:47 -04:00
#include "clang/Analysis/Analyses/LiveVariables.h"
2009-10-14 14:03:49 -04:00
#include "llvm/ADT/ImmutableMap.h"
2009-06-02 13:58:47 -04:00
using namespace clang;
2009-06-23 10:50:21 -04:00
SVal Environment::GetSVal(const Stmt *E, ValueManager& ValMgr) const {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
for (;;) {
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
switch (E->getStmtClass()) {
2009-10-14 14:03:49 -04:00
case Stmt::AddrLabelExprClass:
2009-06-23 10:50:21 -04:00
return ValMgr.makeLoc(cast<AddrLabelExpr>(E));
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// ParenExprs are no-ops.
2009-10-14 14:03:49 -04:00
case Stmt::ParenExprClass:
2009-06-02 13:58:47 -04:00
E = cast<ParenExpr>(E)->getSubExpr();
continue;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
case Stmt::CharacterLiteralClass: {
2009-06-22 04:08:35 -04:00
const CharacterLiteral* C = cast<CharacterLiteral>(E);
2009-06-23 10:50:21 -04:00
return ValMgr.makeIntVal(C->getValue(), C->getType());
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
case Stmt::IntegerLiteralClass: {
2010-01-15 10:39:40 -05:00
// In C++, this expression may have been bound to a temporary object.
SVal const *X = ExprBindings.lookup(E);
if (X)
return *X;
else
return ValMgr.makeIntVal(cast<IntegerLiteral>(E));
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Casts where the source and target type are the same
// are no-ops. We blast through these to get the descendant
// subexpression that has a value.
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
case Stmt::ImplicitCastExprClass:
case Stmt::CStyleCastExprClass: {
2009-06-22 04:08:35 -04:00
const CastExpr* C = cast<CastExpr>(E);
2009-06-02 13:58:47 -04:00
QualType CT = C->getType();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (CT->isVoidType())
return UnknownVal();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
break;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Handle all other Stmt* using a lookup.
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
default:
break;
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
break;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
return LookupExpr(E);
}
2009-10-14 14:03:49 -04:00
Environment EnvironmentManager::BindExpr(Environment Env, const Stmt *S,
SVal V, bool Invalidate) {
assert(S);
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
if (V.isUnknown()) {
2009-06-02 13:58:47 -04:00
if (Invalidate)
2009-10-14 14:03:49 -04:00
return Environment(F.Remove(Env.ExprBindings, S), Env.ACtx);
2009-06-02 13:58:47 -04:00
else
return Env;
}
2009-10-14 14:03:49 -04:00
return Environment(F.Add(Env.ExprBindings, S, V), Env.ACtx);
2009-06-02 13:58:47 -04:00
}
namespace {
2009-12-01 06:08:04 -05:00
class MarkLiveCallback : public SymbolVisitor {
2009-06-02 13:58:47 -04:00
SymbolReaper &SymReaper;
public:
2009-10-14 14:03:49 -04:00
MarkLiveCallback(SymbolReaper &symreaper) : SymReaper(symreaper) {}
2009-06-02 13:58:47 -04:00
bool VisitSymbol(SymbolRef sym) { SymReaper.markLive(sym); return true; }
};
} // end anonymous namespace
// RemoveDeadBindings:
// - Remove subexpression bindings.
// - Remove dead block expression bindings.
// - Keep live block expression bindings:
2009-10-14 14:03:49 -04:00
// - Mark their reachable symbols live in SymbolReaper,
2009-06-02 13:58:47 -04:00
// see ScanReachableSymbols.
// - Mark the region in DRoots if the binding is a loc::MemRegionVal.
2009-10-14 14:03:49 -04:00
Environment
EnvironmentManager::RemoveDeadBindings(Environment Env, const Stmt *S,
SymbolReaper &SymReaper,
const GRState *ST,
llvm::SmallVectorImpl<const MemRegion*> &DRoots) {
CFG &C = *Env.getAnalysisContext().getCFG();
// We construct a new Environment object entirely, as this is cheaper than
// individually removing all the subexpression bindings (which will greatly
// outnumber block-level expression bindings).
Environment NewEnv = getInitialEnvironment(&Env.getAnalysisContext());
2009-06-02 13:58:47 -04:00
// Iterate over the block-expr bindings.
2009-10-14 14:03:49 -04:00
for (Environment::iterator I = Env.begin(), E = Env.end();
2009-06-02 13:58:47 -04:00
I != E; ++I) {
2009-10-14 14:03:49 -04:00
2009-06-22 04:08:35 -04:00
const Stmt *BlkExpr = I.getKey();
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
// Not a block-level expression?
if (!C.isBlkExpr(BlkExpr))
continue;
const SVal &X = I.getData();
if (SymReaper.isLive(S, BlkExpr)) {
// Copy the binding to the new map.
NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X);
2009-06-02 13:58:47 -04:00
// If the block expr's value is a memory region, then mark that region.
2009-07-04 09:58:54 -04:00
if (isa<loc::MemRegionVal>(X)) {
const MemRegion* R = cast<loc::MemRegionVal>(X).getRegion();
DRoots.push_back(R);
// Mark the super region of the RX as live.
2009-10-14 14:03:49 -04:00
// e.g.: int x; char *y = (char*) &x; if (*y) ...
2009-07-04 09:58:54 -04:00
// 'y' => element region. 'x' is its super region.
// We only add one level super region for now.
// FIXME: maybe multiple level of super regions should be added.
2009-10-14 14:03:49 -04:00
if (const SubRegion *SR = dyn_cast<SubRegion>(R))
2009-07-04 09:58:54 -04:00
DRoots.push_back(SR->getSuperRegion());
}
2009-06-02 13:58:47 -04:00
// Mark all symbols in the block expr's value live.
MarkLiveCallback cb(SymReaper);
2009-10-14 14:03:49 -04:00
ST->scanReachableSymbols(X, cb);
continue;
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
// Otherwise the expression is dead with a couple exceptions.
// Do not misclean LogicalExpr or ConditionalOperator. It is dead at the
// beginning of itself, but we need its UndefinedVal to determine its
// SVal.
if (X.isUndef() && cast<UndefinedVal>(X).getData())
NewEnv.ExprBindings = F.Add(NewEnv.ExprBindings, BlkExpr, X);
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
return NewEnv;
2009-06-02 13:58:47 -04:00
}