Static Value-Flow Analysis
ThreadAPI.h
Go to the documentation of this file.
1 //===- ThreadAPI.h -- API for threads-----------------------------------------//
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  * ThreadAPI.h
25  *
26  * Created on: Jan 21, 2014
27  * Author: Yulei Sui, dye
28  */
29 
30 #ifndef THREADAPI_H_
31 #define THREADAPI_H_
32 
33 #include "SVFIR/SVFValue.h"
34 
35 namespace SVF
36 {
37 
38 class SVFModule;
39 class ICFGNode;
40 class CallICFGNode;
41 class SVFVar;
42 
43 /*
44  * ThreadAPI class contains interfaces for pthread programs
45  */
46 class ThreadAPI
47 {
48 
49 public:
50  enum TD_TYPE
51  {
52  TD_DUMMY = 0,
71  };
72 
74 
75 private:
78 
81  {
82  init();
83  }
84 
86  void init();
87 
89  static ThreadAPI* tdAPI;
90 
92  inline TD_TYPE getType(const SVFFunction* F) const
93  {
94  if(F)
95  {
96  TDAPIMap::const_iterator it= tdAPIMap.find(F->getName());
97  if(it != tdAPIMap.end())
98  return it->second;
99  }
100  return TD_DUMMY;
101  }
102 
103 public:
106  {
107  if(tdAPI == nullptr)
108  {
109  tdAPI = new ThreadAPI();
110  }
111  return tdAPI;
112  }
113 
114  static void destroy()
115  {
116  if(tdAPI != nullptr)
117  {
118  delete tdAPI;
119  tdAPI = nullptr;
120  }
121  }
122 
124 
125  const SVFVar* getForkedThread(const CallICFGNode *inst) const;
130  const SVFVar* getForkedFun(const CallICFGNode *inst) const;
131 
134  const SVFVar* getActualParmAtForkSite(const CallICFGNode *inst) const;
135 
137  const SVFVar* getFormalParmOfForkedFun(const SVFFunction* F) const;
139 
140 
141 
143 
144  bool isTDFork(const CallICFGNode *inst) const;
146 
148 
149  bool isTDJoin(const CallICFGNode *inst) const;
151 
153 
154  const SVFVar* getJoinedThread(const CallICFGNode *inst) const;
159  const SVFVar* getRetParmAtJoinedSite(const CallICFGNode *inst) const;
161 
162 
164 
165  bool isTDExit(const CallICFGNode *inst) const;
167 
169 
170  bool isTDAcquire(const CallICFGNode* inst) const;
172 
174 
175  bool isTDRelease(const CallICFGNode *inst) const;
177 
179 
180  const SVFVar* getLockVal(const ICFGNode *inst) const;
183 
185 
186  bool isTDBarWait(const CallICFGNode *inst) const;
188 
189  void performAPIStat(SVFModule* m);
190  void statInit(Map<std::string, u32_t>& tdAPIStatMap);
191 };
192 
193 } // End namespace SVF
194 
195 #endif /* THREADAPI_H_ */
#define F(f)
bool isTDFork(const CallICFGNode *inst) const
Return true if this call create a new thread.
Definition: ThreadAPI.cpp:133
static ThreadAPI * tdAPI
Static reference.
Definition: ThreadAPI.h:89
@ TD_COND_SIGNAL
wait a condition
Definition: ThreadAPI.h:62
@ TD_DETACH
wait for a thread to join
Definition: ThreadAPI.h:55
@ TD_CONDVAR_INI
initial a mutex variable
Definition: ThreadAPI.h:66
@ HARE_PAR_FOR
Barrier wait.
Definition: ThreadAPI.h:70
@ TD_BAR_INIT
initial a mutex variable
Definition: ThreadAPI.h:68
@ TD_ACQUIRE
detach a thread directly instead wait for it to join
Definition: ThreadAPI.h:56
@ TD_MUTEX_DESTROY
initial a mutex variable
Definition: ThreadAPI.h:65
@ TD_FORK
dummy type
Definition: ThreadAPI.h:53
@ TD_CONDVAR_DESTROY
initial a mutex variable
Definition: ThreadAPI.h:67
@ TD_JOIN
create a new thread
Definition: ThreadAPI.h:54
@ TD_BAR_WAIT
Barrier init.
Definition: ThreadAPI.h:69
@ TD_COND_BROADCAST
signal a condition
Definition: ThreadAPI.h:63
@ TD_COND_WAIT
cancel a thread by another
Definition: ThreadAPI.h:61
@ TD_TRY_ACQUIRE
acquire a lock
Definition: ThreadAPI.h:57
@ TD_MUTEX_INI
broadcast a condition
Definition: ThreadAPI.h:64
@ TD_RELEASE
try to acquire a lock
Definition: ThreadAPI.h:58
@ TD_EXIT
release a lock
Definition: ThreadAPI.h:59
@ TD_CANCEL
exit/kill a thread
Definition: ThreadAPI.h:60
TDAPIMap tdAPIMap
API map, from a string to threadAPI type.
Definition: ThreadAPI.h:77
bool isTDJoin(const CallICFGNode *inst) const
Return true if this call wait for a worker thread.
Definition: ThreadAPI.cpp:138
void init()
Initialize the map.
Definition: ThreadAPI.cpp:103
const SVFVar * getFormalParmOfForkedFun(const SVFFunction *F) const
Return the formal parm of forked function (the first arg in pthread)
Definition: ThreadAPI.cpp:184
bool isTDRelease(const CallICFGNode *inst) const
Return true if this call release a lock.
Definition: ThreadAPI.cpp:153
void performAPIStat(SVFModule *m)
Definition: ThreadAPI.cpp:266
static ThreadAPI * getThreadAPI()
Return a static reference.
Definition: ThreadAPI.h:105
const SVFVar * getLockVal(const ICFGNode *inst) const
Return lock value.
Definition: ThreadAPI.cpp:199
TD_TYPE getType(const SVFFunction *F) const
Get the function type if it is a threadAPI function.
Definition: ThreadAPI.h:92
const SVFVar * getForkedFun(const CallICFGNode *inst) const
Definition: ThreadAPI.cpp:170
bool isTDExit(const CallICFGNode *inst) const
Return true if this call exits/terminate a thread.
Definition: ThreadAPI.cpp:143
const SVFVar * getRetParmAtJoinedSite(const CallICFGNode *inst) const
Definition: ThreadAPI.cpp:193
void statInit(Map< std::string, u32_t > &tdAPIStatMap)
Definition: ThreadAPI.cpp:226
const SVFVar * getJoinedThread(const CallICFGNode *inst) const
Return arguments/attributes of pthread_join.
Definition: ThreadAPI.cpp:207
ThreadAPI()
Constructor.
Definition: ThreadAPI.h:80
Map< std::string, TD_TYPE > TDAPIMap
Definition: ThreadAPI.h:73
const SVFVar * getActualParmAtForkSite(const CallICFGNode *inst) const
Definition: ThreadAPI.cpp:178
const SVFVar * getForkedThread(const CallICFGNode *inst) const
Return arguments/attributes of pthread_create / hare_parallel_for.
Definition: ThreadAPI.cpp:164
bool isTDBarWait(const CallICFGNode *inst) const
Return true if this call waits for a barrier.
Definition: ThreadAPI.cpp:158
bool isTDAcquire(const CallICFGNode *inst) const
Return true if this call acquire a lock.
Definition: ThreadAPI.cpp:148
static void destroy()
Definition: ThreadAPI.h:114
for isBitcode
Definition: BasicTypes.h:68
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101