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 "Util/Options.h"
31#include "Util/SVFStat.h"
32#include "Graphs/CallGraph.h"
33
34using namespace SVF;
35using namespace std;
36
41
42SVFStat::SVFStat() : startTime(0), endTime(0)
43{
45 && "PTAStat: unknown clock type!");
46}
47
49{
50 if (Options::MarkedClocksOnly() && !mark) return 0.0;
51
53 {
54 struct timespec time;
56 return (double)(time.tv_nsec + time.tv_sec * 1000000000) / 1000000.0;
57 }
59 {
60 return CLOCK_IN_MS();
61 }
62
63 assert(false && "PTAStat::getClk: unknown clock type");
64 abort();
65}
66
68{
69
70 std::string moduleName(SVFIR::getPAG()->getModule()->getModuleIdentifier());
71 std::vector<std::string> names = SVFUtil::split(moduleName,'/');
72 if (names.size() > 1)
73 {
74 moduleName = names[names.size() - 1];
75 }
76
77 SVFUtil::outs() << "\n*********" << statname << "***************\n";
78 SVFUtil::outs() << "################ (program : " << moduleName << ")###############\n";
79 SVFUtil::outs().flags(std::ios::left);
80 unsigned field_width = 20;
81
82 for(NUMStatMap::iterator it = generalNumMap.begin(), eit = generalNumMap.end(); it!=eit; ++it)
83 {
84 // format out put with width 20 space
85 std::cout << std::setw(field_width) << it->first << it->second << "\n";
86 }
87
88 if(!timeStatMap.empty())
89 SVFUtil::outs() << "----------------Time and memory stats--------------------\n";
90 for(TIMEStatMap::iterator it = timeStatMap.begin(), eit = timeStatMap.end(); it!=eit; ++it)
91 {
92 // format out put with width 20 space
93 SVFUtil::outs() << std::setw(field_width) << it->first << it->second << "\n";
94 }
95
96 if(!PTNumStatMap.empty())
97 SVFUtil::outs() << "----------------Numbers stats----------------------------\n";
98 for(NUMStatMap::iterator it = PTNumStatMap.begin(), eit = PTNumStatMap.end(); it!=eit; ++it)
99 {
100 // format out put with width 20 space
101 SVFUtil::outs() << std::setw(field_width) << it->first << it->second << "\n";
102 }
103 SVFUtil::outs() << "#######################################################" << std::endl;
104 SVFUtil::outs().flush();
105 generalNumMap.clear();
106 PTNumStatMap.clear();
107 timeStatMap.clear();
108}
109
111{
112
114 if(printGeneralStats == false)
115 return;
116
117 SVFIR* pag = SVFIR::getPAG();
119 u32_t numOfGlobal = 0;
120 u32_t numOfStack = 0;
121 u32_t numOfHeap = 0;
126 u32_t numOfScalar = 0;
128 u32_t fiObjNumber = 0;
129 u32_t fsObjNumber = 0;
131 for(SVFIR::iterator it = pag->begin(), eit = pag->end(); it!=eit; ++it)
132 {
133 PAGNode* node = it->second;
134 if(ObjVar* obj = SVFUtil::dyn_cast<ObjVar>(node))
135 {
136 const MemObj* mem = obj->getMemObj();
137 if (memObjSet.insert(mem->getId()).second == false)
138 continue;
139 if(mem->isBlackHoleObj())
140 continue;
141 if(mem->isFunction())
143 if(mem->isGlobalObj())
144 numOfGlobal++;
145 if (pag->getBaseObject(obj->getId()) &&
146 SVFUtil::isa<StackObjVar>(
147 pag->getBaseObject(obj->getId())))
148 numOfStack++;
149 if (pag->getBaseObject(obj->getId()) &&
150 SVFUtil::isa<HeapObjVar, DummyObjVar>(
151 pag->getBaseObject(obj->getId())))
152 {
153 numOfHeap++;
154 }
155 if(mem->isVarArray())
157 if(mem->isVarStruct())
159 if(mem->isConstantArray())
161 if(mem->isConstantStruct())
163 if(mem->getType()->isPointerTy() == false)
164 numOfScalar++;
165 if(mem->isConstDataOrConstGlobal())
167
168 if (mem->isFieldInsensitive())
169 fiObjNumber++;
170 else
171 fsObjNumber++;
172 }
173 }
174
175
176
177 generalNumMap["TotalPointers"] = pag->getValueNodeNum() + pag->getFieldValNodeNum();
178 generalNumMap["TotalObjects"] = pag->getObjectNodeNum();
179 generalNumMap["TotalFieldObjects"] = pag->getFieldObjNodeNum();
181 generalNumMap["TotalSVFStmts"] = pag->getPAGEdgeNum();
182 generalNumMap["TotalPTASVFStmts"] = pag->getPTAPAGEdgeNum();
183 generalNumMap["FIObjNum"] = fiObjNumber;
184 generalNumMap["FSObjNum"] = fsObjNumber;
185
186 generalNumMap["AddrsNum"] = pag->getSVFStmtSet(SVFStmt::Addr).size();
187 generalNumMap["LoadsNum"] = pag->getSVFStmtSet(SVFStmt::Load).size();
188 generalNumMap["StoresNum"] = pag->getSVFStmtSet(SVFStmt::Store).size();
189 generalNumMap["CopysNum"] = pag->getSVFStmtSet(SVFStmt::Copy).size();
190 generalNumMap["GepsNum"] = pag->getSVFStmtSet(SVFStmt::Gep).size();
191 generalNumMap["CallsNum"] = pag->getSVFStmtSet(SVFStmt::Call).size();
192 generalNumMap["ReturnsNum"] = pag->getSVFStmtSet(SVFStmt::Ret).size();
193
194 generalNumMap["FunctionObjs"] = numOfFunction;
195 generalNumMap["GlobalObjs"] = numOfGlobal;
196 generalNumMap["HeapObjs"] = numOfHeap;
197 generalNumMap["StackObjs"] = numOfStack;
198
199 generalNumMap["VarStructObj"] = numOfHasVarStruct;
200 generalNumMap["VarArrayObj"] = numOfHasVarArray;
201 generalNumMap["ConstStructObj"] = numOfHasConstStruct;
202 generalNumMap["ConstArrayObj"] = numOfHasConstArray;
203 generalNumMap["NonPtrObj"] = numOfScalar;
204 generalNumMap["ConstantObj"] = numOfConstant;
205
206 generalNumMap["IndCallSites"] = pag->getIndirectCallsites().size();
207 generalNumMap["TotalCallSite"] = pag->getCallSiteSet().size();
208
212
213 // REFACTOR-TODO bitcastInstStat();
214 branchStat();
215
216 printStat("General Stats");
217
218 printGeneralStats = false;
219}
220
221
223{
227 for (const auto& item: *svfirCallGraph)
228 {
229 const SVFFunction* func = item.second->getFunction();
230 for (SVFFunction::const_iterator bbIt = func->begin(), bbEit = func->end();
231 bbIt != bbEit; ++bbIt)
232 {
233 const SVFBasicBlock* bb = *bbIt;
235 if (numOfSucc == 2)
237 else if (numOfSucc > 2)
239 }
240 }
241
242 generalNumMap["BBWith2Succ"] = numOfBB_2Succ;
243 generalNumMap["BBWith3Succ"] = numOfBB_3Succ;
244}
245
246/* REFACTOR-TODO
247void PTAStat::bitcastInstStat()
248{
249 SVFModule* module = pta->getModule();
250 u32_t numberOfBitCast = 0;
251 for (SVFModule::llvm_const_iterator funIter = module->llvmFunBegin(), funEiter = module->llvmFunEnd();
252 funIter != funEiter; ++funIter)
253 {
254 const Function* func = *funIter;
255 for (Function::const_iterator bbIt = func->begin(), bbEit = func->end();
256 bbIt != bbEit; ++bbIt)
257 {
258 const BasicBlock& bb = *bbIt;
259 for (BasicBlock::const_iterator instIt = bb.begin(), instEit = bb.end();
260 instIt != instEit; ++instIt)
261 {
262 const Instruction& inst = *instIt;
263 if (const BitCastInst* bitcast = SVFUtil::dyn_cast<BitCastInst>(&inst))
264 {
265 if (SVFUtil::isa<PointerType>(bitcast->getSrcTy()))
266 numberOfBitCast++;
267 }
268 }
269 }
270 }
271
272 generalNumMap["BitCastNumber"] = numberOfBitCast;
273}
274*/
#define CLOCK_IN_MS()
Definition SVFType.h:513
cJSON * item
Definition cJSON.h:222
iterator begin()
Iterators.
IDToNodeMapTy::iterator iterator
Node Iterators.
u32_t getValueNodeNum() const
Definition IRGraph.h:186
u32_t getPTAPAGEdgeNum() const
Definition IRGraph.h:211
u32_t getPAGEdgeNum() const
Definition IRGraph.h:207
u32_t getObjectNodeNum() const
Definition IRGraph.h:190
bool isConstantStruct() const
const SVFType * getType() const
Get obj type.
bool isConstDataOrConstGlobal() const
SymID getId() const
Get the memory object id.
bool isGlobalObj() const
bool isFieldInsensitive() const
Return true if its field limit is 0.
bool isVarArray() const
bool isBlackHoleObj() const
Whether it is a black hole object.
bool isConstantArray() const
bool isVarStruct() const
bool isFunction() const
object attributes methods
static const OptionMap< enum PTAStat::ClockType > ClockType
Definition Options.h:24
static const Option< bool > MarkedClocksOnly
Definition Options.h:31
u32_t getNumSuccessors() const
Definition SVFValue.h:626
const_iterator end() const
Definition SVFValue.h:451
const_iterator begin() const
Definition SVFValue.h:446
std::vector< constSVFBasicBlock * >::const_iterator const_iterator
Definition SVFValue.h:305
const CallSiteSet & getCallSiteSet() const
Get all callsites.
Definition SVFIR.h:255
SVFStmt::SVFStmtSetTy & getSVFStmtSet(SVFStmt::PEDGEK kind)
Get/set methods to get SVFStmts based on their kinds and ICFGNodes.
Definition SVFIR.h:202
CallGraph * getCallGraph()
Definition SVFIR.h:193
u32_t getFieldObjNodeNum() const
Definition SVFIR.h:339
const BaseObjVar * getBaseObject(NodeID id) const
Definition SVFIR.h:405
u32_t getFieldValNodeNum() const
Node and edge statistics.
Definition SVFIR.h:335
const CallSiteToFunPtrMap & getIndirectCallsites() const
Add/get indirect callsites.
Definition SVFIR.h:351
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:116
NUMStatMap generalNumMap
Definition SVFStat.h:76
NUMStatMap PTNumStatMap
Definition SVFStat.h:77
virtual void printStat(std::string str="")
Definition SVFStat.cpp:67
void branchStat()
Definition SVFStat.cpp:222
static bool printGeneralStats
SVF's general statistics are only printed once even if you run multiple anayses.
Definition SVFStat.h:74
std::string moduleName
Definition SVFStat.h:99
TIMEStatMap timeStatMap
Definition SVFStat.h:78
virtual void performStat()=0
Definition SVFStat.cpp:110
static double timeOfBuildingLLVMModule
Definition SVFStat.h:93
static double getClk(bool mark=false)
Definition SVFStat.cpp:48
static double timeOfBuildingSymbolTable
Definition SVFStat.h:94
static double timeOfBuildingSVFIR
Definition SVFStat.h:95
bool isPointerTy() const
Definition SVFType.h:249
static SymbolTableInfo * SymbolInfo()
Singleton design here to make sure we only have one instance during any analysis.
u32_t getMaxStructSize() const
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50
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:203
for isBitcode
Definition BasicTypes.h:68
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
unsigned u32_t
Definition GeneralType.h:46