Static Value-Flow Analysis
Loading...
Searching...
No Matches
CFLAlias.cpp
Go to the documentation of this file.
1//===----- CFLAlias.cpp -- CFL Alias Analysis Client--------------//
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 * CFLAlias.cpp
25 *
26 * Created on: June 27 , 2022
27 * Author: Pei Xu
28 */
29
30#include "CFL/CFLAlias.h"
31#include "Util/SVFUtil.h"
32
33using namespace SVF;
34using namespace SVFUtil;
35
42{
43 for(CallSiteToFunPtrMap::const_iterator iter = callsites.begin(), eiter = callsites.end(); iter!=eiter; ++iter)
44 {
45 const CallICFGNode* cs = iter->first;
46
47 if (cs->isVirtualCall())
48 {
49 const SVFVar* vtbl = cs->getVtablePtr();
50
51 assert(vtbl != nullptr);
52 NodeID vtblId = vtbl->getId();
54 }
55 else
57 }
58}
59
65{
66 assert(F);
67
68 DBOUT(DAndersen, outs() << "connect parameters from indirect callsite " << cs->toString() << " to callee " << *F << "\n");
69
70 const CallICFGNode* callBlockNode = cs;
72
74 {
76 }
77
79 {
81 const PAGNode* fun_return = svfir->getFunRet(F);
82 if (cs_return->isPointer() && fun_return->isPointer())
83 {
85 NodeID srcret = fun_return->getId();
87 }
88 else
89 {
90 DBOUT(DAndersen, outs() << "not a pointer ignored\n");
91 }
92 }
93
94 if (svfir->hasCallSiteArgsMap(callBlockNode) && svfir->hasFunArgsList(F))
95 {
96
97 // connect actual and formal param
100 //Go through the fixed parameters.
101 DBOUT(DPAGBuild, outs() << " args:");
102 SVFIR::ValVarList::const_iterator funArgIt = funArgList.begin(), funArgEit = funArgList.end();
103 SVFIR::ValVarList::const_iterator csArgIt = csArgList.begin(), csArgEit = csArgList.end();
104 for (; funArgIt != funArgEit; ++csArgIt, ++funArgIt)
105 {
106 //Some programs (e.g. Linux kernel) leave unneeded parameters empty.
107 if (csArgIt == csArgEit)
108 {
109 DBOUT(DAndersen, outs() << " !! not enough args\n");
110 break;
111 }
112 const PAGNode *cs_arg = *csArgIt ;
113 const PAGNode *fun_arg = *funArgIt;
114
115 if (cs_arg->isPointer() && fun_arg->isPointer())
116 {
117 DBOUT(DAndersen, outs() << "process actual parm " << cs_arg->toString() << " \n");
118 NodeID srcAA = cs_arg->getId();
119 NodeID dstFA = fun_arg->getId();
121 }
122 }
123
124 //Any remaining actual args must be varargs.
125 if (F->isVarArg())
126 {
128 DBOUT(DPAGBuild, outs() << "\n varargs:");
129 for (; csArgIt != csArgEit; ++csArgIt)
130 {
131 const PAGNode *cs_arg = *csArgIt;
132 if (cs_arg->isPointer())
133 {
134 NodeID vnAA = cs_arg->getId();
136 }
137 }
138 }
139 if(csArgIt != csArgEit)
140 {
141 writeWrnMsg("too many args to non-vararg func.");
142 writeWrnMsg("(" + cs->getSourceLoc() + ")");
143 }
144 }
145}
146
148{
149 assert(cs->getCalledFunction() == nullptr && "not an indirect callsite?");
153 CallSite2DummyValPN::const_iterator it = callsite2DummyValPN.find(cs);
154 if(it != callsite2DummyValPN.end())
155 {
156 srcret = it->second;
157 }
158 else
159 {
162 callsite2DummyValPN.insert(std::make_pair(cs,valNode));
165 srcret = valNode;
166 }
167
168 NodeID dstrec = cs_return->getId();
170}
171
176{
179 for(CallEdgeMap::iterator it = newEdges.begin(), eit = newEdges.end(); it!=eit; ++it )
180 {
181 for(FunctionSet::iterator cit = it->second.begin(), ecit = it->second.end(); cit!=ecit; ++cit)
182 {
184 }
185 }
186
187 return (!solver->isWorklistEmpty());
188}
189
191{
192 stat = new CFLStat(this);
193
194 // Parameter Checking
196
197 // Build CFL Grammar
199
200 // Build CFL Graph
202
203 // Normalize CFL Grammar
205
206 // Initialize solver
208}
209
214
216{
218
219 if(Options::PrintCFL() == true)
220 {
221 if (Options::CFLGraph().empty())
222 svfir->dump("IR");
223 grammar->dump("Grammar");
224 graph->dump("CFLGraph");
225 }
226 if (Options::CFLGraph().empty())
228}
229
231{
232 // Start solving
233 double start = stat->getClk(true);
234
235 solver->solve();
236 if (Options::CFLGraph().empty())
237 {
239 {
241 solver->solve();
242 }
243 } // Only cflgraph built from bc could reanalyze by update call graph
244
245 double end = stat->getClk(true);
246 timeOfSolving += (end - start) / TIMEINTERVAL;
247}
248
253
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition SVFType.h:576
#define TIMEINTERVAL
Definition SVFType.h:604
#define DPAGBuild
Definition SVFType.h:584
#define DAndersen
Definition SVFType.h:595
void dump() const
void connectCaller2CalleeParams(const CallICFGNode *cs, const FunObjVar *F)
Connect formal and actual parameters for indirect callsites.
Definition CFLAlias.cpp:64
virtual void finalize()
Print grammar and graph.
Definition CFLAlias.cpp:215
virtual bool addCopyEdge(NodeID src, NodeID dst)
Need Original one for virtual table.
Definition CFLAlias.h:111
virtual void onTheFlyCallGraphSolve(const CallSiteToFunPtrMap &callsites, CallEdgeMap &newEdges)
On the fly call graph construction.
Definition CFLAlias.cpp:41
virtual void initializeSolver()
Initialize Solver.
Definition CFLAlias.cpp:210
void heapAllocatorViaIndCall(const CallICFGNode *cs)
Definition CFLAlias.cpp:147
virtual void solve()
Solving CFL Reachability.
Definition CFLAlias.cpp:230
virtual const PointsTo & getCFLPts(NodeID ptr)
Get points-to targets of a pointer. V In this context.
Definition CFLAlias.h:75
virtual bool updateCallGraph(const CallSiteToFunPtrMap &callsites)
Update call graph for the input indirect callsites.
Definition CFLAlias.cpp:175
CallSite2DummyValPN callsite2DummyValPN
Map an instruction to a dummy obj which created at an indirect callsite, which invokes a heap allocat...
Definition CFLAlias.h:144
virtual void initialize()
Initialize the grammar, graph, solver.
Definition CFLAlias.cpp:190
virtual void buildCFLGraph()
Build CFLGraph based on Option.
Definition CFLBase.cpp:84
CFLSolver * solver
Definition CFLBase.h:110
virtual void normalizeCFLGrammar()
Normalize grammar.
Definition CFLBase.cpp:111
static double numOfChecks
Definition CFLBase.h:101
virtual void checkParameter()
Parameter Checking.
Definition CFLBase.cpp:55
virtual void buildCFLGrammar()
Build Grammar from text file.
Definition CFLBase.cpp:71
CFLGraph * graph
Definition CFLBase.h:107
static double timeOfSolving
Definition CFLBase.h:102
CFGrammar * grammar
Definition CFLBase.h:109
SVFIR * svfir
Definition CFLBase.h:106
static double numOfIteration
Definition CFLBase.h:100
virtual void addCFLNode(NodeID id, CFLNode *node)
Definition CFLGraph.cpp:42
void dump(const std::string &filename)
Definition CFLGraph.cpp:73
virtual void solve()
Start solving.
static double numOfChecks
Definition CFLSolver.h:53
virtual bool isWorklistEmpty()
Definition CFLSolver.h:89
const std::string toString() const override
Definition ICFG.cpp:129
const SVFVar * getVtablePtr() const
Definition ICFGNode.h:520
const FunObjVar * getCalledFunction() const
Definition ICFGNode.h:501
const RetICFGNode * getRetICFGNode() const
Return callsite.
Definition ICFGNode.h:440
const std::string getSourceLoc() const override
Definition ICFGNode.h:571
bool isVirtualCall() const
Definition ICFGNode.h:510
bool isVarArg() const
void dump(std::string name)
Dump SVFIR.
Definition IRGraph.cpp:320
NodeID getVarargNode(const FunObjVar *func) const
getVarargNode - Return the unique node representing the variadic argument of a variadic function.
Definition IRGraph.cpp:71
static const Option< std::string > CFLGraph
Definition Options.h:222
static const Option< bool > PrintCFL
Definition Options.h:223
virtual void initializeSolver()
Initialize POCR Solver.
Definition CFLAlias.cpp:249
Solver Utilize Hybrid Representation of Graph.
Definition CFLSolver.h:296
virtual void initializeSolver()
Initialize POCRHybrid Solver.
Definition CFLAlias.cpp:254
Solver Utilize CFLData.
Definition CFLSolver.h:118
virtual void finalize()
Finalization of a pointer analysis, including checking alias correctness.
OrderedMap< const CallICFGNode *, FunctionSet > CallEdgeMap
PTAStat * stat
Statistics.
virtual void resolveIndCalls(const CallICFGNode *cs, const PointsTo &target, CallEdgeMap &newEdges)
Resolve indirect call edges.
virtual void resolveCPPIndCalls(const CallICFGNode *cs, const PointsTo &target, CallEdgeMap &newEdges)
Resolve cpp indirect call edges.
SVFIR::CallSiteToFunPtrMap CallSiteToFunPtrMap
bool funHasRet(const FunObjVar *func) const
Definition SVFIR.h:429
bool hasFunArgsList(const FunObjVar *func) const
Function has arguments list.
Definition SVFIR.h:368
const ValVar * getCallSiteRet(const RetICFGNode *cs) const
Get callsite return.
Definition SVFIR.h:407
const ValVarList & getFunArgsList(const FunObjVar *func) const
Get function arguments list.
Definition SVFIR.h:378
std::vector< const ValVar * > ValVarList
Definition SVFIR.h:60
bool hasCallSiteArgsMap(const CallICFGNode *cs) const
Callsite has argument list.
Definition SVFIR.h:385
const CallSiteToFunPtrMap & getIndirectCallsites() const
Add/get indirect callsites.
Definition SVFIR.h:453
bool callsiteHasRet(const RetICFGNode *cs) const
Definition SVFIR.h:413
NodeID addDummyValNode()
Definition SVFIR.h:566
const ValVar * getFunRet(const FunObjVar *func) const
Get function return list.
Definition SVFIR.h:423
const ValVarList & getCallSiteArgsList(const CallICFGNode *cs) const
Get callsite argument list.
Definition SVFIR.h:395
NodeID addDummyObjNode(const SVFType *type)
Definition SVFIR.h:570
static double getClk(bool mark=false)
Definition SVFStat.cpp:51
NodeID getId() const
Get ID.
Definition SVFValue.h:158
virtual const SVFType * getType() const
Definition SVFValue.h:169
bool isHeapAllocExtFunViaRet(const FunObjVar *fun)
Return true if the call is a heap allocator/reallocator.
Definition SVFUtil.h:279
void writeWrnMsg(const std::string &msg)
Writes a message run through wrnMsg.
Definition SVFUtil.cpp:72
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:52
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76