Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVFUtil.h
Go to the documentation of this file.
1//===- SVFUtil.h -- Analysis helper functions----------------------------//
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 * SVFUtil.h
25 *
26 * Created on: Apr 11, 2013
27 * Author: Yulei Sui, dye
28 */
29
30#ifndef AnalysisUtil_H_
31#define AnalysisUtil_H_
32
33#include <time.h>
34
36#include "Util/Casting.h"
37#include "Util/ExtAPI.h"
38#include "Util/GeneralType.h"
40#include "Util/ThreadAPI.h"
41
42namespace SVF
43{
44
45/*
46 * Util class to assist pointer analysis
47 */
48namespace SVFUtil
49{
50
52inline std::ostream &outs()
53{
54 return std::cout;
55}
56
58inline std::ostream &errs()
59{
60 return std::cerr;
61}
62
66
68void dumpPointsToSet(unsigned node, NodeBS To) ;
69void dumpSparseSet(const NodeBS& To);
70
72void dumpAliasSet(unsigned node, NodeBS To) ;
73
75std::string sucMsg(const std::string& msg);
76
78std::string wrnMsg(const std::string& msg);
79
81void writeWrnMsg(const std::string& msg);
82
84
85std::string errMsg(const std::string& msg);
86std::string bugMsg1(const std::string& msg);
87std::string bugMsg2(const std::string& msg);
88std::string bugMsg3(const std::string& msg);
90
92std::string pasMsg(const std::string& msg);
93
95void reportMemoryUsageKB(const std::string& infor,
97
100
102void increaseStackSize();
103
109inline bool cmpPts (const PointsTo& lpts,const PointsTo& rpts)
110{
111 if (lpts.count() != rpts.count())
112 return (lpts.count() < rpts.count());
113 else
114 {
115 PointsTo::iterator bit = lpts.begin(), eit = lpts.end();
116 PointsTo::iterator rbit = rpts.begin(), reit = rpts.end();
117 for (; bit != eit && rbit != reit; bit++, rbit++)
118 {
119 if (*bit != *rbit)
120 return (*bit < *rbit);
121 }
122
123 return false;
124 }
125}
126
127inline bool cmpNodeBS(const NodeBS& lpts,const NodeBS& rpts)
128{
129 if (lpts.count() != rpts.count())
130 return (lpts.count() < rpts.count());
131 else
132 {
133 NodeBS::iterator bit = lpts.begin(), eit = lpts.end();
134 NodeBS::iterator rbit = rpts.begin(), reit = rpts.end();
135 for (; bit != eit && rbit != reit; bit++, rbit++)
136 {
137 if (*bit != *rbit)
138 return (*bit < *rbit);
139 }
140
141 return false;
142 }
143}
144
145typedef struct equalPointsTo
146{
147 bool operator()(const PointsTo& lhs, const PointsTo& rhs) const
148 {
149 return SVFUtil::cmpPts(lhs, rhs);
150 }
151} equalPointsTo;
152
153typedef struct equalNodeBS
154{
155 bool operator()(const NodeBS& lhs, const NodeBS& rhs) const
156 {
157 return SVFUtil::cmpNodeBS(lhs, rhs);
158 }
159} equalNodeBS;
160
163
165bool isIntrinsicInst(const ICFGNode* inst);
167
168
169bool isCallSite(const ICFGNode* inst);
170
171bool isRetInstNode(const ICFGNode* node);
172
173
175inline bool isNonInstricCallSite(const ICFGNode* inst)
176{
177 if(isIntrinsicInst(inst))
178 return false;
179 return isCallSite(inst);
180}
181
185bool matchArgs(const CallICFGNode* cs, const FunObjVar* callee);
186
187
189inline std::vector<std::string> split(const std::string& s, char separator)
190{
191 std::vector<std::string> output;
192 std::string::size_type prev_pos = 0, pos = 0;
193 while ((pos = s.find(separator, pos)) != std::string::npos)
194 {
195 std::string substring(s.substr(prev_pos, pos - prev_pos));
196 if (!substring.empty())
197 {
198 output.push_back(substring);
199 }
200 prev_pos = ++pos;
201 }
202 std::string lastSubstring(s.substr(prev_pos, pos - prev_pos));
203 if (!lastSubstring.empty())
204 {
205 output.push_back(lastSubstring);
206 }
207 return output;
208}
209
211template <typename Data>
213{
214 for (const typename Map<Data, unsigned>::value_type &ptocc : from)
215 {
216 to[ptocc.first] += ptocc.second;
217 }
218}
219
222
224template <typename Key, typename KeySet>
225inline void insertKey(const Key &key, KeySet &keySet)
226{
227 keySet.insert(key);
228}
229
231inline void insertKey(const NodeID &key, NodeBS &keySet)
232{
233 keySet.set(key);
234}
235
237template <typename Key, typename KeySet>
238inline void removeKey(const Key &key, KeySet &keySet)
239{
240 keySet.erase(key);
241}
242
244inline void removeKey(const NodeID &key, NodeBS &keySet)
245{
246 keySet.reset(key);
247}
248
250void timeLimitReached(int signum);
251
256
260
263
264
265bool isExtCall(const FunObjVar* fun);
266
267
269
270
271
272inline bool isHeapAllocExtFunViaRet(const FunObjVar* fun)
273{
274 return fun && (ExtAPI::getExtAPI()->is_alloc(fun)
275 || ExtAPI::getExtAPI()->is_realloc(fun));
276}
277
278inline bool isHeapAllocExtFunViaArg(const FunObjVar* fun)
279{
280 return fun && ExtAPI::getExtAPI()->is_arg_alloc(fun);
281}
282
284
285
291
292
293
294inline bool isReallocExtFun(const FunObjVar* fun)
295{
296 return fun && (ExtAPI::getExtAPI()->is_realloc(fun));
297}
298
300
301
302
303bool isProgEntryFunction(const FunObjVar*);
304
306const FunObjVar* getProgFunction(const std::string& funName);
307
310
311
313
314bool isProgExitFunction(const FunObjVar *fun);
315
316
317
319
321
323
324inline const ValVar* getForkedFun(const CallICFGNode *inst)
325{
327}
329
330
331bool isExtCall(const CallICFGNode* cs);
332
333bool isExtCall(const ICFGNode* node);
334
336
337
340
341bool isHeapAllocExtCall(const ICFGNode* cs);
342
344
347
348bool isReallocExtCall(const CallICFGNode* cs);
350
353inline bool isThreadForkCall(const CallICFGNode *inst)
354{
355 return ThreadAPI::getThreadAPI()->isTDFork(inst);
356}
358
361inline bool isThreadJoinCall(const CallICFGNode* cs)
362{
363 return ThreadAPI::getThreadAPI()->isTDJoin(cs);
364}
366
369inline bool isThreadExitCall(const CallICFGNode* cs)
370{
371 return ThreadAPI::getThreadAPI()->isTDExit(cs);
372}
374
377inline bool isLockAquireCall(const CallICFGNode* cs)
378{
380}
382
385inline bool isLockReleaseCall(const CallICFGNode* cs)
386{
388}
390
392
393inline bool isBarrierWaitCall(const CallICFGNode* cs)
394{
396}
398
400
406
407
408bool isProgExitCall(const CallICFGNode* cs);
409
410
411template<typename T>
412constexpr typename std::remove_reference<T>::type &&
413move(T &&t) noexcept
414{
415 return std::move(t);
416}
417
418} // End namespace SVFUtil
419
420} // End namespace SVF
421
422#endif /* AnalysisUtil_H_ */
bool is_arg_alloc(const FunObjVar *F)
Definition ExtAPI.cpp:302
s32_t get_alloc_arg_pos(const FunObjVar *F)
Definition ExtAPI.cpp:313
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:44
bool is_realloc(const FunObjVar *F)
Definition ExtAPI.cpp:329
bool is_alloc(const FunObjVar *F)
Definition ExtAPI.cpp:296
const ValVar * getForkedFun(const CallICFGNode *inst) const
bool isTDFork(const CallICFGNode *inst) const
Return true if this call create a new thread.
bool isTDJoin(const CallICFGNode *inst) const
Return true if this call wait for a worker thread.
bool isTDRelease(const CallICFGNode *inst) const
Return true if this call release a lock.
bool isTDExit(const CallICFGNode *inst) const
Return true if this call exits/terminate a thread.
const ValVar * getActualParmAtForkSite(const CallICFGNode *inst) const
static ThreadAPI * getThreadAPI()
Return a static reference.
Definition ThreadAPI.h:99
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.
hclust_fast_methods
Definition fastcluster.h:66
bool isReallocExtCall(const CallICFGNode *cs)
Definition SVFUtil.cpp:373
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
Definition SVFUtil.cpp:59
void increaseStackSize()
Increase the stack size limit.
Definition SVFUtil.cpp:233
const ObjVar * getObjVarOfValVar(const ValVar *valVar)
Definition SVFUtil.cpp:435
std::string bugMsg1(const std::string &msg)
Definition SVFUtil.cpp:87
bool isProgEntryFunction(const FunObjVar *)
Program entry function e.g. main.
Definition SVFUtil.cpp:446
std::string hclustMethodToString(hclust_fast_methods method)
Returns a string representation of a hclust method.
Definition SVFUtil.cpp:252
void stopAnalysisLimitTimer(bool limitTimerSet)
Definition SVFUtil.cpp:302
bool isLockAquireCall(const CallICFGNode *cs)
Definition SVFUtil.h:377
bool isIntrinsicInst(const ICFGNode *inst)
Return true if it is an llvm intrinsic instruction.
Definition SVFUtil.cpp:325
u32_t getHeapAllocHoldingArgPosition(const FunObjVar *fun)
Get the position of argument that holds an allocated heap object.
Definition SVFUtil.h:286
const FunObjVar * getProgEntryFunction()
Get program entry function.
Definition SVFUtil.cpp:414
bool isBarrierWaitCall(const CallICFGNode *cs)
Return true if this is a barrier wait call.
Definition SVFUtil.h:393
void mergePtsOccMaps(Map< Data, unsigned > &to, const Map< Data, unsigned > from)
Given a map mapping points-to sets to a count, adds from into to.
Definition SVFUtil.h:212
bool isHeapAllocExtFunViaRet(const FunObjVar *fun)
Return true if the call is a heap allocator/reallocator.
Definition SVFUtil.h:272
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
Definition SVFUtil.cpp:105
OrderedSet< PointsTo, equalPointsTo > PointsToList
Definition SVFUtil.h:161
void dumpAliasSet(unsigned node, NodeBS To)
Dump alias set.
Definition SVFUtil.cpp:143
bool isHeapAllocExtFunViaArg(const FunObjVar *fun)
Definition SVFUtil.h:278
bool getMemoryUsageKB(u32_t *vmrss_kb, u32_t *vmsize_kb)
Get memory usage from system file. Return TRUE if succeed.
Definition SVFUtil.cpp:183
void reportMemoryUsageKB(const std::string &infor, OutStream &O=SVFUtil::outs())
Print memory usage in KB.
Definition SVFUtil.cpp:173
bool startAnalysisLimitTimer(unsigned timeLimit)
Definition SVFUtil.cpp:281
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition SVFUtil.cpp:82
std::string bugMsg3(const std::string &msg)
Definition SVFUtil.cpp:97
bool isHeapAllocExtCallViaArg(const CallICFGNode *cs)
Definition SVFUtil.cpp:343
bool isHeapAllocExtCall(const ICFGNode *cs)
Definition SVFUtil.cpp:361
bool isNonInstricCallSite(const ICFGNode *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
Definition SVFUtil.h:175
bool isThreadJoinCall(const CallICFGNode *cs)
Definition SVFUtil.h:361
bool isArgOfUncalledFunction(const SVFVar *svfvar)
Definition SVFUtil.cpp:426
bool cmpNodeBS(const NodeBS &lpts, const NodeBS &rpts)
Definition SVFUtil.h:127
std::ostream & errs()
Overwrite llvm::errs()
Definition SVFUtil.h:58
bool cmpPts(const PointsTo &lpts, const PointsTo &rpts)
Definition SVFUtil.h:109
bool isExtCall(const FunObjVar *fun)
Definition SVFUtil.cpp:441
bool isThreadForkCall(const CallICFGNode *inst)
Definition SVFUtil.h:353
bool matchArgs(const CallICFGNode *cs, const FunObjVar *callee)
Definition SVFUtil.cpp:312
void writeWrnMsg(const std::string &msg)
Writes a message run through wrnMsg.
Definition SVFUtil.cpp:72
void dumpSparseSet(const NodeBS &To)
Definition SVFUtil.cpp:121
const ValVar * getActualParmAtForkSite(const CallICFGNode *cs)
Return sole argument of the thread routine.
Definition SVFUtil.h:401
bool isLockReleaseCall(const CallICFGNode *cs)
Definition SVFUtil.h:385
void dumpPointsToSet(unsigned node, NodeBS To)
Dump points-to set.
Definition SVFUtil.cpp:113
bool isThreadExitCall(const CallICFGNode *cs)
Definition SVFUtil.h:369
std::string bugMsg2(const std::string &msg)
Definition SVFUtil.cpp:92
bool isProgExitCall(const CallICFGNode *cs)
Definition SVFUtil.cpp:395
bool isCallSite(const ICFGNode *inst)
Definition SVFUtil.cpp:320
std::string wrnMsg(const std::string &msg)
Returns warning message by converting a string into yellow string output.
Definition SVFUtil.cpp:67
bool isReallocExtFun(const FunObjVar *fun)
Return true if the call is a heap reallocator.
Definition SVFUtil.h:294
const ValVar * getForkedFun(const CallICFGNode *inst)
Return thread fork function.
Definition SVFUtil.h:324
bool isRetInstNode(const ICFGNode *node)
Definition SVFUtil.cpp:380
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
void timeLimitReached(int signum)
Function to call when alarm for time limit hits.
Definition SVFUtil.cpp:272
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
Definition SVFUtil.h:413
bool isHeapAllocExtCallViaRet(const CallICFGNode *cs)
interfaces to be used externally
Definition SVFUtil.cpp:367
void dumpSet(NodeBS To, OutStream &O=SVFUtil::outs())
Dump sparse bitvector set.
Definition SVFUtil.cpp:153
void removeKey(const Key &key, KeySet &keySet)
Removes an element from a Set/CondSet (or anything implementing ::erase).
Definition SVFUtil.h:238
void insertKey(const Key &key, KeySet &keySet)
Inserts an element into a Set/CondSet (with ::insert).
Definition SVFUtil.h:225
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:189
void dumpPointsToList(const PointsToList &ptl)
Definition SVFUtil.cpp:128
bool isProgExitFunction(const FunObjVar *fun)
Return true if this is a program exit function call.
Definition SVFUtil.cpp:388
const FunObjVar * getProgFunction(const std::string &funName)
Get program entry function from function name.
Definition SVFUtil.cpp:401
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
std::ostream OutStream
Definition GeneralType.h:66
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67
bool operator()(const NodeBS &lhs, const NodeBS &rhs) const
Definition SVFUtil.h:155
bool operator()(const PointsTo &lhs, const PointsTo &rhs) const
Definition SVFUtil.h:147