Static Value-Flow Analysis
DPItem.h
Go to the documentation of this file.
1 //===- DPItem.h -- Context/path sensitive classes----------------------------//
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  * DPItem.h
25  *
26  * Created on: Apr 1, 2014
27  * Author: Yulei Sui
28  */
29 
30 #ifndef DPITEM_H_
31 #define DPITEM_H_
32 
34 #include <algorithm> // std::sort
35 
36 namespace SVF
37 {
38 
44 class DPItem
45 {
46 protected:
49 
50 public:
52  DPItem(NodeID c) : cur(c)
53  {
54  }
56  DPItem(const DPItem& dps) : cur(dps.cur)
57  {
58  }
60  DPItem(DPItem&& dps) noexcept : cur(dps.cur)
61  {
62 
63  }
65  DPItem &operator=(DPItem &&rhs) noexcept
66  {
67  if (this != &rhs)
68  {
69  cur = rhs.cur;
70  }
71  return *this;
72  }
74  virtual ~DPItem()
75  {
76  }
77  inline NodeID getCurNodeID() const
78  {
79  return cur;
80  }
81  inline void setCurNodeID(NodeID c)
82  {
83  cur = c;
84  }
86  static inline void setMaxBudget(u32_t max)
87  {
88  maximumBudget = max;
89  }
90  static inline u32_t getMaxBudget()
91  {
92  return maximumBudget;
93  }
96  inline bool operator< (const DPItem& rhs) const
97  {
98  return cur < rhs.cur;
99  }
101  inline DPItem& operator= (const DPItem& rhs)
102  {
103  if(*this!=rhs)
104  {
105  cur = rhs.cur;
106  }
107  return *this;
108  }
110  inline bool operator== (const DPItem& rhs) const
111  {
112  return (cur == rhs.cur);
113  }
115  inline bool operator!= (const DPItem& rhs) const
116  {
117  return !(*this == rhs);
118  }
119 
120  inline void dump() const
121  {
122  SVFUtil::outs() << "var " << cur << "\n";
123  }
124 };
125 
126 
130 template<class LocCond>
131 class StmtDPItem : public DPItem
132 {
133 
134 
135 protected:
136  const LocCond* curloc;
137 
138 public:
140  StmtDPItem(NodeID c, const LocCond* locCond) : DPItem(c), curloc(locCond)
141  {
142  }
144  StmtDPItem(const StmtDPItem& dps) :
145  DPItem(dps), curloc(dps.curloc)
146  {
147  }
149  virtual ~StmtDPItem()
150  {
151  }
153  inline const LocCond* getLoc() const
154  {
155  return this->curloc;
156  }
158  inline void setLoc(const LocCond* l)
159  {
160  this->curloc = l;
161  }
163  inline void setLocVar(const LocCond* l,NodeID v)
164  {
165  this->curloc = l;
166  this->cur = v;
167  }
170  inline bool operator< (const StmtDPItem& rhs) const
171  {
172  if (this->cur != rhs.cur)
173  return this->cur < rhs.cur;
174  else
175  return this->curloc < rhs.curloc;
176  }
178  inline StmtDPItem& operator= (const StmtDPItem& rhs)
179  {
180  if(*this!=rhs)
181  {
182  DPItem::operator=(rhs);
183  this->curloc = rhs.getLoc();
184  }
185  return *this;
186  }
188  inline bool operator== (const StmtDPItem& rhs) const
189  {
190  return (this->cur == rhs.cur && this->curloc == rhs.getLoc());
191  }
193  inline bool operator!= (const StmtDPItem& rhs) const
194  {
195  return !(*this==rhs);
196  }
197  inline void dump() const
198  {
199  SVFUtil::outs() << "statement " << *(this->curloc) << ", var " << this->cur << "\n";
200  }
201 };
202 
207 {
208 public:
209  typedef CallStrCxt::const_iterator const_iterator;
212  {
213  }
216  {
217  }
219  ContextCond(ContextCond &&cond) noexcept: context(std::move(cond.context)), concreteCxt(cond.concreteCxt) {}
222  {
223  if(this!=&cond)
224  {
225  context = std::move(cond.context);
226  concreteCxt = cond.concreteCxt;
227  }
228  return *this;
229  }
231  virtual ~ContextCond()
232  {
233  }
235  inline const CallStrCxt& getContexts() const
236  {
237  return context;
238  }
241  {
242  return context;
243  }
245  inline bool isConcreteCxt() const
246  {
247  return concreteCxt;
248  }
250  inline void setNonConcreteCxt()
251  {
252  concreteCxt = false;
253  }
255  inline bool containCallStr(NodeID cxt) const
256  {
257  return std::find(context.begin(),context.end(),cxt) != context.end();
258  }
260  inline u32_t cxtSize() const
261  {
262  return context.size();
263  }
265  static inline void setMaxCxtLen(u32_t max)
266  {
267  maximumCxtLen = max;
268  }
270  static inline void setMaxPathLen(u32_t max)
271  {
272  maximumPathLen = max;
273  }
274  inline u32_t getMaxPathLen() const
275  {
276  return maximumPathLen;
277  }
279  inline virtual bool pushContext(NodeID ctx)
280  {
281 
282  if(context.size() < maximumCxtLen)
283  {
284  context.push_back(ctx);
285 
286  if(context.size() > maximumCxt)
287  maximumCxt = context.size();
288  return true;
289  }
290  else
291  {
292  if(!context.empty())
293  {
295  context.erase(context.begin());
296  context.push_back(ctx);
297  }
298  return false;
299  }
300  }
301 
303  inline virtual bool matchContext(NodeID ctx)
304  {
306  if(context.empty())
307  return true;
309  else if(context.back() == ctx)
310  {
311  context.pop_back();
312  return true;
313  }
314  return false;
315  }
316 
319  inline bool operator< (const ContextCond& rhs) const
320  {
321  return context < rhs.context;
322  }
324  inline NodeID operator[] (const u32_t index) const
325  {
326  assert(index < context.size());
327  return context[index];
328  }
330  inline ContextCond& operator= (const ContextCond& rhs)
331  {
332  if(*this!=rhs)
333  {
334  context = rhs.getContexts();
335  concreteCxt = rhs.isConcreteCxt();
336  }
337  return *this;
338  }
340  inline bool operator== (const ContextCond& rhs) const
341  {
342  return (context == rhs.getContexts());
343  }
345  inline bool operator!= (const ContextCond& rhs) const
346  {
347  return !(*this==rhs);
348  }
350  inline const_iterator begin() const
351  {
352  return context.begin();
353  }
355  inline const_iterator end() const
356  {
357  return context.end();
358  }
360  inline std::string toString() const
361  {
362  std::string str;
363  std::stringstream rawstr(str);
364  rawstr << "[:";
365  for(CallStrCxt::const_iterator it = context.begin(), eit = context.end(); it!=eit; ++it)
366  {
367  rawstr << *it << " ";
368  }
369  rawstr << " ]";
370  return rawstr.str();
371  }
372 protected:
377 public:
380 };
381 
387 
388 template<class LocCond>
389 class CxtStmtDPItem : public StmtDPItem<LocCond>
390 {
391 private:
393 public:
395  CxtStmtDPItem(const CxtVar& var, const LocCond* locCond) : StmtDPItem<LocCond>(var.get_id(),locCond), context(var.get_cond())
396  {
397  }
400  StmtDPItem<LocCond>(dps), context(dps.context)
401  {
402  }
404  virtual ~CxtStmtDPItem()
405  {
406  }
408  inline CxtVar getCondVar() const
409  {
410  CxtVar var(this->context,this->cur);
411  return var;
412  }
414  inline const ContextCond& getCond() const
415  {
416  return this->context;
417  }
420  {
421  return this->context;
422  }
424  inline bool pushContext(NodeID cxt)
425  {
426  return this->context.pushContext(cxt);
427  }
428 
430  inline bool matchContext(NodeID cxt)
431  {
432  return this->context.matchContext(cxt);
433  }
434 
437  inline bool operator< (const CxtStmtDPItem<LocCond>& rhs) const
438  {
439  if (this->cur != rhs.cur)
440  return this->cur < rhs.cur;
441  else if(this->curloc != rhs.getLoc())
442  return this->curloc < rhs.getLoc();
443  else
444  return this->context < rhs.context;
445  }
448  {
449  if(*this!=rhs)
450  {
452  this->context = rhs.getCond();
453  }
454  return *this;
455  }
457  inline bool operator== (const CxtStmtDPItem<LocCond>& rhs) const
458  {
459  return (this->cur == rhs.cur && this->curloc == rhs.getLoc() && this->context == rhs.context);
460  }
462  inline bool operator!= (const CxtStmtDPItem<LocCond>& rhs) const
463  {
464  return !(*this==rhs);
465  }
466  inline void dump() const
467  {
468  SVFUtil::outs() << "statement " << *(this->curloc) << ", var " << this->cur << " ";
469  SVFUtil::outs() << this->context.toString() <<"\n";
470  }
471 };
472 
476 typedef CondVar<ContextCond> CxtVar;
477 class CxtDPItem : public DPItem
478 {
479 private:
481 
482 public:
484  CxtDPItem(NodeID c, const ContextCond& cxt) : DPItem(c),context(cxt)
485  {
486  }
487  CxtDPItem(const CxtVar& var) : DPItem(var.get_id()),context(var.get_cond())
488  {
489  }
491  CxtDPItem(const CxtDPItem& dps) :
492  DPItem(dps.getCurNodeID()), context(dps.context)
493  {
494  }
496  CxtDPItem(CxtDPItem &&dps) noexcept: DPItem(dps), context(std::move(dps.context))
497  {
498  }
500  CxtDPItem &operator=(CxtDPItem &&dps) noexcept
501  {
502  if (this != &dps)
503  {
504  DPItem::operator=(dps);
505  context = std::move(dps.context);
506  }
507  return *this;
508  }
510  virtual ~CxtDPItem()
511  {
512  }
513 
515  inline const ContextCond& getContexts() const
516  {
517  return context;
518  }
520  inline void pushContext(NodeID cxt)
521  {
522  context.pushContext(cxt);
523  }
524 
526  inline virtual bool matchContext(NodeID cxt)
527  {
528  return context.matchContext(cxt);
529  }
530 
533  inline bool operator< (const CxtDPItem& rhs) const
534  {
535  if (cur != rhs.cur)
536  return cur < rhs.cur;
537  else
538  return context < rhs.context;
539  }
541  inline CxtDPItem& operator= (const CxtDPItem& rhs)
542  {
543  if(*this!=rhs)
544  {
545  cur = rhs.cur;
546  context = rhs.context;
547  }
548  return *this;
549  }
551  inline bool operator== (const CxtDPItem& rhs) const
552  {
553  return (cur == rhs.cur) && (context == rhs.context);
554  }
556  inline bool operator!= (const CxtDPItem& rhs) const
557  {
558  return !(*this == rhs);
559  }
560 
561 };
562 } // End namespace SVF
563 
565 template <>
566 struct std::hash<SVF::CxtDPItem>
567 {
568  size_t operator()(const SVF::CxtDPItem &cdpi) const
569  {
571  return h(std::make_pair(cdpi.getCurNodeID(), cdpi.getContexts()));
572  }
573 };
574 
576 template <typename LocCond>
577 struct std::hash<SVF::StmtDPItem<LocCond>>
578 {
579  size_t operator()(const SVF::StmtDPItem<LocCond> &sdpi) const
580  {
582  return h(std::make_pair(sdpi.getCurNodeID(), sdpi.getLoc()));
583  }
584 };
585 
587 template<class LocCond>
588 struct std::hash<SVF::CxtStmtDPItem<LocCond>>
589 {
590  size_t operator()(const SVF::CxtStmtDPItem<LocCond> &csdpi) const
591  {
593  return h(std::make_pair(csdpi.getCurNodeID(),
594  std::make_pair(csdpi.getLoc(), csdpi.getCond())));
595  }
596 };
597 
599 template <>
600 struct std::hash<const SVF::ContextCond>
601 {
602  size_t operator()(const SVF::ContextCond &cc) const
603  {
604  std::hash<SVF::CallStrCxt> h;
605  return h(cc.getContexts());
606  }
607 };
608 
609 template <>
610 struct std::hash<SVF::ContextCond>
611 {
612  size_t operator()(const SVF::ContextCond &cc) const
613  {
614  std::hash<SVF::CallStrCxt> h;
615  return h(cc.getContexts());
616  }
617 };
618 #endif /* DPITEM_H_ */
return true
Definition: cJSON.cpp:2295
int index
Definition: cJSON.h:170
const char *const string
Definition: cJSON.h:172
static void setMaxPathLen(u32_t max)
set max path limit
Definition: DPItem.h:270
CallStrCxt::const_iterator const_iterator
Definition: DPItem.h:209
static u32_t maximumCxt
Definition: DPItem.h:378
u32_t cxtSize() const
Get context size.
Definition: DPItem.h:260
CallStrCxt & getContexts()
Get context.
Definition: DPItem.h:240
static u32_t maximumCxtLen
Definition: DPItem.h:374
bool operator!=(const ContextCond &rhs) const
Overloading operator!=.
Definition: DPItem.h:345
bool concreteCxt
Definition: DPItem.h:376
const_iterator begin() const
Begin iterators.
Definition: DPItem.h:350
const_iterator end() const
End iterators.
Definition: DPItem.h:355
bool operator<(const ContextCond &rhs) const
Definition: DPItem.h:319
ContextCond(const ContextCond &cond)
Copy Constructor.
Definition: DPItem.h:215
std::string toString() const
Dump context condition.
Definition: DPItem.h:360
NodeID operator[](const u32_t index) const
Overloading operator[].
Definition: DPItem.h:324
bool operator==(const ContextCond &rhs) const
Overloading operator==.
Definition: DPItem.h:340
bool containCallStr(NodeID cxt) const
Whether contains callstring cxt.
Definition: DPItem.h:255
const CallStrCxt & getContexts() const
Get context.
Definition: DPItem.h:235
static u32_t maximumPath
Definition: DPItem.h:379
ContextCond & operator=(ContextCond &&cond) noexcept
Move operator=.
Definition: DPItem.h:221
static void setMaxCxtLen(u32_t max)
set max context limit
Definition: DPItem.h:265
u32_t getMaxPathLen() const
Definition: DPItem.h:274
ContextCond()
Constructor.
Definition: DPItem.h:211
ContextCond(ContextCond &&cond) noexcept
Move Constructor.
Definition: DPItem.h:219
static u32_t maximumPathLen
Definition: DPItem.h:375
virtual bool matchContext(NodeID ctx)
Match context.
Definition: DPItem.h:303
CallStrCxt context
Definition: DPItem.h:373
bool isConcreteCxt() const
Whether it is an concrete context.
Definition: DPItem.h:245
void setNonConcreteCxt()
Whether it is an concrete context.
Definition: DPItem.h:250
virtual ~ContextCond()
Destructor.
Definition: DPItem.h:231
virtual bool pushContext(NodeID ctx)
Push context.
Definition: DPItem.h:279
CxtDPItem(const CxtVar &var)
Definition: DPItem.h:487
const ContextCond & getContexts() const
Get context.
Definition: DPItem.h:515
void pushContext(NodeID cxt)
Push context.
Definition: DPItem.h:520
CxtDPItem & operator=(CxtDPItem &&dps) noexcept
Move operator=.
Definition: DPItem.h:500
bool operator==(const CxtDPItem &rhs) const
Overloading Operator==.
Definition: DPItem.h:551
CxtDPItem(const CxtDPItem &dps)
Copy constructor.
Definition: DPItem.h:491
CxtDPItem(NodeID c, const ContextCond &cxt)
Constructor.
Definition: DPItem.h:484
bool operator<(const CxtDPItem &rhs) const
Definition: DPItem.h:533
CxtDPItem(CxtDPItem &&dps) noexcept
Move constructor.
Definition: DPItem.h:496
virtual ~CxtDPItem()
Destructor.
Definition: DPItem.h:510
ContextCond context
Definition: DPItem.h:480
bool operator!=(const CxtDPItem &rhs) const
Overloading Operator!=.
Definition: DPItem.h:556
virtual bool matchContext(NodeID cxt)
Match context.
Definition: DPItem.h:526
virtual ~CxtStmtDPItem()
Destructor.
Definition: DPItem.h:404
bool operator==(const CxtStmtDPItem< LocCond > &rhs) const
Overloading operator==.
Definition: DPItem.h:457
bool operator!=(const CxtStmtDPItem< LocCond > &rhs) const
Overloading operator==.
Definition: DPItem.h:462
bool operator<(const CxtStmtDPItem< LocCond > &rhs) const
Definition: DPItem.h:437
bool matchContext(NodeID cxt)
Match context.
Definition: DPItem.h:430
bool pushContext(NodeID cxt)
Push context.
Definition: DPItem.h:424
CxtStmtDPItem(const CxtStmtDPItem< LocCond > &dps)
Copy constructor.
Definition: DPItem.h:399
CxtStmtDPItem(const CxtVar &var, const LocCond *locCond)
Constructor.
Definition: DPItem.h:395
ContextCond context
Definition: DPItem.h:392
ContextCond & getCond()
Get context.
Definition: DPItem.h:419
CxtVar getCondVar() const
Get context var.
Definition: DPItem.h:408
void dump() const
Definition: DPItem.h:466
const ContextCond & getCond() const
Get context.
Definition: DPItem.h:414
CxtStmtDPItem< LocCond > & operator=(const CxtStmtDPItem< LocCond > &rhs)
Overloading operator=.
Definition: DPItem.h:447
NodeID getCurNodeID() const
Definition: DPItem.h:77
virtual ~DPItem()
Destructor.
Definition: DPItem.h:74
DPItem & operator=(DPItem &&rhs) noexcept
Move operator=.
Definition: DPItem.h:65
static u64_t maximumBudget
Definition: DPItem.h:48
void setCurNodeID(NodeID c)
Definition: DPItem.h:81
void dump() const
Definition: DPItem.h:120
bool operator<(const DPItem &rhs) const
Definition: DPItem.h:96
static void setMaxBudget(u32_t max)
set max step budge per query
Definition: DPItem.h:86
DPItem(const DPItem &dps)
Copy constructor.
Definition: DPItem.h:56
NodeID cur
Definition: DPItem.h:47
DPItem(DPItem &&dps) noexcept
Move constructor.
Definition: DPItem.h:60
DPItem(NodeID c)
Constructor.
Definition: DPItem.h:52
bool operator==(const DPItem &rhs) const
Overloading Operator==.
Definition: DPItem.h:110
bool operator!=(const DPItem &rhs) const
Overloading Operator!=.
Definition: DPItem.h:115
static u32_t getMaxBudget()
Definition: DPItem.h:90
StmtDPItem & operator=(const StmtDPItem &rhs)
Overloading operator==.
Definition: DPItem.h:178
const LocCond * getLoc() const
Get context.
Definition: DPItem.h:153
StmtDPItem(const StmtDPItem &dps)
Copy constructor.
Definition: DPItem.h:144
void setLocVar(const LocCond *l, NodeID v)
Set location and pointer id.
Definition: DPItem.h:163
void dump() const
Definition: DPItem.h:197
virtual ~StmtDPItem()
Destructor.
Definition: DPItem.h:149
void setLoc(const LocCond *l)
Set location.
Definition: DPItem.h:158
const LocCond * curloc
Definition: DPItem.h:136
bool operator!=(const StmtDPItem &rhs) const
Overloading operator!=.
Definition: DPItem.h:193
StmtDPItem(NodeID c, const LocCond *locCond)
Constructor.
Definition: DPItem.h:140
bool operator<(const StmtDPItem &rhs) const
Definition: DPItem.h:170
bool operator==(const StmtDPItem &rhs) const
Overloading operator==.
Definition: DPItem.h:188
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
Definition: SVFUtil.h:447
std::ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:50
for isBitcode
Definition: BasicTypes.h:68
unsigned long long u64_t
Definition: GeneralType.h:48
u32_t NodeID
Definition: GeneralType.h:55
CondVar< ContextCond > CxtVar
Definition: DPItem.h:385
std::vector< u32_t > CallStrCxt
Definition: GeneralType.h:122
unsigned u32_t
Definition: GeneralType.h:46
CondStdSet< CxtVar > CxtPtSet
Definition: DPItem.h:386
provide extra hash function for std::pair handling
Definition: GeneralType.h:85
size_t operator()(const SVF::ContextCond &cc) const
Definition: DPItem.h:612
size_t operator()(const SVF::CxtDPItem &cdpi) const
Definition: DPItem.h:568
size_t operator()(const SVF::CxtStmtDPItem< LocCond > &csdpi) const
Definition: DPItem.h:590
size_t operator()(const SVF::StmtDPItem< LocCond > &sdpi) const
Definition: DPItem.h:579
size_t operator()(const SVF::ContextCond &cc) const
Definition: DPItem.h:602