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 ExtAPIgetExtAPI ()
 
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 ExtAPIextOp = 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 53 of file ExtAPI.cpp.

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

◆ get_alloc_arg_pos()

s32_t ExtAPI::get_alloc_arg_pos ( const FunObjVar F)

Definition at line 313 of file ExtAPI.cpp.

314{
315 std::string allocArg = getExtFuncAnnotation(F, "ALLOC_HEAP_ARG");
316 assert(!allocArg.empty() && "Not an alloc call via argument or incorrect extern function annotation!");
317
318 std::string number;
319 for (char c : allocArg)
320 {
321 if (isdigit(c))
322 number.push_back(c);
323 }
324 assert(!number.empty() && "Incorrect naming convention for svf external functions(ALLOC_HEAP_ARG + number)?");
325 return std::stoi(number);
326}
const char *const const double number
Definition cJSON.h:268
std::string getExtFuncAnnotation(const FunObjVar *fun, const std::string &funcAnnotation)
Definition ExtAPI.cpp:262
int isdigit(int c)
Definition extapi.c:974
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76

◆ getExtAPI()

ExtAPI * ExtAPI::getExtAPI ( )
static

Definition at line 44 of file ExtAPI.cpp.

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

◆ getExtBcPath()

std::string ExtAPI::getExtBcPath ( )

Definition at line 154 of file ExtAPI.cpp.

155{
156 // Try to locate extapi.bc in the following order:
157 // 1. If setExtBcPath() has been called, use that path.
158 // 2. If the command line argument -extapi=path/to/extapi.bc is provided, use that path.
159 // 3. If SVF is being developed and used directly from a build directory, try the build dir (SVF_BUILD_DIR).
160 // 4. If the SVF_DIR environment variable is set, try $SVF_DIR with the standard relative path.
161 // 5. If installed via npm, use `npm root` and append the standard relative path.
162 // 6. As a last resort, use the directory of the loaded libSVFCore.so (or .dylib) and append extapi.bc.
163
164 std::vector<std::string> candidatePaths;
165
166 // 1. Use path set by setExtBcPath()
167 if (!extBcPath.empty())
168 return extBcPath;
169
170 // 2. Use command line argument -extapi=...
172 {
173 return extBcPath;
174 }
175 else
176 {
178 }
179
180 // 3. SVF developer: try build directory (SVF_BUILD_DIR)
181 if (setExtBcPath(SVF_BUILD_DIR "/lib/extapi.bc"))
182 {
183 return extBcPath;
184 }
185 else
186 {
187 candidatePaths.push_back(SVF_BUILD_DIR "/lib/extapi.bc");
188 }
189
190 // 4. Use $SVF_DIR environment variable + standard relative path
191 if (setExtBcPath(getFilePath("SVF_DIR")))
192 {
193 return extBcPath;
194 }
195 else
196 {
197 candidatePaths.push_back(getFilePath("SVF_DIR"));
198 }
199
200 // 5. Use npm root + standard relative path (for npm installations)
201 std::string npmPath = getFilePath("npm root");
203 {
204 return extBcPath;
205 }
206 else
207 {
208 candidatePaths.push_back(npmPath.empty() ? "npm root unavailable" : npmPath);
209 }
210
211 // 6. Use the directory of the loaded libSVFCore.so/.dylib + extapi.bc
212 std::string soPath = getCurrentSOPath();
213 if (!soPath.empty())
214 {
215 std::string dir = soPath.substr(0, soPath.find_last_of('/'));
216 std::string candidate = dir + "/extapi.bc";
218 {
219 return extBcPath;
220 }
221 else
222 {
223 candidatePaths.push_back(candidate);
224 }
225 }
226
227 // If all candidate paths failed, print error and suggestions
228 SVFUtil::errs() << "ERROR: Failed to locate \"extapi.bc\". Tried the following candidate paths:" << std::endl;
229 for (const auto& path : candidatePaths)
230 {
231 SVFUtil::errs() << " " << path << std::endl;
232 }
233 SVFUtil::errs() << "To override the default locations for \"extapi.bc\", you can:" << std::endl
234 << "\t1. Use the command line argument \"-extapi=path/to/extapi.bc\"" << std::endl
235 << "\t2. Use the \"setExtBcPath()\" function *BEFORE* calling \"buildSVFModule()\"" << std::endl
236 << "\t3. Override the paths in \"include/SVF/Util/config.h\" (WARNING: will be overwritten "
237 << "when rebuilding SVF (generated by CMakeLists.txt))" << std::endl;
238 abort();
239}
static std::string getFilePath(const std::string &path)
Definition ExtAPI.cpp:102
std::string getCurrentSOPath()
Definition ExtAPI.cpp:143
static std::string extBcPath
Definition ExtAPI.h:59
static bool setExtBcPath(const std::string &path)
Definition ExtAPI.cpp:63
static const Option< std::string > ExtAPIPath
Definition Options.h:213
std::ostream & errs()
Overwrite llvm::errs()
Definition SVFUtil.h:58

◆ getExtFuncAnnotation()

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

Definition at line 262 of file ExtAPI.cpp.

263{
264 assert(fun && "Null FunObjVar* pointer");
265 auto it = funObjVar2Annotations.find(fun);
266 if (it != funObjVar2Annotations.end())
267 {
268 for (const std::string& annotation : it->second)
269 if (annotation.find(funcAnnotation) != std::string::npos)
271 }
272 return "";
273}
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 275 of file ExtAPI.cpp.

276{
277 assert(fun && "Null FunObjVar* pointer");
278 auto it = funObjVar2Annotations.find(fun);
279 if (it != funObjVar2Annotations.end())
280 return it->second;
281 return funObjVar2Annotations[fun];
282}

◆ has_static()

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

◆ hasExtFuncAnnotation()

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

Definition at line 248 of file ExtAPI.cpp.

249{
250 assert(fun && "Null FunObjVar* pointer");
251 auto it = funObjVar2Annotations.find(fun);
252 if (it != funObjVar2Annotations.end())
253 {
254 for (const std::string& annotation : it->second)
255 if (annotation.find(funcAnnotation) != std::string::npos)
256 return true;
257 }
258 return false;
259}
#define true
Definition cJSON.cpp:65

◆ is_alloc()

bool ExtAPI::is_alloc ( const FunObjVar F)

Definition at line 296 of file ExtAPI.cpp.

297{
298 return F && hasExtFuncAnnotation(F, "ALLOC_HEAP_RET");
299}
bool hasExtFuncAnnotation(const FunObjVar *fun, const std::string &funcAnnotation)
Definition ExtAPI.cpp:248

◆ is_alloc_stack_ret()

bool ExtAPI::is_alloc_stack_ret ( const FunObjVar F)

Definition at line 307 of file ExtAPI.cpp.

308{
309 return F && hasExtFuncAnnotation(F, "ALLOC_STACK_RET");
310}

◆ is_arg_alloc()

bool ExtAPI::is_arg_alloc ( const FunObjVar F)

Definition at line 302 of file ExtAPI.cpp.

303{
304 return F && hasExtFuncAnnotation(F, "ALLOC_HEAP_ARG");
305}

◆ is_ext()

bool ExtAPI::is_ext ( const FunObjVar funObjVar)

Definition at line 333 of file ExtAPI.cpp.

334{
335 assert(F && "Null FunObjVar* pointer");
336 if (F->isDeclaration() || F->isIntrinsic())
337 return true;
338 else if (hasExtFuncAnnotation(F, "OVERWRITE") && getExtFuncAnnotations(F).size() == 1)
339 return false;
340 else
341 return !getExtFuncAnnotations(F).empty();
342}
const std::vector< std::string > & getExtFuncAnnotations(const FunObjVar *fun)
Definition ExtAPI.cpp:275

◆ is_memcpy()

bool ExtAPI::is_memcpy ( const FunObjVar F)

Definition at line 284 of file ExtAPI.cpp.

285{
286 return F &&
287 (hasExtFuncAnnotation(F, "MEMCPY") || hasExtFuncAnnotation(F, "STRCPY")
288 || hasExtFuncAnnotation(F, "STRCAT"));
289}

◆ is_memset()

bool ExtAPI::is_memset ( const FunObjVar F)

Definition at line 291 of file ExtAPI.cpp.

292{
293 return F && hasExtFuncAnnotation(F, "MEMSET");
294}

◆ is_realloc()

bool ExtAPI::is_realloc ( const FunObjVar F)

Definition at line 329 of file ExtAPI.cpp.

330{
331 return F && hasExtFuncAnnotation(F, "REALLOC_HEAP_RET");
332}

◆ setExtBcPath()

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

Definition at line 63 of file ExtAPI.cpp.

64{
65 struct stat statbuf;
66 if (!path.empty() && !stat(path.c_str(), &statbuf))
67 {
69 return true;
70 }
71 return false;
72}

◆ setExtFuncAnnotations()

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

Definition at line 242 of file ExtAPI.cpp.

243{
244 assert(fun && "Null FunObjVar* pointer");
246}

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: