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

Adds helper function to determine if a function was generated by phasar #665

Merged
merged 4 commits into from
Sep 10, 2023
Merged
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
12 changes: 12 additions & 0 deletions include/phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
static constexpr llvm::StringLiteral GlobalCRuntimeModelName =
"__psrCRuntimeGlobalCtorsModel";

static constexpr llvm::StringLiteral GlobalCRuntimeDtorModelName =
"__psrCRuntimeGlobalDtorsModel";

static constexpr llvm::StringLiteral GlobalCRuntimeDtorsCallerName =
"__psrGlobalDtorsCaller";

static constexpr llvm::StringLiteral GlobalCRuntimeUserEntrySelectorName =
"__psrCRuntimeUserEntrySelector";

/// Constructs the ICFG based on the given IRDB and the entry-points using a
/// fixpoint iteration. This may take a long time.
///
Expand Down Expand Up @@ -119,6 +128,9 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
/// Gets the underlying IRDB
[[nodiscard]] LLVMProjectIRDB *getIRDB() const noexcept { return IRDB; }

/// Returns true, if a function was generated by phasar.
[[nodiscard]] static bool isPhasarGenerated(const llvm::Function &) noexcept;

using CFGBase::print;
using ICFGBase::print;

Expand Down
14 changes: 14 additions & 0 deletions lib/PhasarLLVM/ControlFlow/LLVMBasedICFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -380,6 +381,19 @@ LLVMBasedICFG::LLVMBasedICFG(LLVMProjectIRDB *IRDB,

LLVMBasedICFG::~LLVMBasedICFG() = default;

bool LLVMBasedICFG::isPhasarGenerated(const llvm::Function &F) noexcept {
if (F.hasName()) {
llvm::StringRef FunctionName = F.getName();
return llvm::StringSwitch<bool>(FunctionName)
.Cases(GlobalCRuntimeModelName, GlobalCRuntimeDtorModelName,
GlobalCRuntimeDtorsCallerName,
GlobalCRuntimeUserEntrySelectorName, true)
.Default(false);
}

return false;
}

[[nodiscard]] FunctionRange LLVMBasedICFG::getAllFunctionsImpl() const {
return IRDB->getAllFunctions();
}
Expand Down
11 changes: 6 additions & 5 deletions lib/PhasarLLVM/ControlFlow/LLVMBasedICFGGlobalsImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ static llvm::Function *createDtorCallerForModule(
&RegisteredDtors) {

auto *PhasarDtorCaller = llvm::cast<llvm::Function>(
Mod.getOrInsertFunction("__psrGlobalDtorsCaller." +
getReducedModuleName(Mod),
llvm::Type::getVoidTy(Mod.getContext()))
Mod.getOrInsertFunction(
LLVMBasedICFG::GlobalCRuntimeDtorsCallerName.str() + '.' +
getReducedModuleName(Mod),
llvm::Type::getVoidTy(Mod.getContext()))
.getCallee());

auto *BB =
Expand Down Expand Up @@ -195,7 +196,7 @@ static std::pair<llvm::Function *, bool> buildCRuntimeGlobalDtorsModel(

auto &CTX = M.getContext();
auto *Cleanup = llvm::cast<llvm::Function>(
M.getOrInsertFunction("__psrCRuntimeGlobalDtorsModel",
M.getOrInsertFunction(LLVMBasedICFG::GlobalCRuntimeDtorModelName,
llvm::Type::getVoidTy(CTX))
.getCallee());

Expand Down Expand Up @@ -301,7 +302,7 @@ llvm::Function *LLVMBasedICFG::buildCRuntimeGlobalCtorsDtorsModel(
} else {

auto UEntrySelectorFn = M.getOrInsertFunction(
"__psrCRuntimeUserEntrySelector", llvm::Type::getInt32Ty(CTX));
GlobalCRuntimeUserEntrySelectorName, llvm::Type::getInt32Ty(CTX));

auto *UEntrySelector = IRB.CreateCall(UEntrySelectorFn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ TEST_F(LLVMBasedICFGGlobCtorDtorTest, CtorTest) {

// GlobalCtor->print(llvm::outs());

ensureFunctionOrdering(GlobalCtor, ICFG,
{{"_GLOBAL__sub_I_globals_ctor_1.cpp", "main"},
{"main", "__psrCRuntimeGlobalDtorsModel"}});
ensureFunctionOrdering(
GlobalCtor, ICFG,
{{"_GLOBAL__sub_I_globals_ctor_1.cpp", "main"},
{"main", LLVMBasedICFG::GlobalCRuntimeDtorModelName}});
}

TEST_F(LLVMBasedICFGGlobCtorDtorTest, CtorTest2) {
Expand Down Expand Up @@ -144,10 +145,12 @@ TEST_F(LLVMBasedICFGGlobCtorDtorTest, DtorTest1) {
ensureFunctionOrdering(
GlobalCtor, ICFG,
{{"_GLOBAL__sub_I_globals_dtor_1.cpp", "main"},
{"main", "__psrGlobalDtorsCaller.globals_dtor_1_cpp.ll"}});
{"main", LLVMBasedICFG::GlobalCRuntimeDtorsCallerName.str() +
".globals_dtor_1_cpp.ll"}});

auto *GlobalDtor =
IRDB.getFunction("__psrGlobalDtorsCaller.globals_dtor_1_cpp.ll");
IRDB.getFunction(LLVMBasedICFG::GlobalCRuntimeDtorsCallerName.str() +
".globals_dtor_1_cpp.ll");

ASSERT_NE(nullptr, GlobalDtor);

Expand Down
4 changes: 2 additions & 2 deletions unittests/PhasarLLVM/ControlFlow/LLVMBasedICFGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ TEST(LLVMBasedICFGTest, StaticCallSite_2b) {
const llvm::Function *FOO = IRDB.getFunctionDefinition("foo");
const llvm::Function *BAR = IRDB.getFunctionDefinition("bar");
const llvm::Function *CTOR =
IRDB.getFunctionDefinition("__psrCRuntimeGlobalCtorsModel");
IRDB.getFunctionDefinition(LLVMBasedICFG::GlobalCRuntimeModelName);
const llvm::Function *DTOR =
IRDB.getFunctionDefinition("__psrCRuntimeGlobalDtorsModel");
IRDB.getFunctionDefinition(LLVMBasedICFG::GlobalCRuntimeDtorModelName);
ASSERT_TRUE(F);
ASSERT_TRUE(FOO);
ASSERT_TRUE(BAR);
Expand Down
Loading