Skip to content

Commit

Permalink
graph db
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonZhongZexin committed Nov 4, 2024
1 parent 064d65b commit efa0055
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 410 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ 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)
find_library(LGRAPH_CPP_CLIENT_LIBRARIES lgraph_client_cpp_rpc 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)
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} ${NEO4J_CLIENT_LIBRARIES} SvfCore)
target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} ${LGRAPH_CPP_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
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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
target_link_libraries(ae PUBLIC ${llvm_libs} ${LGRAPH_CPP_CLIENT_LIBRARIES} SvfLLVM)
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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM )
target_link_libraries(cfl PUBLIC ${llvm_libs} ${LGRAPH_CPP_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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
target_link_libraries(dvf PUBLIC ${llvm_libs} ${LGRAPH_CPP_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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
target_link_libraries(svf-ex PUBLIC ${llvm_libs} ${LGRAPH_CPP_CLIENT_LIBRARIES} SvfLLVM)
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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
target_link_libraries(llvm2svf PUBLIC ${llvm_libs} ${LGRAPH_CPP_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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
target_link_libraries(mta PUBLIC ${llvm_libs} ${LGRAPH_CPP_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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM)
target_link_libraries(saber PUBLIC ${llvm_libs} ${LGRAPH_CPP_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} ${NEO4J_CLIENT_LIBRARIES} SvfLLVM Threads::Threads)
target_link_libraries(wpa PUBLIC ${llvm_libs} ${LGRAPH_CPP_CLIENT_LIBRARIES} SvfLLVM Threads::Threads)
2 changes: 1 addition & 1 deletion svf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Define the core library
add_library(SvfCore)

target_link_libraries(SvfCore PRIVATE ${NEO4J_CLIENT_LIBRARIES})
target_link_libraries(SvfCore PRIVATE ${LGRAPH_CPP_CLIENT_LIBRARIES})

# Add the public headers as an include directory
target_include_directories(SvfCore
Expand Down
112 changes: 55 additions & 57 deletions svf/include/SVFIR/GraphDBClient.h
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
#ifndef INCLUDE_GRAPHDBCLIENT_H_
#define INCLUDE_GRAPHDBCLIENT_H_
#include <neo4j-client.h>
#include "Util/SVFUtil.h"

#include "lgraph/lgraph_rpc_client.h"
#include <errno.h>
#include <stdio.h>
#include "Graphs/CallGraph.h"

namespace SVF
{
class GraphDBClient{
private:
neo4j_connection_t* connection;

GraphDBClient()
{
const char* uri = "bolt://localhost:7687";
neo4j_config_t* config = neo4j_new_config();
if (neo4j_config_set_username(config, "admin") != 0 &&
neo4j_config_set_password(config, "73@TuGraph") != 0){
SVFUtil::outs() << "Failed to connect to GraphDB.\n";
} else {
connection = neo4j_connect(uri, config, NEO4J_INSECURE);
if (connection == nullptr) {
SVFUtil::outs() << "Failed to connect to GraphDB.\n";
} else {
SVFUtil::outs() << "Connected to GraphDB successfully.\n";
}
}

}

~GraphDBClient()
{
if (connection != nullptr)
{
if (neo4j_close(connection) !=0){
SVFUtil::outs() << "Failed to close GraphDB.\n";
} else {
SVFUtil::outs() << "GraphDB connection closed.\n";
connection = nullptr;
}
}
}
public:
static GraphDBClient& getInstance()
{
static GraphDBClient instance;
return instance;
}

GraphDBClient(const GraphDBClient&) = delete;
GraphDBClient& operator = (const GraphDBClient&) = delete;

neo4j_connection_t* getConnection()
{
return connection;
}

void loadSchema(neo4j_connection_t* connection, const std::string& filepath, const std::string& queryParam);
};

}

#endif
class RpcClient;
class CallGraphEdge;
class CallGraphNode;
class GraphDBClient
{
private:
lgraph::RpcClient* connection;

GraphDBClient()
{
const char* url = "127.0.0.1:9090";
connection = new lgraph::RpcClient(url, "admin", "73@TuGraph");
}

~GraphDBClient()
{
if (connection != nullptr)
{
connection = nullptr;
}
}

public:
static GraphDBClient& getInstance()
{
static GraphDBClient instance;
return instance;
}

GraphDBClient(const GraphDBClient&) = delete;
GraphDBClient& operator=(const GraphDBClient&) = delete;

lgraph::RpcClient* getConnection()
{
return connection;
}

bool loadSchema(lgraph::RpcClient* connection, const std::string& filepath,
const std::string& dbname);
bool addCallGraphNode2db(lgraph::RpcClient* connection, const CallGraphNode* node,
const std::string& dbname);
bool addCallGraphEdge2db(lgraph::RpcClient* connection,
const CallGraphEdge* edge, const int& csid,
const std::string& dbname);
// pasre the directcallsIds/indirectcallsIds string to vector
std::vector<int> stringToIds(const std::string& str);
};

} // namespace SVF

#endif
31 changes: 24 additions & 7 deletions svf/lib/Graphs/CallGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using namespace SVFUtil;
CallGraph::CallSiteToIdMap CallGraph::csToIdMap;
CallGraph::IdToCallSiteMap CallGraph::idToCSMap;
CallSiteID CallGraph::totalCallSiteNum = 1;
lgraph::RpcClient* dbConnection = SVF::GraphDBClient::getInstance().getConnection();


/// Add direct and indirect callsite
Expand Down Expand Up @@ -111,15 +112,25 @@ CallGraph::CallGraph(CGEK k): kind(k)
{
callGraphNodeNum = 0;
numOfResolvedIndCallEdge = 0;
neo4j_connection_t* dbConnection = SVF::GraphDBClient::getInstance().getConnection();
if (dbConnection != nullptr)
{
if (dbConnection != nullptr) {
SVF::GraphDBClient::getInstance().loadSchema(dbConnection, "./DBSchema/CallGraphEdgeSchema.json", "CALL db.createEdgeLabelByJson($edge_schema)");
SVF::GraphDBClient::getInstance().loadSchema(dbConnection, "./DBSchema/CallGraphNodeSchema.json", "CALL db.createVertexLabelByJson($node_schema)");
} else {
SVFUtil::outs() << "Failed to connect to the database.\n";
}
std::string result;
bool ret = dbConnection->CallCypherToLeader(result,"CALL dbms.graph.createGraph('callGraph')");
SVFUtil::outs()<<"Create Graph result: "<<result<<"\n";
// create a new graph named callGraph in the GraphDB
if ( ret )
{
if (dbConnection != nullptr) {
// load edge schema to the callGraph db
SVF::GraphDBClient::getInstance().loadSchema(dbConnection, "/root/svf/SVF-1/svf/lib/Graphs/DBSchema/CallGraphEdgeSchema.json","callGraph");
//load vertex schema to the callGraph db
SVF::GraphDBClient::getInstance().loadSchema(dbConnection, "/root/svf/SVF-1/svf/lib/Graphs/DBSchema/CallGraphNodeSchema.json", "callGraph");
} else {
SVFUtil::outs()<<"Failed to create graphDB for callGraph.\n";
}
} else {
SVFUtil::outs() << "Failed to create sub-graph [callGraph]\n";
}
}
}

Expand All @@ -140,6 +151,10 @@ void CallGraph::addCallGraphNode(const SVFFunction* fun)
addGNode(id,callGraphNode);
funToCallGraphNodeMap[fun] = callGraphNode;
callGraphNodeNum++;
if ( nullptr != dbConnection)
{
SVF::GraphDBClient::getInstance().addCallGraphNode2db(dbConnection, callGraphNode, "callGraph");
}
}

/*!
Expand Down Expand Up @@ -190,6 +205,7 @@ void CallGraph::addDirectCallGraphEdge(const CallICFGNode* cs,const SVFFunction*
CallGraphEdge* edge = new CallGraphEdge(caller,callee,CallGraphEdge::CallRetEdge,csId);
edge->addDirectCallSite(cs);
addEdge(edge);
SVF::GraphDBClient::getInstance().addCallGraphEdge2db(dbConnection, edge, csId, "callGraph");
callinstToCallGraphEdgesMap[cs].insert(edge);
}
}
Expand All @@ -212,6 +228,7 @@ void CallGraph::addIndirectCallGraphEdge(const CallICFGNode* cs,const SVFFunctio
CallGraphEdge* edge = new CallGraphEdge(caller,callee,CallGraphEdge::CallRetEdge, csId);
edge->addInDirectCallSite(cs);
addEdge(edge);
SVF::GraphDBClient::getInstance().addCallGraphEdge2db(dbConnection, edge, csId, "callGraph");
callinstToCallGraphEdgesMap[cs].insert(edge);
}
}
Expand Down
16 changes: 14 additions & 2 deletions svf/lib/Graphs/DBSchema/CallGraphEdgeSchema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schema": [
{
"label":"CallRetEdge",
"label":"CallGraphEdge",
"type":"EDGE",
"properties" : [
{
Expand All @@ -12,7 +12,7 @@
},
{
"name":"kind",
"type":"STRING",
"type":"INT32",
"optional":false,
"index":false
},
Expand All @@ -21,6 +21,18 @@
"type":"BOOL",
"optional":false,
"index":false
},
{
"name":"directCalls",
"type":"STRING",
"optional":false,
"index":false
},
{
"name":"indirectCalls",
"type":"STRING",
"optional":false,
"index":false
}
]
}
Expand Down
5 changes: 2 additions & 3 deletions svf/lib/Graphs/DBSchema/CallGraphNodeSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
"index":false
},
{
"name":"isReachableFromProgEntry",
"name":"is_reachable_from_prog_entry",
"type":"BOOL",
"optional":false,
"index":false
}
]
}
]
}

}
Loading

0 comments on commit efa0055

Please sign in to comment.