SVF
Public Member Functions | Protected Member Functions | List of all members
SVF::LocSymTableInfo Class Reference

#include <SymbolTableInfo.h>

Inheritance diagram for SVF::LocSymTableInfo:
SVF::SymbolTableInfo

Public Member Functions

 LocSymTableInfo ()
 Constructor. More...
 
virtual ~LocSymTableInfo ()
 Destructor. More...
 
virtual bool computeGepOffset (const User *V, LocationSet &ls)
 Compute gep offset. More...
 
virtual LocationSet getModulusOffset (const MemObj *obj, const LocationSet &ls)
 Given an offset from a Gep Instruction, return it modulus offset by considering memory layout. More...
 
void verifyStructSize (StInfo *stInfo, u32_t structSize)
 Verify struct size construction. More...
 
- Public Member Functions inherited from SVF::SymbolTableInfo
SVFModulegetModule ()
 Module. More...
 
void buildMemModel (SVFModule *svfModule)
 Start building memory model. More...
 
const TypegetBaseTypeAndFlattenedFields (const Value *V, std::vector< LocationSet > &fields)
 Get the base type and max offset. More...
 
u32_t getFields (std::vector< LocationSet > &fields, const Type *T, u32_t msz)
 Replace fields with flatten fields of T if the number of its fields is larger than msz. More...
 
void collectTypeInfo (const Type *T)
 Collect type info. More...
 
void printFlattenFields (const Type *type)
 Debug method. More...
 
virtual void dump ()
 Another debug method. More...
 
void setModelConstants (bool _modelConstants)
 Set / Get modelConstants. More...
 
bool getModelConstants () const
 
const CallSiteSetgetCallSiteSet () const
 Get callsite set. More...
 
u32_t getTypeSizeInBytes (const Type *type)
 Helper method to get the size of the type from target data layout. More...
 
u32_t getTypeSizeInBytes (const StructType *sty, u32_t field_index)
 
void collectSym (const Value *val)
 collect the syms More...
 
void collectVal (const Value *val)
 
void collectObj (const Value *val)
 
void collectRet (const Function *val)
 
void collectVararg (const Function *val)
 
void handleGlobalCE (const GlobalVariable *G)
 Handle constant expression. More...
 
void handleGlobalInitializerCE (const Constant *C, u32_t offset)
 
void handleCE (const Value *val)
 
SymID getValSym (const Value *val)
 Get different kinds of syms. More...
 
bool hasValSym (const Value *val)
 
const ValuegetGlobalRep (const Value *val) const
 find the unique defined global across multiple modules More...
 
SymID getObjSym (const Value *val) const
 
MemObjgetObj (SymID id) const
 
SymID getRetSym (const Function *val) const
 
SymID getVarargSym (const Function *val) const
 
Size_t getTotalSymNum () const
 Statistics. More...
 
u32_t getMaxStructSize () const
 
ValueToIDMapTyvalSyms ()
 Get different kinds of syms maps. More...
 
ValueToIDMapTyobjSyms ()
 
IDToMemMapTyidToObjMap ()
 
FunToIDMapTyretSyms ()
 
FunToIDMapTyvarargSyms ()
 
TypeToFieldInfoMap::iterator getStructInfoIter (const Type *T)
 Get struct info. More...
 
StInfogetStructInfo (const Type *T)
 Get a reference to StructInfo. More...
 
const std::vector< u32_t > & getFattenFieldIdxVec (const Type *T)
 Get a reference to the components of struct_info. More...
 
const std::vector< u32_t > & getFattenFieldOffsetVec (const Type *T)
 
const std::vector< FieldInfo > & getFlattenFieldInfoVec (const Type *T)
 
const TypegetOrigSubTypeWithFldInx (const Type *baseType, u32_t field_idx)
 
const TypegetOrigSubTypeWithByteOffset (const Type *baseType, u32_t byteOffset)
 
virtual ~SymbolTableInfo ()
 
bool isConstantObjSym (const Value *val)
 
void createBlkOrConstantObj (SymID symId)
 
MemObjgetBlkObj () const
 
MemObjgetConstantObj () const
 
SymID blkPtrSymID () const
 
SymID nullPtrSymID () const
 
SymID constantSymID () const
 
SymID blackholeSymID () const
 
const MemObjcreateDummyObj (SymID symId, const Type *type)
 Can only be invoked by PAG::addDummyNode() when creaing PAG from file. More...
 

Protected Member Functions

virtual void collectStructInfo (const StructType *T)
 Collect the struct info. More...
 
virtual void collectArrayInfo (const ArrayType *T)
 Collect the array info. More...
 
- Protected Member Functions inherited from SVF::SymbolTableInfo
 SymbolTableInfo (void)
 Constructor. More...
 
virtual void collectSimpleTypeInfo (const Type *T)
 Collect simple type (non-aggregate) info. More...
 

Additional Inherited Members

- Public Types inherited from SVF::SymbolTableInfo
typedef OrderedMap< const Value *, SymIDValueToIDMapTy
 various maps defined More...
 
typedef OrderedMap< SymID, MemObj * > IDToMemMapTy
 sym id to memory object map More...
 
typedef OrderedMap< const Function *, SymIDFunToIDMapTy
 function to sym id map More...
 
typedef OrderedMap< SymID, SYMTYPEIDToSymTyMapTy
 sym id to sym type map More...
 
typedef OrderedMap< const Type *, StInfo * > TypeToFieldInfoMap
 struct type to struct info map More...
 
typedef Set< CallSiteCallSiteSet
 
typedef OrderedMap< const Instruction *, CallSiteIDCallSiteToIDMapTy
 
typedef OrderedMap< CallSiteID, const Instruction * > IDToCallSiteMapTy
 
- Static Public Member Functions inherited from SVF::SymbolTableInfo
static DataLayoutgetDataLayout (Module *mod)
 Get target machine data layout. More...
 
static std::string toString (SYMTYPE symtype)
 
static SymbolTableInfoSymbolInfo ()
 Singleton design here to make sure we only have one instance during any analysis. More...
 
static void releaseSymbolInfo ()
 
static bool isNullPtrSym (const Value *val)
 special value More...
 
static bool isBlackholeSym (const Value *val)
 
static bool isBlkPtr (NodeID id)
 
static bool isNullPtr (NodeID id)
 
static bool isBlkObj (NodeID id)
 
static bool isConstantObj (NodeID id)
 
static bool isBlkObjOrConstantObj (NodeID id)
 
- Protected Attributes inherited from SVF::SymbolTableInfo
TypeToFieldInfoMap typeToFieldInfo
 
const TypemaxStruct
 The struct type with the most fields. More...
 
u32_t maxStSize
 The number of fields in max_struct. More...
 

Detailed Description

Bytes/bits-level modeling of memory locations to handle weakly type languages. (declared with one type but accessed as another) Abstract memory objects are created according to the static allocated size.

Definition at line 460 of file SymbolTableInfo.h.

Constructor & Destructor Documentation

◆ LocSymTableInfo()

SVF::LocSymTableInfo::LocSymTableInfo ( )
inline

Constructor.

Definition at line 465 of file SymbolTableInfo.h.

466  {
467  }

◆ ~LocSymTableInfo()

virtual SVF::LocSymTableInfo::~LocSymTableInfo ( )
inlinevirtual

Destructor.

Definition at line 469 of file SymbolTableInfo.h.

470  {
471  }

Member Function Documentation

◆ collectArrayInfo()

void LocSymTableInfo::collectArrayInfo ( const ArrayType T)
protectedvirtual

Collect the array info.

Collect array information

Reimplemented from SVF::SymbolTableInfo.

Definition at line 400 of file MemModel.cpp.

401 {
402  /*
403  StInfo *stinfo = new StInfo();
404  typeToFieldInfo[ty] = stinfo;
405 
409  u64_t out_num = ty->getNumElements();
410  const Type* elemTy = ty->getElementType();
411  u32_t out_stride = getTypeSizeInBytes(elemTy);
412 
414  stinfo->addOffsetWithType(0, elemTy);
415 
416  while (const ArrayType* aty = dyn_cast<ArrayType>(elemTy)) {
417  out_num *= aty->getNumElements();
418  elemTy = aty->getElementType();
419  out_stride = getTypeSizeInBytes(elemTy);
420  }
421 
425  StInfo* elemStInfo = getStructInfo(elemTy);
426  u32_t nfE = elemStInfo->getFlattenFieldInfoVec().size();
427  for (u32_t j = 0; j < nfE; j++) {
428  u32_t off = elemStInfo->getFlattenFieldInfoVec()[j].getFlattenOffset();
429  const Type* fieldTy = elemStInfo->getFlattenFieldInfoVec()[j].getFlattenElemTy();
430  FieldInfo::ElemNumStridePairVec pair = elemStInfo->getFlattenFieldInfoVec()[j].getElemNumStridePairVect();
432  pair.push_back(std::make_pair(out_num, out_stride));
433  FieldInfo field(off, fieldTy, pair);
434  stinfo->getFlattenFieldInfoVec().push_back(field);
435  }
436  */
437 }

◆ collectStructInfo()

void LocSymTableInfo::collectStructInfo ( const StructType T)
protectedvirtual

Collect the struct info.

Reimplemented from SVF::SymbolTableInfo.

Definition at line 443 of file MemModel.cpp.

444 {
445  /*
446  StInfo *stinfo = new StInfo();
447  typeToFieldInfo[ty] = stinfo;
448 
449  const StructLayout *stTySL = getDataLayout(getModule().getMainLLVMModule())->getStructLayout( const_cast<StructType *>(ty) );
450 
451  u32_t field_idx = 0;
452  for (StructType::element_iterator it = ty->element_begin(), ie =
453  ty->element_end(); it != ie; ++it, ++field_idx) {
454  const Type *et = *it;
455 
456  // The offset is where this element will be placed in the struct.
457  // This offset is computed after alignment with the current struct
458  u64_t eOffsetInBytes = stTySL->getElementOffset(field_idx);
459 
460  //The offset is where this element will be placed in the exp. struct.
463  stinfo->addOffsetWithType(static_cast<u32_t>(eOffsetInBytes), et);
464 
465  StInfo* fieldStinfo = getStructInfo(et);
466  u32_t nfE = fieldStinfo->getFlattenFieldInfoVec().size();
467  //Copy ST's info, whose element 0 is the size of ST itself.
468  for (u32_t j = 0; j < nfE; j++) {
469  u32_t oft = eOffsetInBytes + fieldStinfo->getFlattenFieldInfoVec()[j].getFlattenOffset();
470  const Type* elemTy = fieldStinfo->getFlattenFieldInfoVec()[j].getFlattenElemTy();
471  FieldInfo::ElemNumStridePairVec pair = fieldStinfo->getFlattenFieldInfoVec()[j].getElemNumStridePairVect();
472  pair.push_back(std::make_pair(1, 0));
473  FieldInfo newField(oft, elemTy, pair);
474  stinfo->getFlattenFieldInfoVec().push_back(newField);
475  }
476  }
477 
478  // verifyStructSize(stinfo,stTySL->getSizeInBytes());
479 
480  //Record the size of the complete struct and update max_struct.
481  if (stTySL->getSizeInBytes() > maxStSize) {
482  maxStruct = ty;
483  maxStSize = stTySL->getSizeInBytes();
484  }
485  */
486 }

◆ computeGepOffset()

bool LocSymTableInfo::computeGepOffset ( const User V,
LocationSet ls 
)
virtual

Compute gep offset.

Compute gep offset

variant offset

Reimplemented from SVF::SymbolTableInfo.

Definition at line 305 of file MemModel.cpp.

306 {
307 
308  assert(V);
309  int baseIndex = -1;
310  int index = 0;
311  for (bridge_gep_iterator gi = bridge_gep_begin(*V), ge = bridge_gep_end(*V);
312  gi != ge; ++gi, ++index)
313  {
314  if(SVFUtil::isa<ConstantInt>(gi.getOperand()) == false)
315  baseIndex = index;
316  }
317 
318  index = 0;
319  for (bridge_gep_iterator gi = bridge_gep_begin(*V), ge = bridge_gep_end(*V);
320  gi != ge; ++gi, ++index)
321  {
322 
323  if (index <= baseIndex)
324  {
326  // Handling pointer types
327  if (const PointerType* pty = SVFUtil::dyn_cast<PointerType>(*gi))
328  {
329  const Type* et = pty->getElementType();
330  Size_t sz = getTypeSizeInBytes(et);
331 
332  Size_t num = 1;
333  if (const ArrayType* aty = SVFUtil::dyn_cast<ArrayType>(et))
334  num = aty->getNumElements();
335  else
336  num = StInfo::getMaxFieldLimit();
337 
338  ls.addElemNumStridePair(std::make_pair(num, sz));
339  }
340  // Calculate the size of the array element
341  else if(const ArrayType* at = SVFUtil::dyn_cast<ArrayType>(*gi))
342  {
343  const Type* et = at->getElementType();
344  Size_t sz = getTypeSizeInBytes(et);
345  Size_t num = at->getNumElements();
346  ls.addElemNumStridePair(std::make_pair(num, sz));
347  }
348  else
349  assert(false && "what other types?");
350  }
351  // constant offset
352  else
353  {
354  assert(SVFUtil::isa<ConstantInt>(gi.getOperand()) && "expecting a constant");
355 
356  ConstantInt *op = SVFUtil::cast<ConstantInt>(gi.getOperand());
357 
358  //The actual index
359  Size_t idx = op->getSExtValue();
360 
361  // Handling pointer types
362  // These GEP instructions are simply making address computations from the base pointer address
363  // e.g. idx1 = (char*) &MyVar + 4, at this case gep only one offset index (idx)
364  if (const PointerType* pty = SVFUtil::dyn_cast<PointerType>(*gi))
365  {
366  const Type* et = pty->getElementType();
367  Size_t sz = getTypeSizeInBytes(et);
368  ls.setByteOffset(ls.getByteOffset() + idx * sz);
369  }
370  // Calculate the size of the array element
371  else if(const ArrayType* at = SVFUtil::dyn_cast<ArrayType>(*gi))
372  {
373  const Type* et = at->getElementType();
374  Size_t sz = getTypeSizeInBytes(et);
375  ls.setByteOffset(ls.getByteOffset() + idx * sz);
376  }
377  // Handling struct here
378  else if (const StructType *ST = SVFUtil::dyn_cast<StructType>(*gi))
379  {
380  assert(op && "non-const struct index in GEP");
381  const vector<u32_t> &so = SymbolTableInfo::SymbolInfo()->getFattenFieldOffsetVec(ST);
382  if ((unsigned)idx >= so.size())
383  {
384  outs() << "!! Struct index out of bounds" << idx << "\n";
385  assert(0);
386  }
387  //add the translated offset
388  ls.setByteOffset(ls.getByteOffset() + so[idx]);
389  }
390  else
391  assert(false && "what other types?");
392  }
393  }
394  return true;
395 }
void setByteOffset(Size_t os)
Definition: LocationSet.h:206
llvm::Type Type
Definition: BasicTypes.h:75
Size_t getByteOffset() const
Definition: LocationSet.h:198
#define assert(ex)
Definition: util.h:141
llvm::PointerType PointerType
Definition: BasicTypes.h:108
llvm::ArrayType ArrayType
Definition: BasicTypes.h:107
llvm::ConstantInt ConstantInt
Definition: BasicTypes.h:153
void addElemNumStridePair(const NodePair &pair)
Definition: LocationSet.cpp:42
bridge_gep_iterator bridge_gep_begin(const User *GEP)
llvm::StructType StructType
LLVM types.
Definition: BasicTypes.h:106
signed long Size_t
Definition: SVFBasicTypes.h:78
const std::vector< u32_t > & getFattenFieldOffsetVec(const Type *T)
static u32_t getMaxFieldLimit()
Definition: MemModel.h:88
static SymbolTableInfo * SymbolInfo()
Singleton design here to make sure we only have one instance during any analysis. ...
raw_ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:47
bridge_gep_iterator bridge_gep_end(const User *GEP)
u32_t getTypeSizeInBytes(const Type *type)
Helper method to get the size of the type from target data layout.

◆ getModulusOffset()

LocationSet LocSymTableInfo::getModulusOffset ( const MemObj obj,
const LocationSet ls 
)
virtual

Given an offset from a Gep Instruction, return it modulus offset by considering memory layout.

Given LocationSet from a Gep Instruction, return a new LocationSet which matches the field information of this ObjTypeInfo by considering memory layout

Find an appropriate field for this LocationSet

This location set represent one object

if the offset is negative, it's possible that we're looking for an obj node out of range of current struct. Make the offset positive so we can still get a node within current struct to represent this obj.

This location set represents multiple objects

Reimplemented from SVF::SymbolTableInfo.

Definition at line 493 of file MemModel.cpp.

494 {
495  const Type* ety = obj->getType();
496 
497  if (SVFUtil::isa<StructType>(ety) || SVFUtil::isa<ArrayType>(ety))
498  {
500  const std::vector<FieldInfo>& infovec = SymbolTableInfo::SymbolInfo()->getFlattenFieldInfoVec(ety);
501  std::vector<FieldInfo>::const_iterator it = infovec.begin();
502  std::vector<FieldInfo>::const_iterator eit = infovec.end();
503  for (; it != eit; ++it)
504  {
505  const FieldInfo& fieldLS = *it;
506  LocationSet rhsLS(fieldLS);
508  if (result == LocationSet::Same ||
509  result == LocationSet::Superset ||
510  result == LocationSet::Subset)
511  return ls;
512  else if (result == LocationSet::Overlap)
513  {
514  // TODO:
515  return ls;
516  }
517  else if (result == LocationSet::NonOverlap)
518  {
519  continue;
520  }
521 
522  assert(false && "cannot find an appropriate field for specified LocationSet");
523  return ls;
524  }
525  }
526  else
527  {
528  if (obj->isStaticObj() || obj->isHeap())
529  {
530  // TODO: Objects which cannot find proper field for a certain offset including
531  // arguments in main(), static objects allocated before main and heap
532  // objects. Right now they're considered to have infinite fields. So we
533  // just return the location set without modifying it.
534  return ls;
535  }
536  else
537  {
538  // TODO: Using new memory model (locMM) may create objects with spurious offset
539  // as we simply return new offset by mod operation without checking its
540  // correctness in LocSymTableInfo::getModulusOffset(). So the following
541  // assertion may fail. Try to refine the new memory model.
542  assert(ls.isConstantOffset() && "expecting a constant location set");
543  return ls;
544  }
545  }
546 
548  if (ls.isConstantOffset())
549  {
553  Size_t offset = ls.getOffset();
554  if(offset < 0)
555  {
556  writeWrnMsg("try to create a gep node with negative offset.");
557  offset = abs(offset);
558  }
559  u32_t maxOffset = obj->getMaxFieldOffsetLimit();
560  if (maxOffset != 0)
561  offset = offset % maxOffset;
562  else
563  offset = 0;
564  }
566  else
567  {
568 
569  }
570 
571  return ls;
572 }
llvm::Type Type
Definition: BasicTypes.h:75
#define assert(ex)
Definition: util.h:141
const llvm::Type * getType() const
Get obj type.
Definition: MemModel.cpp:278
bool isConstantOffset() const
Return TRUE if this is a constant location set.
Definition: LocationSet.h:219
unsigned u32_t
Definition: SVFBasicTypes.h:75
void writeWrnMsg(std::string msg)
Writes a message run through wrnMsg.
Definition: SVFUtil.cpp:67
const std::vector< FieldInfo > & getFlattenFieldInfoVec(const Type *T)
signed long Size_t
Definition: SVFBasicTypes.h:78
bool isStaticObj() const
Definition: MemModel.h:364
static SymbolTableInfo * SymbolInfo()
Singleton design here to make sure we only have one instance during any analysis. ...
static LSRelation checkRelation(const LocationSet &LHS, const LocationSet &RHS)
Check relations of two location sets.
Definition: LocationSet.h:231
Size_t getOffset() const
Get methods.
Definition: LocationSet.h:194
static int result
Definition: cuddGenetic.c:121
Size_t getMaxFieldOffsetLimit() const
Get max field offset limit.
Definition: MemModel.h:319
bool isHeap() const
Definition: MemModel.h:372

◆ verifyStructSize()

void LocSymTableInfo::verifyStructSize ( StInfo stinfo,
u32_t  structSize 
)

Verify struct size construction.

Verify struct size

Please note this verify may not be complete as different machine has different alignment mechanism

Definition at line 577 of file MemModel.cpp.

578 {
579 
580  u32_t lastOff = stinfo->getFlattenFieldInfoVec().back().getFlattenByteOffset();
581  u32_t strideSize = 0;
582  FieldInfo::ElemNumStridePairVec::const_iterator pit = stinfo->getFlattenFieldInfoVec().back().elemStridePairBegin();
583  FieldInfo::ElemNumStridePairVec::const_iterator epit = stinfo->getFlattenFieldInfoVec().back().elemStridePairEnd();
584  for(; pit!=epit; ++pit)
585  strideSize += pit->first * pit->second;
586 
587  u32_t lastSize = getTypeSizeInBytes(stinfo->getFlattenFieldInfoVec().back().getFlattenElemTy());
589  assert((structSize == lastOff + strideSize + lastSize) && "struct size not consistent");
590 
591 }
#define assert(ex)
Definition: util.h:141
unsigned u32_t
Definition: SVFBasicTypes.h:75
std::vector< FieldInfo > & getFlattenFieldInfoVec()
Definition: MemModel.h:111
u32_t getTypeSizeInBytes(const Type *type)
Helper method to get the size of the type from target data layout.

The documentation for this class was generated from the following files: