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
 

Detailed Description

Definition at line 46 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 294 of file ExtAPI.cpp.

295{
296 std::string allocArg = getExtFuncAnnotation(F, "ALLOC_HEAP_ARG");
297 assert(!allocArg.empty() && "Not an alloc call via argument or incorrect extern function annotation!");
298
299 std::string number;
300 for (char c : allocArg)
301 {
302 if (isdigit(c))
303 number.push_back(c);
304 }
305 assert(!number.empty() && "Incorrect naming convention for svf external functions(ALLOC_HEAP_ARG + number)?");
306 return std::stoi(number);
307}
const char *const const double number
Definition cJSON.h:268
std::string getExtFuncAnnotation(const FunObjVar *fun, const std::string &funcAnnotation)
Definition ExtAPI.cpp:243
int isdigit(int c)
Definition extapi.c:937
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

◆ 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 136 of file ExtAPI.cpp.

137{
138 // Try to locate extapi.bc in the following order:
139 // 1. If setExtBcPath() has been called, use that path.
140 // 2. If the command line argument -extapi=path/to/extapi.bc is provided, use that path.
141 // 3. If SVF is being developed and used directly from a build directory, try the build dir (SVF_BUILD_DIR).
142 // 4. If the SVF_DIR environment variable is set, try $SVF_DIR with the standard relative path.
143 // 5. If installed via npm, use `npm root` and append the standard relative path.
144 // 6. As a last resort, use the directory of the loaded libSVFCore.so (or .dylib) and append extapi.bc.
145
146 std::vector<std::string> candidatePaths;
147
148 // 1. Use path set by setExtBcPath()
149 if (!extBcPath.empty())
150 return extBcPath;
151
152 // 2. Use command line argument -extapi=...
154 {
155 return extBcPath;
156 }
157 else
158 {
160 }
161
162 // 3. SVF developer: try build directory (SVF_BUILD_DIR)
163 if (setExtBcPath(SVF_BUILD_DIR "/lib/extapi.bc"))
164 {
165 return extBcPath;
166 }
167 else
168 {
169 candidatePaths.push_back(SVF_BUILD_DIR "/lib/extapi.bc");
170 }
171
172 // 4. Use $SVF_DIR environment variable + standard relative path
173 if (setExtBcPath(getFilePath("SVF_DIR")))
174 {
175 return extBcPath;
176 }
177 else
178 {
179 candidatePaths.push_back(getFilePath("SVF_DIR"));
180 }
181
182 // 5. Use npm root + standard relative path (for npm installations)
183 if (setExtBcPath(getFilePath("npm root")))
184 {
185 return extBcPath;
186 }
187 else
188 {
189 candidatePaths.push_back(getFilePath("npm root"));
190 }
191
192 // 6. Use the directory of the loaded libSVFCore.so/.dylib + extapi.bc
193 std::string soPath = getCurrentSOPath();
194 if (!soPath.empty())
195 {
196 std::string dir = soPath.substr(0, soPath.find_last_of('/'));
197 std::string candidate = dir + "/extapi.bc";
199 {
200 return extBcPath;
201 }
202 else
203 {
204 candidatePaths.push_back(candidate);
205 }
206 }
207
208 // If all candidate paths failed, print error and suggestions
209 SVFUtil::errs() << "ERROR: Failed to locate \"extapi.bc\". Tried the following candidate paths:" << std::endl;
210 for (const auto& path : candidatePaths)
211 {
212 SVFUtil::errs() << " " << path << std::endl;
213 }
214 SVFUtil::errs() << "To override the default locations for \"extapi.bc\", you can:" << std::endl
215 << "\t1. Use the command line argument \"-extapi=path/to/extapi.bc\"" << std::endl
216 << "\t2. Use the \"setExtBcPath()\" function *BEFORE* calling \"buildSVFModule()\"" << std::endl
217 << "\t3. Override the paths in \"include/SVF/Util/config.h\" (WARNING: will be overwritten "
218 << "when rebuilding SVF (generated by CMakeLists.txt))" << std::endl;
219 abort();
220}
static std::string getFilePath(const std::string &path)
Definition ExtAPI.cpp:99
std::string getCurrentSOPath()
Definition ExtAPI.cpp:125
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:217
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 243 of file ExtAPI.cpp.

244{
245 assert(fun && "Null FunObjVar* pointer");
246 auto it = funObjVar2Annotations.find(fun);
247 if (it != funObjVar2Annotations.end())
248 {
249 for (const std::string& annotation : it->second)
250 if (annotation.find(funcAnnotation) != std::string::npos)
252 }
253 return "";
254}
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 256 of file ExtAPI.cpp.

257{
258 assert(fun && "Null FunObjVar* pointer");
259 auto it = funObjVar2Annotations.find(fun);
260 if (it != funObjVar2Annotations.end())
261 return it->second;
262 return funObjVar2Annotations[fun];
263}

◆ has_static()

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

◆ hasExtFuncAnnotation()

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

Definition at line 229 of file ExtAPI.cpp.

230{
231 assert(fun && "Null FunObjVar* pointer");
232 auto it = funObjVar2Annotations.find(fun);
233 if (it != funObjVar2Annotations.end())
234 {
235 for (const std::string& annotation : it->second)
236 if (annotation.find(funcAnnotation) != std::string::npos)
237 return true;
238 }
239 return false;
240}
#define true
Definition cJSON.cpp:65

◆ is_alloc()

bool ExtAPI::is_alloc ( const FunObjVar F)

Definition at line 277 of file ExtAPI.cpp.

278{
279 return F && hasExtFuncAnnotation(F, "ALLOC_HEAP_RET");
280}
bool hasExtFuncAnnotation(const FunObjVar *fun, const std::string &funcAnnotation)
Definition ExtAPI.cpp:229

◆ is_alloc_stack_ret()

bool ExtAPI::is_alloc_stack_ret ( const FunObjVar F)

Definition at line 288 of file ExtAPI.cpp.

289{
290 return F && hasExtFuncAnnotation(F, "ALLOC_STACK_RET");
291}

◆ is_arg_alloc()

bool ExtAPI::is_arg_alloc ( const FunObjVar F)

Definition at line 283 of file ExtAPI.cpp.

284{
285 return F && hasExtFuncAnnotation(F, "ALLOC_HEAP_ARG");
286}

◆ is_ext()

bool ExtAPI::is_ext ( const FunObjVar funObjVar)

Definition at line 314 of file ExtAPI.cpp.

315{
316 assert(F && "Null FunObjVar* pointer");
317 if (F->isDeclaration() || F->isIntrinsic())
318 return true;
319 else if (hasExtFuncAnnotation(F, "OVERWRITE") && getExtFuncAnnotations(F).size() == 1)
320 return false;
321 else
322 return !getExtFuncAnnotations(F).empty();
323}
const std::vector< std::string > & getExtFuncAnnotations(const FunObjVar *fun)
Definition ExtAPI.cpp:256

◆ is_memcpy()

bool ExtAPI::is_memcpy ( const FunObjVar F)

Definition at line 265 of file ExtAPI.cpp.

266{
267 return F &&
268 (hasExtFuncAnnotation(F, "MEMCPY") || hasExtFuncAnnotation(F, "STRCPY")
269 || hasExtFuncAnnotation(F, "STRCAT"));
270}

◆ is_memset()

bool ExtAPI::is_memset ( const FunObjVar F)

Definition at line 272 of file ExtAPI.cpp.

273{
274 return F && hasExtFuncAnnotation(F, "MEMSET");
275}

◆ is_realloc()

bool ExtAPI::is_realloc ( const FunObjVar F)

Definition at line 310 of file ExtAPI.cpp.

311{
312 return F && hasExtFuncAnnotation(F, "REALLOC_HEAP_RET");
313}

◆ 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 223 of file ExtAPI.cpp.

224{
225 assert(fun && "Null FunObjVar* pointer");
227}

Friends And Related Symbol Documentation

◆ LLVMModuleSet

Definition at line 48 of file ExtAPI.h.

◆ SVFIRBuilder

Definition at line 49 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: