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

Add SVFG::fromValue #906

Open
wants to merge 5 commits into
base: master
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
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,15 @@ endif()
add_subdirectory(lib)
add_subdirectory(tools)

INSTALL(
install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
COMPONENT devel
DESTINATION include/svf
FILES_MATCHING
PATTERN "**/*.h"
)
)

install(
FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/Util/ExtAPI.json
DESTINATION share/svf
)
5 changes: 5 additions & 0 deletions include/Graphs/SVFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ class SVFG : public VFG
return getSVFGNode(getDef(pagNode));
}

/// Return the corresponding SVFGNodes to a given llvm::Value.
/// return an empty list, if the no mapping is possible
std::set<const SVFGNode*> fromValue(const llvm::Value* value) const;


/// Perform statistics
void performStat();

Expand Down
5 changes: 4 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ file (GLOB SOURCES
FastCluster/*.cpp
)

add_llvm_library(Svf STATIC ${SOURCES} LINK_LIBS ${Z3_LIBRARIES})

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

add_llvm_library(Svf STATIC ${SOURCES} LINK_LIBS ${Z3_LIBRARIES} Threads::Threads)


if(DEFINED IN_SOURCE_BUILD)
Expand Down
22 changes: 22 additions & 0 deletions lib/Graphs/SVFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,28 @@ void SVFG::dump(const std::string& file, bool simple)
GraphPrinter::WriteGraphToFile(outs(), file, this, simple);
}

std::set<const SVFGNode*> SVFG::fromValue(const llvm::Value* value) const
{
SVFIR* svfir = SVFIR::getPAG();
std::set<const SVFGNode*> ret;
// search for all SVFStmts first
for (const SVFStmt* svfStmt : svfir->getValueEdges(value))
{
PAGEdgeToStmtVFGNodeMapTy::const_iterator it = PAGEdgeToStmtVFGNodeMap.find(svfStmt);
if (it != PAGEdgeToStmtVFGNodeMap.end())
{
ret.emplace(it->second);
}
}
// add all SVFVars
SVFVar* svfVar = svfir->getGNode(svfir->getValueNode(value));
if(hasDef(svfVar))
{
ret.emplace(getDefSVFGNode(svfVar));
}
return ret;
}

/**
* Get all inter value flow edges at this indirect call site, including call and return edges.
*/
Expand Down
57 changes: 52 additions & 5 deletions lib/Util/ExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ using namespace SVF;
ExtAPI *ExtAPI::extOp = nullptr;
cJSON *ExtAPI::root = nullptr;

// Get environment variables $SVF_DIR and "npm root" through popen() method
// print error message and fail
[[ noreturn ]] static void failToLoadJson(const std::string &msg) {
SVFUtil::errs() << SVFUtil::errMsg("failed to load ExtAPI.json! ") << msg << '\n';
exit(1);
}
[[ noreturn ]] static void failToLoadJson(const std::string &msg, const std::string &path) {
SVFUtil::errs() << SVFUtil::errMsg("failed to load ExtAPI.json! ") << msg << " The current ExtAPI.json path is: " << path << '\n';
exit(1);
}

// Get environment variables $SVF_DIR, $SVF_EXT_API_JSON and "npm root" through popen() method
static std::string GetStdoutFromCommand(const std::string &command)
{
char buffer[128];
Expand All @@ -48,6 +58,7 @@ static std::string GetStdoutFromCommand(const std::string &command)
FILE *pipe = popen(command.c_str(), "r");
if (!pipe)
{
failToLoadJson("GetStdoutFromCommand(): popen failed!");
return "popen failed!";
}
// read till end of process:
Expand Down Expand Up @@ -89,9 +100,12 @@ static std::string getJsonFile(const std::string &path)

static cJSON *parseJson(const std::string &path, off_t fileSize)
{
SVFUtil::outs() << "load ExtAPI.json at " << path << '\n';

FILE *file = fopen(path.c_str(), "r");
if (!file)
{
failToLoadJson("parseJson(): fopen failed!", path);
return nullptr;
}

Expand All @@ -102,7 +116,7 @@ static cJSON *parseJson(const std::string &path, off_t fileSize)
u32_t size = fread(jsonStr, sizeof(char), fileSize, file);
if (size == 0)
{
SVFUtil::errs() << SVFUtil::errMsg("\t Wrong ExtAPI.json path!! ") << "The current ExtAPI.json path is: " << path << "\n";
failToLoadJson("Wrong ExtAPI.json path!!", path);
assert(false && "Read ExtAPI.json file fails!");
return nullptr;
}
Expand All @@ -112,6 +126,7 @@ static cJSON *parseJson(const std::string &path, off_t fileSize)
cJSON *root = cJSON_Parse(jsonStr);
if (!root)
{
failToLoadJson("parseJson(): cJSON_Parse() failed", path);
free(jsonStr);
return nullptr;
}
Expand All @@ -131,9 +146,12 @@ ExtAPI *ExtAPI::getExtAPI(const std::string &path)

// Four ways to get ExtAPI.json path
// 1. Explicit path provided
// 2. default path (get ExtAPI.json path from Util/config.h)
// 3. from $SVF_DIR
// 4. from "npm root"(If SVF is installed via npm)
// 2. from $SVF_EXT_API_JSON
// 3. default path (get ExtAPI.json path from Util/config.h)
// 4. from $SVF_DIR
// 5. from ${HOME}/.local/share/svf (If SVF is installed for local user)
// 6. from "/usr/share/svf" or "/usr/local/share/svf" (If SVF is installed into the system)
// 7. from "npm root" (If SVF is installed via npm)

std::string jsonFilePath = path;
if (!jsonFilePath.empty() && !stat(jsonFilePath.c_str(), &statbuf))
Expand All @@ -142,6 +160,13 @@ ExtAPI *ExtAPI::getExtAPI(const std::string &path)
return extOp;
}

jsonFilePath = GetStdoutFromCommand("echo $SVF_EXT_API_JSON");
if (!stat(jsonFilePath.c_str(), &statbuf))
{
root = parseJson(jsonFilePath, statbuf.st_size);
return extOp;
}

jsonFilePath = PROJECT_PATH + std::string(EXTAPI_JSON_PATH);
if (!stat(jsonFilePath.c_str(), &statbuf))
{
Expand All @@ -156,13 +181,35 @@ ExtAPI *ExtAPI::getExtAPI(const std::string &path)
return extOp;
}

jsonFilePath = GetStdoutFromCommand("echo ${HOME}/.local/share/svf/ExtAPI.json");
if (!stat(jsonFilePath.c_str(), &statbuf))
{
root = parseJson(jsonFilePath, statbuf.st_size);
return extOp;
}

jsonFilePath = "/usr/share/svf/ExtAPI.json";
if (!stat(jsonFilePath.c_str(), &statbuf))
{
root = parseJson(jsonFilePath, statbuf.st_size);
return extOp;
}

jsonFilePath = "/usr/local/share/svf/ExtAPI.json";
if (!stat(jsonFilePath.c_str(), &statbuf))
{
root = parseJson(jsonFilePath, statbuf.st_size);
return extOp;
}

jsonFilePath = getJsonFile("npm root");
if (!stat(jsonFilePath.c_str(), &statbuf))
{
root = parseJson(jsonFilePath, statbuf.st_size);
return extOp;
}

failToLoadJson("Could not find ExtAPI.json");
assert(false && "Open ExtAPI.json file fails!");
}
return extOp;
Expand Down