Static Value-Flow Analysis
FSMPTA.h
Go to the documentation of this file.
1 //===- FSMPTA.h -- Flow-sensitive analysis of multithreaded programs-------------//
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  * FSMPTA.h
25  *
26  * Created on: Jul 29, 2015
27  * Author: Yulei Sui, Peng Di
28  *
29  * The implementation is based on
30  * Yulei Sui, Peng Di, and Jingling Xue. "Sparse Flow-Sensitive Pointer Analysis for Multithreaded Programs".
31  * 2016 International Symposium on Code Generation and Optimization (CGO'16)
32  */
33 
34 #ifndef FSPTANALYSIS_H_
35 #define FSPTANALYSIS_H_
36 
37 
38 #include "WPA/FlowSensitive.h"
39 #include "MSSA/SVFGBuilder.h"
40 #include "MTA/LockAnalysis.h"
41 #include "MemoryModel/PointsTo.h"
42 #include "MTA/MHP.h"
43 
44 namespace SVF
45 {
46 
47 class MHP;
48 class LockAnalysis;
49 
50 
52 {
53 public:
55  SVFGNode(SVFGnode), lockSpan(lockspan) {}
56  virtual ~SVFGNodeLockSpan() {}
57 
58  inline bool operator< (const SVFGNodeLockSpan& rhs) const
59  {
60  if (SVFGNode != rhs.getSVFGNode())
61  return SVFGNode < rhs.getSVFGNode();
62  return lockSpan.size() < rhs.getLockSpan().size();
63  }
65  {
66  if(*this != rhs)
67  {
68  SVFGNode = rhs.getSVFGNode();
69  lockSpan = rhs.getLockSpan();
70  }
71  return *this;
72  }
73  inline bool operator== (const SVFGNodeLockSpan& rhs) const
74  {
75  return (SVFGNode == rhs.getSVFGNode() && lockSpan == rhs.getLockSpan());
76  }
77  inline bool operator!= (const SVFGNodeLockSpan& rhs) const
78  {
79  return !(*this == rhs);
80  }
81  inline const StmtSVFGNode* getSVFGNode() const
82  {
83  return SVFGNode;
84  }
85  inline const LockAnalysis::LockSpan getLockSpan() const
86  {
87  return lockSpan;
88  }
89 private:
92 };
93 
98 {
99 
100 public:
105  typedef std::vector<const SVFGNode*> SVFGNodeVec;
108  typedef std::pair<NodeID,NodeID> NodeIDPair;
110 
113  {
114  }
115 
117  virtual ~MTASVFGBuilder() {}
118 
123 
124 protected:
126  virtual void buildSVFG();
127 
128 private:
130  bool recordEdge(NodeID id1, NodeID id2, PointsTo pts);
131  bool recordAddingEdge(NodeID id1, NodeID id2, PointsTo pts);
132  bool recordRemovingEdge(NodeID id1, NodeID id2, PointsTo pts);
134  void performAddingMHPEdges();
136  SVFGEdge* addTDEdges(NodeID srcId, NodeID dstId, PointsTo& pts);
138  void connectMHPEdges(PointerAnalysis* pta);
139 
140  void handleStoreLoadNonSparse(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta);
141  void handleStoreStoreNonSparse(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta);
142 
143  void handleStoreLoad(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta);
144  void handleStoreStore(const StmtSVFGNode* n1,const StmtSVFGNode* n2, PointerAnalysis* pta);
145 
148 
149  void mergeSpan(NodeBS comlocks, InstSet& res);
150  void readPrecision();
151 
155 
158  bool isHeadofSpan(const StmtSVFGNode* n, InstSet mergespan);
159  bool isTailofSpan(const StmtSVFGNode* n, InstSet mergespan);
160  bool isHeadofSpan(const StmtSVFGNode* n);
161  bool isTailofSpan(const StmtSVFGNode* n);
164 
168 
172 
175 
176 
179 
182 
185 
186 
187  static const u32_t ADDEDGE_NOEDGE= 0;
188  static const u32_t ADDEDGE_NONSPARSE= 1;
189  static const u32_t ADDEDGE_ALLOPT= 2;
190  static const u32_t ADDEDGE_NOMHP= 3;
191  static const u32_t ADDEDGE_NOALIAS= 4;
192  static const u32_t ADDEDGE_NOLOCK= 5;
193  static const u32_t ADDEDGE_NORP= 6;
194 // static const u32_t ADDEDGE_PRECISELOCK= 5;
195 
196 };
197 
198 
202 class FSMPTA : public FlowSensitive
203 {
204 
205 
206 public:
207 
209  FSMPTA(MHP* m, LockAnalysis* la) : FlowSensitive(m->getTCT()->getPTA()->getPAG()), mhp(m), lockana(la)
210  {
211  }
212 
215  {
216  }
217 
219  void initialize(SVFModule* module);
220 
221  inline SVFIR* getPAG()
222  {
223  return mhp->getTCT()->getPTA()->getPAG();
224  }
225 
227  static FSMPTA* createFSMPTA(SVFModule* module, MHP* m, LockAnalysis* la)
228  {
229  if (mfspta == nullptr)
230  {
231  mfspta = new FSMPTA(m,la);
232  mfspta->analyze();
233  }
234  return mfspta;
235  }
236 
238  static void releaseFSMPTA()
239  {
240  if (mfspta)
241  delete mfspta;
242  mfspta = nullptr;
243  }
244 
246  inline MHP* getMHP() const
247  {
248  return mhp;
249  }
250 
251 private:
252  static FSMPTA* mfspta;
256 };
257 
258 } // End namespace SVF
259 
260 template <> struct std::hash<SVF::SVFGNodeLockSpan>
261 {
262  size_t operator()(const SVF::SVFGNodeLockSpan &cs) const
263  {
264  std::hash<SVF::StmtSVFGNode* >h;
265  SVF::StmtSVFGNode* node = const_cast<SVF::StmtSVFGNode* > (cs.getSVFGNode());
266  return h(node);
267  }
268 };
269 
270 #endif /* FSPTANALYSIS_H_ */
cJSON * n
Definition: cJSON.cpp:2558
MHP * getMHP() const
Get MHP.
Definition: FSMPTA.h:246
FSMPTA(MHP *m, LockAnalysis *la)
Constructor.
Definition: FSMPTA.h:209
~FSMPTA()
Destructor.
Definition: FSMPTA.h:214
static FSMPTA * createFSMPTA(SVFModule *module, MHP *m, LockAnalysis *la)
Create single instance of flow-sensitive pointer analysis.
Definition: FSMPTA.h:227
MHP * mhp
Definition: FSMPTA.h:253
LockAnalysis * lockana
Definition: FSMPTA.h:254
void initialize() override
Initialize analysis.
SVFIR * getPAG()
Definition: FSMPTA.h:221
static void releaseFSMPTA()
Release flow-sensitive pointer analysis.
Definition: FSMPTA.h:238
static FSMPTA * mfspta
Definition: FSMPTA.h:252
void analyze() override
Flow sensitive analysis.
void initialize() override
Initialize analysis.
Set< CxtStmt > LockSpan
Definition: LockAnalysis.h:68
Definition: MHP.h:46
TCT * getTCT() const
Get Thread Creation Tree.
Definition: MHP.h:82
LockAnalysis * lockana
Definition: FSMPTA.h:171
static u32_t numOfNewSVFGEdges
Number of newly added SVFG edges.
Definition: FSMPTA.h:120
static u32_t numOfRemovedSVFGEdges
Definition: FSMPTA.h:121
static u32_t numOfRemovedPTS
Definition: FSMPTA.h:122
void performAddingMHPEdges()
perform adding/removing MHP Edges in value flow graph
Definition: FSMPTA.cpp:113
void connectMHPEdges(PointerAnalysis *pta)
Connect MHP indirect value-flow edges for two nodes that may-happen-in-parallel.
Definition: FSMPTA.cpp:706
Set< NodeIDPair > recordedges
Definition: FSMPTA.h:173
NodeBS SVFGNodeIDSet
Definition: FSMPTA.h:106
void handleStoreStoreWithLockPrecisely(const StmtSVFGNode *n1, const StmtSVFGNode *n2, PointerAnalysis *pta)
Definition: FSMPTA.cpp:588
Map< NodeIDPair, PointsTo > edge2pts
Definition: FSMPTA.h:174
static const u32_t ADDEDGE_NOMHP
Definition: FSMPTA.h:190
void handleStoreLoad(const StmtSVFGNode *n1, const StmtSVFGNode *n2, PointerAnalysis *pta)
Definition: FSMPTA.cpp:484
void performRemovingMHPEdges()
Definition: FSMPTA.cpp:145
Map< const StmtSVFGNode *, SVFGNodeIDSet > prevset
Definition: FSMPTA.h:177
PointerAnalysis::FunctionSet FunctionSet
Definition: FSMPTA.h:103
void handleStoreStore(const StmtSVFGNode *n1, const StmtSVFGNode *n2, PointerAnalysis *pta)
Definition: FSMPTA.cpp:518
static const u32_t ADDEDGE_NONSPARSE
Definition: FSMPTA.h:188
std::vector< const SVFGNode * > SVFGNodeVec
Definition: FSMPTA.h:105
SVFGEdge * addTDEdges(NodeID srcId, NodeID dstId, PointsTo &pts)
Definition: FSMPTA.cpp:125
bool recordAddingEdge(NodeID id1, NodeID id2, PointsTo pts)
Definition: FSMPTA.cpp:103
virtual void buildSVFG()
Re-write create SVFG method.
Definition: FSMPTA.cpp:47
Set< const SVFGNode * > SVFGNodeSet
Definition: FSMPTA.h:104
void collectLoadStoreSVFGNodes()
Collect all loads/stores SVFGNodes.
Definition: FSMPTA.cpp:64
virtual ~MTASVFGBuilder()
Destructor.
Definition: FSMPTA.h:117
void readPrecision()
For o, n2-o->n1, n1 and n2 are write. Foreach n3:n1->n3, n2->n3; then remove n2->n1.
Definition: FSMPTA.cpp:658
bool recordEdge(NodeID id1, NodeID id2, PointsTo pts)
Record edges.
Definition: FSMPTA.cpp:88
PointerAnalysis::CallEdgeMap CallEdgeMap
Definition: FSMPTA.h:102
bool isHeadofSpan(const StmtSVFGNode *n, LockAnalysis::LockSpan lspan)
whether is a first write in the lock span.
Definition: FSMPTA.cpp:201
PointerAnalysis::CallSiteSet CallSiteSet
Definition: FSMPTA.h:101
SVFGNodeSet ldnodeSet
Definition: FSMPTA.h:167
SVFGNodeIDSet getSuccNodes(const StmtSVFGNode *n)
Definition: FSMPTA.cpp:382
MHP * mhp
MHP class.
Definition: FSMPTA.h:170
SVFGNodeSet stnodeSet
all stores/loads SVFGNodes
Definition: FSMPTA.h:166
SVFGNodeIDSet getPrevNodes(const StmtSVFGNode *n)
Definition: FSMPTA.cpp:344
static const u32_t ADDEDGE_NOEDGE
Definition: FSMPTA.h:187
static const u32_t ADDEDGE_ALLOPT
Definition: FSMPTA.h:189
static const u32_t ADDEDGE_NOLOCK
Definition: FSMPTA.h:192
void handleStoreLoadNonSparse(const StmtSVFGNode *n1, const StmtSVFGNode *n2, PointerAnalysis *pta)
Definition: FSMPTA.cpp:465
void handleStoreStoreNonSparse(const StmtSVFGNode *n1, const StmtSVFGNode *n2, PointerAnalysis *pta)
Definition: FSMPTA.cpp:475
bool recordRemovingEdge(NodeID id1, NodeID id2, PointsTo pts)
Definition: FSMPTA.cpp:108
static const u32_t ADDEDGE_NOALIAS
Definition: FSMPTA.h:191
PairToBoolMap pairtailmap
Definition: FSMPTA.h:184
void handleStoreLoadWithLockPrecisely(const StmtSVFGNode *n1, const StmtSVFGNode *n2, PointerAnalysis *pta)
Definition: FSMPTA.cpp:547
Set< const SVFInstruction * > InstSet
Definition: FSMPTA.h:107
bool isTailofSpan(const StmtSVFGNode *n, LockAnalysis::LockSpan lspan)
whether is a last write in the lock span.
Definition: FSMPTA.cpp:285
Map< const StmtSVFGNode *, SVFGNodeIDSet > succset
Definition: FSMPTA.h:178
Map< const StmtSVFGNode *, bool > headmap
Definition: FSMPTA.h:180
Map< const StmtSVFGNode *, bool > tailmap
Definition: FSMPTA.h:181
static const u32_t ADDEDGE_NORP
Definition: FSMPTA.h:193
void mergeSpan(NodeBS comlocks, InstSet &res)
Definition: FSMPTA.cpp:647
MTASVFGBuilder(MHP *m, LockAnalysis *la)
Constructor.
Definition: FSMPTA.h:112
std::pair< NodeID, NodeID > NodeIDPair
Definition: FSMPTA.h:108
Map< SVFGNodeLockSpan, bool > PairToBoolMap
Definition: FSMPTA.h:109
PairToBoolMap pairheadmap
Definition: FSMPTA.h:183
SVFIR * getPAG() const
OrderedMap< const CallICFGNode *, FunctionSet > CallEdgeMap
Set< const CallICFGNode * > CallSiteSet
Indirect call edges type, map a callsite to a set of callees.
Set< const SVFFunction * > FunctionSet
SVFGNodeLockSpan(const StmtSVFGNode *SVFGnode, LockAnalysis::LockSpan lockspan)
Definition: FSMPTA.h:54
const LockAnalysis::LockSpan getLockSpan() const
Definition: FSMPTA.h:85
bool operator<(const SVFGNodeLockSpan &rhs) const
Definition: FSMPTA.h:58
LockAnalysis::LockSpan lockSpan
Definition: FSMPTA.h:91
bool operator==(const SVFGNodeLockSpan &rhs) const
Definition: FSMPTA.h:73
const StmtSVFGNode * getSVFGNode() const
Definition: FSMPTA.h:81
const StmtSVFGNode * SVFGNode
Definition: FSMPTA.h:90
virtual ~SVFGNodeLockSpan()
Definition: FSMPTA.h:56
bool operator!=(const SVFGNodeLockSpan &rhs) const
Definition: FSMPTA.h:77
SVFGNodeLockSpan & operator=(const SVFGNodeLockSpan &rhs)
Definition: FSMPTA.h:64
PointerAnalysis * getPTA() const
Get PTA.
Definition: TCT.h:187
for isBitcode
Definition: BasicTypes.h:68
unsigned NodeID
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
unsigned u32_t
Definition: GeneralType.h:46
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96
size_t operator()(const SVF::SVFGNodeLockSpan &cs) const
Definition: FSMPTA.h:262