Static Value-Flow Analysis
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 SVFFunction *fun, const std::string &funcAnnotation)
 
const std::vector< std::string > & getExtFuncAnnotations (const SVFFunction *fun)
 
bool hasExtFuncAnnotation (const SVFFunction *fun, const std::string &funcAnnotation)
 
bool has_static (const SVFFunction *F)
 
bool is_memcpy (const SVFFunction *F)
 
bool is_memset (const SVFFunction *F)
 
bool is_alloc (const SVFFunction *F)
 
bool is_arg_alloc (const SVFFunction *F)
 
s32_t get_alloc_arg_pos (const SVFFunction *F)
 
bool is_realloc (const SVFFunction *F)
 
bool is_ext (const SVFFunction *F)
 

Static Public Member Functions

static ExtAPIgetExtAPI ()
 
static void destory ()
 
static bool setExtBcPath (const std::string &path)
 

Private Member Functions

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

Private Attributes

Map< const SVFFunction *, std::vector< std::string > > func2Annotations
 

Static Private Attributes

static ExtAPIextOp = nullptr
 
static std::string extBcPath = ""
 

Friends

class LLVMModuleSet
 

Detailed Description

Definition at line 44 of file ExtAPI.h.

Constructor & Destructor Documentation

◆ ExtAPI()

SVF::ExtAPI::ExtAPI ( )
privatedefault

Member Function Documentation

◆ destory()

void ExtAPI::destory ( )
static

Definition at line 51 of file ExtAPI.cpp.

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

◆ get_alloc_arg_pos()

s32_t ExtAPI::get_alloc_arg_pos ( const SVFFunction F)

Definition at line 229 of file ExtAPI.cpp.

230 {
231  std::string allocArg = getExtFuncAnnotation(F, "ALLOC_ARG");
232  assert(!allocArg.empty() && "Not an alloc call via argument or incorrect extern function annotation!");
233 
235  for (char c : allocArg)
236  {
237  if (isdigit(c))
238  number.push_back(c);
239  }
240  assert(!number.empty() && "Incorrect naming convention for svf external functions(ALLOC_ARG + number)?");
241  return std::stoi(number);
242 }
#define F(f)
const char *const const double number
Definition: cJSON.h:268
const char *const string
Definition: cJSON.h:172
std::string getExtFuncAnnotation(const SVFFunction *fun, const std::string &funcAnnotation)
Definition: ExtAPI.cpp:183
int isdigit(int c)
Definition: extapi.c:851

◆ getExtAPI()

ExtAPI * ExtAPI::getExtAPI ( )
static

Definition at line 42 of file ExtAPI.cpp.

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

◆ getExtBcPath()

std::string ExtAPI::getExtBcPath ( )

Definition at line 119 of file ExtAPI.cpp.

120 {
121  // Default ways of retrieving extapi.bc (in order of precedence):
122  // 1. Set `path/to/extapi.bc` through `setExtBcPath()`
123  // 2. Set `path/to/extapi.bc` through the command line argument `-extapi=path/to/extapi.bc`
124  // 3. Get location generated by CMakeLists.txt from `config.h` header file (if SVF was installed)
125  // 4. Get location in build tree based from `config.h` header file (if SVF was only built)
126  // 5. Get location based on environment variable $ENV{SVF_DIR}
127  // 6. Search for `extapi.bc` from root directory for npm installation (iff SVF installed through npm)
128 
129  // 1. Set `path/to/extapi.bc` through `setExtBcPath()`
130  if (!extBcPath.empty())
131  return extBcPath;
132 
133  // 2. Set `path/to/extapi.bc` through the command line argument `-extapi=path/to/extapi.bc`
135  return extBcPath;
136 
137  // 3. Get location generated by CMakeLists.txt from `config.h` header file (if SVF was installed)
138  if (setExtBcPath(SVF_EXTAPI_BC)) // Full path is available (for custom file names)
139  return extBcPath;
140  if (setExtBcPath(SVF_EXTAPI_DIR "/extapi.bc")) // Based on directory & default filename
141  return extBcPath;
142 
143  // 4. Get location in build tree based from `config.h` header file (if SVF was only built)
144  if (setExtBcPath(SVF_BUILD_DIR "/lib/extapi.bc"))
145  return extBcPath;
146 
147  // 5. Get location based on environment variable $ENV{SVF_DIR}
148  if (setExtBcPath(getFilePath("SVF_DIR")))
149  return extBcPath;
150 
151  // 6. Search for `extapi.bc` from root directory for npm installation (iff SVF installed through npm)
152  if (setExtBcPath(getFilePath("npm root")))
153  return extBcPath;
154 
155  SVFUtil::errs() << "ERROR: Failed to find \"extapi.bc\" LLVM bitcode file in " << extBcPath << std::endl
156  << "To override the default locations for \"extapi.bc\", you can:" << std::endl
157  << "\t1. Use the command line argument \"-extapi=path/to/extapi.bc\"" << std::endl
158  << "\t2. Use the \"setExtBcPath()\" function *BEFORE* calling \"buildSVFModule()\"" << std::endl
159  << "\t3. Override the paths in \"svf/Util/config.h\" (WARNING: will be overwritten when "
160  << "rebuilding SVF (generated by CMakeLists.txt))" << std::endl;
161  abort();
162 }
static std::string getFilePath(const std::string &path)
Definition: ExtAPI.cpp:97
static std::string extBcPath
Definition: ExtAPI.h:55
static bool setExtBcPath(const std::string &path)
Definition: ExtAPI.cpp:61
static const Option< std::string > ExtAPIPath
Definition: Options.h:220
std::ostream & errs()
Overwrite llvm::errs()
Definition: SVFUtil.h:56

◆ getExtFuncAnnotation()

std::string ExtAPI::getExtFuncAnnotation ( const SVFFunction fun,
const std::string funcAnnotation 
)

Definition at line 183 of file ExtAPI.cpp.

184 {
185  assert(fun && "Null SVFFunction* pointer");
186  auto it = func2Annotations.find(fun);
187  if (it != func2Annotations.end())
188  {
189  for (const std::string& annotation : it->second)
190  if (annotation.find(funcAnnotation) != std::string::npos)
191  return annotation;
192  }
193  return "";
194 }
Map< const SVFFunction *, std::vector< std::string > > func2Annotations
Definition: ExtAPI.h:52

◆ getExtFuncAnnotations()

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

Definition at line 196 of file ExtAPI.cpp.

197 {
198  assert(fun && "Null SVFFunction* pointer");
199  auto it = func2Annotations.find(fun);
200  if (it != func2Annotations.end())
201  return it->second;
202  return func2Annotations[fun];
203 }

◆ has_static()

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

◆ hasExtFuncAnnotation()

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

Definition at line 170 of file ExtAPI.cpp.

171 {
172  assert(fun && "Null SVFFunction* pointer");
173  auto it = func2Annotations.find(fun);
174  if (it != func2Annotations.end())
175  {
176  for (const std::string& annotation : it->second)
177  if (annotation.find(funcAnnotation) != std::string::npos)
178  return true;
179  }
180  return false;
181 }

◆ is_alloc()

bool ExtAPI::is_alloc ( const SVFFunction F)

Definition at line 217 of file ExtAPI.cpp.

218 {
219  return F && hasExtFuncAnnotation(F, "ALLOC_RET");
220 }
bool hasExtFuncAnnotation(const SVFFunction *fun, const std::string &funcAnnotation)
Definition: ExtAPI.cpp:170

◆ is_arg_alloc()

bool ExtAPI::is_arg_alloc ( const SVFFunction F)

Definition at line 223 of file ExtAPI.cpp.

224 {
225  return F && hasExtFuncAnnotation(F, "ALLOC_ARG");
226 }

◆ is_ext()

bool ExtAPI::is_ext ( const SVFFunction F)

Definition at line 253 of file ExtAPI.cpp.

254 {
255  assert(F && "Null SVFFunction* pointer");
256  if (F->isDeclaration() || F->isIntrinsic())
257  return true;
258  else if (hasExtFuncAnnotation(F, "OVERWRITE") && getExtFuncAnnotations(F).size() == 1)
259  return false;
260  else
261  return !getExtFuncAnnotations(F).empty();
262 }
const std::vector< std::string > & getExtFuncAnnotations(const SVFFunction *fun)
Definition: ExtAPI.cpp:196

◆ is_memcpy()

bool ExtAPI::is_memcpy ( const SVFFunction F)

Definition at line 205 of file ExtAPI.cpp.

206 {
207  return F &&
208  (hasExtFuncAnnotation(F, "MEMCPY") || hasExtFuncAnnotation(F, "STRCPY")
209  || hasExtFuncAnnotation(F, "STRCAT"));
210 }

◆ is_memset()

bool ExtAPI::is_memset ( const SVFFunction F)

Definition at line 212 of file ExtAPI.cpp.

213 {
214  return F && hasExtFuncAnnotation(F, "MEMSET");
215 }

◆ is_realloc()

bool ExtAPI::is_realloc ( const SVFFunction F)

Definition at line 245 of file ExtAPI.cpp.

246 {
247  return F && hasExtFuncAnnotation(F, "REALLOC_RET");
248 }

◆ setExtBcPath()

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

Definition at line 61 of file ExtAPI.cpp.

62 {
63  struct stat statbuf;
64  if (!path.empty() && !stat(path.c_str(), &statbuf))
65  {
66  extBcPath = path;
67  return true;
68  }
69  return false;
70 }

◆ setExtFuncAnnotations()

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

Definition at line 164 of file ExtAPI.cpp.

165 {
166  assert(fun && "Null SVFFunction* pointer");
167  func2Annotations[fun] = funcAnnotations;
168 }

Friends And Related Function Documentation

◆ LLVMModuleSet

friend class LLVMModuleSet
friend

Definition at line 46 of file ExtAPI.h.

Member Data Documentation

◆ extBcPath

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

Definition at line 55 of file ExtAPI.h.

◆ extOp

ExtAPI * ExtAPI::extOp = nullptr
staticprivate

Definition at line 49 of file ExtAPI.h.

◆ func2Annotations

Map<const SVFFunction*, std::vector<std::string> > SVF::ExtAPI::func2Annotations
private

Definition at line 52 of file ExtAPI.h.


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