Static Value-Flow Analysis
Loading...
Searching...
No Matches
Andersen.h
Go to the documentation of this file.
1//===- Andersen.h -- Field-sensitive Andersen's pointer analysis-------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2017> <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 * Andersen.h
25 *
26 * Created on: Nov 12, 2013
27 * Author: Yulei Sui
28 *
29 * The field-sensitive implementation is improved based on
30 *
31 * Yuxiang Lei and Yulei Sui. "Fast and Precise Handling of Positive Weight Cycles for Field-sensitive Pointer Analysis".
32 * 26th International Static Analysis Symposium (SAS'19)
33 */
34
35#ifndef INCLUDE_WPA_ANDERSEN_H_
36#define INCLUDE_WPA_ANDERSEN_H_
37
38#include "MemoryModel/PTATY.h"
41#include "WPA/WPASolver.h"
42#include "Graphs/ConsG.h"
43#include "Util/GeneralType.h"
44#include "Util/Options.h"
45
46#include <optional>
47
48namespace SVF
49{
50
51class ThreadCallGraph;
52class SVFIR;
53
58
60{
61public:
63
64public:
65
72
74 ~AndersenBase() override;
75
77 virtual void analyze() override;
78
79 virtual void solveAndwritePtsToFile(const std::string& filename);
80
81 virtual void readPtsFromFile(const std::string& filename);
82
83 virtual void solveConstraints();
84
86 virtual void initialize() override;
87
89 virtual void finalize() override;
90
92 virtual bool updateCallGraph(const CallSiteToFunPtrMap&) override;
93
96
98 virtual void connectCaller2ForkedFunParams(const CallICFGNode* cs, const FunObjVar* F,
100
102 virtual void connectCaller2CalleeParams(const CallICFGNode* cs, const FunObjVar* F,
104
106
107 static inline bool classof(const AndersenBase *)
108 {
109 return true;
110 }
111 static inline bool classof(const PointerAnalysis *pta)
112 {
113 return ( pta->getAnalysisTy() == PTATY::Andersen_BASE
120 }
122
125 {
126 return consCG;
127 }
128
130
131 inline NodeID sccRepNode(NodeID id) const override
132 {
133 return consCG->sccRepNode(id);
134 }
136 {
137 return consCG->sccSubNodes(repId);
138 }
140
142 virtual bool addCopyEdge(NodeID src, NodeID dst) = 0;
143
145 inline void printStat()
146 {
148 }
149
150 virtual void normalizePointsTo() override;
151
153 void cleanConsCG(NodeID id);
154
156
158
166
168 static double timeOfSCCDetection;
169 static double timeOfSCCMerges;
170 static double timeOfCollapse;
173 static double timeOfProcessCopyGep;
177
178protected:
186};
187
192{
193
194
195public:
197
201 {
202 }
203
205 virtual ~Andersen()
206 {
207
208 }
209
211 virtual void initialize();
212
214 virtual void finalize();
215
217 inline void resetData()
218 {
223 }
224
226
227 static inline bool classof(const Andersen *)
228 {
229 return true;
230 }
231 static inline bool classof(const PointerAnalysis *pta)
232 {
233 return (pta->getAnalysisTy() == PTATY::Andersen_WPA
237 }
239
241 virtual inline const PointsTo& getPts(NodeID id)
242 {
243 return getPTDataTy()->getPts(sccRepNode(id));
244 }
245 virtual inline bool unionPts(NodeID id, const PointsTo& target)
246 {
247 id = sccRepNode(id);
248 return getPTDataTy()->unionPts(id, target);
249 }
250 virtual inline bool unionPts(NodeID id, NodeID ptd)
251 {
252 id = sccRepNode(id);
253 ptd = sccRepNode(ptd);
254 return getPTDataTy()->unionPts(id,ptd);
255 }
256
258 virtual NodeBS getMayAliases(NodeID node);
259
260
261 void dumpTopLevelPtsTo();
262
264 {
266 }
267
268protected:
269
271
273 virtual inline void computeDiffPts(NodeID id)
274 {
275 if (Options::DiffPts())
276 {
277 NodeID rep = sccRepNode(id);
278 getDiffPTDataTy()->computeDiffPts(rep, getDiffPTDataTy()->getPts(rep));
279 }
280 }
281 virtual inline const PointsTo& getDiffPts(NodeID id)
282 {
283 NodeID rep = sccRepNode(id);
284 if (Options::DiffPts())
285 return getDiffPTDataTy()->getDiffPts(rep);
286 else
287 return getPTDataTy()->getPts(rep);
288 }
289
292 {
293 if (!Options::DiffPts())
294 return;
297 getDiffPTDataTy()->updatePropaPtsMap(srcRep, dstRep);
298 }
299 inline void clearPropaPts(NodeID src)
300 {
301 if (Options::DiffPts())
302 {
303 NodeID rep = sccRepNode(src);
304 getDiffPTDataTy()->clearPropaPts(rep);
305 }
306 }
307
308 virtual void initWorklist() {}
309
311 virtual void processNode(NodeID nodeId);
312
314
315 void processAllAddr();
316
317 virtual bool processLoad(NodeID node, const ConstraintEdge* load);
318 virtual bool processStore(NodeID node, const ConstraintEdge* load);
319 virtual bool processCopy(NodeID node, const ConstraintEdge* edge);
320 virtual bool processGep(NodeID node, const GepCGEdge* edge);
321 virtual void handleCopyGep(ConstraintNode* node);
322 virtual void handleLoadStore(ConstraintNode* node);
323 virtual void processAddr(const AddrCGEdge* addr);
324 virtual bool processGepPts(const PointsTo& pts, const GepCGEdge* edge);
326
328 virtual inline bool addCopyEdge(NodeID src, NodeID dst)
329 {
330 if (consCG->addCopyCGEdge(src, dst))
331 {
332 updatePropaPts(src, dst);
333 return true;
334 }
335 return false;
336 }
337
340
341 virtual bool mergeSrcToTgt(NodeID srcId,NodeID tgtId);
342
344
345 void mergeSccNodes(NodeID repNodeId, const NodeBS& subNodes);
346 void mergeSccCycle();
348
350 virtual void collapsePWCNode(NodeID nodeId);
351 void collapseFields();
355
358
360 virtual NodeStack& SCCDetect();
361
364 std::optional<NodeBS> collectMayAliasesFromIndex(const PointsTo& expandedPts);
365
367 virtual void validateSuccessTests(std::string fun);
368
369
370
373 {
374 for(ConstraintGraph::iterator it = consCG->begin(), eit = consCG->end(); it!=eit; ++it)
375 {
376 const PointsTo& pts = getPts(it->first);
378
379 for (NodeID o : pts)
380 {
383 }
384
385 for (NodeID o : fldInsenObjs)
386 {
388 for (NodeID f : allFields) addPts(it->first, f);
389 }
390 }
391 }
392
394 virtual const std::string PTAName() const
395 {
396 return "AndersenWPA";
397 }
398
401 virtual void cluster(void) const;
402};
403
404
405
410{
411
412private:
413
414 static AndersenWaveDiff* diffWave; // static instance
415
416public:
418
421 {
422 if(diffWave==nullptr)
423 {
425 diffWave->analyze();
426 return diffWave;
427 }
428 return diffWave;
429 }
431 {
432 if (diffWave)
433 delete diffWave;
434 diffWave = nullptr;
435 }
436
437 virtual void initialize();
438 virtual void solveWorklist();
439 virtual void processNode(NodeID nodeId);
440 virtual void postProcessNode(NodeID nodeId);
441 virtual bool handleLoad(NodeID id, const ConstraintEdge* load);
442 virtual bool handleStore(NodeID id, const ConstraintEdge* store);
443};
444
445} // End namespace SVF
446
447#endif /* INCLUDE_WPA_ANDERSEN_H_ */
newitem type
Definition cJSON.cpp:2739
void setValue(T v)
static bool classof(const PointerAnalysis *pta)
Definition Andersen.h:111
static double timeOfSCCMerges
Definition Andersen.h:169
static u32_t numOfProcessedCopy
Number of processed Addr edge.
Definition Andersen.h:160
NodeBS & sccSubNodes(NodeID repId)
Definition Andersen.h:135
static u32_t numOfSCCDetection
Definition Andersen.h:167
virtual void normalizePointsTo() override
static u32_t numOfSfrs
Number of processed Store edge.
Definition Andersen.h:164
virtual void finalize() override
Finalize analysis.
Definition Andersen.cpp:98
static double timeOfUpdateCallGraph
Definition Andersen.h:175
static u32_t numOfProcessedStore
Number of processed Load edge.
Definition Andersen.h:163
virtual void connectCaller2ForkedFunParams(const CallICFGNode *cs, const FunObjVar *F, NodePairSet &cpySrcNodes)
Connect formal and actual parameters for indirect forksites.
Definition Andersen.cpp:264
static u32_t numOfProcessedAddr
Statistics.
Definition Andersen.h:159
virtual bool updateCallGraph(const CallSiteToFunPtrMap &) override
Update call graph.
Definition Andersen.cpp:210
AndersenBase(SVFIR *_pag, PTATY type=PTATY::Andersen_BASE, bool alias_check=true)
Constructor.
Definition Andersen.h:67
void printStat()
dump statistics
Definition Andersen.h:145
void heapAllocatorViaIndCall(const CallICFGNode *cs, NodePairSet &cpySrcNodes)
CallSite2DummyValPN callsite2DummyValPN
Definition Andersen.h:182
virtual void readPtsFromFile(const std::string &filename)
Definition Andersen.cpp:174
static u32_t numOfProcessedLoad
Number of processed Gep edge.
Definition Andersen.h:162
static double timeOfSCCDetection
Definition Andersen.h:168
NodeBS redundantGepNodes
Definition Andersen.h:155
virtual bool addCopyEdge(NodeID src, NodeID dst)=0
Add copy edge on constraint graph.
virtual void initialize() override
Initialize analysis.
Definition Andersen.cpp:82
ConstraintGraph * getConstraintGraph()
Get constraint graph.
Definition Andersen.h:124
~AndersenBase() override
Destructor.
Definition Andersen.cpp:73
virtual void analyze() override
Andersen analysis.
Definition Andersen.cpp:150
static u32_t numOfFieldExpand
Definition Andersen.h:165
static double timeOfProcessLoadStore
Definition Andersen.h:174
static u32_t numOfProcessedGep
Number of processed Copy edge.
Definition Andersen.h:161
static double timeOfProcessCopyGep
Definition Andersen.h:173
OrderedMap< const CallICFGNode *, NodeID > CallSite2DummyValPN
Definition Andersen.h:62
static u32_t MaxPointsToSetSize
Definition Andersen.h:172
virtual void solveConstraints()
Definition Andersen.cpp:109
virtual bool updateThreadCallGraph(const CallSiteToFunPtrMap &, NodePairSet &)
Update thread call graph.
Definition Andersen.cpp:244
virtual void connectCaller2CalleeParams(const CallICFGNode *cs, const FunObjVar *F, NodePairSet &cpySrcNodes)
Connect formal and actual parameters for indirect callsites.
static double timeOfCollapse
Definition Andersen.h:170
static u32_t AveragePointsToSetSize
Definition Andersen.h:171
virtual void solveAndwritePtsToFile(const std::string &filename)
Definition Andersen.cpp:185
ConstraintGraph * consCG
Constraint Graph.
Definition Andersen.h:180
void cleanConsCG(NodeID id)
remove redundant gepnodes in constraint graph
Definition Andersen.cpp:197
static bool classof(const AndersenBase *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition Andersen.h:107
NodeID sccRepNode(NodeID id) const override
SCC methods.
Definition Andersen.h:131
static AndersenWaveDiff * createAndersenWaveDiff(SVFIR *_pag)
Create an singleton instance directly instead of invoking llvm pass manager.
Definition Andersen.h:420
static void releaseAndersenWaveDiff()
Definition Andersen.h:430
virtual bool handleStore(NodeID id, const ConstraintEdge *store)
virtual bool handleLoad(NodeID id, const ConstraintEdge *load)
virtual void postProcessNode(NodeID nodeId)
AndersenWaveDiff(SVFIR *_pag, PTATY type=PTATY::AndersenWaveDiff_WPA, bool alias_check=true)
Definition Andersen.h:417
static AndersenWaveDiff * diffWave
Definition Andersen.h:414
virtual void processNode(NodeID nodeId)
virtual void handleLoadStore(ConstraintNode *node)
Definition Andersen.cpp:518
void setDetectPWC(bool flag)
Definition Andersen.h:263
virtual ~Andersen()
Destructor.
Definition Andersen.h:205
virtual NodeBS getMayAliases(NodeID node)
Collect exactly the SVFIR nodes q for which mayAlias(node, q) holds.
Definition Andersen.cpp:938
void mergeSccNodes(NodeID repNodeId, const NodeBS &subNodes)
Merge sub node in a SCC cycle to their rep node.
Definition Andersen.cpp:756
virtual void processNode(NodeID nodeId)
Override WPASolver function in order to use the default solver.
Definition Andersen.cpp:477
virtual void initialize()
Initialize analysis.
Definition Andersen.cpp:438
CallSite2DummyValPN callsite2DummyValPN
Map an instruction to a dummy obj which created at an indirect callsite, which invokes a heap allocat...
Definition Andersen.h:270
void sanitizePts()
Sanitize pts for field insensitive objects.
Definition Andersen.h:372
virtual NodeStack & SCCDetect()
SCC detection.
Definition Andersen.cpp:852
virtual void mergeNodeToRep(NodeID nodeId, NodeID newRepId)
Merge sub node to its rep.
Definition Andersen.cpp:907
bool collapseNodePts(NodeID nodeId)
Definition Andersen.cpp:771
void dumpTopLevelPtsTo()
void updatePropaPts(NodeID dstId, NodeID srcId)
Handle propagated points-to set.
Definition Andersen.h:291
static bool classof(const PointerAnalysis *pta)
Definition Andersen.h:231
std::optional< NodeBS > collectMayAliasesFromIndex(const PointsTo &expandedPts)
Definition Andersen.cpp:980
SCCDetection< ConstraintGraph * > CGSCC
Definition Andersen.h:196
virtual void computeDiffPts(NodeID id)
Handle diff points-to set.
Definition Andersen.h:273
Andersen(SVFIR *_pag, PTATY type=PTATY::Andersen_WPA, bool alias_check=true)
Constructor.
Definition Andersen.h:199
void clearPropaPts(NodeID src)
Definition Andersen.h:299
virtual bool addCopyEdge(NodeID src, NodeID dst)
Add copy edge on constraint graph.
Definition Andersen.h:328
virtual bool unionPts(NodeID id, NodeID ptd)
Definition Andersen.h:250
virtual void initWorklist()
Definition Andersen.h:308
void resetData()
Reset data.
Definition Andersen.h:217
virtual const PointsTo & getDiffPts(NodeID id)
Definition Andersen.h:281
virtual bool processGep(NodeID node, const GepCGEdge *edge)
Definition Andersen.cpp:635
static bool classof(const Andersen *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition Andersen.h:227
virtual void handleCopyGep(ConstraintNode *node)
Definition Andersen.cpp:498
virtual bool unionPts(NodeID id, const PointsTo &target)
Definition Andersen.h:245
virtual bool processLoad(NodeID node, const ConstraintEdge *load)
Definition Andersen.cpp:575
virtual const PointsTo & getPts(NodeID id)
Operation of points-to set.
Definition Andersen.h:241
void collapseFields()
collapse positive weight cycles of a graph
Definition Andersen.cpp:717
virtual bool processStore(NodeID node, const ConstraintEdge *load)
Definition Andersen.cpp:595
virtual bool processCopy(NodeID node, const ConstraintEdge *edge)
Definition Andersen.cpp:615
virtual bool processGepPts(const PointsTo &pts, const GepCGEdge *edge)
Definition Andersen.cpp:644
void mergeSccCycle()
Definition Andersen.cpp:732
virtual void processAddr(const AddrCGEdge *addr)
Definition Andersen.cpp:560
void updateNodeRepAndSubs(NodeID nodeId, NodeID newRepId)
Updates subnodes of its rep, and rep node of its subs.
Definition Andersen.cpp:917
virtual void finalize()
Finalize analysis.
Definition Andersen.cpp:452
virtual const std::string PTAName() const
Get PTA name.
Definition Andersen.h:394
void processAllAddr()
handling various constraints
Definition Andersen.cpp:546
virtual void cluster(void) const
virtual bool mergeSrcToTgt(NodeID srcId, NodeID tgtId)
Definition Andersen.cpp:876
virtual void collapsePWCNode(NodeID nodeId)
Collapse a field object into its base for field insensitive analysis.
Definition Andersen.cpp:708
virtual void validateSuccessTests(std::string fun)
Also check getMayAliases on the pointers that the alias tests use.
bool collapseField(NodeID nodeId)
Definition Andersen.cpp:791
DiffPTDataTy * getDiffPTDataTy() const
PTDataTy * getPTDataTy() const
Get points-to data structure.
virtual bool addPts(NodeID id, NodeID ptd)
NodeID sccRepNode(NodeID id) const
SCC rep/sub nodes methods.
Definition ConsG.h:230
CopyCGEdge * addCopyCGEdge(NodeID src, NodeID dst)
Add Copy edge.
Definition ConsG.cpp:226
NodeBS & sccSubNodes(NodeID id)
Definition ConsG.h:238
NodeBS & getAllFieldsObjVars(NodeID id)
Definition ConsG.h:311
iterator begin()
Iterators.
IDToNodeMapTy::iterator iterator
Node Iterators.
static Option< bool > DetectPWC
Definition Options.h:206
static const Option< bool > DiffPts
Definition Options.h:205
bool isFieldInsensitive(NodeID id) const
void dumpStat()
Dump the statistics.
PTATY getAnalysisTy() const
Type of pointer analysis.
SVFIR::CallSiteToFunPtrMap CallSiteToFunPtrMap
u32_t OnTheFlyIterBudgetForStat
Flag for iteration budget for on-the-fly statistics.
void set(unsigned Idx)
u32_t iterationForPrintStat
print out statistics for i-th iteration
Definition WPASolver.h:174
for isBitcode
Definition BasicTypes.h:70
std::stack< NodeID > NodeStack
Definition GeneralType.h:92
PTATY
Pointer analysis type list.
Definition PTATY.h:9
@ Andersen_WPA
Andersen PTA.
Definition PTATY.h:12
@ AndersenSFR_WPA
Stride-based field representation.
Definition PTATY.h:14
@ AndersenWaveDiff_WPA
Diff wave propagation andersen-style WPA.
Definition PTATY.h:15
@ TypeCPP_WPA
Type-based analysis for C++.
Definition PTATY.h:26
@ Steensgaard_WPA
Steensgaard PTA.
Definition PTATY.h:16
@ Andersen_BASE
Base Andersen PTA.
Definition PTATY.h:11
@ AndersenSCD_WPA
Selective cycle detection andersen-style WPA.
Definition PTATY.h:13
u32_t NodeID
Definition GeneralType.h:76
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
Set< NodePair > NodePairSet
Definition GeneralType.h:88
unsigned u32_t
Definition GeneralType.h:67
WPASolver< ConstraintGraph * > WPAConstraintSolver
Definition Andersen.h:57