svf-lib 1.0.1050 → 1.0.1053

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,8 +70,7 @@ private:
70
70
 
71
71
  if (callee)
72
72
  {
73
- if (ExtAPI::EFT_L_A0__A0R_A1R == ExtAPI::getExtAPI()->get_type(callee)
74
- || callee->getName().find("llvm.memset") != std::string::npos)
73
+ if (callee->getName().find("llvm.memset") != std::string::npos)
75
74
  return I;
76
75
  }
77
76
  I = I->getPrevNode();
@@ -78,7 +78,7 @@ public:
78
78
  inline bool isMemcpy(const Instruction *I)
79
79
  {
80
80
  const SVFFunction* F =SVFUtil::getCallee(I);
81
- return F && ExtAPI::EFT_L_A0__A0R_A1R == ExtAPI::getExtAPI()->get_type(F);
81
+ return F && F->getName().find("llvm.memcpy") != std::string::npos;
82
82
  }
83
83
 
84
84
  private:
@@ -416,9 +416,9 @@ private:
416
416
 
417
417
  if (const SVFFunction *callee = SVFUtil::getCallee(I))
418
418
  {
419
- if (ExtAPI::EFT_L_A0__A0R_A1R == ExtAPI::getExtAPI()->get_type(callee)
420
- || callee->getName().find("llvm.memset") != std::string::npos)
419
+ if (callee->getName().find("llvm.memset") != std::string::npos)
421
420
  return I;
421
+
422
422
  }
423
423
  I = I->getPrevNode();
424
424
  }
@@ -34,6 +34,7 @@
34
34
  #include "Util/ExtAPI.h"
35
35
  #include "SVF-FE/BasicTypes.h"
36
36
  #include "SVF-FE/ICFGBuilder.h"
37
+ #include "Util/cJSON.h"
37
38
 
38
39
  namespace SVF
39
40
  {
@@ -240,6 +241,8 @@ protected:
240
241
 
241
242
  /// Handle external call
242
243
  //@{
244
+ virtual u32_t getArgIndex(std::string s);
245
+ virtual NodeID parseNode(std::string s, CallSite cs, const Instruction *inst);
243
246
  virtual void handleExtCall(CallSite cs, const SVFFunction *F);
244
247
  void addComplexConsForExt(Value *D, Value *S, const Value* sz);
245
248
  //@}
@@ -28,218 +28,242 @@
28
28
 
29
29
  /*
30
30
  * Modified by Yulei Sui 2013
31
- */
31
+ */
32
32
 
33
33
  #ifndef __ExtAPI_H
34
34
  #define __ExtAPI_H
35
35
 
36
36
  #include "Util/BasicTypes.h"
37
- #include <map>
38
- #include <set>
37
+ #include "Util/cJSON.h"
38
+ #include "Util/config.h"
39
39
  #include <string>
40
+ #include <map>
41
+
42
+ #define EXTAPI_JSON_PATH "/lib/Util/ExtAPI.json"
40
43
 
41
44
  namespace SVF
42
45
  {
43
46
 
44
- //------------------------------------------------------------------------------
47
+ /*
48
+
49
+ Specifications in ExtAPI.json
50
+
51
+ Specification language: The specification language of external function includes:
52
+ 1. the name of the function,
53
+ 2. the type of the function,
54
+ 3. the operations of the function.
55
+
56
+ Funtion type: Funtion type represents some property of the function.
57
+ For example,
58
+ ”EFT_ALLOC“, represents if this external function allocates a new object and assigns it to one of its arguments,
59
+ For the selection of Function type and a more detailed explanation, please refer to enum extf_t in ExtAPI.h.
60
+
61
+ Function operations: Function operations indicates the relationship between input and output,
62
+ mainly the relationship between function parameters, or between parameters and return values after function is executed.
63
+ For example,
64
+ "copy": ["A2", "L"], indicates that after this external function is executed, the value of the 2th parameter is copied into the return value.
65
+ For the selection of Function type and a more detailed explanation, please refer to enum extType in ExtAPI.h.
66
+
67
+ For operands of function operation, e.g., "A2", "L", there are the following options:
68
+ "A": represents an parameter;
69
+ "N": represents a number;
70
+ "R": represents a reference;
71
+ "L": represents a return value;
72
+ "V": represents a dummy node;
73
+
74
+ Among them, parameter may have multiple forms, because there may be multiple parameters, and parameter may be a reference or complex structure,
75
+
76
+ Here we use regular expressions "(AN)(R|RN)^*" to represent parameter, for example,
77
+ "A0": represents the 1th parameter;
78
+ "A2R": represents that the 3th parameter is a reference;
79
+ "A1R2": represents the 3th substructure of 2th parameter "A1R", where "A1R" is a complex structure;
80
+ "A2R3R": represents the 4th substructure of 3th parameter "A2R" is a reference, where "A2R" is a complex structure;
81
+
82
+
83
+ Specification format:
84
+ "functionName": {
85
+ "type": "functional type",
86
+ "function operation_1": [ operand_1, operand_2, ... , operand_n],
87
+ "function operation_2": [ operand_1, operand_2, ... , operand_n],
88
+ ...
89
+ "function operation_n": [ operand_1, operand_2, ... , operand_n]
90
+ }
91
+ */
92
+
93
+
45
94
  class ExtAPI
46
95
  {
47
96
  public:
48
- //External Function types
49
- //Assume a call in the form LHS= F(arg0, arg1, arg2, arg3).
50
97
  enum extf_t
51
98
  {
52
- EFT_NOOP= 0, //no effect on pointers
53
- EFT_ALLOC, //returns a ptr to a newly allocated object
54
- EFT_REALLOC, //like L_A0 if arg0 is a non-null ptr, else ALLOC
55
- EFT_FREE, //free memory arg0 and all pointers passing into free function
56
- EFT_FREE_MULTILEVEL, //any argument with 2-level pointer passing too a free wrapper function e.g., XFree(void**) which frees memory for void* and void**
57
- EFT_NOSTRUCT_ALLOC, //like ALLOC but only allocates non-struct data
58
- EFT_STAT, //retval points to an unknown static var X
59
- EFT_STAT2, //ret -> X -> Y (X, Y - external static vars)
60
- EFT_L_A0, //copies arg0, arg1, or arg2 into LHS
99
+ EXT_ADDR = 0, // Handle addr edge
100
+ EXT_COPY, // Handle copy edge
101
+ EXT_LOAD, // Handle load edge
102
+ EXT_STORE, // Handle store edge
103
+ EXT_LOADSTORE, // Handle load and store edges, and add a dummy node
104
+ EXT_COPY_N, // Copy the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str
105
+ EXT_COPY_MN, // Copies n characters from memory area src to memory area dest.
106
+ EXT_FUNPTR, // Handle function void *dlsym(void *handle, const char *symbol)
107
+ EXT_COMPLEX, // Handle function _ZSt29_Rb_tree_insert_and_rebalancebPSt18_Rb_tree_node_baseS0_RS_
108
+ EXT_OTHER
109
+ };
110
+
111
+ // External Function types
112
+ // Assume a call in the form LHS= F(arg0, arg1, arg2, arg3).
113
+ enum extType
114
+ {
115
+ EFT_NOOP = 0, // no effect on pointers
116
+ EFT_ALLOC, // returns a ptr to a newly allocated object
117
+ EFT_REALLOC, // like L_A0 if arg0 is a non-null ptr, else ALLOC
118
+ EFT_FREE, // free memory arg0 and all pointers passing into free function
119
+ EFT_FREE_MULTILEVEL, // any argument with 2-level pointer passing too a free wrapper function e.g., XFree(void**) which frees memory for void* and void**
120
+ EFT_NOSTRUCT_ALLOC, // like ALLOC but only allocates non-struct data
121
+ EFT_STAT, // retval points to an unknown static var X
122
+ EFT_STAT2, // ret -> X -> Y (X, Y - external static vars)
123
+ EFT_L_A0, // copies arg0, arg1, or arg2 into LHS
61
124
  EFT_L_A1,
62
125
  EFT_L_A2,
63
126
  EFT_L_A8,
64
- EFT_L_A0__A0R_A1, //stores arg1 into *arg0 and returns arg0 (currently only for memset)
65
- EFT_L_A0__A0R_A1R, //copies the data that arg1 points to into the location
66
- EFT_L_A1__FunPtr, //obtain the address of a symbol based on the arg1 (char*) and parse a function to LHS (e.g., void *dlsym(void *handle, char *funname);)
127
+ EFT_L_A0__A0R_A1, // stores arg1 into *arg0 and returns arg0 (currently only for memset)
128
+ EFT_L_A0__A0R_A1R, // copies the data that arg1 points to into the location
129
+ EFT_L_A1__FunPtr, // obtain the address of a symbol based on the arg1 (char*) and parse a function to LHS (e.g., void *dlsym(void *handle, char *funname);)
67
130
  // arg0 points to; note that several fields may be
68
131
  // copied at once if both point to structs.
69
132
  // Returns arg0.
70
- EFT_A1R_A0R, //copies *arg0 into *arg1, with non-ptr return
71
- EFT_A3R_A1R_NS, //copies *arg1 into *arg3 (non-struct copy only)
72
- EFT_A1R_A0, //stores arg0 into *arg1
73
- EFT_A2R_A1, //stores arg1 into *arg2
74
- EFT_A4R_A1, //stores arg1 into *arg4
75
- EFT_L_A0__A2R_A0, //stores arg0 into *arg2 and returns it
76
- EFT_L_A0__A1_A0, //store arg1 into arg0's base and returns arg0
77
- EFT_A0R_NEW, //stores a pointer to an allocated object in *arg0
78
- EFT_A1R_NEW, //as above, into *arg1, etc.
133
+ EFT_A1R_A0R, // copies *arg0 into *arg1, with non-ptr return
134
+ EFT_A3R_A1R_NS, // copies *arg1 into *arg3 (non-struct copy only)
135
+ EFT_A1R_A0, // stores arg0 into *arg1
136
+ EFT_A2R_A1, // stores arg1 into *arg2
137
+ EFT_A4R_A1, // stores arg1 into *arg4
138
+ EFT_L_A0__A2R_A0, // stores arg0 into *arg2 and returns it
139
+ EFT_L_A0__A1_A0, // store arg1 into arg0's base and returns arg0
140
+ EFT_A0R_NEW, // stores a pointer to an allocated object in *arg0
141
+ EFT_A1R_NEW, // as above, into *arg1, etc.
79
142
  EFT_A2R_NEW,
80
143
  EFT_A4R_NEW,
81
144
  EFT_A11R_NEW,
82
- EFT_STD_RB_TREE_INSERT_AND_REBALANCE, // Some complex effects
83
- EFT_STD_RB_TREE_INCREMENT, // Some complex effects
84
- EFT_STD_LIST_HOOK, // Some complex effects
145
+ EFT_STD_RB_TREE_INSERT_AND_REBALANCE, // Some complex effects
146
+ EFT_STD_RB_TREE_INCREMENT, // Some complex effects
147
+ EFT_STD_LIST_HOOK, // Some complex effects
85
148
 
86
- CPP_EFT_A0R_A1, //stores arg1 into *arg0
87
- CPP_EFT_A0R_A1R, //copies *arg1 into *arg0
88
- CPP_EFT_A1R, //load arg1
149
+ CPP_EFT_A0R_A1, // stores arg1 into *arg0
150
+ CPP_EFT_A0R_A1R, // copies *arg1 into *arg0
151
+ CPP_EFT_A1R, // load arg1
89
152
  EFT_CXA_BEGIN_CATCH, //__cxa_begin_catch
90
153
  CPP_EFT_DYNAMIC_CAST, // dynamic_cast
91
- EFT_OTHER //not found in the list
154
+ EFT_NULL // not found in the list
92
155
  };
93
- private:
94
156
 
95
- //Each Function name is mapped to its extf_t
96
- Map<std::string, extf_t> info;
97
- //A cache of is_ext results for all SVFFunction*'s (hash_map is fastest).
98
- Map<const SVFFunction*, bool> isext_cache;
99
-
100
- void init(); //fill in the map (see ExtAPI.cpp)
157
+ private:
158
+ std::map<std::string, extf_t> op_pair =
159
+ {
160
+ {"addr", EXT_ADDR},
161
+ {"copy", EXT_COPY},
162
+ {"load", EXT_LOAD},
163
+ {"store", EXT_STORE},
164
+ {"load_store", EXT_LOADSTORE},
165
+ {"copy_n", EXT_COPY_N},
166
+ {"copy_mn", EXT_COPY_MN},
167
+ {"complex", EXT_COMPLEX},
168
+ {"funptr", EXT_FUNPTR}
169
+ };
101
170
 
102
- ExtAPI()
171
+ std::map<std::string, extType> type_pair =
103
172
  {
104
- init();
105
- isext_cache.clear();
106
- }
173
+ {"EFT_NOOP", EFT_NOOP},
174
+ {"EFT_ALLOC", EFT_ALLOC},
175
+ {"EFT_REALLOC", EFT_REALLOC},
176
+ {"EFT_FREE", EFT_FREE},
177
+ {"EFT_FREE_MULTILEVEL", EFT_FREE_MULTILEVEL},
178
+ {"EFT_NOSTRUCT_ALLOC", EFT_NOSTRUCT_ALLOC},
179
+ {"EFT_STAT", EFT_STAT},
180
+ {"EFT_STAT2", EFT_STAT2},
181
+ {"EFT_L_A0", EFT_L_A0},
182
+ {"EFT_L_A1", EFT_L_A1},
183
+ {"EFT_L_A2", EFT_L_A2},
184
+ {"EFT_L_A8", EFT_L_A8},
185
+ {"EFT_L_A0__A0R_A1", EFT_L_A0__A0R_A1},
186
+ {"EFT_L_A0__A0R_A1R", EFT_L_A0__A0R_A1R},
187
+ {"EFT_L_A1__FunPtr", EFT_L_A1__FunPtr},
188
+ {"EFT_A1R_A0R", EFT_A1R_A0R},
189
+ {"EFT_A3R_A1R_NS", EFT_A3R_A1R_NS},
190
+ {"EFT_A1R_A0", EFT_A1R_A0},
191
+ {"EFT_A2R_A1", EFT_A2R_A1},
192
+ {"EFT_A4R_A1", EFT_A4R_A1},
193
+ {"EFT_L_A0__A2R_A0", EFT_L_A0__A2R_A0},
194
+ {"EFT_L_A0__A1_A0", EFT_L_A0__A1_A0},
195
+ {"EFT_A0R_NEW", EFT_A0R_NEW},
196
+ {"EFT_A1R_NEW", EFT_A1R_NEW},
197
+ {"EFT_A2R_NEW", EFT_A2R_NEW},
198
+ {"EFT_A4R_NEW", EFT_A4R_NEW},
199
+ {"EFT_A11R_NEW", EFT_A11R_NEW},
200
+ {"EFT_STD_RB_TREE_INSERT_AND_REBALANCE", EFT_STD_RB_TREE_INSERT_AND_REBALANCE},
201
+ {"EFT_STD_RB_TREE_INCREMENT", EFT_STD_RB_TREE_INCREMENT},
202
+ {"EFT_STD_LIST_HOOK", EFT_STD_LIST_HOOK},
203
+ {"CPP_EFT_A0R_A1R", CPP_EFT_A0R_A1R},
204
+ {"CPP_EFT_A1R", CPP_EFT_A1R},
205
+ {"EFT_CXA_BEGIN_CATCH", EFT_CXA_BEGIN_CATCH},
206
+ {"CPP_EFT_DYNAMIC_CAST", CPP_EFT_DYNAMIC_CAST},
207
+ {"", EFT_NULL}
208
+ };
107
209
 
108
- // Singleton pattern here to enable instance of SVFIR can only be created once.
109
- static ExtAPI* extAPI;
210
+ static ExtAPI *extOp;
211
+
212
+ // Store specifications of external functions in ExtAPI.json file
213
+ static cJSON *root;
110
214
 
111
215
  public:
216
+ static ExtAPI *getExtAPI();
112
217
 
113
- /// Singleton design here to make sure we only have one instance during whole analysis
114
- static ExtAPI* getExtAPI()
115
- {
116
- if (extAPI == nullptr)
117
- {
118
- extAPI = new ExtAPI();
119
- }
120
- return extAPI;
121
- }
218
+ static void destory();
122
219
 
123
- static void destory()
124
- {
125
- if (extAPI != nullptr)
126
- {
127
- delete extAPI;
128
- extAPI = nullptr;
129
- }
130
- }
220
+ // Get the corresponding name in ext_t, e.g. "EXT_ADDR" in {"addr", EXT_ADDR},
221
+ extf_t get_opName(std::string s);
131
222
 
132
- //Return the extf_t of (F).
133
- extf_t get_type(const SVFFunction* F) const
134
- {
135
- assert(F);
136
- std::string funName = F->getName();
137
- if(F->isIntrinsic())
138
- {
139
- unsigned start = funName.find('.');
140
- unsigned end = funName.substr(start + 1).find('.');
141
- funName = "llvm." + funName.substr(start + 1, end);
142
- }
143
- Map<std::string, extf_t>::const_iterator it= info.find(funName);
144
- if(it == info.end())
145
- return EFT_OTHER;
146
- else
147
- return it->second;
148
- }
223
+ // Return the extf_t of (F).
224
+ // Get external function name, e.g "memcpy"
225
+ std::string get_name(const SVFFunction *F);
149
226
 
150
- //Does (F) have a static var X (unavailable to us) that its return points to?
151
- bool has_static(const SVFFunction* F) const
152
- {
153
- extf_t t= get_type(F);
154
- return t==EFT_STAT || t==EFT_STAT2;
155
- }
156
- //Assuming hasStatic(F), does (F) have a second static Y where X -> Y?
157
- bool has_static2(const SVFFunction* F) const
158
- {
159
- return get_type(F) == EFT_STAT2;
160
- }
161
- //Does (F) allocate a new object and return it?
162
- bool is_alloc(const SVFFunction* F) const
163
- {
164
- extf_t t= get_type(F);
165
- return t==EFT_ALLOC || t==EFT_NOSTRUCT_ALLOC;
166
- }
167
- //Does (F) allocate a new object and assign it to one of its arguments?
168
- bool is_arg_alloc(const SVFFunction* F) const
169
- {
170
- extf_t t= get_type(F);
171
- return t>=EFT_A0R_NEW && t<=EFT_A11R_NEW;
172
- }
173
- //Get the position of argument which holds the new object
174
- int get_alloc_arg_pos(const SVFFunction* F) const
175
- {
176
- extf_t t= get_type(F);
177
- switch(t)
178
- {
179
- case EFT_A0R_NEW:
180
- return 0;
181
- case EFT_A1R_NEW:
182
- return 1;
183
- case EFT_A2R_NEW:
184
- return 2;
185
- case EFT_A4R_NEW:
186
- return 4;
187
- case EFT_A11R_NEW:
188
- return 11;
189
- default:
190
- assert(false && "Not an alloc call via argument.");
191
- return -1;
192
- }
193
- }
194
- //Does (F) allocate only non-struct objects?
195
- bool no_struct_alloc(const SVFFunction* F) const
196
- {
197
- return get_type(F) == EFT_NOSTRUCT_ALLOC;
198
- }
199
- //Does (F) not free/release any memory?
200
- bool is_dealloc(const SVFFunction* F) const
201
- {
202
- extf_t t= get_type(F);
203
- return t == EFT_FREE;
204
- }
205
- //Does (F) not do anything with the known pointers?
206
- bool is_noop(const SVFFunction* F) const
207
- {
208
- extf_t t= get_type(F);
209
- return t == EFT_NOOP || t == EFT_FREE;
210
- }
211
- //Does (F) reallocate a new object?
212
- bool is_realloc(const SVFFunction* F) const
213
- {
214
- extf_t t= get_type(F);
215
- return t==EFT_REALLOC;
216
- }
217
- //Should (F) be considered "external" (either not defined in the program
218
- // or a user-defined version of a known alloc or no-op)?
219
- bool is_ext(const SVFFunction* F)
220
- {
221
- assert(F);
222
- //Check the cache first; everything below is slower.
223
- Map<const SVFFunction*, bool>::iterator i_iec= isext_cache.find(F);
224
- if(i_iec != isext_cache.end())
225
- return i_iec->second;
226
-
227
- bool res;
228
- if(F->isDeclaration() || F->isIntrinsic())
229
- {
230
- res= 1;
231
- }
232
- else
233
- {
234
- extf_t t= get_type(F);
235
- res= t==EFT_ALLOC || t==EFT_REALLOC || t==EFT_NOSTRUCT_ALLOC
236
- || t==EFT_NOOP || t==EFT_FREE;
237
- }
238
- isext_cache[F]= res;
239
- return res;
240
- }
241
- };
227
+ // Get arguments of the operation, e.g. ["A1R", "A0", "A2"]
228
+ std::vector<std::string> get_opArgs(const cJSON *value);
229
+
230
+ // Get specifications of external functions in ExtAPI.json file
231
+ cJSON *get_FunJson(const std::string funName);
232
+
233
+ // Get property of the operation, e.g. "EFT_A1R_A0R"
234
+ extType get_type(const SVF::SVFFunction *callee);
235
+
236
+ // Does (F) have a static var X (unavailable to us) that its return points to?
237
+ bool has_static(const SVFFunction *F);
238
+
239
+ // Assuming hasStatic(F), does (F) have a second static Y where X -> Y?
240
+ bool has_static2(const SVFFunction *F);
242
241
 
242
+ // Does (F) allocate a new object and return it?
243
+ bool is_alloc(const SVFFunction *F);
244
+
245
+ // Does (F) allocate a new object and assign it to one of its arguments?
246
+ bool is_arg_alloc(const SVFFunction *F);
247
+
248
+ // Get the position of argument which holds the new object
249
+ s32_t get_alloc_arg_pos(const SVFFunction *F);
250
+
251
+ // Does (F) allocate only non-struct objects?
252
+ bool no_struct_alloc(const SVFFunction *F);
253
+
254
+ // Does (F) not free/release any memory?
255
+ bool is_dealloc(const SVFFunction *F);
256
+
257
+ // Does (F) not do anything with the known pointers?
258
+ bool is_noop(const SVFFunction *F);
259
+
260
+ // Does (F) reallocate a new object?
261
+ bool is_realloc(const SVFFunction *F);
262
+
263
+ // Should (F) be considered "external" (either not defined in the program
264
+ // or a user-defined version of a known alloc or no-op)?
265
+ bool is_ext(const SVFFunction *F);
266
+ };
243
267
  } // End namespace SVF
244
268
 
245
- #endif
269
+ #endif
@@ -405,12 +405,6 @@ inline bool isStaticExtFun(const SVFFunction* fun)
405
405
  return fun && ExtAPI::getExtAPI()->has_static(fun);
406
406
  }
407
407
 
408
- /// Return external call type
409
- inline ExtAPI::extf_t extCallTy(const SVFFunction* fun)
410
- {
411
- return ExtAPI::getExtAPI()->get_type(fun);
412
- }
413
-
414
408
  /// Program entry function e.g. main
415
409
  //@{
416
410
  /// Return true if this is a program entry function (e.g. main)