Skip to content

Commit

Permalink
[analyzer] Move taint API from ProgramState to a separate header. NFC.
Browse files Browse the repository at this point in the history
It is now an inter-checker communication API, similar to the one that
connects MallocChecker/CStringChecker/InnerPointerChecker: simply a set of
setters and getters for a state trait.

Differential Revision: https://reviews.llvm.org/D59861

llvm-svn: 357326
  • Loading branch information
haoNoQ committed Mar 29, 2019
1 parent 60cde76 commit 44551cf
Show file tree
Hide file tree
Showing 17 changed files with 375 additions and 375 deletions.
Expand Up @@ -307,20 +307,6 @@ class CXXSelfAssignmentBRVisitor final : public BugReporterVisitor {
BugReport &BR) override;
};

/// The bug visitor prints a diagnostic message at the location where a given
/// variable was tainted.
class TaintBugVisitor final : public BugReporterVisitor {
private:
const SVal V;

public:
TaintBugVisitor(const SVal V) : V(V) {}
void Profile(llvm::FoldingSetNodeID &ID) const override { ID.Add(V); }

std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
BugReporterContext &BRC,
BugReport &BR) override;
};

/// The bug visitor will walk all the nodes in a path and collect all the
/// constraints. When it reaches the root node, will create a refutation
Expand Down
Expand Up @@ -20,7 +20,6 @@
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/ImmutableMap.h"
#include "llvm/Support/Allocator.h"
Expand All @@ -43,7 +42,6 @@ typedef std::unique_ptr<ConstraintManager>(*ConstraintManagerCreator)(
ProgramStateManager &, SubEngine *);
typedef std::unique_ptr<StoreManager>(*StoreManagerCreator)(
ProgramStateManager &);
typedef llvm::ImmutableMap<const SubRegion*, TaintTagType> TaintedSubRegions;

//===----------------------------------------------------------------------===//
// ProgramStateTrait - Traits used by the Generic Data Map of a ProgramState.
Expand Down Expand Up @@ -367,38 +365,6 @@ class ProgramState : public llvm::FoldingSetNode {
template <typename CB> CB
scanReachableSymbols(llvm::iterator_range<region_iterator> Reachable) const;

/// Create a new state in which the statement is marked as tainted.
LLVM_NODISCARD ProgramStateRef
addTaint(const Stmt *S, const LocationContext *LCtx,
TaintTagType Kind = TaintTagGeneric) const;

/// Create a new state in which the value is marked as tainted.
LLVM_NODISCARD ProgramStateRef
addTaint(SVal V, TaintTagType Kind = TaintTagGeneric) const;

/// Create a new state in which the symbol is marked as tainted.
LLVM_NODISCARD ProgramStateRef addTaint(SymbolRef S,
TaintTagType Kind = TaintTagGeneric) const;

/// Create a new state in which the region symbol is marked as tainted.
LLVM_NODISCARD ProgramStateRef
addTaint(const MemRegion *R, TaintTagType Kind = TaintTagGeneric) const;

/// Create a new state in a which a sub-region of a given symbol is tainted.
/// This might be necessary when referring to regions that can not have an
/// individual symbol, e.g. if they are represented by the default binding of
/// a LazyCompoundVal.
LLVM_NODISCARD ProgramStateRef
addPartialTaint(SymbolRef ParentSym, const SubRegion *SubRegion,
TaintTagType Kind = TaintTagGeneric) const;

/// Check if the statement is tainted in the current state.
bool isTainted(const Stmt *S, const LocationContext *LCtx,
TaintTagType Kind = TaintTagGeneric) const;
bool isTainted(SVal V, TaintTagType Kind = TaintTagGeneric) const;
bool isTainted(SymbolRef Sym, TaintTagType Kind = TaintTagGeneric) const;
bool isTainted(const MemRegion *Reg, TaintTagType Kind=TaintTagGeneric) const;

//==---------------------------------------------------------------------==//
// Accessing the Generic Data Map (GDM).
//==---------------------------------------------------------------------==//
Expand Down Expand Up @@ -462,10 +428,8 @@ class ProgramState : public llvm::FoldingSetNode {
const LocationContext *CurrentLC = nullptr) const;
void printDOT(raw_ostream &Out,
const LocationContext *CurrentLC = nullptr) const;
void printTaint(raw_ostream &Out, const char *nl = "\n") const;

void dump() const;
void dumpTaint() const;

private:
friend void ProgramStateRetain(const ProgramState *state);
Expand Down Expand Up @@ -499,7 +463,6 @@ class ProgramStateManager {
std::unique_ptr<ConstraintManager> ConstraintMgr;

ProgramState::GenericDataMap::Factory GDMFactory;
TaintedSubRegions::Factory TSRFactory;

typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
GDMContextsTy GDMContexts;
Expand Down

This file was deleted.

29 changes: 0 additions & 29 deletions clang/include/clang/StaticAnalyzer/Core/PathSensitive/TaintTag.h

This file was deleted.

4 changes: 3 additions & 1 deletion clang/lib/StaticAnalyzer/Checkers/ArrayBoundCheckerV2.cpp
Expand Up @@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//

#include "Taint.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/AST/CharUnits.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Expand All @@ -24,6 +25,7 @@

using namespace clang;
using namespace ento;
using namespace taint;

namespace {
class ArrayBoundCheckerV2 :
Expand Down Expand Up @@ -204,7 +206,7 @@ void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
// If we are under constrained and the index variables are tainted, report.
if (state_exceedsUpperBound && state_withinUpperBound) {
SVal ByteOffset = rawOffset.getByteOffset();
if (state->isTainted(ByteOffset)) {
if (isTainted(state, ByteOffset)) {
reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted,
llvm::make_unique<TaintBugVisitor>(ByteOffset));
return;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
Expand Up @@ -87,6 +87,7 @@ add_clang_library(clangStaticAnalyzerCheckers
StackAddrEscapeChecker.cpp
StdLibraryFunctionsChecker.cpp
StreamChecker.cpp
Taint.cpp
TaintTesterChecker.cpp
TestAfterDivZeroChecker.cpp
TraversalChecker.cpp
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/DivZeroChecker.cpp
Expand Up @@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//

#include "Taint.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
Expand All @@ -19,6 +20,7 @@

using namespace clang;
using namespace ento;
using namespace taint;

namespace {
class DivZeroChecker : public Checker< check::PreStmt<BinaryOperator> > {
Expand Down Expand Up @@ -83,10 +85,10 @@ void DivZeroChecker::checkPreStmt(const BinaryOperator *B,
return;
}

bool TaintedD = C.getState()->isTainted(*DV);
bool TaintedD = isTainted(C.getState(), *DV);
if ((stateNotZero && stateZero && TaintedD)) {
reportBug("Division by a tainted value, possibly zero", stateZero, C,
llvm::make_unique<TaintBugVisitor>(*DV));
llvm::make_unique<taint::TaintBugVisitor>(*DV));
return;
}

Expand Down
23 changes: 17 additions & 6 deletions clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
Expand Up @@ -13,6 +13,8 @@
// aggressively, even if the involved symbols are under constrained.
//
//===----------------------------------------------------------------------===//

#include "Taint.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/AST/Attr.h"
#include "clang/Basic/Builtins.h"
Expand All @@ -27,6 +29,7 @@

using namespace clang;
using namespace ento;
using namespace taint;

namespace {
class GenericTaintChecker
Expand All @@ -41,6 +44,9 @@ class GenericTaintChecker

void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;

void printState(raw_ostream &Out, ProgramStateRef State,
const char *NL, const char *Sep) const override;

private:
static const unsigned InvalidArgIndex = UINT_MAX;
/// Denotes the return vale.
Expand Down Expand Up @@ -152,14 +158,14 @@ class GenericTaintChecker

static bool isTaintedOrPointsToTainted(const Expr *E, ProgramStateRef State,
CheckerContext &C) {
if (State->isTainted(E, C.getLocationContext()) || isStdin(E, C))
if (isTainted(State, E, C.getLocationContext()) || isStdin(E, C))
return true;

if (!E->getType().getTypePtr()->isPointerType())
return false;

Optional<SVal> V = getPointedToSVal(C, E);
return (V && State->isTainted(*V));
return (V && isTainted(State, *V));
}

/// Pre-process a function which propagates taint according to the
Expand Down Expand Up @@ -313,6 +319,11 @@ void GenericTaintChecker::checkPostStmt(const CallExpr *CE,
propagateFromPre(CE, C);
}

void GenericTaintChecker::printState(raw_ostream &Out, ProgramStateRef State,
const char *NL, const char *Sep) const {
printTaint(State, Out, NL, Sep);
}

void GenericTaintChecker::addSourcesPre(const CallExpr *CE,
CheckerContext &C) const {
ProgramStateRef State = nullptr;
Expand Down Expand Up @@ -354,7 +365,7 @@ bool GenericTaintChecker::propagateFromPre(const CallExpr *CE,
for (unsigned ArgNum : TaintArgs) {
// Special handling for the tainted return value.
if (ArgNum == ReturnValueIndex) {
State = State->addTaint(CE, C.getLocationContext());
State = addTaint(State, CE, C.getLocationContext());
continue;
}

Expand All @@ -365,7 +376,7 @@ bool GenericTaintChecker::propagateFromPre(const CallExpr *CE,
const Expr *Arg = CE->getArg(ArgNum);
Optional<SVal> V = getPointedToSVal(C, Arg);
if (V)
State = State->addTaint(*V);
State = addTaint(State, *V);
}

// Clear up the taint info from the state.
Expand Down Expand Up @@ -570,9 +581,9 @@ bool GenericTaintChecker::generateReportIfTainted(const Expr *E,
ProgramStateRef State = C.getState();
Optional<SVal> PointedToSVal = getPointedToSVal(C, E);
SVal TaintedSVal;
if (PointedToSVal && State->isTainted(*PointedToSVal))
if (PointedToSVal && isTainted(State, *PointedToSVal))
TaintedSVal = *PointedToSVal;
else if (State->isTainted(E, C.getLocationContext()))
else if (isTainted(State, E, C.getLocationContext()))
TaintedSVal = C.getSVal(E);
else
return false;
Expand Down

0 comments on commit 44551cf

Please sign in to comment.