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
42#ifdef _MSC_VER
43# define SVF_WEAK inline
44#else
45# define SVF_WEAK __attribute__((weak))
46#endif
47
48namespace SVF
49{
50
51/*
52 * Util class to assist pointer analysis
53 */
54namespace SVFUtil
55{
56
58inline std::ostream &outs()
59{
60 return std::cout;
61}
62
64inline std::ostream &errs()
65{
66 return std::cerr;
67}
68
72
74void dumpPointsToSet(unsigned node, NodeBS To) ;
75void dumpSparseSet(const NodeBS& To);
76
78void dumpAliasSet(unsigned node, NodeBS To) ;
79
81std::string sucMsg(const std::string& msg);
82
84std::string wrnMsg(const std::string& msg);
85
87void writeWrnMsg(const std::string& msg);
88
90
91std::string errMsg(const std::string& msg);
92std::string bugMsg1(const std::string& msg);
93std::string bugMsg2(const std::string& msg);
94std::string bugMsg3(const std::string& msg);
96
98std::string pasMsg(const std::string& msg);
99
101void reportMemoryUsageKB(const std::string& infor,
102 OutStream& O = SVFUtil::outs());
103
106
108void increaseStackSize();
109
111bool fileExists(const std::string& path);
112
114std::string getStdoutFromCommand(const std::string& command);
115
117std::string getCurrentSOPath();
118
124inline bool cmpPts (const PointsTo& lpts,const PointsTo& rpts)
125{
126 if (lpts.count() != rpts.count())
127 return (lpts.count() < rpts.count());
128 else
129 {
130 PointsTo::iterator bit = lpts.begin(), eit = lpts.end();
131 PointsTo::iterator rbit = rpts.begin(), reit = rpts.end();
132 for (; bit != eit && rbit != reit; bit++, rbit++)
133 {
134 if (*bit != *rbit)
135 return (*bit < *rbit);
136 }
137
138 return false;
139 }
140}
141
142inline bool cmpNodeBS(const NodeBS& lpts,const NodeBS& rpts)
143{
144 if (lpts.count() != rpts.count())
145 return (lpts.count() < rpts.count());
146 else
147 {
148 NodeBS::iterator bit = lpts.begin(), eit = lpts.end();
149 NodeBS::iterator rbit = rpts.begin(), reit = rpts.end();
150 for (; bit != eit && rbit != reit; bit++, rbit++)
151 {
152 if (*bit != *rbit)
153 return (*bit < *rbit);
154 }
155
156 return false;
157 }
158}
159
160typedef struct equalPointsTo
161{
162 bool operator()(const PointsTo& lhs, const PointsTo& rhs) const
163 {
164 return SVFUtil::cmpPts(lhs, rhs);
165 }
166} equalPointsTo;
167
168typedef struct equalNodeBS
169{
170 bool operator()(const NodeBS& lhs, const NodeBS& rhs) const
171 {
172 return SVFUtil::cmpNodeBS(lhs, rhs);
173 }
174} equalNodeBS;
175
178
180bool isIntrinsicInst(const ICFGNode* inst);
182
183
184bool isCallSite(const ICFGNode* inst);
185
186bool isRetInstNode(const ICFGNode* node);
187
188
190inline bool isNonInstricCallSite(const ICFGNode* inst)
191{
192 if(isIntrinsicInst(inst))
193 return false;
194 return isCallSite(inst);
195}
196
200bool matchArgs(const CallICFGNode* cs, const FunObjVar* callee);
201
202
204inline std::vector<std::string> split(const std::string& s, char separator)
205{
206 std::vector<std::string> output;
207 std::string::size_type prev_pos = 0, pos = 0;
208 while ((pos = s.find(separator, pos)) != std::string::npos)
209 {
210 std::string substring(s.substr(prev_pos, pos - prev_pos));
211 if (!substring.empty())
212 {
213 output.push_back(substring);
214 }
215 prev_pos = ++pos;
216 }
217 std::string lastSubstring(s.substr(prev_pos, pos - prev_pos));
218 if (!lastSubstring.empty())
219 {
220 output.push_back(lastSubstring);
221 }
222 return output;
223}
224
226template <typename Data>
228{
229 for (const typename Map<Data, unsigned>::value_type &ptocc : from)
230 {
231 to[ptocc.first] += ptocc.second;
232 }
233}
234
237
239template <typename Key, typename KeySet>
240inline void insertKey(const Key &key, KeySet &keySet)
241{
242 keySet.insert(key);
243}
244
246inline void insertKey(const NodeID &key, NodeBS &keySet)
247{
248 keySet.set(key);
249}
250
252template <typename Key, typename KeySet>
253inline void removeKey(const Key &key, KeySet &keySet)
254{
255 keySet.erase(key);
256}
257
259inline void removeKey(const NodeID &key, NodeBS &keySet)
260{
261 keySet.reset(key);
262}
263
265void timeLimitReached(int signum);
266
269
271void stopAnalysisLimitTimer(void);
272
275
276
277bool isExtCall(const FunObjVar* fun);
278
279
281
282
283
284inline bool isHeapAllocExtFunViaRet(const FunObjVar* fun)
285{
286 return fun && (ExtAPI::getExtAPI()->is_alloc(fun)
287 || ExtAPI::getExtAPI()->is_realloc(fun));
288}
289
290inline bool isHeapAllocExtFunViaArg(const FunObjVar* fun)
291{
292 return fun && ExtAPI::getExtAPI()->is_arg_alloc(fun);
293}
294
296
297
303
304
305
306inline bool isReallocExtFun(const FunObjVar* fun)
307{
308 return fun && (ExtAPI::getExtAPI()->is_realloc(fun));
309}
310
312
313
314
315bool isProgEntryFunction(const FunObjVar*);
316
318const FunObjVar* getProgFunction(const std::string& funName);
319
322
323
325
326bool isProgExitFunction(const FunObjVar *fun);
327
328
329
331
333
335
336inline const ValVar* getForkedFun(const CallICFGNode *inst)
337{
339}
341
342
343bool isExtCall(const CallICFGNode* cs);
344
345bool isExtCall(const ICFGNode* node);
346
348
349
352
353bool isHeapAllocExtCall(const ICFGNode* cs);
354
356
359
360bool isReallocExtCall(const CallICFGNode* cs);
362
365inline bool isThreadForkCall(const CallICFGNode *inst)
366{
367 return ThreadAPI::getThreadAPI()->isTDFork(inst);
368}
370
373inline bool isThreadJoinCall(const CallICFGNode* cs)
374{
375 return ThreadAPI::getThreadAPI()->isTDJoin(cs);
376}
378
381inline bool isThreadExitCall(const CallICFGNode* cs)
382{
383 return ThreadAPI::getThreadAPI()->isTDExit(cs);
384}
386
389inline bool isLockAquireCall(const CallICFGNode* cs)
390{
392}
394
397inline bool isLockReleaseCall(const CallICFGNode* cs)
398{
400}
402
404
405inline bool isBarrierWaitCall(const CallICFGNode* cs)
406{
408}
410
412
418
419
420bool isProgExitCall(const CallICFGNode* cs);
421
422
423template<typename T>
424constexpr typename std::remove_reference<T>::type &&
425move(T &&t) noexcept
426{
427 return std::move(t);
428}
429
430} // End namespace SVFUtil
431
432} // End namespace SVF
433
434#endif /* AnalysisUtil_H_ */
bool is_arg_alloc(const FunObjVar *F)
Definition ExtAPI.cpp:259
s32_t get_alloc_arg_pos(const FunObjVar *F)
Definition ExtAPI.cpp:270
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:43
bool is_realloc(const FunObjVar *F)
Definition ExtAPI.cpp:286
bool is_alloc(const FunObjVar *F)
Definition ExtAPI.cpp:253
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:458
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
Definition SVFUtil.cpp:75
void increaseStackSize()
Increase the stack size limit.
Definition SVFUtil.cpp:249
const ObjVar * getObjVarOfValVar(const ValVar *valVar)
Definition SVFUtil.cpp:520
std::string bugMsg1(const std::string &msg)
Definition SVFUtil.cpp:103
bool isProgEntryFunction(const FunObjVar *)
Program entry function e.g. main.
Definition SVFUtil.cpp:531
std::string hclustMethodToString(hclust_fast_methods method)
Returns a string representation of a hclust method.
Definition SVFUtil.cpp:335
bool isLockAquireCall(const CallICFGNode *cs)
Definition SVFUtil.h:389
bool isIntrinsicInst(const ICFGNode *inst)
Return true if it is an llvm intrinsic instruction.
Definition SVFUtil.cpp:410
u32_t getHeapAllocHoldingArgPosition(const FunObjVar *fun)
Get the position of argument that holds an allocated heap object.
Definition SVFUtil.h:298
const FunObjVar * getProgEntryFunction()
Get program entry function.
Definition SVFUtil.cpp:499
bool isBarrierWaitCall(const CallICFGNode *cs)
Return true if this is a barrier wait call.
Definition SVFUtil.h:405
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:227
bool isHeapAllocExtFunViaRet(const FunObjVar *fun)
Return true if the call is a heap allocator/reallocator.
Definition SVFUtil.h:284
std::string pasMsg(const std::string &msg)
Print each pass/phase message by converting a string into blue string output.
Definition SVFUtil.cpp:121
OrderedSet< PointsTo, equalPointsTo > PointsToList
Definition SVFUtil.h:176
void dumpAliasSet(unsigned node, NodeBS To)
Dump alias set.
Definition SVFUtil.cpp:159
bool isHeapAllocExtFunViaArg(const FunObjVar *fun)
Definition SVFUtil.h:290
bool getMemoryUsageKB(u32_t *vmrss_kb, u32_t *vmsize_kb)
Get memory usage from system file. Return TRUE if succeed.
Definition SVFUtil.cpp:199
void reportMemoryUsageKB(const std::string &infor, OutStream &O=SVFUtil::outs())
Print memory usage in KB.
Definition SVFUtil.cpp:189
void stopAnalysisLimitTimer(void)
Stops analysis timer.
Definition SVFUtil.cpp:383
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition SVFUtil.cpp:98
std::string bugMsg3(const std::string &msg)
Definition SVFUtil.cpp:113
bool isHeapAllocExtCallViaArg(const CallICFGNode *cs)
Definition SVFUtil.cpp:428
bool isHeapAllocExtCall(const ICFGNode *cs)
Definition SVFUtil.cpp:446
bool isNonInstricCallSite(const ICFGNode *inst)
Whether an instruction is a callsite in the application code, excluding llvm intrinsic calls.
Definition SVFUtil.h:190
bool isThreadJoinCall(const CallICFGNode *cs)
Definition SVFUtil.h:373
bool isArgOfUncalledFunction(const SVFVar *svfvar)
Definition SVFUtil.cpp:511
bool cmpNodeBS(const NodeBS &lpts, const NodeBS &rpts)
Definition SVFUtil.h:142
std::ostream & errs()
Overwrite llvm::errs()
Definition SVFUtil.h:64
bool cmpPts(const PointsTo &lpts, const PointsTo &rpts)
Definition SVFUtil.h:124
bool isExtCall(const FunObjVar *fun)
Definition SVFUtil.cpp:526
bool isThreadForkCall(const CallICFGNode *inst)
Definition SVFUtil.h:365
bool matchArgs(const CallICFGNode *cs, const FunObjVar *callee)
Definition SVFUtil.cpp:397
void startAnalysisLimitTimer(unsigned timeLimit)
Starts analysis timer. timeLimit must be non-0. Timer must not be already set.
Definition SVFUtil.cpp:364
void writeWrnMsg(const std::string &msg)
Writes a message run through wrnMsg.
Definition SVFUtil.cpp:88
void dumpSparseSet(const NodeBS &To)
Definition SVFUtil.cpp:137
const ValVar * getActualParmAtForkSite(const CallICFGNode *cs)
Return sole argument of the thread routine.
Definition SVFUtil.h:413
bool isLockReleaseCall(const CallICFGNode *cs)
Definition SVFUtil.h:397
void dumpPointsToSet(unsigned node, NodeBS To)
Dump points-to set.
Definition SVFUtil.cpp:129
bool fileExists(const std::string &path)
Check whether a file exists at the given path.
Definition SVFUtil.cpp:271
std::string getCurrentSOPath()
Return the absolute path of the current module/executable.
Definition SVFUtil.cpp:302
bool isThreadExitCall(const CallICFGNode *cs)
Definition SVFUtil.h:381
std::string bugMsg2(const std::string &msg)
Definition SVFUtil.cpp:108
bool isProgExitCall(const CallICFGNode *cs)
Definition SVFUtil.cpp:480
bool isCallSite(const ICFGNode *inst)
Definition SVFUtil.cpp:405
std::string wrnMsg(const std::string &msg)
Returns warning message by converting a string into yellow string output.
Definition SVFUtil.cpp:83
bool isReallocExtFun(const FunObjVar *fun)
Return true if the call is a heap reallocator.
Definition SVFUtil.h:306
const ValVar * getForkedFun(const CallICFGNode *inst)
Return thread fork function.
Definition SVFUtil.h:336
bool isRetInstNode(const ICFGNode *node)
Definition SVFUtil.cpp:465
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:58
void timeLimitReached(int signum)
Function to call when alarm for time limit hits.
Definition SVFUtil.cpp:355
std::string getStdoutFromCommand(const std::string &command)
Execute a shell command and capture its stdout output.
Definition SVFUtil.cpp:277
constexpr std::remove_reference< T >::type && move(T &&t) noexcept
Definition SVFUtil.h:425
bool isHeapAllocExtCallViaRet(const CallICFGNode *cs)
interfaces to be used externally
Definition SVFUtil.cpp:452
void dumpSet(NodeBS To, OutStream &O=SVFUtil::outs())
Dump sparse bitvector set.
Definition SVFUtil.cpp:169
void removeKey(const Key &key, KeySet &keySet)
Removes an element from a Set/CondSet (or anything implementing ::erase).
Definition SVFUtil.h:253
void insertKey(const Key &key, KeySet &keySet)
Inserts an element into a Set/CondSet (with ::insert).
Definition SVFUtil.h:240
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:204
void dumpPointsToList(const PointsToList &ptl)
Definition SVFUtil.cpp:144
bool isProgExitFunction(const FunObjVar *fun)
Return true if this is a program exit function call.
Definition SVFUtil.cpp:473
const FunObjVar * getProgFunction(const std::string &funName)
Get program entry function from function name.
Definition SVFUtil.cpp:486
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:170
bool operator()(const PointsTo &lhs, const PointsTo &rhs) const
Definition SVFUtil.h:162