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 friend class SVFIRWriter;
49 friend class SVFIRReader;
50
51private:
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
72public:
73 StInfo() = delete;
74 StInfo(const StInfo& st) = delete;
75 void operator=(const StInfo&) = delete;
76
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
130
137
140 {
142 }
143
146 {
147 return numOfFlattenFields;
148 }
150 inline u32_t getStride() const
151 {
152 return stride;
153 }
154};
155
157{
158 friend class SVFIRWriter;
159 friend class SVFIRReader;
160 friend class LLVMModuleSet;
161
162public:
163 typedef s64_t GNodeK;
164
175
176public:
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
190private:
191
193 static SVFType* svfI8Ty;
194
195private:
202
203protected:
204 SVFType(bool svt, SVFTyKind k, u32_t i = 0, u32_t Sz = 1)
205 : kind(k), typeinfo(nullptr),
207 {
208 }
209public:
210 SVFType(void) = delete;
211 virtual ~SVFType() {}
212
213 inline GNodeK getKind() const
214 {
215 return kind;
216 }
217
220 std::string toString() const;
221
222 virtual void print(std::ostream& os) const = 0;
223
224
225 u32_t getId() const
226 {
227 return id;
228 }
229
230 inline void setTypeInfo(StInfo* ti)
231 {
232 typeinfo = ti;
233 }
234
236 {
237 assert(typeinfo && "set the type info first");
238 return typeinfo;
239 }
240
241 inline const StInfo* getTypeInfo() const
242 {
243 assert(typeinfo && "set the type info first");
244 return typeinfo;
245 }
246
249 inline u32_t getByteSize() const
250 {
251 return byteSize;
252 }
253
254 inline bool isPointerTy() const
255 {
256 return kind == SVFPointerTy;
257 }
258
259 inline bool isArrayTy() const
260 {
261 return kind == SVFArrayTy;
262 }
263
264 inline bool isStructTy() const
265 {
266 return kind == SVFStructTy;
267 }
268
269 inline bool isSingleValueType() const
270 {
271 return isSingleValTy;
272 }
273};
274
275std::ostream& operator<<(std::ostream& os, const SVFType& type);
276
278{
279 friend class SVFIRWriter;
280 friend class SVFIRReader;
281
282public:
287
288 static inline bool classof(const SVFType* node)
289 {
290 return node->getKind() == SVFPointerTy;
291 }
292
293 void print(std::ostream& os) const override;
294};
295
297{
298 friend class SVFIRWriter;
299 friend class SVFIRReader;
300
301private:
303
304public:
306 static inline bool classof(const SVFType* node)
307 {
308 return node->getKind() == SVFIntegerTy;
309 }
310
311 void print(std::ostream& os) const override;
312
313 void setSignAndWidth(short sw)
314 {
316 }
317
318 bool isSigned() const
319 {
320 return signAndWidth < 0;
321 }
322};
323
325{
326 friend class SVFIRWriter;
327 friend class SVFIRReader;
328
329private:
331 std::vector<const SVFType*> params;
332 bool varArg;
333
334public:
335 SVFFunctionType(u32_t i, const SVFType* rt, const std::vector<const SVFType*>& p, bool isvararg)
336 : SVFType(false, SVFFunctionTy, i, 1), retTy(rt), params(p), varArg(isvararg)
337 {
338 }
339
340 static inline bool classof(const SVFType* node)
341 {
342 return node->getKind() == SVFFunctionTy;
343 }
344 const SVFType* getReturnType() const
345 {
346 return retTy;
347 }
348
349 const std::vector<const SVFType*>& getParamTypes() const
350 {
351 return params;
352 }
353
354
355 bool isVarArg() const
356 {
357 return varArg;
358 }
359
360 void print(std::ostream& os) const override;
361};
362
363class SVFStructType : public SVFType
364{
365 friend class SVFIRWriter;
366 friend class SVFIRReader;
367
368private:
370 std::string name;
371 std::vector<const SVFType*> fields;
372
373public:
374 SVFStructType(u32_t i, std::vector<const SVFType *> &f, u32_t byteSize = 1) :
376 {
377 }
378
379 static inline bool classof(const SVFType* node)
380 {
381 return node->getKind() == SVFStructTy;
382 }
383
384 void print(std::ostream& os) const override;
385
386 const std::string& getName()
387 {
388 return name;
389 }
390 void setName(const std::string& structName)
391 {
393 }
394 void setName(std::string&& structName)
395 {
396 name = std::move(structName);
397 }
398
399 const std::vector<const SVFType*>& getFieldTypes() const
400 {
401 return fields;
402 }
403};
404
405class SVFArrayType : public SVFType
406{
407 friend class SVFIRWriter;
408 friend class SVFIRReader;
409
410private:
411 unsigned numOfElement;
413
414public:
419
420 static inline bool classof(const SVFType* node)
421 {
422 return node->getKind() == SVFArrayTy;
423 }
424
425 void print(std::ostream& os) const override;
426
428 {
429 return typeOfElement;
430 }
431
433 {
435 }
436
437 void setNumOfElement(unsigned elemNum)
438 {
439 numOfElement = elemNum;
440 }
441
442
443};
444
445class SVFOtherType : public SVFType
446{
447 friend class SVFIRWriter;
448 friend class SVFIRReader;
449
450private:
451 std::string repr;
452
453public:
455
456 static inline bool classof(const SVFType* node)
457 {
458 return node->getKind() == SVFOtherTy;
459 }
460
461 const std::string& getRepr()
462 {
463 return repr;
464 }
465
466 void setRepr(std::string&& r)
467 {
468 repr = std::move(r);
469 }
470
471 void setRepr(const std::string& r)
472 {
473 repr = r;
474 }
475
476 void print(std::ostream& os) const override;
477};
478
479// TODO: be explicit that this is a pair of 32-bit unsigneds?
480template <> struct Hash<NodePair>
481{
482 size_t operator()(const NodePair& p) const
483 {
484 // Make sure our assumptions are sound: use u32_t
485 // and u64_t. If NodeID is not actually u32_t or size_t
486 // is not u64_t we should be fine since we get a
487 // consistent result.
488 uint32_t first = (uint32_t)(p.first);
489 uint32_t second = (uint32_t)(p.second);
490 return ((uint64_t)(first) << 32) | (uint64_t)(second);
491 }
492};
493
494#if !defined NDBUG && defined USE_SVF_DBOUT
495// TODO: This comes from the following link
496// https://github.com/llvm/llvm-project/blob/75e33f71c2dae584b13a7d1186ae0a038ba98838/llvm/include/llvm/Support/Debug.h#L64
497// The original LLVM implementation makes use of type. But we can get that info,
498// so we can't simulate the full behaviour for now.
499# define SVF_DEBUG_WITH_TYPE(TYPE, X) \
500 do \
501 { \
502 X; \
503 } while (false)
504#else
505# define SVF_DEBUG_WITH_TYPE(TYPE, X) \
506 do \
507 { \
508 } while (false)
509#endif
510
512#define DBOUT(TYPE, X) SVF_DEBUG_WITH_TYPE(TYPE, X)
513#define DOSTAT(X) X
514#define DOTIMESTAT(X) X
515
518#define DGENERAL "general"
519
520#define DPAGBuild "pag"
521#define DMemModel "mm"
522#define DMemModelCE "mmce"
523#define DCOMModel "comm"
524#define DDDA "dda"
525#define DDumpPT "dumppt"
526#define DRefinePT "sbpt"
527#define DCache "cache"
528#define DWPA "wpa"
529#define DMSSA "mssa"
530#define DInstrument "ins"
531#define DAndersen "ander"
532#define DSaber "saber"
533#define DMTA "mta"
534#define DCHA "cha"
535
536/*
537 * Number of clock ticks per second. A clock tick is the unit by which
538 * processor time is measured and is returned by 'clock'.
539 */
540#define TIMEINTERVAL 1000
541#define CLOCK_IN_MS() (clock() / (CLOCKS_PER_SEC / TIMEINTERVAL))
542
544#define NATIVE_INT_SIZE (sizeof(unsigned long long) * CHAR_BIT)
545
553
561
562} // End namespace SVF
563
564template <> struct std::hash<SVF::NodePair>
565{
566 size_t operator()(const SVF::NodePair& p) const
567 {
568 // Make sure our assumptions are sound: use u32_t
569 // and u64_t. If NodeID is not actually u32_t or size_t
570 // is not u64_t we should be fine since we get a
571 // consistent result.
572 uint32_t first = (uint32_t)(p.first);
573 uint32_t second = (uint32_t)(p.second);
574 return ((uint64_t)(first) << 32) | (uint64_t)(second);
575 }
576};
577
579template <unsigned N> struct std::hash<SVF::SparseBitVector<N>>
580{
581 size_t operator()(const SVF::SparseBitVector<N>& sbv) const
582 {
584 return h(std::make_pair(std::make_pair(sbv.count(), sbv.find_first()),
585 sbv.find_last()));
586 }
587};
588
589template <typename T> struct std::hash<std::vector<T>>
590{
591 size_t operator()(const std::vector<T>& v) const
592 {
593 // TODO: repetition with CBV.
594 size_t h = v.size();
595
596 SVF::Hash<T> hf;
597 for (const T& t : v)
598 {
599 h ^= hf(t) + 0x9e3779b9 + (h << 6) + (h >> 2);
600 }
601
602 return h;
603 }
604};
605
606#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 SVFType * getTypeOfElement() const
Definition SVFType.h:427
const SVFType * typeOfElement
For printing & debugging.
Definition SVFType.h:412
unsigned numOfElement
Definition SVFType.h:411
void setTypeOfElement(const SVFType *elemType)
Definition SVFType.h:432
friend class SVFIRReader
Definition SVFType.h:408
void setNumOfElement(unsigned elemNum)
Definition SVFType.h:437
static bool classof(const SVFType *node)
Definition SVFType.h:420
void print(std::ostream &os) const override
Definition SVFType.cpp:82
friend class SVFIRWriter
Definition SVFType.h:407
SVFArrayType(u32_t i, u32_t byteSize=1)
For printing & debugging.
Definition SVFType.h:415
const SVFType * getReturnType() const
Definition SVFType.h:344
const SVFType * retTy
Definition SVFType.h:330
static bool classof(const SVFType *node)
Definition SVFType.h:340
SVFFunctionType(u32_t i, const SVFType *rt, const std::vector< const SVFType * > &p, bool isvararg)
Definition SVFType.h:335
bool isVarArg() const
Definition SVFType.h:355
const std::vector< const SVFType * > & getParamTypes() const
Definition SVFType.h:349
friend class SVFIRReader
Definition SVFType.h:327
std::vector< const SVFType * > params
Definition SVFType.h:331
friend class SVFIRWriter
Definition SVFType.h:326
void print(std::ostream &os) const override
Definition SVFType.cpp:37
bool isSigned() const
Definition SVFType.h:318
short signAndWidth
For printing.
Definition SVFType.h:302
friend class SVFIRReader
Definition SVFType.h:299
void print(std::ostream &os) const override
Definition SVFType.cpp:29
SVFIntegerType(u32_t i, u32_t byteSize=1)
Definition SVFType.h:305
friend class SVFIRWriter
Definition SVFType.h:298
void setSignAndWidth(short sw)
Definition SVFType.h:313
static bool classof(const SVFType *node)
Definition SVFType.h:306
const std::string & getRepr()
Definition SVFType.h:461
void print(std::ostream &os) const override
Definition SVFType.cpp:87
void setRepr(std::string &&r)
Definition SVFType.h:466
static bool classof(const SVFType *node)
Definition SVFType.h:456
void setRepr(const std::string &r)
Definition SVFType.h:471
friend class SVFIRReader
Definition SVFType.h:448
friend class SVFIRWriter
Definition SVFType.h:447
SVFOtherType(u32_t i, bool isSingleValueTy, u32_t byteSize=1)
Field representation for printing.
Definition SVFType.h:454
std::string repr
Definition SVFType.h:451
static bool classof(const SVFType *node)
Definition SVFType.h:288
friend class SVFIRReader
Definition SVFType.h:280
SVFPointerType(u32_t i, u32_t byteSize=1)
Definition SVFType.h:283
friend class SVFIRWriter
Definition SVFType.h:279
void print(std::ostream &os) const override
Definition SVFType.cpp:24
std::string name
Field for printing & debugging.
Definition SVFType.h:370
static bool classof(const SVFType *node)
Definition SVFType.h:379
void setName(const std::string &structName)
Definition SVFType.h:390
std::vector< const SVFType * > fields
Definition SVFType.h:371
friend class SVFIRReader
Definition SVFType.h:366
const std::vector< const SVFType * > & getFieldTypes() const
Definition SVFType.h:399
void print(std::ostream &os) const override
Definition SVFType.cpp:65
friend class SVFIRWriter
Definition SVFType.h:365
void setName(std::string &&structName)
Definition SVFType.h:394
const std::string & getName()
Definition SVFType.h:386
SVFStructType(u32_t i, std::vector< const SVFType * > &f, u32_t byteSize=1)
Definition SVFType.h:374
virtual ~SVFType()
Definition SVFType.h:211
const StInfo * getTypeInfo() const
Definition SVFType.h:241
u32_t getId() const
Definition SVFType.h:225
u32_t id
array
Definition SVFType.h:200
bool isArrayTy() const
Definition SVFType.h:259
StInfo * getTypeInfo()
Definition SVFType.h:235
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
LLVM Byte Size.
Definition SVFType.h:199
static SVFType * getSVFPtrType()
Definition SVFType.h:178
bool isSingleValueType() const
Definition SVFType.h:269
SVFType(bool svt, SVFTyKind k, u32_t i=0, u32_t Sz=1)
Definition SVFType.h:204
static SVFType * svfPtrTy
ptr type
Definition SVFType.h:192
StInfo * typeinfo
SVF's TypeInfo.
Definition SVFType.h:197
bool isPointerTy() const
Definition SVFType.h:254
std::string toString() const
u32_t getByteSize() const
Definition SVFType.h:249
friend class SVFIRReader
Definition SVFType.h:159
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
friend class SVFIRWriter
Definition SVFType.h:158
static SVFType * getSVFInt8Type()
Definition SVFType.h:184
void setTypeInfo(StInfo *ti)
Definition SVFType.h:230
virtual void print(std::ostream &os) const =0
bool isStructTy() const
Definition SVFType.h:264
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:31
std::vector< const SVFType * > & getFlattenElementTypes()
Definition SVFType.h:102
const std::vector< u32_t > & getFlattenedElemIdxVec() const
Definition SVFType.h:114
void setNumOfFieldsAndElems(u32_t nf, u32_t ne)
Set number of fields and elements of an aggregate.
Definition SVFType.h:132
std::vector< u32_t > & getFlattenedElemIdxVec()
Definition SVFType.h:98
StInfo()=delete
Max field limit.
u32_t getNumOfFlattenElements() const
Return number of elements after flattening (including array elements)
Definition SVFType.h:139
std::vector< const SVFType * > & getFlattenFieldTypes()
Definition SVFType.h:106
~StInfo()=default
Destructor.
u32_t stride
Definition SVFType.h:63
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
friend class SVFIRReader
Definition SVFType.h:49
StInfo(const StInfo &st)=delete
u32_t numOfFlattenFields
number of fields after flattening (ignoring array elements)
Definition SVFType.h:67
const std::vector< const SVFType * > & getFlattenFieldTypes() const
Definition SVFType.h:122
std::vector< const SVFType * > flattenElementTypes
Type vector of fields.
Definition SVFType.h:69
friend class SVFIRWriter
Definition SVFType.h:48
const std::vector< const SVFType * > & getFlattenElementTypes() const
Definition SVFType.h:118
std::vector< u32_t > & getFlattenedFieldIdxVec()
Definition SVFType.h:94
const std::vector< u32_t > & getFlattenedFieldIdxVec() const
Definition SVFType.h:110
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
for isBitcode
Definition BasicTypes.h:68
ModRefInfo
Definition SVFType.h:547
@ Ref
Definition SVFType.h:549
@ NoModRef
Definition SVFType.h:551
@ ModRef
Definition SVFType.h:548
@ Mod
Definition SVFType.h:550
AliasResult
Definition SVFType.h:555
@ PartialAlias
Definition SVFType.h:559
@ MustAlias
Definition SVFType.h:558
@ MayAlias
Definition SVFType.h:557
@ NoAlias
Definition SVFType.h:556
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:482
provide extra hash function for std::pair handling
Definition GeneralType.h:85
size_t operator()(const SVF::NodePair &p) const
Definition SVFType.h:566
size_t operator()(const SVF::SparseBitVector< N > &sbv) const
Definition SVFType.h:581
size_t operator()(const std::vector< T > &v) const
Definition SVFType.h:591