Static Value-Flow Analysis
SVFType.h
Go to the documentation of this file.
1 //===- SVFBasicTypes.h -- Basic types used in SVF-----------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-> <Yulei Sui>
6 //
7 
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * BasicTypes.h
25  *
26  * Created on: Apr 1, 2014
27  * Author: Yulei Sui
28  */
29 
30 #ifndef INCLUDE_SVFIR_SVFTYPE_H_
31 #define INCLUDE_SVFIR_SVFTYPE_H_
32 
33 #include "Util/SparseBitVector.h"
34 #include "Util/GeneralType.h"
35 
36 
37 namespace SVF
38 {
39 class SVFType;
40 class SVFPointerType;
41 
42 
46 class StInfo
47 {
48  friend class SVFIRWriter;
49  friend class SVFIRReader;
50 
51 private:
53  std::vector<u32_t> fldIdxVec;
56  std::vector<u32_t> elemIdxVec;
60  std::vector<const SVFType*> finfo;
69  std::vector<const SVFType*> flattenElementTypes;
71 
72 public:
73  StInfo() = delete;
74  StInfo(const StInfo& st) = delete;
75  void operator=(const StInfo&) = delete;
76 
78  explicit StInfo(u32_t s)
80  {
81  }
83  ~StInfo() = default;
84 
91  //{@
92  const SVFType* getOriginalElemType(u32_t fldIdx) const;
93 
94  inline std::vector<u32_t>& getFlattenedFieldIdxVec()
95  {
96  return fldIdxVec;
97  }
98  inline std::vector<u32_t>& getFlattenedElemIdxVec()
99  {
100  return elemIdxVec;
101  }
102  inline std::vector<const SVFType*>& getFlattenElementTypes()
103  {
104  return flattenElementTypes;
105  }
106  inline std::vector<const SVFType*>& getFlattenFieldTypes()
107  {
108  return finfo;
109  }
110  inline const std::vector<u32_t>& getFlattenedFieldIdxVec() const
111  {
112  return fldIdxVec;
113  }
114  inline const std::vector<u32_t>& getFlattenedElemIdxVec() const
115  {
116  return elemIdxVec;
117  }
118  inline const std::vector<const SVFType*>& getFlattenElementTypes() const
119  {
120  return flattenElementTypes;
121  }
122  inline const std::vector<const SVFType*>& getFlattenFieldTypes() const
123  {
124  return finfo;
125  }
127 
129  void addFldWithType(u32_t fldIdx, const SVFType* type, u32_t elemIdx);
130 
132  inline void setNumOfFieldsAndElems(u32_t nf, u32_t ne)
133  {
134  numOfFlattenFields = nf;
136  }
137 
140  {
141  return numOfFlattenElements;
142  }
143 
146  {
147  return numOfFlattenFields;
148  }
150  inline u32_t getStride() const
151  {
152  return stride;
153  }
154 };
155 
156 class SVFType
157 {
158  friend class SVFIRWriter;
159  friend class SVFIRReader;
160  friend class LLVMModuleSet;
161 
162 public:
163  typedef s64_t GNodeK;
164 
166  {
174  };
175 
176 public:
177 
178  inline static SVFType* getSVFPtrType()
179  {
180  assert(svfPtrTy && "ptr type not set?");
181  return svfPtrTy;
182  }
183 
184  inline static SVFType* getSVFInt8Type()
185  {
186  assert(svfI8Ty && "int8 type not set?");
187  return svfI8Ty;
188  }
189 
190 private:
191 
192  static SVFType* svfPtrTy;
193  static SVFType* svfI8Ty;
194 
195 private:
201 
202 protected:
203  SVFType(bool svt, SVFTyKind k, u32_t Sz = 1)
204  : kind(k), typeinfo(nullptr),
205  isSingleValTy(svt), byteSize(Sz)
206  {
207  }
208 
209 public:
210  SVFType(void) = delete;
211  virtual ~SVFType() {}
212 
213  inline GNodeK getKind() const
214  {
215  return kind;
216  }
217 
221 
222  virtual void print(std::ostream& os) const = 0;
223 
224 
225  inline void setTypeInfo(StInfo* ti)
226  {
227  typeinfo = ti;
228  }
229 
230  inline StInfo* getTypeInfo()
231  {
232  assert(typeinfo && "set the type info first");
233  return typeinfo;
234  }
235 
236  inline const StInfo* getTypeInfo() const
237  {
238  assert(typeinfo && "set the type info first");
239  return typeinfo;
240  }
241 
244  inline u32_t getByteSize() const
245  {
246  return byteSize;
247  }
248 
249  inline bool isPointerTy() const
250  {
251  return kind == SVFPointerTy;
252  }
253 
254  inline bool isArrayTy() const
255  {
256  return kind == SVFArrayTy;
257  }
258 
259  inline bool isStructTy() const
260  {
261  return kind == SVFStructTy;
262  }
263 
264  inline bool isSingleValueType() const
265  {
266  return isSingleValTy;
267  }
268 };
269 
270 std::ostream& operator<<(std::ostream& os, const SVFType& type);
271 
272 class SVFPointerType : public SVFType
273 {
274  friend class SVFIRWriter;
275  friend class SVFIRReader;
276 
277 public:
280  {
281  }
282 
283  static inline bool classof(const SVFType* node)
284  {
285  return node->getKind() == SVFPointerTy;
286  }
287 
288  void print(std::ostream& os) const override;
289 };
290 
291 class SVFIntegerType : public SVFType
292 {
293  friend class SVFIRWriter;
294  friend class SVFIRReader;
295 
296 private:
297  short signAndWidth;
298 
299 public:
301  static inline bool classof(const SVFType* node)
302  {
303  return node->getKind() == SVFIntegerTy;
304  }
305 
306  void print(std::ostream& os) const override;
307 
308  void setSignAndWidth(short sw)
309  {
310  signAndWidth = sw;
311  }
312 
313  bool isSigned() const
314  {
315  return signAndWidth < 0;
316  }
317 };
318 
319 class SVFFunctionType : public SVFType
320 {
321  friend class SVFIRWriter;
322  friend class SVFIRReader;
323 
324 private:
325  const SVFType* retTy;
326 
327 public:
329  : SVFType(false, SVFFunctionTy, 1), retTy(rt)
330  {
331  }
332  static inline bool classof(const SVFType* node)
333  {
334  return node->getKind() == SVFFunctionTy;
335  }
336  const SVFType* getReturnType() const
337  {
338  return retTy;
339  }
340 
341  void print(std::ostream& os) const override;
342 };
343 
344 class SVFStructType : public SVFType
345 {
346  friend class SVFIRWriter;
347  friend class SVFIRReader;
348 
349 private:
352 
353 public:
355 
356  static inline bool classof(const SVFType* node)
357  {
358  return node->getKind() == SVFStructTy;
359  }
360 
361  void print(std::ostream& os) const override;
362 
364  {
365  return name;
366  }
368  {
369  name = structName;
370  }
372  {
374  }
375 };
376 
377 class SVFArrayType : public SVFType
378 {
379  friend class SVFIRWriter;
380  friend class SVFIRReader;
381 
382 private:
383  unsigned numOfElement;
385 
386 public:
389  {
390  }
391 
392  static inline bool classof(const SVFType* node)
393  {
394  return node->getKind() == SVFArrayTy;
395  }
396 
397  void print(std::ostream& os) const override;
398 
399  const SVFType* getTypeOfElement() const
400  {
401  return typeOfElement;
402  }
403 
404  void setTypeOfElement(const SVFType* elemType)
405  {
406  typeOfElement = elemType;
407  }
408 
409  void setNumOfElement(unsigned elemNum)
410  {
411  numOfElement = elemNum;
412  }
413 
414 
415 };
416 
417 class SVFOtherType : public SVFType
418 {
419  friend class SVFIRWriter;
420  friend class SVFIRReader;
421 
422 private:
424 
425 public:
426  SVFOtherType(bool isSingleValueTy, u32_t byteSize = 1) : SVFType(isSingleValueTy, SVFOtherTy, byteSize) {}
427 
428  static inline bool classof(const SVFType* node)
429  {
430  return node->getKind() == SVFOtherTy;
431  }
432 
434  {
435  return repr;
436  }
437 
439  {
440  repr = std::move(r);
441  }
442 
443  void setRepr(const std::string& r)
444  {
445  repr = r;
446  }
447 
448  void print(std::ostream& os) const override;
449 };
450 
451 // TODO: be explicit that this is a pair of 32-bit unsigneds?
452 template <> struct Hash<NodePair>
453 {
454  size_t operator()(const NodePair& p) const
455  {
456  // Make sure our assumptions are sound: use u32_t
457  // and u64_t. If NodeID is not actually u32_t or size_t
458  // is not u64_t we should be fine since we get a
459  // consistent result.
460  uint32_t first = (uint32_t)(p.first);
461  uint32_t second = (uint32_t)(p.second);
462  return ((uint64_t)(first) << 32) | (uint64_t)(second);
463  }
464 };
465 
466 #if !defined NDBUG && defined USE_SVF_DBOUT
467 // TODO: This comes from the following link
468 // https://github.com/llvm/llvm-project/blob/75e33f71c2dae584b13a7d1186ae0a038ba98838/llvm/include/llvm/Support/Debug.h#L64
469 // The original LLVM implementation makes use of type. But we can get that info,
470 // so we can't simulate the full behaviour for now.
471 # define SVF_DEBUG_WITH_TYPE(TYPE, X) \
472  do \
473  { \
474  X; \
475  } while (false)
476 #else
477 # define SVF_DEBUG_WITH_TYPE(TYPE, X) \
478  do \
479  { \
480  } while (false)
481 #endif
482 
484 #define DBOUT(TYPE, X) SVF_DEBUG_WITH_TYPE(TYPE, X)
485 #define DOSTAT(X) X
486 #define DOTIMESTAT(X) X
487 
490 #define DGENERAL "general"
491 
492 #define DPAGBuild "pag"
493 #define DMemModel "mm"
494 #define DMemModelCE "mmce"
495 #define DCOMModel "comm"
496 #define DDDA "dda"
497 #define DDumpPT "dumppt"
498 #define DRefinePT "sbpt"
499 #define DCache "cache"
500 #define DWPA "wpa"
501 #define DMSSA "mssa"
502 #define DInstrument "ins"
503 #define DAndersen "ander"
504 #define DSaber "saber"
505 #define DMTA "mta"
506 #define DCHA "cha"
507 
508 /*
509  * Number of clock ticks per second. A clock tick is the unit by which
510  * processor time is measured and is returned by 'clock'.
511  */
512 #define TIMEINTERVAL 1000
513 #define CLOCK_IN_MS() (clock() / (CLOCKS_PER_SEC / TIMEINTERVAL))
514 
516 #define NATIVE_INT_SIZE (sizeof(unsigned long long) * CHAR_BIT)
517 
519 {
524 };
525 
527 {
532 };
533 
534 } // End namespace SVF
535 
536 template <> struct std::hash<SVF::NodePair>
537 {
538  size_t operator()(const SVF::NodePair& p) const
539  {
540  // Make sure our assumptions are sound: use u32_t
541  // and u64_t. If NodeID is not actually u32_t or size_t
542  // is not u64_t we should be fine since we get a
543  // consistent result.
544  uint32_t first = (uint32_t)(p.first);
545  uint32_t second = (uint32_t)(p.second);
546  return ((uint64_t)(first) << 32) | (uint64_t)(second);
547  }
548 };
549 
551 template <unsigned N> struct std::hash<SVF::SparseBitVector<N>>
552 {
553  size_t operator()(const SVF::SparseBitVector<N>& sbv) const
554  {
556  return h(std::make_pair(std::make_pair(sbv.count(), sbv.find_first()),
557  sbv.find_last()));
558  }
559 };
560 
561 template <typename T> struct std::hash<std::vector<T>>
562 {
563  size_t operator()(const std::vector<T>& v) const
564  {
565  // TODO: repetition with CBV.
566  size_t h = v.size();
567 
568  SVF::Hash<T> hf;
569  for (const T& t : v)
570  {
571  h ^= hf(t) + 0x9e3779b9 + (h << 6) + (h >> 2);
572  }
573 
574  return h;
575  }
576 };
577 
578 #endif /* INCLUDE_SVFIR_SVFTYPE_H_ */
const std::string structName
Definition: CppUtil.cpp:56
cJSON * p
Definition: cJSON.cpp:2559
newitem type
Definition: cJSON.cpp:2739
#define false
Definition: cJSON.cpp:70
return true
Definition: cJSON.cpp:2295
const char *const string
Definition: cJSON.h:172
const SVFType * getTypeOfElement() const
Definition: SVFType.h:399
const SVFType * typeOfElement
For printing & debugging.
Definition: SVFType.h:384
SVFArrayType(u32_t byteSize=1)
For printing & debugging.
Definition: SVFType.h:387
unsigned numOfElement
Definition: SVFType.h:383
void setTypeOfElement(const SVFType *elemType)
Definition: SVFType.h:404
void setNumOfElement(unsigned elemNum)
Definition: SVFType.h:409
static bool classof(const SVFType *node)
Definition: SVFType.h:392
void print(std::ostream &os) const override
Definition: SVFType.cpp:47
const SVFType * retTy
Definition: SVFType.h:325
static bool classof(const SVFType *node)
Definition: SVFType.h:332
const SVFType * getReturnType() const
Definition: SVFType.h:336
SVFFunctionType(const SVFType *rt)
Definition: SVFType.h:328
void print(std::ostream &os) const override
Definition: SVFType.cpp:37
SVFIntegerType(u32_t byteSize=1)
Definition: SVFType.h:300
bool isSigned() const
Definition: SVFType.h:313
short signAndWidth
For printing.
Definition: SVFType.h:297
void print(std::ostream &os) const override
Definition: SVFType.cpp:29
void setSignAndWidth(short sw)
Definition: SVFType.h:308
static bool classof(const SVFType *node)
Definition: SVFType.h:301
void print(std::ostream &os) const override
Definition: SVFType.cpp:52
void setRepr(std::string &&r)
Definition: SVFType.h:438
static bool classof(const SVFType *node)
Definition: SVFType.h:428
void setRepr(const std::string &r)
Definition: SVFType.h:443
SVFOtherType(bool isSingleValueTy, u32_t byteSize=1)
Field representation for printing.
Definition: SVFType.h:426
const std::string & getRepr()
Definition: SVFType.h:433
std::string repr
Definition: SVFType.h:423
static bool classof(const SVFType *node)
Definition: SVFType.h:283
SVFPointerType(u32_t byteSize=1)
Definition: SVFType.h:278
void print(std::ostream &os) const override
Definition: SVFType.cpp:24
std::string name
Field for printing & debugging.
Definition: SVFType.h:351
static bool classof(const SVFType *node)
Definition: SVFType.h:356
void setName(const std::string &structName)
Definition: SVFType.h:367
SVFStructType(u32_t byteSize=1)
Definition: SVFType.h:354
void print(std::ostream &os) const override
Definition: SVFType.cpp:42
const std::string & getName()
Definition: SVFType.h:363
void setName(std::string &&structName)
Definition: SVFType.h:371
virtual ~SVFType()
Definition: SVFType.h:211
StInfo * getTypeInfo()
Definition: SVFType.h:230
SVFType(bool svt, SVFTyKind k, u32_t Sz=1)
Definition: SVFType.h:203
static SVFType * getSVFInt8Type()
Definition: SVFType.h:184
bool isArrayTy() const
Definition: SVFType.h:254
@ SVFFunctionTy
Definition: SVFType.h:170
@ SVFIntegerTy
Definition: SVFType.h:169
@ SVFPointerTy
Definition: SVFType.h:168
static SVFType * getSVFPtrType()
Definition: SVFType.h:178
GNodeK kind
used for classof
Definition: SVFType.h:196
GNodeK getKind() const
Definition: SVFType.h:213
s64_t GNodeK
Definition: SVFType.h:163
u32_t byteSize
array
Definition: SVFType.h:199
bool isSingleValueType() const
Definition: SVFType.h:264
static SVFType * svfPtrTy
ptr type
Definition: SVFType.h:192
StInfo * typeinfo
SVF's TypeInfo.
Definition: SVFType.h:197
bool isPointerTy() const
Definition: SVFType.h:249
std::string toString() const
u32_t getByteSize() const
Definition: SVFType.h:244
SVFType(void)=delete
static SVFType * svfI8Ty
8-bit int type
Definition: SVFType.h:193
bool isSingleValTy
The type represents a single value, not struct or.
Definition: SVFType.h:198
void setTypeInfo(StInfo *ti)
Definition: SVFType.h:225
virtual void print(std::ostream &os) const =0
bool isStructTy() const
Definition: SVFType.h:259
const StInfo * getTypeInfo() const
Definition: SVFType.h:236
unsigned count() const
void operator=(const StInfo &)=delete
std::vector< const SVFType * > & getFlattenElementTypes()
Definition: SVFType.h:102
const SVFType * getOriginalElemType(u32_t fldIdx) const
Definition: SVFValue.cpp:20
void setNumOfFieldsAndElems(u32_t nf, u32_t ne)
Set number of fields and elements of an aggregate.
Definition: SVFType.h:132
const std::vector< u32_t > & getFlattenedFieldIdxVec() const
Definition: SVFType.h:110
StInfo()=delete
Max field limit.
u32_t getNumOfFlattenElements() const
Return number of elements after flattening (including array elements)
Definition: SVFType.h:139
std::vector< u32_t > & getFlattenedElemIdxVec()
Definition: SVFType.h:98
~StInfo()=default
Destructor.
u32_t stride
Definition: SVFType.h:63
const std::vector< const SVFType * > & getFlattenElementTypes() const
Definition: SVFType.h:118
std::vector< u32_t > fldIdxVec
flattened field indices of a struct (ignoring arrays)
Definition: SVFType.h:53
u32_t numOfFlattenElements
number of elements after flattening (including array elements)
Definition: SVFType.h:65
std::vector< u32_t > elemIdxVec
Definition: SVFType.h:56
StInfo(u32_t s)
Constructor.
Definition: SVFType.h:78
StInfo(const StInfo &st)=delete
const std::vector< u32_t > & getFlattenedElemIdxVec() const
Definition: SVFType.h:114
const std::vector< const SVFType * > & getFlattenFieldTypes() const
Definition: SVFType.h:122
u32_t numOfFlattenFields
number of fields after flattening (ignoring array elements)
Definition: SVFType.h:67
void addFldWithType(u32_t fldIdx, const SVFType *type, u32_t elemIdx)
Add field index and element index and their corresponding type.
Definition: SVFValue.cpp:9
std::vector< u32_t > & getFlattenedFieldIdxVec()
Definition: SVFType.h:94
std::vector< const SVFType * > flattenElementTypes
Type vector of fields.
Definition: SVFType.h:69
std::vector< const SVFType * > & getFlattenFieldTypes()
Definition: SVFType.h:106
Map< u32_t, const SVFType * > fldIdx2TypeMap
Types of all fields of a struct.
Definition: SVFType.h:58
std::vector< const SVFType * > finfo
All field infos after flattening a struct.
Definition: SVFType.h:60
u32_t getNumOfFlattenFields() const
Return the number of fields after flattening (ignoring array elements)
Definition: SVFType.h:145
u32_t getStride() const
Return the stride.
Definition: SVFType.h:150
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
Definition: SVFUtil.h:447
for isBitcode
Definition: BasicTypes.h:68
ModRefInfo
Definition: SVFType.h:519
@ Ref
Definition: SVFType.h:521
@ NoModRef
Definition: SVFType.h:523
@ ModRef
Definition: SVFType.h:520
@ Mod
Definition: SVFType.h:522
AliasResult
Definition: SVFType.h:527
@ PartialAlias
Definition: SVFType.h:531
@ MustAlias
Definition: SVFType.h:530
@ MayAlias
Definition: SVFType.h:529
@ NoAlias
Definition: SVFType.h:528
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
unsigned u32_t
Definition: GeneralType.h:46
signed long long s64_t
Definition: GeneralType.h:49
std::pair< NodeID, NodeID > NodePair
Definition: GeneralType.h:111
IntervalValue operator<<(const IntervalValue &lhs, const IntervalValue &rhs)
Left binary shift of IntervalValues.
size_t operator()(const NodePair &p) const
Definition: SVFType.h:454
provide extra hash function for std::pair handling
Definition: GeneralType.h:85
size_t operator()(const SVF::NodePair &p) const
Definition: SVFType.h:538
size_t operator()(const SVF::SparseBitVector< N > &sbv) const
Definition: SVFType.h:553
size_t operator()(const std::vector< T > &v) const
Definition: SVFType.h:563