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

Fix the bug of failing to find extapi.bc in npm #1183

Merged
merged 2 commits into from
Aug 31, 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
1 change: 1 addition & 0 deletions .github/workflows/svf-lib_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
cp -rf $GITHUB_WORKSPACE/svf/include SVF-${osVersion}/svf
cp -rf $GITHUB_WORKSPACE/svf-llvm/include SVF-${osVersion}/svf-llvm
cp -f $GITHUB_WORKSPACE/Release-build/svf-llvm/libSvfLLVM.a SVF-${osVersion}/Release-build/svf-llvm/libSvfLLVM.a
cp -f $GITHUB_WORKSPACE/Release-build/svf-llvm/extapi.bc SVF-${osVersion}/Release-build/svf-llvm/extapi.bc
cp -f $GITHUB_WORKSPACE/Release-build/svf/libSvfCore.a SVF-${osVersion}/Release-build/svf/libSvfCore.a
cp -rf $GITHUB_WORKSPACE/Release-build/include SVF-${osVersion}/Release-build
git add .
Expand Down
5 changes: 5 additions & 0 deletions svf/include/Util/ExtAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

/// For a more detailed explanation of how External APIs are handled in SVF, please refer to the SVF Wiki: https://github.com/SVF-tools/SVF/wiki/Handling-External-APIs-with-extapi.c

#define EXTAPI_BC_PATH "Release-build/svf-llvm/extapi.bc"

namespace SVF
{

Expand All @@ -55,6 +57,9 @@ class ExtAPI

static void destory();

// Get extapi.bc file path
std::string getExtBcPath();

// Get the annotation of (F)
std::string getExtFuncAnnotation(const SVFFunction* fun, const std::string& funcAnnotation);

Expand Down
73 changes: 73 additions & 0 deletions svf/lib/Util/ExtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/

#include "Util/ExtAPI.h"
#include "Util/SVFUtil.h"
#include <sys/stat.h>

using namespace SVF;

Expand All @@ -51,6 +53,77 @@ void ExtAPI::destory()
}
}

// Get environment variables $SVF_DIR and "npm root" through popen() method
static std::string GetStdoutFromCommand(const std::string& command)
{
char buffer[128];
std::string result = "";
// Open pipe to file
FILE* pipe = popen(command.c_str(), "r");
if (!pipe)
{
return "popen failed!";
}
// read till end of process:
while (!feof(pipe))
{
// use buffer to read and add to result
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
// remove "\n"
result.erase(remove(result.begin(), result.end(), '\n'), result.end());
return result;
}

// Get extapi.bc file path in npm
static std::string getFilePath(const std::string& path)
{
std::string bcFilePath = GetStdoutFromCommand(path);
if (path.compare("npm root") == 0)
{
int os_flag = 1;
// SVF installed via npm needs to determine the type of operating
// system, otherwise the extapi.bc path may not be found.
#ifdef linux
// Linux os
os_flag = 0;
bcFilePath.append("/svf-lib/SVF-linux");
#endif
// Mac os
if (os_flag == 1)
{
bcFilePath.append("/svf-lib/SVF-osx");
}
}

if (bcFilePath.back() != '/')
bcFilePath.push_back('/');
bcFilePath.append(EXTAPI_BC_PATH);
return bcFilePath;
}

// Get extapi.bc path
std::string ExtAPI::getExtBcPath()
{
struct stat statbuf;
std::string bcFilePath = std::string(EXTAPI_PATH) + "/extapi.bc";
if (!stat(bcFilePath.c_str(), &statbuf))
return bcFilePath;

bcFilePath = getFilePath("echo $SVF_DIR");
if (!stat(bcFilePath.c_str(), &statbuf))
return bcFilePath;

bcFilePath = getFilePath("npm root");
if (!stat(bcFilePath.c_str(), &statbuf))
return bcFilePath;

SVFUtil::errs() << "No extpai.bc found at " << bcFilePath << " for getExtAPI(); set $SVF_DIR first!\n";
abort();
}

std::string ExtAPI::getExtFuncAnnotation(const SVFFunction* fun, const std::string& funcAnnotation)
{
assert(fun && "Null SVFFunction* pointer");
Expand Down
3 changes: 2 additions & 1 deletion svf/lib/Util/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Util/Options.h"
#include "Util/CommandLine.h"
#include "Util/ExtAPI.h"

namespace SVF
{
Expand Down Expand Up @@ -772,7 +773,7 @@ const Option<bool> Options::VtableInSVFIR(

//WPAPass.cpp
const Option<std::string> Options::ExtAPIInput(
"extapi", "External API extapi.bc", std::string(EXTAPI_PATH) + "/extapi.bc"
"extapi", "External API extapi.bc", ExtAPI::getExtAPI()->getExtBcPath()
);

const Option<bool> Options::AnderSVFG(
Expand Down