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 <iomanip>
31
32#include "SVFIR/SVFIR.h"
33#include "Util/Options.h"
34#include "Util/SVFStat.h"
35#include "Graphs/CallGraph.h"
36
37using namespace SVF;
38using namespace std;
39
44
45SVFStat::SVFStat() : startTime(0), endTime(0)
46{
48 && "PTAStat: unknown clock type!");
49}
50
52{
53 if (Options::MarkedClocksOnly() && !mark) return 0.0;
54
56 {
57 struct timespec time;
59 return (double)(time.tv_nsec + time.tv_sec * 1000000000) / 1000000.0;
60 }
62 {
63 return CLOCK_IN_MS();
64 }
65
66 assert(false && "PTAStat::getClk: unknown clock type");
67 abort();
68}
69
71{
72
73 std::string moduleName(SVFIR::getPAG()->getModuleIdentifier());
74 std::vector<std::string> names = SVFUtil::split(moduleName,'/');
75 if (names.size() > 1)
76 {
77 moduleName = names[names.size() - 1];
78 }
79
80 SVFUtil::outs() << "\n*********" << statname << "***************\n";
81 SVFUtil::outs() << "################ (program : " << moduleName << ")###############\n";
82 SVFUtil::outs().flags(std::ios::left);
83 unsigned field_width = 20;
84
85 for(NUMStatMap::iterator it = generalNumMap.begin(), eit = generalNumMap.end(); it!=eit; ++it)
86 {
87 // format out put with width 20 space
88 std::cout << std::setw(field_width) << it->first << it->second << "\n";
89 }
90
91 if(!timeStatMap.empty())
92 SVFUtil::outs() << "----------------Time and memory stats--------------------\n";
93 for(TIMEStatMap::iterator it = timeStatMap.begin(), eit = timeStatMap.end(); it!=eit; ++it)
94 {
95 // format out put with width 20 space
96 SVFUtil::outs() << std::setw(field_width) << it->first << it->second << "\n";
97 }
98
99 if(!PTNumStatMap.empty())
100 SVFUtil::outs() << "----------------Numbers stats----------------------------\n";
101 for(NUMStatMap::iterator it = PTNumStatMap.begin(), eit = PTNumStatMap.end(); it!=eit; ++it)
102 {
103 // format out put with width 20 space
104 SVFUtil::outs() << std::setw(field_width) << it->first << it->second << "\n";
105 }
106 SVFUtil::outs() << "#######################################################" << std::endl;
107 SVFUtil::outs().flush();
108 generalNumMap.clear();
109 PTNumStatMap.clear();
110 timeStatMap.clear();
111}
112
114{
115
117 if(printGeneralStats == false)
118 return;
119
120 SVFIR* pag = SVFIR::getPAG();
122 u32_t numOfGlobal = 0;
123 u32_t numOfStack = 0;
124 u32_t numOfHeap = 0;
129 u32_t numOfScalar = 0;
131 u32_t fiObjNumber = 0;
132 u32_t fsObjNumber = 0;
134 for(SVFIR::iterator it = pag->begin(), eit = pag->end(); it!=eit; ++it)
135 {
136 PAGNode* node = it->second;
137 if(ObjVar* obj = SVFUtil::dyn_cast<ObjVar>(node))
138 {
139 const BaseObjVar* mem = pag->getBaseObject(obj->getId());
140 if (memObjSet.insert(mem->getId()).second == false)
141 continue;
142 if(mem->isBlackHoleObj())
143 continue;
144 if(mem->isFunction())
146 if(mem->isGlobalObj())
147 numOfGlobal++;
148 if (pag->getBaseObject(obj->getId()) &&
149 SVFUtil::isa<StackObjVar>(
150 pag->getBaseObject(obj->getId())))
151 numOfStack++;
152 if (pag->getBaseObject(obj->getId()) &&
153 SVFUtil::isa<HeapObjVar, DummyObjVar>(
154 pag->getBaseObject(obj->getId())))
155 {
156 numOfHeap++;
157 }
158 if(mem->isVarArray())
160 if(mem->isVarStruct())
162 if(mem->isConstantArray())
164 if(mem->isConstantStruct())
166 if(mem->getType()->isPointerTy() == false)
167 numOfScalar++;
168 if(mem->isConstDataOrConstGlobal())
170
171 if (mem->isFieldInsensitive())
172 fiObjNumber++;
173 else
174 fsObjNumber++;
175 }
176 }
177
178
179
180 generalNumMap["TotalPointers"] = pag->getValueNodeNum();
181 generalNumMap["TotalObjects"] = pag->getObjectNodeNum();
182 generalNumMap["TotalFieldObjects"] = pag->getFieldObjNodeNum();
183 generalNumMap["MaxStructSize"] = pag->getMaxStructSize();
184 generalNumMap["TotalSVFStmts"] = pag->getSVFStmtNum();
185 generalNumMap["TotalPTASVFStmts"] = pag->getPTAPAGEdgeNum();
186 generalNumMap["FIObjNum"] = fiObjNumber;
187 generalNumMap["FSObjNum"] = fsObjNumber;
188
189 generalNumMap["AddrsNum"] = pag->getSVFStmtSet(SVFStmt::Addr).size();
190 generalNumMap["LoadsNum"] = pag->getSVFStmtSet(SVFStmt::Load).size();
191 generalNumMap["StoresNum"] = pag->getSVFStmtSet(SVFStmt::Store).size();
192 generalNumMap["CopysNum"] = pag->getSVFStmtSet(SVFStmt::Copy).size();
193 generalNumMap["GepsNum"] = pag->getSVFStmtSet(SVFStmt::Gep).size();
194 generalNumMap["CallsNum"] = pag->getSVFStmtSet(SVFStmt::Call).size();
195 generalNumMap["ReturnsNum"] = pag->getSVFStmtSet(SVFStmt::Ret).size();
196
197 generalNumMap["FunctionObjs"] = numOfFunction;
198 generalNumMap["GlobalObjs"] = numOfGlobal;
199 generalNumMap["HeapObjs"] = numOfHeap;
200 generalNumMap["StackObjs"] = numOfStack;
201
202 generalNumMap["VarStructObj"] = numOfHasVarStruct;
203 generalNumMap["VarArrayObj"] = numOfHasVarArray;
204 generalNumMap["ConstStructObj"] = numOfHasConstStruct;
205 generalNumMap["ConstArrayObj"] = numOfHasConstArray;
206 generalNumMap["NonPtrObj"] = numOfScalar;
207 generalNumMap["ConstantObj"] = numOfConstant;
208
209 generalNumMap["IndCallSites"] = pag->getIndirectCallsites().size();
210 generalNumMap["TotalCallSite"] = pag->getCallSiteSet().size();
211
215
216 // REFACTOR-TODO bitcastInstStat();
217 branchStat();
218
219 printStat("General Stats");
220
221 printGeneralStats = false;
222}
223
224
226{
230 for (const auto& item: *svfirCallGraph)
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;
238 if (numOfSucc == 2)
240 else if (numOfSucc > 2)
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:622
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
iterator begin()
Iterators.
IDToNodeMapTy::iterator iterator
Node Iterators.
u32_t getMaxStructSize() const
Definition IRGraph.h:204
u32_t getObjectNodeNum()
Definition IRGraph.cpp:343
u32_t getSVFStmtNum() const
Definition IRGraph.h:334
u32_t getPTAPAGEdgeNum() const
Definition IRGraph.h:338
u32_t getValueNodeNum()
Definition IRGraph.cpp:330
static const Option< bool > MarkedClocksOnly
Definition Options.h:28
static const OptionMap< SVFStat::ClockType > ClockType
Definition Options.h:21
u32_t getNumSuccessors() const
const CallSiteSet & getCallSiteSet() const
Get all callsites.
Definition SVFIR.h:349
SVFStmt::SVFStmtSetTy & getSVFStmtSet(SVFStmt::PEDGEK kind)
Get/set methods to get SVFStmts based on their kinds and ICFGNodes.
Definition SVFIR.h:296
u32_t getFieldObjNodeNum() const
Definition SVFIR.h:439
const BaseObjVar * getBaseObject(NodeID id) const
Definition SVFIR.h:496
const CallGraph * getCallGraph()
Get CG.
Definition SVFIR.h:246
const CallSiteToFunPtrMap & getIndirectCallsites() const
Add/get indirect callsites.
Definition SVFIR.h:451
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:118
NUMStatMap generalNumMap
Definition SVFStat.h:78
NUMStatMap PTNumStatMap
Definition SVFStat.h:79
virtual void printStat(std::string str="")
Definition SVFStat.cpp:70
void branchStat()
Definition SVFStat.cpp:225
static bool printGeneralStats
SVF's general statistics are only printed once even if you run multiple anayses.
Definition SVFStat.h:76
std::string moduleName
Definition SVFStat.h:101
TIMEStatMap timeStatMap
Definition SVFStat.h:80
virtual void performStat()=0
Definition SVFStat.cpp:113
static double timeOfBuildingLLVMModule
Definition SVFStat.h:95
static double getClk(bool mark=false)
Definition SVFStat.cpp:51
static double timeOfBuildingSymbolTable
Definition SVFStat.h:96
static double timeOfBuildingSVFIR
Definition SVFStat.h:97
bool isPointerTy() const
Definition SVFType.h:294
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
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:47