Static Value-Flow Analysis
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
SVF::ExtAPI Class Reference

#include <ExtAPI.h>

Public Member Functions

std::string getExtBcPath ()
 
std::string getExtFuncAnnotation (const FunObjVar *fun, const std::string &funcAnnotation)
 
const std::vector< std::string > & getExtFuncAnnotations (const FunObjVar *fun)
 
bool hasExtFuncAnnotation (const FunObjVar *fun, const std::string &funcAnnotation)
 
bool has_static (const FunObjVar *F)
 
bool is_memcpy (const FunObjVar *F)
 
bool is_memset (const FunObjVar *F)
 
bool is_alloc (const FunObjVar *F)
 
bool is_arg_alloc (const FunObjVar *F)
 
bool is_alloc_stack_ret (const FunObjVar *F)
 
s32_t get_alloc_arg_pos (const FunObjVar *F)
 
bool is_realloc (const FunObjVar *F)
 
bool is_ext (const FunObjVar *funObjVar)
 

Static Public Member Functions

static ExtAPI * getExtAPI ()
 
static void destory ()
 
static bool setExtBcPath (const std::string &path)
 

Private Member Functions

 ExtAPI ()=default
 
void setExtFuncAnnotations (const FunObjVar *fun, const std::vector< std::string > &funcAnnotations)
 

Private Attributes

Map< const FunObjVar *, std::vector< std::string > > funObjVar2Annotations
 

Static Private Attributes

static ExtAPI * extOp = nullptr
 
static std::string extBcPath = ""
 

Friends

class LLVMModuleSet
 
class SVFIRBuilder
 
class GraphDBClient
 

Detailed Description

Definition at line 45 of file ExtAPI.h.

Constructor & Destructor Documentation

◆ ExtAPI()

SVF::ExtAPI::ExtAPI ( )
privatedefault

Member Function Documentation

◆ destory()

void ExtAPI::destory ( )
static

Definition at line 52 of file ExtAPI.cpp.

53{
54 if (extOp != nullptr)
55 {
56 delete extOp;
57 extOp = nullptr;
58 }
59}
static ExtAPI * extOp
Definition ExtAPI.h:53

◆ get_alloc_arg_pos()

s32_t ExtAPI::get_alloc_arg_pos ( const FunObjVar *  F)

Definition at line 270 of file ExtAPI.cpp.

271{
272 std::string allocArg = getExtFuncAnnotation(F, "ALLOC_HEAP_ARG");
273 assert(!allocArg.empty() && "Not an alloc call via argument or incorrect extern function annotation!");
274
275 std::string number;
276 for (char c : allocArg)
277 {
278 if (isdigit(c))
279 number.push_back(c);
280 }
281 assert(!number.empty() && "Incorrect naming convention for svf external functions(ALLOC_HEAP_ARG + number)?");
282 return std::stoi(number);
283}
const char *const const double number
Definition cJSON.h:268
std::string getExtFuncAnnotation(const FunObjVar *fun, const std::string &funcAnnotation)
Definition ExtAPI.cpp:219
int isdigit(int c)
Definition extapi.c:1016
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76

◆ getExtAPI()

ExtAPI * ExtAPI::getExtAPI ( )
static

Definition at line 43 of file ExtAPI.cpp.

44{
45 if (extOp == nullptr)
46 {
47 extOp = new ExtAPI;
48 }
49 return extOp;
50}
ExtAPI()=default

◆ getExtBcPath()

std::string ExtAPI::getExtBcPath ( )

Definition at line 111 of file ExtAPI.cpp.

112{
113 // Try to locate extapi.bc in the following order:
114 // 1. If setExtBcPath() has been called, use that path.
115 // 2. If the command line argument -extapi=path/to/extapi.bc is provided, use that path.
116 // 3. If SVF is being developed and used directly from a build directory, try the build dir (SVF_BUILD_DIR).
117 // 4. If the SVF_DIR environment variable is set, try $SVF_DIR with the standard relative path.
118 // 5. If installed via npm, use `npm root` and append the standard relative path.
119 // 6. As a last resort, use the directory of the loaded libSVFCore.so (or .dylib) and append extapi.bc.
120
121 std::vector<std::string> candidatePaths;
122
123 // 1. Use path set by setExtBcPath()
124 if (!extBcPath.empty())
125 return extBcPath;
126
127 // 2. Use command line argument -extapi=...
129 {
130 return extBcPath;
131 }
132 else
133 {
135 }
136
137 // 3. SVF developer: try build directory (SVF_BUILD_DIR)
138 if (setExtBcPath(SVF_BUILD_DIR "/lib/extapi.bc"))
139 {
140 return extBcPath;
141 }
142 else
143 {
144 candidatePaths.push_back(SVF_BUILD_DIR "/lib/extapi.bc");
145 }
146
147 // 4. Use $SVF_DIR environment variable + standard relative path
148 if (setExtBcPath(getFilePath("SVF_DIR")))
149 {
150 return extBcPath;
151 }
152 else
153 {
154 candidatePaths.push_back(getFilePath("SVF_DIR"));
155 }
156
157 // 5. Use npm root + standard relative path (for npm installations)
158 std::string npmPath = getFilePath("npm root");
160 {
161 return extBcPath;
162 }
163 else
164 {
165 candidatePaths.push_back(npmPath.empty() ? "npm root unavailable" : npmPath);
166 }
167
168 // 6. Use the directory of the loaded libSVFCore.so/.dylib + extapi.bc
169 std::string soPath = SVFUtil::getCurrentSOPath();
170 if (!soPath.empty())
171 {
172 std::string dir = soPath.substr(0, soPath.find_last_of('/'));
173 std::string candidate = dir + "/extapi.bc";
175 {
176 return extBcPath;
177 }
178 else
179 {
180 candidatePaths.push_back(candidate);
181 }
182 }
183
184 // If all candidate paths failed, print error and suggestions
185 SVFUtil::errs() << "ERROR: Failed to locate \"extapi.bc\". Tried the following candidate paths:" << std::endl;
186 for (const auto& path : candidatePaths)
187 {
188 SVFUtil::errs() << " " << path << std::endl;
189 }
190 SVFUtil::errs() << "To override the default locations for \"extapi.bc\", you can:" << std::endl
191 << "\t1. Use the command line argument \"-extapi=path/to/extapi.bc\"" << std::endl
192 << "\t2. Use the \"setExtBcPath()\" function *BEFORE* calling \"buildSVFModule()\"" << std::endl
193 << "\t3. Override the paths in \"include/SVF/Util/config.h\" (WARNING: will be overwritten "
194 << "when rebuilding SVF (generated by CMakeLists.txt))" << std::endl;
195 abort();
196}
static std::string getFilePath(const std::string &path)
Definition ExtAPI.cpp:73
static std::string extBcPath
Definition ExtAPI.h:59
static bool setExtBcPath(const std::string &path)
Definition ExtAPI.cpp:62
static const Option< std::string > ExtAPIPath
Definition Options.h:210
std::ostream & errs()
Overwrite llvm::errs()
Definition SVFUtil.h:64
std::string getCurrentSOPath()
Return the absolute path of the current module/executable.
Definition SVFUtil.cpp:302

◆ getExtFuncAnnotation()

std::string ExtAPI::getExtFuncAnnotation ( const FunObjVar *  fun,
const std::string &  funcAnnotation 
)

Definition at line 219 of file ExtAPI.cpp.

220{
221 assert(fun && "Null FunObjVar* pointer");
222 auto it = funObjVar2Annotations.find(fun);
223 if (it != funObjVar2Annotations.end())
224 {
225 for (const std::string& annotation : it->second)
226 if (annotation.find(funcAnnotation) != std::string::npos)
228 }
229 return "";
230}
if(prebuffer< 0)
Definition cJSON.cpp:1269
return(char *) p.buffer
const char *const string
Definition cJSON.h:172
Map< const FunObjVar *, std::vector< std::string > > funObjVar2Annotations
Definition ExtAPI.h:56

◆ getExtFuncAnnotations()

const std::vector< std::string > & ExtAPI::getExtFuncAnnotations ( const FunObjVar *  fun)

Definition at line 232 of file ExtAPI.cpp.

233{
234 assert(fun && "Null FunObjVar* pointer");
235 auto it = funObjVar2Annotations.find(fun);
236 if (it != funObjVar2Annotations.end())
237 return it->second;
238 return funObjVar2Annotations[fun];
239}

◆ has_static()

bool SVF::ExtAPI::has_static ( const FunObjVar *  F)

◆ hasExtFuncAnnotation()

bool ExtAPI::hasExtFuncAnnotation ( const FunObjVar *  fun,
const std::string &  funcAnnotation 
)

Definition at line 205 of file ExtAPI.cpp.

206{
207 assert(fun && "Null FunObjVar* pointer");
208 auto it = funObjVar2Annotations.find(fun);
209 if (it != funObjVar2Annotations.end())
210 {
211 for (const std::string& annotation : it->second)
212 if (annotation.find(funcAnnotation) != std::string::npos)
213 return true;
214 }
215 return false;
216}
#define true
Definition cJSON.cpp:65

◆ is_alloc()

bool ExtAPI::is_alloc ( const FunObjVar *  F)

Definition at line 253 of file ExtAPI.cpp.

254{
255 return F && hasExtFuncAnnotation(F, "ALLOC_HEAP_RET");
256}
bool hasExtFuncAnnotation(const FunObjVar *fun, const std::string &funcAnnotation)
Definition ExtAPI.cpp:205

◆ is_alloc_stack_ret()

bool ExtAPI::is_alloc_stack_ret ( const FunObjVar *  F)

Definition at line 264 of file ExtAPI.cpp.

265{
266 return F && hasExtFuncAnnotation(F, "ALLOC_STACK_RET");
267}

◆ is_arg_alloc()

bool ExtAPI::is_arg_alloc ( const FunObjVar *  F)

Definition at line 259 of file ExtAPI.cpp.

260{
261 return F && hasExtFuncAnnotation(F, "ALLOC_HEAP_ARG");
262}

◆ is_ext()

bool ExtAPI::is_ext ( const FunObjVar *  funObjVar)

Definition at line 290 of file ExtAPI.cpp.

291{
292 assert(F && "Null FunObjVar* pointer");
293 if (F->isDeclaration() || F->isIntrinsic())
294 return true;
295 else if (hasExtFuncAnnotation(F, "OVERWRITE") && getExtFuncAnnotations(F).size() == 1)
296 return false;
297 else
298 return !getExtFuncAnnotations(F).empty();
299}
const std::vector< std::string > & getExtFuncAnnotations(const FunObjVar *fun)
Definition ExtAPI.cpp:232

◆ is_memcpy()

bool ExtAPI::is_memcpy ( const FunObjVar *  F)

Definition at line 241 of file ExtAPI.cpp.

242{
243 return F &&
244 (hasExtFuncAnnotation(F, "MEMCPY") || hasExtFuncAnnotation(F, "STRCPY")
245 || hasExtFuncAnnotation(F, "STRCAT"));
246}

◆ is_memset()

bool ExtAPI::is_memset ( const FunObjVar *  F)

Definition at line 248 of file ExtAPI.cpp.

249{
250 return F && hasExtFuncAnnotation(F, "MEMSET");
251}

◆ is_realloc()

bool ExtAPI::is_realloc ( const FunObjVar *  F)

Definition at line 286 of file ExtAPI.cpp.

287{
288 return F && hasExtFuncAnnotation(F, "REALLOC_HEAP_RET");
289}

◆ setExtBcPath()

bool ExtAPI::setExtBcPath ( const std::string &  path)
static

Definition at line 62 of file ExtAPI.cpp.

63{
64 if (!path.empty() && SVFUtil::fileExists(path))
65 {
67 return true;
68 }
69 return false;
70}
bool fileExists(const std::string &path)
Check whether a file exists at the given path.
Definition SVFUtil.cpp:271

◆ setExtFuncAnnotations()

void ExtAPI::setExtFuncAnnotations ( const FunObjVar *  fun,
const std::vector< std::string > &  funcAnnotations 
)
private

Definition at line 199 of file ExtAPI.cpp.

200{
201 assert(fun && "Null FunObjVar* pointer");
203}

Friends And Related Symbol Documentation

◆ GraphDBClient

friend class GraphDBClient
friend

Definition at line 49 of file ExtAPI.h.

◆ LLVMModuleSet

Definition at line 47 of file ExtAPI.h.

◆ SVFIRBuilder

Definition at line 48 of file ExtAPI.h.

Member Data Documentation

◆ extBcPath

std::string ExtAPI::extBcPath = ""
staticprivate

Definition at line 59 of file ExtAPI.h.

◆ extOp

ExtAPI * ExtAPI::extOp = nullptr
staticprivate

Definition at line 53 of file ExtAPI.h.

◆ funObjVar2Annotations

Map<const FunObjVar*, std::vector<std::string> > SVF::ExtAPI::funObjVar2Annotations
private

Definition at line 56 of file ExtAPI.h.


The documentation for this class was generated from the following files: