Skip to content

Commit

Permalink
- fetch upsteam
Browse files Browse the repository at this point in the history
- add callgraph node to tugraph
- add callgraph edges to tugraph
  • Loading branch information
JasonZhongZexin committed Oct 28, 2024
1 parent a68b293 commit 064d65b
Show file tree
Hide file tree
Showing 73 changed files with 1,296 additions and 536 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ set(CMAKE_INSTALL_RPATH ${Z3_INCLUDES})
link_libraries(${Z3_LIBRARIES})
include_directories(SYSTEM ${Z3_INCLUDES})

# Add the libneo4j library to all targets of SVF
find_library(NEO4J_CLIENT_LIBRARIES neo4j-client REQUIRED PATHS /usr/local/lib)
include_directories(SYSTEM /usr/local/include)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};/usr/local/lib")
include_directories(SYSTEM /usr/local/include)
link_directories(/usr/local/lib)
link_libraries(/usr/local/lib)


# Add the actual SVF and SVF-LLVM targets
add_subdirectory(svf)
add_subdirectory(svf-llvm)
Expand Down
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ if [[ ! -d "$Z3_DIR" ]]; then
fi

# Add LLVM & Z3 to $PATH and $LD_LIBRARY_PATH (prepend so that selected instances will be used first)
PATH=$LLVM_DIR/bin:$Z3_DIR/bin:$PATH
LD_LIBRARY_PATH=$LLVM_DIR/lib:$Z3_BIN/lib:$LD_LIBRARY_PATH
PATH=$LLVM_DIR/bin:$Z3_DIR/bin:/usr/local/lib:$PATH
LD_LIBRARY_PATH=$LLVM_DIR/lib:$Z3_BIN/lib:/usr/local/lib:$LD_LIBRARY_PATH

echo "LLVM_DIR=$LLVM_DIR"
echo "Z3_DIR=$Z3_DIR"
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ set_target_properties(SvfLLVM PROPERTIES
)

# Link LLVM's libraries to SvfLLVM, as well as the SVF core library
target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} SvfCore)
target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfCore)

# Get the source files (i.e. all *.c/*.cpp files) for SVF's subprojects and add them to SvfLLVM
file(GLOB_RECURSE SVF_LLVM_SOURCES lib/*.cpp)
Expand Down
4 changes: 2 additions & 2 deletions svf-llvm/include/SVF-LLVM/ICFGBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class ICFGBuilder
public:
typedef FIFOWorkList<const Instruction*> WorkList;

ICFGBuilder(): icfg(new ICFG())
ICFGBuilder(ICFG* i): icfg(i)
{

}
ICFG* build();
void build();

private:

Expand Down
7 changes: 0 additions & 7 deletions svf-llvm/include/SVF-LLVM/LLVMModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class LLVMModuleSet
static bool preProcessed;
SymbolTableInfo* symInfo;
SVFModule* svfModule; ///< Borrowed from singleton SVFModule::svfModule
ICFG* icfg;
std::unique_ptr<LLVMContext> owned_ctx;
std::vector<std::unique_ptr<Module>> owned_modules;
std::vector<std::reference_wrapper<Module>> modules;
Expand Down Expand Up @@ -105,7 +104,6 @@ class LLVMModuleSet
InstToBlockNodeMapTy InstToBlockNodeMap; ///< map a basic block to its ICFGNode
FunToFunEntryNodeMapTy FunToFunEntryNodeMap; ///< map a function to its FunExitICFGNode
FunToFunExitNodeMapTy FunToFunExitNodeMap; ///< map a function to its FunEntryICFGNode
PTACallGraph* callgraph;

/// Constructor
LLVMModuleSet();
Expand Down Expand Up @@ -364,11 +362,6 @@ class LLVMModuleSet

ObjTypeInference* getTypeInference();

inline ICFG* getICFG()
{
return icfg;
}

private:
/// Create SVFTypes
SVFType* addSVFTypeInfo(const Type* t);
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/include/SVF-LLVM/SVFIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class SVFIRBuilder: public llvm::InstVisitor<SVFIRBuilder>
//}@

/// connect PAG edges based on callgraph
void updateCallGraph(PTACallGraph* callgraph);
void updateCallGraph(CallGraph* callgraph);

protected:
/// Handle globals including (global variable and functions)
Expand Down
24 changes: 13 additions & 11 deletions svf-llvm/lib/ICFGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ using namespace SVFUtil;
/*!
* Create ICFG nodes and edges
*/
ICFG* ICFGBuilder::build()
void ICFGBuilder::build()
{
DBOUT(DGENERAL, outs() << pasMsg("\t Building ICFG ...\n"));
// Add the unique global ICFGNode at the entry of a program (before the main method).
Expand Down Expand Up @@ -78,7 +78,6 @@ ICFG* ICFGBuilder::build()

}
connectGlobalToProgEntry();
return icfg;
}

void ICFGBuilder::checkICFGNodesVisited(const Function* fun)
Expand Down Expand Up @@ -157,7 +156,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
}
InstVec nextInsts;
LLVMUtil::getNextInsts(inst, nextInsts);
s64_t branchID = 0;
u32_t branchID = 0;
for (InstVec::const_iterator nit = nextInsts.begin(), enit =
nextInsts.end(); nit != enit; ++nit)
{
Expand Down Expand Up @@ -185,7 +184,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
{
assert(branchID <= 1 && "if/else has more than two branches?");
if(br->isConditional())
icfg->addConditionalIntraEdge(srcNode, dstNode, 1 - branchID);
icfg->addConditionalIntraEdge(srcNode, dstNode, llvmModuleSet()->getSVFValue(br->getCondition()), 1 - branchID);
else
icfg->addIntraEdge(srcNode, dstNode);
}
Expand All @@ -197,7 +196,7 @@ void ICFGBuilder::processFunBody(WorkList& worklist)
s64_t val = -1;
if (condVal && condVal->getBitWidth() <= 64)
val = condVal->getSExtValue();
icfg->addConditionalIntraEdge(srcNode, dstNode,val);
icfg->addConditionalIntraEdge(srcNode, dstNode, llvmModuleSet()->getSVFValue(si->getCondition()),val);
}
else
icfg->addIntraEdge(srcNode, dstNode);
Expand Down Expand Up @@ -236,6 +235,8 @@ void ICFGBuilder::processFunExit(const Function* f)
*/
InterICFGNode* ICFGBuilder::addInterBlockICFGNode(const Instruction* inst)
{
SVFInstruction* svfInst =
llvmModuleSet()->getSVFInstruction(inst);
assert(LLVMUtil::isCallSite(inst) && "not a call instruction?");
assert(LLVMUtil::isNonInstricCallSite(inst) && "associating an intrinsic debug instruction with an ICFGNode!");
assert(llvmModuleSet()->getCallBlock(inst)==nullptr && "duplicate CallICFGNode");
Expand All @@ -253,10 +254,8 @@ InterICFGNode* ICFGBuilder::addInterBlockICFGNode(const Instruction* inst)
llvmModuleSet()->getSVFValue(called_llvmval));
}

SVFBasicBlock* bb = llvmModuleSet()->getSVFBasicBlock(inst->getParent());

CallICFGNode* callICFGNode = icfg->addCallICFGNode(
bb, llvmModuleSet()->getSVFType(inst->getType()),
svfInst->getParent(), llvmModuleSet()->getSVFType(inst->getType()),
calledFunc, cb->getFunctionType()->isVarArg(), isvcall,
isvcall ? cppUtil::getVCallIdx(cb) : 0,
isvcall ? cppUtil::getFunNameOfVCallSite(cb) : "");
Expand Down Expand Up @@ -331,22 +330,25 @@ void ICFGBuilder::connectGlobalToProgEntry()
inline ICFGNode* ICFGBuilder::addBlockICFGNode(const Instruction* inst)
{
ICFGNode* node;
SVFInstruction* svfINst =
llvmModuleSet()->getSVFInstruction(inst);
if(LLVMUtil::isNonInstricCallSite(inst))
node = addInterBlockICFGNode(inst);
else
node = addIntraBlockICFGNode(inst);
const_cast<SVFBasicBlock*>(
llvmModuleSet()->getSVFBasicBlock(inst->getParent()))
const_cast<SVFBasicBlock*>(svfINst->getParent())
->addICFGNode(node);
return node;
}

IntraICFGNode* ICFGBuilder::addIntraBlockICFGNode(const Instruction* inst)
{
SVFInstruction* svfInst =
llvmModuleSet()->getSVFInstruction(inst);
IntraICFGNode* node = llvmModuleSet()->getIntraBlock(inst);
assert (node==nullptr && "no IntraICFGNode for this instruction?");
IntraICFGNode* sNode = icfg->addIntraICFGNode(
llvmModuleSet()->getSVFBasicBlock(inst->getParent()), SVFUtil::isa<ReturnInst>(inst));
svfInst->getParent(), SVFUtil::isa<ReturnInst>(inst));
instToBlockNodeMap()[inst] = sNode;
llvmModuleSet()->setValueAttr(inst, sNode);
return sNode;
Expand Down
8 changes: 0 additions & 8 deletions svf-llvm/lib/LLVMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
#include "llvm/Support/FileSystem.h"
#include "SVF-LLVM/ObjTypeInference.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "SVF-LLVM/ICFGBuilder.h"
#include "Graphs/PTACallGraph.h"
#include "Util/CallGraphBuilder.h"

using namespace std;
using namespace SVF;
Expand Down Expand Up @@ -169,11 +166,6 @@ void LLVMModuleSet::build()

createSVFDataStructure();
initSVFFunction();
ICFGBuilder icfgbuilder;
icfg = icfgbuilder.build();

CallGraphBuilder callGraphBuilder;
callgraph = callGraphBuilder.buildSVFIRCallGraph(svfModule);
}

void LLVMModuleSet::createSVFDataStructure()
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/lib/LLVMUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ const std::string SVFBaseNode::valueOnlyToString() const
{
std::string str;
llvm::raw_string_ostream rawstr(str);
if (const SVF::PTACallGraphNode* fun = SVFUtil::dyn_cast<PTACallGraphNode>(this))
if (const SVF::CallGraphNode* fun = SVFUtil::dyn_cast<CallGraphNode>(this))
{
rawstr << "Function: " << fun->getFunction()->getName() << " ";
}
Expand Down
38 changes: 9 additions & 29 deletions svf-llvm/lib/SVFIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "SVFIR/SVFFileSystem.h"
#include "SVFIR/SVFModule.h"
#include "SVFIR/SVFValue.h"
#include "Util/CallGraphBuilder.h"
#include "Util/Options.h"
#include "Util/SVFUtil.h"

Expand All @@ -61,10 +60,10 @@ SVFIR* SVFIRBuilder::build()
pag->setModule(svfModule);

// Build ICFG
pag->setICFG(llvmModuleSet()->getICFG());

// Set callgraph
pag->setCallGraph(llvmModuleSet()->callgraph);
ICFG* icfg = new ICFG();
ICFGBuilder icfgbuilder(icfg);
icfgbuilder.build();
pag->setICFG(icfg);

// Set icfgnode in memobj
for (auto& it : SymbolTableInfo::SymbolInfo()->idToObjMap())
Expand Down Expand Up @@ -975,17 +974,6 @@ void SVFIRBuilder::visitBranchInst(BranchInst &inst)
branchID++;
}
addBranchStmt(brinst, cond, successors);
/// set conditional svf var
if (inst.isConditional())
{
for (auto& edge : llvmModuleSet()->getICFGNode(&inst)->getOutEdges())
{
if (IntraCFGEdge* intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
{
intraEdge->setConditionVar(pag->getGNode(cond));
}
}
}
}


Expand Down Expand Up @@ -1054,14 +1042,6 @@ void SVFIRBuilder::visitSwitchInst(SwitchInst &inst)
successors.push_back(std::make_pair(icfgNode, val));
}
addBranchStmt(brinst, cond, successors);
/// set conditional svf var
for (auto& edge : llvmModuleSet()->getICFGNode(&inst)->getOutEdges())
{
if (IntraCFGEdge* intraEdge = SVFUtil::dyn_cast<IntraCFGEdge>(edge))
{
intraEdge->setConditionVar(pag->getGNode(cond));
}
}
}


Expand Down Expand Up @@ -1189,17 +1169,17 @@ void SVFIRBuilder::handleIndCall(CallBase* cs)
pag->addIndirectCallsites(cbn,pag->getValueNode(svfcalledval));
}

void SVFIRBuilder::updateCallGraph(PTACallGraph* callgraph)
void SVFIRBuilder::updateCallGraph(CallGraph* callgraph)
{
PTACallGraph::CallEdgeMap::const_iterator iter = callgraph->getIndCallMap().begin();
PTACallGraph::CallEdgeMap::const_iterator eiter = callgraph->getIndCallMap().end();
CallGraph::CallEdgeMap::const_iterator iter = callgraph->getIndCallMap().begin();
CallGraph::CallEdgeMap::const_iterator eiter = callgraph->getIndCallMap().end();
for (; iter != eiter; iter++)
{
const CallICFGNode* callBlock = iter->first;
const CallBase* callbase = SVFUtil::cast<CallBase>(llvmModuleSet()->getLLVMValue(callBlock));
assert(callBlock->isIndirectCall() && "this is not an indirect call?");
const PTACallGraph::FunctionSet& functions = iter->second;
for (PTACallGraph::FunctionSet::const_iterator func_iter = functions.begin(); func_iter != functions.end(); func_iter++)
const CallGraph::FunctionSet& functions = iter->second;
for (CallGraph::FunctionSet::const_iterator func_iter = functions.begin(); func_iter != functions.end(); func_iter++)
{
const Function* callee = SVFUtil::cast<Function>(llvmModuleSet()->getLLVMValue(*func_iter));

Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/tools/AE/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(ae ae.cpp)
target_link_libraries(ae PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(ae PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
2 changes: 1 addition & 1 deletion svf-llvm/tools/AE/ae.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ int main(int argc, char** argv)
SVFIRBuilder builder(svfModule);
SVFIR* pag = builder.build();
AndersenWaveDiff* ander = AndersenWaveDiff::createAndersenWaveDiff(pag);
PTACallGraph* callgraph = ander->getCallGraph();
CallGraph* callgraph = ander->getCallGraph();
builder.updateCallGraph(callgraph);
pag->getICFG()->updateCallGraph(callgraph);
AbstractInterpretation& ae = AbstractInterpretation::getAEInstance();
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/tools/CFL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(cfl cfl.cpp)
target_link_libraries(cfl PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(cfl PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM )
2 changes: 1 addition & 1 deletion svf-llvm/tools/DDA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(dvf dda.cpp)
target_link_libraries(dvf PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(dvf PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
2 changes: 1 addition & 1 deletion svf-llvm/tools/Example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(svf-ex svf-ex.cpp)
target_link_libraries(svf-ex PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(svf-ex PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
2 changes: 1 addition & 1 deletion svf-llvm/tools/Example/svf-ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ int main(int argc, char ** argv)


/// Call Graph
PTACallGraph* callgraph = ander->getCallGraph();
CallGraph* callgraph = ander->getCallGraph();

/// ICFG
ICFG* icfg = pag->getICFG();
Expand Down
2 changes: 1 addition & 1 deletion svf-llvm/tools/LLVM2SVF/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(llvm2svf llvm2svf.cpp)
target_link_libraries(llvm2svf PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(llvm2svf PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
2 changes: 1 addition & 1 deletion svf-llvm/tools/MTA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(mta mta.cpp)
target_link_libraries(mta PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(mta PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
2 changes: 1 addition & 1 deletion svf-llvm/tools/SABER/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_llvm_executable(saber saber.cpp)
target_link_libraries(saber PUBLIC ${llvm_libs} SvfLLVM)
target_link_libraries(saber PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
2 changes: 1 addition & 1 deletion svf-llvm/tools/WPA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_llvm_executable(wpa wpa.cpp)
target_link_libraries(wpa PUBLIC ${llvm_libs} SvfLLVM Threads::Threads)
target_link_libraries(wpa PUBLIC ${llvm_libs} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM Threads::Threads)
2 changes: 2 additions & 0 deletions svf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Define the core library
add_library(SvfCore)

target_link_libraries(SvfCore PRIVATE ${NEO4J_CLIENT_LIBRARIES})

# Add the public headers as an include directory
target_include_directories(SvfCore
PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion svf/include/AE/Svfexe/AbstractInterpretation.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class AbstractInterpretation
friend class BufOverflowDetector;

public:
typedef SCCDetection<PTACallGraph*> CallGraphSCC;
typedef SCCDetection<CallGraph*> CallGraphSCC;
/// Constructor
AbstractInterpretation();

Expand Down
Loading

0 comments on commit 064d65b

Please sign in to comment.