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#include <vector>
48
49namespace SVF
50{
51
52class ThreadCallGraph;
53class SVFIR;
54
59
61{
62public:
64
65public:
66
73
75 ~AndersenBase() override;
76
78 virtual void analyze() override;
79
80 virtual void solveAndwritePtsToFile(const std::string& filename);
81
82 virtual void readPtsFromFile(const std::string& filename);
83
84 virtual void solveConstraints();
85
87 virtual void initialize() override;
88
90 virtual void finalize() override;
91
93 virtual bool updateCallGraph(const CallSiteToFunPtrMap&) override;
94
97
99 virtual void connectCaller2ForkedFunParams(const CallICFGNode* cs, const FunObjVar* F,
101
103 virtual void connectCaller2CalleeParams(const CallICFGNode* cs, const FunObjVar* F,
105
107
108 static inline bool classof(const AndersenBase *)
109 {
110 return true;
111 }
112 static inline bool classof(const PointerAnalysis *pta)
113 {
114 return ( pta->getAnalysisTy() == PTATY::Andersen_BASE
121 }
123
126 {
127 return consCG;
128 }
129
131
132 inline NodeID sccRepNode(NodeID id) const override
133 {
134 return consCG->sccRepNode(id);
135 }
137 {
138 return consCG->sccSubNodes(repId);
139 }
141
143 virtual bool addCopyEdge(NodeID src, NodeID dst) = 0;
144
146 inline void printStat()
147 {
149 }
150
151 virtual void normalizePointsTo() override;
152
154 void cleanConsCG(NodeID id);
155
157
159
167
169 static double timeOfSCCDetection;
170 static double timeOfSCCMerges;
171 static double timeOfCollapse;
174 static double timeOfProcessCopyGep;
178
179protected:
187};
188
193{
194
195
196public:
198
202 {
203 }
204
206 virtual ~Andersen()
207 {
208
209 }
210
212 virtual void initialize();
213
215 virtual void finalize();
216
218 inline void resetData()
219 {
224 }
225
227
228 static inline bool classof(const Andersen *)
229 {
230 return true;
231 }
232 static inline bool classof(const PointerAnalysis *pta)
233 {
234 return (pta->getAnalysisTy() == PTATY::Andersen_WPA
238 }
240
242 virtual inline const PointsTo& getPts(NodeID id)
243 {
244 return getPTDataTy()->getPts(sccRepNode(id));
245 }
246 virtual inline bool unionPts(NodeID id, const PointsTo& target)
247 {
248 id = sccRepNode(id);
249 return getPTDataTy()->unionPts(id, target);
250 }
251 virtual inline bool unionPts(NodeID id, NodeID ptd)
252 {
253 id = sccRepNode(id);
254 ptd = sccRepNode(ptd);
255 return getPTDataTy()->unionPts(id,ptd);
256 }
257
259 virtual NodeBS getMayAliases(NodeID node);
260
268 std::vector<NodeBS> getMayAliasesBatch(const std::vector<NodeID>& nodes);
269
270
271 void dumpTopLevelPtsTo();
272
274 {
276 }
277
278protected:
279
281
283 virtual inline void computeDiffPts(NodeID id)
284 {
285 if (Options::DiffPts())
286 {
287 NodeID rep = sccRepNode(id);
288 getDiffPTDataTy()->computeDiffPts(rep, getDiffPTDataTy()->getPts(rep));
289 }
290 }
291 virtual inline const PointsTo& getDiffPts(NodeID id)
292 {
293 NodeID rep = sccRepNode(id);
294 if (Options::DiffPts())
295 return getDiffPTDataTy()->getDiffPts(rep);
296 else
297 return getPTDataTy()->getPts(rep);
298 }
299
302 {
303 if (!Options::DiffPts())
304 return;
307 getDiffPTDataTy()->updatePropaPtsMap(srcRep, dstRep);
308 }
309 inline void clearPropaPts(NodeID src)
310 {
311 if (Options::DiffPts())
312 {
313 NodeID rep = sccRepNode(src);
314 getDiffPTDataTy()->clearPropaPts(rep);
315 }
316 }
317
318 virtual void initWorklist() {}
319
321 virtual void processNode(NodeID nodeId);
322
324
325 void processAllAddr();
326
327 virtual bool processLoad(NodeID node, const ConstraintEdge* load);
328 virtual bool processStore(NodeID node, const ConstraintEdge* load);
329 virtual bool processCopy(NodeID node, const ConstraintEdge* edge);
330 virtual bool processGep(NodeID node, const GepCGEdge* edge);
331 virtual void handleCopyGep(ConstraintNode* node);
332 virtual void handleLoadStore(ConstraintNode* node);
333 virtual void processAddr(const AddrCGEdge* addr);
334 virtual bool processGepPts(const PointsTo& pts, const GepCGEdge* edge);
336
338 virtual inline bool addCopyEdge(NodeID src, NodeID dst)
339 {
340 if (consCG->addCopyCGEdge(src, dst))
341 {
342 updatePropaPts(src, dst);
343 return true;
344 }
345 return false;
346 }
347
350
351 virtual bool mergeSrcToTgt(NodeID srcId,NodeID tgtId);
352
354
355 void mergeSccNodes(NodeID repNodeId, const NodeBS& subNodes);
356 void mergeSccCycle();
358
360 virtual void collapsePWCNode(NodeID nodeId);
361 void collapseFields();
365
368
370 virtual NodeStack& SCCDetect();
371
374 std::optional<NodeBS> collectMayAliasesFromIndex(const PointsTo& expandedPts);
375
377 virtual void validateSuccessTests(std::string fun);
378
379
380
383 {
384 for(ConstraintGraph::iterator it = consCG->begin(), eit = consCG->end(); it!=eit; ++it)
385 {
386 const PointsTo& pts = getPts(it->first);
388
389 for (NodeID o : pts)
390 {
393 }
394
395 for (NodeID o : fldInsenObjs)
396 {
398 for (NodeID f : allFields) addPts(it->first, f);
399 }
400 }
401 }
402
404 virtual const std::string PTAName() const
405 {
406 return "AndersenWPA";
407 }
408
411 virtual void cluster(void) const;
412};
413
414
415
420{
421
422private:
423
424 static AndersenWaveDiff* diffWave; // static instance
425
426public:
428
431 {
432 if(diffWave==nullptr)
433 {
435 diffWave->analyze();
436 return diffWave;
437 }
438 return diffWave;
439 }
441 {
442 if (diffWave)
443 delete diffWave;
444 diffWave = nullptr;
445 }
446
447 virtual void initialize();
448 virtual void finalize();
449 virtual void solveWorklist();
450 virtual void processNode(NodeID nodeId);
451 virtual void postProcessNode(NodeID nodeId);
452 virtual bool handleLoad(const PointsTo& objs, const ConstraintEdge* load);
453 virtual bool handleStore(const PointsTo& objs, const ConstraintEdge* store);
454
455private:
464};
465
466} // End namespace SVF
467
468#endif /* INCLUDE_WPA_ANDERSEN_H_ */
newitem type
Definition cJSON.cpp:2739
void setValue(T v)
static bool classof(const PointerAnalysis *pta)
Definition Andersen.h:112
static double timeOfSCCMerges
Definition Andersen.h:170
static u32_t numOfProcessedCopy
Number of processed Addr edge.
Definition Andersen.h:161
NodeBS & sccSubNodes(NodeID repId)
Definition Andersen.h:136
static u32_t numOfSCCDetection
Definition Andersen.h:168
virtual void normalizePointsTo() override
static u32_t numOfSfrs
Number of processed Store edge.
Definition Andersen.h:165
virtual void finalize() override
Finalize analysis.
Definition Andersen.cpp:98
static double timeOfUpdateCallGraph
Definition Andersen.h:176
static u32_t numOfProcessedStore
Number of processed Load edge.
Definition Andersen.h:164
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:160
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:68
void printStat()
dump statistics
Definition Andersen.h:146
void heapAllocatorViaIndCall(const CallICFGNode *cs, NodePairSet &cpySrcNodes)
CallSite2DummyValPN callsite2DummyValPN
Definition Andersen.h:183
virtual void readPtsFromFile(const std::string &filename)
Definition Andersen.cpp:174
static u32_t numOfProcessedLoad
Number of processed Gep edge.
Definition Andersen.h:163
static double timeOfSCCDetection
Definition Andersen.h:169
NodeBS redundantGepNodes
Definition Andersen.h:156
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:125
~AndersenBase() override
Destructor.
Definition Andersen.cpp:73
virtual void analyze() override
Andersen analysis.
Definition Andersen.cpp:150
static u32_t numOfFieldExpand
Definition Andersen.h:166
static double timeOfProcessLoadStore
Definition Andersen.h:175
static u32_t numOfProcessedGep
Number of processed Copy edge.
Definition Andersen.h:162
static double timeOfProcessCopyGep
Definition Andersen.h:174
OrderedMap< const CallICFGNode *, NodeID > CallSite2DummyValPN
Definition Andersen.h:63
static u32_t MaxPointsToSetSize
Definition Andersen.h:173
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:171
static u32_t AveragePointsToSetSize
Definition Andersen.h:172
virtual void solveAndwritePtsToFile(const std::string &filename)
Definition Andersen.cpp:185
ConstraintGraph * consCG
Constraint Graph.
Definition Andersen.h:181
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:108
NodeID sccRepNode(NodeID id) const override
SCC methods.
Definition Andersen.h:132
static AndersenWaveDiff * createAndersenWaveDiff(SVFIR *_pag)
Create an singleton instance directly instead of invoking llvm pass manager.
Definition Andersen.h:430
static void releaseAndersenWaveDiff()
Definition Andersen.h:440
virtual bool handleLoad(const PointsTo &objs, const ConstraintEdge *load)
virtual bool handleStore(const PointsTo &objs, const ConstraintEdge *store)
Map< NodeID, LoadStoreState > loadStoreStates
Per node, the objects and load/store endpoints already handled.
Definition Andersen.h:463
virtual void postProcessNode(NodeID nodeId)
AndersenWaveDiff(SVFIR *_pag, PTATY type=PTATY::AndersenWaveDiff_WPA, bool alias_check=true)
Definition Andersen.h:427
static AndersenWaveDiff * diffWave
Definition Andersen.h:424
virtual void processNode(NodeID nodeId)
virtual void handleLoadStore(ConstraintNode *node)
Definition Andersen.cpp:518
void setDetectPWC(bool flag)
Definition Andersen.h:273
virtual ~Andersen()
Destructor.
Definition Andersen.h:206
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:280
void sanitizePts()
Sanitize pts for field insensitive objects.
Definition Andersen.h:382
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:301
static bool classof(const PointerAnalysis *pta)
Definition Andersen.h:232
std::optional< NodeBS > collectMayAliasesFromIndex(const PointsTo &expandedPts)
SCCDetection< ConstraintGraph * > CGSCC
Definition Andersen.h:197
virtual void computeDiffPts(NodeID id)
Handle diff points-to set.
Definition Andersen.h:283
Andersen(SVFIR *_pag, PTATY type=PTATY::Andersen_WPA, bool alias_check=true)
Constructor.
Definition Andersen.h:200
void clearPropaPts(NodeID src)
Definition Andersen.h:309
virtual bool addCopyEdge(NodeID src, NodeID dst)
Add copy edge on constraint graph.
Definition Andersen.h:338
virtual bool unionPts(NodeID id, NodeID ptd)
Definition Andersen.h:251
virtual void initWorklist()
Definition Andersen.h:318
void resetData()
Reset data.
Definition Andersen.h:218
virtual const PointsTo & getDiffPts(NodeID id)
Definition Andersen.h:291
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:228
virtual void handleCopyGep(ConstraintNode *node)
Definition Andersen.cpp:498
virtual bool unionPts(NodeID id, const PointsTo &target)
Definition Andersen.h:246
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:242
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:404
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.
std::vector< NodeBS > getMayAliasesBatch(const std::vector< NodeID > &nodes)
Definition Andersen.cpp:972
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:207
static const Option< bool > DiffPts
Definition Options.h:206
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
iter_range< typename GenericGraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
Set< NodePair > NodePairSet
Definition GeneralType.h:88
unsigned u32_t
Definition GeneralType.h:67
WPASolver< ConstraintGraph * > WPAConstraintSolver
Definition Andersen.h:58