Static Value-Flow Analysis
Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes | List of all members
SVF::MTAAnnotator Class Reference

#include <MTAAnnotator.h>

Inheritance diagram for SVF::MTAAnnotator:
SVF::Annotator

Public Types

typedef Set< const Instruction * > InstSet
 

Public Member Functions

 MTAAnnotator ()
 Constructor. More...
 
virtual ~MTAAnnotator ()
 Destructor. More...
 
void annotateDRCheck (Instruction *inst)
 Annotation. More...
 
void initialize (MHP *mhp, LockAnalysis *lsa)
 Initialize. More...
 
void pruneThreadLocal (PointerAnalysis *pta)
 Prune candidate instructions that are thread local. More...
 
void pruneAliasMHP (PointerAnalysis *pta)
 Prune candidate instructions that non-mhp and non-alias with others. More...
 
void performAnnotate ()
 Perform annotation. More...
 
void collectLoadStoreInst (SVFModule *mod)
 Collect all load and store instruction. More...
 
const ValuegetStoreOperand (const Instruction *inst)
 Get operand of store and load. More...
 
const ValuegetLoadOperand (const Instruction *inst)
 
bool isMemset (const Instruction *I)
 Check if Function "F" is memset. More...
 
bool isMemcpy (const Instruction *I)
 Check if Function "F" is memcpy. More...
 
- Public Member Functions inherited from SVF::Annotator
 Annotator ()
 Constructor. More...
 
virtual ~Annotator ()
 Destructor. More...
 
bool hasSBSourceFlag (Instruction *inst) const
 SB Has flag methods. More...
 
bool hasSBSinkFlag (Instruction *inst) const
 
bool hasDRNotCheckFlag (Instruction *inst) const
 Race Detection Has flag methods. More...
 
bool hasDRNotCheckFlag (const Instruction *inst) const
 
bool hasDRCheckFlag (Instruction *inst) const
 
bool hasDRCheckFlag (const Instruction *inst) const
 
void addMDTag (Instruction *inst, std::string str)
 Simple add/remove meta data information. More...
 
void removeMDTag (Instruction *inst, std::string str)
 
void addMDTag (Instruction *inst, Value *val, std::string str)
 manipulate llvm meta data on instructions for a specific value More...
 
void removeMDTag (Instruction *inst, Value *val, std::string str)
 remove flag from llvm metadata More...
 

Public Attributes

u32_t numOfAllSt
 
u32_t numOfAllLd
 
u32_t numOfNonLocalSt
 
u32_t numOfNonLocalLd
 
u32_t numOfAliasSt
 
u32_t numOfAliasLd
 
u32_t numOfMHPSt
 
u32_t numOfMHPLd
 
u32_t numOfAnnotatedSt
 
u32_t numOfAnnotatedLd
 

Static Public Attributes

static const u32_t ANNO_MHP = 0x04
 Constant INTERLEV_FLAG values. More...
 
static const u32_t ANNO_ALIAS = 0x02
 
static const u32_t ANNO_LOCAL = 0x01
 

Private Attributes

MHPmhp
 
LockAnalysislsa
 
InstSet loadset
 
InstSet storeset
 

Additional Inherited Members

- Protected Attributes inherited from SVF::Annotator
const char * SB_SLICESOURCE
 Saber annotations. More...
 
const char * SB_SLICESINK
 
const char * SB_FESIBLE
 
const char * SB_INFESIBLE
 
const char * DR_NOT_CHECK
 Race Detection annotations. More...
 
const char * DR_CHECK
 

Detailed Description

MTA annotation

Definition at line 23 of file MTAAnnotator.h.

Member Typedef Documentation

◆ InstSet

Definition at line 27 of file MTAAnnotator.h.

Constructor & Destructor Documentation

◆ MTAAnnotator()

SVF::MTAAnnotator::MTAAnnotator ( )
inline

Constructor.

Definition at line 29 of file MTAAnnotator.h.

29  : mhp(nullptr),lsa(nullptr)
30  {
31  numOfAllSt = 0;
32  numOfAllLd = 0;
33  numOfNonLocalSt = 0;
34  numOfNonLocalLd = 0;
35  numOfAliasSt = 0;
36  numOfAliasLd = 0;
37  numOfMHPSt = 0;
38  numOfMHPLd = 0;
39  numOfAnnotatedSt = 0;
40  numOfAnnotatedLd = 0;
41  }
LockAnalysis * lsa
Definition: MTAAnnotator.h:90

◆ ~MTAAnnotator()

virtual SVF::MTAAnnotator::~MTAAnnotator ( )
inlinevirtual

Destructor.

Definition at line 43 of file MTAAnnotator.h.

44  {
45 
46  }

Member Function Documentation

◆ annotateDRCheck()

void MTAAnnotator::annotateDRCheck ( Instruction inst)

Annotation.

memcpy and memset is not annotated

Definition at line 18 of file MTAAnnotator.cpp.

19 {
20  std::string str;
21  std::stringstream rawstr(str);
22  rawstr << DR_CHECK;
23 
25  if (StoreInst* st = SVFUtil::dyn_cast<StoreInst>(inst))
26  {
28  addMDTag(inst, st->getPointerOperand(), rawstr.str());
29  }
30  else if (LoadInst* ld = SVFUtil::dyn_cast<LoadInst>(inst))
31  {
33  addMDTag(inst, ld->getPointerOperand(), rawstr.str());
34  }
35 }
const char *const string
Definition: cJSON.h:172
const char * DR_CHECK
Definition: Annotator.h:174
void addMDTag(Instruction *inst, std::string str)
Simple add/remove meta data information.
Definition: Annotator.h:90
llvm::LoadInst LoadInst
Definition: BasicTypes.h:149
llvm::StoreInst StoreInst
Definition: BasicTypes.h:148

◆ collectLoadStoreInst()

void MTAAnnotator::collectLoadStoreInst ( SVFModule mod)

Collect all load and store instruction.

Definition at line 37 of file MTAAnnotator.cpp.

38 {
39 
40  for (Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
41  {
42  for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
43  {
44  if (SVFUtil::isExtCall(LLVMModuleSet::getLLVMModuleSet()->getSVFFunction(&*F)))
45  continue;
46  for (const_inst_iterator II = inst_begin(*F), E = inst_end(*F); II != E; ++II)
47  {
48  const Instruction* inst = &*II;
49  if (SVFUtil::isa<LoadInst>(inst))
50  {
51  loadset.insert(inst);
52  }
53  else if (SVFUtil::isa<StoreInst>(inst))
54  {
55  storeset.insert(inst);
56  }
57  else if (isMemset(inst))
58  {
59  storeset.insert(inst);
60  }
61  else if (isMemcpy(inst))
62  {
63  storeset.insert(inst);
64  loadset.insert(inst);
65  }
66  }
67  }
68  }
69 
70  numOfAllSt = storeset.size();
71  numOfAllLd = loadset.size();
72 }
#define F(f)
static LLVMModuleSet * getLLVMModuleSet()
Definition: LLVMModule.h:105
bool isMemset(const Instruction *I)
Check if Function "F" is memset.
Definition: MTAAnnotator.h:73
bool isMemcpy(const Instruction *I)
Check if Function "F" is memcpy.
Definition: MTAAnnotator.h:81
bool isExtCall(const SVFFunction *fun)
Definition: SVFUtil.h:309
llvm::const_inst_iterator const_inst_iterator
Definition: BasicTypes.h:250
llvm::Instruction Instruction
Definition: BasicTypes.h:87
llvm::Module Module
Definition: BasicTypes.h:84

◆ getLoadOperand()

const Value * MTAAnnotator::getLoadOperand ( const Instruction inst)

Definition at line 92 of file MTAAnnotator.cpp.

93 {
94  if (const LoadInst* ld = SVFUtil::dyn_cast<LoadInst>(inst))
95  {
96  return ld->getPointerOperand();
97  }
98  else if (isMemcpy(inst))
99  {
100  return inst->getOperand(1);
101  }
102 
103  assert(false);
104  return nullptr;
105 }

◆ getStoreOperand()

const Value * MTAAnnotator::getStoreOperand ( const Instruction inst)

Get operand of store and load.

Definition at line 74 of file MTAAnnotator.cpp.

75 {
76  if (const StoreInst* st = SVFUtil::dyn_cast<StoreInst>(inst))
77  {
78  return st->getPointerOperand();
79  }
80  else if (isMemset(inst))
81  {
82  return inst->getOperand(0);
83  }
84  else if (isMemcpy(inst))
85  {
86  return inst->getOperand(0);
87  }
88 
89  assert(false);
90  return nullptr;
91 }

◆ initialize()

void MTAAnnotator::initialize ( MHP mhp,
LockAnalysis lsa 
)

Initialize.

Definition at line 107 of file MTAAnnotator.cpp.

108 {
109  mhp = m;
110  lsa = la;
111  if (!Options::AnnoFlag())
112  return;
114 }
TCT * getTCT() const
Get Thread Creation Tree.
Definition: MHP.h:82
void collectLoadStoreInst(SVFModule *mod)
Collect all load and store instruction.
static const Option< u32_t > AnnoFlag
Definition: Options.h:172
SVFModule * getModule() const
Module.
PointerAnalysis * getPTA() const
Get PTA.
Definition: TCT.h:187

◆ isMemcpy()

bool SVF::MTAAnnotator::isMemcpy ( const Instruction I)
inline

Check if Function "F" is memcpy.

Definition at line 81 of file MTAAnnotator.h.

82  {
83  const SVFInstruction* svfInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(I);
84  const SVFFunction* F = SVFUtil::getCallee(svfInst);
85  return F && F->getName().find("llvm.memcpy") != std::string::npos;
86  }
SVFInstruction * getSVFInstruction(const Instruction *inst) const
Definition: LLVMModule.h:224
const SVFFunction * getCallee(const CallSite cs)
Return callee of a callsite. Return null if this is an indirect call.
Definition: SVFUtil.h:241

◆ isMemset()

bool SVF::MTAAnnotator::isMemset ( const Instruction I)
inline

Check if Function "F" is memset.

Definition at line 73 of file MTAAnnotator.h.

74  {
75  const SVFInstruction* svfInst = LLVMModuleSet::getLLVMModuleSet()->getSVFInstruction(I);
76  const SVFFunction* F =SVFUtil::getCallee(svfInst);
77  return F && F->getName().find("llvm.memset") != std::string::npos;
78  }

◆ performAnnotate()

void MTAAnnotator::performAnnotate ( )

Perform annotation.

Definition at line 281 of file MTAAnnotator.cpp.

282 {
283  if (!Options::AnnoFlag())
284  return;
285  for (InstSet::iterator it = storeset.begin(), eit = storeset.end(); it != eit; ++it)
286  {
287  annotateDRCheck(const_cast<Instruction*>(*it));
288  }
289  for (InstSet::iterator it = loadset.begin(), eit = loadset.end(); it != eit; ++it)
290  {
291  annotateDRCheck(const_cast<Instruction*>(*it));
292  }
293 }
void annotateDRCheck(Instruction *inst)
Annotation.

◆ pruneAliasMHP()

void MTAAnnotator::pruneAliasMHP ( PointerAnalysis pta)

Prune candidate instructions that non-mhp and non-alias with others.

if it1 == it2, mhp analysis will annotate it1 that locates in loop or recursion. but alias analysis fails to determine whether it1 is in loop or recursion, that means all store instructions will be annotated by alias analysis to guarantee sound.

Definition at line 202 of file MTAAnnotator.cpp.

203 {
204 
205  bool AnnoMHP = Options::AnnoFlag() & ANNO_MHP;
206  bool AnnoAlias = Options::AnnoFlag() & ANNO_ALIAS;
207 
208  if (!AnnoMHP && !AnnoAlias)
209  return;
210 
211  DBOUT(DGENERAL, outs() << pasMsg("Run annotator prune Alias or MHP pairs\n"));
212  InstSet needannost;
213  InstSet needannold;
214  for (InstSet::iterator it1 = storeset.begin(), eit1 = storeset.end(); it1 != eit1; ++it1)
215  {
217  for (InstSet::iterator it2 = it1, eit2 = storeset.end(); it2 != eit2; ++it2)
218  {
222 
223  if(!pta->alias(v1, v2))
224  continue;
225 
226  if (AnnoMHP)
227  {
228  if (mhp->mayHappenInParallel(inst1, inst2) && !lsa->isProtectedByCommonLock(inst1, inst2))
229  {
230  needannost.insert(*it1);
231  needannost.insert(*it2);
232  }
233  }
234  else
235  {
239  needannost.insert(*it1);
240  needannost.insert(*it2);
241  }
242  }
243  for (InstSet::iterator it2 = loadset.begin(), eit2 = loadset.end(); it2 != eit2; ++it2)
244  {
248 
249  if(!pta->alias(v1,v2))
250  continue;
251 
252  if (AnnoMHP)
253  {
254  if (mhp->mayHappenInParallel(inst1, inst2) && !lsa->isProtectedByCommonLock(inst1, inst2))
255  {
256  needannost.insert(*it1);
257  needannold.insert(*it2);
258  }
259  }
260  else
261  {
262  needannost.insert(*it1);
263  needannold.insert(*it2);
264  }
265  }
266  }
267  storeset = needannost;
268  loadset = needannold;
269 
270  if (AnnoMHP)
271  {
272  numOfMHPSt = storeset.size();
273  numOfMHPLd = loadset.size();
274  }
275  else if (AnnoAlias)
276  {
277  numOfAliasSt = storeset.size();
278  numOfAliasLd = loadset.size();
279  }
280 }
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition: SVFType.h:484
#define DGENERAL
Definition: SVFType.h:490
SVFValue * getSVFValue(const Value *value)
bool isProtectedByCommonLock(const SVFInstruction *i1, const SVFInstruction *i2)
virtual bool mayHappenInParallel(const SVFInstruction *i1, const SVFInstruction *i2)
Interface to query whether two instructions may happen-in-parallel.
Definition: MHP.cpp:602
static const u32_t ANNO_ALIAS
Definition: MTAAnnotator.h:113
const Value * getLoadOperand(const Instruction *inst)
const Value * getStoreOperand(const Instruction *inst)
Get operand of store and load.
static const u32_t ANNO_MHP
Constant INTERLEV_FLAG values.
Definition: MTAAnnotator.h:112
Set< const Instruction * > InstSet
Definition: MTAAnnotator.h:27
virtual AliasResult alias(const SVFValue *V1, const SVFValue *V2)=0
Interface exposed to users of our pointer analysis, given Value infos.
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
Definition: SVFUtil.cpp:99
std::ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:50

◆ pruneThreadLocal()

void MTAAnnotator::pruneThreadLocal ( PointerAnalysis pta)

Prune candidate instructions that are thread local.

find fork arguments' objects

find global pointer-to objects

find all non-local objects that are transitively pointed by global and fork arguments.

compute all store and load instructions that may operate a non-local object.

Definition at line 116 of file MTAAnnotator.cpp.

117 {
118  bool AnnoLocal = Options::AnnoFlag() & ANNO_LOCAL;
119  if (!AnnoLocal)
120  return;
121 
122  DBOUT(DGENERAL, outs() << pasMsg("Run annotator prune thread local pairs\n"));
123  SVFIR* pag = pta->getPAG();
124  PointsTo nonlocalobjs;
125  PointsTo worklist;
126 
129  for (SVFStmt::SVFStmtSetTy::const_iterator it = forkedges.begin(), eit = forkedges.end(); it != eit; ++it)
130  {
131  PAGEdge* edge = *it;
132  worklist |= pta->getPts(edge->getDstID());
133  worklist |= pta->getPts(edge->getSrcID());
134  }
135 
137  const SVFIR::SVFStmtSet& globaledges = pag->getGlobalSVFStmtSet();
138  for (SVFIR::SVFStmtSet::const_iterator it = globaledges.begin(), eit = globaledges.end(); it != eit; ++it)
139  {
140  const PAGEdge* edge = *it;
141  if (edge->getEdgeKind() == SVFStmt::Addr)
142  {
143  worklist.set(edge->getSrcID());
144  }
145  }
146 
148  while (!worklist.empty())
149  {
150  NodeID obj = worklist.find_first();
151  nonlocalobjs.set(obj);
152  worklist.reset(obj);
153  PointsTo pts = pta->getPts(obj);
154  for (PointsTo::iterator pit = pts.begin(), epit = pts.end(); pit != epit; ++pit)
155  {
156  if (!nonlocalobjs.test(*pit))
157  worklist.set(*pit);
158  }
159  NodeBS fields = pag->getAllFieldsObjVars(obj);
160  for (NodeBS::iterator pit = fields.begin(), epit = fields.end(); pit != epit; ++pit)
161  {
162  if (!nonlocalobjs.test(*pit))
163  worklist.set(*pit);
164  }
165  }
166 
168  InstSet needannost;
169  InstSet needannold;
170  for (InstSet::iterator it = storeset.begin(), eit = storeset.end(); it != eit; ++it)
171  {
173  for (PointsTo::iterator pit = pts.begin(), epit = pts.end(); pit != epit; ++pit)
174  {
175  if (nonlocalobjs.test(*pit))
176  {
177  needannost.insert(*it);
178  break;
179  }
180  }
181  }
182 
183  for (InstSet::iterator it = loadset.begin(), eit = loadset.end(); it != eit; ++it)
184  {
186  for (PointsTo::iterator pit = pts.begin(), epit = pts.end(); pit != epit; ++pit)
187  {
188  if (nonlocalobjs.test(*pit))
189  {
190  needannold.insert(*it);
191  break;
192  }
193  }
194  }
195 
196  storeset = needannost;
197  loadset = needannold;
198 
199  numOfNonLocalSt = storeset.size();
200  numOfNonLocalLd = loadset.size();
201 }
GEdgeKind getEdgeKind() const
Definition: GenericGraph.h:89
NodeID getDstID() const
Definition: GenericGraph.h:85
NodeID getSrcID() const
get methods of the components
Definition: GenericGraph.h:81
Set< const SVFStmt * > SVFStmtSet
Definition: IRGraph.h:55
NodeID getValueNode(const SVFValue *V)
Definition: IRGraph.h:137
static const u32_t ANNO_LOCAL
Definition: MTAAnnotator.h:114
SVFIR * getPAG() const
virtual const PointsTo & getPts(NodeID ptr)=0
Get points-to targets of a pointer. It needs to be implemented in child class.
bool empty() const
Returns true if set is empty.
Definition: PointsTo.cpp:98
int find_first()
Definition: PointsTo.cpp:203
void reset(u32_t n)
Removes n from the set.
Definition: PointsTo.cpp:166
const_iterator end() const
Definition: PointsTo.h:132
void set(u32_t n)
Inserts n in the set.
Definition: PointsTo.cpp:157
const_iterator begin() const
Definition: PointsTo.h:128
bool test(u32_t n) const
Returns true if n is in this set.
Definition: PointsTo.cpp:131
SVFStmtSet & getGlobalSVFStmtSet()
Get global PAGEdges (not in a procedure)
Definition: SVFIR.h:236
SVFStmt::SVFStmtSetTy & getPTASVFStmtSet(SVFStmt::PEDGEK kind)
Get PTA edges set according to its kind.
Definition: SVFIR.h:193
NodeBS & getAllFieldsObjVars(const MemObj *obj)
Get all fields of an object.
Definition: SVFIR.cpp:477
GenericNode< SVFVar, SVFStmt >::GEdgeSetTy SVFStmtSetTy
iterator end() const
iterator begin() const
unsigned NodeID

Member Data Documentation

◆ ANNO_ALIAS

const u32_t SVF::MTAAnnotator::ANNO_ALIAS = 0x02
static

Definition at line 113 of file MTAAnnotator.h.

◆ ANNO_LOCAL

const u32_t SVF::MTAAnnotator::ANNO_LOCAL = 0x01
static

Definition at line 114 of file MTAAnnotator.h.

◆ ANNO_MHP

const u32_t SVF::MTAAnnotator::ANNO_MHP = 0x04
static

Constant INTERLEV_FLAG values.

Definition at line 112 of file MTAAnnotator.h.

◆ loadset

InstSet SVF::MTAAnnotator::loadset
private

Definition at line 91 of file MTAAnnotator.h.

◆ lsa

LockAnalysis* SVF::MTAAnnotator::lsa
private

Definition at line 90 of file MTAAnnotator.h.

◆ mhp

MHP* SVF::MTAAnnotator::mhp
private

Definition at line 89 of file MTAAnnotator.h.

◆ numOfAliasLd

u32_t SVF::MTAAnnotator::numOfAliasLd

Definition at line 104 of file MTAAnnotator.h.

◆ numOfAliasSt

u32_t SVF::MTAAnnotator::numOfAliasSt

Definition at line 103 of file MTAAnnotator.h.

◆ numOfAllLd

u32_t SVF::MTAAnnotator::numOfAllLd

Definition at line 100 of file MTAAnnotator.h.

◆ numOfAllSt

u32_t SVF::MTAAnnotator::numOfAllSt

Definition at line 99 of file MTAAnnotator.h.

◆ numOfAnnotatedLd

u32_t SVF::MTAAnnotator::numOfAnnotatedLd

Definition at line 108 of file MTAAnnotator.h.

◆ numOfAnnotatedSt

u32_t SVF::MTAAnnotator::numOfAnnotatedSt

Definition at line 107 of file MTAAnnotator.h.

◆ numOfMHPLd

u32_t SVF::MTAAnnotator::numOfMHPLd

Definition at line 106 of file MTAAnnotator.h.

◆ numOfMHPSt

u32_t SVF::MTAAnnotator::numOfMHPSt

Definition at line 105 of file MTAAnnotator.h.

◆ numOfNonLocalLd

u32_t SVF::MTAAnnotator::numOfNonLocalLd

Definition at line 102 of file MTAAnnotator.h.

◆ numOfNonLocalSt

u32_t SVF::MTAAnnotator::numOfNonLocalSt

Definition at line 101 of file MTAAnnotator.h.

◆ storeset

InstSet SVF::MTAAnnotator::storeset
private

Definition at line 92 of file MTAAnnotator.h.


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