Static Value-Flow Analysis
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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:
201
202protected:
204 : kind(k), typeinfo(nullptr),
206 {
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 inline void setTypeInfo(StInfo* ti)
226 {
227 typeinfo = ti;
228 }
229
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
270std::ostream& operator<<(std::ostream& os, const SVFType& type);
271
273{
274 friend class SVFIRWriter;
275 friend class SVFIRReader;
276
277public:
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
292{
293 friend class SVFIRWriter;
294 friend class SVFIRReader;
295
296private:
298
299public:
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 {
311 }
312
313 bool isSigned() const
314 {
315 return signAndWidth < 0;
316 }
317};
318
320{
321 friend class SVFIRWriter;
322 friend class SVFIRReader;
323
324private:
326 std::vector<const SVFType*> params;
327 bool varArg;
328
329public:
330 SVFFunctionType(const SVFType* rt, const std::vector<const SVFType*>& p, bool isvararg)
331 : SVFType(false, SVFFunctionTy, 1), retTy(rt), params(p), varArg(isvararg)
332 {
333 }
334
335 static inline bool classof(const SVFType* node)
336 {
337 return node->getKind() == SVFFunctionTy;
338 }
339 const SVFType* getReturnType() const
340 {
341 return retTy;
342 }
343
344 const std::vector<const SVFType*>& getParamTypes() const
345 {
346 return params;
347 }
348
349
350 bool isVarArg() const
351 {
352 return varArg;
353 }
354
355 void print(std::ostream& os) const override;
356};
357
358class SVFStructType : public SVFType
359{
360 friend class SVFIRWriter;
361 friend class SVFIRReader;
362
363private:
365 std::string name;
366
367public:
369
370 static inline bool classof(const SVFType* node)
371 {
372 return node->getKind() == SVFStructTy;
373 }
374
375 void print(std::ostream& os) const override;
376
377 const std::string& getName()
378 {
379 return name;
380 }
381 void setName(const std::string& structName)
382 {
384 }
385 void setName(std::string&& structName)
386 {
387 name = std::move(structName);
388 }
389};
390
391class SVFArrayType : public SVFType
392{
393 friend class SVFIRWriter;
394 friend class SVFIRReader;
395
396private:
397 unsigned numOfElement;
399
400public:
405
406 static inline bool classof(const SVFType* node)
407 {
408 return node->getKind() == SVFArrayTy;
409 }
410
411 void print(std::ostream& os) const override;
412
414 {
415 return typeOfElement;
416 }
417
419 {
421 }
422
423 void setNumOfElement(unsigned elemNum)
424 {
425 numOfElement = elemNum;
426 }
427
428
429};
430
431class SVFOtherType : public SVFType
432{
433 friend class SVFIRWriter;
434 friend class SVFIRReader;
435
436private:
437 std::string repr;
438
439public:
441
442 static inline bool classof(const SVFType* node)
443 {
444 return node->getKind() == SVFOtherTy;
445 }
446
447 const std::string& getRepr()
448 {
449 return repr;
450 }
451
452 void setRepr(std::string&& r)
453 {
454 repr = std::move(r);
455 }
456
457 void setRepr(const std::string& r)
458 {
459 repr = r;
460 }
461
462 void print(std::ostream& os) const override;
463};
464
465// TODO: be explicit that this is a pair of 32-bit unsigneds?
466template <> struct Hash<NodePair>
467{
468 size_t operator()(const NodePair& p) const
469 {
470 // Make sure our assumptions are sound: use u32_t
471 // and u64_t. If NodeID is not actually u32_t or size_t
472 // is not u64_t we should be fine since we get a
473 // consistent result.
474 uint32_t first = (uint32_t)(p.first);
475 uint32_t second = (uint32_t)(p.second);
476 return ((uint64_t)(first) << 32) | (uint64_t)(second);
477 }
478};
479
480#if !defined NDBUG && defined USE_SVF_DBOUT
481// TODO: This comes from the following link
482// https://github.com/llvm/llvm-project/blob/75e33f71c2dae584b13a7d1186ae0a038ba98838/llvm/include/llvm/Support/Debug.h#L64
483// The original LLVM implementation makes use of type. But we can get that info,
484// so we can't simulate the full behaviour for now.
485# define SVF_DEBUG_WITH_TYPE(TYPE, X) \
486 do \
487 { \
488 X; \
489 } while (false)
490#else
491# define SVF_DEBUG_WITH_TYPE(TYPE, X) \
492 do \
493 { \
494 } while (false)
495#endif
496
498#define DBOUT(TYPE, X) SVF_DEBUG_WITH_TYPE(TYPE, X)
499#define DOSTAT(X) X
500#define DOTIMESTAT(X) X
501
504#define DGENERAL "general"
505
506#define DPAGBuild "pag"
507#define DMemModel "mm"
508#define DMemModelCE "mmce"
509#define DCOMModel "comm"
510#define DDDA "dda"
511#define DDumpPT "dumppt"
512#define DRefinePT "sbpt"
513#define DCache "cache"
514#define DWPA "wpa"
515#define DMSSA "mssa"
516#define DInstrument "ins"
517#define DAndersen "ander"
518#define DSaber "saber"
519#define DMTA "mta"
520#define DCHA "cha"
521
522/*
523 * Number of clock ticks per second. A clock tick is the unit by which
524 * processor time is measured and is returned by 'clock'.
525 */
526#define TIMEINTERVAL 1000
527#define CLOCK_IN_MS() (clock() / (CLOCKS_PER_SEC / TIMEINTERVAL))
528
530#define NATIVE_INT_SIZE (sizeof(unsigned long long) * CHAR_BIT)
531
539
547
548} // End namespace SVF
549
550template <> struct std::hash<SVF::NodePair>
551{
552 size_t operator()(const SVF::NodePair& p) const
553 {
554 // Make sure our assumptions are sound: use u32_t
555 // and u64_t. If NodeID is not actually u32_t or size_t
556 // is not u64_t we should be fine since we get a
557 // consistent result.
558 uint32_t first = (uint32_t)(p.first);
559 uint32_t second = (uint32_t)(p.second);
560 return ((uint64_t)(first) << 32) | (uint64_t)(second);
561 }
562};
563
565template <unsigned N> struct std::hash<SVF::SparseBitVector<N>>
566{
567 size_t operator()(const SVF::SparseBitVector<N>& sbv) const
568 {
570 return h(std::make_pair(std::make_pair(sbv.count(), sbv.find_first()),
571 sbv.find_last()));
572 }
573};
574
575template <typename T> struct std::hash<std::vector<T>>
576{
577 size_t operator()(const std::vector<T>& v) const
578 {
579 // TODO: repetition with CBV.
580 size_t h = v.size();
581
582 SVF::Hash<T> hf;
583 for (const T& t : v)
584 {
585 h ^= hf(t) + 0x9e3779b9 + (h << 6) + (h >> 2);
586 }
587
588 return h;
589 }
590};
591
592#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:413
const SVFType * typeOfElement
For printing & debugging.
Definition SVFType.h:398
SVFArrayType(u32_t byteSize=1)
For printing & debugging.
Definition SVFType.h:401
unsigned numOfElement
Definition SVFType.h:397
void setTypeOfElement(const SVFType *elemType)
Definition SVFType.h:418
friend class SVFIRReader
Definition SVFType.h:394
void setNumOfElement(unsigned elemNum)
Definition SVFType.h:423
static bool classof(const SVFType *node)
Definition SVFType.h:406
void print(std::ostream &os) const override
Definition SVFType.cpp:47
friend class SVFIRWriter
Definition SVFType.h:393
const SVFType * getReturnType() const
Definition SVFType.h:339
const SVFType * retTy
Definition SVFType.h:325
SVFFunctionType(const SVFType *rt, const std::vector< const SVFType * > &p, bool isvararg)
Definition SVFType.h:330
static bool classof(const SVFType *node)
Definition SVFType.h:335
bool isVarArg() const
Definition SVFType.h:350
const std::vector< const SVFType * > & getParamTypes() const
Definition SVFType.h:344
friend class SVFIRReader
Definition SVFType.h:322
std::vector< const SVFType * > params
Definition SVFType.h:326
friend class SVFIRWriter
Definition SVFType.h:321
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
friend class SVFIRReader
Definition SVFType.h:294
void print(std::ostream &os) const override
Definition SVFType.cpp:29
friend class SVFIRWriter
Definition SVFType.h:293
void setSignAndWidth(short sw)
Definition SVFType.h:308
static bool classof(const SVFType *node)
Definition SVFType.h:301
const std::string & getRepr()
Definition SVFType.h:447
void print(std::ostream &os) const override
Definition SVFType.cpp:52
void setRepr(std::string &&r)
Definition SVFType.h:452
static bool classof(const SVFType *node)
Definition SVFType.h:442
void setRepr(const std::string &r)
Definition SVFType.h:457
SVFOtherType(bool isSingleValueTy, u32_t byteSize=1)
Field representation for printing.
Definition SVFType.h:440
friend class SVFIRReader
Definition SVFType.h:434
friend class SVFIRWriter
Definition SVFType.h:433
std::string repr
Definition SVFType.h:437
static bool classof(const SVFType *node)
Definition SVFType.h:283
SVFPointerType(u32_t byteSize=1)
Definition SVFType.h:278
friend class SVFIRReader
Definition SVFType.h:275
friend class SVFIRWriter
Definition SVFType.h:274
void print(std::ostream &os) const override
Definition SVFType.cpp:24
std::string name
Field for printing & debugging.
Definition SVFType.h:365
static bool classof(const SVFType *node)
Definition SVFType.h:370
void setName(const std::string &structName)
Definition SVFType.h:381
SVFStructType(u32_t byteSize=1)
Definition SVFType.h:368
friend class SVFIRReader
Definition SVFType.h:361
void print(std::ostream &os) const override
Definition SVFType.cpp:42
friend class SVFIRWriter
Definition SVFType.h:360
void setName(std::string &&structName)
Definition SVFType.h:385
const std::string & getName()
Definition SVFType.h:377
virtual ~SVFType()
Definition SVFType.h:211
const StInfo * getTypeInfo() const
Definition SVFType.h:236
SVFType(bool svt, SVFTyKind k, u32_t Sz=1)
Definition SVFType.h:203
bool isArrayTy() const
Definition SVFType.h:254
StInfo * getTypeInfo()
Definition SVFType.h:230
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
static SVFType * getSVFPtrType()
Definition SVFType.h:178
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
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:225
virtual void print(std::ostream &os) const =0
bool isStructTy() const
Definition SVFType.h:259
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:533
@ Ref
Definition SVFType.h:535
@ NoModRef
Definition SVFType.h:537
@ ModRef
Definition SVFType.h:534
@ Mod
Definition SVFType.h:536
AliasResult
Definition SVFType.h:541
@ PartialAlias
Definition SVFType.h:545
@ MustAlias
Definition SVFType.h:544
@ MayAlias
Definition SVFType.h:543
@ NoAlias
Definition SVFType.h:542
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:468
provide extra hash function for std::pair handling
Definition GeneralType.h:85
size_t operator()(const SVF::NodePair &p) const
Definition SVFType.h:552
size_t operator()(const SVF::SparseBitVector< N > &sbv) const
Definition SVFType.h:567
size_t operator()(const std::vector< T > &v) const
Definition SVFType.h:577