Static Value-Flow Analysis
Loading...
Searching...
No Matches
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
33namespace SVF
34{
35
36class ICFGNode;
41{
42public:
44 CxtStmt(const CallStrCxt& c, const ICFGNode* f) :cxt(c), inst(f)
45 {
46 }
49 {
50 }
52 virtual ~CxtStmt()
53 {
54 }
56 inline const CallStrCxt& getContext() const
57 {
58 return cxt;
59 }
61 inline const ICFGNode* getStmt() const
62 {
63 return inst;
64 }
67 inline bool operator< (const CxtStmt& rhs) const
68 {
69 if(inst!=rhs.getStmt())
70 return inst < rhs.getStmt();
71 else
72 return cxt < rhs.getContext();
73 }
75 inline CxtStmt& operator= (const CxtStmt& rhs)
76 {
77 if(*this!=rhs)
78 {
79 inst = rhs.getStmt();
80 cxt = rhs.getContext();
81 }
82 return *this;
83 }
85 inline bool operator== (const CxtStmt& rhs) const
86 {
87 return (inst == rhs.getStmt() && cxt == rhs.getContext());
88 }
90 inline bool operator!= (const CxtStmt& rhs) const
91 {
92 return !(*this==rhs);
93 }
95 inline std::string cxtToStr() const
96 {
97 std::string str;
98 std::stringstream rawstr(str);
99 rawstr << "[:";
100 for(CallStrCxt::const_iterator it = cxt.begin(), eit = cxt.end(); it!=eit; ++it)
101 {
102 rawstr << *it << " ";
103 }
104 rawstr << " ]";
105 return rawstr.str();
106 }
108 inline void dump() const
109 {
110 SVFUtil::outs() << "[ Current Stmt: " << inst->toString() << "\t Contexts: " << cxtToStr() << " ]\n";
111 }
112
113protected:
116};
117
118
122class CxtThreadStmt : public CxtStmt
123{
124public:
127 {
128 }
135 {
136 }
138 inline NodeID getTid() const
139 {
140 return tid;
141 }
144 inline bool operator< (const CxtThreadStmt& rhs) const
145 {
146 if (tid != rhs.getTid())
147 return tid < rhs.getTid();
148 else if(inst!=rhs.getStmt())
149 return inst < rhs.getStmt();
150 else
151 return cxt < rhs.getContext();
152 }
155 {
156 if(*this!=rhs)
157 {
159 tid = rhs.getTid();
160 }
161 return *this;
162 }
164 inline bool operator== (const CxtThreadStmt& rhs) const
165 {
166 return (tid == rhs.getTid() && inst == rhs.getStmt() && cxt == rhs.getContext());
167 }
169 inline bool operator!= (const CxtThreadStmt& rhs) const
170 {
171 return !(*this==rhs);
172 }
174 inline void dump() const
175 {
176 SVFUtil::outs() << "[ Current Thread id: " << tid << " Stmt: " << inst->toString() << "\t Contexts: " << cxtToStr() << " ]\n";
177 }
178
179private:
181};
182
183
188{
189public:
192 {
193 }
200 virtual ~CxtThread()
201 {
202 }
204 inline const CallStrCxt& getContext() const
205 {
206 return cxt;
207 }
209 inline const ICFGNode* getThread() const
210 {
211 return forksite;
212 }
215 inline bool operator< (const CxtThread& rhs) const
216 {
217 if (forksite != rhs.getThread())
218 return forksite < rhs.getThread();
219 else
220 return cxt < rhs.getContext();
221 }
224 {
225 if(*this!=rhs)
226 {
227 forksite = rhs.getThread();
228 cxt = rhs.getContext();
229 }
230 return *this;
231 }
233 inline bool operator== (const CxtThread& rhs) const
234 {
235 return (forksite == rhs.getThread() && cxt == rhs.getContext());
236 }
238 inline bool operator!= (const CxtThread& rhs) const
239 {
240 return !(*this==rhs);
241 }
243 inline std::string cxtToStr() const
244 {
245 std::string str;
246 std::stringstream rawstr(str);
247 rawstr << "[:";
248 for(CallStrCxt::const_iterator it = cxt.begin(), eit = cxt.end(); it!=eit; ++it)
249 {
250 rawstr << *it << " ";
251 }
252 rawstr << " ]";
253 return rawstr.str();
254 }
255
257
258 inline void setInloop(bool in)
259 {
260 inloop = in;
261 }
262 inline bool isInloop() const
263 {
264 return inloop;
265 }
266 inline void setIncycle(bool in)
267 {
268 incycle = in;
269 }
270 inline bool isIncycle() const
271 {
272 return incycle;
273 }
275
277 inline void dump() const
278 {
279 std::string loop = inloop?", inloop":"";
280 std::string cycle = incycle?", incycle":"";
281
282 if(forksite)
283 {
284 SVFUtil::outs() << "[ Thread: "
285 << forksite->toString() << "\t Contexts: " << cxtToStr()
286 << loop << cycle <<" ]\n";
287 }
288 else
289 {
290 SVFUtil::outs() << "[ Thread: " << "main " << "\t Contexts: " << cxtToStr()
291 << loop << cycle <<" ]\n";
292 }
293 }
294protected:
297 bool inloop;
299};
300
301
308{
309public:
311 CxtProc(const CallStrCxt& c, const FunObjVar* f) :
312 cxt(c), fun(f)
313 {
314 }
318 {
319 }
321 virtual ~CxtProc()
322 {
323 }
325 inline const FunObjVar* getProc() const
326 {
327 return fun;
328 }
330 inline const CallStrCxt& getContext() const
331 {
332 return cxt;
333 }
336 inline bool operator<(const CxtProc& rhs) const
337 {
338 if (fun != rhs.getProc())
339 return fun < rhs.getProc();
340 else
341 return cxt < rhs.getContext();
342 }
344 inline CxtProc& operator=(const CxtProc& rhs)
345 {
346 if (*this != rhs)
347 {
348 fun = rhs.getProc();
349 cxt = rhs.getContext();
350 }
351 return *this;
352 }
354 inline bool operator==(const CxtProc& rhs) const
355 {
356 return (fun == rhs.getProc() && cxt == rhs.getContext());
357 }
359 inline bool operator!=(const CxtProc& rhs) const
360 {
361 return !(*this == rhs);
362 }
364 inline std::string cxtToStr() const
365 {
366 std::string str;
367 std::stringstream rawstr(str);
368 rawstr << "[:";
369 for (CallStrCxt::const_iterator it = cxt.begin(), eit = cxt.end(); it != eit; ++it)
370 {
371 rawstr << *it << " ";
372 }
373 rawstr << " ]";
374 return rawstr.str();
375 }
377 inline void dump() const
378 {
379 SVFUtil::outs() << "[ Proc: " << fun->getName() << "\t Contexts: " << cxtToStr() << " ]\n";
380 }
381
382protected:
385};
386
387
394class CxtThreadProc : public CxtProc
395{
396public:
399 {
400 }
407 {
408 }
410 inline NodeID getTid() const
411 {
412 return tid;
413 }
416 inline bool operator< (const CxtThreadProc& rhs) const
417 {
418 if (tid != rhs.getTid())
419 return tid < rhs.getTid();
420 else if(fun!=rhs.getProc())
421 return fun < rhs.getProc();
422 else
423 return cxt < rhs.getContext();
424 }
427 {
428 if(*this!=rhs)
429 {
430 tid = rhs.getTid();
431 fun = rhs.getProc();
432 cxt = rhs.getContext();
433 }
434 return *this;
435 }
437 inline bool operator== (const CxtThreadProc& rhs) const
438 {
439 return (tid == rhs.getTid() && fun == rhs.getProc() && cxt == rhs.getContext());
440 }
442 inline bool operator!= (const CxtThreadProc& rhs) const
443 {
444 return !(*this==rhs);
445 }
447 inline void dump() const
448 {
449 SVFUtil::outs() << "[ Current Thread id: " << tid << " Proc: " << fun->getName() << "\t Contexts: " << cxtToStr() << " ]\n";
450 }
451
452private:
454};
455
456} // End namespace SVF
457// Specialise hash for the classes defined in this header file.
458// Each hash must combine every field that operator== compares; hashing only a
459// subset (e.g. the tid alone) collapses all keys sharing that field into one
460// bucket. IDs are hashed instead of raw pointers so the bucket layout does not
461// depend on allocation addresses.
462template <> struct std::hash<SVF::CxtThread>
463{
464 size_t operator()(const SVF::CxtThread& ct) const
465 {
466 std::hash<SVF::CallStrCxt> ch;
468 const SVF::NodeID forksite =
469 ct.getThread() != nullptr ? ct.getThread()->getId() : 0;
470 return pairH({forksite, ch(ct.getContext())});
471 }
472};
473template <> struct std::hash<SVF::CxtThreadProc>
474{
475 size_t operator()(const SVF::CxtThreadProc& ctp) const
476 {
477 std::hash<SVF::CallStrCxt> ch;
479 return pairH({ctp.getTid(),
480 pairH({ctp.getProc()->getId(), ch(ctp.getContext())})});
481 }
482};
483template <> struct std::hash<SVF::CxtStmt>
484{
485 size_t operator()(const SVF::CxtStmt& cs) const
486 {
487 std::hash<SVF::CallStrCxt> ch;
489 return pairH({cs.getStmt()->getId(), ch(cs.getContext())});
490 }
491};
492template <> struct std::hash<SVF::CxtThreadStmt>
493{
494 size_t operator()(const SVF::CxtThreadStmt& cts) const
495 {
496 std::hash<SVF::CxtStmt> csH;
498 return pairH({cts.getTid(), csH(cts)});
499 }
500};
501template <> struct std::hash<SVF::CxtProc>
502{
503 size_t operator()(const SVF::CxtProc& cp) const
504 {
505 std::hash<SVF::CallStrCxt> ch;
507 return pairH({cp.getProc()->getId(), ch(cp.getContext())});
508 }
509};
510#endif /* INCLUDE_UTIL_CXTSTMT_H_ */
#define false
Definition cJSON.cpp:70
bool operator!=(const CxtProc &rhs) const
Overloading operator==.
Definition CxtStmt.h:359
CxtProc & operator=(const CxtProc &rhs)
Overloading operator=.
Definition CxtStmt.h:344
void dump() const
Dump CxtProc.
Definition CxtStmt.h:377
CxtProc(const CxtProc &ctm)
Copy constructor.
Definition CxtStmt.h:316
CxtProc(const CallStrCxt &c, const FunObjVar *f)
Constructor.
Definition CxtStmt.h:311
const CallStrCxt & getContext() const
Return current context.
Definition CxtStmt.h:330
bool operator==(const CxtProc &rhs) const
Overloading operator==.
Definition CxtStmt.h:354
virtual ~CxtProc()
Destructor.
Definition CxtStmt.h:321
bool operator<(const CxtProc &rhs) const
Definition CxtStmt.h:336
const FunObjVar * fun
Definition CxtStmt.h:384
std::string cxtToStr() const
Return context in string format.
Definition CxtStmt.h:364
const FunObjVar * getProc() const
Return current procedure.
Definition CxtStmt.h:325
CallStrCxt cxt
Definition CxtStmt.h:383
CxtStmt(const CallStrCxt &c, const ICFGNode *f)
Constructor.
Definition CxtStmt.h:44
bool operator!=(const CxtStmt &rhs) const
Overloading operator==.
Definition CxtStmt.h:90
virtual ~CxtStmt()
Destructor.
Definition CxtStmt.h:52
void dump() const
Dump CxtStmt.
Definition CxtStmt.h:108
const CallStrCxt & getContext() const
Return current context.
Definition CxtStmt.h:56
bool operator<(const CxtStmt &rhs) const
Definition CxtStmt.h:67
std::string cxtToStr() const
Return context in string format.
Definition CxtStmt.h:95
CxtStmt & operator=(const CxtStmt &rhs)
Overloading operator=.
Definition CxtStmt.h:75
const ICFGNode * getStmt() const
Return current statement.
Definition CxtStmt.h:61
const ICFGNode * inst
Definition CxtStmt.h:115
CxtStmt(const CxtStmt &ctm)
Copy constructor.
Definition CxtStmt.h:48
CallStrCxt cxt
Definition CxtStmt.h:114
bool operator==(const CxtStmt &rhs) const
Overloading operator==.
Definition CxtStmt.h:85
CxtThreadProc(NodeID t, const CallStrCxt &c, const FunObjVar *f)
Constructor.
Definition CxtStmt.h:398
CxtThreadProc & operator=(const CxtThreadProc &rhs)
Overloading operator=.
Definition CxtStmt.h:426
bool operator<(const CxtThreadProc &rhs) const
Definition CxtStmt.h:416
NodeID getTid() const
Return current thread id.
Definition CxtStmt.h:410
bool operator!=(const CxtThreadProc &rhs) const
Overloading operator==.
Definition CxtStmt.h:442
virtual ~CxtThreadProc()
Destructor.
Definition CxtStmt.h:406
CxtThreadProc(const CxtThreadProc &ctm)
Copy constructor.
Definition CxtStmt.h:402
void dump() const
Dump CxtThreadProc.
Definition CxtStmt.h:447
bool operator==(const CxtThreadProc &rhs) const
Overloading operator==.
Definition CxtStmt.h:437
CxtThreadStmt(NodeID t, const CallStrCxt &c, const ICFGNode *f)
Constructor.
Definition CxtStmt.h:126
NodeID getTid() const
Return current context.
Definition CxtStmt.h:138
CxtThreadStmt & operator=(const CxtThreadStmt &rhs)
Overloading operator=.
Definition CxtStmt.h:154
void dump() const
Dump CxtThreadStmt.
Definition CxtStmt.h:174
CxtThreadStmt(const CxtThreadStmt &ctm)
Copy constructor.
Definition CxtStmt.h:130
bool operator!=(const CxtThreadStmt &rhs) const
Overloading operator==.
Definition CxtStmt.h:169
virtual ~CxtThreadStmt()
Destructor.
Definition CxtStmt.h:134
bool operator==(const CxtThreadStmt &rhs) const
Overloading operator==.
Definition CxtStmt.h:164
bool operator<(const CxtThreadStmt &rhs) const
Definition CxtStmt.h:144
CxtThread & operator=(const CxtThread &rhs)
Overloading operator=.
Definition CxtStmt.h:223
const CallStrCxt & getContext() const
Return context of the thread.
Definition CxtStmt.h:204
bool isInloop() const
Definition CxtStmt.h:262
bool isIncycle() const
Definition CxtStmt.h:270
virtual ~CxtThread()
Destructor.
Definition CxtStmt.h:200
bool operator<(const CxtThread &rhs) const
Definition CxtStmt.h:215
const ICFGNode * getThread() const
Return forksite.
Definition CxtStmt.h:209
void dump() const
Dump CxtThread.
Definition CxtStmt.h:277
const ICFGNode * forksite
Definition CxtStmt.h:296
CxtThread(const CallStrCxt &c, const ICFGNode *fork)
Constructor.
Definition CxtStmt.h:191
std::string cxtToStr() const
Return context in string format.
Definition CxtStmt.h:243
void setInloop(bool in)
inloop, incycle attributes
Definition CxtStmt.h:258
CxtThread(const CxtThread &ct)
Copy constructor.
Definition CxtStmt.h:195
bool operator!=(const CxtThread &rhs) const
Overloading operator==.
Definition CxtStmt.h:238
void setIncycle(bool in)
Definition CxtStmt.h:266
bool operator==(const CxtThread &rhs) const
Overloading operator==.
Definition CxtStmt.h:233
CallStrCxt cxt
Definition CxtStmt.h:295
virtual const std::string toString() const
Definition ICFG.cpp:50
NodeID getId() const
Get ID.
Definition SVFValue.h:158
virtual const std::string & getName() const
Definition SVFValue.h:184
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
std::vector< u32_t > CallStrCxt
Definition GeneralType.h:96
size_t operator()(const SVF::CxtProc &cp) const
Definition CxtStmt.h:503
size_t operator()(const SVF::CxtStmt &cs) const
Definition CxtStmt.h:485
size_t operator()(const SVF::CxtThreadProc &ctp) const
Definition CxtStmt.h:475
size_t operator()(const SVF::CxtThreadStmt &cts) const
Definition CxtStmt.h:494
size_t operator()(const SVF::CxtThread &ct) const
Definition CxtStmt.h:464