Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions include/phasar/PhasarLLVM/TaintConfig/LLVMTaintConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

#include "phasar/PhasarLLVM/TaintConfig/TaintConfigBase.h"

#include "llvm/IR/Instruction.h"
#include "llvm/ADT/DenseSet.h"

#include <unordered_set>
namespace llvm {
class Instruction;
class Value;
class Function;
} // namespace llvm

namespace psr {
class LLVMTaintConfig;
Expand Down Expand Up @@ -44,6 +48,10 @@ class LLVMTaintConfig : public TaintConfigBase<LLVMTaintConfig> {
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;
Expand Down Expand Up @@ -100,10 +108,12 @@ class LLVMTaintConfig : public TaintConfigBase<LLVMTaintConfig> {
const TaintConfigData &Config);

// --- data members

std::unordered_set<const llvm::Value *> SourceValues;
std::unordered_set<const llvm::Value *> SinkValues;
std::unordered_set<const llvm::Value *> SanitizerValues;
llvm::DenseSet<const llvm::Function *> AnnotatedSourceFunctions;
llvm::DenseSet<const llvm::Function *> AnnotatedSinkFunctions;
llvm::DenseSet<const llvm::Function *> AnnotatedSanitizerFunctions;
llvm::DenseSet<const llvm::Value *> SourceValues;
llvm::DenseSet<const llvm::Value *> SinkValues;
llvm::DenseSet<const llvm::Value *> SanitizerValues;
};

extern template class TaintConfigBase<LLVMTaintConfig>;
Expand Down
40 changes: 37 additions & 3 deletions include/phasar/PhasarLLVM/TaintConfig/TaintConfigUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down
25 changes: 18 additions & 7 deletions include/phasar/Utils/MapUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace psr {

template <typename MapT, typename KeyT>
requires std::is_lvalue_reference_v<MapT>
inline auto getOrDefault(MapT &&Map, KeyT &&Key) -> ByConstRef<
[[nodiscard]] inline auto getOrDefault(MapT &&Map, KeyT &&Key) -> ByConstRef<
std::remove_cvref_t<decltype(Map.find(PSR_FWD(Key))->second)>> {
auto It = Map.find(PSR_FWD(Key));
if (It == Map.end()) {
Expand All @@ -35,7 +35,7 @@ inline auto getOrDefault(MapT &&Map, KeyT &&Key) -> ByConstRef<
template <typename MapT, typename KeyT>
requires(std::is_lvalue_reference_v<MapT> &&
!psr::CanEfficientlyPassByValue<llvm::remove_cvref_t<KeyT>>)
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;
Expand All @@ -49,7 +49,7 @@ inline auto getOrNull(MapT &&Map, KeyT &&Key)
template <typename MapT, typename KeyT>
requires(std::is_lvalue_reference_v<MapT> &&
psr::CanEfficientlyPassByValue<llvm::remove_cvref_t<KeyT>>)
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;
Expand All @@ -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_t<decltype(Map.find(PSR_FWD(Key))->second)>> {
auto It = Map.find(PSR_FWD(Key));
if (It != Map.end()) {
return It->second;
}

return std::nullopt;
}

template <typename MapT, typename KeyT, typename ValueT>
requires CanEfficientlyPassByValue<
typename std::remove_cvref_t<MapT>::mapped_type>
inline auto getOr(MapT &&Map, KeyT &&Key, ValueT &&FallbackVal)
[[nodiscard]] inline auto getOr(MapT &&Map, KeyT &&Key, ValueT &&FallbackVal)
-> std::remove_cvref_t<decltype(Map.find(PSR_FWD(Key))->second)> {
auto It = Map.find(PSR_FWD(Key));

Expand All @@ -78,7 +88,7 @@ template <typename MapT, typename KeyT>
requires(!CanEfficientlyPassByValue<
typename std::remove_cvref_t<MapT>::mapped_type> &&
std::is_lvalue_reference_v<MapT>)
inline auto
[[nodiscard]] inline auto
getOr(MapT &&Map, KeyT &&Key,
const typename std::remove_cvref_t<MapT>::mapped_type &FallbackVal)
-> decltype(Map.find(PSR_FWD(Key))->second) const & {
Expand All @@ -94,8 +104,9 @@ getOr(MapT &&Map, KeyT &&Key,
template <typename MapT, typename KeyT>
requires(!CanEfficientlyPassByValue<
typename std::remove_cvref_t<MapT>::mapped_type>)
auto getOr(MapT &&Map, KeyT &&Key,
const typename std::remove_cvref_t<MapT>::mapped_type &&FallbackVal)
[[nodiscard]] auto
getOr(MapT &&Map, KeyT &&Key,
const typename std::remove_cvref_t<MapT>::mapped_type &&FallbackVal)
-> decltype(Map.find(PSR_FWD(Key))->second) const & = delete;
} // namespace psr

Expand Down
Loading
Loading