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
129 void addFldWithType(u32_t fldIdx, const SVFType* type, u32_t elemIdx);
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
327public:
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
344class SVFStructType : public SVFType
345{
346 friend class SVFIRWriter;
347 friend class SVFIRReader;
348
349private:
351 std::string name;
352
353public:
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
363 const std::string& getName()
364 {
365 return name;
366 }
367 void setName(const std::string& structName)
368 {
370 }
371 void setName(std::string&& structName)
372 {
373 name = std::move(structName);
374 }
375};
376
377class SVFArrayType : public SVFType
378{
379 friend class SVFIRWriter;
380 friend class SVFIRReader;
381
382private:
383 unsigned numOfElement;
385
386public:
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
400 {
401 return typeOfElement;
402 }
403
405 {
407 }
408
409 void setNumOfElement(unsigned elemNum)
410 {
411 numOfElement = elemNum;
412 }
413
414
415};
416
417class SVFOtherType : public SVFType
418{
419 friend class SVFIRWriter;
420 friend class SVFIRReader;
421
422private:
423 std::string repr;
424
425public:
427
428 static inline bool classof(const SVFType* node)
429 {
430 return node->getKind() == SVFOtherTy;
431 }
432
433 const std::string& getRepr()
434 {
435 return repr;
436 }
437
438 void setRepr(std::string&& r)
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?
452template <> 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
525
533
534} // End namespace SVF
535
536template <> 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
551template <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
561template <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
#define true
Definition cJSON.cpp:65
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 * getReturnType() const
Definition SVFType.h:336
const SVFType * retTy
Definition SVFType.h:325
static bool classof(const SVFType *node)
Definition SVFType.h:332
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
const std::string & getRepr()
Definition SVFType.h:433
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
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
void setName(std::string &&structName)
Definition SVFType.h:371
const std::string & getName()
Definition SVFType.h:363
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
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
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 operator=(const StInfo &)=delete
const SVFType * getOriginalElemType(u32_t fldIdx) const
Definition SVFValue.cpp:20
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
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
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< const SVFType * > flattenElementTypes
Type vector of fields.
Definition SVFType.h:69
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: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
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46
signed long long s64_t
Definition GeneralType.h:49
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: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