Static Value-Flow Analysis
Loading...
Searching...
No Matches
SVFStat.cpp
Go to the documentation of this file.
1//===- SVFStat.cpp -- Base class for statistics---------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <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 * SVFStat.cpp
25 *
26 * Created on: Sep 1, 2022
27 * Author: Xiao Cheng
28 */
29
30#include <chrono>
31#include <iomanip>
32
33#include "Graphs/PAG.h"
34#include "Util/Options.h"
35#include "Util/SVFStat.h"
36#include "Graphs/CallGraph.h"
37
38using namespace SVF;
39using namespace std;
40
45
47{
49 && "PTAStat: unknown clock type!");
50}
51
52double SVFStat::getClk(bool mark)
53{
54 if (Options::MarkedClocksOnly() && !mark) return 0.0;
55
57 {
58 return std::chrono::duration<double, std::milli>(
59 std::chrono::steady_clock::now().time_since_epoch()
60 ).count();
61 }
63 {
64 return CLOCK_IN_MS();
65 }
66
67 assert(false && "PTAStat::getClk: unknown clock type");
68 abort();
69}
70
71void SVFStat::printStat(string statname)
72{
73
74 std::string moduleName(PAG::getPAG()->getModuleIdentifier());
75 std::vector<std::string> names = SVFUtil::split(moduleName,'/');
76 if (names.size() > 1)
77 {
78 moduleName = names[names.size() - 1];
79 }
80
81 SVFUtil::outs() << "\n*********" << statname << "***************\n";
82 SVFUtil::outs() << "################ (program : " << moduleName << ")###############\n";
83 SVFUtil::outs().flags(std::ios::left);
84 unsigned field_width = 20;
85
86 for(NUMStatMap::iterator it = generalNumMap.begin(), eit = generalNumMap.end(); it!=eit; ++it)
87 {
88 // format out put with width 20 space
89 std::cout << std::setw(field_width) << it->first << it->second << "\n";
90 }
91
92 if(!timeStatMap.empty())
93 SVFUtil::outs() << "----------------Time and memory stats--------------------\n";
94 for(TIMEStatMap::iterator it = timeStatMap.begin(), eit = timeStatMap.end(); it!=eit; ++it)
95 {
96 // format out put with width 20 space
97 SVFUtil::outs() << std::setw(field_width) << it->first << it->second << "\n";
98 }
99
100 if(!PTNumStatMap.empty())
101 SVFUtil::outs() << "----------------Numbers stats----------------------------\n";
102 for(NUMStatMap::iterator it = PTNumStatMap.begin(), eit = PTNumStatMap.end(); it!=eit; ++it)
103 {
104 // format out put with width 20 space
105 SVFUtil::outs() << std::setw(field_width) << it->first << it->second << "\n";
106 }
107 SVFUtil::outs() << "#######################################################" << std::endl;
108 SVFUtil::outs().flush();
109 generalNumMap.clear();
110 PTNumStatMap.clear();
111 timeStatMap.clear();
112}
113
115{
116
118 if(printGeneralStats == false)
119 return;
120
121 PAG* pag = PAG::getPAG();
122 u32_t numOfFunction = 0;
123 u32_t numOfGlobal = 0;
124 u32_t numOfStack = 0;
125 u32_t numOfHeap = 0;
126 u32_t numOfHasVarArray = 0;
127 u32_t numOfHasVarStruct = 0;
128 u32_t numOfHasConstArray = 0;
129 u32_t numOfHasConstStruct = 0;
130 u32_t numOfScalar = 0;
131 u32_t numOfConstant = 0;
132 u32_t fiObjNumber = 0;
133 u32_t fsObjNumber = 0;
134 Set<NodeID> memObjSet;
135 for(PAG::iterator it = pag->begin(), eit = pag->end(); it!=eit; ++it)
136 {
137 PAGNode* node = it->second;
138 if(ObjVar* obj = SVFUtil::dyn_cast<ObjVar>(node))
139 {
140 const BaseObjVar* mem = pag->getBaseObject(obj->getId());
141 if (memObjSet.insert(mem->getId()).second == false)
142 continue;
143 if(mem->isBlackHoleObj())
144 continue;
145 if(mem->isFunction())
146 numOfFunction++;
147 if(mem->isGlobalObj())
148 numOfGlobal++;
149 if (pag->getBaseObject(obj->getId()) &&
151 pag->getBaseObject(obj->getId())))
152 numOfStack++;
153 if (pag->getBaseObject(obj->getId()) &&
155 pag->getBaseObject(obj->getId())))
156 {
157 numOfHeap++;
158 }
159 if(mem->isVarArray())
160 numOfHasVarArray++;
161 if(mem->isVarStruct())
162 numOfHasVarStruct++;
163 if(mem->isConstantArray())
164 numOfHasConstArray++;
165 if(mem->isConstantStruct())
166 numOfHasConstStruct++;
167 if(mem->getType()->isPointerTy() == false)
168 numOfScalar++;
169 if(mem->isConstDataOrConstGlobal())
170 numOfConstant++;
171
172 if (mem->isFieldInsensitive())
173 fiObjNumber++;
174 else
175 fsObjNumber++;
176 }
177 }
178
179
180
181 generalNumMap["TotalPointers"] = pag->getValueNodeNum();
182 generalNumMap["TotalObjects"] = pag->getObjectNodeNum();
183 generalNumMap["TotalFieldObjects"] = pag->getFieldObjNodeNum();
184 generalNumMap["MaxStructSize"] = pag->getMaxStructSize();
185 generalNumMap["TotalSVFStmts"] = pag->getSVFStmtNum();
186 generalNumMap["TotalPTASVFStmts"] = pag->getPTAPAGEdgeNum();
187 generalNumMap["FIObjNum"] = fiObjNumber;
188 generalNumMap["FSObjNum"] = fsObjNumber;
189
190 generalNumMap["AddrsNum"] = pag->getSVFStmtSet(SVFStmt::Addr).size();
191 generalNumMap["LoadsNum"] = pag->getSVFStmtSet(SVFStmt::Load).size();
192 generalNumMap["StoresNum"] = pag->getSVFStmtSet(SVFStmt::Store).size();
193 generalNumMap["CopysNum"] = pag->getSVFStmtSet(SVFStmt::Copy).size();
194 generalNumMap["GepsNum"] = pag->getSVFStmtSet(SVFStmt::Gep).size();
195 generalNumMap["CallsNum"] = pag->getSVFStmtSet(SVFStmt::Call).size();
196 generalNumMap["ReturnsNum"] = pag->getSVFStmtSet(SVFStmt::Ret).size();
197
198 generalNumMap["FunctionObjs"] = numOfFunction;
199 generalNumMap["GlobalObjs"] = numOfGlobal;
200 generalNumMap["HeapObjs"] = numOfHeap;
201 generalNumMap["StackObjs"] = numOfStack;
202
203 generalNumMap["VarStructObj"] = numOfHasVarStruct;
204 generalNumMap["VarArrayObj"] = numOfHasVarArray;
205 generalNumMap["ConstStructObj"] = numOfHasConstStruct;
206 generalNumMap["ConstArrayObj"] = numOfHasConstArray;
207 generalNumMap["NonPtrObj"] = numOfScalar;
208 generalNumMap["ConstantObj"] = numOfConstant;
209
210 generalNumMap["IndCallSites"] = pag->getIndirectCallsites().size();
211 generalNumMap["TotalCallSite"] = pag->getCallSiteSet().size();
212
216
217 // REFACTOR-TODO bitcastInstStat();
218 branchStat();
219
220 printStat("General Stats");
221
222 printGeneralStats = false;
223}
224
225
227{
228 u32_t numOfBB_2Succ = 0;
229 u32_t numOfBB_3Succ = 0;
230 for (const auto& item: *(PAG::getPAG()->getCallGraph()))
231 {
232 const FunObjVar* func = item.second->getFunction();
233 for (FunObjVar::const_bb_iterator bbIt = func->begin(), bbEit = func->end();
234 bbIt != bbEit; ++bbIt)
235 {
236 const SVFBasicBlock* bb = bbIt->second;
237 u32_t numOfSucc = bb->getNumSuccessors();
238 if (numOfSucc == 2)
239 numOfBB_2Succ++;
240 else if (numOfSucc > 2)
241 numOfBB_3Succ++;
242 }
243 }
244
245 generalNumMap["BBWith2Succ"] = numOfBB_2Succ;
246 generalNumMap["BBWith3Succ"] = numOfBB_3Succ;
247}
248
249/* REFACTOR-TODO
250void PTAStat::bitcastInstStat()
251{
252 SVFModule* module = pta->getModule();
253 u32_t numberOfBitCast = 0;
254 for (SVFModule::llvm_const_iterator funIter = module->llvmFunBegin(), funEiter = module->llvmFunEnd();
255 funIter != funEiter; ++funIter)
256 {
257 const Function* func = *funIter;
258 for (Function::const_iterator bbIt = func->begin(), bbEit = func->end();
259 bbIt != bbEit; ++bbIt)
260 {
261 const BasicBlock& bb = *bbIt;
262 for (BasicBlock::const_iterator instIt = bb.begin(), instEit = bb.end();
263 instIt != instEit; ++instIt)
264 {
265 const Instruction& inst = *instIt;
266 if (const BitCastInst* bitcast = SVFUtil::dyn_cast<BitCastInst>(&inst))
267 {
268 if (SVFUtil::isa<PointerType>(bitcast->getSrcTy()))
269 numberOfBitCast++;
270 }
271 }
272 }
273 }
274
275 generalNumMap["BitCastNumber"] = numberOfBitCast;
276}
277*/
#define CLOCK_IN_MS()
Definition SVFType.h:605
cJSON * item
Definition cJSON.h:222
bool isFunction() const
object attributes methods
bool isConstDataOrConstGlobal() const
bool isGlobalObj() const
bool isConstantStruct() const
bool isVarStruct() const
NodeID getId() const
Get the memory object id.
bool isBlackHoleObj() const
Whether it is a black hole object.
bool isVarArray() const
bool isFieldInsensitive() const
Return true if its field limit is 0.
const SVFType * getType() const
Get obj type.
bool isConstantArray() const
BasicBlockGraph::IDToNodeMapTy::const_iterator const_bb_iterator
const_bb_iterator begin() const
const_bb_iterator end() const
iterator begin()
Iterators.
IDToNodeMapTy::iterator iterator
static const Option< bool > MarkedClocksOnly
Definition Options.h:27
static const OptionMap< SVFStat::ClockType > ClockType
Definition Options.h:20
u32_t getMaxStructSize() const
Definition PAGBase.h:207
u32_t getObjectNodeNum()
Definition IRGraph.cpp:360
u32_t getPTAPAGEdgeNum() const
Definition PAGBase.h:341
u32_t getSVFStmtNum() const
Definition PAGBase.h:337
u32_t getValueNodeNum()
Definition IRGraph.cpp:347
Definition PAG.h:46
static PAG * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition PAG.h:121
const BaseObjVar * getBaseObject(NodeID id) const
Definition PAG.h:499
u32_t getFieldObjNodeNum() const
Definition PAG.h:442
SVFStmt::SVFStmtSetTy & getSVFStmtSet(SVFStmt::PEDGEK kind)
Get/set methods to get SVFStmts based on their kinds and ICFGNodes.
Definition PAG.h:299
const CallSiteToFunPtrMap & getIndirectCallsites() const
Add/get indirect callsites.
Definition PAG.h:454
const CallSiteSet & getCallSiteSet() const
Get all callsites.
Definition PAG.h:352
u32_t getNumSuccessors() const
NUMStatMap generalNumMap
Definition SVFStat.h:79
NUMStatMap PTNumStatMap
Definition SVFStat.h:80
virtual void printStat(std::string str="")
Definition SVFStat.cpp:71
void branchStat()
Definition SVFStat.cpp:226
static bool printGeneralStats
SVF's general statistics are only printed once even if you run multiple anayses.
Definition SVFStat.h:77
std::string moduleName
Definition SVFStat.h:102
TIMEStatMap timeStatMap
Definition SVFStat.h:81
virtual void performStat()=0
Definition SVFStat.cpp:114
double endTime
Definition SVFStat.h:84
static double timeOfBuildingLLVMModule
Definition SVFStat.h:96
static double timeOfBuildingPAG
Definition SVFStat.h:98
double startTime
Definition SVFStat.h:83
static double getClk(bool mark=false)
Definition SVFStat.cpp:52
static double timeOfBuildingSymbolTable
Definition SVFStat.h:97
bool isPointerTy() const
Definition SVFType.h:292
LLVM_NODISCARD bool isa(const Y &Val)
Definition Casting.h:241
std::ostream & outs()
Overwrite llvm::outs().
Definition SVFUtil.h:58
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition Casting.h:405
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
for isBitcode
Definition BasicTypes.h:70
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51
SVFVar PAGNode
Definition PAGBase.h:40
unsigned u32_t
Definition GeneralType.h:67