Static Value-Flow Analysis
Loading...
Searching...
No Matches
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 "Util/GeneralType.h"
35#include <algorithm> // std::sort
36
37namespace SVF
38{
39
45class DPItem
46{
47protected:
50
51public:
54 {
55 }
58 {
59 }
62 {
63
64 }
67 {
68 if (this != &rhs)
69 {
70 cur = rhs.cur;
71 }
72 return *this;
73 }
75 virtual ~DPItem()
76 {
77 }
78 inline NodeID getCurNodeID() const
79 {
80 return cur;
81 }
82 inline void setCurNodeID(NodeID c)
83 {
84 cur = c;
85 }
87 static inline void setMaxBudget(u32_t max)
88 {
89 maximumBudget = max;
90 }
91 static inline u32_t getMaxBudget()
92 {
93 return maximumBudget;
94 }
97 inline bool operator< (const DPItem& rhs) const
98 {
99 return cur < rhs.cur;
100 }
102 inline DPItem& operator= (const DPItem& rhs)
103 {
104 if(*this!=rhs)
105 {
106 cur = rhs.cur;
107 }
108 return *this;
109 }
111 inline bool operator== (const DPItem& rhs) const
112 {
113 return (cur == rhs.cur);
114 }
116 inline bool operator!= (const DPItem& rhs) const
117 {
118 return !(*this == rhs);
119 }
120
121 inline void dump() const
122 {
123 SVFUtil::outs() << "var " << cur << "\n";
124 }
125};
126
127
131template<class LocCond>
132class StmtDPItem : public DPItem
133{
134
135
136protected:
138
139public:
147 {
148 }
150 virtual ~StmtDPItem()
151 {
152 }
154 inline const LocCond* getLoc() const
155 {
156 return this->curloc;
157 }
159 inline void setLoc(const LocCond* l)
160 {
161 this->curloc = l;
162 }
164 inline void setLocVar(const LocCond* l,NodeID v)
165 {
166 this->curloc = l;
167 this->cur = v;
168 }
171 inline bool operator< (const StmtDPItem& rhs) const
172 {
173 if (this->cur != rhs.cur)
174 return this->cur < rhs.cur;
175 else
176 return this->curloc < rhs.curloc;
177 }
180 {
181 if(*this!=rhs)
182 {
184 this->curloc = rhs.getLoc();
185 }
186 return *this;
187 }
189 inline bool operator== (const StmtDPItem& rhs) const
190 {
191 return (this->cur == rhs.cur && this->curloc == rhs.getLoc());
192 }
194 inline bool operator!= (const StmtDPItem& rhs) const
195 {
196 return !(*this==rhs);
197 }
198 inline void dump() const
199 {
200 SVFUtil::outs() << "statement " << *(this->curloc) << ", var " << this->cur << "\n";
201 }
202};
203
208{
209public:
210 typedef CallStrCxt::const_iterator const_iterator;
217 {
218 }
220 ContextCond(ContextCond &&cond) noexcept: context(std::move(cond.context)), concreteCxt(cond.concreteCxt) {}
223 {
224 if(this!=&cond)
225 {
226 context = std::move(cond.context);
227 concreteCxt = cond.concreteCxt;
228 }
229 return *this;
230 }
232 virtual ~ContextCond()
233 {
234 }
236 inline const CallStrCxt& getContexts() const
237 {
238 return context;
239 }
242 {
243 return context;
244 }
246 inline bool isConcreteCxt() const
247 {
248 return concreteCxt;
249 }
251 inline void setNonConcreteCxt()
252 {
253 concreteCxt = false;
254 }
256 inline bool containCallStr(NodeID cxt) const
257 {
258 return std::find(context.begin(),context.end(),cxt) != context.end();
259 }
261 inline u32_t cxtSize() const
262 {
263 return context.size();
264 }
266 static inline void setMaxCxtLen(u32_t max)
267 {
268 maximumCxtLen = max;
269 }
271 static inline void setMaxPathLen(u32_t max)
272 {
273 maximumPathLen = max;
274 }
275 inline u32_t getMaxPathLen() const
276 {
277 return maximumPathLen;
278 }
280 inline virtual bool pushContext(NodeID ctx)
281 {
282
283 if(context.size() < maximumCxtLen)
284 {
285 context.push_back(ctx);
286
287 if(context.size() > maximumCxt)
288 maximumCxt = context.size();
289 return true;
290 }
291 else
292 {
293 if(!context.empty())
294 {
296 context.erase(context.begin());
297 context.push_back(ctx);
298 }
299 return false;
300 }
301 }
302
304 inline virtual bool matchContext(NodeID ctx)
305 {
307 if(context.empty())
308 return true;
310 else if(context.back() == ctx)
311 {
312 context.pop_back();
313 return true;
314 }
315 return false;
316 }
317
320 inline bool operator< (const ContextCond& rhs) const
321 {
322 return context < rhs.context;
323 }
325 inline NodeID operator[] (const u32_t index) const
326 {
327 assert(index < context.size());
328 return context[index];
329 }
332 {
333 if(*this!=rhs)
334 {
336 concreteCxt = rhs.isConcreteCxt();
337 }
338 return *this;
339 }
341 inline bool operator== (const ContextCond& rhs) const
342 {
343 return (context == rhs.getContexts());
344 }
346 inline bool operator!= (const ContextCond& rhs) const
347 {
348 return !(*this==rhs);
349 }
351 inline const_iterator begin() const
352 {
353 return context.begin();
354 }
356 inline const_iterator end() const
357 {
358 return context.end();
359 }
361 inline std::string toString() const
362 {
363 std::string str;
364 std::stringstream rawstr(str);
365 rawstr << "[:";
366 for(CallStrCxt::const_iterator it = context.begin(), eit = context.end(); it!=eit; ++it)
367 {
368 rawstr << *it << " ";
369 }
370 rawstr << " ]";
371 return rawstr.str();
372 }
373protected:
378public:
381};
382
388
389template<class LocCond>
390class CxtStmtDPItem : public StmtDPItem<LocCond>
391{
392private:
394public:
396 CxtStmtDPItem(const CxtVar& var, const LocCond* locCond) : StmtDPItem<LocCond>(var.get_id(),locCond), context(var.get_cond())
397 {
398 }
406 {
407 }
409 inline CxtVar getCondVar() const
410 {
411 CxtVar var(this->context,this->cur);
412 return var;
413 }
415 inline const ContextCond& getCond() const
416 {
417 return this->context;
418 }
421 {
422 return this->context;
423 }
425 inline bool pushContext(NodeID cxt)
426 {
427 return this->context.pushContext(cxt);
428 }
429
431 inline bool matchContext(NodeID cxt)
432 {
433 return this->context.matchContext(cxt);
434 }
435
438 inline bool operator< (const CxtStmtDPItem<LocCond>& rhs) const
439 {
440 if (this->cur != rhs.cur)
441 return this->cur < rhs.cur;
442 else if(this->curloc != rhs.getLoc())
443 return this->curloc < rhs.getLoc();
444 else
445 return this->context < rhs.context;
446 }
449 {
450 if(*this!=rhs)
451 {
453 this->context = rhs.getCond();
454 }
455 return *this;
456 }
458 inline bool operator== (const CxtStmtDPItem<LocCond>& rhs) const
459 {
460 return (this->cur == rhs.cur && this->curloc == rhs.getLoc() && this->context == rhs.context);
461 }
463 inline bool operator!= (const CxtStmtDPItem<LocCond>& rhs) const
464 {
465 return !(*this==rhs);
466 }
467 inline void dump() const
468 {
469 SVFUtil::outs() << "statement " << *(this->curloc) << ", var " << this->cur << " ";
470 SVFUtil::outs() << this->context.toString() <<"\n";
471 }
472};
473
478class CxtDPItem : public DPItem
479{
480private:
482
483public:
486 {
487 }
488 CxtDPItem(const CxtVar& var) : DPItem(var.get_id()),context(var.get_cond())
489 {
490 }
497 CxtDPItem(CxtDPItem &&dps) noexcept: DPItem(dps), context(std::move(dps.context))
498 {
499 }
502 {
503 if (this != &dps)
504 {
506 context = std::move(dps.context);
507 }
508 return *this;
509 }
511 virtual ~CxtDPItem()
512 {
513 }
514
516 inline const ContextCond& getContexts() const
517 {
518 return context;
519 }
521 inline void pushContext(NodeID cxt)
522 {
523 context.pushContext(cxt);
524 }
525
527 inline virtual bool matchContext(NodeID cxt)
528 {
529 return context.matchContext(cxt);
530 }
531
534 inline bool operator< (const CxtDPItem& rhs) const
535 {
536 if (cur != rhs.cur)
537 return cur < rhs.cur;
538 else
539 return context < rhs.context;
540 }
543 {
544 if(*this!=rhs)
545 {
546 cur = rhs.cur;
548 }
549 return *this;
550 }
552 inline bool operator== (const CxtDPItem& rhs) const
553 {
554 return (cur == rhs.cur) && (context == rhs.context);
555 }
557 inline bool operator!= (const CxtDPItem& rhs) const
558 {
559 return !(*this == rhs);
560 }
561
562};
563} // End namespace SVF
564
566template <>
567struct std::hash<SVF::CxtDPItem>
568{
569 size_t operator()(const SVF::CxtDPItem &cdpi) const
570 {
572 return h(std::make_pair(cdpi.getCurNodeID(), cdpi.getContexts()));
573 }
574};
575
577template <typename LocCond>
578struct std::hash<SVF::StmtDPItem<LocCond>>
579{
580 size_t operator()(const SVF::StmtDPItem<LocCond> &sdpi) const
581 {
583 return h(std::make_pair(sdpi.getCurNodeID(), sdpi.getLoc()));
584 }
585};
586
588template<class LocCond>
589struct std::hash<SVF::CxtStmtDPItem<LocCond>>
590{
591 size_t operator()(const SVF::CxtStmtDPItem<LocCond> &csdpi) const
592 {
594 return h(std::make_pair(csdpi.getCurNodeID(),
595 std::make_pair(csdpi.getLoc(), csdpi.getCond())));
596 }
597};
598
600template <>
601struct std::hash<const SVF::ContextCond>
602{
603 size_t operator()(const SVF::ContextCond &cc) const
604 {
605 std::hash<SVF::CallStrCxt> h;
606 return h(cc.getContexts());
607 }
608};
609
610template <>
611struct std::hash<SVF::ContextCond>
612{
613 size_t operator()(const SVF::ContextCond &cc) const
614 {
615 std::hash<SVF::CallStrCxt> h;
616 return h(cc.getContexts());
617 }
618};
619#endif /* DPITEM_H_ */
#define true
Definition cJSON.cpp:65
int index
Definition cJSON.h:170
static void setMaxPathLen(u32_t max)
set max path limit
Definition DPItem.h:271
CallStrCxt::const_iterator const_iterator
Definition DPItem.h:210
static u32_t maximumCxt
Definition DPItem.h:379
u32_t cxtSize() const
Get context size.
Definition DPItem.h:261
static u32_t maximumCxtLen
Definition DPItem.h:375
bool operator!=(const ContextCond &rhs) const
Overloading operator!=.
Definition DPItem.h:346
const CallStrCxt & getContexts() const
Get context.
Definition DPItem.h:236
const_iterator begin() const
Begin iterators.
Definition DPItem.h:351
const_iterator end() const
End iterators.
Definition DPItem.h:356
bool operator<(const ContextCond &rhs) const
Definition DPItem.h:320
ContextCond(const ContextCond &cond)
Copy Constructor.
Definition DPItem.h:216
std::string toString() const
Dump context condition.
Definition DPItem.h:361
NodeID operator[](const u32_t index) const
Overloading operator[].
Definition DPItem.h:325
bool operator==(const ContextCond &rhs) const
Overloading operator==.
Definition DPItem.h:341
bool containCallStr(NodeID cxt) const
Whether contains callstring cxt.
Definition DPItem.h:256
ContextCond & operator=(ContextCond &&cond) noexcept
Move operator=.
Definition DPItem.h:222
static u32_t maximumPath
Definition DPItem.h:380
CallStrCxt & getContexts()
Get context.
Definition DPItem.h:241
static void setMaxCxtLen(u32_t max)
set max context limit
Definition DPItem.h:266
u32_t getMaxPathLen() const
Definition DPItem.h:275
ContextCond()
Constructor.
Definition DPItem.h:212
ContextCond(ContextCond &&cond) noexcept
Move Constructor.
Definition DPItem.h:220
static u32_t maximumPathLen
Definition DPItem.h:376
virtual bool matchContext(NodeID ctx)
Match context.
Definition DPItem.h:304
CallStrCxt context
Definition DPItem.h:374
bool isConcreteCxt() const
Whether it is an concrete context.
Definition DPItem.h:246
void setNonConcreteCxt()
Whether it is an concrete context.
Definition DPItem.h:251
virtual ~ContextCond()
Destructor.
Definition DPItem.h:232
virtual bool pushContext(NodeID ctx)
Push context.
Definition DPItem.h:280
CxtDPItem(const CxtVar &var)
Definition DPItem.h:488
void pushContext(NodeID cxt)
Push context.
Definition DPItem.h:521
bool operator==(const CxtDPItem &rhs) const
Overloading Operator==.
Definition DPItem.h:552
CxtDPItem(const CxtDPItem &dps)
Copy constructor.
Definition DPItem.h:492
CxtDPItem(NodeID c, const ContextCond &cxt)
Constructor.
Definition DPItem.h:485
bool operator<(const CxtDPItem &rhs) const
Definition DPItem.h:534
const ContextCond & getContexts() const
Get context.
Definition DPItem.h:516
CxtDPItem(CxtDPItem &&dps) noexcept
Move constructor.
Definition DPItem.h:497
virtual ~CxtDPItem()
Destructor.
Definition DPItem.h:511
ContextCond context
Definition DPItem.h:481
bool operator!=(const CxtDPItem &rhs) const
Overloading Operator!=.
Definition DPItem.h:557
virtual bool matchContext(NodeID cxt)
Match context.
Definition DPItem.h:527
CxtDPItem & operator=(CxtDPItem &&dps) noexcept
Move operator=.
Definition DPItem.h:501
virtual ~CxtStmtDPItem()
Destructor.
Definition DPItem.h:405
bool operator==(const CxtStmtDPItem< LocCond > &rhs) const
Overloading operator==.
Definition DPItem.h:458
bool operator!=(const CxtStmtDPItem< LocCond > &rhs) const
Overloading operator==.
Definition DPItem.h:463
bool operator<(const CxtStmtDPItem< LocCond > &rhs) const
Definition DPItem.h:438
bool matchContext(NodeID cxt)
Match context.
Definition DPItem.h:431
bool pushContext(NodeID cxt)
Push context.
Definition DPItem.h:425
CxtStmtDPItem(const CxtStmtDPItem< LocCond > &dps)
Copy constructor.
Definition DPItem.h:400
CxtStmtDPItem(const CxtVar &var, const LocCond *locCond)
Constructor.
Definition DPItem.h:396
ContextCond & getCond()
Get context.
Definition DPItem.h:420
ContextCond context
Definition DPItem.h:393
CxtVar getCondVar() const
Get context var.
Definition DPItem.h:409
void dump() const
Definition DPItem.h:467
CxtStmtDPItem< LocCond > & operator=(const CxtStmtDPItem< LocCond > &rhs)
Overloading operator=.
Definition DPItem.h:448
const ContextCond & getCond() const
Get context.
Definition DPItem.h:415
NodeID getCurNodeID() const
Definition DPItem.h:78
virtual ~DPItem()
Destructor.
Definition DPItem.h:75
static u64_t maximumBudget
Definition DPItem.h:49
void setCurNodeID(NodeID c)
Definition DPItem.h:82
void dump() const
Definition DPItem.h:121
bool operator<(const DPItem &rhs) const
Definition DPItem.h:97
static void setMaxBudget(u32_t max)
set max step budge per query
Definition DPItem.h:87
DPItem(const DPItem &dps)
Copy constructor.
Definition DPItem.h:57
NodeID cur
Definition DPItem.h:48
DPItem & operator=(DPItem &&rhs) noexcept
Move operator=.
Definition DPItem.h:66
DPItem(DPItem &&dps) noexcept
Move constructor.
Definition DPItem.h:61
DPItem(NodeID c)
Constructor.
Definition DPItem.h:53
bool operator==(const DPItem &rhs) const
Overloading Operator==.
Definition DPItem.h:111
bool operator!=(const DPItem &rhs) const
Overloading Operator!=.
Definition DPItem.h:116
static u32_t getMaxBudget()
Definition DPItem.h:91
StmtDPItem & operator=(const StmtDPItem &rhs)
Overloading operator==.
Definition DPItem.h:179
StmtDPItem(const StmtDPItem &dps)
Copy constructor.
Definition DPItem.h:145
const LocCond * getLoc() const
Get context.
Definition DPItem.h:154
void setLocVar(const LocCond *l, NodeID v)
Set location and pointer id.
Definition DPItem.h:164
void dump() const
Definition DPItem.h:198
virtual ~StmtDPItem()
Destructor.
Definition DPItem.h:150
void setLoc(const LocCond *l)
Set location.
Definition DPItem.h:159
const LocCond * curloc
Definition DPItem.h:137
bool operator!=(const StmtDPItem &rhs) const
Overloading operator!=.
Definition DPItem.h:194
StmtDPItem(NodeID c, const LocCond *locCond)
Constructor.
Definition DPItem.h:141
bool operator<(const StmtDPItem &rhs) const
Definition DPItem.h:171
bool operator==(const StmtDPItem &rhs) const
Overloading operator==.
Definition DPItem.h:189
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
for isBitcode
Definition BasicTypes.h:70
unsigned long long u64_t
Definition GeneralType.h:69
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
CondVar< ContextCond > CxtVar
Definition DPItem.h:386
std::vector< u32_t > CallStrCxt
Definition GeneralType.h:96
unsigned u32_t
Definition GeneralType.h:67
CondStdSet< CxtVar > CxtPtSet
Definition DPItem.h:387
size_t operator()(const SVF::ContextCond &cc) const
Definition DPItem.h:613
size_t operator()(const SVF::CxtDPItem &cdpi) const
Definition DPItem.h:569
size_t operator()(const SVF::CxtStmtDPItem< LocCond > &csdpi) const
Definition DPItem.h:591
size_t operator()(const SVF::StmtDPItem< LocCond > &sdpi) const
Definition DPItem.h:580
size_t operator()(const SVF::ContextCond &cc) const
Definition DPItem.h:603