Skip to content

Commit d16c5f4

Browse files
author
Balazs Benics
committed
Revert "[analyzer] Fix taint propagation by remembering to the location context"
This reverts commit b099e1e. I'm reverting this since the head of the patch stack caused a build breakage. https://lab.llvm.org/buildbot/#/builders/91/builds/3818
1 parent 2acead3 commit d16c5f4

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ using namespace clang;
3838
using namespace ento;
3939
using namespace taint;
4040

41-
using llvm::ImmutableSet;
42-
4341
namespace {
4442

4543
class GenericTaintChecker;
@@ -436,9 +434,7 @@ template <> struct ScalarEnumerationTraits<TaintConfiguration::VariadicType> {
436434
/// to the call post-visit. The values are signed integers, which are either
437435
/// ReturnValueIndex, or indexes of the pointer/reference argument, which
438436
/// points to data, which should be tainted on return.
439-
REGISTER_MAP_WITH_PROGRAMSTATE(TaintArgsOnPostVisit, const LocationContext *,
440-
ImmutableSet<ArgIdxTy>)
441-
REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(ArgIdxFactory, ArgIdxTy)
437+
REGISTER_SET_WITH_PROGRAMSTATE(TaintArgsOnPostVisit, ArgIdxTy)
442438

443439
void GenericTaintRuleParser::validateArgVector(const std::string &Option,
444440
const ArgVecTy &Args) const {
@@ -689,26 +685,22 @@ void GenericTaintChecker::checkPostCall(const CallEvent &Call,
689685
// Set the marked values as tainted. The return value only accessible from
690686
// checkPostStmt.
691687
ProgramStateRef State = C.getState();
692-
const StackFrameContext *CurrentFrame = C.getStackFrame();
693688

694689
// Depending on what was tainted at pre-visit, we determined a set of
695690
// arguments which should be tainted after the function returns. These are
696691
// stored in the state as TaintArgsOnPostVisit set.
697-
TaintArgsOnPostVisitTy TaintArgsMap = State->get<TaintArgsOnPostVisit>();
698-
699-
const ImmutableSet<ArgIdxTy> *TaintArgs = TaintArgsMap.lookup(CurrentFrame);
700-
if (!TaintArgs)
692+
TaintArgsOnPostVisitTy TaintArgs = State->get<TaintArgsOnPostVisit>();
693+
if (TaintArgs.isEmpty())
701694
return;
702-
assert(!TaintArgs->isEmpty());
703695

704696
LLVM_DEBUG(for (ArgIdxTy I
705-
: *TaintArgs) {
697+
: TaintArgs) {
706698
llvm::dbgs() << "PostCall<";
707699
Call.dump(llvm::dbgs());
708700
llvm::dbgs() << "> actually wants to taint arg index: " << I << '\n';
709701
});
710702

711-
for (ArgIdxTy ArgNum : *TaintArgs) {
703+
for (ArgIdxTy ArgNum : TaintArgs) {
712704
// Special handling for the tainted return value.
713705
if (ArgNum == ReturnValueIndex) {
714706
State = addTaint(State, Call.getReturnValue());
@@ -722,7 +714,7 @@ void GenericTaintChecker::checkPostCall(const CallEvent &Call,
722714
}
723715

724716
// Clear up the taint info from the state.
725-
State = State->remove<TaintArgsOnPostVisit>(CurrentFrame);
717+
State = State->remove<TaintArgsOnPostVisit>();
726718
C.addTransition(State);
727719
}
728720

@@ -784,33 +776,28 @@ void GenericTaintRule::process(const GenericTaintChecker &Checker,
784776
};
785777

786778
/// Propagate taint where it is necessary.
787-
auto &F = State->getStateManager().get_context<ArgIdxFactory>();
788-
ImmutableSet<ArgIdxTy> Result = F.getEmptySet();
789779
ForEachCallArg(
790-
[this, WouldEscape, &Call, &Result, &F](ArgIdxTy I, const Expr *E,
791-
SVal V) {
780+
[this, &State, WouldEscape, &Call](ArgIdxTy I, const Expr *E, SVal V) {
792781
if (PropDstArgs.contains(I)) {
793782
LLVM_DEBUG(llvm::dbgs() << "PreCall<"; Call.dump(llvm::dbgs());
794783
llvm::dbgs()
795784
<< "> prepares tainting arg index: " << I << '\n';);
796-
Result = F.add(Result, I);
785+
State = State->add<TaintArgsOnPostVisit>(I);
797786
}
798787

799788
// TODO: We should traverse all reachable memory regions via the
800789
// escaping parameter. Instead of doing that we simply mark only the
801790
// referred memory region as tainted.
802791
if (WouldEscape(V, E->getType())) {
803-
LLVM_DEBUG(if (!Result.contains(I)) {
792+
LLVM_DEBUG(if (!State->contains<TaintArgsOnPostVisit>(I)) {
804793
llvm::dbgs() << "PreCall<";
805794
Call.dump(llvm::dbgs());
806795
llvm::dbgs() << "> prepares tainting arg index: " << I << '\n';
807796
});
808-
Result = F.add(Result, I);
797+
State = State->add<TaintArgsOnPostVisit>(I);
809798
}
810799
});
811800

812-
if (!Result.isEmpty())
813-
State = State->set<TaintArgsOnPostVisit>(C.getStackFrame(), Result);
814801
C.addTransition(State);
815802
}
816803

@@ -901,11 +888,7 @@ void GenericTaintChecker::taintUnsafeSocketProtocol(const CallEvent &Call,
901888
if (SafeProtocol)
902889
return;
903890

904-
ProgramStateRef State = C.getState();
905-
auto &F = State->getStateManager().get_context<ArgIdxFactory>();
906-
ImmutableSet<ArgIdxTy> Result = F.add(F.getEmptySet(), ReturnValueIndex);
907-
State = State->set<TaintArgsOnPostVisit>(C.getStackFrame(), Result);
908-
C.addTransition(State);
891+
C.addTransition(C.getState()->add<TaintArgsOnPostVisit>(ReturnValueIndex));
909892
}
910893

911894
/// Checker registration

clang/test/Analysis/taint-checker-callback-order-has-definition.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// RUN: -mllvm -debug-only=taint-checker \
44
// RUN: 2>&1 | FileCheck %s
55

6+
// FIXME: We should not crash.
7+
// XFAIL: *
8+
69
struct _IO_FILE;
710
typedef struct _IO_FILE FILE;
811
FILE *fopen(const char *fname, const char *mode);
@@ -28,8 +31,12 @@ void top(const char *fname, char *buf) {
2831
// CHECK-NEXT: PreCall<fgets(buf, 42, fp)> prepares tainting arg index: 1
2932
// CHECK-NEXT: PreCall<fgets(buf, 42, fp)> prepares tainting arg index: 2
3033

31-
// CHECK-NEXT: PostCall<fgets(buf, 42, fp)> actually wants to taint arg index: -1
32-
// CHECK-NEXT: PostCall<fgets(buf, 42, fp)> actually wants to taint arg index: 0
33-
// CHECK-NEXT: PostCall<fgets(buf, 42, fp)> actually wants to taint arg index: 1
34-
// CHECK-NEXT: PostCall<fgets(buf, 42, fp)> actually wants to taint arg index: 2
34+
// FIXME: We should propagate taint from PreCall<fgets> -> PostCall<fgets>.
35+
// CHECK-NEXT: PostCall<nested_call()> actually wants to taint arg index: -1
36+
// CHECK-NEXT: PostCall<nested_call()> actually wants to taint arg index: 0
37+
// CHECK-NEXT: PostCall<nested_call()> actually wants to taint arg index: 1
38+
// CHECK-NEXT: PostCall<nested_call()> actually wants to taint arg index: 2
39+
40+
// FIXME: We should not crash.
41+
// CHECK: PLEASE submit a bug report
3542
}

0 commit comments

Comments
 (0)