Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc fixes #743

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
cmake_policy(SET CMP0077 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

# Allow overwriting cache variables of external projects from this CMakeLists file
cmake_policy(SET CMP0126 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0126 NEW)

# Allow portable use of CMAKE_VISIBILITY_INLINES_HIDDEN not only for shared libraries
cmake_policy(SET CMP0063 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
Expand Down Expand Up @@ -303,6 +307,9 @@ endif()
option(USE_LLVM_FAT_LIB "Link against libLLVM.so instead of the individual LLVM libraries if possible (default is OFF; always on if BUILD_SHARED_LIBS is ON)" OFF)

# LLVM
if (NOT PHASAR_LLVM_VERSION)
set(PHASAR_LLVM_VERSION 14)
endif()
include(add_llvm)
add_llvm()

Expand Down
2 changes: 1 addition & 1 deletion cmake/add_llvm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro(add_llvm)

if (NOT PHASAR_IN_TREE)
# Only search for LLVM if we build out of tree
find_package(LLVM 14 REQUIRED CONFIG)
find_package(LLVM ${PHASAR_LLVM_VERSION} REQUIRED CONFIG)
find_library(LLVM_LIBRARY NAMES LLVM PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH)

if(USE_LLVM_FAT_LIB AND ${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND")
Expand Down
13 changes: 12 additions & 1 deletion cmake/phasar_macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,21 @@ function(generate_ll_file)
endif()

if(GEN_LL_MEM2REG)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
get_filename_component(COMPILER_PATH_STR ${CMAKE_CXX_COMPILER} DIRECTORY)
find_program(OPT_TOOL opt HINTS ${COMPILER_PATH_STR})
else()
find_program(OPT_TOOL opt)
endif()

if(NOT OPT_TOOL)
set(OPT_TOOL opt)
endif()

add_custom_command(
OUTPUT ${test_code_ll_file}
COMMAND ${GEN_CMD} ${test_code_file_path} -o ${test_code_ll_file}
COMMAND ${CMAKE_CXX_COMPILER_LAUNCHER} opt -mem2reg -S -opaque-pointers=0 ${test_code_ll_file} -o ${test_code_ll_file}
COMMAND ${CMAKE_CXX_COMPILER_LAUNCHER} ${OPT_TOOL} -mem2reg -S -opaque-pointers=0 ${test_code_ll_file} -o ${test_code_ll_file}
COMMENT ${GEN_CMD_COMMENT}
DEPENDS ${GEN_LL_FILE}
VERBATIM
Expand Down
4 changes: 1 addition & 3 deletions include/phasar/DataFlow/IfdsIde/Solver/IFDSSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "phasar/DataFlow/IfdsIde/Solver/IDESolver.h"
#include "phasar/Domain/BinaryDomain.h"

#include <memory>
#include <set>
#include <type_traits>
#include <unordered_map>
Expand Down Expand Up @@ -118,8 +117,7 @@ using IFDSSolver_P = IFDSSolver<typename Problem::ProblemAnalysisDomain,

template <typename AnalysisDomainTy, typename Container>
OwningSolverResults<typename AnalysisDomainTy::n_t,
typename AnalysisDomainTy::d_t,
typename AnalysisDomainTy::l_t>
typename AnalysisDomainTy::d_t, BinaryDomain>
solveIFDSProblem(IFDSTabulationProblem<AnalysisDomainTy, Container> &Problem,
const typename AnalysisDomainTy::i_t &ICF) {
IFDSSolver<AnalysisDomainTy, Container> Solver(Problem, &ICF);
Expand Down
47 changes: 38 additions & 9 deletions include/phasar/DataFlow/Mono/Solver/IntraMonoSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#define PHASAR_DATAFLOW_MONO_SOLVER_INTRAMONOSOLVER_H

#include "phasar/DataFlow/Mono/IntraMonoProblem.h"
#include "phasar/Utils/BitVectorSet.h"
#include "phasar/PhasarLLVM/Utils/LLVMShorthands.h"
#include "phasar/Utils/DefaultValue.h"
#include "phasar/Utils/Utilities.h"

#include <deque>
#include <unordered_map>
Expand Down Expand Up @@ -109,20 +111,47 @@ template <typename AnalysisDomainTy> class IntraMonoSolver {
}
}

mono_container_t getResultsAt(n_t Stmt) { return Analysis[Stmt]; }
[[nodiscard]] const mono_container_t &getResultsAt(n_t Stmt) const {
auto It = Analysis.find(Stmt);
if (It != Analysis.end()) {
return It->second;
}
return getDefaultValue<mono_container_t>();
}

virtual void dumpResults(llvm::raw_ostream &OS = llvm::outs()) {
OS << "Intra-Monotone solver results:\n"
"------------------------------\n";
for (auto &[Node, FlowFacts] : this->Analysis) {
OS << "Instruction:\n" << NToString(Node);
OS << "\nFacts:\n";
OS << "\n***************************************************************\n"
<< "* Raw IntraMonoSolver results *\n"
<< "***************************************************************\n";

if (Analysis.empty()) {
OS << "No results computed!" << '\n';
return;
}

std::vector<std::pair<n_t, mono_container_t>> Cells;
Cells.reserve(Analysis.size());
Cells.insert(Cells.end(), Analysis.begin(), Analysis.end());

std::sort(Cells.begin(), Cells.end(), [](const auto &Lhs, const auto &Rhs) {
if constexpr (std::is_same_v<n_t, const llvm::Instruction *>) {
return StringIDLess{}(getMetaDataID(Lhs.first),
getMetaDataID(Rhs.first));
} else {
// If non-LLVM IR is used
return Lhs.first < Rhs.first;
}
});

for (const auto &[Node, FlowFacts] : Cells) {
OS << "Instruction: " << NToString(Node);
OS << "\nFacts: ";
if (FlowFacts.empty()) {
OS << "\tEMPTY\n";
OS << "EMPTY\n";
} else {
IMProblem.printContainer(OS, FlowFacts);
}
OS << "\n\n";
OS << "\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class CHAResolver : public Resolver {
CHAResolver(const LLVMProjectIRDB *IRDB, const LLVMVFTableProvider *VTP,
const LLVMTypeHierarchy *TH);

~CHAResolver() override = default;
// Deleting an incomplete type (LLVMTypeHierarchy) is UB, so instantiate the
// dtor in CHAResolver.cpp
~CHAResolver() override;

FunctionSetTy resolveVirtualCall(const llvm::CallBase *CallSite) override;

Expand Down
22 changes: 21 additions & 1 deletion include/phasar/PhasarLLVM/ControlFlow/Resolver/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,29 @@ class LLVMVFTableProvider;
class LLVMTypeHierarchy;
enum class CallGraphAnalysisType;

/// Assuming that `CallSite` is a virtual call through a vtable, retrieves the
/// index in the vtable of the virtual function called.
[[nodiscard]] std::optional<unsigned>
getVFTIndex(const llvm::CallBase *CallSite);

/// Assuming that `CallSite` is a vall to a non-static member function,
/// retrieves the type of the receiver. Returns nullptr, if the receiver-type
/// could not be extracted
[[nodiscard]] const llvm::StructType *
getReceiverType(const llvm::CallBase *CallSite);

/// Assuming that `CallSite` is a virtual call, where `Idx` is retrieved through
/// `getVFTIndex()` and `T` through `getReceiverType()`
[[nodiscard]] const llvm::Function *
getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
const llvm::CallBase *CallSite,
const psr::LLVMVFTableProvider &VTP);

[[nodiscard]] std::string getReceiverTypeName(const llvm::CallBase *CallSite);

/// Checks whether the signature of `DestFun` matches the required withature of
/// `CallSite`, such that `DestFun` qualifies as callee-candidate, if `CallSite`
/// is an indirect/virtual call.
[[nodiscard]] bool isConsistentCall(const llvm::CallBase *CallSite,
const llvm::Function *DestFun);

Expand All @@ -59,7 +74,12 @@ class Resolver {

const llvm::Function *
getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
const llvm::CallBase *CallSite);
const llvm::CallBase *CallSite) {
if (!VTP) {
return nullptr;
}
return psr::getNonPureVirtualVFTEntry(T, Idx, CallSite, *VTP);
}

public:
using FunctionSetTy = llvm::SmallDenseSet<const llvm::Function *, 4>;
Expand Down
7 changes: 4 additions & 3 deletions include/phasar/PhasarLLVM/DB/LLVMProjectIRDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
Expand All @@ -44,7 +43,8 @@ class LLVMProjectIRDB : public ProjectIRDBBase<LLVMProjectIRDB> {
/// Reads and parses the given LLVM IR file and owns the resulting IR Module.
/// If an error occurs, an error message is written to stderr and subsequent
/// calls to isValid() return false.
explicit LLVMProjectIRDB(const llvm::Twine &IRFileName);
explicit LLVMProjectIRDB(const llvm::Twine &IRFileName,
bool EnableOpaquePointers = LLVM_VERSION_MAJOR > 14);
/// Initializes the new ProjectIRDB with the given IR Module _without_ taking
/// ownership. The module is optionally being preprocessed.
///
Expand All @@ -58,7 +58,8 @@ class LLVMProjectIRDB : public ProjectIRDBBase<LLVMProjectIRDB> {
/// Parses the given LLVM IR file and owns the resulting IR Module.
/// If an error occurs, an error message is written to stderr and subsequent
/// calls to isValid() return false.
explicit LLVMProjectIRDB(llvm::MemoryBufferRef Buf);
explicit LLVMProjectIRDB(llvm::MemoryBufferRef Buf,
bool EnableOpaquePointers = LLVM_VERSION_MAJOR > 14);

LLVMProjectIRDB(const LLVMProjectIRDB &) = delete;
LLVMProjectIRDB &operator=(LLVMProjectIRDB &) = delete;
Expand Down
4 changes: 4 additions & 0 deletions include/phasar/PhasarLLVM/TaintConfig/TaintConfigData.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace psr {
enum class TaintCategory;

struct FunctionData {
#if __cplusplus < 202002L
FunctionData() noexcept = default;
#endif

std::string Name;
TaintCategory ReturnCat{};
Expand All @@ -29,7 +31,9 @@ struct FunctionData {
};

struct VariableData {
#if __cplusplus < 202002L
VariableData() noexcept = default;
#endif

size_t Line{};
std::string Name;
Expand Down
2 changes: 2 additions & 0 deletions lib/PhasarLLVM/ControlFlow/Resolver/CHAResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ CHAResolver::CHAResolver(const LLVMProjectIRDB *IRDB,
}
}

CHAResolver::~CHAResolver() = default;

auto CHAResolver::resolveVirtualCall(const llvm::CallBase *CallSite)
-> FunctionSetTy {
PHASAR_LOG_LEVEL(DEBUG, "Call virtual function: ");
Expand Down
32 changes: 16 additions & 16 deletions lib/PhasarLLVM/ControlFlow/Resolver/Resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ const llvm::StructType *psr::getReceiverType(const llvm::CallBase *CallSite) {
return nullptr;
}

const llvm::Function *
psr::getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
const llvm::CallBase *CallSite,
const LLVMVFTableProvider &VTP) {

if (const auto *VT = VTP.getVFTableOrNull(T)) {
const auto *Target = VT->getFunction(Idx);
if (Target && Target->getName() != LLVMTypeHierarchy::PureVirtualCallName &&
isConsistentCall(CallSite, Target)) {
return Target;
}
}

return nullptr;
}

std::string psr::getReceiverTypeName(const llvm::CallBase *CallSite) {
const auto *RT = getReceiverType(CallSite);
if (RT) {
Expand Down Expand Up @@ -134,22 +150,6 @@ Resolver::Resolver(const LLVMProjectIRDB *IRDB, const LLVMVFTableProvider *VTP)
assert(VTP != nullptr);
}

const llvm::Function *
Resolver::getNonPureVirtualVFTEntry(const llvm::StructType *T, unsigned Idx,
const llvm::CallBase *CallSite) {
if (!VTP) {
return nullptr;
}
if (const auto *VT = VTP->getVFTableOrNull(T)) {
const auto *Target = VT->getFunction(Idx);
if (Target && Target->getName() != LLVMTypeHierarchy::PureVirtualCallName &&
isConsistentCall(CallSite, Target)) {
return Target;
}
}
return nullptr;
}

void Resolver::preCall(const llvm::Instruction *Inst) {}

void Resolver::handlePossibleTargets(const llvm::CallBase *CallSite,
Expand Down
25 changes: 21 additions & 4 deletions lib/PhasarLLVM/DB/LLVMProjectIRDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@

namespace psr {

static void setOpaquePointersForCtx(llvm::LLVMContext &Ctx, bool Enable) {
#if LLVM_VERSION_MAJOR >= 15 && LLVM_VERSION_MAJOR < 17
if (!Enable) {
Ctx.setOpaquePointers(false);
}
#elif LLVM_VERSION_MAJOR < 15
if (Enable) {
Ctx.enableOpaquePointers();
}
#else // LLVM_VERSION_MAJOR >= 17
#error \
"Non-opaque pointers are not supported anymore. Refactor PhASAR to remove typed pointer support."
#endif
}

std::unique_ptr<llvm::Module>
LLVMProjectIRDB::getParsedIRModuleOrNull(llvm::MemoryBufferRef IRFileContent,
llvm::LLVMContext &Ctx) noexcept {
Expand Down Expand Up @@ -61,8 +76,9 @@ LLVMProjectIRDB::getParsedIRModuleOrNull(const llvm::Twine &IRFileName,
return getParsedIRModuleOrNull(*FileOrErr.get(), Ctx);
}

LLVMProjectIRDB::LLVMProjectIRDB(const llvm::Twine &IRFileName) {

LLVMProjectIRDB::LLVMProjectIRDB(const llvm::Twine &IRFileName,
bool EnableOpaquePointers) {
setOpaquePointersForCtx(Ctx, EnableOpaquePointers);
auto M = getParsedIRModuleOrNull(IRFileName, Ctx);

if (!M) {
Expand Down Expand Up @@ -155,8 +171,9 @@ LLVMProjectIRDB::LLVMProjectIRDB(std::unique_ptr<llvm::Module> Mod,
}
}

LLVMProjectIRDB::LLVMProjectIRDB(llvm::MemoryBufferRef Buf) {
llvm::SMDiagnostic Diag;
LLVMProjectIRDB::LLVMProjectIRDB(llvm::MemoryBufferRef Buf,
bool EnableOpaquePointers) {
setOpaquePointersForCtx(Ctx, EnableOpaquePointers);
auto M = getParsedIRModuleOrNull(Buf, Ctx);
if (!M) {
return;
Expand Down