Static Value-Flow Analysis
Loading...
Searching...
No Matches
ThreadAPI.cpp
Go to the documentation of this file.
1//===- ThreadAPI.cpp -- Thread API-------------------------------------------//
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.cpp
25 *
26 * Created on: Jan 21, 2014
27 * Author: Yulei Sui, dye
28 */
29
30#ifndef THREADAPI_CPP_
31#define THREADAPI_CPP_
32
33#include "Util/ThreadAPI.h"
34#include "Util/SVFUtil.h"
35#include "Graphs/CallGraph.h"
36#include "SVFIR/SVFIR.h"
38
39#include <iostream>
40#include <stdio.h>
41#include <iomanip>
42
43using namespace std;
44namespace
45{
47 const SVF::SVFVar* joinArg,
49{
51 cache.joinedThreadObjects.find(joinArg);
52 if (cacheIt != cache.joinedThreadObjects.end())
53 return cacheIt->second;
54
57 std::vector<const SVF::SVFVar*> worklist{joinArg};
58 while (!worklist.empty())
59 {
60 const SVF::SVFVar* v = worklist.back();
61 worklist.pop_back();
62 if (!visited.insert(v->getId()).second)
63 continue;
64
65 for (const SVF::SVFStmt* st : v->getInEdges())
66 {
67 if (const SVF::LoadStmt* load = SVF::SVFUtil::dyn_cast<SVF::LoadStmt>(st))
68 objects |= pta->getPts(load->getRHSVarID()).toNodeBS();
69 else if (const SVF::CopyStmt* copy = SVF::SVFUtil::dyn_cast<SVF::CopyStmt>(st))
70 worklist.push_back(copy->getRHSVar());
71 else if (const SVF::PhiStmt* phi = SVF::SVFUtil::dyn_cast<SVF::PhiStmt>(st))
72 for (SVF::u32_t i = 0; i < phi->getOpVarNum(); ++i)
73 worklist.push_back(phi->getOpVar(i));
74 else if (const SVF::GepStmt* gep = SVF::SVFUtil::dyn_cast<SVF::GepStmt>(st))
75 worklist.push_back(gep->getRHSVar());
76 else if (const SVF::CallPE* call = SVF::SVFUtil::dyn_cast<SVF::CallPE>(st))
77 for (SVF::u32_t i = 0; i < call->getOpVarNum(); ++i)
78 worklist.push_back(call->getOpVar(i));
79 }
80 }
81
82 cache.joinedThreadObjects[joinArg] = objects;
83 return objects;
84}
85}
86
87using namespace SVF;
88
90
91namespace
92{
93
95struct ei_pair
96{
97 const char *n;
99};
100
101} // End anonymous namespace
102
103//Each (name, type) pair will be inserted into the map.
104//All entries of the same type must occur together (for error detection).
105static const ei_pair ei_pairs[]=
106{
107 //The current llvm-gcc puts in the \01.
108 {"pthread_create", ThreadAPI::TD_FORK},
109 {"apr_thread_create", ThreadAPI::TD_FORK},
110 {"pthread_join", ThreadAPI::TD_JOIN},
111 {"\01_pthread_join", ThreadAPI::TD_JOIN},
112 {"pthread_cancel", ThreadAPI::TD_JOIN},
113 {"pthread_mutex_lock", ThreadAPI::TD_ACQUIRE},
114 {"pthread_rwlock_rdlock", ThreadAPI::TD_ACQUIRE},
115 {"sem_wait", ThreadAPI::TD_ACQUIRE},
116 {"_spin_lock", ThreadAPI::TD_ACQUIRE},
117 {"SRE_SplSpecLockEx", ThreadAPI::TD_ACQUIRE},
118 {"pthread_mutex_trylock", ThreadAPI::TD_TRY_ACQUIRE},
119 {"pthread_mutex_unlock", ThreadAPI::TD_RELEASE},
120 {"pthread_rwlock_unlock", ThreadAPI::TD_RELEASE},
121 {"sem_post", ThreadAPI::TD_RELEASE},
122 {"_spin_unlock", ThreadAPI::TD_RELEASE},
123 {"SRE_SplSpecUnlockEx", ThreadAPI::TD_RELEASE},
124// {"pthread_cancel", ThreadAPI::TD_CANCEL},
125 {"pthread_exit", ThreadAPI::TD_EXIT},
126 {"pthread_detach", ThreadAPI::TD_DETACH},
127 {"pthread_cond_wait", ThreadAPI::TD_COND_WAIT},
128 {"pthread_cond_signal", ThreadAPI::TD_COND_SIGNAL},
129 {"pthread_cond_broadcast", ThreadAPI::TD_COND_BROADCAST},
130 {"pthread_cond_init", ThreadAPI::TD_CONDVAR_INI},
131 {"pthread_cond_destroy", ThreadAPI::TD_CONDVAR_DESTROY},
132 {"pthread_mutex_init", ThreadAPI::TD_MUTEX_INI},
133 {"pthread_mutex_destroy", ThreadAPI::TD_MUTEX_DESTROY},
134 {"pthread_barrier_init", ThreadAPI::TD_BAR_INIT},
135 {"pthread_barrier_wait", ThreadAPI::TD_BAR_WAIT},
136
137 // Hare APIs
138 {"hare_parallel_for", ThreadAPI::HARE_PAR_FOR},
139
140 //This must be the last entry.
142};
143
148{
151 t_seen.insert(TD_DUMMY);
152 for(const ei_pair *p= ei_pairs; p->n; ++p)
153 {
154 if(p->t != prev_t)
155 {
156 //This will detect if you move an entry to another block
157 // but forget to change the type.
158 if(t_seen.count(p->t))
159 {
160 fputs(p->n, stderr);
161 putc('\n', stderr);
162 assert(!"ei_pairs not grouped by type");
163 }
164 t_seen.insert(p->t);
165 prev_t= p->t;
166 }
167 if(tdAPIMap.count(p->n))
168 {
169 fputs(p->n, stderr);
170 putc('\n', stderr);
171 assert(!"duplicate name in ei_pairs");
172 }
173 tdAPIMap[p->n]= p->t;
174 }
175}
176
179{
180 if(F)
181 {
182 TDAPIMap::const_iterator it= tdAPIMap.find(F->getName());
183 if(it != tdAPIMap.end())
184 return it->second;
185 }
186 return TD_DUMMY;
187}
188
189bool ThreadAPI::isTDFork(const CallICFGNode *inst) const
190{
191 return getType(inst->getCalledFunction()) == TD_FORK;
192}
193
194bool ThreadAPI::isTDJoin(const CallICFGNode *inst) const
195{
196 return getType(inst->getCalledFunction()) == TD_JOIN;
197}
198
199bool ThreadAPI::isTDExit(const CallICFGNode *inst) const
200{
201 return getType(inst->getCalledFunction()) == TD_EXIT;
202}
203
205{
206 return getType(inst->getCalledFunction()) == TD_ACQUIRE;
207}
208
210{
211 return getType(inst->getCalledFunction()) == TD_RELEASE;
212}
213
215{
216 return getType(inst->getCalledFunction()) == TD_BAR_WAIT;
217}
218
219
221{
222 assert(isTDFork(inst) && "not a thread fork function!");
223 return inst->getArgument(0);
224}
225
227{
228 assert(isTDFork(inst) && "not a thread fork function!");
229 return inst->getArgument(2);
230}
231
235{
236 assert(isTDFork(inst) && "not a thread fork function!");
237 return inst->getArgument(3);
238}
239
241{
242 assert(PAG::getPAG()->hasFunArgsList(F) && "forked function has no args list!");
244 // in pthread, forked functions are of type void *()(void *args)
245 assert(funArgList.size() == 1 && "num of pthread forked function args is not 1!");
246 return funArgList[0];
247}
248
250{
251 assert(isTDJoin(inst) && "not a thread join function!");
252 return inst->getArgument(1);
253}
254
255const SVFVar* ThreadAPI::getLockVal(const ICFGNode *cs) const
256{
257 const CallICFGNode* call = SVFUtil::dyn_cast<CallICFGNode>(cs);
258 assert(call && "not a call ICFGNode?");
259 assert((isTDAcquire(call) || isTDRelease(call)) && "not a lock acquire or release function");
260 return call->getArgument(0);
261}
262
264{
265 assert(isTDJoin(cs) && "not a thread join function!");
266 return cs->getArgument(0);
267}
268
281
283 const SVFVar *joinArg, ForkJoinAliasCache& cache) const
284{
285 // pthread_create receives &t (a pointer to the pthread_t object), so the
286 // forked thread is identified by the pthread_t object(s) in pts(forkArg).
287 const PointsTo& forkObjs = pta->getPts(forkArg->getId());
289 if (forkObjs.toNodeBS().intersects(joinedObjs))
290 return true;
291
292 // (1) Direct case: the join handle is itself a pointer to the same pthread_t.
293 for (NodeID o : forkObjs)
294 if (pta->alias(o, joinArg->getId()))
295 return true;
296
297 // (2) General case: pthread_t is usually a scalar, so pthread_join receives
298 // the pthread_t *value*, which carries no points-to of its own. That
299 // value is loaded from the pthread_t storage, possibly after flowing
300 // through copies / phis / casts / by-value parameter passing. We
301 // backward value-track the join handle to every load it may originate
302 // from and check whether the loaded-from storage is the fork's pthread_t
303 // object. This covers the common shapes soundly (an over-approximate
304 // match only adds a sound join-related value flow).
305 return false;
306}
307
312{
313
314 tdAPIStatMap["pthread_create"] = 0;
315
316 tdAPIStatMap["pthread_join"] = 0;
317
318 tdAPIStatMap["pthread_mutex_lock"] = 0;
319
320 tdAPIStatMap["pthread_mutex_trylock"] = 0;
321
322 tdAPIStatMap["pthread_mutex_unlock"] = 0;
323
324 tdAPIStatMap["pthread_cancel"] = 0;
325
326 tdAPIStatMap["pthread_exit"] = 0;
327
328 tdAPIStatMap["pthread_detach"] = 0;
329
330 tdAPIStatMap["pthread_cond_wait"] = 0;
331
332 tdAPIStatMap["pthread_cond_signal"] = 0;
333
334 tdAPIStatMap["pthread_cond_broadcast"] = 0;
335
336 tdAPIStatMap["pthread_cond_init"] = 0;
337
338 tdAPIStatMap["pthread_cond_destroy"] = 0;
339
340 tdAPIStatMap["pthread_mutex_init"] = 0;
341
342 tdAPIStatMap["pthread_mutex_destroy"] = 0;
343
344 tdAPIStatMap["pthread_barrier_init"] = 0;
345
346 tdAPIStatMap["pthread_barrier_wait"] = 0;
347
348 tdAPIStatMap["hare_parallel_for"] = 0;
349}
350
352{
353
355
357
359 for (const auto& item: *svfirCallGraph)
360 {
361 for (FunObjVar::const_bb_iterator bit = (item.second)->getFunction()->begin(), ebit = (item.second)->getFunction()->end(); bit != ebit; ++bit)
362 {
363 const SVFBasicBlock* bb = bit->second;
364 for (const auto& svfInst: bb->getICFGNodeList())
365 {
367 continue;
368
369 const FunObjVar* fun = SVFUtil::cast<CallICFGNode>(svfInst)->getCalledFunction();
370 TD_TYPE type = getType(fun);
371 switch (type)
372 {
373 case TD_FORK:
374 {
375 tdAPIStatMap["pthread_create"]++;
376 break;
377 }
378 case TD_JOIN:
379 {
380 tdAPIStatMap["pthread_join"]++;
381 break;
382 }
383 case TD_ACQUIRE:
384 {
385 tdAPIStatMap["pthread_mutex_lock"]++;
386 break;
387 }
388 case TD_TRY_ACQUIRE:
389 {
390 tdAPIStatMap["pthread_mutex_trylock"]++;
391 break;
392 }
393 case TD_RELEASE:
394 {
395 tdAPIStatMap["pthread_mutex_unlock"]++;
396 break;
397 }
398 case TD_CANCEL:
399 {
400 tdAPIStatMap["pthread_cancel"]++;
401 break;
402 }
403 case TD_EXIT:
404 {
405 tdAPIStatMap["pthread_exit"]++;
406 break;
407 }
408 case TD_DETACH:
409 {
410 tdAPIStatMap["pthread_detach"]++;
411 break;
412 }
413 case TD_COND_WAIT:
414 {
415 tdAPIStatMap["pthread_cond_wait"]++;
416 break;
417 }
418 case TD_COND_SIGNAL:
419 {
420 tdAPIStatMap["pthread_cond_signal"]++;
421 break;
422 }
424 {
425 tdAPIStatMap["pthread_cond_broadcast"]++;
426 break;
427 }
428 case TD_CONDVAR_INI:
429 {
430 tdAPIStatMap["pthread_cond_init"]++;
431 break;
432 }
434 {
435 tdAPIStatMap["pthread_cond_destroy"]++;
436 break;
437 }
438 case TD_MUTEX_INI:
439 {
440 tdAPIStatMap["pthread_mutex_init"]++;
441 break;
442 }
443 case TD_MUTEX_DESTROY:
444 {
445 tdAPIStatMap["pthread_mutex_destroy"]++;
446 break;
447 }
448 case TD_BAR_INIT:
449 {
450 tdAPIStatMap["pthread_barrier_init"]++;
451 break;
452 }
453 case TD_BAR_WAIT:
454 {
455 tdAPIStatMap["pthread_barrier_wait"]++;
456 break;
457 }
458 case HARE_PAR_FOR:
459 {
460 tdAPIStatMap["hare_parallel_for"]++;
461 break;
462 }
463 case TD_DUMMY:
464 {
465 break;
466 }
467 }
468 }
469 }
470
471 }
472
473 std::string name(PAG::getPAG()->getModuleIdentifier());
474 std::vector<std::string> fullNames = SVFUtil::split(name,'/');
475 if (fullNames.size() > 1)
476 {
477 name = fullNames[fullNames.size() - 1];
478 }
479 SVFUtil::outs() << "################ (program : " << name
480 << ")###############\n";
481 SVFUtil::outs().flags(std::ios::left);
482 unsigned field_width = 20;
484 tdAPIStatMap.end(); it != eit; ++it)
485 {
486 std::string apiName = it->first;
487 // format out put with width 20 space
488 SVFUtil::outs() << std::setw(field_width) << apiName << " : " << it->second
489 << "\n";
490 }
491 SVFUtil::outs() << "#######################################################"
492 << std::endl;
493
494}
495
496#endif /* THREADAPI_CPP_ */
static const ei_pair ei_pairs[]
static const ei_pair ei_pairs[]
cJSON * p
Definition cJSON.cpp:2559
newitem type
Definition cJSON.cpp:2739
copy
Definition cJSON.cpp:414
cJSON * n
Definition cJSON.cpp:2558
const char *const name
Definition cJSON.h:264
cJSON * item
Definition cJSON.h:222
const ValVar * getArgument(u32_t ArgNo) const
Parameter operations.
Definition ICFGNode.h:483
const FunObjVar * getCalledFunction() const
Definition ICFGNode.h:501
BasicBlockGraph::IDToNodeMapTy::const_iterator const_bb_iterator
virtual const PointsTo & getPts(NodeID ptr)=0
Get points-to targets of a pointer. It needs to be implemented in child class.
virtual AliasResult alias(const SVFVar *V1, const SVFVar *V2)=0
Interface exposed to users of our pointer analysis, given Value infos.
const std::vector< const ICFGNode * > & getICFGNodeList() const
const ValVarList & getFunArgsList(const FunObjVar *func) const
Get function arguments list.
Definition SVFIR.h:378
std::vector< const ValVar * > ValVarList
Definition SVFIR.h:60
const CallGraph * getCallGraph()
Get CG.
Definition SVFIR.h:248
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:120
virtual const std::string & getName() const
Definition SVFValue.h:184
const ValVar * getForkedFun(const CallICFGNode *inst) const
bool isTDFork(const CallICFGNode *inst) const
Return true if this call create a new thread.
static ThreadAPI * tdAPI
Static reference.
Definition ThreadAPI.h:92
@ TD_COND_SIGNAL
wait a condition
Definition ThreadAPI.h:65
@ TD_DETACH
wait for a thread to join
Definition ThreadAPI.h:58
@ TD_CONDVAR_INI
initial a mutex variable
Definition ThreadAPI.h:69
@ HARE_PAR_FOR
Barrier wait.
Definition ThreadAPI.h:73
@ TD_BAR_INIT
initial a mutex variable
Definition ThreadAPI.h:71
@ TD_ACQUIRE
detach a thread directly instead wait for it to join
Definition ThreadAPI.h:59
@ TD_MUTEX_DESTROY
initial a mutex variable
Definition ThreadAPI.h:68
@ TD_FORK
dummy type
Definition ThreadAPI.h:56
@ TD_CONDVAR_DESTROY
initial a mutex variable
Definition ThreadAPI.h:70
@ TD_JOIN
create a new thread
Definition ThreadAPI.h:57
@ TD_BAR_WAIT
Barrier init.
Definition ThreadAPI.h:72
@ TD_COND_BROADCAST
signal a condition
Definition ThreadAPI.h:66
@ TD_COND_WAIT
cancel a thread by another
Definition ThreadAPI.h:64
@ TD_TRY_ACQUIRE
acquire a lock
Definition ThreadAPI.h:60
@ TD_MUTEX_INI
broadcast a condition
Definition ThreadAPI.h:67
@ TD_RELEASE
try to acquire a lock
Definition ThreadAPI.h:61
@ TD_EXIT
release a lock
Definition ThreadAPI.h:62
@ TD_CANCEL
exit/kill a thread
Definition ThreadAPI.h:63
const ValVar * getForkedThread(const CallICFGNode *inst) const
Return arguments/attributes of pthread_create / hare_parallel_for.
TDAPIMap tdAPIMap
API map, from a string to threadAPI type.
Definition ThreadAPI.h:80
bool isTDJoin(const CallICFGNode *inst) const
Return true if this call wait for a worker thread.
void init()
Initialize the map.
bool isTDRelease(const CallICFGNode *inst) const
Return true if this call release a lock.
const SVFVar * getFormalParmOfForkedFun(const FunObjVar *F) const
Return the formal parm of forked function (the first arg in pthread)
const SVFVar * getLockVal(const ICFGNode *inst) const
Return lock value.
bool isTDExit(const CallICFGNode *inst) const
Return true if this call exits/terminate a thread.
const SVFVar * getRetParmAtJoinedSite(const CallICFGNode *inst) const
void statInit(Map< std::string, u32_t > &tdAPIStatMap)
const SVFVar * getJoinedThread(const CallICFGNode *inst) const
Return arguments/attributes of pthread_join.
void performAPIStat()
bool isAliasedForkJoin(PointerAnalysis *pta, const SVFVar *forkArg, const SVFVar *joinArg) const
const ValVar * getActualParmAtForkSite(const CallICFGNode *inst) const
bool isTDBarWait(const CallICFGNode *inst) const
Return true if this call waits for a barrier.
bool isTDAcquire(const CallICFGNode *inst) const
Return true if this call acquire a lock.
TD_TYPE getType(const FunObjVar *F) const
Get the function type if it is a threadAPI function.
bool isCallSite(const ICFGNode *inst)
Definition SVFUtil.cpp:320
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
std::vector< std::string > split(const std::string &s, char separator)
Split into two substrings around the first occurrence of a separator string.
Definition SVFUtil.h:196
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition GeneralType.h:56
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67
If fork join the same thread.
Definition ThreadAPI.h:158