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