Static Value-Flow Analysis
CxtStmt.h
Go to the documentation of this file.
1 //===- CxtStmt.h -- Context- and Thread-Sensitive Statement-------------------//
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 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  * CxtStmt.h
25  *
26  * Created on: Jan 21, 2014
27  * Author: Yulei Sui
28  */
29 
30 #ifndef INCLUDE_UTIL_CXTSTMT_H_
31 #define INCLUDE_UTIL_CXTSTMT_H_
32 
33 #include "SVFIR/SVFValue.h"
34 
35 namespace SVF
36 {
37 
38 class ICFGNode;
42 class CxtStmt
43 {
44 public:
46  CxtStmt(const CallStrCxt& c, const ICFGNode* f) :cxt(c), inst(f)
47  {
48  }
50  CxtStmt(const CxtStmt& ctm) : cxt(ctm.getContext()),inst(ctm.getStmt())
51  {
52  }
54  virtual ~CxtStmt()
55  {
56  }
58  inline const CallStrCxt& getContext() const
59  {
60  return cxt;
61  }
63  inline const ICFGNode* getStmt() const
64  {
65  return inst;
66  }
69  inline bool operator< (const CxtStmt& rhs) const
70  {
71  if(inst!=rhs.getStmt())
72  return inst < rhs.getStmt();
73  else
74  return cxt < rhs.getContext();
75  }
77  inline CxtStmt& operator= (const CxtStmt& rhs)
78  {
79  if(*this!=rhs)
80  {
81  inst = rhs.getStmt();
82  cxt = rhs.getContext();
83  }
84  return *this;
85  }
87  inline bool operator== (const CxtStmt& rhs) const
88  {
89  return (inst == rhs.getStmt() && cxt == rhs.getContext());
90  }
92  inline bool operator!= (const CxtStmt& rhs) const
93  {
94  return !(*this==rhs);
95  }
97  inline std::string cxtToStr() const
98  {
99  std::string str;
100  std::stringstream rawstr(str);
101  rawstr << "[:";
102  for(CallStrCxt::const_iterator it = cxt.begin(), eit = cxt.end(); it!=eit; ++it)
103  {
104  rawstr << *it << " ";
105  }
106  rawstr << " ]";
107  return rawstr.str();
108  }
110  inline void dump() const
111  {
112  SVFUtil::outs() << "[ Current Stmt: " << inst->toString() << "\t Contexts: " << cxtToStr() << " ]\n";
113  }
114 
115 protected:
117  const ICFGNode* inst;
118 };
119 
120 
124 class CxtThreadStmt : public CxtStmt
125 {
126 public:
128  CxtThreadStmt(NodeID t, const CallStrCxt& c, const ICFGNode* f) :CxtStmt(c,f), tid(t)
129  {
130  }
132  CxtThreadStmt(const CxtThreadStmt& ctm) :CxtStmt(ctm), tid(ctm.getTid())
133  {
134  }
136  virtual ~CxtThreadStmt()
137  {
138  }
140  inline NodeID getTid() const
141  {
142  return tid;
143  }
146  inline bool operator< (const CxtThreadStmt& rhs) const
147  {
148  if (tid != rhs.getTid())
149  return tid < rhs.getTid();
150  else if(inst!=rhs.getStmt())
151  return inst < rhs.getStmt();
152  else
153  return cxt < rhs.getContext();
154  }
157  {
158  if(*this!=rhs)
159  {
160  CxtStmt::operator=(rhs);
161  tid = rhs.getTid();
162  }
163  return *this;
164  }
166  inline bool operator== (const CxtThreadStmt& rhs) const
167  {
168  return (tid == rhs.getTid() && inst == rhs.getStmt() && cxt == rhs.getContext());
169  }
171  inline bool operator!= (const CxtThreadStmt& rhs) const
172  {
173  return !(*this==rhs);
174  }
176  inline void dump() const
177  {
178  SVFUtil::outs() << "[ Current Thread id: " << tid << " Stmt: " << inst->toString() << "\t Contexts: " << cxtToStr() << " ]\n";
179  }
180 
181 private:
183 };
184 
185 
190 {
191 public:
193  CxtThread(const CallStrCxt& c, const ICFGNode* fork) : cxt(c), forksite(fork), inloop(false), incycle(false)
194  {
195  }
197  CxtThread(const CxtThread& ct) :
198  cxt(ct.getContext()), forksite(ct.getThread()), inloop(ct.isInloop()), incycle(ct.isIncycle())
199  {
200  }
202  virtual ~CxtThread()
203  {
204  }
206  inline const CallStrCxt& getContext() const
207  {
208  return cxt;
209  }
211  inline const ICFGNode* getThread() const
212  {
213  return forksite;
214  }
217  inline bool operator< (const CxtThread& rhs) const
218  {
219  if (forksite != rhs.getThread())
220  return forksite < rhs.getThread();
221  else
222  return cxt < rhs.getContext();
223  }
225  inline CxtThread& operator= (const CxtThread& rhs)
226  {
227  if(*this!=rhs)
228  {
229  forksite = rhs.getThread();
230  cxt = rhs.getContext();
231  }
232  return *this;
233  }
235  inline bool operator== (const CxtThread& rhs) const
236  {
237  return (forksite == rhs.getThread() && cxt == rhs.getContext());
238  }
240  inline bool operator!= (const CxtThread& rhs) const
241  {
242  return !(*this==rhs);
243  }
245  inline std::string cxtToStr() const
246  {
247  std::string str;
248  std::stringstream rawstr(str);
249  rawstr << "[:";
250  for(CallStrCxt::const_iterator it = cxt.begin(), eit = cxt.end(); it!=eit; ++it)
251  {
252  rawstr << *it << " ";
253  }
254  rawstr << " ]";
255  return rawstr.str();
256  }
257 
259 
260  inline void setInloop(bool in)
261  {
262  inloop = in;
263  }
264  inline bool isInloop() const
265  {
266  return inloop;
267  }
268  inline void setIncycle(bool in)
269  {
270  incycle = in;
271  }
272  inline bool isIncycle() const
273  {
274  return incycle;
275  }
277 
279  inline void dump() const
280  {
281  std::string loop = inloop?", inloop":"";
282  std::string cycle = incycle?", incycle":"";
283 
284  if(forksite)
285  {
286  SVFUtil::outs() << "[ Thread: "
287  << forksite->toString() << "\t Contexts: " << cxtToStr()
288  << loop << cycle <<" ]\n";
289  }
290  else
291  {
292  SVFUtil::outs() << "[ Thread: " << "main " << "\t Contexts: " << cxtToStr()
293  << loop << cycle <<" ]\n";
294  }
295  }
296 protected:
299  bool inloop;
300  bool incycle;
301 };
302 
303 
309 class CxtProc
310 {
311 public:
313  CxtProc(const CallStrCxt& c, const SVFFunction* f) :
314  cxt(c), fun(f)
315  {
316  }
318  CxtProc(const CxtProc& ctm) :
319  cxt(ctm.getContext()), fun(ctm.getProc())
320  {
321  }
323  virtual ~CxtProc()
324  {
325  }
327  inline const SVFFunction* getProc() const
328  {
329  return fun;
330  }
332  inline const CallStrCxt& getContext() const
333  {
334  return cxt;
335  }
338  inline bool operator<(const CxtProc& rhs) const
339  {
340  if (fun != rhs.getProc())
341  return fun < rhs.getProc();
342  else
343  return cxt < rhs.getContext();
344  }
346  inline CxtProc& operator=(const CxtProc& rhs)
347  {
348  if (*this != rhs)
349  {
350  fun = rhs.getProc();
351  cxt = rhs.getContext();
352  }
353  return *this;
354  }
356  inline bool operator==(const CxtProc& rhs) const
357  {
358  return (fun == rhs.getProc() && cxt == rhs.getContext());
359  }
361  inline bool operator!=(const CxtProc& rhs) const
362  {
363  return !(*this == rhs);
364  }
366  inline std::string cxtToStr() const
367  {
368  std::string str;
369  std::stringstream rawstr(str);
370  rawstr << "[:";
371  for (CallStrCxt::const_iterator it = cxt.begin(), eit = cxt.end(); it != eit; ++it)
372  {
373  rawstr << *it << " ";
374  }
375  rawstr << " ]";
376  return rawstr.str();
377  }
379  inline void dump() const
380  {
381  SVFUtil::outs() << "[ Proc: " << fun->getName() << "\t Contexts: " << cxtToStr() << " ]\n";
382  }
383 
384 protected:
386  const SVFFunction* fun;
387 };
388 
389 
396 class CxtThreadProc : public CxtProc
397 {
398 public:
400  CxtThreadProc(NodeID t, const CallStrCxt& c, const SVFFunction* f) :CxtProc(c,f),tid(t)
401  {
402  }
404  CxtThreadProc(const CxtThreadProc& ctm) : CxtProc(ctm.getContext(),ctm.getProc()), tid(ctm.getTid())
405  {
406  }
408  virtual ~CxtThreadProc()
409  {
410  }
412  inline NodeID getTid() const
413  {
414  return tid;
415  }
418  inline bool operator< (const CxtThreadProc& rhs) const
419  {
420  if (tid != rhs.getTid())
421  return tid < rhs.getTid();
422  else if(fun!=rhs.getProc())
423  return fun < rhs.getProc();
424  else
425  return cxt < rhs.getContext();
426  }
429  {
430  if(*this!=rhs)
431  {
432  tid = rhs.getTid();
433  fun = rhs.getProc();
434  cxt = rhs.getContext();
435  }
436  return *this;
437  }
439  inline bool operator== (const CxtThreadProc& rhs) const
440  {
441  return (tid == rhs.getTid() && fun == rhs.getProc() && cxt == rhs.getContext());
442  }
444  inline bool operator!= (const CxtThreadProc& rhs) const
445  {
446  return !(*this==rhs);
447  }
449  inline void dump() const
450  {
451  SVFUtil::outs() << "[ Current Thread id: " << tid << " Proc: " << fun->getName() << "\t Contexts: " << cxtToStr() << " ]\n";
452  }
453 
454 private:
456 };
457 
458 } // End namespace SVF
459 // Specialise has for class defined in this header file
460 template <> struct std::hash<SVF::CxtThread>
461 {
462  size_t operator()(const SVF::CxtThread& cs) const
463  {
464  std::hash<SVF::CallStrCxt> h;
465  return h(cs.getContext());
466  }
467 };
468 template <> struct std::hash<SVF::CxtThreadProc>
469 {
470  size_t operator()(const SVF::CxtThreadProc& ctp) const
471  {
472  std::hash<SVF::NodeID> h;
473  return h(ctp.getTid());
474  }
475 };
476 template <> struct std::hash<SVF::CxtThreadStmt>
477 {
478  size_t operator()(const SVF::CxtThreadStmt& cts) const
479  {
480  std::hash<SVF::NodeID> h;
481  return h(cts.getTid());
482  }
483 };
484 template <> struct std::hash<SVF::CxtStmt>
485 {
486  size_t operator()(const SVF::CxtStmt& cs) const
487  {
488  std::hash<SVF::ICFGNode*> h;
489  SVF::ICFGNode* inst = const_cast<SVF::ICFGNode*> (cs.getStmt());
490  return h(inst);
491  }
492 };
493 template <> struct std::hash<SVF::CxtProc>
494 {
495  size_t operator()(const SVF::CxtProc& cs) const
496  {
497  std::hash<SVF::SVFFunction*> h;
498  SVF::SVFFunction* fun = const_cast<SVF::SVFFunction*> (cs.getProc());
499  return h(fun);
500  }
501 };
502 #endif /* INCLUDE_UTIL_CXTSTMT_H_ */
#define false
Definition: cJSON.cpp:70
const char *const string
Definition: cJSON.h:172
bool operator!=(const CxtProc &rhs) const
Overloading operator==.
Definition: CxtStmt.h:361
void dump() const
Dump CxtProc.
Definition: CxtStmt.h:379
CxtProc(const CxtProc &ctm)
Copy constructor.
Definition: CxtStmt.h:318
CxtProc & operator=(const CxtProc &rhs)
Overloading operator=.
Definition: CxtStmt.h:346
const SVFFunction * fun
Definition: CxtStmt.h:386
bool operator==(const CxtProc &rhs) const
Overloading operator==.
Definition: CxtStmt.h:356
virtual ~CxtProc()
Destructor.
Definition: CxtStmt.h:323
const CallStrCxt & getContext() const
Return current context.
Definition: CxtStmt.h:332
bool operator<(const CxtProc &rhs) const
Definition: CxtStmt.h:338
const SVFFunction * getProc() const
Return current procedure.
Definition: CxtStmt.h:327
CxtProc(const CallStrCxt &c, const SVFFunction *f)
Constructor.
Definition: CxtStmt.h:313
std::string cxtToStr() const
Return context in string format.
Definition: CxtStmt.h:366
CallStrCxt cxt
Definition: CxtStmt.h:385
CxtStmt(const CallStrCxt &c, const ICFGNode *f)
Constructor.
Definition: CxtStmt.h:46
bool operator!=(const CxtStmt &rhs) const
Overloading operator==.
Definition: CxtStmt.h:92
const CallStrCxt & getContext() const
Return current context.
Definition: CxtStmt.h:58
virtual ~CxtStmt()
Destructor.
Definition: CxtStmt.h:54
CxtStmt & operator=(const CxtStmt &rhs)
Overloading operator=.
Definition: CxtStmt.h:77
void dump() const
Dump CxtStmt.
Definition: CxtStmt.h:110
bool operator<(const CxtStmt &rhs) const
Definition: CxtStmt.h:69
std::string cxtToStr() const
Return context in string format.
Definition: CxtStmt.h:97
const ICFGNode * inst
Definition: CxtStmt.h:117
CxtStmt(const CxtStmt &ctm)
Copy constructor.
Definition: CxtStmt.h:50
CallStrCxt cxt
Definition: CxtStmt.h:116
bool operator==(const CxtStmt &rhs) const
Overloading operator==.
Definition: CxtStmt.h:87
const ICFGNode * getStmt() const
Return current statement.
Definition: CxtStmt.h:63
CxtThreadProc(NodeID t, const CallStrCxt &c, const SVFFunction *f)
Constructor.
Definition: CxtStmt.h:400
CxtThreadProc & operator=(const CxtThreadProc &rhs)
Overloading operator=.
Definition: CxtStmt.h:428
bool operator<(const CxtThreadProc &rhs) const
Definition: CxtStmt.h:418
NodeID getTid() const
Return current thread id.
Definition: CxtStmt.h:412
bool operator!=(const CxtThreadProc &rhs) const
Overloading operator==.
Definition: CxtStmt.h:444
virtual ~CxtThreadProc()
Destructor.
Definition: CxtStmt.h:408
CxtThreadProc(const CxtThreadProc &ctm)
Copy constructor.
Definition: CxtStmt.h:404
void dump() const
Dump CxtThreadProc.
Definition: CxtStmt.h:449
bool operator==(const CxtThreadProc &rhs) const
Overloading operator==.
Definition: CxtStmt.h:439
CxtThreadStmt(NodeID t, const CallStrCxt &c, const ICFGNode *f)
Constructor.
Definition: CxtStmt.h:128
NodeID getTid() const
Return current context.
Definition: CxtStmt.h:140
void dump() const
Dump CxtThreadStmt.
Definition: CxtStmt.h:176
CxtThreadStmt & operator=(const CxtThreadStmt &rhs)
Overloading operator=.
Definition: CxtStmt.h:156
CxtThreadStmt(const CxtThreadStmt &ctm)
Copy constructor.
Definition: CxtStmt.h:132
bool operator!=(const CxtThreadStmt &rhs) const
Overloading operator==.
Definition: CxtStmt.h:171
virtual ~CxtThreadStmt()
Destructor.
Definition: CxtStmt.h:136
bool operator==(const CxtThreadStmt &rhs) const
Overloading operator==.
Definition: CxtStmt.h:166
bool operator<(const CxtThreadStmt &rhs) const
Definition: CxtStmt.h:146
bool isInloop() const
Definition: CxtStmt.h:264
CxtThread & operator=(const CxtThread &rhs)
Overloading operator=.
Definition: CxtStmt.h:225
bool isIncycle() const
Definition: CxtStmt.h:272
virtual ~CxtThread()
Destructor.
Definition: CxtStmt.h:202
bool operator<(const CxtThread &rhs) const
Definition: CxtStmt.h:217
void dump() const
Dump CxtThread.
Definition: CxtStmt.h:279
const ICFGNode * forksite
Definition: CxtStmt.h:298
CxtThread(const CallStrCxt &c, const ICFGNode *fork)
Constructor.
Definition: CxtStmt.h:193
const ICFGNode * getThread() const
Return forksite.
Definition: CxtStmt.h:211
const CallStrCxt & getContext() const
Return context of the thread.
Definition: CxtStmt.h:206
bool incycle
Definition: CxtStmt.h:300
std::string cxtToStr() const
Return context in string format.
Definition: CxtStmt.h:245
void setInloop(bool in)
inloop, incycle attributes
Definition: CxtStmt.h:260
CxtThread(const CxtThread &ct)
Copy constructor.
Definition: CxtStmt.h:197
bool operator!=(const CxtThread &rhs) const
Overloading operator==.
Definition: CxtStmt.h:240
void setIncycle(bool in)
Definition: CxtStmt.h:268
bool operator==(const CxtThread &rhs) const
Overloading operator==.
Definition: CxtStmt.h:235
CallStrCxt cxt
Definition: CxtStmt.h:297
virtual const std::string toString() const
Definition: ICFG.cpp:61
const std::string & getName() const
Definition: SVFValue.h:243
std::ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:50
for isBitcode
Definition: BasicTypes.h:68
u32_t NodeID
Definition: GeneralType.h:55
std::vector< u32_t > CallStrCxt
Definition: GeneralType.h:122
size_t operator()(const SVF::CxtProc &cs) const
Definition: CxtStmt.h:495
size_t operator()(const SVF::CxtStmt &cs) const
Definition: CxtStmt.h:486
size_t operator()(const SVF::CxtThreadProc &ctp) const
Definition: CxtStmt.h:470
size_t operator()(const SVF::CxtThreadStmt &cts) const
Definition: CxtStmt.h:478
size_t operator()(const SVF::CxtThread &cs) const
Definition: CxtStmt.h:462