SVF
MemModel.h
Go to the documentation of this file.
1 //===- MemModel.h -- Memory model for pointer analysis------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-2017> <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 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 General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * MemModel.h
25  *
26  * Created on: Nov 11, 2013
27  * Author: Yulei Sui
28  */
29 
30 #ifndef OBJECTANDSYMBOL_H_
31 #define OBJECTANDSYMBOL_H_
32 
34 #include "Util/SVFModule.h"
35 
36 namespace SVF
37 {
38 
40 enum SYMTYPE
41 {
50 };
51 
55 class StInfo
56 {
57 
58 private:
60  std::vector<u32_t> fldIdxVec;
62  std::vector<u32_t> foffset;
68  std::vector<FieldInfo> finfo;
69 
72 
73 public:
76  {
77  }
80  {
81  }
82 
83  static inline void setMaxFieldLimit(u32_t limit)
84  {
85  maxFieldLimit = limit;
86  }
87 
88  static inline u32_t getMaxFieldLimit()
89  {
90  return maxFieldLimit;
91  }
92 
94  //{@
95  inline const llvm::Type* getFieldTypeWithFldIdx(u32_t fldIdx)
96  {
97  return fldIdx2TypeMap[fldIdx];
98  }
100  {
101  return offset2TypeMap[offset];
102  }
103  inline std::vector<u32_t>& getFieldIdxVec()
104  {
105  return fldIdxVec;
106  }
107  inline std::vector<u32_t>& getFieldOffsetVec()
108  {
109  return foffset;
110  }
111  inline std::vector<FieldInfo>& getFlattenFieldInfoVec()
112  {
113  return finfo;
114  }
116 
118  inline void addFldWithType(u32_t fldIdx, u32_t offset, const llvm::Type* type)
119  {
120  fldIdxVec.push_back(fldIdx);
121  foffset.push_back(offset);
122  fldIdx2TypeMap[fldIdx] = type;
123  offset2TypeMap[offset] = type;
124  }
125 };
126 
131 {
132 
133 public:
134  typedef enum
135  {
136  FUNCTION_OBJ = 0x1, // object is a function
137  GLOBVAR_OBJ = 0x2, // object is a global variable
138  STATIC_OBJ = 0x4, // object is a static variable allocated before main
139  STACK_OBJ = 0x8, // object is a stack variable
140  HEAP_OBJ = 0x10, // object is a heap variable
141  VAR_STRUCT_OBJ = 0x20, // object contains struct
142  VAR_ARRAY_OBJ = 0x40, // object contains array
143  CONST_STRUCT_OBJ = 0x80, // constant struct
144  CONST_ARRAY_OBJ = 0x100, // constant array
145  CONST_OBJ = 0x200, // constant object str e.g.
146  HASPTR_OBJ = 0x400 // non pointer object including compound type have field that is a pointer type
147  } MEMTYPE;
148 
149 private:
151  const Type* type;
158 public:
159 
161  ObjTypeInfo(const Value*, const Type* t, u32_t max) :
162  type(t), flags(0), maxOffsetLimit(max)
163  {
164  }
166  ObjTypeInfo(u32_t max, const Type* t) : type(t), flags(0), maxOffsetLimit(max)
167  {
168 
169  }
171  virtual ~ObjTypeInfo()
172  {
173 
174  }
176  void init(const Value* value);
177 
180  virtual u32_t getObjSize(const Value* val);
181 
183  void analyzeGlobalStackObjType(const Value* val);
184 
186  void analyzeHeapStaticObjType(const Value* val);
187 
189  inline const Type* getType() const
190  {
191  return type;
192  }
193 
196  {
197  return maxOffsetLimit;
198  }
199 
201  inline void setMaxFieldOffsetLimit(u32_t limit)
202  {
203  maxOffsetLimit = limit;
204  }
205 
207 
208  inline void setFlag(MEMTYPE mask)
209  {
210  flags |= mask;
211  }
212  inline bool hasFlag(MEMTYPE mask)
213  {
214  return (flags & mask) == mask;
215  }
217 
219 
220  inline bool isFunction()
221  {
222  return hasFlag(FUNCTION_OBJ);
223  }
224  inline bool isGlobalObj()
225  {
226  return hasFlag(GLOBVAR_OBJ);
227  }
228  inline bool isStaticObj()
229  {
230  return hasFlag(STATIC_OBJ);
231  }
232  inline bool isStack()
233  {
234  return hasFlag(STACK_OBJ);
235  }
236  inline bool isHeap()
237  {
238  return hasFlag(HEAP_OBJ);
239  }
241 
244 
245  inline bool isVarStruct()
246  {
247  return hasFlag(VAR_STRUCT_OBJ);
248  }
249  inline bool isConstStruct()
250  {
251  return hasFlag(CONST_STRUCT_OBJ);
252  }
253  inline bool isStruct()
254  {
255  return hasFlag(VAR_STRUCT_OBJ) || hasFlag(CONST_STRUCT_OBJ);
256  }
257  inline bool isVarArray()
258  {
259  return hasFlag(VAR_ARRAY_OBJ);
260  }
261  inline bool isConstArray()
262  {
263  return hasFlag(CONST_ARRAY_OBJ);
264  }
265  inline bool isArray()
266  {
267  return hasFlag(VAR_ARRAY_OBJ) || hasFlag(CONST_ARRAY_OBJ);
268  }
269  inline bool isConstant()
270  {
271  return hasFlag(CONST_OBJ);
272  }
273  inline bool hasPtrObj()
274  {
275  return hasFlag(HASPTR_OBJ);
276  }
277  virtual bool isNonPtrFieldObj(const LocationSet& ls);
279 };
280 
284 class MemObj
285 {
286 
287 private:
289  const Value *refVal;
294 
295 public:
296 
298  MemObj(const Value *val, SymID id);
299 
301  MemObj(SymID id, const Type* type = nullptr);
302 
305  {
306  destroy();
307  }
308 
310  void init(const Value *val);
311 
313  void init(const Type* type);
314 
316  const llvm::Type* getType() const;
317 
320  {
321  return typeInfo->getMaxFieldOffsetLimit();
322  }
323 
325  inline const Value *getRefVal() const
326  {
327  return refVal;
328  }
329 
331  inline SymID getSymId() const
332  {
333  return GSymID;
334  }
335 
337  inline bool isFieldInsensitive() const
338  {
339  return getMaxFieldOffsetLimit() == 0;
340  }
341 
343  inline void setFieldInsensitive()
344  {
345  typeInfo->setMaxFieldOffsetLimit(0);
346  }
347 
349  void setFieldSensitive();
350 
352  bool isBlackHoleObj() const;
353 
355 
356  inline bool isFunction() const
357  {
358  return typeInfo->isFunction();
359  }
360  inline bool isGlobalObj() const
361  {
362  return typeInfo->isGlobalObj();
363  }
364  inline bool isStaticObj() const
365  {
366  return typeInfo->isStaticObj();
367  }
368  inline bool isStack() const
369  {
370  return typeInfo->isStack();
371  }
372  inline bool isHeap() const
373  {
374  return typeInfo->isHeap();
375  }
376 
377  inline bool isStruct() const
378  {
379  return typeInfo->isStruct();
380  }
381  inline bool isArray() const
382  {
383  return typeInfo->isArray();
384  }
385  inline bool isVarStruct() const
386  {
387  return typeInfo->isVarStruct();
388  }
389  inline bool isVarArray() const
390  {
391  return typeInfo->isVarArray();
392  }
393  inline bool isConstStruct() const
394  {
395  return typeInfo->isConstStruct();
396  }
397  inline bool isConstArray() const
398  {
399  return typeInfo->isConstArray();
400  }
401  inline bool isConstant() const
402  {
403  return typeInfo->isConstant();
404  }
405  inline bool hasPtrObj() const
406  {
407  return typeInfo->hasPtrObj();
408  }
409  inline bool isNonPtrFieldObj(const LocationSet& ls) const
410  {
411  return typeInfo->isNonPtrFieldObj(ls);
412  }
414 
416  inline bool operator==(const MemObj &mem) const
417  {
418  return refVal == mem.getRefVal();
419  }
420 
422  void destroy();
423 };
424 
425 } // End namespace SVF
426 
427 
428 
429 
430 #endif /* OBJECTANDSYMBOL_H_ */
bool isVarArray() const
Definition: MemModel.h:389
ObjTypeInfo * typeInfo
Type information of this object.
Definition: MemModel.h:293
bool isConstArray()
Definition: MemModel.h:261
bool hasFlag(MEMTYPE mask)
Definition: MemModel.h:212
bool hasPtrObj() const
Definition: MemModel.h:405
std::vector< u32_t > fldIdxVec
flattened field indices of a struct
Definition: MemModel.h:60
void setMaxFieldOffsetLimit(u32_t limit)
Get max field offset limit.
Definition: MemModel.h:201
Map< u32_t, const llvm::Type * > fldIdx2TypeMap
Types of all fields of a struct.
Definition: MemModel.h:64
StInfo()
Constructor.
Definition: MemModel.h:75
llvm::Type Type
Definition: BasicTypes.h:75
bool isFunction() const
object attributes methods
Definition: MemModel.h:356
const llvm::Type * getFieldTypeWithByteOffset(u32_t offset)
Definition: MemModel.h:99
bool isConstStruct()
Definition: MemModel.h:249
bool isFunction()
Object attributes.
Definition: MemModel.h:220
bool isArray() const
Definition: MemModel.h:381
bool isFieldInsensitive() const
Return true if its field limit is 0.
Definition: MemModel.h:337
bool isConstant()
Definition: MemModel.h:269
SYMTYPE
Symbol types.
Definition: MemModel.h:40
bool isVarArray()
Definition: MemModel.h:257
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
unsigned u32_t
Definition: SVFBasicTypes.h:75
ObjTypeInfo(const Value *, const Type *t, u32_t max)
Constructors.
Definition: MemModel.h:161
~StInfo()
Destructor.
Definition: MemModel.h:79
bool isNonPtrFieldObj(const LocationSet &ls) const
Definition: MemModel.h:409
Map< u32_t, const llvm::Type * > offset2TypeMap
Types of all fields of a struct.
Definition: MemModel.h:66
bool isStaticObj()
Definition: MemModel.h:228
u32_t getMaxFieldOffsetLimit()
Get max field offset limit.
Definition: MemModel.h:195
bool isArray()
Definition: MemModel.h:265
std::vector< u32_t > & getFieldIdxVec()
Definition: MemModel.h:103
void addFldWithType(u32_t fldIdx, u32_t offset, const llvm::Type *type)
Add field (index and offset) with its corresponding type.
Definition: MemModel.h:118
static void setMaxFieldLimit(u32_t limit)
Definition: MemModel.h:83
void setFieldInsensitive()
Set the memory object to be field insensitive.
Definition: MemModel.h:343
ObjTypeInfo(u32_t max, const Type *t)
Constructor.
Definition: MemModel.h:166
bool isVarStruct() const
Definition: MemModel.h:385
void setFlag(MEMTYPE mask)
Flag for this object type.
Definition: MemModel.h:208
~MemObj()
Destructor.
Definition: MemModel.h:304
std::vector< u32_t > & getFieldOffsetVec()
Definition: MemModel.h:107
Size_t flags
Type flags.
Definition: MemModel.h:153
const Value * getRefVal() const
Get the reference value to this object.
Definition: MemModel.h:325
std::vector< FieldInfo > & getFlattenFieldInfoVec()
Definition: MemModel.h:111
signed long Size_t
Definition: SVFBasicTypes.h:78
bool isStack()
Definition: MemModel.h:232
unsigned SymID
Definition: SVFBasicTypes.h:82
const Type * getType() const
Get LLVM type.
Definition: MemModel.h:189
bool isStaticObj() const
Definition: MemModel.h:364
static u32_t getMaxFieldLimit()
Definition: MemModel.h:88
bool isVarStruct()
Definition: MemModel.h:245
bool isStruct() const
Definition: MemModel.h:377
std::vector< u32_t > foffset
flattened field offsets of of a struct
Definition: MemModel.h:62
std::vector< FieldInfo > finfo
All field infos after flattening a struct.
Definition: MemModel.h:68
u32_t maxOffsetLimit
Definition: MemModel.h:157
virtual ~ObjTypeInfo()
Destructor.
Definition: MemModel.h:171
bool isGlobalObj() const
Definition: MemModel.h:360
bool isConstant() const
Definition: MemModel.h:401
SymID getSymId() const
Get the memory object id.
Definition: MemModel.h:331
bool hasPtrObj()
Definition: MemModel.h:273
static u32_t maxFieldLimit
Max field limit.
Definition: MemModel.h:71
bool operator==(const MemObj &mem) const
Operator overloading.
Definition: MemModel.h:416
const Type * type
LLVM type.
Definition: MemModel.h:151
bool isStack() const
Definition: MemModel.h:368
for isBitcode
Definition: ContextDDA.h:15
const Value * refVal
The unique pointer value refer this object.
Definition: MemModel.h:289
virtual bool isNonPtrFieldObj(const LocationSet &ls)
Definition: MemModel.cpp:179
const llvm::Type * getFieldTypeWithFldIdx(u32_t fldIdx)
Get method for fields of a struct.
Definition: MemModel.h:95
SymID GSymID
The unique id in the graph.
Definition: MemModel.h:291
bool isConstArray() const
Definition: MemModel.h:397
Size_t getMaxFieldOffsetLimit() const
Get max field offset limit.
Definition: MemModel.h:319
bool isHeap() const
Definition: MemModel.h:372
llvm::Value Value
Definition: BasicTypes.h:78
bool isGlobalObj()
Definition: MemModel.h:224
bool isConstStruct() const
Definition: MemModel.h:393
bool isStruct()
Definition: MemModel.h:253