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"
31using namespace SVF;
32using namespace SVFUtil;
33
40{
41 for(CallSiteToFunPtrMap::const_iterator iter = callsites.begin(), eiter = callsites.end(); iter!=eiter; ++iter)
42 {
43 const CallICFGNode* cs = iter->first;
44
45 if (cs->isVirtualCall())
46 {
47 const SVFVar* vtbl = cs->getVtablePtr();
48
49 assert(vtbl != nullptr);
50 NodeID vtblId = vtbl->getId();
52 }
53 else
55 }
56}
57
63{
64 assert(F);
65
66 DBOUT(DAndersen, outs() << "connect parameters from indirect callsite " << cs->toString() << " to callee " << *F << "\n");
67
68 const CallICFGNode* callBlockNode = cs;
70
72 {
74 }
75
77 {
80 if (cs_return->isPointer() && fun_return->isPointer())
81 {
83 NodeID srcret = fun_return->getId();
85 }
86 else
87 {
88 DBOUT(DAndersen, outs() << "not a pointer ignored\n");
89 }
90 }
91
92 if (svfir->hasCallSiteArgsMap(callBlockNode) && svfir->hasFunArgsList(F))
93 {
94
95 // connect actual and formal param
98 //Go through the fixed parameters.
99 DBOUT(DPAGBuild, outs() << " args:");
100 SVFIR::SVFVarList::const_iterator funArgIt = funArgList.begin(), funArgEit = funArgList.end();
101 SVFIR::SVFVarList::const_iterator csArgIt = csArgList.begin(), csArgEit = csArgList.end();
102 for (; funArgIt != funArgEit; ++csArgIt, ++funArgIt)
103 {
104 //Some programs (e.g. Linux kernel) leave unneeded parameters empty.
105 if (csArgIt == csArgEit)
106 {
107 DBOUT(DAndersen, outs() << " !! not enough args\n");
108 break;
109 }
110 const PAGNode *cs_arg = *csArgIt ;
111 const PAGNode *fun_arg = *funArgIt;
112
113 if (cs_arg->isPointer() && fun_arg->isPointer())
114 {
115 DBOUT(DAndersen, outs() << "process actual parm " << cs_arg->toString() << " \n");
116 NodeID srcAA = cs_arg->getId();
117 NodeID dstFA = fun_arg->getId();
119 }
120 }
121
122 //Any remaining actual args must be varargs.
123 if (F->isVarArg())
124 {
126 DBOUT(DPAGBuild, outs() << "\n varargs:");
127 for (; csArgIt != csArgEit; ++csArgIt)
128 {
129 const PAGNode *cs_arg = *csArgIt;
130 if (cs_arg->isPointer())
131 {
132 NodeID vnAA = cs_arg->getId();
134 }
135 }
136 }
137 if(csArgIt != csArgEit)
138 {
139 writeWrnMsg("too many args to non-vararg func.");
140 writeWrnMsg("(" + cs->getSourceLoc() + ")");
141 }
142 }
143}
144
146{
147 assert(cs->getCalledFunction() == nullptr && "not an indirect callsite?");
151 CallSite2DummyValPN::const_iterator it = callsite2DummyValPN.find(cs);
152 if(it != callsite2DummyValPN.end())
153 {
154 srcret = it->second;
155 }
156 else
157 {
160 callsite2DummyValPN.insert(std::make_pair(cs,valNode));
163 srcret = valNode;
164 }
165
166 NodeID dstrec = cs_return->getId();
168}
169
174{
177 for(CallEdgeMap::iterator it = newEdges.begin(), eit = newEdges.end(); it!=eit; ++it )
178 {
179 for(FunctionSet::iterator cit = it->second.begin(), ecit = it->second.end(); cit!=ecit; ++cit)
180 {
182 }
183 }
184
185 return (!solver->isWorklistEmpty());
186}
187
189{
190 stat = new CFLStat(this);
191
192 // Parameter Checking
194
195 // Build CFL Grammar
197
198 // Build CFL Graph
200
201 // Normalize CFL Grammar
203
204 // Initialize solver
206}
207
212
214{
216
217 if(Options::PrintCFL() == true)
218 {
219 if (Options::CFLGraph().empty())
220 svfir->dump("IR");
221 grammar->dump("Grammar");
222 graph->dump("CFLGraph");
223 }
224 if (Options::CFLGraph().empty())
226}
227
229{
230 // Start solving
231 double start = stat->getClk(true);
232
233 solver->solve();
234 if (Options::CFLGraph().empty())
235 {
237 {
239 solver->solve();
240 }
241 } // Only cflgraph built from bc could reanalyze by update call graph
242
243 double end = stat->getClk(true);
244 timeOfSolving += (end - start) / TIMEINTERVAL;
245}
246
251
#define F(f)
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition SVFType.h:484
#define TIMEINTERVAL
Definition SVFType.h:512
#define DPAGBuild
Definition SVFType.h:492
#define DAndersen
Definition SVFType.h:503
void dump() const
virtual void finalize()
Print grammar and graph.
Definition CFLAlias.cpp:213
virtual bool addCopyEdge(NodeID src, NodeID dst)
Need Original one for virtual table.
Definition CFLAlias.h:118
virtual void onTheFlyCallGraphSolve(const CallSiteToFunPtrMap &callsites, CallEdgeMap &newEdges)
On the fly call graph construction.
Definition CFLAlias.cpp:39
virtual void initializeSolver()
Initialize Solver.
Definition CFLAlias.cpp:208
void connectCaller2CalleeParams(const CallICFGNode *cs, const SVFFunction *F)
Connect formal and actual parameters for indirect callsites.
Definition CFLAlias.cpp:62
void heapAllocatorViaIndCall(const CallICFGNode *cs)
Definition CFLAlias.cpp:145
virtual void solve()
Solving CFL Reachability.
Definition CFLAlias.cpp:228
virtual const PointsTo & getCFLPts(NodeID ptr)
Get points-to targets of a pointer. V In this context.
Definition CFLAlias.h:82
virtual bool updateCallGraph(const CallSiteToFunPtrMap &callsites)
Update call graph for the input indirect callsites.
Definition CFLAlias.cpp:173
CallSite2DummyValPN callsite2DummyValPN
Map an instruction to a dummy obj which created at an indirect callsite, which invokes a heap allocat...
Definition CFLAlias.h:151
virtual void initialize()
Initialize the grammar, graph, solver.
Definition CFLAlias.cpp:188
virtual void buildCFLGraph()
Build CFLGraph based on Option.
Definition CFLBase.cpp:78
CFLSolver * solver
Definition CFLBase.h:113
virtual void normalizeCFLGrammar()
Normalize grammar.
Definition CFLBase.cpp:105
static double numOfChecks
Definition CFLBase.h:104
virtual void checkParameter()
Parameter Checking.
Definition CFLBase.cpp:49
virtual void buildCFLGrammar()
Build Grammar from text file.
Definition CFLBase.cpp:65
CFLGraph * graph
Definition CFLBase.h:110
static double timeOfSolving
Definition CFLBase.h:105
CFGrammar * grammar
Definition CFLBase.h:112
SVFIR * svfir
Definition CFLBase.h:109
static double numOfIteration
Definition CFLBase.h:103
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:52
virtual bool isWorklistEmpty()
Definition CFLSolver.h:88
const std::string toString() const override
Definition ICFG.cpp:131
const SVFVar * getVtablePtr() const
Definition ICFGNode.h:537
const RetICFGNode * getRetICFGNode() const
Return callsite.
Definition ICFGNode.h:457
const std::string getSourceLoc() const override
Definition ICFGNode.h:588
const SVFFunction * getCalledFunction() const
Definition ICFGNode.h:518
bool isVirtualCall() const
Definition ICFGNode.h:527
void dump(std::string name)
Dump SVFIR.
Definition IRGraph.cpp:102
NodeID getVarargNode(const SVFFunction *func) const
getVarargNode - Return the unique node representing the variadic argument of a variadic function.
Definition IRGraph.h:157
static const Option< std::string > CFLGraph
Definition Options.h:232
static const Option< bool > PrintCFL
Definition Options.h:233
virtual void initializeSolver()
Initialize POCR Solver.
Definition CFLAlias.cpp:247
Solver Utilize Hybrid Representation of Graph.
Definition CFLSolver.h:295
virtual void initializeSolver()
Initialize POCRHybrid Solver.
Definition CFLAlias.cpp:252
Solver Utilize CFLData.
Definition CFLSolver.h:117
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
virtual const SVFType * getType() const
NodeID getId() const
Get ID.
std::vector< const SVFVar * > SVFVarList
Definition SVFIR.h:60
const SVFVarList & getFunArgsList(const SVFFunction *func) const
Get function arguments list.
Definition SVFIR.h:276
const SVFVar * getFunRet(const SVFFunction *func) const
Get function return list.
Definition SVFIR.h:321
bool hasCallSiteArgsMap(const CallICFGNode *cs) const
Callsite has argument list.
Definition SVFIR.h:283
const CallSiteToFunPtrMap & getIndirectCallsites() const
Add/get indirect callsites.
Definition SVFIR.h:351
bool callsiteHasRet(const RetICFGNode *cs) const
Definition SVFIR.h:311
NodeID addDummyValNode()
Definition SVFIR.h:496
bool hasFunArgsList(const SVFFunction *func) const
Function has arguments list.
Definition SVFIR.h:266
const SVFVarList & getCallSiteArgsList(const CallICFGNode *cs) const
Get callsite argument list.
Definition SVFIR.h:293
const SVFVar * getCallSiteRet(const RetICFGNode *cs) const
Get callsite return.
Definition SVFIR.h:305
NodeID addDummyObjNode(const SVFType *type)
Definition SVFIR.h:500
bool funHasRet(const SVFFunction *func) const
Definition SVFIR.h:327
static double getClk(bool mark=false)
Definition SVFStat.cpp:48
bool isHeapAllocExtFunViaRet(const SVFFunction *fun)
Return true if the call is a heap allocator/reallocator.
Definition SVFUtil.h:296
void writeWrnMsg(const std::string &msg)
Writes a message run through wrnMsg.
Definition SVFUtil.cpp:67
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:55
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74