|
| 1 | +#include "phasar/PhasarLLVM/Pointer/FilteredLLVMAliasSet.h" |
| 2 | + |
| 3 | +#include "phasar/ControlFlow/CallGraphAnalysisType.h" |
| 4 | +#include "phasar/DataFlow/IfdsIde/Solver/IFDSSolver.h" |
| 5 | +#include "phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h" |
| 6 | +#include "phasar/PhasarLLVM/DB/LLVMProjectIRDB.h" |
| 7 | +#include "phasar/PhasarLLVM/DataFlow/IfdsIde/Problems/IFDSTaintAnalysis.h" |
| 8 | +#include "phasar/PhasarLLVM/Pointer/LLVMAliasSet.h" |
| 9 | +#include "phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h" |
| 10 | +#include "phasar/PhasarLLVM/Utils/LLVMShorthands.h" |
| 11 | + |
| 12 | +#include "llvm/ADT/StringRef.h" |
| 13 | +#include "llvm/IR/InstrTypes.h" |
| 14 | + |
| 15 | +#include "TestConfig.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | + |
| 18 | +using namespace psr; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +static LLVMTaintConfig getDefaultConfig() { |
| 23 | + auto SourceCB = [](const llvm::Instruction *Inst) { |
| 24 | + std::set<const llvm::Value *> Ret; |
| 25 | + if (const auto *Call = llvm::dyn_cast<llvm::CallBase>(Inst); |
| 26 | + Call && Call->getCalledFunction() && |
| 27 | + Call->getCalledFunction()->getName() == "_Z6sourcev") { |
| 28 | + Ret.insert(Call); |
| 29 | + } |
| 30 | + return Ret; |
| 31 | + }; |
| 32 | + auto SinkCB = [](const llvm::Instruction *Inst) { |
| 33 | + std::set<const llvm::Value *> Ret; |
| 34 | + if (const auto *Call = llvm::dyn_cast<llvm::CallBase>(Inst); |
| 35 | + Call && Call->getCalledFunction() && |
| 36 | + Call->getCalledFunction()->getName() == "_Z4sinki") { |
| 37 | + assert(Call->arg_size() > 0); |
| 38 | + Ret.insert(Call->getArgOperand(0)); |
| 39 | + } |
| 40 | + return Ret; |
| 41 | + }; |
| 42 | + return LLVMTaintConfig(std::move(SourceCB), std::move(SinkCB)); |
| 43 | +} |
| 44 | + |
| 45 | +static LLVMTaintConfig getDoubleFreeConfig() { |
| 46 | + auto SourceCB = [](const llvm::Instruction *Inst) { |
| 47 | + std::set<const llvm::Value *> Ret; |
| 48 | + if (const auto *Call = llvm::dyn_cast<llvm::CallBase>(Inst); |
| 49 | + Call && Call->getCalledFunction() && |
| 50 | + Call->getCalledFunction()->getName() == "free") { |
| 51 | + Ret.insert(Call->getArgOperand(0)); |
| 52 | + } |
| 53 | + return Ret; |
| 54 | + }; |
| 55 | + |
| 56 | + return LLVMTaintConfig(SourceCB, SourceCB); |
| 57 | +} |
| 58 | + |
| 59 | +class TaintAnalysis : public ::testing::TestWithParam<std::string_view> { |
| 60 | +protected: |
| 61 | + static constexpr auto PathToLlFiles = |
| 62 | + PHASAR_BUILD_SUBFOLDER("taint_analysis/"); |
| 63 | + const std::vector<std::string> EntryPoints = {"main"}; |
| 64 | + |
| 65 | +}; // Test Fixture |
| 66 | + |
| 67 | +TEST_P(TaintAnalysis, LeaksWithAndWithoutAliasFilteringEqual) { |
| 68 | + |
| 69 | + LLVMProjectIRDB IRDB(PathToLlFiles + GetParam()); |
| 70 | + LLVMAliasSet AS(&IRDB, false); |
| 71 | + FilteredLLVMAliasSet FAS(&AS); |
| 72 | + LLVMBasedICFG ICF(&IRDB, CallGraphAnalysisType::OTF, EntryPoints, nullptr, |
| 73 | + &AS); |
| 74 | + |
| 75 | + auto TSF = llvm::StringRef(GetParam()).startswith("double_free") |
| 76 | + ? getDoubleFreeConfig() |
| 77 | + : getDefaultConfig(); |
| 78 | + |
| 79 | + IFDSTaintAnalysis TaintProblem(&IRDB, &AS, &TSF, EntryPoints); |
| 80 | + IFDSTaintAnalysis FilterTaintProblem(&IRDB, &FAS, &TSF, EntryPoints); |
| 81 | + |
| 82 | + solveIFDSProblem(TaintProblem, ICF).dumpResults(ICF); |
| 83 | + solveIFDSProblem(FilterTaintProblem, ICF).dumpResults(ICF); |
| 84 | + |
| 85 | + EXPECT_EQ(TaintProblem.Leaks.size(), FilterTaintProblem.Leaks.size()); |
| 86 | + |
| 87 | + for (const auto &[LeakInst, LeakFacts] : TaintProblem.Leaks) { |
| 88 | + const auto It = FilterTaintProblem.Leaks.find(LeakInst); |
| 89 | + |
| 90 | + EXPECT_NE(It, FilterTaintProblem.Leaks.end()) |
| 91 | + << "Expected to find leak at " + llvmIRToString(LeakInst); |
| 92 | + |
| 93 | + if (It == FilterTaintProblem.Leaks.end()) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + for (const auto *LeakFact : LeakFacts) { |
| 98 | + EXPECT_TRUE(It->second.count(LeakFact)) |
| 99 | + << "Expected to find leak-fact " + llvmIRToShortString(LeakFact) + |
| 100 | + " at " + llvmIRToString(LeakInst); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +static constexpr std::string_view TaintTestFiles[] = { |
| 106 | + // -- dummy-source-sink |
| 107 | + "dummy_source_sink/taint_01_cpp_dbg.ll", |
| 108 | + "dummy_source_sink/taint_01_cpp_m2r_dbg.ll", |
| 109 | + "dummy_source_sink/taint_02_cpp_dbg.ll", |
| 110 | + "dummy_source_sink/taint_03_cpp_dbg.ll", |
| 111 | + "dummy_source_sink/taint_04_cpp_dbg.ll", |
| 112 | + "dummy_source_sink/taint_05_cpp_dbg.ll", |
| 113 | + "dummy_source_sink/taint_06_cpp_m2r_dbg.ll", |
| 114 | + "dummy_source_sink/taint_exception_01_cpp_dbg.ll", |
| 115 | + "dummy_source_sink/taint_exception_01_cpp_m2r_dbg.ll", |
| 116 | + "dummy_source_sink/taint_exception_02_cpp_dbg.ll", |
| 117 | + "dummy_source_sink/taint_exception_03_cpp_dbg.ll", |
| 118 | + "dummy_source_sink/taint_exception_04_cpp_dbg.ll", |
| 119 | + "dummy_source_sink/taint_exception_05_cpp_dbg.ll", |
| 120 | + "dummy_source_sink/taint_exception_06_cpp_dbg.ll", |
| 121 | + "dummy_source_sink/taint_exception_07_cpp_dbg.ll", |
| 122 | + "dummy_source_sink/taint_exception_08_cpp_dbg.ll", |
| 123 | + "dummy_source_sink/taint_exception_09_cpp_dbg.ll", |
| 124 | + "dummy_source_sink/taint_exception_10_cpp_dbg.ll", |
| 125 | + // -- double-free |
| 126 | + "double_free_01_c.ll", |
| 127 | + "double_free_02_c.ll", |
| 128 | +}; |
| 129 | + |
| 130 | +INSTANTIATE_TEST_SUITE_P(InteractiveIDESolverTest, TaintAnalysis, |
| 131 | + ::testing::ValuesIn(TaintTestFiles)); |
| 132 | + |
| 133 | +} // namespace |
| 134 | + |
| 135 | +int main(int Argc, char **Argv) { |
| 136 | + ::testing::InitGoogleTest(&Argc, Argv); |
| 137 | + return RUN_ALL_TESTS(); |
| 138 | +} |
0 commit comments