Static Value-Flow Analysis
MTAAnnotator.cpp
Go to the documentation of this file.
1 /*
2  * MTAAnnotator.cpp
3  *
4  * Created on: May 4, 2014
5  * Author: Yulei Sui, Peng Di
6  */
7 
8 #include "Util/Options.h"
9 #include "MTAAnnotator.h"
10 #include "MTA/LockAnalysis.h"
11 #include "MemoryModel/PointsTo.h"
12 #include <sstream>
13 
14 using namespace SVF;
15 using namespace SVFUtil;
16 
17 
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  {
27  numOfAnnotatedSt++;
28  addMDTag(inst, st->getPointerOperand(), rawstr.str());
29  }
30  else if (LoadInst* ld = SVFUtil::dyn_cast<LoadInst>(inst))
31  {
32  numOfAnnotatedLd++;
33  addMDTag(inst, ld->getPointerOperand(), rawstr.str());
34  }
35 }
36 
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 }
73 
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 }
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 }
106 
108 {
109  mhp = m;
110  lsa = la;
111  if (!Options::AnnoFlag())
112  return;
113  collectLoadStoreInst(mhp->getTCT()->getPTA()->getModule());
114 }
115 
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  {
172  PointsTo pts = pta->getPts(pag->getValueNode(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(getStoreOperand(*it))));
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  {
185  PointsTo pts = pta->getPts(pag->getValueNode(LLVMModuleSet::getLLVMModuleSet()->getSVFValue(getLoadOperand(*it))));
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 }
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  {
220  const SVFValue* v1 = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(getStoreOperand(*it1));
221  const SVFValue* v2 = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(getStoreOperand(*it2));
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  {
246  const SVFValue* v1 = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(getStoreOperand(*it1));
247  const SVFValue* v2 = LLVMModuleSet::getLLVMModuleSet()->getSVFValue(getLoadOperand(*it2));
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 }
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 }
#define F(f)
#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
const char *const string
Definition: cJSON.h:172
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 LLVMModuleSet * getLLVMModuleSet()
Definition: LLVMModule.h:105
SVFInstruction * getSVFInstruction(const Instruction *inst) const
Definition: LLVMModule.h:224
SVFValue * getSVFValue(const Value *value)
Definition: MHP.h:46
void pruneAliasMHP(PointerAnalysis *pta)
Prune candidate instructions that non-mhp and non-alias with others.
void initialize(MHP *mhp, LockAnalysis *lsa)
Initialize.
const Value * getLoadOperand(const Instruction *inst)
const Value * getStoreOperand(const Instruction *inst)
Get operand of store and load.
void performAnnotate()
Perform annotation.
void annotateDRCheck(Instruction *inst)
Annotation.
Set< const Instruction * > InstSet
Definition: MTAAnnotator.h:27
void collectLoadStoreInst(SVFModule *mod)
Collect all load and store instruction.
void pruneThreadLocal(PointerAnalysis *pta)
Prune candidate instructions that are thread local.
static const Option< u32_t > AnnoFlag
Definition: Options.h:172
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.
virtual AliasResult alias(const SVFValue *V1, const SVFValue *V2)=0
Interface exposed to users of our pointer analysis, given Value infos.
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
bool isExtCall(const SVFFunction *fun)
Definition: SVFUtil.h:309
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
for isBitcode
Definition: BasicTypes.h:68
llvm::const_inst_iterator const_inst_iterator
Definition: BasicTypes.h:250
unsigned NodeID
llvm::LoadInst LoadInst
Definition: BasicTypes.h:149
llvm::Instruction Instruction
Definition: BasicTypes.h:87
llvm::Value Value
LLVM Basic classes.
Definition: BasicTypes.h:82
llvm::Module Module
Definition: BasicTypes.h:84
llvm::StoreInst StoreInst
Definition: BasicTypes.h:148