Static Value-Flow Analysis
SymbolTableInfo.h
Go to the documentation of this file.
1 //===- SymbolTableInfo.h -- Symbol information from IR------------------------//
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  * SymbolTableInfo.h
25  *
26  * Created on: Nov 11, 2013
27  * Author: Yulei Sui
28  */
29 
30 #ifndef INCLUDE_SVFIR_SYMBOLTABLEINFO_H_
31 #define INCLUDE_SVFIR_SYMBOLTABLEINFO_H_
32 
33 
34 #include "Util/SVFUtil.h"
35 #include "MemoryModel/AccessPath.h"
36 #include "SVFIR/SVFModule.h"
37 namespace SVF
38 {
39 
40 class MemObj;
41 class ObjTypeInfo;
42 class StInfo;
43 
48 {
49  friend class SymbolTableBuilder;
50  friend class SVFIRWriter;
51  friend class SVFIRReader;
52 
53 public:
54 
56  enum SYMTYPE
57  {
66  };
67 
69  //{@
80 
81 private:
87 
88  // Singleton pattern here to enable instance of SymbolTableInfo can only be created once.
90 
93 
95  void destroy();
96 
99 
102 
103 protected:
106  : mod(nullptr), modelConstants(false), totalSymNum(0),
107  maxStruct(nullptr), maxStSize(0)
108  {
109  }
110 
111 public:
112 
114 
115  static SymbolTableInfo* SymbolInfo();
116 
117  static void releaseSymbolInfo()
118  {
119  delete symInfo;
120  symInfo = nullptr;
121  }
123  {
124  destroy();
125  }
127 
129 
130  void setModelConstants(bool _modelConstants)
131  {
132  modelConstants = _modelConstants;
133  }
134  bool getModelConstants() const
135  {
136  return modelConstants;
137  }
139 
142  {
143  return mod;
144  }
146  inline void setModule(SVFModule* m)
147  {
148  mod = m;
149  }
150 
152  // @{
153  static inline bool isBlkPtr(NodeID id)
154  {
155  return (id == BlkPtr);
156  }
157  static inline bool isNullPtr(NodeID id)
158  {
159  return (id == NullPtr);
160  }
161  static inline bool isBlkObj(NodeID id)
162  {
163  return (id == BlackHole);
164  }
165  static inline bool isConstantObj(NodeID id)
166  {
167  return (id == ConstantObj);
168  }
169  static inline bool isBlkObjOrConstantObj(NodeID id)
170  {
171  return (isBlkObj(id) || isConstantObj(id));
172  }
173 
174  inline MemObj* getBlkObj() const
175  {
176  return getObj(blackholeSymID());
177  }
178  inline MemObj* getConstantObj() const
179  {
180  return getObj(constantSymID());
181  }
182 
183  inline SymID blkPtrSymID() const
184  {
185  return BlkPtr;
186  }
187 
188  inline SymID nullPtrSymID() const
189  {
190  return NullPtr;
191  }
192 
193  inline SymID constantSymID() const
194  {
195  return ConstantObj;
196  }
197 
198  inline SymID blackholeSymID() const
199  {
200  return BlackHole;
201  }
202 
204  const MemObj* createDummyObj(SymID symId, const SVFType* type);
205  // @}
206 
208 
209  SymID getValSym(const SVFValue* val);
210 
211  bool hasValSym(const SVFValue* val);
212 
213  inline SymID getObjSym(const SVFValue* val) const
214  {
215  const SVFValue* svfVal = val;
216  if(const SVFGlobalValue* g = SVFUtil::dyn_cast<SVFGlobalValue>(val))
217  svfVal = g->getDefGlobalForMultipleModule();
218  ValueToIDMapTy::const_iterator iter = objSymMap.find(svfVal);
219  assert(iter!=objSymMap.end() && "obj sym not found");
220  return iter->second;
221  }
222 
223  inline MemObj* getObj(SymID id) const
224  {
225  IDToMemMapTy::const_iterator iter = objMap.find(id);
226  assert(iter!=objMap.end() && "obj not found");
227  return iter->second;
228  }
229 
230  inline SymID getRetSym(const SVFFunction* val) const
231  {
232  FunToIDMapTy::const_iterator iter = returnSymMap.find(val);
233  assert(iter!=returnSymMap.end() && "ret sym not found");
234  return iter->second;
235  }
236 
237  inline SymID getVarargSym(const SVFFunction* val) const
238  {
239  FunToIDMapTy::const_iterator iter = varargSymMap.find(val);
240  assert(iter!=varargSymMap.end() && "vararg sym not found");
241  return iter->second;
242  }
244 
245 
247 
248  inline u32_t getTotalSymNum() const
249  {
250  return totalSymNum;
251  }
252  inline u32_t getMaxStructSize() const
253  {
254  return maxStSize;
255  }
257 
259 
261  {
262  return valSymMap;
263  }
264 
266  {
267  return objSymMap;
268  }
269 
271  {
272  return objMap;
273  }
274 
275  inline const IDToMemMapTy& idToObjMap() const
276  {
277  return objMap;
278  }
279 
281  {
282  return returnSymMap;
283  }
284 
286  {
287  return varargSymMap;
288  }
289 
291 
293 
294  inline const SVFTypeSet& getSVFTypes() const
295  {
296  return svfTypes;
297  }
298 
299  inline const Set<const StInfo*>& getStInfos() const
300  {
301  return stInfos;
302  }
304 
306 
307  const StInfo* getTypeInfo(const SVFType* T) const;
309  inline bool hasSVFTypeInfo(const SVFType* T)
310  {
311  return svfTypes.find(T) != svfTypes.end();
312  }
313 
318  u32_t getFlattenedElemIdx(const SVFType* T, u32_t origId);
320  const SVFType* getFlatternedElemType(const SVFType* baseType, u32_t flatten_idx);
324  const SVFType* getOriginalElemType(const SVFType* baseType, u32_t origId) const;
326 
328  void printFlattenFields(const SVFType* type);
329 
330  static std::string toString(SYMTYPE symtype);
331 
333  virtual void dump();
334 
336  virtual APOffset getModulusOffset(const MemObj* obj,
337  const APOffset& apOffset);
338 
341 
344 
345  inline void addTypeInfo(const SVFType* ty)
346  {
347  bool inserted = svfTypes.insert(ty).second;
348  if(!inserted)
349  assert(false && "this type info has been added before");
350  }
351 
352  inline void addStInfo(StInfo* stInfo)
353  {
354  stInfos.insert(stInfo);
355  }
356 
357 protected:
358 
360  const std::vector<const SVFType*>& getFlattenFieldTypes(const SVFStructType *T);
361 
364 
371 
374 };
375 
376 class SVFBaseNode;
377 
381 class MemObj
382 {
383  friend class SVFIRWriter;
384  friend class SVFIRReader;
385  friend class SVFIRBuilder;
386 
387 private:
391  const SVFValue* refVal;
394 
396 
397 public:
399  MemObj(SymID id, ObjTypeInfo* ti, const SVFValue* val = nullptr, const SVFBaseNode* node = nullptr);
400 
402  virtual ~MemObj()
403  {
404  destroy();
405  }
406 
407  virtual const std::string toString() const;
408 
410  inline const SVFValue* getValue() const
411  {
412  return refVal;
413  }
414 
416  inline const SVFBaseNode* getGNode() const
417  {
418  return gNode;
419  }
420 
422  inline SymID getId() const
423  {
424  return symId;
425  }
426 
428  const SVFType* getType() const;
429 
431  u32_t getNumOfElements() const;
432 
434  void setNumOfElements(u32_t num);
435 
438 
440  bool isFieldInsensitive() const;
441 
443  void setFieldInsensitive();
444 
446  void setFieldSensitive();
447 
449  bool isBlackHoleObj() const;
450 
452  u32_t getByteSizeOfObj() const;
453 
455  bool isConstantByteSize() const;
456 
457 
459 
460  bool isFunction() const;
461  bool isGlobalObj() const;
462  bool isStaticObj() const;
463  bool isStack() const;
464  bool isHeap() const;
465  bool isStruct() const;
466  bool isArray() const;
467  bool isVarStruct() const;
468  bool isVarArray() const;
469  bool isConstantStruct() const;
470  bool isConstantArray() const;
471  bool isConstDataOrConstGlobal() const;
472  bool isConstDataOrAggData() const;
474 
476  inline bool operator==(const MemObj& mem) const
477  {
478  return getValue() == mem.getValue();
479  }
480 
482  void destroy();
483 };
484 
489 {
490  friend class SVFIRWriter;
491  friend class SVFIRReader;
492  friend class SymbolTableBuilder;
493 
494 public:
495  typedef enum
496  {
497  FUNCTION_OBJ = 0x1, // object is a function
498  GLOBVAR_OBJ = 0x2, // object is a global variable
499  STATIC_OBJ = 0x4, // object is a static variable allocated before main
500  STACK_OBJ = 0x8, // object is a stack variable
501  HEAP_OBJ = 0x10, // object is a heap variable
502  VAR_STRUCT_OBJ = 0x20, // object contains struct
503  VAR_ARRAY_OBJ = 0x40, // object contains array
504  CONST_STRUCT_OBJ = 0x80, // constant struct
505  CONST_ARRAY_OBJ = 0x100, // constant array
506  CONST_GLOBAL_OBJ = 0x200, // global constant object
507  CONST_DATA = 0x400, // constant object str e.g. 5, 10, 1.0
508  } MEMTYPE;
509 
510 private:
512  const SVFType* type;
521 
524 
526 public:
527 
529  ObjTypeInfo(const SVFType* t, u32_t max);
530 
532  virtual ~ObjTypeInfo()
533  {
534  }
535 
537  inline const SVFType* getType() const
538  {
539  return type;
540  }
541 
544  {
545  return maxOffsetLimit;
546  }
547 
549  inline void setMaxFieldOffsetLimit(u32_t limit)
550  {
551  maxOffsetLimit = limit;
552  }
553 
555  inline void setNumOfElements(u32_t num)
556  {
557  elemNum = num;
559  }
560 
562  inline u32_t getNumOfElements() const
563  {
564  return elemNum;
565  }
566 
568  inline u32_t getByteSizeOfObj() const
569  {
570  assert(isConstantByteSize() && "This Obj's byte size is not constant.");
571  return byteSize;
572  }
573 
575  inline void setByteSizeOfObj(u32_t size)
576  {
577  byteSize = size;
578  }
579 
581  inline bool isConstantByteSize() const
582  {
583  return byteSize != 0;
584  }
585 
587 
588  inline void setFlag(MEMTYPE mask)
589  {
590  flags |= mask;
591  }
592  inline bool hasFlag(MEMTYPE mask)
593  {
594  return (flags & mask) == mask;
595  }
597 
599 
600  inline bool isFunction()
601  {
602  return hasFlag(FUNCTION_OBJ);
603  }
604  inline bool isGlobalObj()
605  {
606  return hasFlag(GLOBVAR_OBJ);
607  }
608  inline bool isStaticObj()
609  {
610  return hasFlag(STATIC_OBJ);
611  }
612  inline bool isStack()
613  {
614  return hasFlag(STACK_OBJ);
615  }
616  inline bool isHeap()
617  {
618  return hasFlag(HEAP_OBJ);
619  }
621 
624 
625  inline bool isVarStruct()
626  {
627  return hasFlag(VAR_STRUCT_OBJ);
628  }
629  inline bool isConstantStruct()
630  {
631  return hasFlag(CONST_STRUCT_OBJ);
632  }
633  inline bool isStruct()
634  {
636  }
637  inline bool isVarArray()
638  {
639  return hasFlag(VAR_ARRAY_OBJ);
640  }
641  inline bool isConstantArray()
642  {
643  return hasFlag(CONST_ARRAY_OBJ);
644  }
645  inline bool isArray()
646  {
648  }
650  {
652  }
653  inline bool isConstDataOrAggData()
654  {
655  return hasFlag(CONST_DATA);
656  }
658 };
659 
660 
661 } // End namespace SVF
662 
663 #endif /* INCLUDE_SVFIR_SYMBOLTABLEINFO_H_ */
newitem type
Definition: cJSON.cpp:2739
#define false
Definition: cJSON.cpp:70
const char *const string
Definition: cJSON.h:172
bool isConstantStruct() const
bool isStruct() const
const SVFType * getType() const
Get obj type.
bool isConstDataOrConstGlobal() const
bool operator==(const MemObj &mem) const
Operator overloading.
void destroy()
Clean up memory.
const SVFValue * getValue() const
Get the reference value to this object.
const SVFValue * refVal
The unique value of this symbol/variable.
bool isConstDataOrAggData() const
SymID symId
The unique id to represent this symbol.
SymID getId() const
Get the memory object id.
bool isGlobalObj() const
bool isFieldInsensitive() const
Return true if its field limit is 0.
const SVFBaseNode * gNode
MemObj(SymID id, ObjTypeInfo *ti, const SVFValue *val=nullptr, const SVFBaseNode *node=nullptr)
Constructor.
u32_t getMaxFieldOffsetLimit() const
Get max field offset limit.
virtual const std::string toString() const
bool isVarArray() const
ObjTypeInfo * typeInfo
Type information of this object.
bool isBlackHoleObj() const
Whether it is a black hole object.
bool isConstantArray() const
bool isHeap() const
void setNumOfElements(u32_t num)
Set the number of elements of this object.
bool isStaticObj() const
virtual ~MemObj()
Destructor.
void setFieldSensitive()
Set the memory object to be field sensitive (up to max field limit)
u32_t getNumOfElements() const
Get the number of elements of this object.
const SVFBaseNode * getGNode() const
Get the reference value to this object.
void setFieldInsensitive()
Set the memory object to be field insensitive.
u32_t getByteSizeOfObj() const
Get the byte size of this object.
bool isVarStruct() const
bool isFunction() const
object attributes methods
bool isConstantByteSize() const
Check if byte size is a const value.
bool isArray() const
bool isStack() const
const SVFType * type
SVF type.
ObjTypeInfo(const SVFType *t, u32_t max)
Constructors.
u32_t elemNum
Size of the object or number of elements.
u32_t getMaxFieldOffsetLimit()
Get max field offset limit.
bool hasFlag(MEMTYPE mask)
bool isConstDataOrConstGlobal()
u32_t getNumOfElements() const
Get the number of elements of this object.
bool isConstantByteSize() const
Check if byte size is a const value.
void setMaxFieldOffsetLimit(u32_t limit)
Get max field offset limit.
const SVFType * getType() const
Get LLVM type.
u32_t byteSize
Byte size of object.
u32_t flags
Type flags.
void setByteSizeOfObj(u32_t size)
Set the byte size of this object.
bool isFunction()
Object attributes.
virtual ~ObjTypeInfo()
Destructor.
u32_t getByteSizeOfObj() const
Get the byte size of this object.
void setFlag(MEMTYPE mask)
Flag for this object type.
void resetTypeForHeapStaticObj(const SVFType *type)
void setNumOfElements(u32_t num)
Set the number of elements of this object.
SymID blackholeSymID() const
const SVFType * maxStruct
The struct type with the most fields.
SymID totalSymNum
total number of symbols
SymID blkPtrSymID() const
void printFlattenFields(const SVFType *type)
Debug method.
const std::vector< const SVFType * > & getFlattenFieldTypes(const SVFStructType *T)
Return the flattened field type for struct type only.
u32_t getFlattenedElemIdx(const SVFType *T, u32_t origId)
Flattened element idx of an array or struct by considering stride.
static SymbolTableInfo * SymbolInfo()
Singleton design here to make sure we only have one instance during any analysis.
u32_t getTotalSymNum() const
Statistics.
ObjTypeInfo * createObjTypeInfo(const SVFType *type)
Create an objectInfo based on LLVM type (value is null, and type could be null, representing a dummy ...
static bool isBlkObjOrConstantObj(NodeID id)
const Set< const StInfo * > & getStInfos() const
ValueToIDMapTy & objSyms()
SVFModule * getModule()
Module.
const SVFType * getOriginalElemType(const SVFType *baseType, u32_t origId) const
void setModelConstants(bool _modelConstants)
Set / Get modelConstants.
void setModule(SVFModule *m)
Module.
static SymbolTableInfo * symInfo
ValueToIDMapTy valSymMap
map a value to its sym id
virtual APOffset getModulusOffset(const MemObj *obj, const APOffset &apOffset)
Given an offset from a Gep Instruction, return it modulus offset by considering memory layout.
ValueToIDMapTy objSymMap
map a obj reference to its sym id
const StInfo * getTypeInfo(const SVFType *T) const
Get struct info.
MemObj * getObj(SymID id) const
MemObj * getConstantObj() const
SymID getVarargSym(const SVFFunction *val) const
static bool isConstantObj(NodeID id)
void addStInfo(StInfo *stInfo)
SymID getValSym(const SVFValue *val)
Get different kinds of syms.
SymID getObjSym(const SVFValue *val) const
static std::string toString(SYMTYPE symtype)
const SVFType * getFlatternedElemType(const SVFType *baseType, u32_t flatten_idx)
Return the type of a flattened element given a flattened index.
u32_t getNumOfFlattenElements(const SVFType *T)
Number of flattened elements of an array or struct.
OrderedMap< SymID, MemObj * > IDToMemMapTy
sym id to memory object map
void destroy()
Clean up memory.
Set< const SVFType * > SVFTypeSet
struct type to struct info map
bool getModelConstants() const
ValueToIDMapTy & valSyms()
Get different kinds of syms maps.
void addTypeInfo(const SVFType *ty)
IDToMemMapTy & idToObjMap()
FunToIDMapTy varargSymMap
vararg map
Set< const StInfo * > stInfos
(owned) All StInfo
virtual void dump()
Another debug method.
static void releaseSymbolInfo()
FunToIDMapTy & retSyms()
SymID constantSymID() const
u32_t maxStSize
The number of fields in max_struct.
u32_t getMaxStructSize() const
FunToIDMapTy & varargSyms()
static bool isBlkPtr(NodeID id)
special value
OrderedMap< const SVFFunction *, SymID > FunToIDMapTy
function to sym id map
static bool isNullPtr(NodeID id)
OrderedMap< const SVFValue *, SymID > ValueToIDMapTy
various maps defined
bool modelConstants
Whether to model constants.
static bool isBlkObj(NodeID id)
SymID nullPtrSymID() const
const IDToMemMapTy & idToObjMap() const
const MemObj * createDummyObj(SymID symId, const SVFType *type)
Can only be invoked by SVFIR::addDummyNode() when creating SVFIR from file.
FunToIDMapTy returnSymMap
return map
const SVFTypeSet & getSVFTypes() const
Constant reader that won't change the state of the symbol table.
IDToMemMapTy objMap
map a memory sym id to its obj
MemObj * getBlkObj() const
bool hasValSym(const SVFValue *val)
bool hasSVFTypeInfo(const SVFType *T)
SVFModule * mod
Module.
SymID getRetSym(const SVFFunction *val) const
SymbolTableInfo(void)
Constructor.
for isBitcode
Definition: BasicTypes.h:68
u32_t NodeID
Definition: GeneralType.h:55
s64_t APOffset
Definition: GeneralType.h:60
unsigned SymID
Definition: GeneralType.h:57
unsigned u32_t
Definition: GeneralType.h:46
std::map< Key, Value, Compare, Allocator > OrderedMap
Definition: GeneralType.h:109
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96