Skip to content

Commit 76fe080

Browse files
vulderMMoryMartin Moryfabianbs96
authored
Adds helper function to determine if a function was generated by phasar (#665)
* Adds helper function to determine if a function was generated by phasar During global analysis, phasar generates helper function to correctly handle global ctors/dtors and other global code fragments. With the new checking function, users can determine whether a given function was generated by phasar or not, e.g., for global analysis. * get rid of magic string literals that name the various generated functions of phasar's global ctor/dtor modelling * minor style --------- Co-authored-by: Martin Mory <[email protected]> Co-authored-by: Martin Mory <[email protected]> Co-authored-by: Fabian Schiebel <[email protected]>
1 parent c78dbe0 commit 76fe080

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

include/phasar/PhasarLLVM/ControlFlow/LLVMBasedICFG.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
5555
static constexpr llvm::StringLiteral GlobalCRuntimeModelName =
5656
"__psrCRuntimeGlobalCtorsModel";
5757

58+
static constexpr llvm::StringLiteral GlobalCRuntimeDtorModelName =
59+
"__psrCRuntimeGlobalDtorsModel";
60+
61+
static constexpr llvm::StringLiteral GlobalCRuntimeDtorsCallerName =
62+
"__psrGlobalDtorsCaller";
63+
64+
static constexpr llvm::StringLiteral GlobalCRuntimeUserEntrySelectorName =
65+
"__psrCRuntimeUserEntrySelector";
66+
5867
/// Constructs the ICFG based on the given IRDB and the entry-points using a
5968
/// fixpoint iteration. This may take a long time.
6069
///
@@ -119,6 +128,9 @@ class LLVMBasedICFG : public LLVMBasedCFG, public ICFGBase<LLVMBasedICFG> {
119128
/// Gets the underlying IRDB
120129
[[nodiscard]] LLVMProjectIRDB *getIRDB() const noexcept { return IRDB; }
121130

131+
/// Returns true, if a function was generated by phasar.
132+
[[nodiscard]] static bool isPhasarGenerated(const llvm::Function &) noexcept;
133+
122134
using CFGBase::print;
123135
using ICFGBase::print;
124136

lib/PhasarLLVM/ControlFlow/LLVMBasedICFG.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "llvm/ADT/STLExtras.h"
3131
#include "llvm/ADT/SmallVector.h"
32+
#include "llvm/ADT/StringSwitch.h"
3233
#include "llvm/IR/Function.h"
3334
#include "llvm/IR/Instruction.h"
3435
#include "llvm/Support/ErrorHandling.h"
@@ -380,6 +381,19 @@ LLVMBasedICFG::LLVMBasedICFG(LLVMProjectIRDB *IRDB,
380381

381382
LLVMBasedICFG::~LLVMBasedICFG() = default;
382383

384+
bool LLVMBasedICFG::isPhasarGenerated(const llvm::Function &F) noexcept {
385+
if (F.hasName()) {
386+
llvm::StringRef FunctionName = F.getName();
387+
return llvm::StringSwitch<bool>(FunctionName)
388+
.Cases(GlobalCRuntimeModelName, GlobalCRuntimeDtorModelName,
389+
GlobalCRuntimeDtorsCallerName,
390+
GlobalCRuntimeUserEntrySelectorName, true)
391+
.Default(false);
392+
}
393+
394+
return false;
395+
}
396+
383397
[[nodiscard]] FunctionRange LLVMBasedICFG::getAllFunctionsImpl() const {
384398
return IRDB->getAllFunctions();
385399
}

lib/PhasarLLVM/ControlFlow/LLVMBasedICFGGlobalsImpl.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ static llvm::Function *createDtorCallerForModule(
124124
&RegisteredDtors) {
125125

126126
auto *PhasarDtorCaller = llvm::cast<llvm::Function>(
127-
Mod.getOrInsertFunction("__psrGlobalDtorsCaller." +
128-
getReducedModuleName(Mod),
129-
llvm::Type::getVoidTy(Mod.getContext()))
127+
Mod.getOrInsertFunction(
128+
LLVMBasedICFG::GlobalCRuntimeDtorsCallerName.str() + '.' +
129+
getReducedModuleName(Mod),
130+
llvm::Type::getVoidTy(Mod.getContext()))
130131
.getCallee());
131132

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

196197
auto &CTX = M.getContext();
197198
auto *Cleanup = llvm::cast<llvm::Function>(
198-
M.getOrInsertFunction("__psrCRuntimeGlobalDtorsModel",
199+
M.getOrInsertFunction(LLVMBasedICFG::GlobalCRuntimeDtorModelName,
199200
llvm::Type::getVoidTy(CTX))
200201
.getCallee());
201202

@@ -301,7 +302,7 @@ llvm::Function *LLVMBasedICFG::buildCRuntimeGlobalCtorsDtorsModel(
301302
} else {
302303

303304
auto UEntrySelectorFn = M.getOrInsertFunction(
304-
"__psrCRuntimeUserEntrySelector", llvm::Type::getInt32Ty(CTX));
305+
GlobalCRuntimeUserEntrySelectorName, llvm::Type::getInt32Ty(CTX));
305306

306307
auto *UEntrySelector = IRB.CreateCall(UEntrySelectorFn);
307308

unittests/PhasarLLVM/ControlFlow/LLVMBasedICFGGlobCtorDtorTest.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ TEST_F(LLVMBasedICFGGlobCtorDtorTest, CtorTest) {
9393

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

96-
ensureFunctionOrdering(GlobalCtor, ICFG,
97-
{{"_GLOBAL__sub_I_globals_ctor_1.cpp", "main"},
98-
{"main", "__psrCRuntimeGlobalDtorsModel"}});
96+
ensureFunctionOrdering(
97+
GlobalCtor, ICFG,
98+
{{"_GLOBAL__sub_I_globals_ctor_1.cpp", "main"},
99+
{"main", LLVMBasedICFG::GlobalCRuntimeDtorModelName}});
99100
}
100101

101102
TEST_F(LLVMBasedICFGGlobCtorDtorTest, CtorTest2) {
@@ -144,10 +145,12 @@ TEST_F(LLVMBasedICFGGlobCtorDtorTest, DtorTest1) {
144145
ensureFunctionOrdering(
145146
GlobalCtor, ICFG,
146147
{{"_GLOBAL__sub_I_globals_dtor_1.cpp", "main"},
147-
{"main", "__psrGlobalDtorsCaller.globals_dtor_1_cpp.ll"}});
148+
{"main", LLVMBasedICFG::GlobalCRuntimeDtorsCallerName.str() +
149+
".globals_dtor_1_cpp.ll"}});
148150

149151
auto *GlobalDtor =
150-
IRDB.getFunction("__psrGlobalDtorsCaller.globals_dtor_1_cpp.ll");
152+
IRDB.getFunction(LLVMBasedICFG::GlobalCRuntimeDtorsCallerName.str() +
153+
".globals_dtor_1_cpp.ll");
151154

152155
ASSERT_NE(nullptr, GlobalDtor);
153156

unittests/PhasarLLVM/ControlFlow/LLVMBasedICFGTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ TEST(LLVMBasedICFGTest, StaticCallSite_2b) {
8383
const llvm::Function *FOO = IRDB.getFunctionDefinition("foo");
8484
const llvm::Function *BAR = IRDB.getFunctionDefinition("bar");
8585
const llvm::Function *CTOR =
86-
IRDB.getFunctionDefinition("__psrCRuntimeGlobalCtorsModel");
86+
IRDB.getFunctionDefinition(LLVMBasedICFG::GlobalCRuntimeModelName);
8787
const llvm::Function *DTOR =
88-
IRDB.getFunctionDefinition("__psrCRuntimeGlobalDtorsModel");
88+
IRDB.getFunctionDefinition(LLVMBasedICFG::GlobalCRuntimeDtorModelName);
8989
ASSERT_TRUE(F);
9090
ASSERT_TRUE(FOO);
9191
ASSERT_TRUE(BAR);

0 commit comments

Comments
 (0)