diff --git a/include/phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h b/include/phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h index ddd4ed8389..c6c9c2db29 100644 --- a/include/phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h +++ b/include/phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h @@ -12,9 +12,13 @@ #include "phasar/PhasarLLVM/TaintConfig/TaintConfigBase.h" -#include "llvm/IR/Instruction.h" +#include "llvm/ADT/DenseSet.h" -#include +namespace llvm { +class Instruction; +class Value; +class Function; +} // namespace llvm namespace psr { class LLVMTaintConfig; @@ -44,6 +48,10 @@ class LLVMTaintConfig : public TaintConfigBase { void addTaintCategory(const llvm::Value *Val, llvm::StringRef AnnotationStr); void addTaintCategory(const llvm::Value *Val, TaintCategory Annotation); + bool isSourceFunction(const llvm::Function *Func) const; + bool isSinkFunction(const llvm::Function *Func) const; + bool isSanitizerFunction(const llvm::Function *Func) const; + private: [[nodiscard]] bool isSourceImpl(const llvm::Value *V) const; [[nodiscard]] bool isSinkImpl(const llvm::Value *V) const; @@ -100,10 +108,12 @@ class LLVMTaintConfig : public TaintConfigBase { const TaintConfigData &Config); // --- data members - - std::unordered_set SourceValues; - std::unordered_set SinkValues; - std::unordered_set SanitizerValues; + llvm::DenseSet AnnotatedSourceFunctions; + llvm::DenseSet AnnotatedSinkFunctions; + llvm::DenseSet AnnotatedSanitizerFunctions; + llvm::DenseSet SourceValues; + llvm::DenseSet SinkValues; + llvm::DenseSet SanitizerValues; }; extern template class TaintConfigBase; diff --git a/include/phasar/PhasarLLVM/TaintConfig/TaintConfigUtilities.h b/include/phasar/PhasarLLVM/TaintConfig/TaintConfigUtilities.h index 28786e1185..92629eec36 100644 --- a/include/phasar/PhasarLLVM/TaintConfig/TaintConfigUtilities.h +++ b/include/phasar/PhasarLLVM/TaintConfig/TaintConfigUtilities.h @@ -42,6 +42,11 @@ void forallGeneratedFacts( if (!Callee) { return; } + + if (Config.isSourceFunction(Callee)) { + std::invoke(WithGenFact, CB); + } + for (unsigned I = 0, End = Callee->arg_size(); I < End; ++I) { if (Config.isSource(Callee->getArg(I))) { std::invoke(WithGenFact, CB->getArgOperand(I)); @@ -67,6 +72,11 @@ void collectGeneratedFacts(ContainerTy &Dest, const LLVMTaintConfig &Config, if (!Callee) { return; } + + if (Config.isSourceFunction(Callee)) { + Dest.insert(CB); + } + for (unsigned I = 0, End = Callee->arg_size(); I < End; ++I) { if (Config.isSource(Callee->getArg(I))) { Dest.insert(CB->getArgOperand(I)); @@ -89,7 +99,15 @@ void forallLeakedFacts(const LLVMTaintConfig &Config, const llvm::CallBase *CB, if (!Callee) { return; } - for (unsigned I = 0, End = Callee->arg_size(); I < End; ++I) { + if (Config.isSinkFunction(Callee)) { + auto *LeakParam = CB->getArgOperand(Callee->hasStructRetAttr()); + assert(LeakParam); + if (std::invoke(LeakIf, LeakParam)) { + std::invoke(WithLeakFact, LeakParam); + } + } + + for (size_t I = 0, End = Callee->arg_size(); I < End; ++I) { if (Config.isSink(Callee->getArg(I)) && std::invoke(LeakIf, CB->getArgOperand(I))) { std::invoke(WithLeakFact, CB->getArgOperand(I)); @@ -113,7 +131,16 @@ void collectLeakedFacts(ContainerTy &Dest, const LLVMTaintConfig &Config, if (!Callee) { return; } - for (unsigned I = 0, End = Callee->arg_size(); I < End; ++I) { + + if (Config.isSinkFunction(Callee)) { + auto *LeakParam = CB->getArgOperand(Callee->hasStructRetAttr()); + assert(LeakParam); + if (std::invoke(LeakIf, LeakParam)) { + Dest.insert(LeakParam); + } + } + + for (size_t I = 0, End = Callee->arg_size(); I < End; ++I) { if (Config.isSink(Callee->getArg(I)) && LeakIf(CB->getArgOperand(I))) { Dest.insert(CB->getArgOperand(I)); } @@ -137,7 +164,14 @@ void collectSanitizedFacts(ContainerTy &Dest, const LLVMTaintConfig &Config, if (!Callee) { return; } - for (unsigned I = 0, End = Callee->arg_size(); I < End; ++I) { + + if (Config.isSanitizerFunction(Callee)) { + auto *SaniParam = CB->getArgOperand(Callee->hasStructRetAttr()); + assert(SaniParam); + Dest.insert(SaniParam); + } + + for (size_t I = 0, End = Callee->arg_size(); I < End; ++I) { if (Config.isSanitizer(Callee->getArg(I))) { Dest.insert(CB->getArgOperand(I)); } diff --git a/include/phasar/Utils/MapUtils.h b/include/phasar/Utils/MapUtils.h index 270ceb4eef..90298dfcaa 100644 --- a/include/phasar/Utils/MapUtils.h +++ b/include/phasar/Utils/MapUtils.h @@ -22,7 +22,7 @@ namespace psr { template requires std::is_lvalue_reference_v -inline auto getOrDefault(MapT &&Map, KeyT &&Key) -> ByConstRef< +[[nodiscard]] inline auto getOrDefault(MapT &&Map, KeyT &&Key) -> ByConstRef< std::remove_cvref_tsecond)>> { auto It = Map.find(PSR_FWD(Key)); if (It == Map.end()) { @@ -35,7 +35,7 @@ inline auto getOrDefault(MapT &&Map, KeyT &&Key) -> ByConstRef< template requires(std::is_lvalue_reference_v && !psr::CanEfficientlyPassByValue>) -inline auto getOrNull(MapT &&Map, KeyT &&Key) +[[nodiscard]] inline auto getOrNull(MapT &&Map, KeyT &&Key) -> decltype(&Map.find(PSR_FWD(Key))->second) { auto It = Map.find(PSR_FWD(Key)); decltype(&It->second) Ret = nullptr; @@ -49,7 +49,7 @@ inline auto getOrNull(MapT &&Map, KeyT &&Key) template requires(std::is_lvalue_reference_v && psr::CanEfficientlyPassByValue>) -inline auto getOrNull(MapT &&Map, KeyT Key) +[[nodiscard]] inline auto getOrNull(MapT &&Map, KeyT Key) -> decltype(&Map.find(Key)->second) { auto It = Map.find(PSR_FWD(Key)); decltype(&It->second) Ret = nullptr; @@ -60,10 +60,20 @@ inline auto getOrNull(MapT &&Map, KeyT Key) return Ret; } +[[nodiscard]] inline auto getOrNone(auto &&Map, auto &&Key) -> std::optional< + std::remove_cvref_tsecond)>> { + auto It = Map.find(PSR_FWD(Key)); + if (It != Map.end()) { + return It->second; + } + + return std::nullopt; +} + template requires CanEfficientlyPassByValue< typename std::remove_cvref_t::mapped_type> -inline auto getOr(MapT &&Map, KeyT &&Key, ValueT &&FallbackVal) +[[nodiscard]] inline auto getOr(MapT &&Map, KeyT &&Key, ValueT &&FallbackVal) -> std::remove_cvref_tsecond)> { auto It = Map.find(PSR_FWD(Key)); @@ -78,7 +88,7 @@ template requires(!CanEfficientlyPassByValue< typename std::remove_cvref_t::mapped_type> && std::is_lvalue_reference_v) -inline auto +[[nodiscard]] inline auto getOr(MapT &&Map, KeyT &&Key, const typename std::remove_cvref_t::mapped_type &FallbackVal) -> decltype(Map.find(PSR_FWD(Key))->second) const & { @@ -94,8 +104,9 @@ getOr(MapT &&Map, KeyT &&Key, template requires(!CanEfficientlyPassByValue< typename std::remove_cvref_t::mapped_type>) -auto getOr(MapT &&Map, KeyT &&Key, - const typename std::remove_cvref_t::mapped_type &&FallbackVal) +[[nodiscard]] auto +getOr(MapT &&Map, KeyT &&Key, + const typename std::remove_cvref_t::mapped_type &&FallbackVal) -> decltype(Map.find(PSR_FWD(Key))->second) const & = delete; } // namespace psr diff --git a/lib/PhasarLLVM/TaintConfig/LLVMTaintConfig.cpp b/lib/PhasarLLVM/TaintConfig/LLVMTaintConfig.cpp index 5383e77edd..d64a21b4a9 100644 --- a/lib/PhasarLLVM/TaintConfig/LLVMTaintConfig.cpp +++ b/lib/PhasarLLVM/TaintConfig/LLVMTaintConfig.cpp @@ -10,6 +10,7 @@ #include "phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h" #include "phasar/PhasarLLVM/ControlFlow/LLVMBasedCFG.h" +#include "phasar/PhasarLLVM/ControlFlow/LLVMVFTableProvider.h" #include "phasar/PhasarLLVM/DB/LLVMProjectIRDB.h" #include "phasar/PhasarLLVM/TaintConfig/TaintConfigBase.h" #include "phasar/PhasarLLVM/TaintConfig/TaintConfigData.h" @@ -23,13 +24,17 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" +#include "llvm/IR/Argument.h" #include "llvm/IR/DebugInfo.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/Function.h" #include "llvm/IR/InstIterator.h" +#include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Metadata.h" - -#include +#include "llvm/IR/Value.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/raw_ostream.h" namespace psr { @@ -222,7 +227,6 @@ LLVMTaintConfig::LLVMTaintConfig(const psr::LLVMProjectIRDB &Code, } LLVMTaintConfig::LLVMTaintConfig(const psr::LLVMProjectIRDB &AnnotatedCode) { - llvm::SmallVector VarAnnotations{}; llvm::SmallVector PtrAnnotations{}; for (const auto *F : AnnotatedCode.getAllFunctions()) { @@ -249,21 +253,47 @@ LLVMTaintConfig::LLVMTaintConfig(const psr::LLVMProjectIRDB &AnnotatedCode) { } } } + // handle "global" annotation declarations const auto *GlobalAnnotations = AnnotatedCode.getGlobalVariableDefinition("llvm.global.annotations"); + if (GlobalAnnotations) { for (const auto &Op : GlobalAnnotations->operands()) { if (auto *Array = llvm::dyn_cast(Op)) { for (auto &ArrayElem : Array->operands()) { if (auto *Struct = llvm::dyn_cast(ArrayElem)) { GlobalAnnotation A(Struct); - // get annotation string an remove prefix "psr." - auto AnnotationStr = A.getAnnotationString().substr(4); - const auto *Fun = A.getFunction(); - for (const auto &User : Fun->users()) { - if (llvm::isa(User)) { - addTaintCategory(User, AnnotationStr); + if (const auto *Fun = A.getFunction()) { + // get annotation string an remove prefix "psr." + auto AnnotStr = A.getAnnotationString().substr(4); + auto Kind = toTaintCategory(AnnotStr); + switch (Kind) { + case TaintCategory::None: + PHASAR_LOG_LEVEL(WARNING, + "Invalid taint category: '" + << A.getAnnotationString() + << "', expected one of psr.source, " + "psr.sink, psr.sanitizer"); + break; + case TaintCategory::Source: + AnnotatedSourceFunctions.insert(Fun); + break; + case TaintCategory::Sink: + case TaintCategory::Sanitizer: + if (Fun->arg_size() <= Fun->hasStructRetAttr()) { + PHASAR_LOG_LEVEL( + WARNING, + "Cannot annotate function '" + << Fun->getName() << "' with '" << AnnotStr + << "'; it would apply on the first argument, or the " + "implicit 'this' pointer."); + break; + } + (Kind == TaintCategory::Sink ? AnnotatedSinkFunctions + : AnnotatedSanitizerFunctions) + .insert(Fun); + break; } } } @@ -344,14 +374,54 @@ void LLVMTaintConfig::addTaintCategory(const llvm::Value *Val, // --- TaintConfigBase API function implementations // +bool LLVMTaintConfig::isSourceFunction(const llvm::Function *Func) const { + return AnnotatedSourceFunctions.contains(Func); +} + +bool LLVMTaintConfig::isSinkFunction(const llvm::Function *Func) const { + return AnnotatedSinkFunctions.contains(Func); +} + +bool LLVMTaintConfig::isSanitizerFunction(const llvm::Function *Func) const { + return AnnotatedSanitizerFunctions.contains(Func); +} + bool LLVMTaintConfig::isSourceImpl(const llvm::Value *V) const { - return SourceValues.contains(V); + if (SourceValues.contains(V)) { + return true; + } + + if (const auto *Call = llvm::dyn_cast(V)) { + if (const auto *Callee = llvm::dyn_cast( + Call->getCalledOperand()->stripPointerCastsAndAliases())) { + return AnnotatedSourceFunctions.contains(Callee); + } + } + return false; } + bool LLVMTaintConfig::isSinkImpl(const llvm::Value *V) const { - return SinkValues.contains(V); + if (SinkValues.contains(V)) { + return true; + } + + if (const auto *Arg = llvm::dyn_cast(V); + Arg && Arg->getArgNo() == Arg->getParent()->hasStructRetAttr()) { + return AnnotatedSinkFunctions.contains(Arg->getParent()); + } + return false; } + bool LLVMTaintConfig::isSanitizerImpl(const llvm::Value *V) const { - return SanitizerValues.contains(V); + if (SanitizerValues.contains(V)) { + return true; + } + + if (const auto *Arg = llvm::dyn_cast(V); + Arg && Arg->getArgNo() == Arg->getParent()->hasStructRetAttr()) { + return AnnotatedSanitizerFunctions.contains(Arg->getParent()); + } + return false; } void LLVMTaintConfig::forAllGeneratedValuesAtImpl( @@ -367,6 +437,10 @@ void LLVMTaintConfig::forAllGeneratedValuesAtImpl( } if (Callee) { + if (AnnotatedSourceFunctions.contains(Callee)) { + Handler(Inst); + } + for (const auto &Arg : Callee->args()) { if (SourceValues.contains(&Arg)) { Handler(Inst->getOperand(Arg.getArgNo())); @@ -402,6 +476,12 @@ void LLVMTaintConfig::forAllLeakCandidatesAtImpl( } if (Callee) { + if (isSinkFunction(Callee)) { + auto *LeakParam = Inst->getOperand(Callee->hasStructRetAttr()); + assert(LeakParam); + Handler(LeakParam); + } + for (const auto &Arg : Callee->args()) { if (SinkValues.contains(&Arg)) { Handler(Inst->getOperand(Arg.getArgNo())); @@ -428,6 +508,12 @@ void LLVMTaintConfig::forAllSanitizedValuesAtImpl( } if (Callee) { + if (isSanitizerFunction(Callee)) { + auto *SaniParam = Inst->getOperand(Callee->hasStructRetAttr()); + assert(SaniParam); + Handler(SaniParam); + } + for (const auto &Arg : Callee->args()) { if (SanitizerValues.contains(&Arg)) { Handler(Inst->getOperand(Arg.getArgNo())); @@ -444,10 +530,15 @@ bool LLVMTaintConfig::generatesValuesAtImpl( return true; } - if (Callee && llvm::any_of(Callee->args(), [this](const auto &Arg) { - return SourceValues.count(&Arg); - })) { - return true; + if (Callee) { + if (isSourceFunction(Callee)) { + return true; + } + if (llvm::any_of(Callee->args(), [this](const auto &Arg) { + return SourceValues.count(&Arg); + })) { + return true; + } } return SourceValues.contains(Inst) || @@ -464,9 +555,17 @@ bool LLVMTaintConfig::mayLeakValuesAtImpl(const llvm::Instruction *Inst, return true; } - return Callee && llvm::any_of(Callee->args(), [this](const auto &Arg) { - return SinkValues.count(&Arg); - }); + if (!Callee) { + return false; + } + + if (AnnotatedSinkFunctions.contains(Callee)) { + return true; + } + + return llvm::any_of(Callee->args(), [this](const auto &Arg) { + return SinkValues.count(&Arg); + }); } bool LLVMTaintConfig::sanitizesValuesAtImpl( @@ -477,9 +576,17 @@ bool LLVMTaintConfig::sanitizesValuesAtImpl( return true; } - return Callee && llvm::any_of(Callee->args(), [this](const auto &Arg) { - return SanitizerValues.count(&Arg); - }); + if (!Callee) { + return false; + } + + if (AnnotatedSanitizerFunctions.contains(Callee)) { + return true; + } + + return llvm::any_of(Callee->args(), [this](const auto &Arg) { + return SanitizerValues.count(&Arg); + }); } TaintCategory LLVMTaintConfig::getCategoryImpl(const llvm::Value *V) const { @@ -524,20 +631,35 @@ void LLVMTaintConfig::printImpl(llvm::raw_ostream &OS) const { OS << "empty\n"; return; } - OS << "\n\tSourceCallBack registered: " << (bool)SourceCallBack << '\n'; - OS << "\tSinkCallBack registered: " << (bool)SinkCallBack << '\n'; - OS << "\tSources (" << SourceValues.size() << "):\n"; + OS << "\n\tSourceCallBack registered: " << bool(SourceCallBack) << '\n'; + OS << "\tSinkCallBack registered: " << bool(SinkCallBack) << '\n'; + OS << "\tSource Values (" << SourceValues.size() << "):\n"; for (const auto *SourceValue : SourceValues) { OS << "\t\t" << psr::llvmIRToString(SourceValue) << '\n'; } - OS << "\tSinks (" << SinkValues.size() << "):\n"; + OS << "\tSource Functions (" << AnnotatedSourceFunctions.size() << "):\n"; + for (const auto *SrcFun : AnnotatedSourceFunctions) { + OS << "\t\t" << SrcFun->getName() << '\n'; + } + + OS << "\tSink Values (" << SinkValues.size() << "):\n"; for (const auto *SinkValue : SinkValues) { OS << "\t\t" << psr::llvmIRToString(SinkValue) << '\n'; } - OS << "\tSanitizers (" << SanitizerValues.size() << "):\n"; + OS << "\tSink Functions (" << AnnotatedSinkFunctions.size() << "):\n"; + for (const auto *SrcFun : AnnotatedSinkFunctions) { + OS << "\t\t" << SrcFun->getName() << '\n'; + } + + OS << "\tSanitizer Values (" << SanitizerValues.size() << "):\n"; for (const auto *SanitizerValue : SanitizerValues) { OS << "\t\t" << psr::llvmIRToString(SanitizerValue) << '\n'; } + OS << "\tSanitizer Functions (" << AnnotatedSanitizerFunctions.size() + << "):\n"; + for (const auto *SrcFun : AnnotatedSanitizerFunctions) { + OS << "\t\t" << SrcFun->getName() << '\n'; + } } template class TaintConfigBase; diff --git a/test/llvm_test_code/xtaint/CMakeLists.txt b/test/llvm_test_code/xtaint/CMakeLists.txt index 6cf0304842..bff1c48def 100644 --- a/test/llvm_test_code/xtaint/CMakeLists.txt +++ b/test/llvm_test_code/xtaint/CMakeLists.txt @@ -1,7 +1,6 @@ set(XTAINT_DBG_SOURCES xtaint01.cpp xtaint01_json.cpp - # xtaint01_json.cpp xtaint02.cpp xtaint03.cpp @@ -26,6 +25,8 @@ set(XTAINT_DBG_SOURCES xtaint21.cpp xtaint22.cpp xtaint23.cpp + xtaint24.cpp + xtaint25.cpp ) foreach(TEST_SRC ${XTAINT_DBG_SOURCES}) diff --git a/test/llvm_test_code/xtaint/xtaint10.cpp b/test/llvm_test_code/xtaint/xtaint10.cpp index 143f910f21..bb9a4edfec 100644 --- a/test/llvm_test_code/xtaint/xtaint10.cpp +++ b/test/llvm_test_code/xtaint/xtaint10.cpp @@ -37,7 +37,6 @@ int main() { // ifds-fieldsens-taint: /xtaint/xtaint10.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=monoifds-taint --module %llvm_test_code/xtaint/xtaint10_cpp_dbg.ll | FileCheck %s -check-prefix=monoifds-taint -// monoifds-taint: /xtaint/xtaint10.cpp:23:3: // monoifds-taint: /xtaint/xtaint10.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=sparse-ifds-taint --module %llvm_test_code/xtaint/xtaint10_cpp_dbg.ll | FileCheck %s -check-prefix=sparse-ifds-taint diff --git a/test/llvm_test_code/xtaint/xtaint11.cpp b/test/llvm_test_code/xtaint/xtaint11.cpp index 037c60853c..e43c8f6e87 100644 --- a/test/llvm_test_code/xtaint/xtaint11.cpp +++ b/test/llvm_test_code/xtaint/xtaint11.cpp @@ -37,7 +37,6 @@ int main() { // ifds-fieldsens-taint: /xtaint/xtaint11.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=monoifds-taint --module %llvm_test_code/xtaint/xtaint11_cpp_dbg.ll | FileCheck %s -check-prefix=monoifds-taint -// monoifds-taint: /xtaint/xtaint11.cpp:23:3: // monoifds-taint: /xtaint/xtaint11.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=sparse-ifds-taint --module %llvm_test_code/xtaint/xtaint11_cpp_dbg.ll | FileCheck %s -check-prefix=sparse-ifds-taint diff --git a/test/llvm_test_code/xtaint/xtaint13.cpp b/test/llvm_test_code/xtaint/xtaint13.cpp index 809f6917ac..3ac66207e0 100644 --- a/test/llvm_test_code/xtaint/xtaint13.cpp +++ b/test/llvm_test_code/xtaint/xtaint13.cpp @@ -28,7 +28,7 @@ int main() { // ifds-fieldsens-taint: /xtaint/xtaint13.cpp:17:3: // RUN: %phasar-cli --data-flow-analysis=monoifds-taint --module %llvm_test_code/xtaint/xtaint13_cpp_dbg.ll | FileCheck %s -check-prefix=monoifds-taint -// monoifds-taint: /xtaint/xtaint13.cpp:17:3 +// monoifds-taint: /xtaint/xtaint13.cpp:17:3: // RUN: %phasar-cli --data-flow-analysis=sparse-ifds-taint --module %llvm_test_code/xtaint/xtaint13_cpp_dbg.ll | FileCheck %s -check-prefix=sparse-ifds-taint // sparse-ifds-taint: /xtaint/xtaint13.cpp:17:3 diff --git a/test/llvm_test_code/xtaint/xtaint15.cpp b/test/llvm_test_code/xtaint/xtaint15.cpp index a86eee666e..cf7894d2aa 100644 --- a/test/llvm_test_code/xtaint/xtaint15.cpp +++ b/test/llvm_test_code/xtaint/xtaint15.cpp @@ -27,16 +27,18 @@ int main() { } // RUN: %phasar-cli --data-flow-analysis=ide-xtaint --module %llvm_test_code/xtaint/xtaint15_cpp_dbg.ll | FileCheck %s -check-prefix=ide-xtaint -// ide-xtaint: No leaks found! +// ide-xtaint: /xtaint/xtaint15.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=ifds-taint --module %llvm_test_code/xtaint/xtaint15_cpp_dbg.ll | FileCheck %s -check-prefix=ifds-taint -// ifds-taint: No leaks found! +// ifds-taint: /xtaint/xtaint15.cpp:23:3: +// ifds-taint: /xtaint/xtaint15.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=ifds-fieldsens-taint --module %llvm_test_code/xtaint/xtaint15_cpp_dbg.ll | FileCheck %s -check-prefix=ifds-fieldsens-taint -// ifds-fieldsens-taint: No leaks found! +// ifds-fieldsens-taint: xtaint/xtaint15.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=monoifds-taint --module %llvm_test_code/xtaint/xtaint15_cpp_dbg.ll | FileCheck %s -check-prefix=monoifds-taint -// monoifds-taint: No leaks found! +// monoifds-taint: xtaint/xtaint15.cpp:24:3: // RUN: %phasar-cli --data-flow-analysis=sparse-ifds-taint --module %llvm_test_code/xtaint/xtaint15_cpp_dbg.ll | FileCheck %s -check-prefix=sparse-ifds-taint -// sparse-ifds-taint: No leaks found! +// sparse-ifds-taint: /xtaint/xtaint15.cpp:23:3: +// sparse-ifds-taint: /xtaint/xtaint15.cpp:24:3: diff --git a/test/llvm_test_code/xtaint/xtaint24.cpp b/test/llvm_test_code/xtaint/xtaint24.cpp new file mode 100644 index 0000000000..fa255a5d10 --- /dev/null +++ b/test/llvm_test_code/xtaint/xtaint24.cpp @@ -0,0 +1,40 @@ +[[clang::annotate("psr.source")]] int source() { return 42; } +void sanitize([[clang::annotate("psr.sanitizer")]] int) noexcept {} +class Sink { +public: + virtual void sink([[clang::annotate("psr.sink")]] int) {} +}; + +Sink *makeSink() { return new Sink; } +extern void disposeSink(Sink *snk); + +int main() { + auto srcOne = source(); + auto srcTwo = source(); + + sanitize(srcOne); + + auto *SinkObj = makeSink(); + SinkObj->sink(srcOne); + SinkObj->sink(srcTwo); + + disposeSink(SinkObj); + disposeSink(SinkObj); +} + +// RUN: %phasar-cli --data-flow-analysis=ide-xtaint --module %llvm_test_code/xtaint/xtaint24_cpp_dbg.ll | FileCheck %s -check-prefix=ide-xtaint +// ide-xtaint: /xtaint/xtaint24.cpp:19:12: + +// RUN: %phasar-cli --data-flow-analysis=ifds-taint --module %llvm_test_code/xtaint/xtaint24_cpp_dbg.ll | FileCheck %s -check-prefix=ifds-taint +// ifds-taint: /xtaint/xtaint24.cpp:18:12: +// ifds-taint: /xtaint/xtaint24.cpp:19:12: + +// RUN: %phasar-cli --data-flow-analysis=ifds-fieldsens-taint --module %llvm_test_code/xtaint/xtaint24_cpp_dbg.ll | FileCheck %s -check-prefix=ifds-fieldsens-taint +// ifds-fieldsens-taint: /xtaint/xtaint24.cpp:19:12: + +// RUN: %phasar-cli --data-flow-analysis=monoifds-taint --module %llvm_test_code/xtaint/xtaint24_cpp_dbg.ll | FileCheck %s -check-prefix=monoifds-taint +// monoifds-taint: /xtaint/xtaint24.cpp:19:12: + +// RUN: %phasar-cli --data-flow-analysis=sparse-ifds-taint --module %llvm_test_code/xtaint/xtaint24_cpp_dbg.ll | FileCheck %s -check-prefix=sparse-ifds-taint +// sparse-ifds-taint: /xtaint/xtaint24.cpp:18:12: +// sparse-ifds-taint: /xtaint/xtaint24.cpp:19:12: diff --git a/test/llvm_test_code/xtaint/xtaint25.cpp b/test/llvm_test_code/xtaint/xtaint25.cpp new file mode 100644 index 0000000000..e260a3d036 --- /dev/null +++ b/test/llvm_test_code/xtaint/xtaint25.cpp @@ -0,0 +1,39 @@ +[[clang::annotate("psr.source")]] int source() { return 42; } +void sink([[clang::annotate("psr.sink")]] int) {} +class Sanitizer { +public: + virtual void sanitize([[clang::annotate("psr.sanitizer")]] int) noexcept {} +}; + +Sanitizer *makeSanitizer() { return new Sanitizer; } +extern void disposeSanitizer(Sanitizer *sanitizer); + +int main() { + auto srcOne = source(); + auto srcTwo = source(); + + auto *SanitizerObj = makeSanitizer(); + SanitizerObj->sanitize(srcOne); + + sink(srcOne); + sink(srcTwo); + + disposeSanitizer(SanitizerObj); +} + +// RUN: %phasar-cli --data-flow-analysis=ide-xtaint --module %llvm_test_code/xtaint/xtaint25_cpp_dbg.ll | FileCheck %s -check-prefix=ide-xtaint +// ide-xtaint: /xtaint/xtaint25.cpp:19:3: + +// RUN: %phasar-cli --data-flow-analysis=ifds-taint --module %llvm_test_code/xtaint/xtaint25_cpp_dbg.ll | FileCheck %s -check-prefix=ifds-taint +// ifds-taint: /xtaint/xtaint25.cpp:18:3: +// ifds-taint: /xtaint/xtaint25.cpp:19:3: + +// RUN: %phasar-cli --data-flow-analysis=ifds-fieldsens-taint --module %llvm_test_code/xtaint/xtaint25_cpp_dbg.ll | FileCheck %s -check-prefix=ifds-fieldsens-taint +// ifds-fieldsens-taint: /xtaint/xtaint25.cpp:19:3: + +// RUN: %phasar-cli --data-flow-analysis=monoifds-taint --module %llvm_test_code/xtaint/xtaint25_cpp_dbg.ll | FileCheck %s -check-prefix=monoifds-taint +// monoifds-taint: /xtaint/xtaint25.cpp:19:3: + +// RUN: %phasar-cli --data-flow-analysis=sparse-ifds-taint --module %llvm_test_code/xtaint/xtaint25_cpp_dbg.ll | FileCheck %s -check-prefix=sparse-ifds-taint +// sparse-ifds-taint: /xtaint/xtaint25.cpp:18:3: +// sparse-ifds-taint: /xtaint/xtaint25.cpp:19:3: diff --git a/tools/phasar-cli/Controller/AnalysisControllerXIFDSTaint.cpp b/tools/phasar-cli/Controller/AnalysisControllerXIFDSTaint.cpp index b1e68161d7..c4f21a628e 100644 --- a/tools/phasar-cli/Controller/AnalysisControllerXIFDSTaint.cpp +++ b/tools/phasar-cli/Controller/AnalysisControllerXIFDSTaint.cpp @@ -21,4 +21,6 @@ void controller::executeIFDSTaint(AnalysisController &Data) { Data.HA, &Config, Data.getEntryPoints(), false); IFDSSolver Solver(&Problem, &Data.HA.getICFG(), Data.SolverConfig); executeIfdsIdeAnalysisImpl(Solver, Data); + + llvm::outs() << Config << "\n"; } diff --git a/tools/phasar-cli/Controller/AnalysisControllerXMonoIFDSTaint.cpp b/tools/phasar-cli/Controller/AnalysisControllerXMonoIFDSTaint.cpp index eb170f7a93..35b9f5fded 100644 --- a/tools/phasar-cli/Controller/AnalysisControllerXMonoIFDSTaint.cpp +++ b/tools/phasar-cli/Controller/AnalysisControllerXMonoIFDSTaint.cpp @@ -29,6 +29,8 @@ void controller::executeMonoIFDSTaint(AnalysisController &Data) { // .setCGSCCs(&Data.HA->getCGSCCs()) // .setFunctionCompressor(&Data.HA->getCompressedFunctions()); + llvm::outs() << Config << "\n"; + monoifds::MonoIFDSSolver Solver(&TA, Data.HA); { diff --git a/unittests/PhasarLLVM/TaintConfig/TaintConfigTest.cpp b/unittests/PhasarLLVM/TaintConfig/TaintConfigTest.cpp index 5dac6f01f7..75ec2510eb 100644 --- a/unittests/PhasarLLVM/TaintConfig/TaintConfigTest.cpp +++ b/unittests/PhasarLLVM/TaintConfig/TaintConfigTest.cpp @@ -59,12 +59,13 @@ TEST_F(TaintConfigTest, Basic_01) { const std::string File = "basic_01_c_dbg.ll"; psr::LLVMProjectIRDB IR({PathToAttrTaintConfigTestCode + File}); psr::LLVMTaintConfig Config(IR); - llvm::outs() << Config << '\n'; + llvm::errs() << Config << '\n'; const auto *Bar = IR.getFunction("bar"); assert(Bar); for (const auto &User : Bar->users()) { if (llvm::isa(User)) { - ASSERT_TRUE(Config.isSource(User)); + ASSERT_TRUE(Config.isSource(User)) + << "Unexpected no source: " << psr::llvmIRToString(User); } } const auto *Baz = IR.getFunction("baz"); @@ -157,12 +158,8 @@ TEST_F(TaintConfigTest, FunMember_02) { ASSERT_TRUE(TConfig.isSource(I4)); // TODO sink annotation is missing for destructor const auto *Sanitizer = IR.getFunction("_ZN1X5sanitEv"); - assert(Sanitizer); - for (const auto &User : Sanitizer->users()) { - if (llvm::isa(User)) { - ASSERT_TRUE(TConfig.isSanitizer(User)); - } - } + ASSERT_TRUE(Sanitizer); + EXPECT_TRUE(TConfig.isSanitizer(Sanitizer->getArg(0))); } TEST_F(TaintConfigTest, NameMangling_01) {