Static Value-Flow Analysis
Loading...
Searching...
No Matches
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
34#include "Util/GeneralType.h"
35
36
37namespace SVF
38{
39class SVFType;
40class SVFPointerType;
41
42
46class StInfo
47{
48
49 friend class GraphDBClient;
50 friend class IRGraph;
51
52protected:
60
61 inline const u32_t getStinfoId() const
62 {
63 return StInfoId;
64 }
65
67 {
68 return fldIdx2TypeMap;
69 }
70
71 inline void setStinfoId(u32_t id)
72 {
73 StInfoId = id;
74 }
75
76private:
79 std::vector<u32_t> fldIdxVec;
82 std::vector<u32_t> elemIdxVec;
86 std::vector<const SVFType*> finfo;
95 std::vector<const SVFType*> flattenElementTypes;
97
98public:
99 StInfo() = delete;
100 StInfo(const StInfo& st) = delete;
101 void operator=(const StInfo&) = delete;
102
104 explicit StInfo(u32_t s)
106 {
107 }
108
110 ~StInfo() = default;
111
118 //{@
119 const SVFType* getOriginalElemType(u32_t fldIdx) const;
120
121 inline std::vector<u32_t>& getFlattenedFieldIdxVec()
122 {
123 return fldIdxVec;
124 }
125 inline std::vector<u32_t>& getFlattenedElemIdxVec()
126 {
127 return elemIdxVec;
128 }
129 inline std::vector<const SVFType*>& getFlattenElementTypes()
130 {
131 return flattenElementTypes;
132 }
133 inline std::vector<const SVFType*>& getFlattenFieldTypes()
134 {
135 return finfo;
136 }
137 inline const std::vector<u32_t>& getFlattenedFieldIdxVec() const
138 {
139 return fldIdxVec;
140 }
141 inline const std::vector<u32_t>& getFlattenedElemIdxVec() const
142 {
143 return elemIdxVec;
144 }
145 inline const std::vector<const SVFType*>& getFlattenElementTypes() const
146 {
147 return flattenElementTypes;
148 }
149 inline const std::vector<const SVFType*>& getFlattenFieldTypes() const
150 {
151 return finfo;
152 }
154
157
164
167 {
169 }
170
173 {
174 return numOfFlattenFields;
175 }
177 inline u32_t getStride() const
178 {
179 return stride;
180 }
181};
182
184{
185
186 friend class LLVMModuleSet;
187
188public:
189 typedef s64_t GNodeK;
190
201
202protected:
203
205 inline static void setSVFPtrType(SVFType* ptrTy)
206 {
207 svfPtrTy = ptrTy;
208 }
209
210 inline static void setSVFInt8Type(SVFType* i8Ty)
211 {
212 svfI8Ty = i8Ty;
213 }
214
215
216public:
217
218 inline static SVFType* getSVFPtrType()
219 {
220 assert(svfPtrTy && "ptr type not set?");
221 return svfPtrTy;
222 }
223
224 inline static SVFType* getSVFInt8Type()
225 {
226 assert(svfI8Ty && "int8 type not set?");
227 return svfI8Ty;
228 }
229
230private:
231
233 static SVFType* svfI8Ty;
234
235private:
242
243protected:
244 SVFType(bool svt, SVFTyKind k, u32_t i = 0, u32_t Sz = 1)
245 : kind(k), typeinfo(nullptr),
247 {
248 }
249public:
250 SVFType(void) = delete;
251 virtual ~SVFType() {}
252
253 inline GNodeK getKind() const
254 {
255 return kind;
256 }
257
260 std::string toString() const;
261
262 virtual void print(std::ostream& os) const = 0;
263
264
265 u32_t getId() const
266 {
267 return id;
268 }
269
270 inline void setTypeInfo(StInfo* ti)
271 {
272 typeinfo = ti;
273 }
274
276 {
277 assert(typeinfo && "set the type info first");
278 return typeinfo;
279 }
280
281 inline const StInfo* getTypeInfo() const
282 {
283 assert(typeinfo && "set the type info first");
284 return typeinfo;
285 }
286
289 inline u32_t getByteSize() const
290 {
291 return byteSize;
292 }
293
294 inline bool isPointerTy() const
295 {
296 return kind == SVFPointerTy;
297 }
298
299 inline bool isArrayTy() const
300 {
301 return kind == SVFArrayTy;
302 }
303
304 inline bool isStructTy() const
305 {
306 return kind == SVFStructTy;
307 }
308
309 inline bool isSingleValueType() const
310 {
311 return isSingleValTy;
312 }
313};
314
315std::ostream& operator<<(std::ostream& os, const SVFType& type);
316
318{
319
320 friend class GraphDBClient;
321
322public:
327
328 static inline bool classof(const SVFType* node)
329 {
330 return node->getKind() == SVFPointerTy;
331 }
332
333 void print(std::ostream& os) const override;
334};
335
337{
338 friend class GraphDBClient;
339
340private:
342
343protected:
344
345 short getSignAndWidth() const
346 {
347 return signAndWidth;
348 }
349
350public:
352 static inline bool classof(const SVFType* node)
353 {
354 return node->getKind() == SVFIntegerTy;
355 }
356
357 void print(std::ostream& os) const override;
358
359 void setSignAndWidth(short sw)
360 {
362 }
363
364 bool isSigned() const
365 {
366 return signAndWidth < 0;
367 }
368};
369
371{
372
373 friend class GraphDBClient;
374private:
376 std::vector<const SVFType*> params;
377 bool varArg;
378
379protected:
383 const void setReturnType(const SVFType* rt)
384 {
385 retTy = rt;
386 }
387
389 {
390 params.push_back(type);
391 }
392
393public:
394 SVFFunctionType(u32_t i, const SVFType* rt, const std::vector<const SVFType*>& p, bool isvararg)
395 : SVFType(false, SVFFunctionTy, i, 1), retTy(rt), params(p), varArg(isvararg)
396 {
397 }
398
399 static inline bool classof(const SVFType* node)
400 {
401 return node->getKind() == SVFFunctionTy;
402 }
403 const SVFType* getReturnType() const
404 {
405 return retTy;
406 }
407
408 const std::vector<const SVFType*>& getParamTypes() const
409 {
410 return params;
411 }
412
413 bool isVarArg() const
414 {
415 return varArg;
416 }
417
418 void print(std::ostream& os) const override;
419};
420
421class SVFStructType : public SVFType
422{
423 friend class GraphDBClient;
424
425protected:
426
427 const std::string& getName() const
428 {
429 return name;
430 }
431
433 {
434 fields.push_back(type);
435 }
436
437private:
439 std::string name;
440 std::vector<const SVFType*> fields;
441
442public:
443 SVFStructType(u32_t i, std::vector<const SVFType *> &f, u32_t byteSize = 1) :
445 {
446 }
447
448 static inline bool classof(const SVFType* node)
449 {
450 return node->getKind() == SVFStructTy;
451 }
452
453 void print(std::ostream& os) const override;
454
455 const std::string& getName()
456 {
457 return name;
458 }
459
460 void setName(const std::string& structName)
461 {
463 }
464 void setName(std::string&& structName)
465 {
466 name = std::move(structName);
467 }
468
469 const std::vector<const SVFType*>& getFieldTypes() const
470 {
471 return fields;
472 }
473
474};
475
476class SVFArrayType : public SVFType
477{
478 friend class GraphDBClient;
479
480protected:
481 const unsigned getNumOfElement() const
482 {
483 return numOfElement;
484 }
485
486private:
487 unsigned numOfElement;
489
490public:
495
496 static inline bool classof(const SVFType* node)
497 {
498 return node->getKind() == SVFArrayTy;
499 }
500
501 void print(std::ostream& os) const override;
502
504 {
505 return typeOfElement;
506 }
507
509 {
511 }
512
513 void setNumOfElement(unsigned elemNum)
514 {
515 numOfElement = elemNum;
516 }
517
518
519};
520
521class SVFOtherType : public SVFType
522{
523 friend class GraphDBClient;
524
525protected:
526 const std::string& getRepr() const
527 {
528 return repr;
529 }
530
531private:
532 std::string repr;
533
534public:
536
537 static inline bool classof(const SVFType* node)
538 {
539 return node->getKind() == SVFOtherTy;
540 }
541
542 const std::string& getRepr()
543 {
544 return repr;
545 }
546
547 void setRepr(std::string&& r)
548 {
549 repr = std::move(r);
550 }
551
552 void setRepr(const std::string& r)
553 {
554 repr = r;
555 }
556
557 void print(std::ostream& os) const override;
558};
559
560// TODO: be explicit that this is a pair of 32-bit unsigneds?
561template <> struct Hash<NodePair>
562{
563 size_t operator()(const NodePair& p) const
564 {
565 // Make sure our assumptions are sound: use u32_t
566 // and u64_t. If NodeID is not actually u32_t or size_t
567 // is not u64_t we should be fine since we get a
568 // consistent result.
569 uint32_t first = (uint32_t)(p.first);
570 uint32_t second = (uint32_t)(p.second);
571 return ((uint64_t)(first) << 32) | (uint64_t)(second);
572 }
573};
574
575#if !defined NDBUG && defined USE_SVF_DBOUT
576// TODO: This comes from the following link
577// https://github.com/llvm/llvm-project/blob/75e33f71c2dae584b13a7d1186ae0a038ba98838/llvm/include/llvm/Support/Debug.h#L64
578// The original LLVM implementation makes use of type. But we can get that info,
579// so we can't simulate the full behaviour for now.
580# define SVF_DEBUG_WITH_TYPE(TYPE, X) \
581 do \
582 { \
583 X; \
584 } while (false)
585#else
586# define SVF_DEBUG_WITH_TYPE(TYPE, X) \
587 do \
588 { \
589 } while (false)
590#endif
591
593#define DBOUT(TYPE, X) SVF_DEBUG_WITH_TYPE(TYPE, X)
594#define DOSTAT(X) X
595#define DOTIMESTAT(X) X
596
599#define DGENERAL "general"
600
601#define DPAGBuild "pag"
602#define DMemModel "mm"
603#define DMemModelCE "mmce"
604#define DCOMModel "comm"
605#define DDDA "dda"
606#define DDumpPT "dumppt"
607#define DRefinePT "sbpt"
608#define DCache "cache"
609#define DWPA "wpa"
610#define DMSSA "mssa"
611#define DInstrument "ins"
612#define DAndersen "ander"
613#define DSaber "saber"
614#define DMTA "mta"
615#define DCHA "cha"
616
617/*
618 * Number of clock ticks per second. A clock tick is the unit by which
619 * processor time is measured and is returned by 'clock'.
620 */
621#define TIMEINTERVAL 1000
622#define CLOCK_IN_MS() (clock() / (CLOCKS_PER_SEC / TIMEINTERVAL))
623
625#define NATIVE_INT_SIZE (sizeof(unsigned long long) * CHAR_BIT)
626
634
642
643} // End namespace SVF
644
645template <> struct std::hash<SVF::NodePair>
646{
647 size_t operator()(const SVF::NodePair& p) const
648 {
649 // Make sure our assumptions are sound: use u32_t
650 // and u64_t. If NodeID is not actually u32_t or size_t
651 // is not u64_t we should be fine since we get a
652 // consistent result.
653 uint32_t first = (uint32_t)(p.first);
654 uint32_t second = (uint32_t)(p.second);
655 return ((uint64_t)(first) << 32) | (uint64_t)(second);
656 }
657};
658
660template <unsigned N> struct std::hash<SVF::SparseBitVector<N>>
661{
662 size_t operator()(const SVF::SparseBitVector<N>& sbv) const
663 {
665 return h(std::make_pair(std::make_pair(sbv.count(), sbv.find_first()),
666 sbv.find_last()));
667 }
668};
669
670template <typename T> struct std::hash<std::vector<T>>
671{
672 size_t operator()(const std::vector<T>& v) const
673 {
674 // TODO: repetition with CBV.
675 size_t h = v.size();
676
677 SVF::Hash<T> hf;
678 for (const T& t : v)
679 {
680 h ^= hf(t) + 0x9e3779b9 + (h << 6) + (h >> 2);
681 }
682
683 return h;
684 }
685};
686
687#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
#define true
Definition cJSON.cpp:65
const unsigned getNumOfElement() const
Definition SVFType.h:481
const SVFType * getTypeOfElement() const
Definition SVFType.h:503
const SVFType * typeOfElement
For printing & debugging.
Definition SVFType.h:488
unsigned numOfElement
Definition SVFType.h:487
void setTypeOfElement(const SVFType *elemType)
Definition SVFType.h:508
void setNumOfElement(unsigned elemNum)
Definition SVFType.h:513
static bool classof(const SVFType *node)
Definition SVFType.h:496
friend class GraphDBClient
Definition SVFType.h:478
void print(std::ostream &os) const override
Definition SVFType.cpp:82
SVFArrayType(u32_t i, u32_t byteSize=1)
For printing & debugging.
Definition SVFType.h:491
const SVFType * getReturnType() const
Definition SVFType.h:403
const SVFType * retTy
Definition SVFType.h:375
static bool classof(const SVFType *node)
Definition SVFType.h:399
SVFFunctionType(u32_t i, const SVFType *rt, const std::vector< const SVFType * > &p, bool isvararg)
Definition SVFType.h:394
bool isVarArg() const
Definition SVFType.h:413
const std::vector< const SVFType * > & getParamTypes() const
Definition SVFType.h:408
std::vector< const SVFType * > params
Definition SVFType.h:376
const void setReturnType(const SVFType *rt)
Definition SVFType.h:383
friend class GraphDBClient
Definition SVFType.h:373
void addParamType(const SVFType *type)
Definition SVFType.h:388
void print(std::ostream &os) const override
Definition SVFType.cpp:37
bool isSigned() const
Definition SVFType.h:364
short signAndWidth
For printing.
Definition SVFType.h:341
short getSignAndWidth() const
Definition SVFType.h:345
void print(std::ostream &os) const override
Definition SVFType.cpp:29
friend class GraphDBClient
Definition SVFType.h:338
SVFIntegerType(u32_t i, u32_t byteSize=1)
Definition SVFType.h:351
void setSignAndWidth(short sw)
Definition SVFType.h:359
static bool classof(const SVFType *node)
Definition SVFType.h:352
const std::string & getRepr()
Definition SVFType.h:542
void print(std::ostream &os) const override
Definition SVFType.cpp:87
void setRepr(std::string &&r)
Definition SVFType.h:547
static bool classof(const SVFType *node)
Definition SVFType.h:537
void setRepr(const std::string &r)
Definition SVFType.h:552
friend class GraphDBClient
Definition SVFType.h:523
const std::string & getRepr() const
Definition SVFType.h:526
SVFOtherType(u32_t i, bool isSingleValueTy, u32_t byteSize=1)
Field representation for printing.
Definition SVFType.h:535
std::string repr
Definition SVFType.h:532
static bool classof(const SVFType *node)
Definition SVFType.h:328
SVFPointerType(u32_t i, u32_t byteSize=1)
Definition SVFType.h:323
friend class GraphDBClient
Definition SVFType.h:320
void print(std::ostream &os) const override
Definition SVFType.cpp:24
void addFieldsType(const SVFType *type)
Definition SVFType.h:432
std::string name
Field for printing & debugging.
Definition SVFType.h:439
const std::string & getName() const
Definition SVFType.h:427
static bool classof(const SVFType *node)
Definition SVFType.h:448
void setName(const std::string &structName)
Definition SVFType.h:460
std::vector< const SVFType * > fields
Definition SVFType.h:440
const std::vector< const SVFType * > & getFieldTypes() const
Definition SVFType.h:469
friend class GraphDBClient
Definition SVFType.h:423
void print(std::ostream &os) const override
Definition SVFType.cpp:65
void setName(std::string &&structName)
Definition SVFType.h:464
const std::string & getName()
Definition SVFType.h:455
SVFStructType(u32_t i, std::vector< const SVFType * > &f, u32_t byteSize=1)
Definition SVFType.h:443
virtual ~SVFType()
Definition SVFType.h:251
const StInfo * getTypeInfo() const
Definition SVFType.h:281
static void setSVFInt8Type(SVFType *i8Ty)
Definition SVFType.h:210
u32_t getId() const
Definition SVFType.h:265
u32_t id
array
Definition SVFType.h:240
bool isArrayTy() const
Definition SVFType.h:299
StInfo * getTypeInfo()
Definition SVFType.h:275
GNodeK kind
used for classof
Definition SVFType.h:236
GNodeK getKind() const
Definition SVFType.h:253
s64_t GNodeK
Definition SVFType.h:189
u32_t byteSize
LLVM Byte Size.
Definition SVFType.h:239
static SVFType * getSVFPtrType()
Definition SVFType.h:218
bool isSingleValueType() const
Definition SVFType.h:309
SVFType(bool svt, SVFTyKind k, u32_t i=0, u32_t Sz=1)
Definition SVFType.h:244
static SVFType * svfPtrTy
ptr type
Definition SVFType.h:232
static void setSVFPtrType(SVFType *ptrTy)
set svfptrty and svfi8ty when initializing SVFType from db query results
Definition SVFType.h:205
StInfo * typeinfo
SVF's TypeInfo.
Definition SVFType.h:237
bool isPointerTy() const
Definition SVFType.h:294
std::string toString() const
u32_t getByteSize() const
Definition SVFType.h:289
SVFType(void)=delete
static SVFType * svfI8Ty
8-bit int type
Definition SVFType.h:233
bool isSingleValTy
The type represents a single value, not struct or.
Definition SVFType.h:238
static SVFType * getSVFInt8Type()
Definition SVFType.h:224
void setTypeInfo(StInfo *ti)
Definition SVFType.h:270
virtual void print(std::ostream &os) const =0
bool isStructTy() const
Definition SVFType.h:304
unsigned count() const
void addFldWithType(u32_t fldIdx, const SVFType *type, u32_t elemIdx)
Add field index and element index and their corresponding type.
void operator=(const StInfo &)=delete
const SVFType * getOriginalElemType(u32_t fldIdx) const
Definition SVFValue.cpp:68
std::vector< const SVFType * > & getFlattenElementTypes()
Definition SVFType.h:129
const std::vector< u32_t > & getFlattenedElemIdxVec() const
Definition SVFType.h:141
void setNumOfFieldsAndElems(u32_t nf, u32_t ne)
Set number of fields and elements of an aggregate.
Definition SVFType.h:159
std::vector< u32_t > & getFlattenedElemIdxVec()
Definition SVFType.h:125
StInfo()=delete
Max field limit.
const Map< u32_t, const SVFType * > & getFldIdx2TypeMap() const
Definition SVFType.h:66
u32_t getNumOfFlattenElements() const
Return number of elements after flattening (including array elements)
Definition SVFType.h:166
std::vector< const SVFType * > & getFlattenFieldTypes()
Definition SVFType.h:133
~StInfo()=default
Destructor.
u32_t stride
Definition SVFType.h:89
std::vector< u32_t > fldIdxVec
flattened field indices of a struct (ignoring arrays)
Definition SVFType.h:79
u32_t numOfFlattenElements
number of elements after flattening (including array elements)
Definition SVFType.h:91
std::vector< u32_t > elemIdxVec
Definition SVFType.h:82
StInfo(u32_t s)
Constructor.
Definition SVFType.h:104
u32_t StInfoId
Definition SVFType.h:77
StInfo(const StInfo &st)=delete
const u32_t getStinfoId() const
Definition SVFType.h:61
u32_t numOfFlattenFields
number of fields after flattening (ignoring array elements)
Definition SVFType.h:93
const std::vector< const SVFType * > & getFlattenFieldTypes() const
Definition SVFType.h:149
friend class GraphDBClient
Definition SVFType.h:49
std::vector< const SVFType * > flattenElementTypes
Type vector of fields.
Definition SVFType.h:95
StInfo(u32_t id, std::vector< u32_t > fldIdxVec, std::vector< u32_t > elemIdxVec, Map< u32_t, const SVFType * > fldIdx2TypeMap, std::vector< const SVFType * > finfo, u32_t stride, u32_t numOfFlattenElements, u32_t numOfFlattenFields, std::vector< const SVFType * > flattenElementTypes)
Definition SVFType.h:53
void setStinfoId(u32_t id)
Definition SVFType.h:71
const std::vector< const SVFType * > & getFlattenElementTypes() const
Definition SVFType.h:145
std::vector< u32_t > & getFlattenedFieldIdxVec()
Definition SVFType.h:121
const std::vector< u32_t > & getFlattenedFieldIdxVec() const
Definition SVFType.h:137
Map< u32_t, const SVFType * > fldIdx2TypeMap
Types of all fields of a struct.
Definition SVFType.h:84
std::vector< const SVFType * > finfo
All field infos after flattening a struct.
Definition SVFType.h:86
u32_t getNumOfFlattenFields() const
Return the number of fields after flattening (ignoring array elements)
Definition SVFType.h:172
u32_t getStride() const
Return the stride.
Definition SVFType.h:177
for isBitcode
Definition BasicTypes.h:68
ModRefInfo
Definition SVFType.h:628
@ Ref
Definition SVFType.h:630
@ NoModRef
Definition SVFType.h:632
@ ModRef
Definition SVFType.h:629
@ Mod
Definition SVFType.h:631
AliasResult
Definition SVFType.h:636
@ PartialAlias
Definition SVFType.h:640
@ MustAlias
Definition SVFType.h:639
@ MayAlias
Definition SVFType.h:638
@ NoAlias
Definition SVFType.h:637
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:47
signed long long s64_t
Definition GeneralType.h:50
std::pair< NodeID, NodeID > NodePair
IntervalValue operator<<(const IntervalValue &lhs, const IntervalValue &rhs)
Left binary shift of IntervalValues.
size_t operator()(const NodePair &p) const
Definition SVFType.h:563
provide extra hash function for std::pair handling
Definition GeneralType.h:85
size_t operator()(const SVF::NodePair &p) const
Definition SVFType.h:647
size_t operator()(const SVF::SparseBitVector< N > &sbv) const
Definition SVFType.h:662
size_t operator()(const std::vector< T > &v) const
Definition SVFType.h:672